| 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 | #ifndef WEBRTC_JETSON_AV1_ENCODER_IMPL_H_ |
| 18 | #define WEBRTC_JETSON_AV1_ENCODER_IMPL_H_ |
| 19 | |
| 20 | #include <cstdint> |
| 21 | #include <vector> |
| 22 | |
| 23 | #include "api/environment/environment.h" |
| 24 | #include "api/video/video_codec_constants.h" |
| 25 | #include "api/video_codecs/sdp_video_format.h" |
| 26 | #include "api/video_codecs/video_encoder.h" |
| 27 | #include "modules/video_coding/svc/scalable_video_controller_no_layering.h" |
| 28 | |
| 29 | #include "jetson_mmapi_encoder.h" |
| 30 | |
| 31 | namespace webrtc { |
| 32 | |
| 33 | class JetsonAV1EncoderImpl : public VideoEncoder { |
| 34 | public: |
| 35 | struct LayerConfig { |
| 36 | int simulcast_idx = 0; |
| 37 | int width = -1; |
| 38 | int height = -1; |
| 39 | bool sending = true; |
| 40 | bool key_frame_request = false; |
| 41 | float max_frame_rate = 0; |
| 42 | uint32_t target_bps = 0; |
| 43 | uint32_t max_bps = 0; |
| 44 | bool frame_dropping_on = false; |
| 45 | int key_frame_interval = 0; |
| 46 | int num_temporal_layers = 1; |
| 47 | |
| 48 | void SetStreamState(bool send_stream); |
| 49 | }; |
| 50 | |
| 51 | public: |
| 52 | JetsonAV1EncoderImpl(const webrtc::Environment& env, |
| 53 | const SdpVideoFormat& format); |
| 54 | ~JetsonAV1EncoderImpl() override; |
| 55 | |
| 56 | int32_t InitEncode(const VideoCodec* codec_settings, |
| 57 | const Settings& settings) override; |
| 58 | |
| 59 | int32_t RegisterEncodeCompleteCallback( |
| 60 | EncodedImageCallback* callback) override; |
| 61 | |
| 62 | int32_t Release() override; |
| 63 | |
| 64 | int32_t Encode(const VideoFrame& frame, |
| 65 | const std::vector<VideoFrameType>* frame_types) override; |
| 66 | |
| 67 | void SetRates(const RateControlParameters& rc_parameters) override; |
| 68 | |
| 69 | EncoderInfo GetEncoderInfo() const override; |
| 70 | |
| 71 | private: |
| 72 | int32_t ProcessEncodedFrame(std::vector<uint8_t>& packet, |
| 73 | const ::webrtc::VideoFrame& input_frame, |
| 74 | bool is_keyframe); |
| 75 | |
| 76 | const webrtc::Environment& env_; |
| 77 | EncodedImageCallback* encoded_image_callback_ = nullptr; |
| 78 | livekit::JetsonMmapiEncoder encoder_; |
| 79 | LayerConfig configuration_; |
| 80 | EncodedImage encoded_image_; |
| 81 | VideoCodec codec_; |
| 82 | void ReportInit(); |
| 83 | void ReportError(); |
| 84 | bool has_reported_init_ = false; |
| 85 | bool has_reported_error_ = false; |
| 86 | bool sent_decodable_keyframe_ = false; |
| 87 | std::vector<uint8_t> cached_sequence_header_obu_; |
| 88 | // Produces the per-frame GenericFrameInfo and the keyframe |
| 89 | // FrameDependencyStructure that back the AV1 dependency descriptor. The |
| 90 | // Jetson hardware only emits an OBU bitstream, so (like libaom's encoder and |
| 91 | // WebRTC's SimpleEncoderWrapper) we synthesize the L1T1 scalability metadata |
| 92 | // here. Without it the RTP layer has no dependency descriptor to send and the |
| 93 | // packetizer never emits packets for AV1. |
| 94 | ScalableVideoControllerNoLayering svc_controller_; |
| 95 | const SdpVideoFormat format_; |
| 96 | }; |
| 97 | |
| 98 | } // namespace webrtc |
| 99 | |
| 100 | #endif // WEBRTC_JETSON_AV1_ENCODER_IMPL_H_ |