Re-subimission of https://codereview.chromium.org/1041213003/
[chromium-blink-merge.git] / content / renderer / media / media_stream_center.cc
blobf5f5e7ad6d92a344fb83a66f118a0137f97f158d
1 // Copyright (c) 2012 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_center.h"
7 #include <string>
9 #include "base/command_line.h"
10 #include "base/logging.h"
11 #include "content/common/media/media_stream_messages.h"
12 #include "content/public/common/content_switches.h"
13 #include "content/public/renderer/media_stream_audio_sink.h"
14 #include "content/public/renderer/render_thread.h"
15 #include "content/renderer/media/media_stream.h"
16 #include "content/renderer/media/media_stream_source.h"
17 #include "content/renderer/media/media_stream_video_source.h"
18 #include "content/renderer/media/media_stream_video_track.h"
19 #include "content/renderer/media/webrtc/peer_connection_dependency_factory.h"
20 #include "content/renderer/media/webrtc_local_audio_source_provider.h"
21 #include "third_party/WebKit/public/platform/WebMediaConstraints.h"
22 #include "third_party/WebKit/public/platform/WebMediaStream.h"
23 #include "third_party/WebKit/public/platform/WebMediaStreamCenterClient.h"
24 #include "third_party/WebKit/public/platform/WebMediaStreamSource.h"
25 #include "third_party/WebKit/public/platform/WebMediaStreamTrack.h"
26 #include "third_party/WebKit/public/platform/WebMediaStreamTrackSourcesRequest.h"
27 #include "third_party/WebKit/public/platform/WebSourceInfo.h"
28 #include "third_party/WebKit/public/platform/WebVector.h"
29 #include "third_party/WebKit/public/web/WebFrame.h"
31 using blink::WebFrame;
32 using blink::WebView;
34 namespace content {
36 namespace {
38 void CreateNativeAudioMediaStreamTrack(
39 const blink::WebMediaStreamTrack& track,
40 PeerConnectionDependencyFactory* factory) {
41 DCHECK(!track.extraData());
42 blink::WebMediaStreamSource source = track.source();
43 DCHECK_EQ(source.type(), blink::WebMediaStreamSource::TypeAudio);
44 factory->CreateLocalAudioTrack(track);
47 void CreateNativeVideoMediaStreamTrack(
48 const blink::WebMediaStreamTrack& track) {
49 DCHECK(track.extraData() == NULL);
50 blink::WebMediaStreamSource source = track.source();
51 DCHECK_EQ(source.type(), blink::WebMediaStreamSource::TypeVideo);
52 MediaStreamVideoSource* native_source =
53 MediaStreamVideoSource::GetVideoSource(source);
54 DCHECK(native_source);
55 blink::WebMediaStreamTrack writable_track(track);
56 // TODO(perkj): The constraints to use here should be passed from blink when
57 // a new track is created. For cloning, it should be the constraints of the
58 // cloned track and not the originating source.
59 // Also - source.constraints() returns an uninitialized constraint if the
60 // source is coming from a remote video track. See http://crbug/287805.
61 blink::WebMediaConstraints constraints = source.constraints();
62 if (constraints.isNull())
63 constraints.initialize();
64 writable_track.setExtraData(
65 new MediaStreamVideoTrack(native_source, constraints,
66 MediaStreamVideoSource::ConstraintsCallback(),
67 track.isEnabled()));
70 void CreateNativeMediaStreamTrack(const blink::WebMediaStreamTrack& track,
71 PeerConnectionDependencyFactory* factory) {
72 DCHECK(!track.isNull() && !track.extraData());
73 DCHECK(!track.source().isNull());
75 switch (track.source().type()) {
76 case blink::WebMediaStreamSource::TypeAudio:
77 CreateNativeAudioMediaStreamTrack(track, factory);
78 break;
79 case blink::WebMediaStreamSource::TypeVideo:
80 CreateNativeVideoMediaStreamTrack(track);
81 break;
85 } // namespace
87 MediaStreamCenter::MediaStreamCenter(blink::WebMediaStreamCenterClient* client,
88 PeerConnectionDependencyFactory* factory)
89 : rtc_factory_(factory) {}
91 MediaStreamCenter::~MediaStreamCenter() {}
93 void MediaStreamCenter::didCreateMediaStreamTrack(
94 const blink::WebMediaStreamTrack& track) {
95 DVLOG(1) << "MediaStreamCenter::didCreateMediaStreamTrack";
96 CreateNativeMediaStreamTrack(track, rtc_factory_);
99 void MediaStreamCenter::didEnableMediaStreamTrack(
100 const blink::WebMediaStreamTrack& track) {
101 MediaStreamTrack* native_track =
102 MediaStreamTrack::GetTrack(track);
103 if (native_track)
104 native_track->SetEnabled(true);
107 void MediaStreamCenter::didDisableMediaStreamTrack(
108 const blink::WebMediaStreamTrack& track) {
109 MediaStreamTrack* native_track =
110 MediaStreamTrack::GetTrack(track);
111 if (native_track)
112 native_track->SetEnabled(false);
115 bool MediaStreamCenter::didStopMediaStreamTrack(
116 const blink::WebMediaStreamTrack& track) {
117 DVLOG(1) << "MediaStreamCenter::didStopMediaStreamTrack";
118 MediaStreamTrack* native_track = MediaStreamTrack::GetTrack(track);
119 native_track->Stop();
120 return true;
123 blink::WebAudioSourceProvider*
124 MediaStreamCenter::createWebAudioSourceFromMediaStreamTrack(
125 const blink::WebMediaStreamTrack& track) {
126 DVLOG(1) << "MediaStreamCenter::createWebAudioSourceFromMediaStreamTrack";
127 MediaStreamTrack* media_stream_track =
128 static_cast<MediaStreamTrack*>(track.extraData());
129 // Only local audio track is supported now.
130 // TODO(xians): Support remote audio track.
131 if (!media_stream_track || !media_stream_track->is_local_track()) {
132 NOTIMPLEMENTED();
133 return NULL;
136 blink::WebMediaStreamSource source = track.source();
137 DCHECK_EQ(source.type(), blink::WebMediaStreamSource::TypeAudio);
138 WebRtcLocalAudioSourceProvider* source_provider =
139 new WebRtcLocalAudioSourceProvider(track);
140 return source_provider;
143 void MediaStreamCenter::didStopLocalMediaStream(
144 const blink::WebMediaStream& stream) {
145 DVLOG(1) << "MediaStreamCenter::didStopLocalMediaStream";
146 MediaStream* native_stream = MediaStream::GetMediaStream(stream);
147 if (!native_stream) {
148 NOTREACHED();
149 return;
152 // TODO(perkj): MediaStream::Stop is being deprecated. But for the moment we
153 // need to support both MediaStream::Stop and MediaStreamTrack::Stop.
154 blink::WebVector<blink::WebMediaStreamTrack> audio_tracks;
155 stream.audioTracks(audio_tracks);
156 for (size_t i = 0; i < audio_tracks.size(); ++i)
157 didStopMediaStreamTrack(audio_tracks[i]);
159 blink::WebVector<blink::WebMediaStreamTrack> video_tracks;
160 stream.videoTracks(video_tracks);
161 for (size_t i = 0; i < video_tracks.size(); ++i)
162 didStopMediaStreamTrack(video_tracks[i]);
165 void MediaStreamCenter::didCreateMediaStream(blink::WebMediaStream& stream) {
166 DVLOG(1) << "MediaStreamCenter::didCreateMediaStream";
167 blink::WebMediaStream writable_stream(stream);
168 MediaStream* native_stream(
169 new MediaStream(stream));
170 writable_stream.setExtraData(native_stream);
172 blink::WebVector<blink::WebMediaStreamTrack> video_tracks;
173 stream.videoTracks(video_tracks);
174 for (size_t i = 0; i < video_tracks.size(); ++i) {
175 if (!MediaStreamTrack::GetTrack(video_tracks[i]))
176 CreateNativeMediaStreamTrack(video_tracks[i], rtc_factory_);
180 bool MediaStreamCenter::didAddMediaStreamTrack(
181 const blink::WebMediaStream& stream,
182 const blink::WebMediaStreamTrack& track) {
183 DVLOG(1) << "MediaStreamCenter::didAddMediaStreamTrack";
184 MediaStream* native_stream = MediaStream::GetMediaStream(stream);
185 return native_stream->AddTrack(track);
188 bool MediaStreamCenter::didRemoveMediaStreamTrack(
189 const blink::WebMediaStream& stream,
190 const blink::WebMediaStreamTrack& track) {
191 DVLOG(1) << "MediaStreamCenter::didRemoveMediaStreamTrack";
192 MediaStream* native_stream = MediaStream::GetMediaStream(stream);
193 return native_stream->RemoveTrack(track);
196 } // namespace content