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 CODEC_LISTENER_STUB_H
17 #define CODEC_LISTENER_STUB_H
18 
19 #include <atomic>
20 #include <condition_variable>
21 #include <mutex>
22 #include <thread>
23 #include <unordered_map>
24 #include "avcodec_common.h"
25 #include "avcodec_log.h"
26 #include "buffer_converter.h"
27 #include "i_standard_codec_listener.h"
28 
29 namespace OHOS {
30 namespace MediaAVCodec {
31 class CodecListenerStub : public IRemoteStub<IStandardCodecListener> {
32 public:
33     CodecListenerStub();
34     virtual ~CodecListenerStub();
35     int OnRemoteRequest(uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option) override;
36     void OnError(AVCodecErrorType errorType, int32_t errorCode) override;
37     void OnOutputFormatChanged(const Format &format) override;
38     void OnInputBufferAvailable(uint32_t index, std::shared_ptr<AVBuffer> buffer) override;
39     void OnOutputBufferAvailable(uint32_t index, std::shared_ptr<AVBuffer> buffer) override;
40 
41     void SetCallback(const std::shared_ptr<AVCodecCallback> &callback);
42     void SetCallback(const std::shared_ptr<MediaCodecCallback> &callback);
43     void SetCallback(const std::shared_ptr<MediaCodecParameterCallback> &callback);
44     void SetCallback(const std::shared_ptr<MediaCodecParameterWithAttrCallback> &callback);
45 
46     void ClearListenerCache();
47     void FlushListenerCache();
48     bool WriteInputParameterToParcel(uint32_t index, MessageParcel &data);
49     bool WriteInputBufferToParcel(uint32_t index, MessageParcel &data);
50     bool WriteInputMemoryToParcel(uint32_t index, AVCodecBufferInfo info, AVCodecBufferFlag flag, MessageParcel &data);
51     bool WriteOutputBufferToParcel(uint32_t index, MessageParcel &data);
52 
53     void SetMutex(std::shared_ptr<std::recursive_mutex> &mutex);
54     void SetConverter(std::shared_ptr<BufferConverter> &converter);
55     void SetNeedListen(const bool needListen);
56     void InitLabel(const uint64_t uid);
57 
58 private:
59     void OnInputBufferAvailable(uint32_t index, MessageParcel &data);
60     void OnOutputBufferAvailable(uint32_t index, MessageParcel &data);
61     bool CheckGeneration(uint64_t messageGeneration) const;
62 
63     class CodecBufferCache;
64     std::unique_ptr<CodecBufferCache> inputBufferCache_;
65     std::unique_ptr<CodecBufferCache> outputBufferCache_;
66     std::weak_ptr<AVCodecCallback> callback_;
67     std::weak_ptr<MediaCodecCallback> videoCallback_;
68     std::weak_ptr<MediaCodecParameterCallback> paramCallback_;
69     std::weak_ptr<MediaCodecParameterWithAttrCallback> paramWithAttrCallback_;
70     bool needListen_ = false;
71     std::shared_ptr<std::recursive_mutex> syncMutex_;
72     std::shared_ptr<BufferConverter> converter_ = nullptr;
73 
74     const OHOS::HiviewDFX::HiLogLabel LABEL = {LOG_CORE, LOG_DOMAIN_FRAMEWORK, "CodecListenerStub"};
75     std::string tag_ = "";
76 };
77 } // namespace MediaAVCodec
78 } // namespace OHOS
79 #endif // CODEC_LISTENER_STUB_H
80