#ifdef UNSAFE_BUFFERS_BUILD
#pragma allow_unsafe_buffers
#endif
#include "third_party/blink/renderer/modules/mediastream/video_track_adapter.h"
#include <algorithm>
#include <cmath>
#include <limits>
#include <memory>
#include <optional>
#include <string>
#include <utility>
#include "base/containers/flat_map.h"
#include "base/functional/bind.h"
#include "base/location.h"
#include "base/metrics/histogram_macros.h"
#include "base/sequence_checker.h"
#include "base/strings/string_number_conversions.h"
#include "base/task/bind_post_task.h"
#include "base/task/sequenced_task_runner.h"
#include "base/task/single_thread_task_runner.h"
#include "base/trace_event/trace_event.h"
#include "build/build_config.h"
#include "media/base/limits.h"
#include "media/base/video_util.h"
#include "third_party/blink/public/common/features.h"
#include "third_party/blink/public/platform/platform.h"
#include "third_party/blink/renderer/modules/mediastream/video_track_adapter_settings.h"
#include "third_party/blink/renderer/platform/scheduler/public/post_cross_thread_task.h"
#include "third_party/blink/renderer/platform/wtf/cross_thread_copier_base.h"
#include "third_party/blink/renderer/platform/wtf/cross_thread_copier_gfx.h"
#include "third_party/blink/renderer/platform/wtf/functional.h"
#include "third_party/blink/renderer/platform/wtf/thread_safe_ref_counted.h"
namespace WTF {
template <>
struct CrossThreadCopier<blink::VideoTrackAdapterSettings>
: public CrossThreadCopierPassThrough<blink::VideoTrackAdapterSettings> { … };
}
namespace blink {
namespace {
const float kFirstFrameTimeoutInFrameIntervals = …;
const float kNormalFrameTimeoutInFrameIntervals = …;
constexpr float kMaxDeltaDeviationFactor = …;
constexpr base::TimeDelta kMaxTimeBetweenFrames = …;
constexpr base::TimeDelta kFrameRateChangeInterval = …;
const double kFrameRateChangeRate = …;
constexpr base::TimeDelta kFrameRateUpdateInterval = …;
struct ComputedSettings { … };
int ClampToValidDimension(int dimension) { … }
void ComputeFrameRate(const base::TimeDelta& frame_timestamp,
double* frame_rate,
base::TimeDelta* prev_frame_timestamp) { … }
bool MaybeUpdateFrameRate(ComputedSettings* settings) { … }
VideoTrackAdapterSettings ReturnSettingsMaybeOverrideMaxFps(
const VideoTrackAdapterSettings& settings) { … }
}
class VideoTrackAdapter::VideoFrameResolutionAdapter
: public WTF::ThreadSafeRefCounted<VideoFrameResolutionAdapter> { … };
VideoTrackAdapter::VideoFrameResolutionAdapter::VideoFrameResolutionAdapter(
scoped_refptr<base::SingleThreadTaskRunner> reader_task_runner,
const VideoTrackAdapterSettings& settings,
base::WeakPtr<MediaStreamVideoSource> media_stream_video_source)
: … { … }
VideoTrackAdapter::VideoFrameResolutionAdapter::~VideoFrameResolutionAdapter() { … }
void VideoTrackAdapter::VideoFrameResolutionAdapter::AddCallbacks(
const MediaStreamVideoTrack* track,
VideoCaptureDeliverFrameInternalCallback frame_callback,
VideoCaptureNotifyFrameDroppedInternalCallback
notify_frame_dropped_callback,
DeliverEncodedVideoFrameInternalCallback encoded_frame_callback,
VideoCaptureSubCaptureTargetVersionInternalCallback
sub_capture_target_version_callback,
VideoTrackSettingsInternalCallback settings_callback,
VideoTrackFormatInternalCallback format_callback) { … }
void VideoTrackAdapter::VideoFrameResolutionAdapter::RemoveCallbacks(
const MediaStreamVideoTrack* track) { … }
VideoTrackAdapter::VideoFrameResolutionAdapter::VideoTrackCallbacks
VideoTrackAdapter::VideoFrameResolutionAdapter::RemoveAndGetCallbacks(
const MediaStreamVideoTrack* track) { … }
void VideoTrackAdapter::VideoFrameResolutionAdapter::DeliverFrame(
scoped_refptr<media::VideoFrame> video_frame,
const base::TimeTicks& estimated_capture_time,
bool is_device_rotated) { … }
void VideoTrackAdapter::VideoFrameResolutionAdapter::DeliverEncodedVideoFrame(
scoped_refptr<EncodedVideoFrame> frame,
base::TimeTicks estimated_capture_time) { … }
void VideoTrackAdapter::VideoFrameResolutionAdapter::
NewSubCaptureTargetVersionOnVideoTaskRunner(
uint32_t sub_capture_target_version) { … }
bool VideoTrackAdapter::VideoFrameResolutionAdapter::SettingsMatch(
const VideoTrackAdapterSettings& settings) const { … }
bool VideoTrackAdapter::VideoFrameResolutionAdapter::IsEmpty() const { … }
void VideoTrackAdapter::VideoFrameResolutionAdapter::DoDeliverFrame(
scoped_refptr<media::VideoFrame> video_frame,
const base::TimeTicks& estimated_capture_time) { … }
void VideoTrackAdapter::VideoFrameResolutionAdapter::OnFrameDropped(
media::VideoCaptureFrameDropReason reason) { … }
bool VideoTrackAdapter::VideoFrameResolutionAdapter::MaybeDropFrame(
const media::VideoFrame& frame,
float source_frame_rate,
media::VideoCaptureFrameDropReason* reason) { … }
void VideoTrackAdapter::VideoFrameResolutionAdapter::MaybeUpdateTrackSettings(
const VideoTrackSettingsInternalCallback& settings_callback,
const media::VideoFrame& frame) { … }
void VideoTrackAdapter::VideoFrameResolutionAdapter::MaybeUpdateTracksFormat(
const media::VideoFrame& frame) { … }
void VideoTrackAdapter::VideoFrameResolutionAdapter::ResetFrameRate() { … }
VideoTrackAdapter::VideoTrackAdapter(
scoped_refptr<base::SequencedTaskRunner> video_task_runner,
base::WeakPtr<MediaStreamVideoSource> media_stream_video_source)
: … { … }
VideoTrackAdapter::~VideoTrackAdapter() { … }
void VideoTrackAdapter::AddTrack(
const MediaStreamVideoTrack* track,
VideoCaptureDeliverFrameCB frame_callback,
VideoCaptureNotifyFrameDroppedCB notify_frame_dropped_callback,
EncodedVideoFrameCB encoded_frame_callback,
VideoCaptureSubCaptureTargetVersionCB sub_capture_target_version_callback,
VideoTrackSettingsCallback settings_callback,
VideoTrackFormatCallback format_callback,
const VideoTrackAdapterSettings& settings) { … }
void VideoTrackAdapter::AddTrackOnVideoTaskRunner(
const MediaStreamVideoTrack* track,
VideoCaptureDeliverFrameInternalCallback frame_callback,
VideoCaptureNotifyFrameDroppedInternalCallback
notify_frame_dropped_callback,
DeliverEncodedVideoFrameInternalCallback encoded_frame_callback,
VideoCaptureSubCaptureTargetVersionInternalCallback
sub_capture_target_version_callback,
VideoTrackSettingsInternalCallback settings_callback,
VideoTrackFormatInternalCallback format_callback,
const VideoTrackAdapterSettings& settings) { … }
void VideoTrackAdapter::RemoveTrack(const MediaStreamVideoTrack* track) { … }
void VideoTrackAdapter::ReconfigureTrack(
const MediaStreamVideoTrack* track,
const VideoTrackAdapterSettings& settings) { … }
void VideoTrackAdapter::StartFrameMonitoring(
double source_frame_rate,
const OnMutedCallback& on_muted_callback) { … }
void VideoTrackAdapter::StopFrameMonitoring() { … }
void VideoTrackAdapter::SetSourceFrameSize(const gfx::Size& source_frame_size) { … }
bool VideoTrackAdapter::CalculateDesiredSize(
bool is_rotated,
const gfx::Size& original_input_size,
const VideoTrackAdapterSettings& settings,
gfx::Size* desired_size) { … }
void VideoTrackAdapter::StartFrameMonitoringOnVideoTaskRunner(
OnMutedInternalCallback on_muted_callback,
double source_frame_rate) { … }
void VideoTrackAdapter::StopFrameMonitoringOnVideoTaskRunner() { … }
void VideoTrackAdapter::SetSourceFrameSizeOnVideoTaskRunner(
const gfx::Size& source_frame_size) { … }
void VideoTrackAdapter::RemoveTrackOnVideoTaskRunner(
const MediaStreamVideoTrack* track) { … }
void VideoTrackAdapter::ReconfigureTrackOnVideoTaskRunner(
const MediaStreamVideoTrack* track,
const VideoTrackAdapterSettings& settings) { … }
void VideoTrackAdapter::DeliverFrameOnVideoTaskRunner(
scoped_refptr<media::VideoFrame> video_frame,
base::TimeTicks estimated_capture_time) { … }
void VideoTrackAdapter::DeliverEncodedVideoFrameOnVideoTaskRunner(
scoped_refptr<EncodedVideoFrame> frame,
base::TimeTicks estimated_capture_time) { … }
void VideoTrackAdapter::OnFrameDroppedOnVideoTaskRunner(
media::VideoCaptureFrameDropReason reason) { … }
void VideoTrackAdapter::NewSubCaptureTargetVersionOnVideoTaskRunner(
uint32_t sub_capture_target_version) { … }
void VideoTrackAdapter::CheckFramesReceivedOnVideoTaskRunner() { … }
}