|
| 1 | +/* |
| 2 | + * Copyright 2026 Alex Andres |
| 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 JNI_WEBRTC_MEDIA_VIDEO_CODEC_FALLBACK_VIDEO_ENCODER_H_ |
| 18 | +#define JNI_WEBRTC_MEDIA_VIDEO_CODEC_FALLBACK_VIDEO_ENCODER_H_ |
| 19 | + |
| 20 | +#include "api/video/video_frame.h" |
| 21 | +#include "api/video_codecs/video_codec.h" |
| 22 | +#include "api/video_codecs/video_encoder.h" |
| 23 | + |
| 24 | +#include <memory> |
| 25 | +#include <optional> |
| 26 | +#include <vector> |
| 27 | + |
| 28 | +namespace jni |
| 29 | +{ |
| 30 | + // Encodes with a hardware encoder, and switches to a software encoder of |
| 31 | + // the same codec when the hardware one fails to initialize, or gives up |
| 32 | + // while encoding by returning WEBRTC_VIDEO_CODEC_FALLBACK_SOFTWARE. The |
| 33 | + // switch is for good; the software encoder starts with a key frame. |
| 34 | + // |
| 35 | + // WebRTC has such a wrapper too, but it is not part of the WebRTC |
| 36 | + // library this library links. |
| 37 | + class FallbackVideoEncoder : public webrtc::VideoEncoder |
| 38 | + { |
| 39 | + public: |
| 40 | + FallbackVideoEncoder(std::unique_ptr<webrtc::VideoEncoder> hardware, |
| 41 | + std::unique_ptr<webrtc::VideoEncoder> software); |
| 42 | + ~FallbackVideoEncoder() override = default; |
| 43 | + |
| 44 | + int32_t InitEncode(const webrtc::VideoCodec * codecSettings, const Settings & settings) override; |
| 45 | + int32_t RegisterEncodeCompleteCallback(webrtc::EncodedImageCallback * callback) override; |
| 46 | + int32_t Release() override; |
| 47 | + int32_t Encode(const webrtc::VideoFrame & frame, const std::vector<webrtc::VideoFrameType> * frameTypes) override; |
| 48 | + void SetRates(const RateControlParameters & parameters) override; |
| 49 | + void OnPacketLossRateUpdate(float packetLossRate) override; |
| 50 | + void OnRttUpdate(int64_t rttMs) override; |
| 51 | + EncoderInfo GetEncoderInfo() const override; |
| 52 | + |
| 53 | + private: |
| 54 | + // Initializes the software encoder with the settings the hardware |
| 55 | + // one was given. Returns whether it is ready. |
| 56 | + bool StartSoftware(); |
| 57 | + |
| 58 | + webrtc::VideoEncoder * Active() const; |
| 59 | + |
| 60 | + private: |
| 61 | + const std::unique_ptr<webrtc::VideoEncoder> hardware; |
| 62 | + const std::unique_ptr<webrtc::VideoEncoder> software; |
| 63 | + |
| 64 | + bool useSoftware; |
| 65 | + bool initialized; |
| 66 | + |
| 67 | + std::optional<webrtc::VideoCodec> codecSettings; |
| 68 | + std::optional<Settings> encoderSettings; |
| 69 | + std::optional<RateControlParameters> rates; |
| 70 | + webrtc::EncodedImageCallback * callback; |
| 71 | + }; |
| 72 | +} |
| 73 | + |
| 74 | +#endif |
0 commit comments