blob: b70171dfcba3afcec1bfdaac7cd85a0733fd4266 [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
Jonas Olssona4d87372019-07-05 19:08:33 +020011#include "pc/media_stream.h"
12
Yves Gerey3e707812018-11-28 16:47:49 +010013#include <stddef.h>
Jonas Olssona4d87372019-07-05 19:08:33 +020014
henrike@webrtc.org28e20752013-07-10 00:45:36 +000015#include <string>
16
Steve Anton10542f22019-01-11 09:11:00 -080017#include "pc/audio_track.h"
Steve Anton10542f22019-01-11 09:11:00 -080018#include "pc/test/fake_video_track_source.h"
19#include "pc/video_track.h"
Yves Gerey3e707812018-11-28 16:47:49 +010020#include "rtc_base/thread.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020021#include "test/gmock.h"
22#include "test/gtest.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000023
Seth Hampson845e8782018-03-02 11:34:10 -080024static const char kStreamId1[] = "local_stream_1";
henrike@webrtc.org28e20752013-07-10 00:45:36 +000025static const char kVideoTrackId[] = "dummy_video_cam_1";
26static const char kAudioTrackId[] = "dummy_microphone_1";
27
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000028using rtc::scoped_refptr;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000029using ::testing::Exactly;
30
31namespace webrtc {
32
33// Helper class to test Observer.
34class MockObserver : public ObserverInterface {
35 public:
tommi6eca7e32015-12-15 04:27:11 -080036 explicit MockObserver(NotifierInterface* notifier) : notifier_(notifier) {
37 notifier_->RegisterObserver(this);
38 }
39
40 ~MockObserver() { Unregister(); }
41
42 void Unregister() {
43 if (notifier_) {
44 notifier_->UnregisterObserver(this);
45 notifier_ = nullptr;
46 }
47 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +000048
49 MOCK_METHOD0(OnChanged, void());
tommi6eca7e32015-12-15 04:27:11 -080050
51 private:
52 NotifierInterface* notifier_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000053};
54
Mirko Bonadei6a489f22019-04-09 15:11:12 +020055class MediaStreamTest : public ::testing::Test {
henrike@webrtc.org28e20752013-07-10 00:45:36 +000056 protected:
57 virtual void SetUp() {
Seth Hampson845e8782018-03-02 11:34:10 -080058 stream_ = MediaStream::Create(kStreamId1);
henrike@webrtc.org28e20752013-07-10 00:45:36 +000059 ASSERT_TRUE(stream_.get() != NULL);
60
perkj773be362017-07-31 23:22:01 -070061 video_track_ = VideoTrack::Create(
62 kVideoTrackId, FakeVideoTrackSource::Create(), rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +000063 ASSERT_TRUE(video_track_.get() != NULL);
perkjc8f952d2016-03-23 00:33:56 -070064 EXPECT_EQ(MediaStreamTrackInterface::kLive, video_track_->state());
henrike@webrtc.org28e20752013-07-10 00:45:36 +000065
66 audio_track_ = AudioTrack::Create(kAudioTrackId, NULL);
67
68 ASSERT_TRUE(audio_track_.get() != NULL);
perkjc8f952d2016-03-23 00:33:56 -070069 EXPECT_EQ(MediaStreamTrackInterface::kLive, audio_track_->state());
henrike@webrtc.org28e20752013-07-10 00:45:36 +000070
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
77 void ChangeTrack(MediaStreamTrackInterface* track) {
tommi6eca7e32015-12-15 04:27:11 -080078 MockObserver observer(track);
henrike@webrtc.org28e20752013-07-10 00:45:36 +000079
Yves Gerey665174f2018-06-19 15:03:05 +020080 EXPECT_CALL(observer, OnChanged()).Times(Exactly(1));
henrike@webrtc.org28e20752013-07-10 00:45:36 +000081 track->set_enabled(false);
82 EXPECT_FALSE(track->enabled());
henrike@webrtc.org28e20752013-07-10 00:45:36 +000083 }
84
85 scoped_refptr<MediaStreamInterface> stream_;
86 scoped_refptr<AudioTrackInterface> audio_track_;
87 scoped_refptr<VideoTrackInterface> video_track_;
88};
89
90TEST_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());
Yves Gerey665174f2018-06-19 15:03:05 +0200102 EXPECT_TRUE(stream_->FindVideoTrack(video_track->id()).get() ==
103 video_track.get());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000104 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());
Yves Gerey665174f2018-06-19 15:03:05 +0200115 EXPECT_TRUE(stream_->FindAudioTrack(audio_track->id()).get() ==
116 audio_track.get());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000117 audio_track = stream_->GetAudioTracks()[0];
118 EXPECT_EQ(0, audio_track->id().compare(kAudioTrackId));
119 EXPECT_TRUE(audio_track->enabled());
120}
121
122TEST_F(MediaStreamTest, RemoveTrack) {
tommi6eca7e32015-12-15 04:27:11 -0800123 MockObserver observer(stream_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000124
Yves Gerey665174f2018-06-19 15:03:05 +0200125 EXPECT_CALL(observer, OnChanged()).Times(Exactly(2));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000126
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