• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022-2022 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 <string>
21 #include <tuple>
22 #include <vector>
23 #include "sink/i_media_sync_center.h"
24 #include "osal/task/mutex.h"
25 #include "osal/task/autolock.h"
26 #include "osal/utils/steady_clock.h"
27 #include "common/status.h"
28 #include "plugin/plugin_time.h"
29 
30 namespace OHOS {
31 namespace Media {
32 namespace Pipeline {
33 using namespace OHOS::Media::Plugins;
34 class MediaSyncManager : public IMediaSyncCenter, public std::enable_shared_from_this<MediaSyncManager> {
35 public:
36     MediaSyncManager() = default;
37     virtual ~MediaSyncManager();
38     // interfaces called by hiplayer hirecoder etc.
39     Status SetPlaybackRate(float rate);
40     float GetPlaybackRate();
41     void WaitAllPrerolled(bool prerolled = true);
42     Status Resume();
43     Status Pause();
44     Status Seek(int64_t mediaTime);
45     Status Reset() override;
46     bool InSeeking();
47     std::condition_variable seekCond_;
48 
49     // interfaces from IMediaSyncCenter
50     void AddSynchronizer(IMediaSynchronizer* syncer) override;
51 
52     void RemoveSynchronizer(IMediaSynchronizer* syncer) override;
53     /**
54      * anchor a media time(pts) with real clock time.
55      *
56      * @param clockTime based on HST_TIME_BASE
57      * @param mediaTime media time based on HST_TIME_BASE
58      * @param maxMediaTime duration of the resource
59      * @param supplier which report this time anchor
60      * @retval current frame Whether rendering is required
61      */
62     bool UpdateTimeAnchor(int64_t clockTime, int64_t delayTime, int64_t mediaTime, int64_t mediaAbsTime,
63         int64_t maxMediaTime, IMediaSynchronizer* supplier) override;
64 
65     /**
66      * get media time currently
67      * @return media time now
68      */
69     int64_t GetMediaTimeNow() override;
70 
71     /***
72      * get clock time now
73      * @return return clock time based on HST_TIME_BASE
74      */
75     int64_t GetClockTimeNow() override;
76 
77     /**
78      * Get clock time anchored with pts
79      *
80      * @param mediaTime target pts
81      * @return clock time anchored with pts
82      */
83     int64_t GetClockTime(int64_t mediaTime) override;
84 
85     /**
86      * after IMediaSynchronizer has received the first frame, it should call this function to report the receiving of
87      * the first frame.
88      *
89      * @param supplier which report first frame
90      */
91     void ReportPrerolled(IMediaSynchronizer* supplier) override;
92 
93     void SetMediaTimeRangeEnd(int64_t endMediaTime, int32_t trackId) override;
94 
95     void SetMediaTimeRangeStart(int64_t startMediaTime, int32_t trackId) override;
96 
97     void SetStartingTimeMediaUs(int64_t startingTimeMediaUs);
98 
99     int64_t GetSeekTime() override;
100 private:
101     enum class State {
102         RESUMED,
103         PAUSED,
104     };
105     static int64_t GetSystemClock();
106     static int64_t SimpleGetMediaTime(int64_t anchorClockTime, int64_t delayTime, int64_t nowClockTime,
107                                       int64_t anchorMediaTime, float playRate);
108     static int64_t SimpleGetClockTime(int64_t anchorClockTime, int64_t nowMediaTime, int64_t anchorMediaTime,
109                                       float playRate);
110 
111     bool IsSupplierValid(IMediaSynchronizer* supplier);
112 
113     void SimpleUpdateTimeAnchor(int64_t clockTime, int64_t mediaTime, int64_t mediaAbsTime);
114     void SimpleUpdatePlayRate(float playRate);
115     void SetMediaTimeStartEnd(int32_t trackId, int32_t index, int64_t val);
116     void SetAllSyncShouldWaitNoLock();
117     void ResetTimeAnchorNoLock();
118 
119     int64_t ClipMediaTime(int64_t inTime);
120     OHOS::Media::Mutex clockMutex_ {};
121     State clockState_ {State::PAUSED};
122     int8_t currentSyncerPriority_ {IMediaSynchronizer::NONE};
123     int64_t currentAnchorClockTime_ {HST_TIME_NONE};
124     int64_t currentAnchorMediaTime_ {HST_TIME_NONE};
125     int64_t currentAbsMediaTime_ {HST_TIME_NONE};
126     int64_t pausedMediaTime_ {HST_TIME_NONE};
127     int64_t pausedAbsMediaTime_ {HST_TIME_NONE};
128     int64_t pausedClockTime_ {HST_TIME_NONE};
129     int64_t startingTimeMediaUs_ {HST_TIME_NONE};
130 
131     float playRate_ {1.0f};
132     bool alreadySetSyncersShouldWait_ {false};
133     bool allSyncerShouldPrerolled_ {true};
134     bool isSeeking_ {false};
135     int64_t seekingMediaTime_ {HST_TIME_NONE};
136 
137     // trackid start end
138     std::vector<std::tuple<int32_t, int64_t, int64_t>> trackMediaTimeRange_ {};
139     int64_t minRangeStartOfMediaTime_ {HST_TIME_NONE};
140     int64_t maxRangeEndOfMediaTime_ {HST_TIME_NONE};
141 
142     OHOS::Media::Mutex syncersMutex_ {};
143     std::vector<IMediaSynchronizer*> syncers_;
144     std::vector<IMediaSynchronizer*> prerolledSyncers_;
145     int64_t delayTime_ {HST_TIME_NONE};
146 };
147 } // namespace Pipeline
148 } // namespace Media
149 } // namespace OHOS
150 #endif // HISTREAMER_PIPELINE_CORE_PIPELINE_CLOCK_H
151