blob: b49481e114190a4e199ce614cce529aced62c69e [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
Yves Gerey3e707812018-11-28 16:47:49 +010011#include <stddef.h>
henrike@webrtc.org28e20752013-07-10 00:45:36 +000012#include <string>
13
Steve Anton10542f22019-01-11 09:11:00 -080014#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 Gerey3e707812018-11-28 16:47:49 +010018#include "rtc_base/thread.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020019#include "test/gmock.h"
20#include "test/gtest.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000021
Seth Hampson845e8782018-03-02 11:34:10 -080022static const char kStreamId1[] = "local_stream_1";
henrike@webrtc.org28e20752013-07-10 00:45:36 +000023static 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
Mirko Bonadei6a489f22019-04-09 15:11:12 +020053class MediaStreamTest : public ::testing::Test {
henrike@webrtc.org28e20752013-07-10 00:45:36 +000054 protected:
55 virtual void SetUp() {
Seth Hampson845e8782018-03-02 11:34:10 -080056 stream_ = MediaStream::Create(kStreamId1);
henrike@webrtc.org28e20752013-07-10 00:45:36 +000057 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
Yves Gerey665174f2018-06-19 15:03:05 +020078 EXPECT_CALL(observer, OnChanged()).Times(Exactly(1));
henrike@webrtc.org28e20752013-07-10 00:45:36 +000079 track->set_enabled(false);
80 EXPECT_FALSE(track->enabled());
henrike@webrtc.org28e20752013-07-10 00:45:36 +000081 }
82
83 scoped_refptr<MediaStreamInterface> stream_;
84 scoped_refptr<AudioTrackInterface> audio_track_;
85 scoped_refptr<VideoTrackInterface> video_track_;
86};
87
88TEST_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 Gerey665174f2018-06-19 15:03:05 +0200100 EXPECT_TRUE(stream_->FindVideoTrack(video_track->id()).get() ==
101 video_track.get());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000102 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 Gerey665174f2018-06-19 15:03:05 +0200113 EXPECT_TRUE(stream_->FindAudioTrack(audio_track->id()).get() ==
114 audio_track.get());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000115 audio_track = stream_->GetAudioTracks()[0];
116 EXPECT_EQ(0, audio_track->id().compare(kAudioTrackId));
117 EXPECT_TRUE(audio_track->enabled());
118}
119
120TEST_F(MediaStreamTest, RemoveTrack) {
tommi6eca7e32015-12-15 04:27:11 -0800121 MockObserver observer(stream_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000122
Yves Gerey665174f2018-06-19 15:03:05 +0200123 EXPECT_CALL(observer, OnChanged()).Times(Exactly(2));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000124
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
140TEST_F(MediaStreamTest, ChangeVideoTrack) {
141 scoped_refptr<webrtc::VideoTrackInterface> video_track(
142 stream_->GetVideoTracks()[0]);
143 ChangeTrack(video_track.get());
144}
145
146TEST_F(MediaStreamTest, ChangeAudioTrack) {
147 scoped_refptr<webrtc::AudioTrackInterface> audio_track(
148 stream_->GetAudioTracks()[0]);
149 ChangeTrack(audio_track.get());
150}
151
152} // namespace webrtc