1 /* 2 * Copyright (c) 2022 Huawei Device 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_IMAGE_SINK_DECODER_H 17 #define OHOS_IMAGE_SINK_DECODER_H 18 19 #include <thread> 20 #include <mutex> 21 #include <queue> 22 #include <condition_variable> 23 #include <stddef.h> 24 25 #include "avsharedmemory.h" 26 #include "avcodec_common.h" 27 #include "avcodec_video_decoder.h" 28 #include "media_errors.h" 29 #include "format.h" 30 #include "surface.h" 31 32 #include "data_buffer.h" 33 #include "iimage_sink_processor_listener.h" 34 #include "image_decoder_callback.h" 35 #include "video_param.h" 36 37 namespace OHOS { 38 namespace DistributedHardware { 39 class ImageSinkDecoder : public std::enable_shared_from_this<ImageSinkDecoder> { 40 public: ImageSinkDecoder(const std::shared_ptr<IImageSinkProcessorListener> & imageListener)41 ImageSinkDecoder(const std::shared_ptr<IImageSinkProcessorListener> &imageListener) 42 : imageProcessorListener_(imageListener) {}; 43 ~ImageSinkDecoder() = default; 44 45 int32_t ConfigureDecoder(const VideoParam &configParam); 46 int32_t ReleaseDecoder(); 47 int32_t StartDecoder(); 48 int32_t StopDecoder(); 49 int32_t SetOutputSurface(sptr<Surface> &surface); 50 int32_t InputScreenData(const std::shared_ptr<DataBuffer> &data); 51 52 void OnError(Media::AVCodecErrorType errorType, int32_t errorCode); 53 void OnInputBufferAvailable(uint32_t index); 54 void OnOutputBufferAvailable(uint32_t index, Media::AVCodecBufferInfo info, Media::AVCodecBufferFlag flag); 55 void OnOutputFormatChanged(const Media::Format &format); 56 57 private: 58 int32_t SetDecoderFormat(const VideoParam &configParam); 59 int32_t InitVideoDecoder(const VideoParam &configParam); 60 int32_t StartInputThread(); 61 int32_t StopInputThread(); 62 void DecodeScreenData(); 63 int32_t ProcessData(const std::shared_ptr<DataBuffer> &screenData, const int32_t bufferIndex); 64 65 private: 66 static const constexpr char *LOG_TAG = "ImageSinkDecoder"; 67 static constexpr uint32_t DECODE_WAIT_MILLISECONDS = 5000; 68 static constexpr size_t DATA_QUEUE_MAX_SIZE = 1000; 69 70 std::mutex dataMutex_; 71 std::mutex decodeMutex_; 72 std::thread decodeThread_; 73 std::condition_variable decodeCond_; 74 75 Media::Format imageFormat_; 76 Media::AVCodecBufferInfo decoderBufferInfo_; 77 78 bool isDecoderReady_ = false; 79 std::queue<std::shared_ptr<DataBuffer>> videoDataQueue_; 80 std::queue<int32_t> bufferIndexQueue_; 81 std::shared_ptr<Media::AVCodecVideoDecoder> videoDecoder_; 82 std::shared_ptr<Media::AVCodecCallback> decodeVideoCallback_; 83 std::weak_ptr<IImageSinkProcessorListener> imageProcessorListener_; 84 }; 85 } // namespace DistributedHardware 86 } // namespace OHOS 87 #endif