• 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 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_dfx_component.h"
26 #include "avcodec_log.h"
27 #include "buffer_converter.h"
28 #include "i_standard_codec_listener.h"
29 #include "surface_buffer.h"
30 
31 namespace OHOS {
32 namespace MediaAVCodec {
33 class CodecListenerStub : public IRemoteStub<IStandardCodecListener>, public AVCodecDfxComponent {
34 public:
35     CodecListenerStub();
36     virtual ~CodecListenerStub();
37     int OnRemoteRequest(uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option) override;
38     int OnRequestExtras(uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option);
39     void OnError(AVCodecErrorType errorType, int32_t errorCode) override;
40     void OnOutputFormatChanged(const Format &format) override;
41     void OnInputBufferAvailable(uint32_t index, std::shared_ptr<AVBuffer> buffer) override;
42     void OnOutputBufferAvailable(uint32_t index, std::shared_ptr<AVBuffer> buffer) override;
43     void OnOutputBufferBinded(std::map<uint32_t, sptr<SurfaceBuffer>> &bufferMap) override;
44     void OnOutputBufferUnbinded() override;
45 
46     void SetCallback(const std::shared_ptr<MediaCodecCallback> &callback);
47     void Init();
48     void ClearListenerCache();
49     void FlushListenerCache();
50     bool WriteInputBufferToParcel(uint32_t index, MessageParcel &data);
51     bool WriteOutputBufferToParcel(uint32_t index, MessageParcel &data);
52 
53     void SetMutex(std::shared_ptr<std::recursive_mutex> &mutex);
54     void SetNeedListen(const bool needListen);
55 
56 private:
57     void OnInputBufferAvailable(uint32_t index, MessageParcel &data);
58     void OnOutputBufferAvailable(uint32_t index, MessageParcel &data);
59     void OnOutputBufferBinded(MessageParcel &data);
60     void OnOutputBufferUnbinded(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<MediaCodecCallback> callback_;
67     bool needListen_ = false;
68     std::shared_ptr<std::recursive_mutex> syncMutex_;
69 };
70 } // namespace MediaAVCodec
71 } // namespace OHOS
72 #endif // CODEC_LISTENER_STUB_H
73