1 /*
2  * Copyright (c) 2021-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 HISTREAMER_HIRECORDER_STATE_H
17 #define HISTREAMER_HIRECORDER_STATE_H
18 
19 #include <map>
20 #include <memory>
21 #include <string>
22 #include <tuple>
23 #include <unordered_map>
24 #include <functional>
25 #include "foundation/log.h"
26 #include "pipeline/core/error_code.h"
27 #include "plugin/common/any.h"
28 #include "recorder_executor.h"
29 
30 namespace OHOS {
31 namespace Media {
32 namespace Record {
33 enum class StateId {
34     INIT,
35     RECORDING_SETTING,
36     READY,
37     PAUSE,
38     RECORDING,
39     ERROR,
40     BUTT,
41 };
42 
43 enum class Intent {
44     SET_OBS,
45     SET_VIDEO_SOURCE,
46     SET_AUDIO_SOURCE,
47     SET_OUTPUT_FORMAT,
48     CONFIGURE,
49     PREPARE,
50     START,
51     PAUSE,
52     RESUME,
53     STOP,
54     RESET,
55     NOTIFY_READY,
56     NOTIFY_COMPLETE,
57     NOTIFY_ERROR,
58     INTENT_BUTT
59 };
60 
61 enum class Action {
62     TRANS_TO_INIT,
63     TRANS_TO_RECORDING_SETTING,
64     TRANS_TO_READY,
65     TRANS_TO_RECORDING,
66     TRANS_TO_PAUSE,
67     TRANS_TO_ERROR,
68     ACTION_PENDING,
69     ACTION_BUTT
70 };
71 
72 class State {
73 public:
74     State(StateId stateId, std::string name, RecorderExecutor& executor);
75     virtual ~State() = default;
76     virtual std::tuple<ErrorCode, Action> Enter(Intent intent);
77     virtual void Exit();
78     std::tuple<ErrorCode, Action> Execute(Intent intent, const Plugin::Any& param);
79     const std::string &GetName();
80     StateId GetStateId();
81 
82     virtual std::tuple<ErrorCode, Action> SetVideoSource(const Plugin::Any& param);
83     virtual std::tuple<ErrorCode, Action> SetAudioSource(const Plugin::Any& param);
84     virtual std::tuple<ErrorCode, Action> Configure(const Plugin::Any& param);
85     virtual std::tuple<ErrorCode, Action> SetOutputFormat(const Plugin::Any& param);
86     virtual std::tuple<ErrorCode, Action> SetObs();
87     virtual std::tuple<ErrorCode, Action> GetSurface();
88     virtual std::tuple<ErrorCode, Action> Prepare();
89     virtual std::tuple<ErrorCode, Action> Start();
90     virtual std::tuple<ErrorCode, Action> Stop(const Plugin::Any& param);
91     virtual std::tuple<ErrorCode, Action> Pause();
92     virtual std::tuple<ErrorCode, Action> Resume();
93     virtual std::tuple<ErrorCode, Action> Reset();
94     virtual std::tuple<ErrorCode, Action> OnReady();
95     virtual std::tuple<ErrorCode, Action> OnError(const Plugin::Any& param) final;
96     virtual std::tuple<ErrorCode, Action> OnComplete();
97 
98 protected:
99     std::tuple<ErrorCode, Action> DispatchIntent(Intent intent, const Plugin::Any& param);
100 
101     const StateId stateId_;
102     const std::string name_;
103     RecorderExecutor &executor_;
104     const std::map<Intent, std::string> intentDesc_ = {
105         {Intent::SET_OBS, "SET_OBS"},
106         {Intent::SET_VIDEO_SOURCE, "SET_VIDEO_SOURCE"},
107         {Intent::SET_AUDIO_SOURCE, "SET_AUDIO_SOURCE"},
108         {Intent::SET_OUTPUT_FORMAT, "SET_OUTPUT_FORMAT"},
109         {Intent::CONFIGURE, "CONFIGURE"},
110         {Intent::PREPARE, "PREPARE"},
111         {Intent::START, "START"},
112         {Intent::PAUSE, "PAUSE"},
113         {Intent::RESUME, "RESUME"},
114         {Intent::STOP, "STOP"},
115         {Intent::RESET, "RESET"},
116         {Intent::NOTIFY_READY, "NOTIFY_READY"},
117         {Intent::NOTIFY_COMPLETE, "NOTIFY_COMPLETE"},
118         {Intent::NOTIFY_ERROR, "NOTIFY_ERROR"},
119         {Intent::INTENT_BUTT, "INTENT_BUTT"}};
120     const std::map<Action, std::string> actionDesc_ = {
121         {Action::TRANS_TO_INIT, "TRANS_TO_INIT"},
122         {Action::TRANS_TO_RECORDING_SETTING, "TRANS_TO_RECORDING_SETTING"},
123         {Action::TRANS_TO_READY, "TRANS_TO_READY"},
124         {Action::TRANS_TO_RECORDING, "TRANS_TO_RECORDING"},
125         {Action::TRANS_TO_PAUSE, "TRANS_TO_PAUSE"},
126         {Action::TRANS_TO_ERROR, "TRANS_TO_ERROR"},
127         {Action::ACTION_PENDING, "ACTION_PENDING"},
128         {Action::ACTION_BUTT, "ACTION_BUTT"}};
129     const std::unordered_map<Intent, std::function<std::tuple<ErrorCode, Action>(
130         const Plugin::Any &param)>> intentDispatchersMap_ = {
131         {Intent::SET_OBS, [this](const Plugin::Any &param) {
132             std::ignore = param;
133             return SetObs();
134         }},
135         {Intent::SET_VIDEO_SOURCE, [this](const Plugin::Any &param) { return SetVideoSource(param); }},
136         {Intent::SET_AUDIO_SOURCE, [this](const Plugin::Any &param) { return SetAudioSource(param); }},
137         {Intent::CONFIGURE, [this](const Plugin::Any &param) { return Configure(param); }},
138         {Intent::SET_OUTPUT_FORMAT, [this](const Plugin::Any &param) { return SetOutputFormat(param); }},
139         {Intent::PREPARE, [this](const Plugin::Any &param) {
140             std::ignore = param;
141             return Prepare();
142         }},
143         {Intent::START, [this](const Plugin::Any &param) {
144             std::ignore = param;
145             return Start();
146         }},
147         {Intent::PAUSE, [this](const Plugin::Any &param) {
148             std::ignore = param;
149             return Pause();
150         }},
151         {Intent::RESUME, [this](const Plugin::Any &param) {
152             std::ignore = param;
153             return Resume();
154         }},
155         {Intent::RESET, [this](const Plugin::Any &param) {
156             std::ignore = param;
157             return Reset();
158         }},
159         {Intent::STOP, [this](const Plugin::Any &param) {
160             return Stop(param);
161         }},
162         {Intent::NOTIFY_READY, [this](const Plugin::Any &param) {
163             std::ignore = param;
164             return OnReady();
165         }},
166         {Intent::NOTIFY_COMPLETE, [this](const Plugin::Any &param) {
167             std::ignore = param;
168             return OnComplete();
169         }},
170         {Intent::NOTIFY_ERROR, [this](const Plugin::Any &param) {
171             return OnError(param);
172         }},
173     };
174 };
175 } // namespace Record
176 } // namespace Media
177 } // namespace OHOS
178 #endif
179