-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmain.cpp
More file actions
152 lines (124 loc) · 4.71 KB
/
Copy pathmain.cpp
File metadata and controls
152 lines (124 loc) · 4.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
/*
* Copyright 2026 LiveKit, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/// Plays subscribed room audio using PlatformAudio.
///
/// Usage:
/// PlatformAudioPlayer <ws-url> <player-token>
///
/// Or via environment variables:
/// LIVEKIT_URL, LIVEKIT_PLAYER_TOKEN
#include <atomic>
#include <chrono>
#include <csignal>
#include <cstdlib>
#include <exception>
#include <iostream>
#include <memory>
#include <string>
#include <thread>
#include "livekit/livekit.h"
using namespace livekit;
namespace {
std::atomic<bool> g_running{true};
void handleSignal(int) { g_running.store(false); }
std::string getenvOrEmpty(const char* name) {
const char* value = std::getenv(name);
return value ? std::string(value) : std::string{};
}
void printUsage() {
std::cerr << "[error] Usage: PlatformAudioPlayer <ws-url> <player-token>\n"
<< " or set LIVEKIT_URL and LIVEKIT_PLAYER_TOKEN\n";
}
class PlayerDelegate final : public RoomDelegate {
public:
void onParticipantConnected(Room&, const ParticipantConnectedEvent& event) override {
if (event.participant) {
std::cout << "[info] [platform-audio-player] Participant connected identity='" << event.participant->identity()
<< "'\n";
}
}
void onTrackSubscribed(Room&, const TrackSubscribedEvent& event) override {
if (!event.track || event.track->kind() != TrackKind::KIND_AUDIO) {
return;
}
const std::string participant_identity = event.participant ? event.participant->identity() : std::string("unknown");
const std::string publication_name = event.publication ? event.publication->name() : event.track->name();
std::cout << "[info] [platform-audio-player] Playing audio track '" << publication_name
<< "' from participant identity='" << participant_identity << "'\n";
}
void onTrackSubscriptionFailed(Room&, const TrackSubscriptionFailedEvent& event) override {
const std::string participant_identity = event.participant ? event.participant->identity() : std::string("unknown");
std::cerr << "[warn] [platform-audio-player] Audio subscription failed for participant identity='"
<< participant_identity << "' track_sid='" << event.track_sid << "'\n";
}
};
} // namespace
int main(int argc, char* argv[]) {
std::string url = getenvOrEmpty("LIVEKIT_URL");
std::string player_token = getenvOrEmpty("LIVEKIT_PLAYER_TOKEN");
if (argc >= 3) {
url = argv[1];
player_token = argv[2];
}
if (url.empty() || player_token.empty()) {
printUsage();
return 1;
}
std::signal(SIGINT, handleSignal);
#ifdef SIGTERM
std::signal(SIGTERM, handleSignal);
#endif
livekit::initialize(livekit::LogLevel::Info);
try {
PlatformAudio platform_audio;
auto playout_devices = platform_audio.playoutDevices();
std::cout << "[info] [platform-audio-player] Playout devices: " << playout_devices.size() << "\n";
for (const auto& device : playout_devices) {
std::cout << " [" << device.index << "] " << device.name << " id=" << device.id << "\n";
}
auto room = std::make_unique<Room>();
PlayerDelegate delegate;
room->setDelegate(&delegate);
RoomOptions options;
options.auto_subscribe = true;
options.dynacast = false;
if (!room->connect(url, player_token, options)) {
std::cerr << "[error] [platform-audio-player] Failed to connect\n";
room.reset();
livekit::shutdown();
return 1;
}
if (auto local_participant = room->localParticipant().lock()) {
std::cout << "[info] [platform-audio-player] Connected as identity='" << local_participant->identity()
<< "' room='" << room->roomInfo().name << "'\n";
} else {
throw std::runtime_error("unable to lock local participant");
}
std::cout << "[info] [platform-audio-player] Waiting for remote audio; Ctrl-C to exit\n";
while (g_running.load()) {
std::this_thread::sleep_for(std::chrono::milliseconds(50));
}
std::cout << "[info] [platform-audio-player] Disconnecting\n";
room->setDelegate(nullptr);
room.reset();
} catch (const std::exception& error) {
std::cerr << "[error] [platform-audio-player] " << error.what() << "\n";
livekit::shutdown();
return 1;
}
livekit::shutdown();
return 0;
}