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_MACHINE_H 17 #define HISTREAMER_HIRECORDER_STATE_MACHINE_H 18 19 #include <map> 20 #include <memory> 21 #include <queue> 22 #include <tuple> 23 #include "error_state.h" 24 #include "foundation/log.h" 25 #include "foundation/osal/base/synchronizer.h" 26 #include "foundation/osal/thread/mutex.h" 27 #include "foundation/osal/thread/task.h" 28 #include "init_state.h" 29 #include "pause_state.h" 30 #include "pipeline/core/error_code.h" 31 #include "plugin/common/any.h" 32 #include "ready_state.h" 33 #include "recording_setting_state.h" 34 #include "recording_state.h" 35 #include "recorder_executor.h" 36 #include "utils/blocking_queue.h" 37 38 namespace OHOS { 39 namespace Media { 40 namespace Record { 41 class StateChangeCallback { 42 public: 43 virtual ~StateChangeCallback() = default; 44 virtual void OnStateChanged(StateId state) = 0; 45 }; 46 47 class StateMachine final : public OSAL::Task { 48 public: 49 explicit StateMachine(RecorderExecutor& executor); 50 51 ~StateMachine() override = default; 52 53 void Stop() override; 54 55 void SetStateCallback(StateChangeCallback* callback); 56 57 const std::string& GetCurrentState() const; 58 59 StateId GetCurrentStateId() const; 60 61 ErrorCode SendEvent(Intent intent, const Plugin::Any& param = {}); 62 63 ErrorCode SendEvent(Intent intent, const Plugin::Any& param = {}) const; 64 65 ErrorCode SendEventAsync(Intent intent, const Plugin::Any& param = {}); 66 67 ErrorCode SendEventAsync(Intent intent, const Plugin::Any& param = {}) const; 68 69 private: 70 using Job = std::function<Action()>; 71 72 Action ProcessIntent(Intent intent, const Plugin::Any& param); 73 74 void DoTask() override; 75 76 void AddState(const std::shared_ptr<State>& state); 77 78 ErrorCode ProcAction(Action nextAction); 79 80 ErrorCode TransitionTo(const std::shared_ptr<State>& state); 81 82 void OnIntentExecuted(Intent intent, Action action, ErrorCode result); 83 84 mutable OSAL::Mutex mutex_ {}; 85 mutable OSAL::Synchronizer<Intent, ErrorCode> intentSync_; 86 mutable std::shared_ptr<State> curState_ {nullptr}; 87 mutable Intent lastIntent {Intent::INTENT_BUTT}; 88 std::map<StateId, std::shared_ptr<State>> states_ {}; 89 Media::BlockingQueue<Job> jobs_; 90 std::queue<Job> pendingJobs_ {}; 91 StateChangeCallback* callback_ {nullptr}; 92 }; 93 } // namespace Record 94 } // namespace Media 95 } // namespace OHOS 96 #endif 97