1 /*
2  * Copyright (c) 2024 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 HITRANSCODER_CALLBACKER_LOOPER_H
17 #define HITRANSCODER_CALLBACKER_LOOPER_H
18 
19 #include <list>
20 #include <utility>
21 #include "osal/task/task.h"
22 #include "i_transcoder_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 HiTransCoderCallbackLooper : public ITransCoderEngineObs {
31 public:
32     explicit HiTransCoderCallbackLooper();
33     ~HiTransCoderCallbackLooper() override;
34     bool IsStarted();
35     void Stop();
36     void StartWithTransCoderEngineObs(const std::weak_ptr<ITransCoderEngineObs>& obs);
37     void SetTransCoderEngine(ITransCoderEngine* engine, std::string transCoderId);
38     void StartReportMediaProgress(int64_t updateIntervalMs = 1000);
39     void StopReportMediaProgress();
40     void ManualReportMediaProgressOnce();
41     void OnError(TransCoderErrorType errorType, int32_t errorCode) override;
42     void OnInfo(TransCoderOnInfoType type, int32_t extra) override;
43     void DoReportCompletedTime();
44 
45 private:
46     void DoReportMediaProgress();
47     void DoReportInfo(const Any& info);
48     void DoReportError(const Any& error);
49 
50     struct Event {
EventEvent51         Event(int32_t inWhat, int64_t inWhenMs, Any inAny): what(inWhat), whenMs(inWhenMs),
52             detail(std::move(inAny)) {}
53         int32_t what {0};
54         int64_t whenMs {INT64_MAX};
55         Any detail;
56     };
57     class EventQueue {
58     public:
59         void Enqueue(const std::shared_ptr<Event>& event);
60         std::shared_ptr<Event> Next();
61         void RemoveMediaProgressEvent();
62         void Quit();
63     private:
64         OHOS::Media::Mutex queueMutex_ {};
65         OHOS::Media::ConditionVariable queueHeadUpdatedCond_ {};
66         std::list<std::shared_ptr<Event>> queue_ {};
67         bool quit_ {false};
68     };
69 
70     void LoopOnce(const std::shared_ptr<HiTransCoderCallbackLooper::Event>& item);
71     void Enqueue(const std::shared_ptr<Event>& event);
72 
73     std::unique_ptr<OHOS::Media::Task> task_;
74     OHOS::Media::Mutex loopMutex_ {};
75     bool taskStarted_ {false};
76     ITransCoderEngine* transCoderEngine_ {};
77     std::weak_ptr<ITransCoderEngineObs> obs_ {};
78     bool reportMediaProgress_ {false};
79     bool isDropMediaProgress_ {false};
80     int64_t reportProgressIntervalMs_ {100}; // default interval is 100 ms
81 };
82 }  // namespace Media
83 }  // namespace OHOS
84 #endif // HITRANSCODER_CALLBACKER_LOOPER_H
85