1fa7767c5Sopenharmony_ci/*
2fa7767c5Sopenharmony_ci * Copyright (c) 2021-2021 Huawei Device Co., Ltd.
3fa7767c5Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
4fa7767c5Sopenharmony_ci * you may not use this file except in compliance with the License.
5fa7767c5Sopenharmony_ci * You may obtain a copy of the License at
6fa7767c5Sopenharmony_ci *
7fa7767c5Sopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
8fa7767c5Sopenharmony_ci *
9fa7767c5Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
10fa7767c5Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
11fa7767c5Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12fa7767c5Sopenharmony_ci * See the License for the specific language governing permissions and
13fa7767c5Sopenharmony_ci * limitations under the License.
14fa7767c5Sopenharmony_ci */
15fa7767c5Sopenharmony_ci
16fa7767c5Sopenharmony_ci#define HST_LOG_TAG "State"
17fa7767c5Sopenharmony_ci
18fa7767c5Sopenharmony_ci#include "state.h"
19fa7767c5Sopenharmony_ci#include "foundation/log.h"
20fa7767c5Sopenharmony_ci#include "pipeline/core/event.h"
21fa7767c5Sopenharmony_ci
22fa7767c5Sopenharmony_cinamespace OHOS {
23fa7767c5Sopenharmony_cinamespace Media {
24fa7767c5Sopenharmony_ciconst char* State::GetStateName(StateId state)
25fa7767c5Sopenharmony_ci{
26fa7767c5Sopenharmony_ci    static const std::map<StateId, const char*> stateDesc = {
27fa7767c5Sopenharmony_ci        {StateId::IDLE, "IDLE"},
28fa7767c5Sopenharmony_ci        {StateId::INIT, "INIT"},
29fa7767c5Sopenharmony_ci        {StateId::PREPARING, "PREPARING"},
30fa7767c5Sopenharmony_ci        {StateId::READY, "READY"},
31fa7767c5Sopenharmony_ci        {StateId::PAUSE, "PAUSE"},
32fa7767c5Sopenharmony_ci        {StateId::PLAYING, "PLAYING"},
33fa7767c5Sopenharmony_ci        {StateId::STOPPED, "STOPPED"},
34fa7767c5Sopenharmony_ci        {StateId::EOS, "EOS"},
35fa7767c5Sopenharmony_ci        {StateId::BUTT, "BUTT"},
36fa7767c5Sopenharmony_ci    };
37fa7767c5Sopenharmony_ci    return stateDesc.at(state);
38fa7767c5Sopenharmony_ci}
39fa7767c5Sopenharmony_ciconst char* State::GetIntentName(Intent intent)
40fa7767c5Sopenharmony_ci{
41fa7767c5Sopenharmony_ci    static const std::map<Intent, const char*> intentDesc = {
42fa7767c5Sopenharmony_ci        {Intent::SET_SOURCE, "SET_SOURCE"},
43fa7767c5Sopenharmony_ci        {Intent::PREPARE, "PREPARE"},
44fa7767c5Sopenharmony_ci        {Intent::SEEK, "SEEK"},
45fa7767c5Sopenharmony_ci        {Intent::PLAY, "PLAY"},
46fa7767c5Sopenharmony_ci        {Intent::PAUSE, "PAUSE"},
47fa7767c5Sopenharmony_ci        {Intent::RESUME, "RESUME"},
48fa7767c5Sopenharmony_ci        {Intent::STOP, "STOP"},
49fa7767c5Sopenharmony_ci        {Intent::RESET, "RESET"},
50fa7767c5Sopenharmony_ci        {Intent::SET_ATTRIBUTE, "SET_ATTRIBUTE"},
51fa7767c5Sopenharmony_ci        {Intent::NOTIFY_READY, "NOTIFY_READY"},
52fa7767c5Sopenharmony_ci        {Intent::NOTIFY_COMPLETE, "NOTIFY_COMPLETE"},
53fa7767c5Sopenharmony_ci        {Intent::NOTIFY_ERROR, "NOTIFY_ERROR"},
54fa7767c5Sopenharmony_ci        {Intent::INTENT_BUTT, "INTENT_BUTT"}
55fa7767c5Sopenharmony_ci    };
56fa7767c5Sopenharmony_ci    return intentDesc.at(intent);
57fa7767c5Sopenharmony_ci}
58fa7767c5Sopenharmony_ciconst char* State::GetActionName(Action action)
59fa7767c5Sopenharmony_ci{
60fa7767c5Sopenharmony_ci    static const std::map<Action, const char*> actionDesc = {
61fa7767c5Sopenharmony_ci        {Action::TRANS_TO_IDLE, "TRANS_TO_IDLE"},
62fa7767c5Sopenharmony_ci        {Action::TRANS_TO_INIT, "TRANS_TO_INIT"},
63fa7767c5Sopenharmony_ci        {Action::TRANS_TO_PREPARING, "TRANS_TO_PREPARING"},
64fa7767c5Sopenharmony_ci        {Action::TRANS_TO_READY, "TRANS_TO_READY"},
65fa7767c5Sopenharmony_ci        {Action::TRANS_TO_PLAYING, "TRANS_TO_PLAYING"},
66fa7767c5Sopenharmony_ci        {Action::TRANS_TO_PAUSE, "TRANS_TO_PAUSE"},
67fa7767c5Sopenharmony_ci        {Action::TRANS_TO_STOPPED, "TRANS_TO_STOPPED"},
68fa7767c5Sopenharmony_ci        {Action::TRANS_TO_EOS, "TRANS_TO_EOS"},
69fa7767c5Sopenharmony_ci        {Action::ACTION_PENDING, "ACTION_PENDING"},
70fa7767c5Sopenharmony_ci        {Action::ACTION_BUTT, "ACTION_BUTT"}
71fa7767c5Sopenharmony_ci    };
72fa7767c5Sopenharmony_ci    return actionDesc.at(action);
73fa7767c5Sopenharmony_ci}
74fa7767c5Sopenharmony_ci
75fa7767c5Sopenharmony_ciState::State(StateId stateId, std::string name, PlayExecutor& executor)
76fa7767c5Sopenharmony_ci    : stateId_(stateId), name_(std::move(name)), executor_(executor)
77fa7767c5Sopenharmony_ci{
78fa7767c5Sopenharmony_ci}
79fa7767c5Sopenharmony_cistd::tuple<ErrorCode, Action> State::Enter(Intent intent)
80fa7767c5Sopenharmony_ci{
81fa7767c5Sopenharmony_ci    (void)intent;
82fa7767c5Sopenharmony_ci    MEDIA_LOG_D("Enter state: " PUBLIC_LOG_S, name_.c_str());
83fa7767c5Sopenharmony_ci    return {ErrorCode::SUCCESS, Action::ACTION_BUTT};
84fa7767c5Sopenharmony_ci}
85fa7767c5Sopenharmony_civoid State::Exit()
86fa7767c5Sopenharmony_ci{
87fa7767c5Sopenharmony_ci    MEDIA_LOG_D("Exit state: " PUBLIC_LOG_S, name_.c_str());
88fa7767c5Sopenharmony_ci}
89fa7767c5Sopenharmony_cistd::tuple<ErrorCode, Action> State::Execute(Intent intent, const Plugin::Any& param)
90fa7767c5Sopenharmony_ci{
91fa7767c5Sopenharmony_ci    return DispatchIntent(intent, param);
92fa7767c5Sopenharmony_ci}
93fa7767c5Sopenharmony_ciconst std::string& State::GetName()
94fa7767c5Sopenharmony_ci{
95fa7767c5Sopenharmony_ci    return name_;
96fa7767c5Sopenharmony_ci}
97fa7767c5Sopenharmony_ciStateId State::GetStateId()
98fa7767c5Sopenharmony_ci{
99fa7767c5Sopenharmony_ci    return stateId_;
100fa7767c5Sopenharmony_ci}
101fa7767c5Sopenharmony_cistd::tuple<ErrorCode, Action> State::SetSource(const Plugin::Any& param)
102fa7767c5Sopenharmony_ci{
103fa7767c5Sopenharmony_ci    (void)param;
104fa7767c5Sopenharmony_ci    return {ErrorCode::ERROR_INVALID_OPERATION, Action::ACTION_BUTT};
105fa7767c5Sopenharmony_ci}
106fa7767c5Sopenharmony_cistd::tuple<ErrorCode, Action> State::Prepare()
107fa7767c5Sopenharmony_ci{
108fa7767c5Sopenharmony_ci    return {ErrorCode::ERROR_INVALID_OPERATION, Action::ACTION_BUTT};
109fa7767c5Sopenharmony_ci}
110fa7767c5Sopenharmony_cistd::tuple<ErrorCode, Action> State::Play()
111fa7767c5Sopenharmony_ci{
112fa7767c5Sopenharmony_ci    return {ErrorCode::ERROR_INVALID_OPERATION, Action::ACTION_BUTT};
113fa7767c5Sopenharmony_ci}
114fa7767c5Sopenharmony_cistd::tuple<ErrorCode, Action> State::Stop()
115fa7767c5Sopenharmony_ci{
116fa7767c5Sopenharmony_ci    return {ErrorCode::ERROR_INVALID_OPERATION, Action::ACTION_BUTT};
117fa7767c5Sopenharmony_ci}
118fa7767c5Sopenharmony_ci
119fa7767c5Sopenharmony_cistd::tuple<ErrorCode, Action> State::Reset()
120fa7767c5Sopenharmony_ci{
121fa7767c5Sopenharmony_ci    return {ErrorCode::SUCCESS, Action::TRANS_TO_IDLE};
122fa7767c5Sopenharmony_ci}
123fa7767c5Sopenharmony_ci
124fa7767c5Sopenharmony_cistd::tuple<ErrorCode, Action> State::Pause()
125fa7767c5Sopenharmony_ci{
126fa7767c5Sopenharmony_ci    return {ErrorCode::ERROR_INVALID_OPERATION, Action::ACTION_BUTT};
127fa7767c5Sopenharmony_ci}
128fa7767c5Sopenharmony_cistd::tuple<ErrorCode, Action> State::Resume()
129fa7767c5Sopenharmony_ci{
130fa7767c5Sopenharmony_ci    return {ErrorCode::ERROR_INVALID_OPERATION, Action::ACTION_BUTT};
131fa7767c5Sopenharmony_ci}
132fa7767c5Sopenharmony_cistd::tuple<ErrorCode, Action> State::Seek(const Plugin::Any& param)
133fa7767c5Sopenharmony_ci{
134fa7767c5Sopenharmony_ci    (void)param;
135fa7767c5Sopenharmony_ci    return {ErrorCode::ERROR_INVALID_OPERATION, Action::ACTION_BUTT};
136fa7767c5Sopenharmony_ci}
137fa7767c5Sopenharmony_cistd::tuple<ErrorCode, Action> State::SetAttribute()
138fa7767c5Sopenharmony_ci{
139fa7767c5Sopenharmony_ci    return {ErrorCode::ERROR_INVALID_OPERATION, Action::ACTION_BUTT};
140fa7767c5Sopenharmony_ci}
141fa7767c5Sopenharmony_cistd::tuple<ErrorCode, Action> State::OnReady()
142fa7767c5Sopenharmony_ci{
143fa7767c5Sopenharmony_ci    return {ErrorCode::ERROR_INVALID_OPERATION, Action::ACTION_BUTT};
144fa7767c5Sopenharmony_ci}
145fa7767c5Sopenharmony_cistd::tuple<ErrorCode, Action> State::OnError(const Plugin::Any& param)
146fa7767c5Sopenharmony_ci{
147fa7767c5Sopenharmony_ci    ErrorCode errorCode = ErrorCode::ERROR_UNKNOWN;
148fa7767c5Sopenharmony_ci    if (Plugin::Any::IsSameTypeWith<ErrorCode>(param)) {
149fa7767c5Sopenharmony_ci        errorCode = Plugin::AnyCast<ErrorCode>(param);
150fa7767c5Sopenharmony_ci    }
151fa7767c5Sopenharmony_ci    executor_.DoOnError(errorCode);
152fa7767c5Sopenharmony_ci    return {ErrorCode::SUCCESS, Action::TRANS_TO_INIT};
153fa7767c5Sopenharmony_ci}
154fa7767c5Sopenharmony_cistd::tuple<ErrorCode, Action> State::OnComplete()
155fa7767c5Sopenharmony_ci{
156fa7767c5Sopenharmony_ci    return {ErrorCode::ERROR_INVALID_OPERATION, Action::ACTION_BUTT};
157fa7767c5Sopenharmony_ci}
158fa7767c5Sopenharmony_cistd::tuple<ErrorCode, Action> State::DispatchIntent(Intent intent, const Plugin::Any& param)
159fa7767c5Sopenharmony_ci{
160fa7767c5Sopenharmony_ci    ErrorCode rtv = ErrorCode::SUCCESS;
161fa7767c5Sopenharmony_ci    Action nextAction = Action::ACTION_BUTT;
162fa7767c5Sopenharmony_ci    switch (intent) {
163fa7767c5Sopenharmony_ci        case Intent::SET_SOURCE:
164fa7767c5Sopenharmony_ci            std::tie(rtv, nextAction) = SetSource(param);
165fa7767c5Sopenharmony_ci            break;
166fa7767c5Sopenharmony_ci        case Intent::PREPARE:
167fa7767c5Sopenharmony_ci            std::tie(rtv, nextAction) = Prepare();
168fa7767c5Sopenharmony_ci            break;
169fa7767c5Sopenharmony_ci        case Intent::SEEK:
170fa7767c5Sopenharmony_ci            std::tie(rtv, nextAction) = Seek(param);
171fa7767c5Sopenharmony_ci            break;
172fa7767c5Sopenharmony_ci        case Intent::PLAY:
173fa7767c5Sopenharmony_ci            std::tie(rtv, nextAction) = Play();
174fa7767c5Sopenharmony_ci            break;
175fa7767c5Sopenharmony_ci        case Intent::PAUSE:
176fa7767c5Sopenharmony_ci            std::tie(rtv, nextAction) = Pause();
177fa7767c5Sopenharmony_ci            break;
178fa7767c5Sopenharmony_ci        case Intent::RESUME:
179fa7767c5Sopenharmony_ci            std::tie(rtv, nextAction) = Resume();
180fa7767c5Sopenharmony_ci            break;
181fa7767c5Sopenharmony_ci        case Intent::STOP:
182fa7767c5Sopenharmony_ci            std::tie(rtv, nextAction) = Stop();
183fa7767c5Sopenharmony_ci            break;
184fa7767c5Sopenharmony_ci        case Intent::RESET:
185fa7767c5Sopenharmony_ci            std::tie(rtv, nextAction) = Reset();
186fa7767c5Sopenharmony_ci            break;
187fa7767c5Sopenharmony_ci        case Intent::SET_ATTRIBUTE:
188fa7767c5Sopenharmony_ci            std::tie(rtv, nextAction) = SetAttribute();
189fa7767c5Sopenharmony_ci            break;
190fa7767c5Sopenharmony_ci        case Intent::NOTIFY_READY:
191fa7767c5Sopenharmony_ci            std::tie(rtv, nextAction) = OnReady();
192fa7767c5Sopenharmony_ci            break;
193fa7767c5Sopenharmony_ci        case Intent::NOTIFY_COMPLETE:
194fa7767c5Sopenharmony_ci            std::tie(rtv, nextAction) = OnComplete();
195fa7767c5Sopenharmony_ci            break;
196fa7767c5Sopenharmony_ci        case Intent::NOTIFY_ERROR:
197fa7767c5Sopenharmony_ci            std::tie(rtv, nextAction) = OnError(param);
198fa7767c5Sopenharmony_ci            break;
199fa7767c5Sopenharmony_ci        default:
200fa7767c5Sopenharmony_ci            break;
201fa7767c5Sopenharmony_ci    }
202fa7767c5Sopenharmony_ci    MEDIA_LOG_D("DispatchIntent " PUBLIC_LOG_S ", curState: " PUBLIC_LOG_S ", action: " PUBLIC_LOG_S,
203fa7767c5Sopenharmony_ci                GetIntentName(intent), name_.c_str(), GetActionName(nextAction));
204fa7767c5Sopenharmony_ci    return {rtv, nextAction};
205fa7767c5Sopenharmony_ci}
206fa7767c5Sopenharmony_ci} // namespace Media
207fa7767c5Sopenharmony_ci} // namespace OHOS