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 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414
|
// 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 "third_party/blink/renderer/modules/webcodecs/audio_decoder_broker.h"
#include <limits>
#include <memory>
#include <string>
#include "base/memory/weak_ptr.h"
#include "base/task/sequenced_task_runner.h"
#include "build/buildflag.h"
#include "media/base/audio_decoder_config.h"
#include "media/base/decoder_factory.h"
#include "media/base/media_log.h"
#include "media/mojo/buildflags.h"
#include "media/mojo/clients/mojo_decoder_factory.h"
#include "media/mojo/mojom/interface_factory.mojom.h"
#include "media/renderers/default_decoder_factory.h"
#include "mojo/public/cpp/bindings/remote.h"
#include "third_party/blink/public/common/thread_safe_browser_interface_broker_proxy.h"
#include "third_party/blink/public/platform/platform.h"
#include "third_party/blink/public/platform/task_type.h"
#include "third_party/blink/renderer/core/execution_context/execution_context.h"
#include "third_party/blink/renderer/modules/webcodecs/decoder_selector.h"
#include "third_party/blink/renderer/platform/scheduler/public/post_cross_thread_task.h"
#include "third_party/blink/renderer/platform/scheduler/public/thread.h"
#include "third_party/blink/renderer/platform/scheduler/public/worker_pool.h"
#include "third_party/blink/renderer/platform/wtf/cross_thread_copier_base.h"
#include "third_party/blink/renderer/platform/wtf/cross_thread_copier_mojo.h"
#include "third_party/blink/renderer/platform/wtf/cross_thread_functional.h"
#include "third_party/blink/renderer/platform/wtf/functional.h"
using DecoderDetails = blink::AudioDecoderBroker::DecoderDetails;
namespace WTF {
template <>
struct CrossThreadCopier<media::AudioDecoderConfig>
: public CrossThreadCopierPassThrough<media::AudioDecoderConfig> {
STATIC_ONLY(CrossThreadCopier);
};
template <>
struct CrossThreadCopier<media::DecoderStatus>
: public CrossThreadCopierPassThrough<media::DecoderStatus> {
STATIC_ONLY(CrossThreadCopier);
};
template <>
struct CrossThreadCopier<std::optional<DecoderDetails>>
: public CrossThreadCopierPassThrough<std::optional<DecoderDetails>> {
STATIC_ONLY(CrossThreadCopier);
};
} // namespace WTF
namespace blink {
// Wrapper class for state and API calls that must be made from the
// |media_task_runner_|. Construction must happen on blink main thread to safely
// make use of ExecutionContext and Document. These GC blink types must not be
// stored/referenced by any other method.
class MediaAudioTaskWrapper {
public:
using CrossThreadOnceInitCB =
WTF::CrossThreadOnceFunction<void(media::DecoderStatus status,
std::optional<DecoderDetails>)>;
using CrossThreadOnceDecodeCB =
WTF::CrossThreadOnceFunction<void(media::DecoderStatus)>;
using CrossThreadOnceResetCB = WTF::CrossThreadOnceClosure;
MediaAudioTaskWrapper(
base::WeakPtr<CrossThreadAudioDecoderClient> weak_client,
ExecutionContext& execution_context,
std::unique_ptr<media::MediaLog> media_log,
scoped_refptr<base::SequencedTaskRunner> media_task_runner,
scoped_refptr<base::SequencedTaskRunner> main_task_runner)
: weak_client_(std::move(weak_client)),
media_task_runner_(std::move(media_task_runner)),
main_task_runner_(std::move(main_task_runner)),
media_log_(std::move(media_log)) {
DVLOG(2) << __func__;
DETACH_FROM_SEQUENCE(sequence_checker_);
// TODO(chcunningham): set_disconnect_handler?
// Mojo connection setup must occur here on the main thread where its safe
// to use |execution_context| APIs.
mojo::PendingRemote<media::mojom::InterfaceFactory> media_interface_factory;
Platform::Current()->GetBrowserInterfaceBroker()->GetInterface(
media_interface_factory.InitWithNewPipeAndPassReceiver());
// Mojo remote must be bound on media thread where it will be used.
//|Unretained| is safe because |this| must be destroyed on the media task
// runner.
PostCrossThreadTask(
*media_task_runner_, FROM_HERE,
WTF::CrossThreadBindOnce(&MediaAudioTaskWrapper::BindOnTaskRunner,
WTF::CrossThreadUnretained(this),
std::move(media_interface_factory)));
}
virtual ~MediaAudioTaskWrapper() {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
}
MediaAudioTaskWrapper(const MediaAudioTaskWrapper&) = delete;
MediaAudioTaskWrapper& operator=(const MediaAudioTaskWrapper&) = delete;
void Initialize(const media::AudioDecoderConfig& config) {
DVLOG(2) << __func__;
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
selector_ = std::make_unique<WebCodecsAudioDecoderSelector>(
media_task_runner_,
// TODO(chcunningham): Its ugly that we don't use a WeakPtr here, but
// its not possible because the callback returns non-void. It happens
// to be safe given the way the callback is called (never posted), but
// we should refactor the return to be an out-param so we can be
// consistent in using weak pointers.
WTF::BindRepeating(&MediaAudioTaskWrapper::OnCreateDecoders,
WTF::Unretained(this)),
WTF::BindRepeating(&MediaAudioTaskWrapper::OnDecodeOutput,
weak_factory_.GetWeakPtr()));
selector_->SelectDecoder(
config, /*low_delay=*/false,
WTF::BindOnce(&MediaAudioTaskWrapper::OnDecoderSelected,
weak_factory_.GetWeakPtr()));
}
void Decode(scoped_refptr<media::DecoderBuffer> buffer, int cb_id) {
DVLOG(2) << __func__;
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
if (!decoder_) {
OnDecodeDone(cb_id, media::DecoderStatus::Codes::kNotInitialized);
return;
}
decoder_->Decode(std::move(buffer),
WTF::BindOnce(&MediaAudioTaskWrapper::OnDecodeDone,
weak_factory_.GetWeakPtr(), cb_id));
}
void Reset(int cb_id) {
DVLOG(2) << __func__;
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
if (!decoder_) {
OnReset(cb_id);
return;
}
decoder_->Reset(WTF::BindOnce(&MediaAudioTaskWrapper::OnReset,
weak_factory_.GetWeakPtr(), cb_id));
}
private:
void BindOnTaskRunner(
mojo::PendingRemote<media::mojom::InterfaceFactory> interface_factory) {
DVLOG(2) << __func__;
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
media_interface_factory_.Bind(std::move(interface_factory));
// Bind the |interface_factory_| above before passing to
// |external_decoder_factory|.
std::unique_ptr<media::DecoderFactory> external_decoder_factory;
#if BUILDFLAG(ENABLE_MOJO_AUDIO_DECODER)
external_decoder_factory = std::make_unique<media::MojoDecoderFactory>(
media_interface_factory_.get());
#endif
decoder_factory_ = std::make_unique<media::DefaultDecoderFactory>(
std::move(external_decoder_factory));
}
std::vector<std::unique_ptr<media::AudioDecoder>> OnCreateDecoders() {
DVLOG(2) << __func__;
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
std::vector<std::unique_ptr<media::AudioDecoder>> audio_decoders;
decoder_factory_->CreateAudioDecoders(media_task_runner_, media_log_.get(),
&audio_decoders);
return audio_decoders;
}
void OnDecoderSelected(std::unique_ptr<media::AudioDecoder> decoder) {
DVLOG(2) << __func__;
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
// We're done with it.
DCHECK(selector_);
selector_.reset();
decoder_ = std::move(decoder);
media::DecoderStatus status = media::DecoderStatus::Codes::kOk;
std::optional<DecoderDetails> decoder_details = std::nullopt;
if (decoder_) {
decoder_details = DecoderDetails({decoder_->GetDecoderType(),
decoder_->IsPlatformDecoder(),
decoder_->NeedsBitstreamConversion()});
} else {
status = media::DecoderStatus::Codes::kUnsupportedConfig;
}
// Fire |init_cb|.
PostCrossThreadTask(
*main_task_runner_, FROM_HERE,
WTF::CrossThreadBindOnce(&CrossThreadAudioDecoderClient::OnInitialize,
weak_client_, status, decoder_details));
}
void OnDecodeOutput(scoped_refptr<media::AudioBuffer> buffer) {
DVLOG(2) << __func__;
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
PostCrossThreadTask(
*main_task_runner_, FROM_HERE,
WTF::CrossThreadBindOnce(&CrossThreadAudioDecoderClient::OnDecodeOutput,
weak_client_, std::move(buffer)));
}
void OnDecodeDone(int cb_id, media::DecoderStatus status) {
DVLOG(2) << __func__;
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
PostCrossThreadTask(
*main_task_runner_, FROM_HERE,
WTF::CrossThreadBindOnce(&CrossThreadAudioDecoderClient::OnDecodeDone,
weak_client_, cb_id, std::move(status)));
}
void OnReset(int cb_id) {
DVLOG(2) << __func__;
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
PostCrossThreadTask(
*main_task_runner_, FROM_HERE,
WTF::CrossThreadBindOnce(&CrossThreadAudioDecoderClient::OnReset,
weak_client_, cb_id));
}
base::WeakPtr<CrossThreadAudioDecoderClient> weak_client_;
scoped_refptr<base::SequencedTaskRunner> media_task_runner_;
scoped_refptr<base::SequencedTaskRunner> main_task_runner_;
mojo::Remote<media::mojom::InterfaceFactory> media_interface_factory_;
std::unique_ptr<WebCodecsAudioDecoderSelector> selector_;
std::unique_ptr<media::DefaultDecoderFactory> decoder_factory_;
std::unique_ptr<media::AudioDecoder> decoder_;
gfx::ColorSpace target_color_space_;
std::unique_ptr<media::MediaLog> media_log_;
SEQUENCE_CHECKER(sequence_checker_);
// Using unretained for decoder/selector callbacks is generally not safe /
// fragile. Some decoders (e.g. those that offload) will call the output
// callback after destruction.
base::WeakPtrFactory<MediaAudioTaskWrapper> weak_factory_{this};
};
AudioDecoderBroker::AudioDecoderBroker(media::MediaLog* media_log,
ExecutionContext& execution_context)
// Use a worker task runner to avoid scheduling decoder
// work on the main thread.
: media_task_runner_(worker_pool::CreateSequencedTaskRunner({})) {
DVLOG(2) << __func__;
media_tasks_ = std::make_unique<MediaAudioTaskWrapper>(
weak_factory_.GetWeakPtr(), execution_context, media_log->Clone(),
media_task_runner_,
execution_context.GetTaskRunner(TaskType::kInternalMedia));
}
AudioDecoderBroker::~AudioDecoderBroker() {
DVLOG(2) << __func__;
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
media_task_runner_->DeleteSoon(FROM_HERE, std::move(media_tasks_));
}
media::AudioDecoderType AudioDecoderBroker::GetDecoderType() const {
return decoder_details_ ? decoder_details_->decoder_type
: media::AudioDecoderType::kBroker;
}
bool AudioDecoderBroker::IsPlatformDecoder() const {
return decoder_details_ ? decoder_details_->is_platform_decoder : false;
}
void AudioDecoderBroker::Initialize(const media::AudioDecoderConfig& config,
media::CdmContext* cdm_context,
InitCB init_cb,
const OutputCB& output_cb,
const media::WaitingCB& waiting_cb) {
DVLOG(2) << __func__;
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
DCHECK(!init_cb_) << "Initialize already pending";
// The following are not currently supported in WebCodecs.
DCHECK(!cdm_context);
DCHECK(!waiting_cb);
init_cb_ = std::move(init_cb);
output_cb_ = output_cb;
// Clear details from previously initialized decoder. New values will arrive
// via OnInitialize().
decoder_details_.reset();
PostCrossThreadTask(
*media_task_runner_, FROM_HERE,
WTF::CrossThreadBindOnce(&MediaAudioTaskWrapper::Initialize,
WTF::CrossThreadUnretained(media_tasks_.get()),
config));
}
int AudioDecoderBroker::CreateCallbackId() {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
// 0 and -1 are reserved by wtf::HashMap ("empty" and "deleted").
while (++last_callback_id_ == 0 ||
last_callback_id_ == std::numeric_limits<uint32_t>::max() ||
pending_decode_cb_map_.Contains(last_callback_id_) ||
pending_reset_cb_map_.Contains(last_callback_id_))
;
return last_callback_id_;
}
void AudioDecoderBroker::OnInitialize(media::DecoderStatus status,
std::optional<DecoderDetails> details) {
DVLOG(2) << __func__;
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
decoder_details_ = details;
std::move(init_cb_).Run(status);
}
void AudioDecoderBroker::Decode(scoped_refptr<media::DecoderBuffer> buffer,
DecodeCB decode_cb) {
DVLOG(2) << __func__;
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
const int callback_id = CreateCallbackId();
pending_decode_cb_map_.insert(callback_id, std::move(decode_cb));
PostCrossThreadTask(
*media_task_runner_, FROM_HERE,
WTF::CrossThreadBindOnce(&MediaAudioTaskWrapper::Decode,
WTF::CrossThreadUnretained(media_tasks_.get()),
buffer, callback_id));
}
void AudioDecoderBroker::OnDecodeDone(int cb_id, media::DecoderStatus status) {
DVLOG(2) << __func__;
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
DCHECK(pending_decode_cb_map_.Contains(cb_id));
auto iter = pending_decode_cb_map_.find(cb_id);
DecodeCB decode_cb = std::move(iter->value);
pending_decode_cb_map_.erase(cb_id);
// Do this last. Caller may destruct |this| in response to the callback while
// this method is still on the stack.
std::move(decode_cb).Run(std::move(status));
}
void AudioDecoderBroker::Reset(base::OnceClosure reset_cb) {
DVLOG(2) << __func__;
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
const int callback_id = CreateCallbackId();
pending_reset_cb_map_.insert(callback_id, std::move(reset_cb));
PostCrossThreadTask(
*media_task_runner_, FROM_HERE,
WTF::CrossThreadBindOnce(&MediaAudioTaskWrapper::Reset,
WTF::CrossThreadUnretained(media_tasks_.get()),
callback_id));
}
bool AudioDecoderBroker::NeedsBitstreamConversion() const {
// No known scenarios where this is needed by WebCodecs. See
// https://crbug.com/1119947
DCHECK(!decoder_details_ || !decoder_details_->needs_bitstream_conversion);
return decoder_details_ ? decoder_details_->needs_bitstream_conversion
: false;
}
void AudioDecoderBroker::OnReset(int cb_id) {
DVLOG(2) << __func__;
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
DCHECK(pending_reset_cb_map_.Contains(cb_id));
auto iter = pending_reset_cb_map_.find(cb_id);
base::OnceClosure reset_cb = std::move(iter->value);
pending_reset_cb_map_.erase(cb_id);
// Do this last. Caller may destruct |this| in response to the callback while
// this method is still on the stack.
std::move(reset_cb).Run();
}
void AudioDecoderBroker::OnDecodeOutput(
scoped_refptr<media::AudioBuffer> buffer) {
DVLOG(2) << __func__;
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
DCHECK(output_cb_);
output_cb_.Run(std::move(buffer));
}
} // namespace blink
|