xref: /aosp_15_r20/external/webrtc/video/g3doc/adaptation.md (revision d9f758449e529ab9291ac668be2861e7a55c2422)
1*d9f75844SAndroid Build Coastguard Worker<?% config.freshness.reviewed = '2021-04-13' %?>
2*d9f75844SAndroid Build Coastguard Worker<?% config.freshness.owner = 'eshr' %?>
3*d9f75844SAndroid Build Coastguard Worker
4*d9f75844SAndroid Build Coastguard Worker# Video Adaptation
5*d9f75844SAndroid Build Coastguard Worker
6*d9f75844SAndroid Build Coastguard WorkerVideo adaptation is a mechanism which reduces the bandwidth or CPU consumption
7*d9f75844SAndroid Build Coastguard Workerby reducing encoded video quality.
8*d9f75844SAndroid Build Coastguard Worker
9*d9f75844SAndroid Build Coastguard Worker## Overview
10*d9f75844SAndroid Build Coastguard Worker
11*d9f75844SAndroid Build Coastguard WorkerAdaptation occurs when a _Resource_ signals that it is currently underused or
12*d9f75844SAndroid Build Coastguard Workeroverused. When overused, the video quality is decreased and when underused, the
13*d9f75844SAndroid Build Coastguard Workervideo quality is increased. There are currently two dimensions in which the
14*d9f75844SAndroid Build Coastguard Workerquality can be adapted: frame-rate and resolution. The dimension that is adapted
15*d9f75844SAndroid Build Coastguard Workeris based on the degradation preference for the video track.
16*d9f75844SAndroid Build Coastguard Worker
17*d9f75844SAndroid Build Coastguard Worker## Resources
18*d9f75844SAndroid Build Coastguard Worker
19*d9f75844SAndroid Build Coastguard Worker_Resources_ monitor metrics from the system or the video stream. For example, a
20*d9f75844SAndroid Build Coastguard Workerresource could monitor system temperature or the bandwidth usage of the video
21*d9f75844SAndroid Build Coastguard Workerstream. A resource implements the [Resource][resource.h] interface. When a
22*d9f75844SAndroid Build Coastguard Workerresource detects that it is overused, it calls `SetUsageState(kOveruse)`. When
23*d9f75844SAndroid Build Coastguard Workerthe resource is no longer overused, it can signal this using
24*d9f75844SAndroid Build Coastguard Worker`SetUsageState(kUnderuse)`.
25*d9f75844SAndroid Build Coastguard Worker
26*d9f75844SAndroid Build Coastguard WorkerThere are two resources that are used by default on all video tracks: Quality
27*d9f75844SAndroid Build Coastguard Workerscaler resource and encode overuse resource.
28*d9f75844SAndroid Build Coastguard Worker
29*d9f75844SAndroid Build Coastguard Worker### QP Scaler Resource
30*d9f75844SAndroid Build Coastguard Worker
31*d9f75844SAndroid Build Coastguard WorkerThe quality scaler resource monitors the quantization parameter (QP) of the
32*d9f75844SAndroid Build Coastguard Workerencoded video frames for video send stream and ensures that the quality of the
33*d9f75844SAndroid Build Coastguard Workerstream is acceptable for the current resolution. After each frame is encoded the
34*d9f75844SAndroid Build Coastguard Worker[QualityScaler][quality_scaler.h] is given the QP of the encoded frame. Overuse
35*d9f75844SAndroid Build Coastguard Workeror underuse is signalled when the average QP is outside of the
36*d9f75844SAndroid Build Coastguard Worker[QP thresholds][VideoEncoder::QpThresholds]. If the average QP is above the
37*d9f75844SAndroid Build Coastguard Worker_high_ threshold, the QP scaler signals _overuse_, and when below the _low_
38*d9f75844SAndroid Build Coastguard Workerthreshold the QP scaler signals _underuse_.
39*d9f75844SAndroid Build Coastguard Worker
40*d9f75844SAndroid Build Coastguard WorkerThe thresholds are set by the video encoder in the `scaling_settings` property
41*d9f75844SAndroid Build Coastguard Workerof the [EncoderInfo][EncoderInfo].
42*d9f75844SAndroid Build Coastguard Worker
43*d9f75844SAndroid Build Coastguard Worker*Note:* that the QP scaler is only enabled when the degradation preference is
44*d9f75844SAndroid Build Coastguard Worker`MAINTAIN_FRAMERATE` or `BALANCED`.
45*d9f75844SAndroid Build Coastguard Worker
46*d9f75844SAndroid Build Coastguard Worker### Encode Usage Resource
47*d9f75844SAndroid Build Coastguard Worker
48*d9f75844SAndroid Build Coastguard WorkerThe [encoder usage resource][encode_usage_resource.h] monitors how long it takes
49*d9f75844SAndroid Build Coastguard Workerto encode a video frame. This works as a good proxy measurement for CPU usage as
50*d9f75844SAndroid Build Coastguard Workercontention increases when CPU usage is high, increasing the encode times of the
51*d9f75844SAndroid Build Coastguard Workervideo frames.
52*d9f75844SAndroid Build Coastguard Worker
53*d9f75844SAndroid Build Coastguard WorkerThe time is tracked from when frame encoding starts to when it is completed. If
54*d9f75844SAndroid Build Coastguard Workerthe average encoder usage exceeds the thresholds set, *overuse* is triggered.
55*d9f75844SAndroid Build Coastguard Worker
56*d9f75844SAndroid Build Coastguard Worker### Injecting other Resources
57*d9f75844SAndroid Build Coastguard Worker
58*d9f75844SAndroid Build Coastguard WorkerA custom resource can be injected into the call using the
59*d9f75844SAndroid Build Coastguard Worker[Call::AddAdaptationResource][Call::AddAdaptationResource] method.
60*d9f75844SAndroid Build Coastguard Worker
61*d9f75844SAndroid Build Coastguard Worker## Adaptation
62*d9f75844SAndroid Build Coastguard Worker
63*d9f75844SAndroid Build Coastguard WorkerWhen a a *resource* signals the it is over or underused, this signal reaches the
64*d9f75844SAndroid Build Coastguard Worker`ResourceAdaptationProcessor` who requests an `Adaptation` proposal from the
65*d9f75844SAndroid Build Coastguard Worker[VideoStreamAdapter][VideoStreamAdapter]. This proposal is based on the
66*d9f75844SAndroid Build Coastguard Workerdegradation preference of the video stream. `ResourceAdaptationProcessor` will
67*d9f75844SAndroid Build Coastguard Workerdetermine if the `Adaptation` should be applied based on the current adaptation
68*d9f75844SAndroid Build Coastguard Workerstatus and the `Adaptation` proposal.
69*d9f75844SAndroid Build Coastguard Worker
70*d9f75844SAndroid Build Coastguard Worker### Degradation Preference
71*d9f75844SAndroid Build Coastguard Worker
72*d9f75844SAndroid Build Coastguard WorkerThere are 3 degradation preferences, described in the
73*d9f75844SAndroid Build Coastguard Worker[RtpParameters][RtpParameters] header. These are
74*d9f75844SAndroid Build Coastguard Worker
75*d9f75844SAndroid Build Coastguard Worker*   `MAINTIAIN_FRAMERATE`: Adapt video resolution
76*d9f75844SAndroid Build Coastguard Worker*   `MAINTIAIN_RESOLUTION`: Adapt video frame-rate.
77*d9f75844SAndroid Build Coastguard Worker*   `BALANCED`: Adapt video frame-rate or resolution.
78*d9f75844SAndroid Build Coastguard Worker
79*d9f75844SAndroid Build Coastguard WorkerThe degradation preference is set for a video track using the
80*d9f75844SAndroid Build Coastguard Worker`degradation_preference` property in the [RtpParameters][RtpParameters].
81*d9f75844SAndroid Build Coastguard Worker
82*d9f75844SAndroid Build Coastguard Worker## VideoSinkWants and video stream adaptation
83*d9f75844SAndroid Build Coastguard Worker
84*d9f75844SAndroid Build Coastguard WorkerOnce an adaptation is applied it notifies the video stream. The video stream
85*d9f75844SAndroid Build Coastguard Workerconverts this adaptation to a [VideoSinkWants][VideoSinkWants]. These sink wants
86*d9f75844SAndroid Build Coastguard Workerindicate to the video stream that some restrictions should be applied to the
87*d9f75844SAndroid Build Coastguard Workerstream before it is sent to encoding. It has a few properties, but for
88*d9f75844SAndroid Build Coastguard Workeradaptation the properties that might be set are:
89*d9f75844SAndroid Build Coastguard Worker
90*d9f75844SAndroid Build Coastguard Worker*   `target_pixel_count`: The desired number of pixels for each video frame. The
91*d9f75844SAndroid Build Coastguard Worker    actual pixel count should be close to this but does not have to be exact so
92*d9f75844SAndroid Build Coastguard Worker    that aspect ratio can be maintained.
93*d9f75844SAndroid Build Coastguard Worker*   `max_pixel_count`: The maximum number of pixels in each video frame. This
94*d9f75844SAndroid Build Coastguard Worker    value can not be exceeded if set.
95*d9f75844SAndroid Build Coastguard Worker*   `max_framerate_fps`: The maximum frame-rate for the video source. The source
96*d9f75844SAndroid Build Coastguard Worker    is expected to drop frames that cause this threshold to be exceeded.
97*d9f75844SAndroid Build Coastguard Worker
98*d9f75844SAndroid Build Coastguard WorkerThe `VideoSinkWants` can be applied by any video source, or one may use the
99*d9f75844SAndroid Build Coastguard Worker[AdaptedVideoTraceSource][adapted_video_track_source.h] which is a base class
100*d9f75844SAndroid Build Coastguard Workerfor sources that need video adaptation.
101*d9f75844SAndroid Build Coastguard Worker
102*d9f75844SAndroid Build Coastguard Worker[RtpParameters]: https://source.chromium.org/chromium/chromium/src/+/main:third_party/webrtc/api/rtp_parameters.h?q=%22RTC_EXPORT%20RtpParameters%22
103*d9f75844SAndroid Build Coastguard Worker[resource.h]: https://source.chromium.org/chromium/chromium/src/+/main:third_party/webrtc/api/adaptation/resource.h
104*d9f75844SAndroid Build Coastguard Worker[Call::AddAdaptationResource]: https://source.chromium.org/chromium/chromium/src/+/main:third_party/webrtc/call/call.h?q=Call::AddAdaptationResource
105*d9f75844SAndroid Build Coastguard Worker[quality_scaler.h]: https://source.chromium.org/chromium/chromium/src/+/main:third_party/webrtc/modules/video_coding/utility/quality_scaler.h
106*d9f75844SAndroid Build Coastguard Worker[VideoEncoder::QpThresholds]: https://source.chromium.org/chromium/chromium/src/+/main:third_party/webrtc/api/video_codecs/video_encoder.h?q=VideoEncoder::QpThresholds
107*d9f75844SAndroid Build Coastguard Worker[EncoderInfo]: https://source.chromium.org/chromium/chromium/src/+/main:third_party/webrtc/api/video_codecs/video_encoder.h?q=VideoEncoder::EncoderInfo
108*d9f75844SAndroid Build Coastguard Worker[encode_usage_resource.h]: https://source.chromium.org/chromium/chromium/src/+/main:third_party/webrtc/video/adaptation/encode_usage_resource.h
109*d9f75844SAndroid Build Coastguard Worker[VideoStreamAdapter]: https://source.chromium.org/chromium/chromium/src/+/main:third_party/webrtc/call/adaptation/video_stream_adapter.h
110*d9f75844SAndroid Build Coastguard Worker[adaptation_constraint.h]: https://source.chromium.org/chromium/chromium/src/+/main:third_party/webrtc/call/adaptation/adaptation_constraint.h
111*d9f75844SAndroid Build Coastguard Worker[bitrate_constraint.h]: https://source.chromium.org/chromium/chromium/src/+/main:third_party/webrtc/video/adaptation/bitrate_constraint.h
112*d9f75844SAndroid Build Coastguard Worker[AddOrUpdateSink]: https://source.chromium.org/chromium/chromium/src/+/main:third_party/webrtc/api/video/video_source_interface.h?q=AddOrUpdateSink
113*d9f75844SAndroid Build Coastguard Worker[VideoSinkWants]: https://source.chromium.org/chromium/chromium/src/+/main:third_party/webrtc/api/video/video_source_interface.h?q=%22RTC_EXPORT%20VideoSinkWants%22
114*d9f75844SAndroid Build Coastguard Worker[adapted_video_track_source.h]: https://source.chromium.org/chromium/chromium/src/+/main:third_party/webrtc/media/base/adapted_video_track_source.h
115