media_stream_audio_source.cc revision 1320f92c476a1ad9d19dba2a48c72b75566198e9
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. 4 5#include "content/renderer/media/media_stream_audio_source.h" 6 7#include "content/renderer/render_frame_impl.h" 8#include "content/renderer/render_view_impl.h" 9 10namespace content { 11 12namespace { 13// TODO(miu): This is a temporary hack until the Chrome audio vertical is 14// migrated for the Cross-Site Isolation project. http://crbug.com/392596 15int ToRenderViewId(int render_frame_id) { 16 RenderFrameImpl* const frame = 17 RenderFrameImpl::FromRoutingID(render_frame_id); 18 RenderViewImpl* const view = frame ? frame->render_view() : NULL; 19 return view ? view->GetRoutingID() : -1; 20} 21} // namespace 22 23MediaStreamAudioSource::MediaStreamAudioSource( 24 int render_frame_id, 25 const StreamDeviceInfo& device_info, 26 const SourceStoppedCallback& stop_callback, 27 PeerConnectionDependencyFactory* factory) 28 : render_view_id_(ToRenderViewId(render_frame_id)), 29 factory_(factory) { 30 SetDeviceInfo(device_info); 31 SetStopCallback(stop_callback); 32} 33 34MediaStreamAudioSource::MediaStreamAudioSource() 35 : render_view_id_(-1), 36 factory_(NULL) { 37} 38 39MediaStreamAudioSource::~MediaStreamAudioSource() {} 40 41void MediaStreamAudioSource::DoStopSource() { 42 if (audio_capturer_.get()) 43 audio_capturer_->Stop(); 44} 45 46void MediaStreamAudioSource::AddTrack( 47 const blink::WebMediaStreamTrack& track, 48 const blink::WebMediaConstraints& constraints, 49 const ConstraintsCallback& callback) { 50 // TODO(xians): Properly implement for audio sources. 51 if (!local_audio_source_.get()) { 52 if (!factory_->InitializeMediaStreamAudioSource(render_view_id_, 53 constraints, 54 this)) { 55 // The source failed to start. 56 // UserMediaClientImpl rely on the |stop_callback| to be triggered when 57 // the last track is removed from the source. But in this case, the 58 // source is is not even started. So we need to fail both adding the 59 // track and trigger |stop_callback|. 60 callback.Run(this, MEDIA_DEVICE_TRACK_START_FAILURE, ""); 61 StopSource(); 62 return; 63 } 64 } 65 66 factory_->CreateLocalAudioTrack(track); 67 callback.Run(this, MEDIA_DEVICE_OK, ""); 68} 69 70} // namespace content 71