| 1 | /* |
| 2 | * Copyright 2026 LiveKit, Inc. |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #include "livekit/encoded_video_frame_buffer.h" |
| 18 | |
| 19 | #include <utility> |
| 20 | |
| 21 | #include "api/video/i420_buffer.h" |
| 22 | #include "rtc_base/logging.h" |
| 23 | |
| 24 | namespace livekit { |
| 25 | |
| 26 | void EncodedRateControlState::Store(uint64_t target_bitrate_bps, |
| 27 | double framerate_fps) { |
| 28 | std::lock_guard<std::mutex> lock(mutex_); |
| 29 | request_.has_request = true; |
| 30 | request_.target_bitrate_bps = target_bitrate_bps; |
| 31 | request_.framerate_fps = framerate_fps; |
| 32 | } |
| 33 | |
| 34 | EncodedRateControlRequest EncodedRateControlState::Take() { |
| 35 | std::lock_guard<std::mutex> lock(mutex_); |
| 36 | EncodedRateControlRequest request = request_; |
| 37 | request_ = EncodedRateControlRequest(); |
| 38 | return request; |
| 39 | } |
| 40 | |
| 41 | EncodedVideoFrameBuffer::EncodedVideoFrameBuffer( |
| 42 | int width, |
| 43 | int height, |
| 44 | EncodedVideoCodec codec, |
| 45 | EncodedFrameType frame_type, |
| 46 | webrtc::scoped_refptr<webrtc::EncodedImageBuffer> payload, |
| 47 | std::shared_ptr<std::atomic<bool>> keyframe_request_flag, |
| 48 | std::shared_ptr<EncodedRateControlState> rate_control_state) |
| 49 | : width_(width), |
| 50 | height_(height), |
| 51 | codec_(codec), |
| 52 | frame_type_(frame_type), |
| 53 | payload_(std::move(payload)), |
| 54 | keyframe_request_flag_(std::move(keyframe_request_flag)), |
| 55 | rate_control_state_(std::move(rate_control_state)) {} |
| 56 | |
| 57 | webrtc::VideoFrameBuffer::Type EncodedVideoFrameBuffer::type() const { |
| 58 | return Type::kNative; |
| 59 | } |
| 60 | |
| 61 | int EncodedVideoFrameBuffer::width() const { |
| 62 | return width_; |
| 63 | } |
| 64 | |
| 65 | int EncodedVideoFrameBuffer::height() const { |
| 66 | return height_; |
| 67 | } |
| 68 | |
| 69 | webrtc::scoped_refptr<webrtc::I420BufferInterface> |
| 70 | EncodedVideoFrameBuffer::ToI420() { |
| 71 | // Sinks attached to a pre-encoded track (local preview, FFI color |
| 72 | // conversion) convert whatever buffer they receive; the encoded payload |
| 73 | // cannot be decoded here, so hand back a black frame instead of a null |
| 74 | // buffer that would crash the caller. |
| 75 | static std::atomic<bool> logged{false}; |
| 76 | if (!logged.exchange(true)) { |
| 77 | RTC_LOG(LS_WARNING) << "EncodedVideoFrameBuffer::ToI420 cannot decode an " |
| 78 | "encoded access unit; returning black frames"; |
| 79 | } |
| 80 | webrtc::scoped_refptr<webrtc::I420Buffer> buffer = |
| 81 | webrtc::I420Buffer::Create(width_, height_); |
| 82 | webrtc::I420Buffer::SetBlack(buffer.get()); |
| 83 | return buffer; |
| 84 | } |
| 85 | |
| 86 | webrtc::scoped_refptr<webrtc::VideoFrameBuffer> |
| 87 | EncodedVideoFrameBuffer::CropAndScale(int /* offset_x */, |
| 88 | int /* offset_y */, |
| 89 | int /* crop_width */, |
| 90 | int /* crop_height */, |
| 91 | int /* scaled_width */, |
| 92 | int /* scaled_height */) { |
| 93 | // Encoded payloads cannot be rescaled; returning the buffer unchanged |
| 94 | // keeps misbehaving callers alive (the capture path never scales encoded |
| 95 | // frames). |
| 96 | RTC_LOG(LS_WARNING) << "EncodedVideoFrameBuffer::CropAndScale is " |
| 97 | "unsupported; returning the frame unscaled"; |
| 98 | return webrtc::scoped_refptr<webrtc::VideoFrameBuffer>(this); |
| 99 | } |
| 100 | |
| 101 | void EncodedVideoFrameBuffer::request_keyframe() const { |
| 102 | if (keyframe_request_flag_) { |
| 103 | keyframe_request_flag_->store(true, std::memory_order_relaxed); |
| 104 | } |
| 105 | } |
| 106 | |
| 107 | void EncodedVideoFrameBuffer::set_rate_control_request( |
| 108 | uint64_t target_bitrate_bps, |
| 109 | double framerate_fps) const { |
| 110 | if (rate_control_state_) { |
| 111 | rate_control_state_->Store(target_bitrate_bps, framerate_fps); |
| 112 | } |
| 113 | } |
| 114 | |
| 115 | EncodedVideoFrameBuffer* EncodedVideoFrameBuffer::FromNative( |
| 116 | webrtc::VideoFrameBuffer* buffer) { |
| 117 | if (!buffer || buffer->type() != webrtc::VideoFrameBuffer::Type::kNative) { |
| 118 | return nullptr; |
| 119 | } |
| 120 | return dynamic_cast<EncodedVideoFrameBuffer*>(buffer); |
| 121 | } |
| 122 | |
| 123 | } // namespace livekit |