blob: cfd1b50e95b63266f98ad0042a2b170b38fd4a45 [file] [log] [blame]
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001/*
kjellanderb24317b2016-02-10 07:54:43 -08002 * Copyright 2011 The WebRTC project authors. All Rights Reserved.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003 *
kjellanderb24317b2016-02-10 07:54:43 -08004 * 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.org28e20752013-07-10 00:45:36 +00009 */
10
11#include <string>
12
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020013#include "pc/audiotrack.h"
14#include "pc/mediastream.h"
15#include "pc/test/fakevideotracksource.h"
16#include "pc/videotrack.h"
17#include "rtc_base/gunit.h"
18#include "rtc_base/refcount.h"
19#include "test/gmock.h"
20#include "test/gtest.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000021
22static const char kStreamLabel1[] = "local_stream_1";
23static const char kVideoTrackId[] = "dummy_video_cam_1";
24static const char kAudioTrackId[] = "dummy_microphone_1";
25
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000026using rtc::scoped_refptr;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000027using ::testing::Exactly;
28
29namespace webrtc {
30
31// Helper class to test Observer.
32class MockObserver : public ObserverInterface {
33 public:
tommi6eca7e32015-12-15 04:27:11 -080034 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.org28e20752013-07-10 00:45:36 +000046
47 MOCK_METHOD0(OnChanged, void());
tommi6eca7e32015-12-15 04:27:11 -080048
49 private:
50 NotifierInterface* notifier_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000051};
52
53class MediaStreamTest: public testing::Test {
54 protected:
55 virtual void SetUp() {
56 stream_ = MediaStream::Create(kStreamLabel1);
57 ASSERT_TRUE(stream_.get() != NULL);
58
perkj773be362017-07-31 23:22:01 -070059 video_track_ = VideoTrack::Create(
60 kVideoTrackId, FakeVideoTrackSource::Create(), rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +000061 ASSERT_TRUE(video_track_.get() != NULL);
perkjc8f952d2016-03-23 00:33:56 -070062 EXPECT_EQ(MediaStreamTrackInterface::kLive, video_track_->state());
henrike@webrtc.org28e20752013-07-10 00:45:36 +000063
64 audio_track_ = AudioTrack::Create(kAudioTrackId, NULL);
65
66 ASSERT_TRUE(audio_track_.get() != NULL);
perkjc8f952d2016-03-23 00:33:56 -070067 EXPECT_EQ(MediaStreamTrackInterface::kLive, audio_track_->state());
henrike@webrtc.org28e20752013-07-10 00:45:36 +000068
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) {
tommi6eca7e32015-12-15 04:27:11 -080076 MockObserver observer(track);
henrike@webrtc.org28e20752013-07-10 00:45:36 +000077
78 EXPECT_CALL(observer, OnChanged())
79 .Times(Exactly(1));
80 track->set_enabled(false);
81 EXPECT_FALSE(track->enabled());
henrike@webrtc.org28e20752013-07-10 00:45:36 +000082 }
83
84 scoped_refptr<MediaStreamInterface> stream_;
85 scoped_refptr<AudioTrackInterface> audio_track_;
86 scoped_refptr<VideoTrackInterface> video_track_;
87};
88
89TEST_F(MediaStreamTest, GetTrackInfo) {
90 ASSERT_EQ(1u, stream_->GetVideoTracks().size());
91 ASSERT_EQ(1u, stream_->GetAudioTracks().size());
92
93 // Verify the video track.
94 scoped_refptr<webrtc::MediaStreamTrackInterface> video_track(
95 stream_->GetVideoTracks()[0]);
96 EXPECT_EQ(0, video_track->id().compare(kVideoTrackId));
97 EXPECT_TRUE(video_track->enabled());
98
99 ASSERT_EQ(1u, stream_->GetVideoTracks().size());
100 EXPECT_TRUE(stream_->GetVideoTracks()[0].get() == video_track.get());
101 EXPECT_TRUE(stream_->FindVideoTrack(video_track->id()).get()
102 == video_track.get());
103 video_track = stream_->GetVideoTracks()[0];
104 EXPECT_EQ(0, video_track->id().compare(kVideoTrackId));
105 EXPECT_TRUE(video_track->enabled());
106
107 // Verify the audio track.
108 scoped_refptr<webrtc::MediaStreamTrackInterface> audio_track(
109 stream_->GetAudioTracks()[0]);
110 EXPECT_EQ(0, audio_track->id().compare(kAudioTrackId));
111 EXPECT_TRUE(audio_track->enabled());
112 ASSERT_EQ(1u, stream_->GetAudioTracks().size());
113 EXPECT_TRUE(stream_->GetAudioTracks()[0].get() == audio_track.get());
114 EXPECT_TRUE(stream_->FindAudioTrack(audio_track->id()).get()
115 == audio_track.get());
116 audio_track = stream_->GetAudioTracks()[0];
117 EXPECT_EQ(0, audio_track->id().compare(kAudioTrackId));
118 EXPECT_TRUE(audio_track->enabled());
119}
120
121TEST_F(MediaStreamTest, RemoveTrack) {
tommi6eca7e32015-12-15 04:27:11 -0800122 MockObserver observer(stream_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000123
124 EXPECT_CALL(observer, OnChanged())
125 .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
142TEST_F(MediaStreamTest, ChangeVideoTrack) {
143 scoped_refptr<webrtc::VideoTrackInterface> video_track(
144 stream_->GetVideoTracks()[0]);
145 ChangeTrack(video_track.get());
146}
147
148TEST_F(MediaStreamTest, ChangeAudioTrack) {
149 scoped_refptr<webrtc::AudioTrackInterface> audio_track(
150 stream_->GetAudioTracks()[0]);
151 ChangeTrack(audio_track.get());
152}
153
154} // namespace webrtc