• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022-2024 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 HISTREAMER_PIPELINE_CORE_PIPELINE_CLOCK_H
17 #define HISTREAMER_PIPELINE_CORE_PIPELINE_CLOCK_H
18 #include <condition_variable>
19 #include <memory>
20 #include <vector>
21 #include "sink/i_media_sync_center.h"
22 #include "osal/task/mutex.h"
23 #include "osal/task/autolock.h"
24 #include "osal/utils/steady_clock.h"
25 #include "filter/filter.h"
26 #include "common/status.h"
27 #include "plugin/plugin_time.h"
28 
29 namespace OHOS {
30 namespace Media {
31 namespace Pipeline {
32 using namespace OHOS::Media::Plugins;
33 class MediaSyncManager : public IMediaSyncCenter, public std::enable_shared_from_this<MediaSyncManager> {
34 public:
35     MediaSyncManager() = default;
36     virtual ~MediaSyncManager();
37 
38     void AddSynchronizer(IMediaSynchronizer* syncer) override;
39     void RemoveSynchronizer(IMediaSynchronizer* syncer) override;
40 
41     // after supplier has received the first frame, it should call this function to report the receiving of
42     // the first frame.
43     void ReportPrerolled(IMediaSynchronizer* supplier) override;
44 
45     // after supplier has received the eos frame, it should call this function to report the receiving of
46     // the eos frame.
47     void ReportEos(IMediaSynchronizer* supplier) override;
48 
49     // anchor a media time(pts) with real clock time.
50     bool UpdateTimeAnchor(int64_t clockTime, int64_t delayTime, IMediaTime iMediaTime,
51         IMediaSynchronizer* supplier) override;
52 
53     int64_t GetMediaTimeNow() override;
54     int64_t GetClockTimeNow() override;
55     int64_t GetAnchoredClockTime(int64_t mediaTime) override;
56     int64_t GetSeekTime() override;
57     int64_t GetMediaStartPts() override;
58     float GetPlaybackRate() override;
59 
60     void SetMediaTimeRangeStart(int64_t startMediaTime, int32_t trackId, IMediaSynchronizer* supplier) override;
61     void SetMediaTimeRangeEnd(int64_t endMediaTime, int32_t trackId, IMediaSynchronizer* supplier) override;
62     void SetMediaStartPts(int64_t startPts) override;
63     void SetLastAudioBufferDuration(int64_t durationUs) override;
64     void SetLastVideoBufferPts(int64_t bufferPts) override;
65     Status SetPlaybackRate(float rate) override;
66 
67     void ResetMediaStartPts() override;
68     Status Reset() override;
69     Status Pause();
70     Status Resume();
71     Status Seek(int64_t mediaTime, bool isClosest = false);
72     Status Stop();
73 
74     // return isSeeking_
75     bool InSeeking();
76     // notified when leaving seeking,that is, isSeeking_ is set to false
77     std::condition_variable seekCond_;
78 
79     void SetLastVideoBufferAbsPts(int64_t lastVideoBufferAbsPts) override;
80     int64_t GetLastVideoBufferAbsPts() const override;
81 private:
82     enum class State {
83         RESUMED,
84         PAUSED,
85     };
86 
87     std::vector<std::function<bool(MediaSyncManager*, int64_t&)>> setMediaTimeFuncs {
88         &MediaSyncManager::CheckSeekingMediaTime,
89         &MediaSyncManager::CheckPausedMediaTime,
90         &MediaSyncManager::CheckIfMediaTimeIsNone,
91         &MediaSyncManager::CheckFirstMediaTimeAfterSeek
92     };
93 
94     static int64_t GetSystemClock();
95     int64_t GetMediaTime(int64_t clockTime);
96 
97     void SimpleUpdateTimeAnchor(int64_t clockTime, int64_t mediaTime);
98     void ResetTimeAnchorNoLock();
99 
100     void UpdateFirstPtsAfterSeek(int64_t mediaTime);
101     void SetAllSyncShouldWaitNoLock();
102 
103     bool IsSupplierValid(IMediaSynchronizer* supplier);
104     bool IsPlayRateValid(float playRate);
105 
106     // GetMediaTimeNow executes the following functions in sequence,
107     // Check and set media time,
108     // return true to indicate that subsequent functions should continue to execute,
109     // false to indicate that the current mediaTime should be returned directly
110     bool CheckSeekingMediaTime(int64_t& mediaTime);
111     bool CheckPausedMediaTime(int64_t& mediaTime);
112     bool CheckIfMediaTimeIsNone(int64_t& mediaTime);
113     bool CheckFirstMediaTimeAfterSeek(int64_t& mediaTime);
114 
115     // Check the media time range,
116     // ensure that media time does not regress in non-seek state,
117     // and does not exceed the maximum media progress
118     int64_t BoundMediaProgress(int64_t newMediaProgressTime);
119     // get the maximum media progress
120     int64_t GetMaxMediaProgress();
121 
122     OHOS::Media::Mutex syncersMutex_ {};
123     std::vector<IMediaSynchronizer*> syncers_;
124     std::vector<IMediaSynchronizer*> prerolledSyncers_;
125 
126     std::atomic<int64_t> lastAudioBufferDuration_ {0};
127     std::atomic<int64_t> lastVideoBufferPts_ {0};
128     std::atomic<int64_t> lastReportMediaTime_ {HST_TIME_NONE};
129     std::atomic<bool> isFrameAfterSeeked_ {false};
130 
131     OHOS::Media::Mutex clockMutex_ {};
132     float playRate_ {1.0f};
133     bool isSeeking_ {false};
134     State clockState_ {State::PAUSED};
135     int64_t delayTime_ {HST_TIME_NONE};
136     int64_t startPts_ {HST_TIME_NONE};
137     int64_t firstMediaTimeAfterSeek_ {HST_TIME_NONE};
138     bool alreadySetSyncersShouldWait_ {false};
139     int64_t seekingMediaTime_ {HST_TIME_NONE};
140     int64_t pausedMediaTime_ {HST_TIME_NONE};
141     int64_t pausedClockTime_ {HST_TIME_NONE};
142     int64_t minRangeStartOfMediaTime_ {HST_TIME_NONE};
143     int64_t maxRangeEndOfMediaTime_ {HST_TIME_NONE};
144     int64_t currentAnchorClockTime_ {HST_TIME_NONE};
145     int64_t currentAnchorMediaTime_ {HST_TIME_NONE};
146     int8_t currentSyncerPriority_ {IMediaSynchronizer::NONE};
147     int8_t currentRangeStartPriority_ {IMediaSynchronizer::NONE};
148     int8_t currentRangeEndPriority_ {IMediaSynchronizer::NONE};
149 
150     int64_t lastVideoBufferAbsPts_ {HST_TIME_NONE};
151 };
152 } // namespace Pipeline
153 } // namespace Media
154 } // namespace OHOS
155 #endif // HISTREAMER_PIPELINE_CORE_PIPELINE_CLOCK_H
156