Re-subimission of https://codereview.chromium.org/1041213003/
[chromium-blink-merge.git] / content / renderer / media / media_stream.cc
blob245409a73c6d9c67d76f8a4d7004828f8e6aba88
1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "content/renderer/media/media_stream.h"
7 #include "base/logging.h"
8 #include "third_party/WebKit/public/platform/WebString.h"
9 #include "third_party/libjingle/source/talk/app/webrtc/mediastreaminterface.h"
11 namespace content {
13 // static
14 MediaStream* MediaStream::GetMediaStream(
15 const blink::WebMediaStream& stream) {
16 return static_cast<MediaStream*>(stream.extraData());
19 // static
20 webrtc::MediaStreamInterface* MediaStream::GetAdapter(
21 const blink::WebMediaStream& stream) {
22 MediaStream* native_stream = GetMediaStream(stream);
23 DCHECK(native_stream);
24 return native_stream->GetWebRtcAdapter(stream);
27 MediaStream::MediaStream(const blink::WebMediaStream& stream)
28 : is_local_(true),
29 webrtc_media_stream_(NULL) {
32 MediaStream::MediaStream(webrtc::MediaStreamInterface* webrtc_stream)
33 : is_local_(false),
34 webrtc_media_stream_(webrtc_stream) {
37 MediaStream::~MediaStream() {
38 DCHECK(observers_.empty());
41 webrtc::MediaStreamInterface* MediaStream::GetWebRtcAdapter(
42 const blink::WebMediaStream& stream) {
43 DCHECK(webrtc_media_stream_.get());
44 DCHECK(thread_checker_.CalledOnValidThread());
45 return webrtc_media_stream_.get();
48 void MediaStream::AddObserver(MediaStreamObserver* observer) {
49 DCHECK(thread_checker_.CalledOnValidThread());
50 DCHECK(std::find(observers_.begin(), observers_.end(), observer) ==
51 observers_.end());
52 observers_.push_back(observer);
55 void MediaStream::RemoveObserver(MediaStreamObserver* observer) {
56 DCHECK(thread_checker_.CalledOnValidThread());
57 std::vector<MediaStreamObserver*>::iterator it =
58 std::find(observers_.begin(), observers_.end(), observer);
59 DCHECK(it != observers_.end());
60 observers_.erase(it);
63 bool MediaStream::AddTrack(const blink::WebMediaStreamTrack& track) {
64 DCHECK(thread_checker_.CalledOnValidThread());
65 for (std::vector<MediaStreamObserver*>::iterator it = observers_.begin();
66 it != observers_.end(); ++it) {
67 (*it)->TrackAdded(track);
69 return true;
72 bool MediaStream::RemoveTrack(const blink::WebMediaStreamTrack& track) {
73 DCHECK(thread_checker_.CalledOnValidThread());
74 for (std::vector<MediaStreamObserver*>::iterator it = observers_.begin();
75 it != observers_.end(); ++it) {
76 (*it)->TrackRemoved(track);
78 return true;
81 } // namespace content