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 
20 namespace OHOS {
21 namespace Media {
22 namespace Record {
State(StateId stateId,std::string name,RecorderExecutor & executor)23 State::State(StateId stateId, std::string name, RecorderExecutor& executor)
24     : stateId_(stateId), name_(std::move(name)), executor_(executor)
25 {
26 }
27 
Enter(Intent intent)28 std::tuple<ErrorCode, Action> State::Enter(Intent intent)
29 {
30     (void)intent;
31     MEDIA_LOG_D("Enter state: " PUBLIC_LOG_S, name_.c_str());
32     return {ErrorCode::SUCCESS, Action::ACTION_BUTT};
33 }
34 
Exit()35 void State::Exit()
36 {
37     MEDIA_LOG_D("Exit state: " PUBLIC_LOG_S, name_.c_str());
38 }
39 
Execute(Intent intent,const Plugin::Any & param)40 std::tuple<ErrorCode, Action> State::Execute(Intent intent, const Plugin::Any& param)
41 {
42     return DispatchIntent(intent, param);
43 }
44 
GetName()45 const std::string& State::GetName()
46 {
47     return name_;
48 }
49 
GetStateId()50 StateId State::GetStateId()
51 {
52     return stateId_;
53 }
54 
SetVideoSource(const Plugin::Any & param)55 std::tuple<ErrorCode, Action> State::SetVideoSource(const Plugin::Any& param)
56 {
57     (void)param;
58     return {ErrorCode::ERROR_INVALID_OPERATION, Action::ACTION_BUTT};
59 }
60 
SetAudioSource(const Plugin::Any & param)61 std::tuple<ErrorCode, Action> State::SetAudioSource(const Plugin::Any& param)
62 {
63     (void)param;
64     return {ErrorCode::ERROR_INVALID_OPERATION, Action::ACTION_BUTT};
65 }
66 
Configure(const Plugin::Any & param)67 std::tuple<ErrorCode, Action> State::Configure(const Plugin::Any &param)
68 {
69     (void)param;
70     return {ErrorCode::ERROR_INVALID_OPERATION, Action::ACTION_BUTT};
71 }
72 
SetOutputFormat(const Plugin::Any & param)73 std::tuple<ErrorCode, Action> State::SetOutputFormat(const Plugin::Any& param)
74 {
75     (void)param;
76     return {ErrorCode::ERROR_INVALID_OPERATION, Action::ACTION_BUTT};
77 }
78 
SetObs()79 std::tuple<ErrorCode, Action> State::SetObs()
80 {
81     return {ErrorCode::ERROR_INVALID_OPERATION, Action::ACTION_BUTT};
82 }
83 
GetSurface()84 std::tuple<ErrorCode, Action> State::GetSurface()
85 {
86     return {ErrorCode::ERROR_INVALID_OPERATION, Action::ACTION_BUTT};
87 }
88 
Prepare()89 std::tuple<ErrorCode, Action> State::Prepare()
90 {
91     return {ErrorCode::ERROR_INVALID_OPERATION, Action::ACTION_BUTT};
92 }
93 
Start()94 std::tuple<ErrorCode, Action> State::Start()
95 {
96     return {ErrorCode::ERROR_INVALID_OPERATION, Action::ACTION_BUTT};
97 }
98 
Stop(const Plugin::Any & param)99 std::tuple<ErrorCode, Action> State::Stop(const Plugin::Any& param)
100 {
101     (void)param;
102     return {ErrorCode::ERROR_INVALID_OPERATION, Action::ACTION_BUTT};
103 }
104 
Pause()105 std::tuple<ErrorCode, Action> State::Pause()
106 {
107     return {ErrorCode::ERROR_INVALID_OPERATION, Action::ACTION_BUTT};
108 }
109 
Resume()110 std::tuple<ErrorCode, Action> State::Resume()
111 {
112     return {ErrorCode::ERROR_INVALID_OPERATION, Action::ACTION_BUTT};
113 }
114 
Reset()115 std::tuple<ErrorCode, Action> State::Reset()
116 {
117     FALSE_LOG(executor_.DoReset() == ErrorCode::SUCCESS);
118     return {ErrorCode::SUCCESS, Action::TRANS_TO_INIT};
119 }
120 
OnReady()121 std::tuple<ErrorCode, Action> State::OnReady()
122 {
123     return {ErrorCode::ERROR_INVALID_OPERATION, Action::ACTION_BUTT};
124 }
125 
OnError(const Plugin::Any & param)126 std::tuple<ErrorCode, Action> State::OnError(const Plugin::Any& param)
127 {
128     return {ErrorCode::SUCCESS, Action::TRANS_TO_ERROR};
129 }
130 
OnComplete()131 std::tuple<ErrorCode, Action> State::OnComplete()
132 {
133     return {ErrorCode::SUCCESS, Action::ACTION_BUTT};
134 }
135 
DispatchIntent(Intent intent,const Plugin::Any & param)136 std::tuple<ErrorCode, Action> State::DispatchIntent(Intent intent, const Plugin::Any& param)
137 {
138     ErrorCode rtv = ErrorCode::SUCCESS;
139     Action nextAction = Action::ACTION_BUTT;
140 
141     auto iter = intentDispatchersMap_.find(intent);
142     if (iter != intentDispatchersMap_.end()) {
143         std::function<std::tuple<ErrorCode, Action>(const Plugin::Any &param)> updator = iter->second;
144         std::tie(rtv, nextAction) = updator(param);
145     }
146 
147     MEDIA_LOG_D("DispatchIntent " PUBLIC_LOG_S ", curState: " PUBLIC_LOG_S ", nextState: " PUBLIC_LOG_S,
148                 intentDesc_.at(intent).c_str(), name_.c_str(), actionDesc_.at(nextAction).c_str());
149     return {rtv, nextAction};
150 }
151 } // namespace Record
152 } // namespace Media
153 } // namespace OHOS