Taylor Brandstetter | cf846ad | 2015-12-10 15:52:02 -0800 | [diff] [blame] | 1 | /* |
kjellander | b24317b | 2016-02-10 07:54:43 -0800 | [diff] [blame] | 2 | * Copyright 2015 The WebRTC project authors. All Rights Reserved. |
Taylor Brandstetter | cf846ad | 2015-12-10 15:52:02 -0800 | [diff] [blame] | 3 | * |
kjellander | b24317b | 2016-02-10 07:54:43 -0800 | [diff] [blame] | 4 | * 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 Brandstetter | cf846ad | 2015-12-10 15:52:02 -0800 | [diff] [blame] | 9 | */ |
| 10 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 11 | #include "pc/mediastreamobserver.h" |
Taylor Brandstetter | cf846ad | 2015-12-10 15:52:02 -0800 | [diff] [blame] | 12 | |
deadbeef | eb45981 | 2015-12-15 19:24:43 -0800 | [diff] [blame] | 13 | #include <algorithm> |
| 14 | |
| 15 | namespace webrtc { |
| 16 | |
| 17 | MediaStreamObserver::MediaStreamObserver(MediaStreamInterface* stream) |
| 18 | : stream_(stream), |
| 19 | cached_audio_tracks_(stream->GetAudioTracks()), |
| 20 | cached_video_tracks_(stream->GetVideoTracks()) { |
| 21 | stream_->RegisterObserver(this); |
| 22 | } |
| 23 | |
| 24 | MediaStreamObserver::~MediaStreamObserver() { |
| 25 | stream_->UnregisterObserver(this); |
| 26 | } |
| 27 | |
| 28 | void 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 |