henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1 | /* |
kjellander | b24317b | 2016-02-10 07:54:43 -0800 | [diff] [blame] | 2 | * Copyright 2011 The WebRTC project authors. All Rights Reserved. |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 3 | * |
kjellander | b24317b | 2016-02-10 07:54:43 -0800 | [diff] [blame] | 4 | * Use of this source code is governed by a BSD-style license |
| 5 | * that can be found in the LICENSE file in the root of the source |
| 6 | * tree. An additional intellectual property rights grant can be found |
| 7 | * in the file PATENTS. All contributing project authors may |
| 8 | * be found in the AUTHORS file in the root of the source tree. |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 9 | */ |
| 10 | |
Yves Gerey | 3e70781 | 2018-11-28 16:47:49 +0100 | [diff] [blame] | 11 | #include <stddef.h> |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 12 | #include <string> |
| 13 | |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame] | 14 | #include "pc/audio_track.h" |
| 15 | #include "pc/media_stream.h" |
| 16 | #include "pc/test/fake_video_track_source.h" |
| 17 | #include "pc/video_track.h" |
Yves Gerey | 3e70781 | 2018-11-28 16:47:49 +0100 | [diff] [blame] | 18 | #include "rtc_base/thread.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 19 | #include "test/gmock.h" |
| 20 | #include "test/gtest.h" |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 21 | |
Seth Hampson | 845e878 | 2018-03-02 11:34:10 -0800 | [diff] [blame] | 22 | static const char kStreamId1[] = "local_stream_1"; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 23 | static const char kVideoTrackId[] = "dummy_video_cam_1"; |
| 24 | static const char kAudioTrackId[] = "dummy_microphone_1"; |
| 25 | |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 26 | using rtc::scoped_refptr; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 27 | using ::testing::Exactly; |
| 28 | |
| 29 | namespace webrtc { |
| 30 | |
| 31 | // Helper class to test Observer. |
| 32 | class MockObserver : public ObserverInterface { |
| 33 | public: |
tommi | 6eca7e3 | 2015-12-15 04:27:11 -0800 | [diff] [blame] | 34 | explicit MockObserver(NotifierInterface* notifier) : notifier_(notifier) { |
| 35 | notifier_->RegisterObserver(this); |
| 36 | } |
| 37 | |
| 38 | ~MockObserver() { Unregister(); } |
| 39 | |
| 40 | void Unregister() { |
| 41 | if (notifier_) { |
| 42 | notifier_->UnregisterObserver(this); |
| 43 | notifier_ = nullptr; |
| 44 | } |
| 45 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 46 | |
| 47 | MOCK_METHOD0(OnChanged, void()); |
tommi | 6eca7e3 | 2015-12-15 04:27:11 -0800 | [diff] [blame] | 48 | |
| 49 | private: |
| 50 | NotifierInterface* notifier_; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 51 | }; |
| 52 | |
Mirko Bonadei | 6a489f2 | 2019-04-09 15:11:12 +0200 | [diff] [blame] | 53 | class MediaStreamTest : public ::testing::Test { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 54 | protected: |
| 55 | virtual void SetUp() { |
Seth Hampson | 845e878 | 2018-03-02 11:34:10 -0800 | [diff] [blame] | 56 | stream_ = MediaStream::Create(kStreamId1); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 57 | ASSERT_TRUE(stream_.get() != NULL); |
| 58 | |
perkj | 773be36 | 2017-07-31 23:22:01 -0700 | [diff] [blame] | 59 | video_track_ = VideoTrack::Create( |
| 60 | kVideoTrackId, FakeVideoTrackSource::Create(), rtc::Thread::Current()); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 61 | ASSERT_TRUE(video_track_.get() != NULL); |
perkj | c8f952d | 2016-03-23 00:33:56 -0700 | [diff] [blame] | 62 | EXPECT_EQ(MediaStreamTrackInterface::kLive, video_track_->state()); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 63 | |
| 64 | audio_track_ = AudioTrack::Create(kAudioTrackId, NULL); |
| 65 | |
| 66 | ASSERT_TRUE(audio_track_.get() != NULL); |
perkj | c8f952d | 2016-03-23 00:33:56 -0700 | [diff] [blame] | 67 | EXPECT_EQ(MediaStreamTrackInterface::kLive, audio_track_->state()); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 68 | |
| 69 | EXPECT_TRUE(stream_->AddTrack(video_track_)); |
| 70 | EXPECT_FALSE(stream_->AddTrack(video_track_)); |
| 71 | EXPECT_TRUE(stream_->AddTrack(audio_track_)); |
| 72 | EXPECT_FALSE(stream_->AddTrack(audio_track_)); |
| 73 | } |
| 74 | |
| 75 | void ChangeTrack(MediaStreamTrackInterface* track) { |
tommi | 6eca7e3 | 2015-12-15 04:27:11 -0800 | [diff] [blame] | 76 | MockObserver observer(track); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 77 | |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 78 | EXPECT_CALL(observer, OnChanged()).Times(Exactly(1)); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 79 | track->set_enabled(false); |
| 80 | EXPECT_FALSE(track->enabled()); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 81 | } |
| 82 | |
| 83 | scoped_refptr<MediaStreamInterface> stream_; |
| 84 | scoped_refptr<AudioTrackInterface> audio_track_; |
| 85 | scoped_refptr<VideoTrackInterface> video_track_; |
| 86 | }; |
| 87 | |
| 88 | TEST_F(MediaStreamTest, GetTrackInfo) { |
| 89 | ASSERT_EQ(1u, stream_->GetVideoTracks().size()); |
| 90 | ASSERT_EQ(1u, stream_->GetAudioTracks().size()); |
| 91 | |
| 92 | // Verify the video track. |
| 93 | scoped_refptr<webrtc::MediaStreamTrackInterface> video_track( |
| 94 | stream_->GetVideoTracks()[0]); |
| 95 | EXPECT_EQ(0, video_track->id().compare(kVideoTrackId)); |
| 96 | EXPECT_TRUE(video_track->enabled()); |
| 97 | |
| 98 | ASSERT_EQ(1u, stream_->GetVideoTracks().size()); |
| 99 | EXPECT_TRUE(stream_->GetVideoTracks()[0].get() == video_track.get()); |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 100 | EXPECT_TRUE(stream_->FindVideoTrack(video_track->id()).get() == |
| 101 | video_track.get()); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 102 | video_track = stream_->GetVideoTracks()[0]; |
| 103 | EXPECT_EQ(0, video_track->id().compare(kVideoTrackId)); |
| 104 | EXPECT_TRUE(video_track->enabled()); |
| 105 | |
| 106 | // Verify the audio track. |
| 107 | scoped_refptr<webrtc::MediaStreamTrackInterface> audio_track( |
| 108 | stream_->GetAudioTracks()[0]); |
| 109 | EXPECT_EQ(0, audio_track->id().compare(kAudioTrackId)); |
| 110 | EXPECT_TRUE(audio_track->enabled()); |
| 111 | ASSERT_EQ(1u, stream_->GetAudioTracks().size()); |
| 112 | EXPECT_TRUE(stream_->GetAudioTracks()[0].get() == audio_track.get()); |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 113 | EXPECT_TRUE(stream_->FindAudioTrack(audio_track->id()).get() == |
| 114 | audio_track.get()); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 115 | audio_track = stream_->GetAudioTracks()[0]; |
| 116 | EXPECT_EQ(0, audio_track->id().compare(kAudioTrackId)); |
| 117 | EXPECT_TRUE(audio_track->enabled()); |
| 118 | } |
| 119 | |
| 120 | TEST_F(MediaStreamTest, RemoveTrack) { |
tommi | 6eca7e3 | 2015-12-15 04:27:11 -0800 | [diff] [blame] | 121 | MockObserver observer(stream_); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 122 | |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 123 | EXPECT_CALL(observer, OnChanged()).Times(Exactly(2)); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 124 | |
| 125 | EXPECT_TRUE(stream_->RemoveTrack(audio_track_)); |
| 126 | EXPECT_FALSE(stream_->RemoveTrack(audio_track_)); |
| 127 | EXPECT_EQ(0u, stream_->GetAudioTracks().size()); |
| 128 | EXPECT_EQ(0u, stream_->GetAudioTracks().size()); |
| 129 | |
| 130 | EXPECT_TRUE(stream_->RemoveTrack(video_track_)); |
| 131 | EXPECT_FALSE(stream_->RemoveTrack(video_track_)); |
| 132 | |
| 133 | EXPECT_EQ(0u, stream_->GetVideoTracks().size()); |
| 134 | EXPECT_EQ(0u, stream_->GetVideoTracks().size()); |
| 135 | |
| 136 | EXPECT_FALSE(stream_->RemoveTrack(static_cast<AudioTrackInterface*>(NULL))); |
| 137 | EXPECT_FALSE(stream_->RemoveTrack(static_cast<VideoTrackInterface*>(NULL))); |
| 138 | } |
| 139 | |
| 140 | TEST_F(MediaStreamTest, ChangeVideoTrack) { |
| 141 | scoped_refptr<webrtc::VideoTrackInterface> video_track( |
| 142 | stream_->GetVideoTracks()[0]); |
| 143 | ChangeTrack(video_track.get()); |
| 144 | } |
| 145 | |
| 146 | TEST_F(MediaStreamTest, ChangeAudioTrack) { |
| 147 | scoped_refptr<webrtc::AudioTrackInterface> audio_track( |
| 148 | stream_->GetAudioTracks()[0]); |
| 149 | ChangeTrack(audio_track.get()); |
| 150 | } |
| 151 | |
| 152 | } // namespace webrtc |