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 #define HST_LOG_TAG "State"
17 
18 #include "state.h"
19 #include "foundation/log.h"
20 #include "pipeline/core/event.h"
21 
22 namespace OHOS {
23 namespace Media {
GetStateName(StateId state)24 const char* State::GetStateName(StateId state)
25 {
26     static const std::map<StateId, const char*> stateDesc = {
27         {StateId::IDLE, "IDLE"},
28         {StateId::INIT, "INIT"},
29         {StateId::PREPARING, "PREPARING"},
30         {StateId::READY, "READY"},
31         {StateId::PAUSE, "PAUSE"},
32         {StateId::PLAYING, "PLAYING"},
33         {StateId::STOPPED, "STOPPED"},
34         {StateId::EOS, "EOS"},
35         {StateId::BUTT, "BUTT"},
36     };
37     return stateDesc.at(state);
38 }
GetIntentName(Intent intent)39 const char* State::GetIntentName(Intent intent)
40 {
41     static const std::map<Intent, const char*> intentDesc = {
42         {Intent::SET_SOURCE, "SET_SOURCE"},
43         {Intent::PREPARE, "PREPARE"},
44         {Intent::SEEK, "SEEK"},
45         {Intent::PLAY, "PLAY"},
46         {Intent::PAUSE, "PAUSE"},
47         {Intent::RESUME, "RESUME"},
48         {Intent::STOP, "STOP"},
49         {Intent::RESET, "RESET"},
50         {Intent::SET_ATTRIBUTE, "SET_ATTRIBUTE"},
51         {Intent::NOTIFY_READY, "NOTIFY_READY"},
52         {Intent::NOTIFY_COMPLETE, "NOTIFY_COMPLETE"},
53         {Intent::NOTIFY_ERROR, "NOTIFY_ERROR"},
54         {Intent::INTENT_BUTT, "INTENT_BUTT"}
55     };
56     return intentDesc.at(intent);
57 }
GetActionName(Action action)58 const char* State::GetActionName(Action action)
59 {
60     static const std::map<Action, const char*> actionDesc = {
61         {Action::TRANS_TO_IDLE, "TRANS_TO_IDLE"},
62         {Action::TRANS_TO_INIT, "TRANS_TO_INIT"},
63         {Action::TRANS_TO_PREPARING, "TRANS_TO_PREPARING"},
64         {Action::TRANS_TO_READY, "TRANS_TO_READY"},
65         {Action::TRANS_TO_PLAYING, "TRANS_TO_PLAYING"},
66         {Action::TRANS_TO_PAUSE, "TRANS_TO_PAUSE"},
67         {Action::TRANS_TO_STOPPED, "TRANS_TO_STOPPED"},
68         {Action::TRANS_TO_EOS, "TRANS_TO_EOS"},
69         {Action::ACTION_PENDING, "ACTION_PENDING"},
70         {Action::ACTION_BUTT, "ACTION_BUTT"}
71     };
72     return actionDesc.at(action);
73 }
74 
State(StateId stateId,std::string name,PlayExecutor & executor)75 State::State(StateId stateId, std::string name, PlayExecutor& executor)
76     : stateId_(stateId), name_(std::move(name)), executor_(executor)
77 {
78 }
Enter(Intent intent)79 std::tuple<ErrorCode, Action> State::Enter(Intent intent)
80 {
81     (void)intent;
82     MEDIA_LOG_D("Enter state: " PUBLIC_LOG_S, name_.c_str());
83     return {ErrorCode::SUCCESS, Action::ACTION_BUTT};
84 }
Exit()85 void State::Exit()
86 {
87     MEDIA_LOG_D("Exit state: " PUBLIC_LOG_S, name_.c_str());
88 }
Execute(Intent intent,const Plugin::Any & param)89 std::tuple<ErrorCode, Action> State::Execute(Intent intent, const Plugin::Any& param)
90 {
91     return DispatchIntent(intent, param);
92 }
GetName()93 const std::string& State::GetName()
94 {
95     return name_;
96 }
GetStateId()97 StateId State::GetStateId()
98 {
99     return stateId_;
100 }
SetSource(const Plugin::Any & param)101 std::tuple<ErrorCode, Action> State::SetSource(const Plugin::Any& param)
102 {
103     (void)param;
104     return {ErrorCode::ERROR_INVALID_OPERATION, Action::ACTION_BUTT};
105 }
Prepare()106 std::tuple<ErrorCode, Action> State::Prepare()
107 {
108     return {ErrorCode::ERROR_INVALID_OPERATION, Action::ACTION_BUTT};
109 }
Play()110 std::tuple<ErrorCode, Action> State::Play()
111 {
112     return {ErrorCode::ERROR_INVALID_OPERATION, Action::ACTION_BUTT};
113 }
Stop()114 std::tuple<ErrorCode, Action> State::Stop()
115 {
116     return {ErrorCode::ERROR_INVALID_OPERATION, Action::ACTION_BUTT};
117 }
118 
Reset()119 std::tuple<ErrorCode, Action> State::Reset()
120 {
121     return {ErrorCode::SUCCESS, Action::TRANS_TO_IDLE};
122 }
123 
Pause()124 std::tuple<ErrorCode, Action> State::Pause()
125 {
126     return {ErrorCode::ERROR_INVALID_OPERATION, Action::ACTION_BUTT};
127 }
Resume()128 std::tuple<ErrorCode, Action> State::Resume()
129 {
130     return {ErrorCode::ERROR_INVALID_OPERATION, Action::ACTION_BUTT};
131 }
Seek(const Plugin::Any & param)132 std::tuple<ErrorCode, Action> State::Seek(const Plugin::Any& param)
133 {
134     (void)param;
135     return {ErrorCode::ERROR_INVALID_OPERATION, Action::ACTION_BUTT};
136 }
SetAttribute()137 std::tuple<ErrorCode, Action> State::SetAttribute()
138 {
139     return {ErrorCode::ERROR_INVALID_OPERATION, Action::ACTION_BUTT};
140 }
OnReady()141 std::tuple<ErrorCode, Action> State::OnReady()
142 {
143     return {ErrorCode::ERROR_INVALID_OPERATION, Action::ACTION_BUTT};
144 }
OnError(const Plugin::Any & param)145 std::tuple<ErrorCode, Action> State::OnError(const Plugin::Any& param)
146 {
147     ErrorCode errorCode = ErrorCode::ERROR_UNKNOWN;
148     if (Plugin::Any::IsSameTypeWith<ErrorCode>(param)) {
149         errorCode = Plugin::AnyCast<ErrorCode>(param);
150     }
151     executor_.DoOnError(errorCode);
152     return {ErrorCode::SUCCESS, Action::TRANS_TO_INIT};
153 }
OnComplete()154 std::tuple<ErrorCode, Action> State::OnComplete()
155 {
156     return {ErrorCode::ERROR_INVALID_OPERATION, Action::ACTION_BUTT};
157 }
DispatchIntent(Intent intent,const Plugin::Any & param)158 std::tuple<ErrorCode, Action> State::DispatchIntent(Intent intent, const Plugin::Any& param)
159 {
160     ErrorCode rtv = ErrorCode::SUCCESS;
161     Action nextAction = Action::ACTION_BUTT;
162     switch (intent) {
163         case Intent::SET_SOURCE:
164             std::tie(rtv, nextAction) = SetSource(param);
165             break;
166         case Intent::PREPARE:
167             std::tie(rtv, nextAction) = Prepare();
168             break;
169         case Intent::SEEK:
170             std::tie(rtv, nextAction) = Seek(param);
171             break;
172         case Intent::PLAY:
173             std::tie(rtv, nextAction) = Play();
174             break;
175         case Intent::PAUSE:
176             std::tie(rtv, nextAction) = Pause();
177             break;
178         case Intent::RESUME:
179             std::tie(rtv, nextAction) = Resume();
180             break;
181         case Intent::STOP:
182             std::tie(rtv, nextAction) = Stop();
183             break;
184         case Intent::RESET:
185             std::tie(rtv, nextAction) = Reset();
186             break;
187         case Intent::SET_ATTRIBUTE:
188             std::tie(rtv, nextAction) = SetAttribute();
189             break;
190         case Intent::NOTIFY_READY:
191             std::tie(rtv, nextAction) = OnReady();
192             break;
193         case Intent::NOTIFY_COMPLETE:
194             std::tie(rtv, nextAction) = OnComplete();
195             break;
196         case Intent::NOTIFY_ERROR:
197             std::tie(rtv, nextAction) = OnError(param);
198             break;
199         default:
200             break;
201     }
202     MEDIA_LOG_D("DispatchIntent " PUBLIC_LOG_S ", curState: " PUBLIC_LOG_S ", action: " PUBLIC_LOG_S,
203                 GetIntentName(intent), name_.c_str(), GetActionName(nextAction));
204     return {rtv, nextAction};
205 }
206 } // namespace Media
207 } // namespace OHOS