1 /* 2 * Copyright (c) 2023 Shenzhen Kaihong Digital Industry Development Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16 #ifndef OHOS_SHARING_VIDEO_SINK_DECODER_H 17 #define OHOS_SHARING_VIDEO_SINK_DECODER_H 18 19 #include <condition_variable> 20 #include <queue> 21 #include "avcodec_video_decoder.h" 22 #include "common/const_def.h" 23 #include "common/event_comm.h" 24 #include "frame.h" 25 #include "utils/data_buffer.h" 26 27 28 namespace OHOS { 29 namespace Sharing { 30 static constexpr uint32_t DECODE_WAIT_MILLISECONDS = 5000; 31 class VideoSinkDecoderListener { 32 public: 33 using Ptr = std::shared_ptr<VideoSinkDecoderListener>; 34 virtual ~VideoSinkDecoderListener() = default; 35 36 virtual void OnError(int32_t errorCode) = 0; 37 virtual void OnVideoDataDecoded(DataBuffer::Ptr decodedData) = 0; 38 }; 39 40 class VideoSinkDecoder : public MediaAVCodec::AVCodecCallback, 41 public std::enable_shared_from_this<VideoSinkDecoder> { 42 public: 43 explicit VideoSinkDecoder(uint32_t controlId, bool forceSWDecoder = false); 44 virtual ~VideoSinkDecoder(); 45 46 bool Start(); 47 void Stop(); 48 void Release(); 49 bool Init(CodecId videoCodecId = CODEC_H264); 50 bool DecodeVideoData(const char *data, int32_t size); 51 52 bool SetSurface(sptr<OHOS::Surface> surface); 53 bool SetDecoderFormat(const VideoTrack &track); 54 void SetVideoDecoderListener(VideoSinkDecoderListener::Ptr listener); 55 56 void OnOutputFormatChanged(const MediaAVCodec::Format &format) override; 57 void OnError(MediaAVCodec::AVCodecErrorType errorType, int32_t errorCode) override; 58 void OnInputBufferAvailable(uint32_t index, std::shared_ptr<MediaAVCodec::AVSharedMemory> buffer) override; 59 void OnOutputBufferAvailable(uint32_t index, MediaAVCodec::AVCodecBufferInfo info, 60 MediaAVCodec::AVCodecBufferFlag flag, std::shared_ptr<MediaAVCodec::AVSharedMemory> buffer) override; 61 62 private: 63 bool InitDecoder(); 64 bool StopDecoder(); 65 bool StartDecoder(); 66 bool SetVideoCallback(); 67 68 public: 69 bool enableSurface_ = false; 70 bool forceSWDecoder_ = false; 71 uint32_t controlId_ = -1; 72 73 std::queue<int32_t> inQueue_; 74 std::queue<std::shared_ptr<MediaAVCodec::AVSharedMemory>> inBufferQueue_; 75 76 std::mutex inMutex_; 77 std::condition_variable inCond_; 78 std::atomic_bool isRunning_ = false; 79 std::weak_ptr<VideoSinkDecoderListener> videoDecoderListener_; 80 std::shared_ptr<OHOS::MediaAVCodec::AVCodecVideoDecoder> videoDecoder_ = nullptr; 81 82 VideoTrack videoTrack_; 83 CodecId videoCodecId_ = CODEC_NONE; 84 sptr<OHOS::Surface> surface_ = nullptr; 85 }; 86 } // namespace Sharing 87 } // namespace OHOS 88 #endif