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 "error_code.h" 23 #include "foundation/osal/thread/mutex.h" 24 #include "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 error code 54 */ 55 ErrorCode 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 89 private: 90 enum class State { 91 RESUMED, 92 PAUSED, 93 }; 94 static int64_t GetSystemClock(); 95 static int64_t SimpleGetMediaTime(int64_t anchorClockTime, int64_t nowClockTime, int64_t anchorMediaTime, 96 float playRate); 97 static int64_t SimpleGetClockTime(int64_t anchorClockTime, int64_t nowMediaTime, int64_t anchorMediaTime, 98 float playRate); 99 100 bool IsSupplierValid(IMediaSynchronizer* supplier); 101 102 void SimpleUpdateTimeAnchor(int64_t clockTime, int64_t mediaTime); 103 void SimpleUpdatePlayRate(float playRate); 104 void SetMediaTimeStartEnd(int32_t trackId, int32_t index, int64_t val); 105 void SetAllSyncShouldWaitNoLock(); 106 void ResetTimeAnchorNoLock(); 107 108 int64_t ClipMediaTime(int64_t inTime); 109 OSAL::Mutex clockMutex_ {}; 110 State clockState_ {State::PAUSED}; 111 int8_t currentSyncerPriority_ {IMediaSynchronizer::NONE}; 112 int64_t currentAnchorClockTime_ {HST_TIME_NONE}; 113 int64_t currentAnchorMediaTime_ {HST_TIME_NONE}; 114 int64_t pausedMediaTime_ {HST_TIME_NONE}; 115 int64_t pausedClockTime_ {HST_TIME_NONE}; 116 117 float playRate_ {1.0f}; 118 bool alreadySetSyncersShouldWait_ {false}; 119 bool allSyncerShouldPrerolled_ {true}; 120 bool isSeeking_ {false}; 121 int64_t seekingMediaTime_ {HST_TIME_NONE}; 122 123 // trackid start end 124 std::vector<std::tuple<int32_t, int64_t, int64_t>> trackMediaTimeRange_ {}; 125 int64_t minRangeStartOfMediaTime_ {HST_TIME_NONE}; 126 int64_t maxRangeEndOfMediaTime_ {HST_TIME_NONE}; 127 128 OSAL::Mutex syncersMutex_ {}; 129 std::vector<IMediaSynchronizer*> syncers_; 130 std::vector<IMediaSynchronizer*> prerolledSyncers_; 131 }; 132 } // namespace Pipeline 133 } // namespace Media 134 } // namespace OHOS 135 #endif // HISTREAMER_PIPELINE_CORE_PIPELINE_CLOCK_H 136