• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * libjingle
3  * Copyright 2011 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/videotrack.h"
33 #include "testing/gmock/include/gmock/gmock.h"
34 #include "testing/gtest/include/gtest/gtest.h"
35 #include "webrtc/base/gunit.h"
36 #include "webrtc/base/refcount.h"
37 #include "webrtc/base/scoped_ptr.h"
38 
39 static const char kStreamLabel1[] = "local_stream_1";
40 static const char kVideoTrackId[] = "dummy_video_cam_1";
41 static const char kAudioTrackId[] = "dummy_microphone_1";
42 
43 using rtc::scoped_refptr;
44 using ::testing::Exactly;
45 
46 namespace webrtc {
47 
48 // Helper class to test Observer.
49 class MockObserver : public ObserverInterface {
50  public:
MockObserver(NotifierInterface * notifier)51   explicit MockObserver(NotifierInterface* notifier) : notifier_(notifier) {
52     notifier_->RegisterObserver(this);
53   }
54 
~MockObserver()55   ~MockObserver() { Unregister(); }
56 
Unregister()57   void Unregister() {
58     if (notifier_) {
59       notifier_->UnregisterObserver(this);
60       notifier_ = nullptr;
61     }
62   }
63 
64   MOCK_METHOD0(OnChanged, void());
65 
66  private:
67   NotifierInterface* notifier_;
68 };
69 
70 class MediaStreamTest: public testing::Test {
71  protected:
SetUp()72   virtual void SetUp() {
73     stream_ = MediaStream::Create(kStreamLabel1);
74     ASSERT_TRUE(stream_.get() != NULL);
75 
76     video_track_ = VideoTrack::Create(kVideoTrackId, NULL);
77     ASSERT_TRUE(video_track_.get() != NULL);
78     EXPECT_EQ(MediaStreamTrackInterface::kInitializing, video_track_->state());
79 
80     audio_track_ = AudioTrack::Create(kAudioTrackId, NULL);
81 
82     ASSERT_TRUE(audio_track_.get() != NULL);
83     EXPECT_EQ(MediaStreamTrackInterface::kInitializing, audio_track_->state());
84 
85     EXPECT_TRUE(stream_->AddTrack(video_track_));
86     EXPECT_FALSE(stream_->AddTrack(video_track_));
87     EXPECT_TRUE(stream_->AddTrack(audio_track_));
88     EXPECT_FALSE(stream_->AddTrack(audio_track_));
89   }
90 
ChangeTrack(MediaStreamTrackInterface * track)91   void ChangeTrack(MediaStreamTrackInterface* track) {
92     MockObserver observer(track);
93 
94     EXPECT_CALL(observer, OnChanged())
95         .Times(Exactly(1));
96     track->set_enabled(false);
97     EXPECT_FALSE(track->enabled());
98 
99     EXPECT_CALL(observer, OnChanged())
100         .Times(Exactly(1));
101     track->set_state(MediaStreamTrackInterface::kLive);
102     EXPECT_EQ(MediaStreamTrackInterface::kLive, track->state());
103   }
104 
105   scoped_refptr<MediaStreamInterface> stream_;
106   scoped_refptr<AudioTrackInterface> audio_track_;
107   scoped_refptr<VideoTrackInterface> video_track_;
108 };
109 
TEST_F(MediaStreamTest,GetTrackInfo)110 TEST_F(MediaStreamTest, GetTrackInfo) {
111   ASSERT_EQ(1u, stream_->GetVideoTracks().size());
112   ASSERT_EQ(1u, stream_->GetAudioTracks().size());
113 
114   // Verify the video track.
115   scoped_refptr<webrtc::MediaStreamTrackInterface> video_track(
116       stream_->GetVideoTracks()[0]);
117   EXPECT_EQ(0, video_track->id().compare(kVideoTrackId));
118   EXPECT_TRUE(video_track->enabled());
119 
120   ASSERT_EQ(1u, stream_->GetVideoTracks().size());
121   EXPECT_TRUE(stream_->GetVideoTracks()[0].get() == video_track.get());
122   EXPECT_TRUE(stream_->FindVideoTrack(video_track->id()).get()
123               == video_track.get());
124   video_track = stream_->GetVideoTracks()[0];
125   EXPECT_EQ(0, video_track->id().compare(kVideoTrackId));
126   EXPECT_TRUE(video_track->enabled());
127 
128   // Verify the audio track.
129   scoped_refptr<webrtc::MediaStreamTrackInterface> audio_track(
130       stream_->GetAudioTracks()[0]);
131   EXPECT_EQ(0, audio_track->id().compare(kAudioTrackId));
132   EXPECT_TRUE(audio_track->enabled());
133   ASSERT_EQ(1u, stream_->GetAudioTracks().size());
134   EXPECT_TRUE(stream_->GetAudioTracks()[0].get() == audio_track.get());
135   EXPECT_TRUE(stream_->FindAudioTrack(audio_track->id()).get()
136               == audio_track.get());
137   audio_track = stream_->GetAudioTracks()[0];
138   EXPECT_EQ(0, audio_track->id().compare(kAudioTrackId));
139   EXPECT_TRUE(audio_track->enabled());
140 }
141 
TEST_F(MediaStreamTest,RemoveTrack)142 TEST_F(MediaStreamTest, RemoveTrack) {
143   MockObserver observer(stream_);
144 
145   EXPECT_CALL(observer, OnChanged())
146       .Times(Exactly(2));
147 
148   EXPECT_TRUE(stream_->RemoveTrack(audio_track_));
149   EXPECT_FALSE(stream_->RemoveTrack(audio_track_));
150   EXPECT_EQ(0u, stream_->GetAudioTracks().size());
151   EXPECT_EQ(0u, stream_->GetAudioTracks().size());
152 
153   EXPECT_TRUE(stream_->RemoveTrack(video_track_));
154   EXPECT_FALSE(stream_->RemoveTrack(video_track_));
155 
156   EXPECT_EQ(0u, stream_->GetVideoTracks().size());
157   EXPECT_EQ(0u, stream_->GetVideoTracks().size());
158 
159   EXPECT_FALSE(stream_->RemoveTrack(static_cast<AudioTrackInterface*>(NULL)));
160   EXPECT_FALSE(stream_->RemoveTrack(static_cast<VideoTrackInterface*>(NULL)));
161 }
162 
TEST_F(MediaStreamTest,ChangeVideoTrack)163 TEST_F(MediaStreamTest, ChangeVideoTrack) {
164   scoped_refptr<webrtc::VideoTrackInterface> video_track(
165       stream_->GetVideoTracks()[0]);
166   ChangeTrack(video_track.get());
167 }
168 
TEST_F(MediaStreamTest,ChangeAudioTrack)169 TEST_F(MediaStreamTest, ChangeAudioTrack) {
170   scoped_refptr<webrtc::AudioTrackInterface> audio_track(
171       stream_->GetAudioTracks()[0]);
172   ChangeTrack(audio_track.get());
173 }
174 
175 }  // namespace webrtc
176