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 #include "video_audio_sync.h" 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, uint64_t pts); 51 52 bool SetSurface(sptr<OHOS::Surface> surface); 53 bool SetDecoderFormat(const VideoTrack &track); 54 void SetVideoDecoderListener(VideoSinkDecoderListener::Ptr listener); 55 void SetVideoAudioSync(std::shared_ptr<VideoAudioSync> videoAudioSync); 56 57 void OnOutputFormatChanged(const MediaAVCodec::Format &format) override; 58 void OnError(MediaAVCodec::AVCodecErrorType errorType, int32_t errorCode) override; 59 void OnInputBufferAvailable(uint32_t index, std::shared_ptr<MediaAVCodec::AVSharedMemory> buffer) override; 60 void OnOutputBufferAvailable(uint32_t index, MediaAVCodec::AVCodecBufferInfo info, 61 MediaAVCodec::AVCodecBufferFlag flag, std::shared_ptr<MediaAVCodec::AVSharedMemory> buffer) override; 62 63 private: 64 bool InitDecoder(); 65 bool StopDecoder(); 66 bool StartDecoder(); 67 bool SetVideoCallback(); 68 69 public: 70 bool enableSurface_ = false; 71 bool forceSWDecoder_ = false; 72 uint32_t controlId_ = -1; 73 74 std::queue<int32_t> inQueue_; 75 std::queue<std::shared_ptr<MediaAVCodec::AVSharedMemory>> inBufferQueue_; 76 77 std::mutex inMutex_; 78 std::condition_variable inCond_; 79 std::atomic_bool isRunning_ = false; 80 std::weak_ptr<VideoSinkDecoderListener> videoDecoderListener_; 81 std::shared_ptr<OHOS::MediaAVCodec::AVCodecVideoDecoder> videoDecoder_ = nullptr; 82 83 VideoTrack videoTrack_; 84 CodecId videoCodecId_ = CODEC_NONE; 85 sptr<OHOS::Surface> surface_ = nullptr; 86 std::shared_ptr<VideoAudioSync> videoAudioSync_ = nullptr; 87 }; 88 } // namespace Sharing 89 } // namespace OHOS 90 #endif