|
| 1 | +/* |
| 2 | + * Copyright 2026 LiveKit |
| 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 | +#include <chrono> |
| 18 | +#include <condition_variable> |
| 19 | +#include <memory> |
| 20 | +#include <mutex> |
| 21 | +#include <set> |
| 22 | +#include <string> |
| 23 | + |
| 24 | +#include "../common/test_common.h" |
| 25 | + |
| 26 | +namespace livekit::test { |
| 27 | + |
| 28 | +using namespace std::chrono_literals; |
| 29 | + |
| 30 | +namespace { |
| 31 | + |
| 32 | +constexpr auto kSubscriptionTimeout = 20s; |
| 33 | + |
| 34 | +/// Tracks subscription/unpublish events seen by the receiver so tests can wait |
| 35 | +/// for the platform-audio track to round-trip through the SFU. |
| 36 | +struct PlatformTrackState { |
| 37 | + std::mutex mutex; |
| 38 | + std::condition_variable cv; |
| 39 | + std::set<std::string> subscribed_audio_names; |
| 40 | + std::set<std::string> unsubscribed_sids; |
| 41 | + std::set<std::string> unpublished_sids; |
| 42 | +}; |
| 43 | + |
| 44 | +class PlatformTrackCollectorDelegate : public RoomDelegate { |
| 45 | +public: |
| 46 | + explicit PlatformTrackCollectorDelegate(PlatformTrackState& state) : state_(state) {} |
| 47 | + |
| 48 | + void onTrackSubscribed(Room&, const TrackSubscribedEvent& event) override { |
| 49 | + std::lock_guard<std::mutex> lock(state_.mutex); |
| 50 | + if (event.track && event.track->kind() == TrackKind::KIND_AUDIO && event.publication) { |
| 51 | + state_.subscribed_audio_names.insert(event.publication->name()); |
| 52 | + } |
| 53 | + state_.cv.notify_all(); |
| 54 | + } |
| 55 | + |
| 56 | + void onTrackUnsubscribed(Room&, const TrackUnsubscribedEvent& event) override { |
| 57 | + std::lock_guard<std::mutex> lock(state_.mutex); |
| 58 | + if (event.track) { |
| 59 | + state_.unsubscribed_sids.insert(event.track->sid()); |
| 60 | + } |
| 61 | + state_.cv.notify_all(); |
| 62 | + } |
| 63 | + |
| 64 | + void onTrackUnpublished(Room&, const TrackUnpublishedEvent& event) override { |
| 65 | + std::lock_guard<std::mutex> lock(state_.mutex); |
| 66 | + if (event.publication) { |
| 67 | + state_.unpublished_sids.insert(event.publication->sid()); |
| 68 | + } |
| 69 | + state_.cv.notify_all(); |
| 70 | + } |
| 71 | + |
| 72 | +private: |
| 73 | + PlatformTrackState& state_; |
| 74 | +}; |
| 75 | + |
| 76 | +/// Construct a PlatformAudio, or skip the calling test when the platform Audio |
| 77 | +/// Device Module is unavailable (headless CI runners have no audio hardware). |
| 78 | +std::unique_ptr<PlatformAudio> makePlatformAudioOrSkip() { |
| 79 | + try { |
| 80 | + return std::make_unique<PlatformAudio>(); |
| 81 | + } catch (const PlatformAudioError& error) { |
| 82 | + GTEST_SKIP() << "PlatformAudio unavailable: " << error.what(); |
| 83 | + } |
| 84 | + return nullptr; |
| 85 | +} |
| 86 | + |
| 87 | +} // namespace |
| 88 | + |
| 89 | +class PlatformAudioIntegrationTest : public LiveKitTestBase {}; |
| 90 | + |
| 91 | +// Publishing a platform-ADM-backed audio track should reach a remote |
| 92 | +// participant exactly like a manually fed AudioSource track. No real |
| 93 | +// microphone is required: the source captures silence on headless runners, |
| 94 | +// but the publish/subscribe round-trip still completes. |
| 95 | +TEST_F(PlatformAudioIntegrationTest, PublishPlatformAudioTrackEndToEnd) { |
| 96 | + skipIfNotConfigured(); |
| 97 | + |
| 98 | + auto platform_audio = makePlatformAudioOrSkip(); |
| 99 | + ASSERT_NE(platform_audio, nullptr); |
| 100 | + |
| 101 | + RoomOptions options; |
| 102 | + options.auto_subscribe = true; |
| 103 | + |
| 104 | + PlatformTrackState receiver_state; |
| 105 | + PlatformTrackCollectorDelegate receiver_delegate(receiver_state); |
| 106 | + |
| 107 | + auto receiver_room = std::make_unique<Room>(); |
| 108 | + receiver_room->setDelegate(&receiver_delegate); |
| 109 | + ASSERT_TRUE(receiver_room->connect(config_.url, config_.token_b, options)) << "Receiver failed to connect"; |
| 110 | + |
| 111 | + auto sender_room = std::make_unique<Room>(); |
| 112 | + ASSERT_TRUE(sender_room->connect(config_.url, config_.token_a, options)) << "Sender failed to connect"; |
| 113 | + |
| 114 | + const auto source = platform_audio->createAudioSource(); |
| 115 | + ASSERT_NE(source, nullptr); |
| 116 | + EXPECT_NE(source->ffiHandleId(), 0u); |
| 117 | + |
| 118 | + const std::string track_name = "platform-mic"; |
| 119 | + const auto track = LocalAudioTrack::createLocalAudioTrack(track_name, source); |
| 120 | + ASSERT_NE(track, nullptr); |
| 121 | + |
| 122 | + TrackPublishOptions publish_options; |
| 123 | + publish_options.source = TrackSource::SOURCE_MICROPHONE; |
| 124 | + lockLocalParticipant(*sender_room)->publishTrack(track, publish_options); |
| 125 | + |
| 126 | + std::unique_lock<std::mutex> lock(receiver_state.mutex); |
| 127 | + const bool subscribed = receiver_state.cv.wait_for( |
| 128 | + lock, kSubscriptionTimeout, [&]() { return receiver_state.subscribed_audio_names.count(track_name) > 0; }); |
| 129 | + EXPECT_TRUE(subscribed) << "Receiver never subscribed to the platform audio track"; |
| 130 | +} |
| 131 | + |
| 132 | +// Unpublishing a platform audio track must propagate to the remote, exercising |
| 133 | +// the source/track lifecycle that keeps the shared PlatformAudioState alive. |
| 134 | +TEST_F(PlatformAudioIntegrationTest, UnpublishPlatformAudioTrackPropagates) { |
| 135 | + skipIfNotConfigured(); |
| 136 | + |
| 137 | + auto platform_audio = makePlatformAudioOrSkip(); |
| 138 | + ASSERT_NE(platform_audio, nullptr); |
| 139 | + |
| 140 | + RoomOptions options; |
| 141 | + options.auto_subscribe = true; |
| 142 | + |
| 143 | + PlatformTrackState receiver_state; |
| 144 | + PlatformTrackCollectorDelegate receiver_delegate(receiver_state); |
| 145 | + |
| 146 | + auto receiver_room = std::make_unique<Room>(); |
| 147 | + receiver_room->setDelegate(&receiver_delegate); |
| 148 | + ASSERT_TRUE(receiver_room->connect(config_.url, config_.token_b, options)) << "Receiver failed to connect"; |
| 149 | + |
| 150 | + auto sender_room = std::make_unique<Room>(); |
| 151 | + ASSERT_TRUE(sender_room->connect(config_.url, config_.token_a, options)) << "Sender failed to connect"; |
| 152 | + |
| 153 | + const auto source = platform_audio->createAudioSource(); |
| 154 | + ASSERT_NE(source, nullptr); |
| 155 | + |
| 156 | + const std::string track_name = "platform-mic-unpublish"; |
| 157 | + const auto track = LocalAudioTrack::createLocalAudioTrack(track_name, source); |
| 158 | + ASSERT_NE(track, nullptr); |
| 159 | + |
| 160 | + TrackPublishOptions publish_options; |
| 161 | + publish_options.source = TrackSource::SOURCE_MICROPHONE; |
| 162 | + lockLocalParticipant(*sender_room)->publishTrack(track, publish_options); |
| 163 | + |
| 164 | + { |
| 165 | + std::unique_lock<std::mutex> lock(receiver_state.mutex); |
| 166 | + ASSERT_TRUE(receiver_state.cv.wait_for(lock, kSubscriptionTimeout, [&]() { |
| 167 | + return receiver_state.subscribed_audio_names.count(track_name) > 0; |
| 168 | + })) << "Receiver never subscribed to the platform audio track"; |
| 169 | + } |
| 170 | + |
| 171 | + ASSERT_NE(track->publication(), nullptr); |
| 172 | + const std::string published_sid = track->publication()->sid(); |
| 173 | + lockLocalParticipant(*sender_room)->unpublishTrack(published_sid); |
| 174 | + |
| 175 | + std::unique_lock<std::mutex> lock(receiver_state.mutex); |
| 176 | + const bool removed = receiver_state.cv.wait_for(lock, kSubscriptionTimeout, [&]() { |
| 177 | + return receiver_state.unpublished_sids.count(published_sid) > 0 || |
| 178 | + receiver_state.unsubscribed_sids.count(published_sid) > 0; |
| 179 | + }); |
| 180 | + EXPECT_TRUE(removed) << "Receiver never saw the platform audio track removed"; |
| 181 | +} |
| 182 | + |
| 183 | +// A single PlatformAudio manager can vend multiple independent sources, each |
| 184 | +// with a distinct FFI handle, and both should publish end-to-end. |
| 185 | +TEST_F(PlatformAudioIntegrationTest, MultipleSourcesFromOneManagerPublish) { |
| 186 | + skipIfNotConfigured(); |
| 187 | + |
| 188 | + auto platform_audio = makePlatformAudioOrSkip(); |
| 189 | + ASSERT_NE(platform_audio, nullptr); |
| 190 | + |
| 191 | + RoomOptions options; |
| 192 | + options.auto_subscribe = true; |
| 193 | + |
| 194 | + PlatformTrackState receiver_state; |
| 195 | + PlatformTrackCollectorDelegate receiver_delegate(receiver_state); |
| 196 | + |
| 197 | + auto receiver_room = std::make_unique<Room>(); |
| 198 | + receiver_room->setDelegate(&receiver_delegate); |
| 199 | + ASSERT_TRUE(receiver_room->connect(config_.url, config_.token_b, options)) << "Receiver failed to connect"; |
| 200 | + |
| 201 | + auto sender_room = std::make_unique<Room>(); |
| 202 | + ASSERT_TRUE(sender_room->connect(config_.url, config_.token_a, options)) << "Sender failed to connect"; |
| 203 | + |
| 204 | + const auto source_a = platform_audio->createAudioSource(); |
| 205 | + const auto source_b = platform_audio->createAudioSource(); |
| 206 | + ASSERT_NE(source_a, nullptr); |
| 207 | + ASSERT_NE(source_b, nullptr); |
| 208 | + EXPECT_NE(source_a->ffiHandleId(), 0u); |
| 209 | + EXPECT_NE(source_b->ffiHandleId(), 0u); |
| 210 | + EXPECT_NE(source_a->ffiHandleId(), source_b->ffiHandleId()); |
| 211 | + |
| 212 | + const std::string name_a = "platform-mic-a"; |
| 213 | + const std::string name_b = "platform-mic-b"; |
| 214 | + const auto track_a = LocalAudioTrack::createLocalAudioTrack(name_a, source_a); |
| 215 | + const auto track_b = LocalAudioTrack::createLocalAudioTrack(name_b, source_b); |
| 216 | + ASSERT_NE(track_a, nullptr); |
| 217 | + ASSERT_NE(track_b, nullptr); |
| 218 | + |
| 219 | + TrackPublishOptions publish_options; |
| 220 | + publish_options.source = TrackSource::SOURCE_MICROPHONE; |
| 221 | + lockLocalParticipant(*sender_room)->publishTrack(track_a, publish_options); |
| 222 | + lockLocalParticipant(*sender_room)->publishTrack(track_b, publish_options); |
| 223 | + |
| 224 | + std::unique_lock<std::mutex> lock(receiver_state.mutex); |
| 225 | + const bool both_subscribed = receiver_state.cv.wait_for(lock, kSubscriptionTimeout, [&]() { |
| 226 | + return receiver_state.subscribed_audio_names.count(name_a) > 0 && |
| 227 | + receiver_state.subscribed_audio_names.count(name_b) > 0; |
| 228 | + }); |
| 229 | + EXPECT_TRUE(both_subscribed) << "Receiver did not subscribe to both platform audio tracks"; |
| 230 | +} |
| 231 | + |
| 232 | +} // namespace livekit::test |
0 commit comments