1 /* 2 * Copyright (c) 2021-2021 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_CLOCK_MANAGER_H 17 #define HISTREAMER_PIPELINE_CORE_CLOCK_MANAGER_H 18 19 #include <memory> 20 #include <queue> 21 22 #include "clock_provider.h" 23 24 namespace OHOS { 25 namespace Media { 26 namespace Pipeline { 27 class ClockManager { 28 public: 29 ClockManager(const ClockManager&) = delete; 30 ClockManager operator=(const ClockManager&) = delete; 31 ~ClockManager() = default; Instance()32 static ClockManager& Instance() 33 { 34 static ClockManager manager; 35 return manager; 36 } 37 38 // todo how to decide the priority of providers 39 void RegisterProvider(const std::shared_ptr<ClockProvider>& provider); 40 void UnRegisterProvider(const std::shared_ptr<ClockProvider>& provider); 41 GetProvider()42 ClockProvider& GetProvider() 43 { 44 return providerProxy_; 45 } 46 private: 47 ClockManager() = default; 48 class SyncClockProviderProxy : public ClockProvider { 49 public: 50 ~SyncClockProviderProxy() override = default; 51 SetStub(std::shared_ptr<ClockProvider> & stub)52 void SetStub(std::shared_ptr<ClockProvider>& stub) 53 { 54 stub_ = stub; 55 } 56 CheckPts(int64_t pts,int64_t & delta)57 ErrorCode CheckPts(int64_t pts, int64_t &delta) override 58 { 59 if (stub_ == nullptr) { 60 return ErrorCode::ERROR_INVALID_OPERATION; 61 } 62 return stub_->CheckPts(pts, delta); 63 } 64 GetCurrentPosition(int64_t & position)65 ErrorCode GetCurrentPosition(int64_t &position) override 66 { 67 if (stub_ == nullptr) { 68 return ErrorCode::ERROR_INVALID_OPERATION; 69 } 70 return stub_->GetCurrentPosition(position); 71 } 72 GetCurrentTimeNano(int64_t & nowNano)73 ErrorCode GetCurrentTimeNano(int64_t& nowNano) override 74 { 75 if (stub_ == nullptr) { 76 return ErrorCode::ERROR_INVALID_OPERATION; 77 } 78 return stub_->GetCurrentTimeNano(nowNano); 79 } 80 81 private: 82 std::shared_ptr<ClockProvider> stub_; 83 }; 84 85 SyncClockProviderProxy providerProxy_; 86 87 std::priority_queue<std::shared_ptr<ClockProvider>> providers_; 88 }; 89 } // Pipeline 90 } // Media 91 } // OHOS 92 #endif // HISTREAMER_PIPELINE_CORE_CLOCK_MANAGER_H 93