#ifdef UNSAFE_BUFFERS_BUILD
#pragma allow_unsafe_buffers
#endif
#include "third_party/blink/renderer/modules/mediarecorder/media_recorder_handler.h"
#include <algorithm>
#include <memory>
#include <string_view>
#include <utility>
#include "base/feature_list.h"
#include "base/functional/bind.h"
#include "base/location.h"
#include "base/logging.h"
#include "base/metrics/histogram_functions.h"
#include "base/system/sys_info.h"
#include "base/task/bind_post_task.h"
#include "base/task/sequenced_task_runner.h"
#include "base/time/time.h"
#include "media/base/audio_bus.h"
#include "media/base/audio_codecs.h"
#include "media/base/audio_parameters.h"
#include "media/base/mime_util.h"
#include "media/base/video_codec_string_parsers.h"
#include "media/base/video_codecs.h"
#include "media/base/video_frame.h"
#include "media/formats/mp4/mp4_status.h"
#include "media/media_buildflags.h"
#include "media/mojo/clients/mojo_audio_encoder.h"
#include "media/mojo/clients/mojo_video_encoder_metrics_provider.h"
#include "media/muxers/live_webm_muxer_delegate.h"
#include "media/muxers/mp4_muxer.h"
#include "media/muxers/mp4_muxer_delegate.h"
#include "media/muxers/muxer.h"
#include "media/muxers/muxer_timestamp_adapter.h"
#include "media/muxers/webm_muxer.h"
#include "third_party/blink/renderer/core/frame/local_dom_window.h"
#include "third_party/blink/renderer/core/frame/local_frame.h"
#include "third_party/blink/renderer/modules/mediarecorder/media_recorder.h"
#include "third_party/blink/renderer/modules/mediastream/media_stream_video_track.h"
#include "third_party/blink/renderer/platform/bindings/exception_code.h"
#include "third_party/blink/renderer/platform/heap/garbage_collected.h"
#include "third_party/blink/renderer/platform/heap/persistent.h"
#include "third_party/blink/renderer/platform/media_capabilities/web_media_capabilities_info.h"
#include "third_party/blink/renderer/platform/media_capabilities/web_media_configuration.h"
#include "third_party/blink/renderer/platform/mediastream/media_stream_component.h"
#include "third_party/blink/renderer/platform/mediastream/media_stream_descriptor.h"
#include "third_party/blink/renderer/platform/mediastream/media_stream_source.h"
#include "third_party/blink/renderer/platform/mediastream/webrtc_uma_histograms.h"
#include "third_party/blink/renderer/platform/network/mime/content_type.h"
#include "third_party/blink/renderer/platform/wtf/functional.h"
#include "third_party/blink/renderer/platform/wtf/text/string_builder.h"
#if BUILDFLAG(IS_WIN)
#include "media/gpu/windows/mf_audio_encoder.h"
#endif
TimeTicks;
namespace blink {
BASE_FEATURE(…);
namespace {
constexpr double kDefaultVideoFrameRate = …;
const float kNumPixelsPerSecondSmoothnessThresholdLow = …;
const float kNumPixelsPerSecondSmoothnessThresholdHigh = …;
VideoTrackRecorder::CodecId CodecIdFromMediaVideoCodec(media::VideoCodec id) { … }
media::VideoCodec MediaVideoCodecFromCodecId(VideoTrackRecorder::CodecId id) { … }
media::AudioCodec CodecIdToMediaAudioCodec(AudioTrackRecorder::CodecId id) { … }
VideoTrackRecorder::CodecProfile VideoStringToCodecProfile(
const String& codecs) { … }
AudioTrackRecorder::CodecId AudioStringToCodecId(const String& codecs) { … }
bool CanSupportVideoType(const String& type) { … }
bool CanSupportAudioType(const String& type) { … }
bool IsAllowedMp4Type(const String& type) { … }
bool IsMp4MuxerRequired(const String& type) { … }
}
MediaRecorderHandler::MediaRecorderHandler(
scoped_refptr<base::SingleThreadTaskRunner> main_thread_task_runner,
KeyFrameRequestProcessor::Configuration key_frame_config)
: … { … }
bool MediaRecorderHandler::CanSupportMimeType(const String& type,
const String& web_codecs) { … }
bool MediaRecorderHandler::Initialize(
MediaRecorder* recorder,
MediaStreamDescriptor* media_stream,
const String& type,
const String& codecs,
AudioTrackRecorder::BitrateMode audio_bitrate_mode) { … }
AudioTrackRecorder::BitrateMode MediaRecorderHandler::AudioBitrateMode() { … }
bool MediaRecorderHandler::Start(int timeslice,
const String& type,
uint32_t audio_bits_per_second,
uint32_t video_bits_per_second) { … }
void MediaRecorderHandler::Stop() { … }
void MediaRecorderHandler::Pause() { … }
void MediaRecorderHandler::Resume() { … }
void MediaRecorderHandler::EncodingInfo(
const WebMediaConfiguration& configuration,
OnMediaCapabilitiesEncodingInfoCallback callback) { … }
String MediaRecorderHandler::ActualMimeType() { … }
void MediaRecorderHandler::TrackAdded(const WebString& track_id) { … }
void MediaRecorderHandler::TrackRemoved(const WebString& track_id) { … }
void MediaRecorderHandler::OnStreamChanged(const String& message) { … }
void MediaRecorderHandler::OnEncodedVideo(
const media::Muxer::VideoParameters& params,
std::string encoded_data,
std::string encoded_alpha,
std::optional<media::VideoEncoder::CodecDescription> codec_description,
base::TimeTicks timestamp,
bool is_key_frame) { … }
void MediaRecorderHandler::OnPassthroughVideo(
const media::Muxer::VideoParameters& params,
std::string encoded_data,
std::string encoded_alpha,
base::TimeTicks timestamp,
bool is_key_frame) { … }
void MediaRecorderHandler::HandleEncodedVideo(
const media::Muxer::VideoParameters& params,
std::string encoded_data,
std::string encoded_alpha,
std::optional<media::VideoEncoder::CodecDescription> codec_description,
base::TimeTicks timestamp,
bool is_key_frame) { … }
void MediaRecorderHandler::OnEncodedAudio(
const media::AudioParameters& params,
std::string encoded_data,
std::optional<media::AudioEncoder::CodecDescription> codec_description,
base::TimeTicks timestamp) { … }
void MediaRecorderHandler::OnAudioEncodingError(
media::EncoderStatus error_status) { … }
std::unique_ptr<media::VideoEncoderMetricsProvider>
MediaRecorderHandler::CreateVideoEncoderMetricsProvider() { … }
void MediaRecorderHandler::WriteData(std::string_view data) { … }
void MediaRecorderHandler::UpdateTracksLiveAndEnabled() { … }
void MediaRecorderHandler::UpdateTrackLiveAndEnabled(
const MediaStreamComponent& track,
bool is_video) { … }
void MediaRecorderHandler::OnSourceReadyStateChanged() { … }
void MediaRecorderHandler::OnVideoFrameForTesting(
scoped_refptr<media::VideoFrame> frame,
const TimeTicks& timestamp) { … }
void MediaRecorderHandler::OnEncodedVideoFrameForTesting(
scoped_refptr<EncodedVideoFrame> frame,
const base::TimeTicks& timestamp) { … }
void MediaRecorderHandler::OnAudioBusForTesting(
const media::AudioBus& audio_bus,
const base::TimeTicks& timestamp) { … }
void MediaRecorderHandler::SetAudioFormatForTesting(
const media::AudioParameters& params) { … }
void MediaRecorderHandler::Trace(Visitor* visitor) const { … }
void MediaRecorderHandler::OnVideoEncodingError() { … }
}