1 /* 2 * Copyright (c) 2023 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 HCODEC_STATE_H 17 #define HCODEC_STATE_H 18 19 #include <memory> 20 #include <string> 21 #include <utility> 22 #include "msg_handle_loop.h" 23 24 namespace OHOS::MediaAVCodec { 25 class State { 26 public: State(std::string stateName)27 explicit State(std::string stateName) : stateName_(std::move(stateName)) {} GetName()28 const std::string GetName() const { return stateName_; } 29 30 protected: 31 virtual ~State() = default; OnStateEntered()32 virtual void OnStateEntered() {}; OnStateExited()33 virtual void OnStateExited() {}; 34 virtual void OnMsgReceived(const MsgInfo &info) = 0; 35 36 friend class StateMachine; 37 38 std::string stateName_; 39 }; 40 41 class StateMachine : public MsgHandleLoop { 42 public: 43 StateMachine() = default; 44 45 protected: 46 virtual ~StateMachine() = default; 47 void ChangeStateTo(const std::shared_ptr<State> &targetState); 48 void OnMsgReceived(const MsgInfo &info) override; 49 50 std::shared_ptr<State> currState_; 51 }; 52 } // namespace OHOS::MediaAVCodec 53 #endif // HCODEC_STATE_H 54