| 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_H265_ENCODER_IMPL_H_ |
| 18 | #define WEBRTC_JETSON_H265_ENCODER_IMPL_H_ |
| 19 | |
| 20 | #include <cstdint> |
| 21 | #include <memory> |
| 22 | #include <vector> |
| 23 | |
| 24 | #include "absl/container/inlined_vector.h" |
| 25 | #include "api/environment/environment.h" |
| 26 | #include "api/transport/rtp/dependency_descriptor.h" |
| 27 | #include "api/video/i420_buffer.h" |
| 28 | #include "api/video/video_codec_constants.h" |
| 29 | #include "api/video_codecs/sdp_video_format.h" |
| 30 | #include "api/video_codecs/scalability_mode.h" |
| 31 | #include "api/video_codecs/video_encoder.h" |
| 32 | #include "modules/video_coding/svc/scalable_video_controller.h" |
| 33 | #include "modules/video_coding/utility/quality_scaler.h" |
| 34 | |
| 35 | #include "jetson_mmapi_encoder.h" |
| 36 | |
| 37 | namespace webrtc { |
| 38 | |
| 39 | class JetsonH265EncoderImpl : public VideoEncoder { |
| 40 | public: |
| 41 | struct LayerConfig { |
| 42 | int simulcast_idx = 0; |
| 43 | int width = -1; |
| 44 | int height = -1; |
| 45 | bool sending = true; |
| 46 | bool key_frame_request = false; |
| 47 | float max_frame_rate = 0; |
| 48 | uint32_t target_bps = 0; |
| 49 | uint32_t max_bps = 0; |
| 50 | bool frame_dropping_on = false; |
| 51 | int key_frame_interval = 0; |
| 52 | int num_temporal_layers = 1; |
| 53 | |
| 54 | void SetStreamState(bool send_stream); |
| 55 | }; |
| 56 | |
| 57 | public: |
| 58 | JetsonH265EncoderImpl(const webrtc::Environment& env, |
| 59 | const SdpVideoFormat& format); |
| 60 | ~JetsonH265EncoderImpl() override; |
| 61 | |
| 62 | int32_t InitEncode(const VideoCodec* codec_settings, |
| 63 | const Settings& settings) override; |
| 64 | |
| 65 | int32_t RegisterEncodeCompleteCallback( |
| 66 | EncodedImageCallback* callback) override; |
| 67 | |
| 68 | int32_t Release() override; |
| 69 | |
| 70 | int32_t Encode(const VideoFrame& frame, |
| 71 | const std::vector<VideoFrameType>* frame_types) override; |
| 72 | |
| 73 | void SetRates(const RateControlParameters& rc_parameters) override; |
| 74 | |
| 75 | EncoderInfo GetEncoderInfo() const override; |
| 76 | |
| 77 | private: |
| 78 | int32_t ProcessEncodedFrame(std::vector<uint8_t>& packet, |
| 79 | const ::webrtc::VideoFrame& input_frame, |
| 80 | bool is_keyframe); |
| 81 | |
| 82 | const webrtc::Environment& env_; |
| 83 | EncodedImageCallback* encoded_image_callback_ = nullptr; |
| 84 | livekit::JetsonMmapiEncoder encoder_; |
| 85 | LayerConfig configuration_; |
| 86 | EncodedImage encoded_image_; |
| 87 | VideoCodec codec_; |
| 88 | void ReportInit(); |
| 89 | void ReportError(); |
| 90 | bool has_reported_init_ = false; |
| 91 | bool has_reported_error_ = false; |
| 92 | const SdpVideoFormat format_; |
| 93 | }; |
| 94 | |
| 95 | } // namespace webrtc |
| 96 | |
| 97 | #endif // WEBRTC_JETSON_H265_ENCODER_IMPL_H_ |