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 
62     void DoReportMediaProgress();
63     void DoReportInfo(const Any& info);
64     void DoReportError(const Any& error);
65     void DoCollectAmplitude();
66     void DoReportSystemOperation(const Any& info);
67 
68     struct Event {
EventOHOS::Media::HiPlayerCallbackLooper::Event69         Event(int32_t inWhat, int64_t inWhenMs, Any inAny): what(inWhat), whenMs(inWhenMs),
70             detail(std::move(inAny)) {}
71         int32_t what {0};
72         int64_t whenMs {INT64_MAX};
73         Any detail;
74     };
75 
76     void LoopOnce(const std::shared_ptr<HiPlayerCallbackLooper::Event>& item);
77     void Enqueue(const std::shared_ptr<Event>& event);
78 
79     std::unique_ptr<OHOS::Media::Task> task_;
80     OHOS::Media::Mutex loopMutex_ {};
81     bool taskStarted_ {false};
82     IPlayerEngine* playerEngine_ {};
83     std::weak_ptr<IPlayerEngineObs> obs_ {};
84     bool reportMediaProgress_ {false};
85     bool collectMaxAmplitude_ {false};
86     bool isDropMediaProgress_ {false};
87     int64_t reportProgressIntervalMs_ {100}; // default interval is 100 ms
88     int64_t collectMaxAmplitudeIntervalMs_ {100}; // default interval is 100 ms
89     std::vector<float> vMaxAmplitudeArray_ {};
90 };
91 }  // namespace Media
92 }  // namespace OHOS
93 #endif // HISTREAMER_HIPLAYER_CALLBACKER_LOOPER_H
94