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