blob: c0344efe57765cc6b8f23ddfa0ffaa94c0676876 [file] [log] [blame]
Taylor Brandstettercf846ad2015-12-10 15:52:02 -08001/*
kjellanderb24317b2016-02-10 07:54:43 -08002 * Copyright 2015 The WebRTC project authors. All Rights Reserved.
Taylor Brandstettercf846ad2015-12-10 15:52:02 -08003 *
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.
Taylor Brandstettercf846ad2015-12-10 15:52:02 -08009 */
10
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020011#include "pc/mediastreamobserver.h"
Taylor Brandstettercf846ad2015-12-10 15:52:02 -080012
deadbeefeb459812015-12-15 19:24:43 -080013#include <algorithm>
14
15namespace webrtc {
16
17MediaStreamObserver::MediaStreamObserver(MediaStreamInterface* stream)
18 : stream_(stream),
19 cached_audio_tracks_(stream->GetAudioTracks()),
20 cached_video_tracks_(stream->GetVideoTracks()) {
21 stream_->RegisterObserver(this);
22}
23
24MediaStreamObserver::~MediaStreamObserver() {
25 stream_->UnregisterObserver(this);
26}
27
28void MediaStreamObserver::OnChanged() {
29 AudioTrackVector new_audio_tracks = stream_->GetAudioTracks();
30 VideoTrackVector new_video_tracks = stream_->GetVideoTracks();
31
32 // Find removed audio tracks.
33 for (const auto& cached_track : cached_audio_tracks_) {
34 auto it = std::find_if(
35 new_audio_tracks.begin(), new_audio_tracks.end(),
36 [cached_track](const AudioTrackVector::value_type& new_track) {
37 return new_track->id().compare(cached_track->id()) == 0;
38 });
39 if (it == new_audio_tracks.end()) {
40 SignalAudioTrackRemoved(cached_track.get(), stream_);
41 }
42 }
43
44 // Find added audio tracks.
45 for (const auto& new_track : new_audio_tracks) {
46 auto it = std::find_if(
47 cached_audio_tracks_.begin(), cached_audio_tracks_.end(),
48 [new_track](const AudioTrackVector::value_type& cached_track) {
49 return new_track->id().compare(cached_track->id()) == 0;
50 });
51 if (it == cached_audio_tracks_.end()) {
52 SignalAudioTrackAdded(new_track.get(), stream_);
53 }
54 }
55
56 // Find removed video tracks.
57 for (const auto& cached_track : cached_video_tracks_) {
58 auto it = std::find_if(
59 new_video_tracks.begin(), new_video_tracks.end(),
60 [cached_track](const VideoTrackVector::value_type& new_track) {
61 return new_track->id().compare(cached_track->id()) == 0;
62 });
63 if (it == new_video_tracks.end()) {
64 SignalVideoTrackRemoved(cached_track.get(), stream_);
65 }
66 }
67
68 // Find added video tracks.
69 for (const auto& new_track : new_video_tracks) {
70 auto it = std::find_if(
71 cached_video_tracks_.begin(), cached_video_tracks_.end(),
72 [new_track](const VideoTrackVector::value_type& cached_track) {
73 return new_track->id().compare(cached_track->id()) == 0;
74 });
75 if (it == cached_video_tracks_.end()) {
76 SignalVideoTrackAdded(new_track.get(), stream_);
77 }
78 }
79
80 cached_audio_tracks_ = new_audio_tracks;
81 cached_video_tracks_ = new_video_tracks;
82}
83
84} // namespace webrtc