|
| 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_DECODER_H_ |
| 18 | +#define JNI_WEBRTC_MEDIA_VIDEO_CODEC_FALLBACK_VIDEO_DECODER_H_ |
| 19 | + |
| 20 | +#include "api/video/encoded_image.h" |
| 21 | +#include "api/video_codecs/video_decoder.h" |
| 22 | + |
| 23 | +#include <memory> |
| 24 | +#include <optional> |
| 25 | + |
| 26 | +namespace jni |
| 27 | +{ |
| 28 | + // Decodes with a hardware decoder, and switches to a software decoder of |
| 29 | + // the same codec when the hardware one fails to configure, or gives up |
| 30 | + // while decoding by returning WEBRTC_VIDEO_CODEC_FALLBACK_SOFTWARE. The |
| 31 | + // switch is for good. The software decoder starts with the frame the |
| 32 | + // hardware one gave up on; if that is no key frame, it fails to decode |
| 33 | + // it, and the receiver asks the sender for 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 FallbackVideoDecoder : public webrtc::VideoDecoder |
| 38 | + { |
| 39 | + public: |
| 40 | + FallbackVideoDecoder(std::unique_ptr<webrtc::VideoDecoder> hardware, |
| 41 | + std::unique_ptr<webrtc::VideoDecoder> software); |
| 42 | + ~FallbackVideoDecoder() override = default; |
| 43 | + |
| 44 | + using webrtc::VideoDecoder::Decode; |
| 45 | + |
| 46 | + bool Configure(const Settings & settings) override; |
| 47 | + int32_t Decode(const webrtc::EncodedImage & image, int64_t renderTimeMs) override; |
| 48 | + int32_t RegisterDecodeCompleteCallback(webrtc::DecodedImageCallback * callback) override; |
| 49 | + int32_t Release() override; |
| 50 | + DecoderInfo GetDecoderInfo() const override; |
| 51 | + const char * ImplementationName() const override; |
| 52 | + |
| 53 | + private: |
| 54 | + // Configures the software decoder with the settings the hardware |
| 55 | + // one was given. Returns whether it is ready. |
| 56 | + bool StartSoftware(); |
| 57 | + |
| 58 | + webrtc::VideoDecoder * Active() const; |
| 59 | + |
| 60 | + private: |
| 61 | + const std::unique_ptr<webrtc::VideoDecoder> hardware; |
| 62 | + const std::unique_ptr<webrtc::VideoDecoder> software; |
| 63 | + |
| 64 | + bool useSoftware; |
| 65 | + |
| 66 | + std::optional<Settings> settings; |
| 67 | + webrtc::DecodedImageCallback * callback; |
| 68 | + }; |
| 69 | +} |
| 70 | + |
| 71 | +#endif |
0 commit comments