• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 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 VIDEO_DECODER_ADAPTER_H
17 #define VIDEO_DECODER_ADAPTER_H
18 
19 #include <shared_mutex>
20 #include <vector>
21 #include "surface.h"
22 #include "avcodec_video_decoder.h"
23 #include "buffer/avbuffer.h"
24 #include "buffer/avbuffer_queue.h"
25 #include "buffer/avbuffer_queue_producer.h"
26 #include "osal/task/condition_variable.h"
27 #include "meta/format.h"
28 #include "video_sink.h"
29 
30 namespace OHOS {
31 namespace Media {
32 class VideoDecoderAdapter : public std::enable_shared_from_this<VideoDecoderAdapter> {
33 public:
34     VideoDecoderAdapter();
35     virtual ~VideoDecoderAdapter();
36 
37     int32_t Init(MediaAVCodec::AVCodecType type, bool isMimeType, const std::string &name);
38     int32_t Configure(const Format &format);
39     int32_t SetParameter(const Format &format);
40     int32_t Start();
41     int32_t Pause();
42     int32_t Flush();
43     int32_t Resume();
44     int32_t Stop();
45     int32_t Reset();
46     int32_t Release();
47     int32_t SetCallback(const std::shared_ptr<MediaAVCodec::MediaCodecCallback> &callback);
48 
49     sptr<OHOS::Media::AVBufferQueueProducer> GetInputBufferQueue();
50     void OnInputBufferAvailable(uint32_t index, std::shared_ptr<AVBuffer> buffer);
51     void OnError(MediaAVCodec::AVCodecErrorType errorType, int32_t errorCode);
52     void OnOutputFormatChanged(const MediaAVCodec::Format &format);
53     void OnOutputBufferAvailable(uint32_t index, std::shared_ptr<AVBuffer> buffer);
54     int32_t ReleaseOutputBuffer(uint32_t index, std::shared_ptr<Pipeline::VideoSink> videoSink,
55         std::shared_ptr<AVBuffer> &outputBuffer, bool doSync);
56     void AquireAvailableInputBuffer();
57     int32_t SetOutputSurface(sptr<Surface> videoSurface);
58     int32_t GetOutputFormat(Format &format);
59     void SetEventReceiver(const std::shared_ptr<Pipeline::EventReceiver>& receiver);
60 
61     int32_t SetDecryptConfig(const sptr<DrmStandard::IMediaKeySessionService> &keySession,
62         const bool svpFlag);
63 
64 private:
65     void RenderLoop();
66     std::shared_ptr<Media::AVBufferQueue> inputBufferQueue_;
67     sptr<Media::AVBufferQueueProducer> inputBufferQueueProducer_;
68     sptr<Media::AVBufferQueueConsumer> inputBufferQueueConsumer_;
69 
70     std::shared_ptr<MediaAVCodec::AVCodecVideoDecoder> mediaCodec_;
71     std::shared_ptr<MediaAVCodec::MediaCodecCallback> callback_;
72     std::shared_ptr<AVBuffer> buffer_;
73 
74     std::unique_ptr<std::thread> readThread_ = nullptr;
75     std::shared_ptr<Pipeline::EventReceiver> eventReceiver_ {nullptr};
76 
77     std::condition_variable condBufferAvailable_;
78     std::list<std::function<void()>> indexs_;
79     std::mutex mutex_;
80     std::atomic<bool> isThreadExit_ = true;
81     std::atomic<bool> isPaused_ = false;
82     std::vector<std::shared_ptr<AVBuffer>> bufferVector_;
83 };
84 
85 class AVBufferAvailableListener : public OHOS::Media::IConsumerListener {
86 public:
87     AVBufferAvailableListener(std::shared_ptr<VideoDecoderAdapter> videoDecoder);
88     virtual ~AVBufferAvailableListener();
89 
90     void OnBufferAvailable();
91 private:
92     std::weak_ptr<VideoDecoderAdapter> videoDecoder_;
93 };
94 
95 class VideoDecoderCallback : public OHOS::MediaAVCodec::MediaCodecCallback {
96 public:
97     explicit VideoDecoderCallback(std::shared_ptr<VideoDecoderAdapter> videoDecoder);
98     virtual ~VideoDecoderCallback();
99 
100     void OnError(MediaAVCodec::AVCodecErrorType errorType, int32_t errorCode);
101     void OnOutputFormatChanged(const MediaAVCodec::Format &format);
102     void OnInputBufferAvailable(uint32_t index, std::shared_ptr<AVBuffer> buffer);
103     void OnOutputBufferAvailable(uint32_t index, std::shared_ptr<AVBuffer> buffer);
104 
105 private:
106     std::weak_ptr<VideoDecoderAdapter> videoDecoderAdapter_;
107 };
108 } // namespace Media
109 } // namespace OHOS
110 #endif // VIDEO_DECODER_ADAPTER_H