1 /*
2  * Copyright (c) 2022-2023 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 <cstddef>
24 
25 #include "buffer/avsharedmemory.h"
26 #include "avcodec_common.h"
27 #include "avcodec_video_decoder.h"
28 #include "avcodec_errors.h"
29 #include "meta/format.h"
30 #include "surface.h"
31 #include "iconsumer_surface.h"
32 
33 #include "data_buffer.h"
34 #include "iimage_sink_processor_listener.h"
35 #include "image_decoder_callback.h"
36 #include "video_param.h"
37 
38 namespace OHOS {
39 namespace DistributedHardware {
40 class ConsumBufferListener : public IBufferConsumerListener {
41 public:
ConsumBufferListener(const std::shared_ptr<ImageSinkDecoder> decoder)42     ConsumBufferListener(const std::shared_ptr<ImageSinkDecoder> decoder) : decoder_(decoder) {};
43     ~ConsumBufferListener() = default;
44     void OnBufferAvailable() override;
45 private:
46     static const constexpr char *DSCREEN_LOG_TAG = "ConsumBufferListener";
47     std::shared_ptr<ImageSinkDecoder> decoder_;
48 };
49 class ImageSinkDecoder : public std::enable_shared_from_this<ImageSinkDecoder> {
50 public:
ImageSinkDecoder(const std::shared_ptr<IImageSinkProcessorListener> & imageListener)51     explicit ImageSinkDecoder(const std::shared_ptr<IImageSinkProcessorListener> &imageListener)
52         : imageProcessorListener_(imageListener) {};
53     ~ImageSinkDecoder() = default;
54 
55     int32_t ConfigureDecoder(const VideoParam &configParam);
56     int32_t ReleaseDecoder();
57     int32_t StartDecoder();
58     int32_t StopDecoder();
59     int32_t SetOutputSurface(sptr<Surface> &surface);
60     int32_t InputScreenData(const std::shared_ptr<DataBuffer> &data);
61     int32_t AddSurface();
62     void ConsumeSurface();
63     uint8_t *GetLastFrame();
64     sptr<SurfaceBuffer> GetWinSurfaceBuffer();
65     void NormalProcess(sptr<SurfaceBuffer> surfaceBuffer, sptr<SurfaceBuffer> windowSurfaceBuffer);
66     void OffsetProcess(sptr<SurfaceBuffer> surfaceBuffer, sptr<SurfaceBuffer> windowSurfaceBuffer);
67     void OnError(MediaAVCodec::AVCodecErrorType errorType, int32_t errorCode);
68     void OnInputBufferAvailable(uint32_t index, std::shared_ptr<Media::AVSharedMemory> buffer);
69     void OnOutputBufferAvailable(uint32_t index, MediaAVCodec::AVCodecBufferInfo info,
70                                  MediaAVCodec::AVCodecBufferFlag flag, std::shared_ptr<Media::AVSharedMemory> buffer);
71     void OnOutputFormatChanged(const Media::Format &format);
72 
73 private:
74     int32_t SetDecoderFormat(const VideoParam &configParam);
75     int32_t InitVideoDecoder(const VideoParam &configParam);
76     int32_t StartInputThread();
77     int32_t StopInputThread();
78     void DecodeScreenData();
79     int32_t ProcessData(const std::shared_ptr<DataBuffer> &screenData, const int32_t bufferIndex);
80 
81 private:
82     static const constexpr char *DSCREEN_LOG_TAG = "ImageSinkDecoder";
83     VideoParam configParam_;
84     std::mutex dataMutex_;
85     std::mutex decodeMutex_;
86     std::thread decodeThread_;
87     std::condition_variable decodeCond_;
88 
89     uint8_t *lastFrame_ = nullptr;
90     int32_t lastFrameSize_ = 0;
91     Media::Format imageFormat_;
92     Media::Format decodeOutputFormat_;
93     MediaAVCodec::AVCodecBufferInfo decoderBufferInfo_;
94 
95     bool isDecoderReady_ = false;
96     uint32_t alignedHeight_ = 0;
97     sptr<IConsumerSurface> consumerSurface_;
98     sptr<Surface> producerSurface_;
99     sptr<Surface> windowSurface_;
100     sptr<IBufferConsumerListener> consumerBufferListener_;
101     std::queue<std::shared_ptr<DataBuffer>> videoDataQueue_;
102     std::queue<int32_t> availableInputIndexsQueue_;
103     std::queue<std::shared_ptr<Media::AVSharedMemory>> availableInputBufferQueue_;
104     std::shared_ptr<MediaAVCodec::AVCodecVideoDecoder> videoDecoder_;
105     std::shared_ptr<MediaAVCodec::AVCodecCallback> decodeVideoCallback_;
106     std::weak_ptr<IImageSinkProcessorListener> imageProcessorListener_;
107 };
108 } // namespace DistributedHardware
109 } // namespace OHOS
110 #endif