Index: talk/app/webrtc/mediastreamobserver.cc |
diff --git a/talk/app/webrtc/mediastreamobserver.cc b/talk/app/webrtc/mediastreamobserver.cc |
index a8561647b2f59744f6bef8bb5dd3110854a85518..2650b9a6f79e3825bc1c7f68b341d346d0f9f416 100644 |
--- a/talk/app/webrtc/mediastreamobserver.cc |
+++ b/talk/app/webrtc/mediastreamobserver.cc |
@@ -27,4 +27,75 @@ |
#include "talk/app/webrtc/mediastreamobserver.h" |
-// This file is currently stubbed so that Chromium's build files can be updated. |
+#include <algorithm> |
+ |
+namespace webrtc { |
+ |
+MediaStreamObserver::MediaStreamObserver(MediaStreamInterface* stream) |
+ : stream_(stream), |
+ cached_audio_tracks_(stream->GetAudioTracks()), |
+ cached_video_tracks_(stream->GetVideoTracks()) { |
+ stream_->RegisterObserver(this); |
+} |
+ |
+MediaStreamObserver::~MediaStreamObserver() { |
+ stream_->UnregisterObserver(this); |
+} |
+ |
+void MediaStreamObserver::OnChanged() { |
+ AudioTrackVector new_audio_tracks = stream_->GetAudioTracks(); |
+ VideoTrackVector new_video_tracks = stream_->GetVideoTracks(); |
+ |
+ // Find removed audio tracks. |
+ for (const auto& cached_track : cached_audio_tracks_) { |
+ auto it = std::find_if( |
+ new_audio_tracks.begin(), new_audio_tracks.end(), |
+ [cached_track](const AudioTrackVector::value_type& new_track) { |
+ return new_track->id().compare(cached_track->id()) == 0; |
+ }); |
+ if (it == new_audio_tracks.end()) { |
+ SignalAudioTrackRemoved(cached_track.get(), stream_); |
+ } |
+ } |
+ |
+ // Find added audio tracks. |
+ for (const auto& new_track : new_audio_tracks) { |
+ auto it = std::find_if( |
+ cached_audio_tracks_.begin(), cached_audio_tracks_.end(), |
+ [new_track](const AudioTrackVector::value_type& cached_track) { |
+ return new_track->id().compare(cached_track->id()) == 0; |
+ }); |
+ if (it == cached_audio_tracks_.end()) { |
+ SignalAudioTrackAdded(new_track.get(), stream_); |
+ } |
+ } |
+ |
+ // Find removed video tracks. |
+ for (const auto& cached_track : cached_video_tracks_) { |
+ auto it = std::find_if( |
+ new_video_tracks.begin(), new_video_tracks.end(), |
+ [cached_track](const VideoTrackVector::value_type& new_track) { |
+ return new_track->id().compare(cached_track->id()) == 0; |
+ }); |
+ if (it == new_video_tracks.end()) { |
+ SignalVideoTrackRemoved(cached_track.get(), stream_); |
+ } |
+ } |
+ |
+ // Find added video tracks. |
+ for (const auto& new_track : new_video_tracks) { |
+ auto it = std::find_if( |
+ cached_video_tracks_.begin(), cached_video_tracks_.end(), |
+ [new_track](const VideoTrackVector::value_type& cached_track) { |
+ return new_track->id().compare(cached_track->id()) == 0; |
+ }); |
+ if (it == cached_video_tracks_.end()) { |
+ SignalVideoTrackAdded(new_track.get(), stream_); |
+ } |
+ } |
+ |
+ cached_audio_tracks_ = new_audio_tracks; |
+ cached_video_tracks_ = new_video_tracks; |
+} |
+ |
+} // namespace webrtc |