1 /*
2  * Copyright (C) 2021 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 PLAYER_SERVER_TASK_MGR_H
17 #define PLAYER_SERVER_TASK_MGR_H
18 
19 #include <list>
20 #include <mutex>
21 #include "task_queue.h"
22 
23 namespace OHOS {
24 namespace Media {
25 enum class PlayerServerTaskType : uint8_t {
26     /**
27      * The following types are treated as two-phase tasks. Such a task consists of two phases.
28      * The first phase is initiated by the taskhandler corresponding to the task, and the second
29      * phase needs to be manually marked. The next two-phase task is blocked until the second
30      * stage of the currently executing two-phase task is marked.
31      *
32      * If a two-phase task of the same type (except for STATE_CHANGE) already exists in the two-phase task pending list,
33      * it will be replaced, regardless of the fact that the new task is a late entry.
34      */
35     STATE_CHANGE,
36     SEEKING,
37     RATE_CHANGE,
38     CANCEL_TASK,
39     SEEK_CONTINOUS,
40     LIGHT_TASK,
41     SET_VIDEO_SURFACE,
42     BUTT,
43 };
44 
45 class PlayerServerTaskMgr {
46 public:
47     PlayerServerTaskMgr();
48     ~PlayerServerTaskMgr();
49 
50     int32_t Init();
51     int32_t LaunchTask(const std::shared_ptr<ITaskHandler> &task, PlayerServerTaskType type,
52         const std::string &taskName, const std::shared_ptr<ITaskHandler> &cancelTask = nullptr);
53     int32_t SeekTask(const std::shared_ptr<ITaskHandler> &task, const std::shared_ptr<ITaskHandler> &cancelTask,
54         const std::string &taskName, int32_t seekMode, int32_t seekTime);
55     int32_t SpeedTask(const std::shared_ptr<ITaskHandler> &task, const std::shared_ptr<ITaskHandler> &cancelTask,
56         const std::string &taskName, int32_t speedMode);
57     int32_t SeekContinousTask(const std::shared_ptr<ITaskHandler> &task, const std::string &taskName);
58     int32_t SetVideoSurfaeTask(const std::shared_ptr<ITaskHandler> &task, const std::string &taskName);
59     // only take effect when it is called at the task thread.
60     int32_t MarkTaskDone(const std::string &taskName);
61     void ClearAllTask();
62     int32_t Reset();
63 
64 private:
65     int32_t EnqueueTask(const std::shared_ptr<ITaskHandler> &task, PlayerServerTaskType type,
66         const std::string &taskName);
67     int32_t EnqueueSeekTask(const std::shared_ptr<ITaskHandler> &task, PlayerServerTaskType type,
68         const std::string &taskName, int32_t seekMode, int32_t seekTime);
69     struct TwoPhaseTaskItem {
70         PlayerServerTaskType type;
71         std::shared_ptr<ITaskHandler> task;
72         std::shared_ptr<ITaskHandler> cancelTask;
73         std::string taskName;
74         int32_t seekMode_ = -1;
75         int32_t seekTime_ = -1;
76         int32_t speedMode_ = -1;
77     };
78 
79     std::unique_ptr<TaskQueue> taskThread_;
80     std::shared_ptr<ITaskHandler> currTwoPhaseTask_;
81     PlayerServerTaskType currTwoPhaseType_ = PlayerServerTaskType::BUTT;
82     std::string currTwoPhaseTaskName_ = "Unknown";
83     int32_t currentSeekMode_ = -1;
84     int32_t currentSeekTime_ = -1;
85     std::list<TwoPhaseTaskItem> pendingTwoPhaseTasks_;
86     bool isInited_ = false;
87     std::thread::id taskThreadId_;
88     std::mutex mutex_;
89 };
90 }
91 }
92 
93 #endif // PLAYER_SERVER_TASK_MGR_H
94