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 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200
|
// Copyright 2018 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "content/browser/media/audio_input_stream_broker.h"
#include <optional>
#include <utility>
#include "base/command_line.h"
#include "base/functional/bind.h"
#include "base/location.h"
#include "base/logging.h"
#include "base/metrics/histogram_macros.h"
#include "base/trace_event/trace_event.h"
#include "build/build_config.h"
#include "content/browser/browser_main_loop.h"
#include "content/browser/media/audio_stream_broker_helper.h"
#include "content/browser/media/media_internals.h"
#include "content/public/browser/browser_task_traits.h"
#include "content/public/browser/browser_thread.h"
#include "media/audio/audio_logging.h"
#include "media/base/media_switches.h"
#include "media/mojo/mojom/audio_processing.mojom.h"
namespace content {
using DisconnectReason =
media::mojom::AudioInputStreamObserver::DisconnectReason;
using InputStreamErrorCode = media::mojom::InputStreamErrorCode;
AudioInputStreamBroker::AudioInputStreamBroker(
int render_process_id,
int render_frame_id,
const std::string& device_id,
const media::AudioParameters& params,
uint32_t shared_memory_count,
bool enable_agc,
media::mojom::AudioProcessingConfigPtr processing_config,
AudioStreamBroker::DeleterCallback deleter,
mojo::PendingRemote<blink::mojom::RendererAudioInputStreamFactoryClient>
renderer_factory_client)
: AudioStreamBroker(render_process_id, render_frame_id),
device_id_(device_id),
params_(params),
shared_memory_count_(shared_memory_count),
enable_agc_(enable_agc),
deleter_(std::move(deleter)),
processing_config_(std::move(processing_config)),
renderer_factory_client_(std::move(renderer_factory_client)) {
DCHECK_CURRENTLY_ON(BrowserThread::IO);
DCHECK(renderer_factory_client_);
DCHECK(deleter_);
TRACE_EVENT_NESTABLE_ASYNC_BEGIN0("audio", "AudioInputStreamBroker", this);
// Unretained is safe because |this| owns |renderer_factory_client_|.
renderer_factory_client_.set_disconnect_handler(base::BindOnce(
&AudioInputStreamBroker::ClientBindingLost, base::Unretained(this)));
NotifyFrameHostOfAudioStreamStarted(render_process_id, render_frame_id,
/*is_capturing=*/true);
if (base::CommandLine::ForCurrentProcess()->HasSwitch(
switches::kUseFakeDeviceForMediaStream)) {
params_.set_format(media::AudioParameters::AUDIO_FAKE);
}
}
AudioInputStreamBroker::~AudioInputStreamBroker() {
DCHECK_CURRENTLY_ON(BrowserThread::IO);
NotifyFrameHostOfAudioStreamStopped(render_process_id(), render_frame_id(),
/*is_capturing=*/true);
// TODO(crbug.com/40091014) update tab recording indicator.
if (awaiting_created_) {
TRACE_EVENT_NESTABLE_ASYNC_END1("audio", "CreateStream", this, "success",
"failed or cancelled");
}
TRACE_EVENT_NESTABLE_ASYNC_END1("audio", "AudioInputStreamBroker", this,
"disconnect reason",
static_cast<uint32_t>(disconnect_reason_));
}
void AudioInputStreamBroker::CreateStream(
media::mojom::AudioStreamFactory* factory) {
DCHECK_CURRENTLY_ON(BrowserThread::IO);
DCHECK(!observer_receiver_.is_bound());
DCHECK(!pending_client_receiver_);
TRACE_EVENT_NESTABLE_ASYNC_BEGIN1("audio", "CreateStream", this, "device id",
device_id_);
awaiting_created_ = true;
mojo::PendingRemote<media::mojom::AudioInputStreamClient> client;
pending_client_receiver_ = client.InitWithNewPipeAndPassReceiver();
mojo::PendingRemote<media::mojom::AudioInputStream> stream;
auto stream_receiver = stream.InitWithNewPipeAndPassReceiver();
mojo::PendingRemote<media::mojom::AudioInputStreamObserver> observer;
observer_receiver_.Bind(observer.InitWithNewPipeAndPassReceiver());
// Unretained is safe because |this| owns |observer_binding_|.
observer_receiver_.set_disconnect_with_reason_handler(base::BindOnce(
&AudioInputStreamBroker::ObserverBindingLost, base::Unretained(this)));
// Note that the component id for AudioLog is used to differentiate between
// several users of the same audio log. Since this audio log is for a single
// stream, the component id used doesn't matter.
// We can move |processing_config_| because this method is only called once.
constexpr int log_component_id = 0;
factory->CreateInputStream(
std::move(stream_receiver), std::move(client), std::move(observer),
MediaInternals::GetInstance()->CreateMojoAudioLog(
media::AudioLogFactory::AudioComponent::kAudioInputController,
log_component_id, render_process_id(), render_frame_id()),
device_id_, params_, shared_memory_count_, enable_agc_,
std::move(processing_config_),
base::BindOnce(&AudioInputStreamBroker::StreamCreated,
weak_ptr_factory_.GetWeakPtr(), std::move(stream)));
}
void AudioInputStreamBroker::DidStartRecording() {
DCHECK_CURRENTLY_ON(BrowserThread::IO);
// TODO(crbug.com/40091014) update tab recording indicator.
}
void AudioInputStreamBroker::StreamCreated(
mojo::PendingRemote<media::mojom::AudioInputStream> stream,
media::mojom::ReadWriteAudioDataPipePtr data_pipe,
bool initially_muted,
const std::optional<base::UnguessableToken>& stream_id) {
DCHECK_CURRENTLY_ON(BrowserThread::IO);
awaiting_created_ = false;
TRACE_EVENT_NESTABLE_ASYNC_END1("audio", "CreateStream", this, "success",
!!data_pipe);
if (!data_pipe) {
disconnect_reason_ = DisconnectReason::kStreamCreationFailed;
Cleanup();
return;
}
DCHECK(stream_id.has_value());
DCHECK(renderer_factory_client_);
renderer_factory_client_->StreamCreated(
std::move(stream), std::move(pending_client_receiver_),
std::move(data_pipe), initially_muted, stream_id);
}
InputStreamErrorCode MapDisconnectReasonToErrorCode(DisconnectReason reason) {
switch (static_cast<DisconnectReason>(reason)) {
case DisconnectReason::kSystemPermissions:
return InputStreamErrorCode::kSystemPermissions;
case DisconnectReason::kDeviceInUse:
return InputStreamErrorCode::kDeviceInUse;
case DisconnectReason::kDefault:
case DisconnectReason::kPlatformError:
case DisconnectReason::kTerminatedByClient:
case DisconnectReason::kStreamCreationFailed:
case DisconnectReason::kDocumentDestroyed:
break;
}
return InputStreamErrorCode::kUnknown;
}
void AudioInputStreamBroker::ObserverBindingLost(
uint32_t reason,
const std::string& description) {
DCHECK_CURRENTLY_ON(BrowserThread::IO);
DisconnectReason disconnection_reason = static_cast<DisconnectReason>(reason);
if (!media::mojom::IsKnownEnumValue(disconnection_reason)) {
DLOG(ERROR) << "Invalid reason: " << reason;
} else if (disconnect_reason_ == DisconnectReason::kDocumentDestroyed) {
disconnect_reason_ = disconnection_reason;
}
renderer_factory_client_.ResetWithReason(
static_cast<uint32_t>(
MapDisconnectReasonToErrorCode(disconnection_reason)),
description);
Cleanup();
}
void AudioInputStreamBroker::ClientBindingLost() {
DCHECK_CURRENTLY_ON(BrowserThread::IO);
disconnect_reason_ = DisconnectReason::kTerminatedByClient;
Cleanup();
}
void AudioInputStreamBroker::Cleanup() {
DCHECK_CURRENTLY_ON(BrowserThread::IO);
std::move(deleter_).Run(this);
}
} // namespace content
|