• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2023-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 MEDIA_PIPELINE_VIDEO_SINK_FILTER_H
17 #define MEDIA_PIPELINE_VIDEO_SINK_FILTER_H
18 
19 #include <atomic>
20 #include "plugin/plugin_time.h"
21 #include "avcodec_common.h"
22 #include "surface/surface.h"
23 #include "osal/task/condition_variable.h"
24 #include "osal/task/mutex.h"
25 #include "osal/task/task.h"
26 #include "video_sink.h"
27 #include "sink/media_synchronous_sink.h"
28 #include "common/status.h"
29 #include "meta/meta.h"
30 #include "meta/format.h"
31 #include "filter/filter.h"
32 #include "media_sync_manager.h"
33 #include "foundation/multimedia/drm_framework/services/drm_service/ipc/i_keysession_service.h"
34 #include "common/media_core.h"
35 #include "common/seek_callback.h"
36 
37 namespace OHOS {
38 namespace Media {
39 class VideoDecoderAdapter;
40 namespace Pipeline {
41 class DecoderSurfaceFilter : public Filter, public std::enable_shared_from_this<DecoderSurfaceFilter> {
42 public:
43     explicit DecoderSurfaceFilter(const std::string& name, FilterType type);
44     ~DecoderSurfaceFilter() override;
45 
46     void Init(const std::shared_ptr<EventReceiver> &receiver,
47         const std::shared_ptr<FilterCallback> &callback) override;
48     Status Configure(const std::shared_ptr<Meta> &parameter);
49     Status DoInitAfterLink() override;
50     Status DoPrepare() override;
51     Status DoStart() override;
52     Status DoPause() override;
53     Status DoPauseDragging() override;
54     Status DoResume() override;
55     Status DoResumeDragging() override;
56     Status DoStop() override;
57     Status DoFlush() override;
58     Status DoRelease() override;
59     Status DoPreroll() override;
60     Status DoWaitPrerollDone(bool render) override;
61     Status DoSetPlayRange(int64_t start, int64_t end) override;
62     Status DoProcessInputBuffer(int recvArg, bool dropFrame) override;
63     Status DoProcessOutputBuffer(int recvArg, bool dropFrame, bool byIdx, uint32_t idx, int64_t renderTime) override;
64     Status DoSetPerfRecEnabled(bool isPerfRecEnabled) override;
65 
66     void SetParameter(const std::shared_ptr<Meta>& parameter) override;
67     void GetParameter(std::shared_ptr<Meta>& parameter) override;
68     void SetInterruptState(bool isInterruptNeeded);
69 
70     Status LinkNext(const std::shared_ptr<Filter> &nextFilter, StreamType outType) override;
71     Status UpdateNext(const std::shared_ptr<Filter> &nextFilter, StreamType outType) override;
72     Status UnLinkNext(const std::shared_ptr<Filter> &nextFilter, StreamType outType) override;
73     void OnLinkedResult(const sptr<AVBufferQueueProducer> &outputBufferQueue, std::shared_ptr<Meta> &meta);
74     void OnUpdatedResult(std::shared_ptr<Meta> &meta);
75 
76     void OnUnlinkedResult(std::shared_ptr<Meta> &meta);
77     FilterType GetFilterType();
78     void DrainOutputBuffer(uint32_t index, std::shared_ptr<AVBuffer> &outputBuffer);
79     Status SetVideoSurface(sptr<Surface> videoSurface);
80 
81     Status SetDecryptConfig(const sptr<DrmStandard::IMediaKeySessionService> &keySessionProxy,
82         bool svp);
83 
84     void OnError(MediaAVCodec::AVCodecErrorType errorType, int32_t errorCode);
85 
86     sptr<AVBufferQueueProducer> GetInputBufferQueue();
87     void SetSyncCenter(std::shared_ptr<MediaSyncManager> syncCenter);
88 
89     void SetSeekTime(int64_t seekTimeUs);
90     void ResetSeekInfo();
91     Status HandleInputBuffer();
92     void OnDumpInfo(int32_t fd);
93 
94     void SetBitrateStart();
95     void OnOutputFormatChanged(const MediaAVCodec::Format &format);
96 
97     void SetCallingInfo(int32_t appUid, int32_t appPid, const std::string& bundleName, uint64_t instanceId);
98 
99     Status GetLagInfo(int32_t& lagTimes, int32_t& maxLagDuration, int32_t& avgLagDuration);
100 
101     Status StartSeekContinous();
102     Status StopSeekContinous();
103     void RegisterVideoFrameReadyCallback(std::shared_ptr<VideoFrameReadyCallback> &callback);
104     void DeregisterVideoFrameReadyCallback();
105     int32_t GetDecRateUpperLimit();
106     bool GetIsSupportSeekWithoutFlush();
107     void ConsumeVideoFrame(uint32_t index, bool isRender, int64_t renderTimeNs = 0L);
108 
109 protected:
110     Status OnLinked(StreamType inType, const std::shared_ptr<Meta> &meta,
111         const std::shared_ptr<FilterLinkCallback> &callback) override;
112     Status OnUpdated(StreamType inType, const std::shared_ptr<Meta> &meta,
113         const std::shared_ptr<FilterLinkCallback> &callback) override;
114     Status OnUnLinked(StreamType inType, const std::shared_ptr<FilterLinkCallback>& callback) override;
115 
116 private:
117     void RenderLoop();
118     std::string GetCodecName(std::string mimeType);
119     int64_t CalculateNextRender(uint32_t index, std::shared_ptr<AVBuffer> &outputBuffer);
120     void ParseDecodeRateLimit();
121     void RenderNextOutput(uint32_t index, std::shared_ptr<AVBuffer> &outputBuffer);
122     Status ReleaseOutputBuffer(int index, bool render, const std::shared_ptr<AVBuffer> &outBuffer, int64_t renderTime);
123     bool AcquireNextRenderBuffer(bool byIdx, uint32_t &index, std::shared_ptr<AVBuffer> &outBuffer);
124     bool DrainSeekContinuous(uint32_t index, std::shared_ptr<AVBuffer> &outputBuffer);
125     bool DrainPreroll(uint32_t index, std::shared_ptr<AVBuffer> &outputBuffer);
126     bool DrainSeekClosest(uint32_t index, std::shared_ptr<AVBuffer> &outputBuffer);
127 
128     std::string name_;
129     FilterType filterType_;
130     std::shared_ptr<EventReceiver> eventReceiver_;
131     std::shared_ptr<FilterCallback> filterCallback_;
132     std::shared_ptr<FilterLinkCallback> onLinkedResultCallback_;
133     std::shared_ptr<VideoDecoderAdapter> videoDecoder_;
134     std::shared_ptr<VideoSink> videoSink_;
135     std::string codecMimeType_;
136     std::shared_ptr<Meta> configureParameter_;
137     std::shared_ptr<Meta> meta_;
138 
139     std::shared_ptr<Filter> nextFilter_;
140     Format configFormat_;
141 
142     std::atomic<uint64_t> renderFrameCnt_{0};
143     std::atomic<uint64_t> discardFrameCnt_{0};
144 
145     bool refreshTotalPauseTime_{false};
146     int64_t latestBufferTime_{HST_TIME_NONE};
147     int64_t latestPausedTime_{HST_TIME_NONE};
148     int64_t totalPausedTime_{0};
149     int64_t stopTime_{0};
150     sptr<Surface> videoSurface_;
151     bool isDrmProtected_ = false;
152     sptr<DrmStandard::IMediaKeySessionService> keySessionServiceProxy_;
153     bool svpFlag_ = false;
154     std::atomic<bool> isPaused_{false};
155     std::atomic<bool> isSeek_{false};
156     int64_t seekTimeUs_{0};
157     std::list<std::pair<int, std::shared_ptr<AVBuffer>>> outputBuffers_;
158     std::mutex mutex_;
159     std::unique_ptr<std::thread> readThread_ = nullptr;
160     std::atomic<bool> isThreadExit_ = true;
161     std::condition_variable condBufferAvailable_;
162 
163     std::atomic<int32_t> bitrateChange_{0};
164     int32_t surfaceWidth_{0};
165     int32_t surfaceHeight_{0};
166     std::atomic<bool> isRenderStarted_{false};
167     std::atomic<bool> isInterruptNeeded_{false};
168     Mutex formatChangeMutex_{};
169     int32_t rateUpperLimit_{0};
170 
171     std::mutex prerollMutex_ {};
172     std::atomic<bool> inPreroll_ {false};
173     std::condition_variable prerollDoneCond_ {};
174     std::atomic<bool> prerollDone_ {true};
175     std::atomic<bool> eosNext_ {false};
176     bool isFirstFrameAfterResume_ {true};
177 
178     int32_t appUid_ = -1;
179     int32_t appPid_ = -1;
180     std::string bundleName_;
181     uint64_t instanceId_ = 0;
182     int64_t playRangeStartTime_ = -1;
183     int64_t playRangeEndTime_ = -1;
184 
185     std::shared_ptr<VideoFrameReadyCallback> videoFrameReadyCallback_;
186     bool isInSeekContinous_{false};
187     std::unordered_map<uint32_t, std::shared_ptr<AVBuffer>> outputBufferMap_;
188     std::mutex draggingMutex_ {};
189 };
190 } // namespace Pipeline
191 } // namespace Media
192 } // namespace OHOS
193 #endif // MEDIA_PIPELINE_VIDEO_SINK_FILTER_H
194