| OLD | NEW |
| (Empty) |
| 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 "talk/app/webrtc/mediastreamhandler.h" | |
| 29 | |
| 30 #include <string> | |
| 31 | |
| 32 #include "talk/app/webrtc/audiotrack.h" | |
| 33 #include "talk/app/webrtc/mediastream.h" | |
| 34 #include "talk/app/webrtc/remoteaudiosource.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 MediaStreamHandler. | |
| 56 class MockAudioProvider : public AudioProviderInterface { | |
| 57 public: | |
| 58 virtual ~MockAudioProvider() {} | |
| 59 MOCK_METHOD3(SetAudioPlayout, void(uint32 ssrc, bool enable, | |
| 60 cricket::AudioRenderer* renderer)); | |
| 61 MOCK_METHOD4(SetAudioSend, void(uint32 ssrc, bool enable, | |
| 62 const cricket::AudioOptions& options, | |
| 63 cricket::AudioRenderer* renderer)); | |
| 64 MOCK_METHOD2(SetAudioPlayoutVolume, void(uint32 ssrc, double volume)); | |
| 65 }; | |
| 66 | |
| 67 // Helper class to test MediaStreamHandler. | |
| 68 class MockVideoProvider : public VideoProviderInterface { | |
| 69 public: | |
| 70 virtual ~MockVideoProvider() {} | |
| 71 MOCK_METHOD2(SetCaptureDevice, bool(uint32 ssrc, | |
| 72 cricket::VideoCapturer* camera)); | |
| 73 MOCK_METHOD3(SetVideoPlayout, void(uint32 ssrc, | |
| 74 bool enable, | |
| 75 cricket::VideoRenderer* renderer)); | |
| 76 MOCK_METHOD3(SetVideoSend, void(uint32 ssrc, bool enable, | |
| 77 const cricket::VideoOptions* options)); | |
| 78 }; | |
| 79 | |
| 80 class FakeVideoSource : public Notifier<VideoSourceInterface> { | |
| 81 public: | |
| 82 static rtc::scoped_refptr<FakeVideoSource> Create() { | |
| 83 return new rtc::RefCountedObject<FakeVideoSource>(); | |
| 84 } | |
| 85 virtual cricket::VideoCapturer* GetVideoCapturer() { | |
| 86 return &fake_capturer_; | |
| 87 } | |
| 88 virtual void Stop() {} | |
| 89 virtual void Restart() {} | |
| 90 virtual void AddSink(cricket::VideoRenderer* output) {} | |
| 91 virtual void RemoveSink(cricket::VideoRenderer* output) {} | |
| 92 virtual SourceState state() const { return state_; } | |
| 93 virtual const cricket::VideoOptions* options() const { return &options_; } | |
| 94 virtual cricket::VideoRenderer* FrameInput() { return NULL; } | |
| 95 | |
| 96 protected: | |
| 97 FakeVideoSource() : state_(kLive) {} | |
| 98 ~FakeVideoSource() {} | |
| 99 | |
| 100 private: | |
| 101 cricket::FakeVideoCapturer fake_capturer_; | |
| 102 SourceState state_; | |
| 103 cricket::VideoOptions options_; | |
| 104 }; | |
| 105 | |
| 106 class MediaStreamHandlerTest : public testing::Test { | |
| 107 public: | |
| 108 MediaStreamHandlerTest() | |
| 109 : handlers_(&audio_provider_, &video_provider_) { | |
| 110 } | |
| 111 | |
| 112 virtual void SetUp() { | |
| 113 stream_ = MediaStream::Create(kStreamLabel1); | |
| 114 rtc::scoped_refptr<VideoSourceInterface> source( | |
| 115 FakeVideoSource::Create()); | |
| 116 video_track_ = VideoTrack::Create(kVideoTrackId, source); | |
| 117 EXPECT_TRUE(stream_->AddTrack(video_track_)); | |
| 118 } | |
| 119 | |
| 120 void AddLocalAudioTrack() { | |
| 121 audio_track_ = AudioTrack::Create(kAudioTrackId, NULL); | |
| 122 EXPECT_TRUE(stream_->AddTrack(audio_track_)); | |
| 123 EXPECT_CALL(audio_provider_, SetAudioSend(kAudioSsrc, true, _, _)); | |
| 124 handlers_.AddLocalAudioTrack(stream_, stream_->GetAudioTracks()[0], | |
| 125 kAudioSsrc); | |
| 126 } | |
| 127 | |
| 128 void AddLocalVideoTrack() { | |
| 129 EXPECT_CALL(video_provider_, SetCaptureDevice( | |
| 130 kVideoSsrc, video_track_->GetSource()->GetVideoCapturer())); | |
| 131 EXPECT_CALL(video_provider_, SetVideoSend(kVideoSsrc, true, _)); | |
| 132 handlers_.AddLocalVideoTrack(stream_, stream_->GetVideoTracks()[0], | |
| 133 kVideoSsrc); | |
| 134 } | |
| 135 | |
| 136 void RemoveLocalAudioTrack() { | |
| 137 EXPECT_CALL(audio_provider_, SetAudioSend(kAudioSsrc, false, _, _)) | |
| 138 .Times(1); | |
| 139 handlers_.RemoveLocalTrack(stream_, audio_track_); | |
| 140 } | |
| 141 | |
| 142 void RemoveLocalVideoTrack() { | |
| 143 EXPECT_CALL(video_provider_, SetCaptureDevice(kVideoSsrc, NULL)) | |
| 144 .Times(1); | |
| 145 EXPECT_CALL(video_provider_, SetVideoSend(kVideoSsrc, false, _)) | |
| 146 .Times(1); | |
| 147 handlers_.RemoveLocalTrack(stream_, video_track_); | |
| 148 } | |
| 149 | |
| 150 void AddRemoteAudioTrack() { | |
| 151 audio_track_ = AudioTrack::Create(kAudioTrackId, | |
| 152 RemoteAudioSource::Create().get()); | |
| 153 EXPECT_TRUE(stream_->AddTrack(audio_track_)); | |
| 154 EXPECT_CALL(audio_provider_, SetAudioPlayout(kAudioSsrc, true, _)); | |
| 155 handlers_.AddRemoteAudioTrack(stream_, stream_->GetAudioTracks()[0], | |
| 156 kAudioSsrc); | |
| 157 } | |
| 158 | |
| 159 void AddRemoteVideoTrack() { | |
| 160 EXPECT_CALL(video_provider_, SetVideoPlayout( | |
| 161 kVideoSsrc, true, video_track_->GetSource()->FrameInput())); | |
| 162 handlers_.AddRemoteVideoTrack(stream_, stream_->GetVideoTracks()[0], | |
| 163 kVideoSsrc); | |
| 164 } | |
| 165 | |
| 166 void RemoveRemoteAudioTrack() { | |
| 167 EXPECT_CALL(audio_provider_, SetAudioPlayout(kAudioSsrc, false, _)); | |
| 168 handlers_.RemoveRemoteTrack(stream_, stream_->GetAudioTracks()[0]); | |
| 169 } | |
| 170 | |
| 171 void RemoveRemoteVideoTrack() { | |
| 172 EXPECT_CALL(video_provider_, SetVideoPlayout(kVideoSsrc, false, NULL)); | |
| 173 handlers_.RemoveRemoteTrack(stream_, stream_->GetVideoTracks()[0]); | |
| 174 } | |
| 175 | |
| 176 protected: | |
| 177 MockAudioProvider audio_provider_; | |
| 178 MockVideoProvider video_provider_; | |
| 179 MediaStreamHandlerContainer handlers_; | |
| 180 rtc::scoped_refptr<MediaStreamInterface> stream_; | |
| 181 rtc::scoped_refptr<VideoTrackInterface> video_track_; | |
| 182 rtc::scoped_refptr<AudioTrackInterface> audio_track_; | |
| 183 }; | |
| 184 | |
| 185 // Test that |audio_provider_| is notified when an audio track is associated | |
| 186 // and disassociated with a MediaStreamHandler. | |
| 187 TEST_F(MediaStreamHandlerTest, AddAndRemoveLocalAudioTrack) { | |
| 188 AddLocalAudioTrack(); | |
| 189 RemoveLocalAudioTrack(); | |
| 190 | |
| 191 handlers_.RemoveLocalStream(stream_); | |
| 192 } | |
| 193 | |
| 194 // Test that |video_provider_| is notified when a video track is associated and | |
| 195 // disassociated with a MediaStreamHandler. | |
| 196 TEST_F(MediaStreamHandlerTest, AddAndRemoveLocalVideoTrack) { | |
| 197 AddLocalVideoTrack(); | |
| 198 RemoveLocalVideoTrack(); | |
| 199 | |
| 200 handlers_.RemoveLocalStream(stream_); | |
| 201 } | |
| 202 | |
| 203 // Test that |video_provider_| and |audio_provider_| is notified when an audio | |
| 204 // and video track is disassociated with a MediaStreamHandler by calling | |
| 205 // RemoveLocalStream. | |
| 206 TEST_F(MediaStreamHandlerTest, RemoveLocalStream) { | |
| 207 AddLocalAudioTrack(); | |
| 208 AddLocalVideoTrack(); | |
| 209 | |
| 210 EXPECT_CALL(video_provider_, SetCaptureDevice(kVideoSsrc, NULL)) | |
| 211 .Times(1); | |
| 212 EXPECT_CALL(video_provider_, SetVideoSend(kVideoSsrc, false, _)) | |
| 213 .Times(1); | |
| 214 EXPECT_CALL(audio_provider_, SetAudioSend(kAudioSsrc, false, _, _)) | |
| 215 .Times(1); | |
| 216 handlers_.RemoveLocalStream(stream_); | |
| 217 } | |
| 218 | |
| 219 | |
| 220 // Test that |audio_provider_| is notified when a remote audio and track is | |
| 221 // associated and disassociated with a MediaStreamHandler. | |
| 222 TEST_F(MediaStreamHandlerTest, AddAndRemoveRemoteAudioTrack) { | |
| 223 AddRemoteAudioTrack(); | |
| 224 RemoveRemoteAudioTrack(); | |
| 225 | |
| 226 handlers_.RemoveRemoteStream(stream_); | |
| 227 } | |
| 228 | |
| 229 // Test that |video_provider_| is notified when a remote | |
| 230 // video track is associated and disassociated with a MediaStreamHandler. | |
| 231 TEST_F(MediaStreamHandlerTest, AddAndRemoveRemoteVideoTrack) { | |
| 232 AddRemoteVideoTrack(); | |
| 233 RemoveRemoteVideoTrack(); | |
| 234 | |
| 235 handlers_.RemoveRemoteStream(stream_); | |
| 236 } | |
| 237 | |
| 238 // Test that |audio_provider_| and |video_provider_| is notified when an audio | |
| 239 // and video track is disassociated with a MediaStreamHandler by calling | |
| 240 // RemoveRemoveStream. | |
| 241 TEST_F(MediaStreamHandlerTest, RemoveRemoteStream) { | |
| 242 AddRemoteAudioTrack(); | |
| 243 AddRemoteVideoTrack(); | |
| 244 | |
| 245 EXPECT_CALL(video_provider_, SetVideoPlayout(kVideoSsrc, false, NULL)) | |
| 246 .Times(1); | |
| 247 EXPECT_CALL(audio_provider_, SetAudioPlayout(kAudioSsrc, false, _)) | |
| 248 .Times(1); | |
| 249 handlers_.RemoveRemoteStream(stream_); | |
| 250 } | |
| 251 | |
| 252 TEST_F(MediaStreamHandlerTest, LocalAudioTrackDisable) { | |
| 253 AddLocalAudioTrack(); | |
| 254 | |
| 255 EXPECT_CALL(audio_provider_, SetAudioSend(kAudioSsrc, false, _, _)); | |
| 256 audio_track_->set_enabled(false); | |
| 257 | |
| 258 EXPECT_CALL(audio_provider_, SetAudioSend(kAudioSsrc, true, _, _)); | |
| 259 audio_track_->set_enabled(true); | |
| 260 | |
| 261 RemoveLocalAudioTrack(); | |
| 262 handlers_.TearDown(); | |
| 263 } | |
| 264 | |
| 265 TEST_F(MediaStreamHandlerTest, RemoteAudioTrackDisable) { | |
| 266 AddRemoteAudioTrack(); | |
| 267 | |
| 268 EXPECT_CALL(audio_provider_, SetAudioPlayout(kAudioSsrc, false, _)); | |
| 269 audio_track_->set_enabled(false); | |
| 270 | |
| 271 EXPECT_CALL(audio_provider_, SetAudioPlayout(kAudioSsrc, true, _)); | |
| 272 audio_track_->set_enabled(true); | |
| 273 | |
| 274 RemoveRemoteAudioTrack(); | |
| 275 handlers_.TearDown(); | |
| 276 } | |
| 277 | |
| 278 TEST_F(MediaStreamHandlerTest, LocalVideoTrackDisable) { | |
| 279 AddLocalVideoTrack(); | |
| 280 | |
| 281 EXPECT_CALL(video_provider_, SetVideoSend(kVideoSsrc, false, _)); | |
| 282 video_track_->set_enabled(false); | |
| 283 | |
| 284 EXPECT_CALL(video_provider_, SetVideoSend(kVideoSsrc, true, _)); | |
| 285 video_track_->set_enabled(true); | |
| 286 | |
| 287 RemoveLocalVideoTrack(); | |
| 288 handlers_.TearDown(); | |
| 289 } | |
| 290 | |
| 291 TEST_F(MediaStreamHandlerTest, RemoteVideoTrackDisable) { | |
| 292 AddRemoteVideoTrack(); | |
| 293 | |
| 294 video_track_->set_enabled(false); | |
| 295 | |
| 296 video_track_->set_enabled(true); | |
| 297 | |
| 298 RemoveRemoteVideoTrack(); | |
| 299 handlers_.TearDown(); | |
| 300 } | |
| 301 | |
| 302 TEST_F(MediaStreamHandlerTest, RemoteAudioTrackSetVolume) { | |
| 303 AddRemoteAudioTrack(); | |
| 304 | |
| 305 double volume = 0.5; | |
| 306 EXPECT_CALL(audio_provider_, SetAudioPlayoutVolume(kAudioSsrc, volume)); | |
| 307 audio_track_->GetSource()->SetVolume(volume); | |
| 308 | |
| 309 // Disable the audio track, this should prevent setting the volume. | |
| 310 EXPECT_CALL(audio_provider_, SetAudioPlayout(kAudioSsrc, false, _)); | |
| 311 audio_track_->set_enabled(false); | |
| 312 audio_track_->GetSource()->SetVolume(1.0); | |
| 313 | |
| 314 EXPECT_CALL(audio_provider_, SetAudioPlayout(kAudioSsrc, true, _)); | |
| 315 audio_track_->set_enabled(true); | |
| 316 | |
| 317 double new_volume = 0.8; | |
| 318 EXPECT_CALL(audio_provider_, SetAudioPlayoutVolume(kAudioSsrc, new_volume)); | |
| 319 audio_track_->GetSource()->SetVolume(new_volume); | |
| 320 | |
| 321 RemoveRemoteAudioTrack(); | |
| 322 handlers_.TearDown(); | |
| 323 } | |
| 324 | |
| 325 } // namespace webrtc | |
| OLD | NEW |