• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2 * Copyright (c) 2023-2025 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 FILTERS_CODEC_FILTER_H
17 #define FILTERS_CODEC_FILTER_H
18 
19 #include <atomic>
20 #include <cstring>
21 #include <mutex>
22 #include "media_codec/media_codec.h"
23 #include "filter/filter.h"
24 #include "plugin/plugin_time.h"
25 #include "audio_decoder_adapter.h"
26 #include "avcodec_common.h"
27 #ifdef SUPPORT_DRM
28 #include "drm_i_keysession_service.h"
29 #endif
30 #include "interrupt_listener.h"
31 
32 namespace OHOS {
33 namespace Media {
34 namespace Pipeline {
35 using namespace OHOS::Media::Plugins;
36 class AudioDecoderFilter : public Filter, public InterruptListener,
37     public std::enable_shared_from_this<AudioDecoderFilter> {
38 public:
39     explicit AudioDecoderFilter(std::string name, FilterType type);
40     explicit AudioDecoderFilter(std::string name, FilterType type, bool isAsyncMode);
41     ~AudioDecoderFilter() override;
42 
43     void Init(const std::shared_ptr<EventReceiver> &receiver, const std::shared_ptr<FilterCallback> &callback) override;
44 
45     Status DoPrepare() override;
46 
47     Status DoStart() override;
48 
49     Status DoPause() override;
50 
51     Status DoFreeze() override;
52 
53     Status DoUnFreeze() override;
54 
55     Status DoPauseAudioAlign() override;
56 
57     Status DoResume() override;
58 
59     Status DoResumeAudioAlign() override;
60 
61     Status DoStop() override;
62 
63     Status DoFlush() override;
64 
65     Status DoRelease() override;
66 
67     void SetParameter(const std::shared_ptr<Meta> &parameter) override;
68 
69     void SetDumpFlag(bool isDump);
70 
71     void GetParameter(std::shared_ptr<Meta> &parameter) override;
72 
73     Status LinkNext(const std::shared_ptr<Filter> &nextFilter, StreamType outType) override;
74 
75     Status UpdateNext(const std::shared_ptr<Filter> &nextFilter, StreamType outType) override;
76 
77     Status UnLinkNext(const std::shared_ptr<Filter> &nextFilter, StreamType outType) override;
78 
79     void OnInterrupted(bool isInterruptNeeded) override;
80 
81     Status DoProcessInputBuffer(int recvArg, bool dropFrame) override;
82 
83     void UpdateIsAsyncMode(bool isAsyncMode);
84 
85     Status HandleInputBuffer(bool isTriggeredByOutPort);
86 
87     Status ChangePlugin(std::shared_ptr<Meta> meta);
88 
89     FilterType GetFilterType();
90 
91     void OnLinkedResult(const sptr<AVBufferQueueProducer> &outputBufferQueue, std::shared_ptr<Meta> &meta);
92 
93     void OnUpdatedResult(std::shared_ptr<Meta> &meta);
94 
95     void OnUnlinkedResult(std::shared_ptr<Meta> &meta);
96 
97     Status SetDecryptionConfig(const sptr<DrmStandard::IMediaKeySessionService> &keySessionProxy,
98         bool svp);
99 
100     void OnDumpInfo(int32_t fd);
101 
102     void SetCallerInfo(uint64_t instanceId, const std::string& appName);
103 
104     void OnError(CodecErrorType errorType, int32_t errorCode);
105 
106     void OnOutputFormatChanged(const Format& format);
107 protected:
108     Status OnLinked(StreamType inType, const std::shared_ptr<Meta> &meta,
109         const std::shared_ptr<FilterLinkCallback> &callback) override;
110 
111     Status OnUpdated(StreamType inType, const std::shared_ptr<Meta> &meta,
112         const std::shared_ptr<FilterLinkCallback> &callback) override;
113 
114     Status OnUnLinked(StreamType inType, const std::shared_ptr<FilterLinkCallback>& callback) override;
115 
116 private:
117     Status SetInputBufferQueueConsumerListener();
118     Status SetOutputBufferQueueProducerListener();
119     bool IsNeedProcessInput(bool isOutPort);
120     void UpdateTrackInfoSampleFormat(const std::string& mime, const std::shared_ptr<Meta> &meta);
121 
122     std::string name_;
123     FilterType filterType_;
124     std::shared_ptr<Meta> meta_;
125     std::shared_ptr<Filter> nextFilter_;
126 
127     std::shared_ptr<EventReceiver> eventReceiver_;
128     std::shared_ptr<FilterCallback> filterCallback_;
129 
130     std::shared_ptr<FilterLinkCallback> onLinkedResultCallback_;
131 
132     std::shared_ptr<AudioDecoderAdapter> decoder_;
133 
134     bool isDrmProtected_ = false;
135 #ifdef SUPPORT_DRM
136     sptr<DrmStandard::IMediaKeySessionService> keySessionServiceProxy_ = nullptr;
137 #endif
138     bool svpFlag_ = false;
139     bool isDump_ = false;
140     bool refreshTotalPauseTime_{false};
141     int64_t latestBufferTime_{HST_TIME_NONE};
142     int64_t latestPausedTime_{HST_TIME_NONE};
143     int64_t totalPausedTime_{0};
144     uint64_t instanceId_ = 0;
145     std::string appName_;
146     std::mutex releaseMutex_;
147     std::atomic<bool> isReleased_ {false};
148 
149     std::mutex bufferStatusMutex_;
150     uint32_t bufferStatus_{static_cast<uint32_t>(InOutPortBufferStatus::INIT)};
151 };
152 
153 class AudioDecoderCallback : public MediaAVCodec::MediaCodecCallback {
154 public:
155     explicit AudioDecoderCallback(std::shared_ptr<AudioDecoderFilter> audioDecoderFilter);
156     virtual ~AudioDecoderCallback();
157 
158     void OnError(MediaAVCodec::AVCodecErrorType errorType, int32_t errorCode) override;
159     void OnOutputFormatChanged(const Format &format) override;
160     void OnInputBufferAvailable(uint32_t index, std::shared_ptr<AVBuffer> buffer) override;
161     void OnOutputBufferAvailable(uint32_t index, std::shared_ptr<AVBuffer> buffer) override;
162 
163 private:
164     std::weak_ptr<AudioDecoderFilter> audioDecoderFilter_;
165 };
166 } // namespace Pipeline
167 } // namespace MEDIA
168 } // namespace OHOS
169 #endif // FILTERS_CODEC_FILTER_H
170 
171