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