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 | |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame] | 11 | #include "pc/media_stream_observer.h" |
Taylor Brandstetter | cf846ad | 2015-12-10 15:52:02 -0800 | [diff] [blame] | 12 | |
Yves Gerey | 3e70781 | 2018-11-28 16:47:49 +0100 | [diff] [blame] | 13 | #include <string> |
| 14 | #include <vector> |
deadbeef | eb45981 | 2015-12-15 19:24:43 -0800 | [diff] [blame] | 15 | |
Steve Anton | 64b626b | 2019-01-28 17:25:26 -0800 | [diff] [blame] | 16 | #include "absl/algorithm/container.h" |
| 17 | |
deadbeef | eb45981 | 2015-12-15 19:24:43 -0800 | [diff] [blame] | 18 | namespace webrtc { |
| 19 | |
| 20 | MediaStreamObserver::MediaStreamObserver(MediaStreamInterface* stream) |
| 21 | : stream_(stream), |
| 22 | cached_audio_tracks_(stream->GetAudioTracks()), |
| 23 | cached_video_tracks_(stream->GetVideoTracks()) { |
| 24 | stream_->RegisterObserver(this); |
| 25 | } |
| 26 | |
| 27 | MediaStreamObserver::~MediaStreamObserver() { |
| 28 | stream_->UnregisterObserver(this); |
| 29 | } |
| 30 | |
| 31 | void MediaStreamObserver::OnChanged() { |
| 32 | AudioTrackVector new_audio_tracks = stream_->GetAudioTracks(); |
| 33 | VideoTrackVector new_video_tracks = stream_->GetVideoTracks(); |
| 34 | |
| 35 | // Find removed audio tracks. |
| 36 | for (const auto& cached_track : cached_audio_tracks_) { |
Steve Anton | 64b626b | 2019-01-28 17:25:26 -0800 | [diff] [blame] | 37 | if (absl::c_none_of( |
| 38 | new_audio_tracks, |
| 39 | [cached_track](const AudioTrackVector::value_type& new_track) { |
| 40 | return new_track->id() == cached_track->id(); |
| 41 | })) { |
deadbeef | eb45981 | 2015-12-15 19:24:43 -0800 | [diff] [blame] | 42 | SignalAudioTrackRemoved(cached_track.get(), stream_); |
| 43 | } |
| 44 | } |
| 45 | |
| 46 | // Find added audio tracks. |
| 47 | for (const auto& new_track : new_audio_tracks) { |
Steve Anton | 64b626b | 2019-01-28 17:25:26 -0800 | [diff] [blame] | 48 | if (absl::c_none_of( |
| 49 | cached_audio_tracks_, |
| 50 | [new_track](const AudioTrackVector::value_type& cached_track) { |
| 51 | return new_track->id() == cached_track->id(); |
| 52 | })) { |
deadbeef | eb45981 | 2015-12-15 19:24:43 -0800 | [diff] [blame] | 53 | SignalAudioTrackAdded(new_track.get(), stream_); |
| 54 | } |
| 55 | } |
| 56 | |
| 57 | // Find removed video tracks. |
| 58 | for (const auto& cached_track : cached_video_tracks_) { |
Steve Anton | 64b626b | 2019-01-28 17:25:26 -0800 | [diff] [blame] | 59 | if (absl::c_none_of( |
| 60 | new_video_tracks, |
| 61 | [cached_track](const VideoTrackVector::value_type& new_track) { |
| 62 | return new_track->id() == cached_track->id(); |
| 63 | })) { |
deadbeef | eb45981 | 2015-12-15 19:24:43 -0800 | [diff] [blame] | 64 | SignalVideoTrackRemoved(cached_track.get(), stream_); |
| 65 | } |
| 66 | } |
| 67 | |
| 68 | // Find added video tracks. |
| 69 | for (const auto& new_track : new_video_tracks) { |
Steve Anton | 64b626b | 2019-01-28 17:25:26 -0800 | [diff] [blame] | 70 | if (absl::c_none_of( |
| 71 | cached_video_tracks_, |
| 72 | [new_track](const VideoTrackVector::value_type& cached_track) { |
| 73 | return new_track->id() == cached_track->id(); |
| 74 | })) { |
deadbeef | eb45981 | 2015-12-15 19:24:43 -0800 | [diff] [blame] | 75 | SignalVideoTrackAdded(new_track.get(), stream_); |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | cached_audio_tracks_ = new_audio_tracks; |
| 80 | cached_video_tracks_ = new_video_tracks; |
| 81 | } |
| 82 | |
| 83 | } // namespace webrtc |