• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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     FREEZE_TASK,
42     UNFREEZE_TASK,
43     SET_VIDEO_SURFACE,
44     BUTT,
45 };
46 
47 class PlayerServerTaskMgr {
48 public:
49     PlayerServerTaskMgr();
50     ~PlayerServerTaskMgr();
51 
52     int32_t Init();
53     int32_t LaunchTask(const std::shared_ptr<ITaskHandler> &task, PlayerServerTaskType type,
54         const std::string &taskName, const std::shared_ptr<ITaskHandler> &cancelTask = nullptr);
55     int32_t SeekTask(const std::shared_ptr<ITaskHandler> &task, const std::shared_ptr<ITaskHandler> &cancelTask,
56         const std::string &taskName, int32_t seekMode, int32_t seekTime);
57     int32_t SpeedTask(const std::shared_ptr<ITaskHandler> &task, const std::shared_ptr<ITaskHandler> &cancelTask,
58         const std::string &taskName, int32_t speedMode);
59     int32_t SeekContinousTask(const std::shared_ptr<ITaskHandler> &task, const std::string &taskName);
60     int32_t SetVideoSurfaeTask(const std::shared_ptr<ITaskHandler> &task, const std::string &taskName);
61     int32_t FreezeTask(const std::shared_ptr<ITaskHandler> &task,
62                        const std::shared_ptr<ITaskHandler> &cancelTask, const std::string &taskName);
63     int32_t UnFreezeTask(const std::shared_ptr<ITaskHandler> &task,
64                          const std::shared_ptr<ITaskHandler> &cancelTask, const std::string &taskName);
65     // only take effect when it is called at the task thread.
66     int32_t MarkTaskDone(const std::string &taskName);
67     void ClearAllTask();
68     int32_t Reset();
69 
70 private:
71     int32_t EnqueueTask(const std::shared_ptr<ITaskHandler> &task, PlayerServerTaskType type,
72         const std::string &taskName);
73     int32_t EnqueueSeekTask(const std::shared_ptr<ITaskHandler> &task, PlayerServerTaskType type,
74         const std::string &taskName, int32_t seekMode, int32_t seekTime);
75     struct TwoPhaseTaskItem {
76         PlayerServerTaskType type;
77         std::shared_ptr<ITaskHandler> task;
78         std::shared_ptr<ITaskHandler> cancelTask;
79         std::string taskName;
80         int32_t seekMode_ = -1;
81         int32_t seekTime_ = -1;
82         int32_t speedMode_ = -1;
83     };
84 
85     std::unique_ptr<TaskQueue> taskThread_;
86     std::shared_ptr<ITaskHandler> currTwoPhaseTask_;
87     PlayerServerTaskType currTwoPhaseType_ = PlayerServerTaskType::BUTT;
88     std::string currTwoPhaseTaskName_ = "Unknown";
89     int32_t currentSeekMode_ = -1;
90     int32_t currentSeekTime_ = -1;
91     std::list<TwoPhaseTaskItem> pendingTwoPhaseTasks_;
92     bool isInited_ = false;
93     std::thread::id taskThreadId_;
94     std::mutex mutex_;
95 };
96 }
97 }
98 
99 #endif // PLAYER_SERVER_TASK_MGR_H
100