• 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 SetInitialVideoFrameRate(double frameRate);
68     double GetInitialVideoFrameRate() override;
69 
70     void ResetMediaStartPts() override;
71     Status Reset() override;
72     Status Pause();
73     Status Resume();
74     Status Seek(int64_t mediaTime, bool isClosest = false);
75     Status Stop();
76 
77     // return isSeeking_
78     bool InSeeking();
79 
80     void UpdataPausedMediaTime(int32_t pausedMediaTime);
81 
82     // notified when leaving seeking,that is, isSeeking_ is set to false
83     std::condition_variable seekCond_;
84 
85     void SetLastVideoBufferAbsPts(int64_t lastVideoBufferAbsPts) override;
86     int64_t GetLastVideoBufferAbsPts() const override;
87 private:
88     enum class State {
89         RESUMED,
90         PAUSED,
91     };
92 
93     std::vector<std::function<bool(MediaSyncManager*, int64_t&)>> setMediaTimeFuncs {
94         &MediaSyncManager::CheckSeekingMediaTime,
95         &MediaSyncManager::CheckPausedMediaTime,
96         &MediaSyncManager::CheckIfMediaTimeIsNone,
97         &MediaSyncManager::CheckFirstMediaTimeAfterSeek
98     };
99 
100     static int64_t GetSystemClock();
101     int64_t GetMediaTime(int64_t clockTime);
102 
103     void SimpleUpdateTimeAnchor(int64_t clockTime, int64_t mediaTime);
104     void ResetTimeAnchorNoLock();
105 
106     void UpdateFirstPtsAfterSeek(int64_t mediaTime);
107     void SetAllSyncShouldWaitNoLock();
108 
109     bool IsSupplierValid(IMediaSynchronizer* supplier);
110     bool IsPlayRateValid(float playRate);
111 
112     // GetMediaTimeNow executes the following functions in sequence,
113     // Check and set media time,
114     // return true to indicate that subsequent functions should continue to execute,
115     // false to indicate that the current mediaTime should be returned directly
116     bool CheckSeekingMediaTime(int64_t& mediaTime);
117     bool CheckPausedMediaTime(int64_t& mediaTime);
118     bool CheckIfMediaTimeIsNone(int64_t& mediaTime);
119     bool CheckFirstMediaTimeAfterSeek(int64_t& mediaTime);
120 
121     // Check the media time range,
122     // ensure that media time does not regress in non-seek state,
123     // and does not exceed the maximum media progress
124     int64_t BoundMediaProgress(int64_t newMediaProgressTime);
125     // get the maximum media progress
126     int64_t GetMaxMediaProgress();
127 
128     OHOS::Media::Mutex syncersMutex_ {};
129     std::vector<IMediaSynchronizer*> syncers_;
130     std::vector<IMediaSynchronizer*> prerolledSyncers_;
131 
132     std::atomic<int64_t> lastAudioBufferDuration_ {0};
133     std::atomic<int64_t> lastVideoBufferPts_ {0};
134     std::atomic<int64_t> lastReportMediaTime_ {HST_TIME_NONE};
135     std::atomic<bool> isFrameAfterSeeked_ {false};
136 
137     double videoInitialFrameRate_ {-1.0};
138 
139     OHOS::Media::Mutex clockMutex_ {};
140     float playRate_ {1.0f};
141     bool isSeeking_ {false};
142     State clockState_ {State::PAUSED};
143     int64_t delayTime_ {HST_TIME_NONE};
144     int64_t startPts_ {HST_TIME_NONE};
145     int64_t firstMediaTimeAfterSeek_ {HST_TIME_NONE};
146     bool alreadySetSyncersShouldWait_ {false};
147     int64_t seekingMediaTime_ {HST_TIME_NONE};
148     int64_t pausedMediaTime_ {HST_TIME_NONE};
149     int64_t pausedClockTime_ {HST_TIME_NONE};
150     int64_t minRangeStartOfMediaTime_ {HST_TIME_NONE};
151     int64_t maxRangeEndOfMediaTime_ {HST_TIME_NONE};
152     int64_t currentAnchorClockTime_ {HST_TIME_NONE};
153     int64_t currentAnchorMediaTime_ {HST_TIME_NONE};
154     int8_t currentSyncerPriority_ {IMediaSynchronizer::NONE};
155     int8_t currentRangeStartPriority_ {IMediaSynchronizer::NONE};
156     int8_t currentRangeEndPriority_ {IMediaSynchronizer::NONE};
157 
158     int64_t lastVideoBufferAbsPts_ {HST_TIME_NONE};
159 };
160 } // namespace Pipeline
161 } // namespace Media
162 } // namespace OHOS
163 #endif // HISTREAMER_PIPELINE_CORE_PIPELINE_CLOCK_H
164