1 /*
2 * libjingle
3 * Copyright 2015 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 "talk/app/webrtc/mediastreamobserver.h"
29
30 #include <algorithm>
31
32 namespace webrtc {
33
MediaStreamObserver(MediaStreamInterface * stream)34 MediaStreamObserver::MediaStreamObserver(MediaStreamInterface* stream)
35 : stream_(stream),
36 cached_audio_tracks_(stream->GetAudioTracks()),
37 cached_video_tracks_(stream->GetVideoTracks()) {
38 stream_->RegisterObserver(this);
39 }
40
~MediaStreamObserver()41 MediaStreamObserver::~MediaStreamObserver() {
42 stream_->UnregisterObserver(this);
43 }
44
OnChanged()45 void MediaStreamObserver::OnChanged() {
46 AudioTrackVector new_audio_tracks = stream_->GetAudioTracks();
47 VideoTrackVector new_video_tracks = stream_->GetVideoTracks();
48
49 // Find removed audio tracks.
50 for (const auto& cached_track : cached_audio_tracks_) {
51 auto it = std::find_if(
52 new_audio_tracks.begin(), new_audio_tracks.end(),
53 [cached_track](const AudioTrackVector::value_type& new_track) {
54 return new_track->id().compare(cached_track->id()) == 0;
55 });
56 if (it == new_audio_tracks.end()) {
57 SignalAudioTrackRemoved(cached_track.get(), stream_);
58 }
59 }
60
61 // Find added audio tracks.
62 for (const auto& new_track : new_audio_tracks) {
63 auto it = std::find_if(
64 cached_audio_tracks_.begin(), cached_audio_tracks_.end(),
65 [new_track](const AudioTrackVector::value_type& cached_track) {
66 return new_track->id().compare(cached_track->id()) == 0;
67 });
68 if (it == cached_audio_tracks_.end()) {
69 SignalAudioTrackAdded(new_track.get(), stream_);
70 }
71 }
72
73 // Find removed video tracks.
74 for (const auto& cached_track : cached_video_tracks_) {
75 auto it = std::find_if(
76 new_video_tracks.begin(), new_video_tracks.end(),
77 [cached_track](const VideoTrackVector::value_type& new_track) {
78 return new_track->id().compare(cached_track->id()) == 0;
79 });
80 if (it == new_video_tracks.end()) {
81 SignalVideoTrackRemoved(cached_track.get(), stream_);
82 }
83 }
84
85 // Find added video tracks.
86 for (const auto& new_track : new_video_tracks) {
87 auto it = std::find_if(
88 cached_video_tracks_.begin(), cached_video_tracks_.end(),
89 [new_track](const VideoTrackVector::value_type& cached_track) {
90 return new_track->id().compare(cached_track->id()) == 0;
91 });
92 if (it == cached_video_tracks_.end()) {
93 SignalVideoTrackAdded(new_track.get(), stream_);
94 }
95 }
96
97 cached_audio_tracks_ = new_audio_tracks;
98 cached_video_tracks_ = new_video_tracks;
99 }
100
101 } // namespace webrtc
102