Upstream version 9.38.198.0
[platform/framework/web/crosswalk.git] / src / content / renderer / media / media_stream_audio_source.cc
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
10 namespace content {
11
12 namespace {
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
15 int 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
23 MediaStreamAudioSource::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
34 MediaStreamAudioSource::MediaStreamAudioSource()
35     : render_view_id_(-1),
36       factory_(NULL) {
37 }
38
39 MediaStreamAudioSource::~MediaStreamAudioSource() {}
40
41 void MediaStreamAudioSource::DoStopSource() {
42   if (audio_capturer_.get())
43     audio_capturer_->Stop();
44 }
45
46 void 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_) {
52     if (!factory_->InitializeMediaStreamAudioSource(render_view_id_,
53                                                     constraints,
54                                                     this)) {
55       // The source failed to start.
56       // MediaStreamImpl rely on the |stop_callback| to be triggered when the
57       // last track is removed from the source. But in this case, the source is
58       // is not even started. So we need to fail both adding the track and
59       // 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