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_HIPLAYER_CALLBACKER_LOOPER_H 17 #define HISTREAMER_HIPLAYER_CALLBACKER_LOOPER_H 18 19 #include <list> 20 #include <utility> 21 #include "osal/task/task.h" 22 #include "i_player_engine.h" 23 #include "meta/any.h" 24 #include "osal/utils/steady_clock.h" 25 26 namespace OHOS { 27 namespace Media { 28 class HiPlayerCallbackLooper : public IPlayerEngineObs { 29 public: 30 explicit HiPlayerCallbackLooper(); 31 ~HiPlayerCallbackLooper() override; 32 33 bool IsStarted(); 34 35 void Stop(); 36 37 void StartWithPlayerEngineObs(const std::weak_ptr<IPlayerEngineObs>& obs); 38 39 void SetPlayEngine(IPlayerEngine* engine); 40 41 void StartReportMediaProgress(int64_t updateIntervalMs = 1000); 42 43 void StopReportMediaProgress(); 44 45 void DropMediaProgress(); 46 47 void ManualReportMediaProgressOnce(); 48 49 void OnError(PlayerErrorType errorType, int32_t errorCode) override; 50 51 void OnInfo(PlayerOnInfoType type, int32_t extra, const Format &infoBody) override; 52 53 void DoReportCompletedTime(); 54 55 private: 56 57 void LoopOnce(); 58 59 void DoReportMediaProgress(); 60 void DoReportInfo(const Any& info); 61 void DoReportError(const Any& error); 62 63 struct Event { EventEvent64 Event(int32_t inWhat, int64_t inWhenMs, Any inAny): what(inWhat), whenMs(inWhenMs), 65 detail(std::move(inAny)) {} 66 int32_t what {0}; 67 int64_t whenMs {INT64_MAX}; 68 Any detail; 69 }; 70 class EventQueue { 71 public: 72 void Enqueue(const std::shared_ptr<Event>& event); 73 std::shared_ptr<Event> Next(); 74 void RemoveMediaProgressEvent(); 75 void Quit(); 76 private: 77 OHOS::Media::Mutex queueMutex_ {}; 78 OHOS::Media::ConditionVariable queueHeadUpdatedCond_ {}; 79 std::list<std::shared_ptr<Event>> queue_ {}; 80 bool quit_ {false}; 81 }; 82 83 OHOS::Media::Task task_; 84 OHOS::Media::Mutex loopMutex_ {}; 85 bool taskStarted_ {false}; 86 IPlayerEngine* playerEngine_ {}; 87 std::weak_ptr<IPlayerEngineObs> obs_ {}; 88 EventQueue eventQueue_ {}; 89 bool reportMediaProgress_ {false}; 90 bool isDropMediaProgress_ {false}; 91 int64_t reportProgressIntervalMs_ {100}; // default interval is 100 ms 92 }; 93 } // namespace Media 94 } // namespace OHOS 95 #endif // HISTREAMER_HIPLAYER_CALLBACKER_LOOPER_H 96