#ifdef UNSAFE_BUFFERS_BUILD
#pragma allow_unsafe_buffers
#endif
#include "media/cast/encoding/external_video_encoder.h"
#include <array>
#include <cmath>
#include <list>
#include <sstream>
#include <utility>
#include "base/command_line.h"
#include "base/functional/bind.h"
#include "base/functional/callback_helpers.h"
#include "base/logging.h"
#include "base/memory/read_only_shared_memory_region.h"
#include "base/memory/shared_memory_mapping.h"
#include "base/memory/unsafe_shared_memory_region.h"
#include "base/metrics/histogram_macros.h"
#include "base/numerics/safe_conversions.h"
#include "base/strings/string_number_conversions.h"
#include "base/task/bind_post_task.h"
#include "base/task/single_thread_task_runner.h"
#include "base/time/time.h"
#include "base/trace_event/trace_event.h"
#include "build/build_config.h"
#include "media/base/bitrate.h"
#include "media/base/bitstream_buffer.h"
#include "media/base/media_switches.h"
#include "media/base/media_util.h"
#include "media/base/video_codecs.h"
#include "media/base/video_encoder_metrics_provider.h"
#include "media/base/video_frame.h"
#include "media/base/video_types.h"
#include "media/base/video_util.h"
#include "media/cast/cast_config.h"
#include "media/cast/common/encoded_frame.h"
#include "media/cast/common/openscreen_conversion_helpers.h"
#include "media/cast/common/rtp_time.h"
#include "media/cast/common/sender_encoded_frame.h"
#include "media/cast/encoding/encoding_util.h"
#include "media/cast/encoding/vpx_quantizer_parser.h"
#include "media/cast/logging/logging_defines.h"
#include "media/parsers/h264_parser.h"
namespace media::cast {
namespace {
constexpr int kFrameSamplingPercentage = …;
constexpr int kMaxH264Quantizer = …;
constexpr size_t kOutputBufferCount = …;
constexpr size_t kExtraInputBufferCount = …;
constexpr int kBacklogRedlineThreshold = …;
constexpr int kQuantizationHistogramSize = …;
bool IsVpxProfile(VideoCodecProfile codec_profile) { … }
}
struct InProgressExternalVideoFrameEncode { … };
class ExternalVideoEncoder::VEAClientImpl final
: public VideoEncodeAccelerator::Client,
public base::RefCountedThreadSafe<VEAClientImpl> { … };
ExternalVideoEncoder::ExternalVideoEncoder(
const scoped_refptr<CastEnvironment>& cast_environment,
const FrameSenderConfig& video_config,
VideoEncoderMetricsProvider& metrics_provider,
const gfx::Size& frame_size,
FrameId first_frame_id,
StatusChangeCallback status_change_cb,
const CreateVideoEncodeAcceleratorCallback& create_vea_cb)
: … { … }
ExternalVideoEncoder::~ExternalVideoEncoder() { … }
void ExternalVideoEncoder::DestroyClientSoon() { … }
void ExternalVideoEncoder::SetErrorToMetricsProvider(
const media::EncoderStatus& encoder_status) { … }
bool ExternalVideoEncoder::EncodeVideoFrame(
scoped_refptr<media::VideoFrame> video_frame,
base::TimeTicks reference_time,
FrameEncodedCallback frame_encoded_callback) { … }
void ExternalVideoEncoder::SetBitRate(int new_bit_rate) { … }
void ExternalVideoEncoder::GenerateKeyFrame() { … }
void ExternalVideoEncoder::OnCreateVideoEncodeAccelerator(
const FrameSenderConfig& video_config,
FrameId first_frame_id,
const StatusChangeCallback& status_change_cb,
scoped_refptr<base::SingleThreadTaskRunner> encoder_task_runner,
std::unique_ptr<media::VideoEncodeAccelerator> vea) { … }
SizeAdaptableExternalVideoEncoder::SizeAdaptableExternalVideoEncoder(
const scoped_refptr<CastEnvironment>& cast_environment,
const FrameSenderConfig& video_config,
std::unique_ptr<VideoEncoderMetricsProvider> metrics_provider,
StatusChangeCallback status_change_cb,
const CreateVideoEncodeAcceleratorCallback& create_vea_cb)
: … { … }
SizeAdaptableExternalVideoEncoder::~SizeAdaptableExternalVideoEncoder() =
default;
std::unique_ptr<VideoEncoder>
SizeAdaptableExternalVideoEncoder::CreateEncoder() { … }
QuantizerEstimator::QuantizerEstimator() = default;
QuantizerEstimator::~QuantizerEstimator() = default;
void QuantizerEstimator::Reset() { … }
double QuantizerEstimator::EstimateForKeyFrame(const VideoFrame& frame) { … }
double QuantizerEstimator::EstimateForDeltaFrame(const VideoFrame& frame) { … }
bool QuantizerEstimator::CanExamineFrame(const VideoFrame& frame) { … }
double QuantizerEstimator::ComputeEntropyFromHistogram(const int* histogram,
size_t histogram_size,
int num_samples) { … }
double QuantizerEstimator::ToQuantizerEstimate(double shannon_entropy) { … }
}