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 #include "osal/task/condition_variable.h" 26 #include "osal/task/mutex.h" 27 28 namespace OHOS { 29 namespace Media { 30 class HiPlayerCallbackLooper : public IPlayerEngineObs { 31 public: 32 explicit HiPlayerCallbackLooper(); 33 ~HiPlayerCallbackLooper() override; 34 35 bool IsStarted(); 36 37 void Stop(); 38 39 void StartWithPlayerEngineObs(const std::weak_ptr<IPlayerEngineObs>& obs); 40 41 void SetPlayEngine(IPlayerEngine* engine, std::string playerId); 42 43 void StartReportMediaProgress(int64_t updateIntervalMs = 1000); 44 45 void StopReportMediaProgress(); 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 OnSystemOperation(PlayerOnSystemOperationType type, PlayerOperationReason reason) override; 54 55 void DoReportCompletedTime(); 56 void StartCollectMaxAmplitude(int64_t updateIntervalMs); 57 void StopCollectMaxAmplitude(); 58 void ReportRemainedMaxAmplitude(); 59 60 private: 61 void DoReportMediaProgress(); 62 void DoReportInfo(const Any& info); 63 void DoReportError(const Any& error); 64 void DoCollectAmplitude(); 65 void DoReportSystemOperation(const Any& info); 66 67 struct Event { EventEvent68 Event(int32_t inWhat, int64_t inWhenMs, Any inAny): what(inWhat), whenMs(inWhenMs), 69 detail(std::move(inAny)) {} 70 int32_t what {0}; 71 int64_t whenMs {INT64_MAX}; 72 Any detail; 73 }; 74 class EventQueue { 75 public: 76 void Enqueue(const std::shared_ptr<Event>& event); 77 std::shared_ptr<Event> Next(); 78 void RemoveMediaProgressEvent(); 79 void Quit(); 80 private: 81 OHOS::Media::Mutex queueMutex_ {}; 82 OHOS::Media::ConditionVariable queueHeadUpdatedCond_ {}; 83 std::list<std::shared_ptr<Event>> queue_ {}; 84 bool quit_ {false}; 85 }; 86 87 void LoopOnce(const std::shared_ptr<HiPlayerCallbackLooper::Event>& item); 88 void Enqueue(const std::shared_ptr<Event>& event); 89 90 std::unique_ptr<OHOS::Media::Task> task_; 91 OHOS::Media::Mutex loopMutex_ {}; 92 bool taskStarted_ {false}; 93 IPlayerEngine* playerEngine_ {}; 94 std::weak_ptr<IPlayerEngineObs> obs_ {}; 95 bool reportMediaProgress_ {false}; 96 bool collectMaxAmplitude_ {false}; 97 bool isDropMediaProgress_ {false}; 98 int64_t reportProgressIntervalMs_ {100}; // default interval is 100 ms 99 int64_t collectMaxAmplitudeIntervalMs_ {100}; // default interval is 100 ms 100 std::vector<float> vMaxAmplitudeArray_ {}; 101 }; 102 } // namespace Media 103 } // namespace OHOS 104 #endif // HISTREAMER_HIPLAYER_CALLBACKER_LOOPER_H 105