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