15d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// Copyright 2014 The Chromium Authors. All rights reserved.
25d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be
35d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// found in the LICENSE file.
45d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
55d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "content/renderer/media/media_stream_audio_source.h"
65d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
7116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch#include "content/renderer/render_frame_impl.h"
8116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch#include "content/renderer/render_view_impl.h"
9116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
105d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)namespace content {
115d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
12116680a4aac90f2aa7413d9095a592090648e557Ben Murdochnamespace {
13116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch// TODO(miu): This is a temporary hack until the Chrome audio vertical is
14116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch// migrated for the Cross-Site Isolation project.  http://crbug.com/392596
15116680a4aac90f2aa7413d9095a592090648e557Ben Murdochint ToRenderViewId(int render_frame_id) {
16116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  RenderFrameImpl* const frame =
17116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch      RenderFrameImpl::FromRoutingID(render_frame_id);
18116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  RenderViewImpl* const view = frame ? frame->render_view() : NULL;
19116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  return view ? view->GetRoutingID() : -1;
20116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch}
21116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch}  // namespace
22116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
235d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)MediaStreamAudioSource::MediaStreamAudioSource(
24116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    int render_frame_id,
255d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    const StreamDeviceInfo& device_info,
265d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    const SourceStoppedCallback& stop_callback,
27cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    PeerConnectionDependencyFactory* factory)
28116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    : render_view_id_(ToRenderViewId(render_frame_id)),
295d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      factory_(factory) {
305d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  SetDeviceInfo(device_info);
315d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  SetStopCallback(stop_callback);
325d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)}
335d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
345d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)MediaStreamAudioSource::MediaStreamAudioSource()
355d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    : render_view_id_(-1),
365d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      factory_(NULL) {
375d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)}
385d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
395d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)MediaStreamAudioSource::~MediaStreamAudioSource() {}
405d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
415d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)void MediaStreamAudioSource::DoStopSource() {
425d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  if (audio_capturer_.get())
435d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    audio_capturer_->Stop();
445d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)}
455d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
465d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)void MediaStreamAudioSource::AddTrack(
475d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    const blink::WebMediaStreamTrack& track,
485d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    const blink::WebMediaConstraints& constraints,
495d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    const ConstraintsCallback& callback) {
505d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // TODO(xians): Properly implement for audio sources.
511320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  if (!local_audio_source_.get()) {
520529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch    if (!factory_->InitializeMediaStreamAudioSource(render_view_id_,
530529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch                                                    constraints,
540529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch                                                    this)) {
550529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch      // The source failed to start.
561320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci      // UserMediaClientImpl rely on the |stop_callback| to be triggered when
571320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci      // the last track is removed from the source. But in this case, the
581320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci      // source is is not even started. So we need to fail both adding the
591320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci      // track and trigger |stop_callback|.
606e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)      callback.Run(this, MEDIA_DEVICE_TRACK_START_FAILURE, "");
610529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch      StopSource();
620529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch      return;
630529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch    }
645d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  }
650529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch
660529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch  factory_->CreateLocalAudioTrack(track);
676e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  callback.Run(this, MEDIA_DEVICE_OK, "");
685d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)}
695d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
705d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)}  // namespace content
71