deadbeef | 70ab1a1 | 2015-09-28 16:53:55 -0700 | [diff] [blame] | 1 | /* |
| 2 | * libjingle |
| 3 | * Copyright 2012 Google Inc. |
| 4 | * |
| 5 | * Redistribution and use in source and binary forms, with or without |
| 6 | * modification, are permitted provided that the following conditions are met: |
| 7 | * |
| 8 | * 1. Redistributions of source code must retain the above copyright notice, |
| 9 | * this list of conditions and the following disclaimer. |
| 10 | * 2. Redistributions in binary form must reproduce the above copyright notice, |
| 11 | * this list of conditions and the following disclaimer in the documentation |
| 12 | * and/or other materials provided with the distribution. |
| 13 | * 3. The name of the author may not be used to endorse or promote products |
| 14 | * derived from this software without specific prior written permission. |
| 15 | * |
| 16 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED |
| 17 | * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF |
| 18 | * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO |
| 19 | * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 20 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
| 21 | * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; |
| 22 | * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, |
| 23 | * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR |
| 24 | * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF |
| 25 | * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 26 | */ |
| 27 | |
| 28 | #include <string> |
| 29 | |
| 30 | #include "talk/app/webrtc/audiotrack.h" |
| 31 | #include "talk/app/webrtc/mediastream.h" |
| 32 | #include "talk/app/webrtc/remoteaudiosource.h" |
| 33 | #include "talk/app/webrtc/rtpreceiver.h" |
| 34 | #include "talk/app/webrtc/rtpsender.h" |
| 35 | #include "talk/app/webrtc/streamcollection.h" |
| 36 | #include "talk/app/webrtc/videosource.h" |
| 37 | #include "talk/app/webrtc/videotrack.h" |
| 38 | #include "talk/media/base/fakevideocapturer.h" |
| 39 | #include "talk/media/base/mediachannel.h" |
| 40 | #include "testing/gmock/include/gmock/gmock.h" |
| 41 | #include "testing/gtest/include/gtest/gtest.h" |
| 42 | #include "webrtc/base/gunit.h" |
| 43 | |
| 44 | using ::testing::_; |
| 45 | using ::testing::Exactly; |
| 46 | |
| 47 | static const char kStreamLabel1[] = "local_stream_1"; |
| 48 | static const char kVideoTrackId[] = "video_1"; |
| 49 | static const char kAudioTrackId[] = "audio_1"; |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 50 | static const uint32_t kVideoSsrc = 98; |
| 51 | static const uint32_t kAudioSsrc = 99; |
deadbeef | 70ab1a1 | 2015-09-28 16:53:55 -0700 | [diff] [blame] | 52 | |
| 53 | namespace webrtc { |
| 54 | |
| 55 | // Helper class to test RtpSender/RtpReceiver. |
| 56 | class MockAudioProvider : public AudioProviderInterface { |
| 57 | public: |
| 58 | virtual ~MockAudioProvider() {} |
solenberg | d4cec0d | 2015-10-09 08:55:48 -0700 | [diff] [blame^] | 59 | MOCK_METHOD2(SetAudioPlayout, |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 60 | void(uint32_t ssrc, |
solenberg | d4cec0d | 2015-10-09 08:55:48 -0700 | [diff] [blame^] | 61 | bool enable)); |
deadbeef | 70ab1a1 | 2015-09-28 16:53:55 -0700 | [diff] [blame] | 62 | MOCK_METHOD4(SetAudioSend, |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 63 | void(uint32_t ssrc, |
deadbeef | 70ab1a1 | 2015-09-28 16:53:55 -0700 | [diff] [blame] | 64 | bool enable, |
| 65 | const cricket::AudioOptions& options, |
| 66 | cricket::AudioRenderer* renderer)); |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 67 | MOCK_METHOD2(SetAudioPlayoutVolume, void(uint32_t ssrc, double volume)); |
deadbeef | 70ab1a1 | 2015-09-28 16:53:55 -0700 | [diff] [blame] | 68 | }; |
| 69 | |
| 70 | // Helper class to test RtpSender/RtpReceiver. |
| 71 | class MockVideoProvider : public VideoProviderInterface { |
| 72 | public: |
| 73 | virtual ~MockVideoProvider() {} |
| 74 | MOCK_METHOD2(SetCaptureDevice, |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 75 | bool(uint32_t ssrc, cricket::VideoCapturer* camera)); |
deadbeef | 70ab1a1 | 2015-09-28 16:53:55 -0700 | [diff] [blame] | 76 | MOCK_METHOD3(SetVideoPlayout, |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 77 | void(uint32_t ssrc, |
deadbeef | 70ab1a1 | 2015-09-28 16:53:55 -0700 | [diff] [blame] | 78 | bool enable, |
| 79 | cricket::VideoRenderer* renderer)); |
| 80 | MOCK_METHOD3(SetVideoSend, |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 81 | void(uint32_t ssrc, |
deadbeef | 70ab1a1 | 2015-09-28 16:53:55 -0700 | [diff] [blame] | 82 | bool enable, |
| 83 | const cricket::VideoOptions* options)); |
| 84 | }; |
| 85 | |
| 86 | class FakeVideoSource : public Notifier<VideoSourceInterface> { |
| 87 | public: |
| 88 | static rtc::scoped_refptr<FakeVideoSource> Create() { |
| 89 | return new rtc::RefCountedObject<FakeVideoSource>(); |
| 90 | } |
| 91 | virtual cricket::VideoCapturer* GetVideoCapturer() { return &fake_capturer_; } |
| 92 | virtual void Stop() {} |
| 93 | virtual void Restart() {} |
| 94 | virtual void AddSink(cricket::VideoRenderer* output) {} |
| 95 | virtual void RemoveSink(cricket::VideoRenderer* output) {} |
| 96 | virtual SourceState state() const { return state_; } |
| 97 | virtual const cricket::VideoOptions* options() const { return &options_; } |
| 98 | virtual cricket::VideoRenderer* FrameInput() { return NULL; } |
| 99 | |
| 100 | protected: |
| 101 | FakeVideoSource() : state_(kLive) {} |
| 102 | ~FakeVideoSource() {} |
| 103 | |
| 104 | private: |
| 105 | cricket::FakeVideoCapturer fake_capturer_; |
| 106 | SourceState state_; |
| 107 | cricket::VideoOptions options_; |
| 108 | }; |
| 109 | |
| 110 | class RtpSenderReceiverTest : public testing::Test { |
| 111 | public: |
| 112 | virtual void SetUp() { |
| 113 | stream_ = MediaStream::Create(kStreamLabel1); |
| 114 | rtc::scoped_refptr<VideoSourceInterface> source(FakeVideoSource::Create()); |
| 115 | video_track_ = VideoTrack::Create(kVideoTrackId, source); |
| 116 | EXPECT_TRUE(stream_->AddTrack(video_track_)); |
| 117 | } |
| 118 | |
| 119 | void CreateAudioRtpSender() { |
| 120 | audio_track_ = AudioTrack::Create(kAudioTrackId, NULL); |
| 121 | EXPECT_TRUE(stream_->AddTrack(audio_track_)); |
| 122 | EXPECT_CALL(audio_provider_, SetAudioSend(kAudioSsrc, true, _, _)); |
| 123 | audio_rtp_sender_ = new AudioRtpSender(stream_->GetAudioTracks()[0], |
| 124 | kAudioSsrc, &audio_provider_); |
| 125 | } |
| 126 | |
| 127 | void CreateVideoRtpSender() { |
| 128 | EXPECT_CALL(video_provider_, |
| 129 | SetCaptureDevice( |
| 130 | kVideoSsrc, video_track_->GetSource()->GetVideoCapturer())); |
| 131 | EXPECT_CALL(video_provider_, SetVideoSend(kVideoSsrc, true, _)); |
| 132 | video_rtp_sender_ = new VideoRtpSender(stream_->GetVideoTracks()[0], |
| 133 | kVideoSsrc, &video_provider_); |
| 134 | } |
| 135 | |
| 136 | void DestroyAudioRtpSender() { |
| 137 | EXPECT_CALL(audio_provider_, SetAudioSend(kAudioSsrc, false, _, _)) |
| 138 | .Times(1); |
| 139 | audio_rtp_sender_ = nullptr; |
| 140 | } |
| 141 | |
| 142 | void DestroyVideoRtpSender() { |
| 143 | EXPECT_CALL(video_provider_, SetCaptureDevice(kVideoSsrc, NULL)).Times(1); |
| 144 | EXPECT_CALL(video_provider_, SetVideoSend(kVideoSsrc, false, _)).Times(1); |
| 145 | video_rtp_sender_ = nullptr; |
| 146 | } |
| 147 | |
| 148 | void CreateAudioRtpReceiver() { |
| 149 | audio_track_ = |
| 150 | AudioTrack::Create(kAudioTrackId, RemoteAudioSource::Create().get()); |
| 151 | EXPECT_TRUE(stream_->AddTrack(audio_track_)); |
solenberg | d4cec0d | 2015-10-09 08:55:48 -0700 | [diff] [blame^] | 152 | EXPECT_CALL(audio_provider_, SetAudioPlayout(kAudioSsrc, true)); |
deadbeef | 70ab1a1 | 2015-09-28 16:53:55 -0700 | [diff] [blame] | 153 | audio_rtp_receiver_ = new AudioRtpReceiver(stream_->GetAudioTracks()[0], |
| 154 | kAudioSsrc, &audio_provider_); |
| 155 | } |
| 156 | |
| 157 | void CreateVideoRtpReceiver() { |
| 158 | EXPECT_CALL(video_provider_, |
| 159 | SetVideoPlayout(kVideoSsrc, true, |
| 160 | video_track_->GetSource()->FrameInput())); |
| 161 | video_rtp_receiver_ = new VideoRtpReceiver(stream_->GetVideoTracks()[0], |
| 162 | kVideoSsrc, &video_provider_); |
| 163 | } |
| 164 | |
| 165 | void DestroyAudioRtpReceiver() { |
solenberg | d4cec0d | 2015-10-09 08:55:48 -0700 | [diff] [blame^] | 166 | EXPECT_CALL(audio_provider_, SetAudioPlayout(kAudioSsrc, false)); |
deadbeef | 70ab1a1 | 2015-09-28 16:53:55 -0700 | [diff] [blame] | 167 | audio_rtp_receiver_ = nullptr; |
| 168 | } |
| 169 | |
| 170 | void DestroyVideoRtpReceiver() { |
| 171 | EXPECT_CALL(video_provider_, SetVideoPlayout(kVideoSsrc, false, NULL)); |
| 172 | video_rtp_receiver_ = nullptr; |
| 173 | } |
| 174 | |
| 175 | protected: |
| 176 | MockAudioProvider audio_provider_; |
| 177 | MockVideoProvider video_provider_; |
| 178 | rtc::scoped_refptr<AudioRtpSender> audio_rtp_sender_; |
| 179 | rtc::scoped_refptr<VideoRtpSender> video_rtp_sender_; |
| 180 | rtc::scoped_refptr<AudioRtpReceiver> audio_rtp_receiver_; |
| 181 | rtc::scoped_refptr<VideoRtpReceiver> video_rtp_receiver_; |
| 182 | rtc::scoped_refptr<MediaStreamInterface> stream_; |
| 183 | rtc::scoped_refptr<VideoTrackInterface> video_track_; |
| 184 | rtc::scoped_refptr<AudioTrackInterface> audio_track_; |
| 185 | }; |
| 186 | |
| 187 | // Test that |audio_provider_| is notified when an audio track is associated |
| 188 | // and disassociated with an AudioRtpSender. |
| 189 | TEST_F(RtpSenderReceiverTest, AddAndDestroyAudioRtpSender) { |
| 190 | CreateAudioRtpSender(); |
| 191 | DestroyAudioRtpSender(); |
| 192 | } |
| 193 | |
| 194 | // Test that |video_provider_| is notified when a video track is associated and |
| 195 | // disassociated with a VideoRtpSender. |
| 196 | TEST_F(RtpSenderReceiverTest, AddAndDestroyVideoRtpSender) { |
| 197 | CreateVideoRtpSender(); |
| 198 | DestroyVideoRtpSender(); |
| 199 | } |
| 200 | |
| 201 | // Test that |audio_provider_| is notified when a remote audio and track is |
| 202 | // associated and disassociated with an AudioRtpReceiver. |
| 203 | TEST_F(RtpSenderReceiverTest, AddAndDestroyAudioRtpReceiver) { |
| 204 | CreateAudioRtpReceiver(); |
| 205 | DestroyAudioRtpReceiver(); |
| 206 | } |
| 207 | |
| 208 | // Test that |video_provider_| is notified when a remote |
| 209 | // video track is associated and disassociated with a VideoRtpReceiver. |
| 210 | TEST_F(RtpSenderReceiverTest, AddAndDestroyVideoRtpReceiver) { |
| 211 | CreateVideoRtpReceiver(); |
| 212 | DestroyVideoRtpReceiver(); |
| 213 | } |
| 214 | |
| 215 | TEST_F(RtpSenderReceiverTest, LocalAudioTrackDisable) { |
| 216 | CreateAudioRtpSender(); |
| 217 | |
| 218 | EXPECT_CALL(audio_provider_, SetAudioSend(kAudioSsrc, false, _, _)); |
| 219 | audio_track_->set_enabled(false); |
| 220 | |
| 221 | EXPECT_CALL(audio_provider_, SetAudioSend(kAudioSsrc, true, _, _)); |
| 222 | audio_track_->set_enabled(true); |
| 223 | |
| 224 | DestroyAudioRtpSender(); |
| 225 | } |
| 226 | |
| 227 | TEST_F(RtpSenderReceiverTest, RemoteAudioTrackDisable) { |
| 228 | CreateAudioRtpReceiver(); |
| 229 | |
solenberg | d4cec0d | 2015-10-09 08:55:48 -0700 | [diff] [blame^] | 230 | EXPECT_CALL(audio_provider_, SetAudioPlayout(kAudioSsrc, false)); |
deadbeef | 70ab1a1 | 2015-09-28 16:53:55 -0700 | [diff] [blame] | 231 | audio_track_->set_enabled(false); |
| 232 | |
solenberg | d4cec0d | 2015-10-09 08:55:48 -0700 | [diff] [blame^] | 233 | EXPECT_CALL(audio_provider_, SetAudioPlayout(kAudioSsrc, true)); |
deadbeef | 70ab1a1 | 2015-09-28 16:53:55 -0700 | [diff] [blame] | 234 | audio_track_->set_enabled(true); |
| 235 | |
| 236 | DestroyAudioRtpReceiver(); |
| 237 | } |
| 238 | |
| 239 | TEST_F(RtpSenderReceiverTest, LocalVideoTrackDisable) { |
| 240 | CreateVideoRtpSender(); |
| 241 | |
| 242 | EXPECT_CALL(video_provider_, SetVideoSend(kVideoSsrc, false, _)); |
| 243 | video_track_->set_enabled(false); |
| 244 | |
| 245 | EXPECT_CALL(video_provider_, SetVideoSend(kVideoSsrc, true, _)); |
| 246 | video_track_->set_enabled(true); |
| 247 | |
| 248 | DestroyVideoRtpSender(); |
| 249 | } |
| 250 | |
| 251 | TEST_F(RtpSenderReceiverTest, RemoteVideoTrackDisable) { |
| 252 | CreateVideoRtpReceiver(); |
| 253 | |
| 254 | video_track_->set_enabled(false); |
| 255 | |
| 256 | video_track_->set_enabled(true); |
| 257 | |
| 258 | DestroyVideoRtpReceiver(); |
| 259 | } |
| 260 | |
| 261 | TEST_F(RtpSenderReceiverTest, RemoteAudioTrackSetVolume) { |
| 262 | CreateAudioRtpReceiver(); |
| 263 | |
| 264 | double volume = 0.5; |
| 265 | EXPECT_CALL(audio_provider_, SetAudioPlayoutVolume(kAudioSsrc, volume)); |
| 266 | audio_track_->GetSource()->SetVolume(volume); |
| 267 | |
| 268 | // Disable the audio track, this should prevent setting the volume. |
solenberg | d4cec0d | 2015-10-09 08:55:48 -0700 | [diff] [blame^] | 269 | EXPECT_CALL(audio_provider_, SetAudioPlayout(kAudioSsrc, false)); |
deadbeef | 70ab1a1 | 2015-09-28 16:53:55 -0700 | [diff] [blame] | 270 | audio_track_->set_enabled(false); |
| 271 | audio_track_->GetSource()->SetVolume(1.0); |
| 272 | |
solenberg | d4cec0d | 2015-10-09 08:55:48 -0700 | [diff] [blame^] | 273 | EXPECT_CALL(audio_provider_, SetAudioPlayout(kAudioSsrc, true)); |
deadbeef | 70ab1a1 | 2015-09-28 16:53:55 -0700 | [diff] [blame] | 274 | audio_track_->set_enabled(true); |
| 275 | |
| 276 | double new_volume = 0.8; |
| 277 | EXPECT_CALL(audio_provider_, SetAudioPlayoutVolume(kAudioSsrc, new_volume)); |
| 278 | audio_track_->GetSource()->SetVolume(new_volume); |
| 279 | |
| 280 | DestroyAudioRtpReceiver(); |
| 281 | } |
| 282 | |
| 283 | } // namespace webrtc |