1
    2
    3
    4
    5
    6
    7
    8
    9
   10
   11
   12
   13
   14
   15
   16
   17
   18
   19
   20
   21
   22
   23
   24
   25
   26
   27
   28
   29
   30
   31
   32
   33
   34
   35
   36
   37
   38
   39
   40
   41
   42
   43
   44
   45
   46
   47
   48
   49
   50
   51
   52
   53
   54
   55
   56
   57
   58
   59
   60
   61
   62
   63
   64
   65
   66
   67
   68
   69

fuchsia_web / webengine / browser / web_engine_media_resource_provider_impl.cc [blame]

// Copyright 2020 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "fuchsia_web/webengine/browser/web_engine_media_resource_provider_impl.h"

#include <lib/fidl/cpp/interface_handle.h>
#include <lib/sys/cpp/component_context.h>

#include "base/command_line.h"
#include "base/fuchsia/process_context.h"
#include "base/functional/bind.h"
#include "content/public/browser/browser_context.h"
#include "content/public/browser/document_service.h"
#include "content/public/browser/permission_controller.h"
#include "content/public/browser/render_frame_host.h"
#include "fuchsia_web/webengine/browser/frame_impl.h"
#include "media/base/media_switches.h"

void WebEngineMediaResourceProviderImpl::Bind(
    content::RenderFrameHost* frame_host,
    mojo::PendingReceiver<mojom::WebEngineMediaResourceProvider> receiver) {
  CHECK(frame_host);
  // The object will delete itself when connection to the frame is broken.
  new WebEngineMediaResourceProviderImpl(*frame_host, std::move(receiver));
}

WebEngineMediaResourceProviderImpl::WebEngineMediaResourceProviderImpl(
    content::RenderFrameHost& render_frame_host,
    mojo::PendingReceiver<mojom::WebEngineMediaResourceProvider> receiver)
    : DocumentService(render_frame_host, std::move(receiver)) {}

WebEngineMediaResourceProviderImpl::~WebEngineMediaResourceProviderImpl() =
    default;

void WebEngineMediaResourceProviderImpl::ShouldUseAudioConsumer(
    ShouldUseAudioConsumerCallback callback) {
  auto* frame_impl = FrameImpl::FromRenderFrameHost(&render_frame_host());
  DCHECK(frame_impl);
  std::move(callback).Run(
      frame_impl->media_settings().has_audio_consumer_session_id());
}

void WebEngineMediaResourceProviderImpl::CreateAudioConsumer(
    fidl::InterfaceRequest<fuchsia::media::AudioConsumer> request) {
  if (base::CommandLine::ForCurrentProcess()->HasSwitch(
          switches::kDisableAudioOutput)) {
    LOG(WARNING)
        << "Could not create AudioConsumer because audio output feature flag "
           "was not enabled.";
    return;
  }

  auto factory = base::ComponentContextForProcess()
                     ->svc()
                     ->Connect<fuchsia::media::SessionAudioConsumerFactory>();
  auto* frame_impl = FrameImpl::FromRenderFrameHost(&render_frame_host());
  DCHECK(frame_impl);

  if (!frame_impl->media_settings().has_audio_consumer_session_id()) {
    LOG(WARNING) << "Renderer tried creating AudioConsumer for a Frame without "
                    "media_session_id().";
    return;
  }

  factory->CreateAudioConsumer(
      frame_impl->media_settings().audio_consumer_session_id(),
      std::move(request));
}