1 /*
2  *  Copyright 2011 The WebRTC project authors. All Rights Reserved.
3  *
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.
9  */
10 
11 #include "pc/media_stream.h"
12 
13 #include <stddef.h>
14 
15 #include <string>
16 
17 #include "pc/audio_track.h"
18 #include "pc/test/fake_video_track_source.h"
19 #include "pc/video_track.h"
20 #include "rtc_base/thread.h"
21 #include "test/gmock.h"
22 #include "test/gtest.h"
23 
24 static const char kStreamId1[] = "local_stream_1";
25 static const char kVideoTrackId[] = "dummy_video_cam_1";
26 static const char kAudioTrackId[] = "dummy_microphone_1";
27 
28 using rtc::scoped_refptr;
29 using ::testing::Exactly;
30 
31 namespace webrtc {
32 
33 // Helper class to test Observer.
34 class MockObserver : public ObserverInterface {
35  public:
MockObserver(NotifierInterface * notifier)36   explicit MockObserver(NotifierInterface* notifier) : notifier_(notifier) {
37     notifier_->RegisterObserver(this);
38   }
39 
~MockObserver()40   ~MockObserver() { Unregister(); }
41 
Unregister()42   void Unregister() {
43     if (notifier_) {
44       notifier_->UnregisterObserver(this);
45       notifier_ = nullptr;
46     }
47   }
48 
49   MOCK_METHOD(void, OnChanged, (), (override));
50 
51  private:
52   NotifierInterface* notifier_;
53 };
54 
55 class MediaStreamTest : public ::testing::Test {
56  protected:
SetUp()57   virtual void SetUp() {
58     stream_ = MediaStream::Create(kStreamId1);
59     ASSERT_TRUE(stream_.get() != NULL);
60 
61     video_track_ = VideoTrack::Create(
62         kVideoTrackId, FakeVideoTrackSource::Create(), rtc::Thread::Current());
63     ASSERT_TRUE(video_track_.get() != NULL);
64     EXPECT_EQ(MediaStreamTrackInterface::kLive, video_track_->state());
65 
66     audio_track_ = AudioTrack::Create(kAudioTrackId, NULL);
67 
68     ASSERT_TRUE(audio_track_.get() != NULL);
69     EXPECT_EQ(MediaStreamTrackInterface::kLive, audio_track_->state());
70 
71     EXPECT_TRUE(stream_->AddTrack(video_track_));
72     EXPECT_FALSE(stream_->AddTrack(video_track_));
73     EXPECT_TRUE(stream_->AddTrack(audio_track_));
74     EXPECT_FALSE(stream_->AddTrack(audio_track_));
75   }
76 
ChangeTrack(MediaStreamTrackInterface * track)77   void ChangeTrack(MediaStreamTrackInterface* track) {
78     MockObserver observer(track);
79 
80     EXPECT_CALL(observer, OnChanged()).Times(Exactly(1));
81     track->set_enabled(false);
82     EXPECT_FALSE(track->enabled());
83   }
84 
85   scoped_refptr<MediaStreamInterface> stream_;
86   scoped_refptr<AudioTrackInterface> audio_track_;
87   scoped_refptr<VideoTrackInterface> video_track_;
88 };
89 
TEST_F(MediaStreamTest,GetTrackInfo)90 TEST_F(MediaStreamTest, GetTrackInfo) {
91   ASSERT_EQ(1u, stream_->GetVideoTracks().size());
92   ASSERT_EQ(1u, stream_->GetAudioTracks().size());
93 
94   // Verify the video track.
95   scoped_refptr<webrtc::MediaStreamTrackInterface> video_track(
96       stream_->GetVideoTracks()[0]);
97   EXPECT_EQ(0, video_track->id().compare(kVideoTrackId));
98   EXPECT_TRUE(video_track->enabled());
99 
100   ASSERT_EQ(1u, stream_->GetVideoTracks().size());
101   EXPECT_TRUE(stream_->GetVideoTracks()[0].get() == video_track.get());
102   EXPECT_TRUE(stream_->FindVideoTrack(video_track->id()).get() ==
103               video_track.get());
104   video_track = stream_->GetVideoTracks()[0];
105   EXPECT_EQ(0, video_track->id().compare(kVideoTrackId));
106   EXPECT_TRUE(video_track->enabled());
107 
108   // Verify the audio track.
109   scoped_refptr<webrtc::MediaStreamTrackInterface> audio_track(
110       stream_->GetAudioTracks()[0]);
111   EXPECT_EQ(0, audio_track->id().compare(kAudioTrackId));
112   EXPECT_TRUE(audio_track->enabled());
113   ASSERT_EQ(1u, stream_->GetAudioTracks().size());
114   EXPECT_TRUE(stream_->GetAudioTracks()[0].get() == audio_track.get());
115   EXPECT_TRUE(stream_->FindAudioTrack(audio_track->id()).get() ==
116               audio_track.get());
117   audio_track = stream_->GetAudioTracks()[0];
118   EXPECT_EQ(0, audio_track->id().compare(kAudioTrackId));
119   EXPECT_TRUE(audio_track->enabled());
120 }
121 
TEST_F(MediaStreamTest,RemoveTrack)122 TEST_F(MediaStreamTest, RemoveTrack) {
123   MockObserver observer(stream_);
124 
125   EXPECT_CALL(observer, OnChanged()).Times(Exactly(2));
126 
127   EXPECT_TRUE(stream_->RemoveTrack(audio_track_));
128   EXPECT_FALSE(stream_->RemoveTrack(audio_track_));
129   EXPECT_EQ(0u, stream_->GetAudioTracks().size());
130   EXPECT_EQ(0u, stream_->GetAudioTracks().size());
131 
132   EXPECT_TRUE(stream_->RemoveTrack(video_track_));
133   EXPECT_FALSE(stream_->RemoveTrack(video_track_));
134 
135   EXPECT_EQ(0u, stream_->GetVideoTracks().size());
136   EXPECT_EQ(0u, stream_->GetVideoTracks().size());
137 
138   EXPECT_FALSE(stream_->RemoveTrack(static_cast<AudioTrackInterface*>(NULL)));
139   EXPECT_FALSE(stream_->RemoveTrack(static_cast<VideoTrackInterface*>(NULL)));
140 }
141 
TEST_F(MediaStreamTest,ChangeVideoTrack)142 TEST_F(MediaStreamTest, ChangeVideoTrack) {
143   scoped_refptr<webrtc::VideoTrackInterface> video_track(
144       stream_->GetVideoTracks()[0]);
145   ChangeTrack(video_track.get());
146 }
147 
TEST_F(MediaStreamTest,ChangeAudioTrack)148 TEST_F(MediaStreamTest, ChangeAudioTrack) {
149   scoped_refptr<webrtc::AudioTrackInterface> audio_track(
150       stream_->GetAudioTracks()[0]);
151   ChangeTrack(audio_track.get());
152 }
153 
154 }  // namespace webrtc
155