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"; |
| 50 | static const uint32 kVideoSsrc = 98; |
| 51 | static const uint32 kAudioSsrc = 99; |
| 52 | |
| 53 | namespace webrtc { |
| 54 | |
| 55 | // Helper class to test RtpSender/RtpReceiver. |
| 56 | class MockAudioProvider : public AudioProviderInterface { |
| 57 | public: |
| 58 | virtual ~MockAudioProvider() {} |
| 59 | MOCK_METHOD3(SetAudioPlayout, |
| 60 | void(uint32 ssrc, |
| 61 | bool enable, |
| 62 | cricket::AudioRenderer* renderer)); |
| 63 | MOCK_METHOD4(SetAudioSend, |
| 64 | void(uint32 ssrc, |
| 65 | bool enable, |
| 66 | const cricket::AudioOptions& options, |
| 67 | cricket::AudioRenderer* renderer)); |
| 68 | MOCK_METHOD2(SetAudioPlayoutVolume, void(uint32 ssrc, double volume)); |
| 69 | }; |
| 70 | |
| 71 | // Helper class to test RtpSender/RtpReceiver. |
| 72 | class MockVideoProvider : public VideoProviderInterface { |
| 73 | public: |
| 74 | virtual ~MockVideoProvider() {} |
| 75 | MOCK_METHOD2(SetCaptureDevice, |
| 76 | bool(uint32 ssrc, cricket::VideoCapturer* camera)); |
| 77 | MOCK_METHOD3(SetVideoPlayout, |
| 78 | void(uint32 ssrc, |
| 79 | bool enable, |
| 80 | cricket::VideoRenderer* renderer)); |
| 81 | MOCK_METHOD3(SetVideoSend, |
| 82 | void(uint32 ssrc, |
| 83 | bool enable, |
| 84 | const cricket::VideoOptions* options)); |
| 85 | }; |
| 86 | |
| 87 | class FakeVideoSource : public Notifier<VideoSourceInterface> { |
| 88 | public: |
| 89 | static rtc::scoped_refptr<FakeVideoSource> Create() { |
| 90 | return new rtc::RefCountedObject<FakeVideoSource>(); |
| 91 | } |
| 92 | virtual cricket::VideoCapturer* GetVideoCapturer() { return &fake_capturer_; } |
| 93 | virtual void Stop() {} |
| 94 | virtual void Restart() {} |
| 95 | virtual void AddSink(cricket::VideoRenderer* output) {} |
| 96 | virtual void RemoveSink(cricket::VideoRenderer* output) {} |
| 97 | virtual SourceState state() const { return state_; } |
| 98 | virtual const cricket::VideoOptions* options() const { return &options_; } |
| 99 | virtual cricket::VideoRenderer* FrameInput() { return NULL; } |
| 100 | |
| 101 | protected: |
| 102 | FakeVideoSource() : state_(kLive) {} |
| 103 | ~FakeVideoSource() {} |
| 104 | |
| 105 | private: |
| 106 | cricket::FakeVideoCapturer fake_capturer_; |
| 107 | SourceState state_; |
| 108 | cricket::VideoOptions options_; |
| 109 | }; |
| 110 | |
| 111 | class RtpSenderReceiverTest : public testing::Test { |
| 112 | public: |
| 113 | virtual void SetUp() { |
| 114 | stream_ = MediaStream::Create(kStreamLabel1); |
| 115 | rtc::scoped_refptr<VideoSourceInterface> source(FakeVideoSource::Create()); |
| 116 | video_track_ = VideoTrack::Create(kVideoTrackId, source); |
| 117 | EXPECT_TRUE(stream_->AddTrack(video_track_)); |
| 118 | } |
| 119 | |
| 120 | void CreateAudioRtpSender() { |
| 121 | audio_track_ = AudioTrack::Create(kAudioTrackId, NULL); |
| 122 | EXPECT_TRUE(stream_->AddTrack(audio_track_)); |
| 123 | EXPECT_CALL(audio_provider_, SetAudioSend(kAudioSsrc, true, _, _)); |
| 124 | audio_rtp_sender_ = new AudioRtpSender(stream_->GetAudioTracks()[0], |
| 125 | kAudioSsrc, &audio_provider_); |
| 126 | } |
| 127 | |
| 128 | void CreateVideoRtpSender() { |
| 129 | EXPECT_CALL(video_provider_, |
| 130 | SetCaptureDevice( |
| 131 | kVideoSsrc, video_track_->GetSource()->GetVideoCapturer())); |
| 132 | EXPECT_CALL(video_provider_, SetVideoSend(kVideoSsrc, true, _)); |
| 133 | video_rtp_sender_ = new VideoRtpSender(stream_->GetVideoTracks()[0], |
| 134 | kVideoSsrc, &video_provider_); |
| 135 | } |
| 136 | |
| 137 | void DestroyAudioRtpSender() { |
| 138 | EXPECT_CALL(audio_provider_, SetAudioSend(kAudioSsrc, false, _, _)) |
| 139 | .Times(1); |
| 140 | audio_rtp_sender_ = nullptr; |
| 141 | } |
| 142 | |
| 143 | void DestroyVideoRtpSender() { |
| 144 | EXPECT_CALL(video_provider_, SetCaptureDevice(kVideoSsrc, NULL)).Times(1); |
| 145 | EXPECT_CALL(video_provider_, SetVideoSend(kVideoSsrc, false, _)).Times(1); |
| 146 | video_rtp_sender_ = nullptr; |
| 147 | } |
| 148 | |
| 149 | void CreateAudioRtpReceiver() { |
| 150 | audio_track_ = |
| 151 | AudioTrack::Create(kAudioTrackId, RemoteAudioSource::Create().get()); |
| 152 | EXPECT_TRUE(stream_->AddTrack(audio_track_)); |
| 153 | EXPECT_CALL(audio_provider_, SetAudioPlayout(kAudioSsrc, true, _)); |
| 154 | audio_rtp_receiver_ = new AudioRtpReceiver(stream_->GetAudioTracks()[0], |
| 155 | kAudioSsrc, &audio_provider_); |
| 156 | } |
| 157 | |
| 158 | void CreateVideoRtpReceiver() { |
| 159 | EXPECT_CALL(video_provider_, |
| 160 | SetVideoPlayout(kVideoSsrc, true, |
| 161 | video_track_->GetSource()->FrameInput())); |
| 162 | video_rtp_receiver_ = new VideoRtpReceiver(stream_->GetVideoTracks()[0], |
| 163 | kVideoSsrc, &video_provider_); |
| 164 | } |
| 165 | |
| 166 | void DestroyAudioRtpReceiver() { |
| 167 | EXPECT_CALL(audio_provider_, SetAudioPlayout(kAudioSsrc, false, _)); |
| 168 | audio_rtp_receiver_ = nullptr; |
| 169 | } |
| 170 | |
| 171 | void DestroyVideoRtpReceiver() { |
| 172 | EXPECT_CALL(video_provider_, SetVideoPlayout(kVideoSsrc, false, NULL)); |
| 173 | video_rtp_receiver_ = nullptr; |
| 174 | } |
| 175 | |
| 176 | protected: |
| 177 | MockAudioProvider audio_provider_; |
| 178 | MockVideoProvider video_provider_; |
| 179 | rtc::scoped_refptr<AudioRtpSender> audio_rtp_sender_; |
| 180 | rtc::scoped_refptr<VideoRtpSender> video_rtp_sender_; |
| 181 | rtc::scoped_refptr<AudioRtpReceiver> audio_rtp_receiver_; |
| 182 | rtc::scoped_refptr<VideoRtpReceiver> video_rtp_receiver_; |
| 183 | rtc::scoped_refptr<MediaStreamInterface> stream_; |
| 184 | rtc::scoped_refptr<VideoTrackInterface> video_track_; |
| 185 | rtc::scoped_refptr<AudioTrackInterface> audio_track_; |
| 186 | }; |
| 187 | |
| 188 | // Test that |audio_provider_| is notified when an audio track is associated |
| 189 | // and disassociated with an AudioRtpSender. |
| 190 | TEST_F(RtpSenderReceiverTest, AddAndDestroyAudioRtpSender) { |
| 191 | CreateAudioRtpSender(); |
| 192 | DestroyAudioRtpSender(); |
| 193 | } |
| 194 | |
| 195 | // Test that |video_provider_| is notified when a video track is associated and |
| 196 | // disassociated with a VideoRtpSender. |
| 197 | TEST_F(RtpSenderReceiverTest, AddAndDestroyVideoRtpSender) { |
| 198 | CreateVideoRtpSender(); |
| 199 | DestroyVideoRtpSender(); |
| 200 | } |
| 201 | |
| 202 | // Test that |audio_provider_| is notified when a remote audio and track is |
| 203 | // associated and disassociated with an AudioRtpReceiver. |
| 204 | TEST_F(RtpSenderReceiverTest, AddAndDestroyAudioRtpReceiver) { |
| 205 | CreateAudioRtpReceiver(); |
| 206 | DestroyAudioRtpReceiver(); |
| 207 | } |
| 208 | |
| 209 | // Test that |video_provider_| is notified when a remote |
| 210 | // video track is associated and disassociated with a VideoRtpReceiver. |
| 211 | TEST_F(RtpSenderReceiverTest, AddAndDestroyVideoRtpReceiver) { |
| 212 | CreateVideoRtpReceiver(); |
| 213 | DestroyVideoRtpReceiver(); |
| 214 | } |
| 215 | |
| 216 | TEST_F(RtpSenderReceiverTest, LocalAudioTrackDisable) { |
| 217 | CreateAudioRtpSender(); |
| 218 | |
| 219 | EXPECT_CALL(audio_provider_, SetAudioSend(kAudioSsrc, false, _, _)); |
| 220 | audio_track_->set_enabled(false); |
| 221 | |
| 222 | EXPECT_CALL(audio_provider_, SetAudioSend(kAudioSsrc, true, _, _)); |
| 223 | audio_track_->set_enabled(true); |
| 224 | |
| 225 | DestroyAudioRtpSender(); |
| 226 | } |
| 227 | |
| 228 | TEST_F(RtpSenderReceiverTest, RemoteAudioTrackDisable) { |
| 229 | CreateAudioRtpReceiver(); |
| 230 | |
| 231 | EXPECT_CALL(audio_provider_, SetAudioPlayout(kAudioSsrc, false, _)); |
| 232 | audio_track_->set_enabled(false); |
| 233 | |
| 234 | EXPECT_CALL(audio_provider_, SetAudioPlayout(kAudioSsrc, true, _)); |
| 235 | audio_track_->set_enabled(true); |
| 236 | |
| 237 | DestroyAudioRtpReceiver(); |
| 238 | } |
| 239 | |
| 240 | TEST_F(RtpSenderReceiverTest, LocalVideoTrackDisable) { |
| 241 | CreateVideoRtpSender(); |
| 242 | |
| 243 | EXPECT_CALL(video_provider_, SetVideoSend(kVideoSsrc, false, _)); |
| 244 | video_track_->set_enabled(false); |
| 245 | |
| 246 | EXPECT_CALL(video_provider_, SetVideoSend(kVideoSsrc, true, _)); |
| 247 | video_track_->set_enabled(true); |
| 248 | |
| 249 | DestroyVideoRtpSender(); |
| 250 | } |
| 251 | |
| 252 | TEST_F(RtpSenderReceiverTest, RemoteVideoTrackDisable) { |
| 253 | CreateVideoRtpReceiver(); |
| 254 | |
| 255 | video_track_->set_enabled(false); |
| 256 | |
| 257 | video_track_->set_enabled(true); |
| 258 | |
| 259 | DestroyVideoRtpReceiver(); |
| 260 | } |
| 261 | |
| 262 | TEST_F(RtpSenderReceiverTest, RemoteAudioTrackSetVolume) { |
| 263 | CreateAudioRtpReceiver(); |
| 264 | |
| 265 | double volume = 0.5; |
| 266 | EXPECT_CALL(audio_provider_, SetAudioPlayoutVolume(kAudioSsrc, volume)); |
| 267 | audio_track_->GetSource()->SetVolume(volume); |
| 268 | |
| 269 | // Disable the audio track, this should prevent setting the volume. |
| 270 | EXPECT_CALL(audio_provider_, SetAudioPlayout(kAudioSsrc, false, _)); |
| 271 | audio_track_->set_enabled(false); |
| 272 | audio_track_->GetSource()->SetVolume(1.0); |
| 273 | |
| 274 | EXPECT_CALL(audio_provider_, SetAudioPlayout(kAudioSsrc, true, _)); |
| 275 | audio_track_->set_enabled(true); |
| 276 | |
| 277 | double new_volume = 0.8; |
| 278 | EXPECT_CALL(audio_provider_, SetAudioPlayoutVolume(kAudioSsrc, new_volume)); |
| 279 | audio_track_->GetSource()->SetVolume(new_volume); |
| 280 | |
| 281 | DestroyAudioRtpReceiver(); |
| 282 | } |
| 283 | |
| 284 | } // namespace webrtc |