1 /*
2  * Copyright (c) 2020-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 PLAYERCONTROL_STATE_MACHINE_H
17 #define PLAYERCONTROL_STATE_MACHINE_H
18 
19 #include <string>
20 #include "hi_state_machine.h"
21 #include "liteplayer_msgtype.h"
22 #include "liteplayer_msgtype.h"
23 #include "liteplayer_define.h"
24 #include "hi_liteplayer.h"
25 #include "format_type.h"
26 #include "hi_demuxer.h"
27 #include "hi_state.h"
28 
29 using OHOS::HiState;
30 using OHOS::HiStateMachineObserver;
31 using OHOS::HiStateMachine;
32 
33 namespace OHOS {
34 namespace Media {
35 class PlayerControlCurState {
36 public:
PlayerControlCurState()37     PlayerControlCurState()
38     {}
~PlayerControlCurState()39     virtual ~PlayerControlCurState()
40     {}
41     virtual PlayerStatus GetCurState(void) = 0;
42     virtual bool EventValidAtCurState(PlayerControlMsgType type) = 0;
43 };
44 
45 class PlayerControlHandle {
46 public:
PlayerControlHandle()47     PlayerControlHandle() {};
~PlayerControlHandle()48     virtual ~PlayerControlHandle() {};
49 
50     virtual void StateChangeCallback(PlayerStatus state) = 0;
51     virtual int32_t DoRegCallback(PlayerCtrlCallbackParam &observer) = 0;
52     virtual int32_t DoSetDataSource(const std::string filepath) = 0;
53     virtual int32_t DoSetDataSource(const int fd) = 0;
54     virtual int32_t DoSetDataSource(BufferStream &stream) = 0;
55     virtual int32_t DoPrepare(void) = 0;
56     virtual int32_t DoPlay(void) = 0;
57     virtual int32_t DoSetVolume(VolumeAttr &volumeAttr) = 0;
58     virtual int32_t ReadPacketAndPushToDecoder(void) = 0;
59     virtual int32_t DoStop(void) = 0;
60     virtual int32_t DoSeek(int64_t timeInMs) = 0;
61     virtual int32_t DoPause(void) = 0;
62     virtual int32_t DoGetFileInfo(FormatFileInfo &fileInfo) = 0;
63     virtual int32_t DoSetMedia(PlayerControlStreamAttr &mediaAttr) = 0;
64     virtual int32_t DoTPlay(TplayAttr& trickPlayAttr) = 0;
65     virtual int32_t DoInvoke(InvokeParameter& invokeParam) = 0;
66     virtual int32_t DoSetAudioStreamType(int32_t type) = 0;
67     virtual void NotifyError(PlayerControlError playerError) = 0;
68 };
69 
70 class PlayerControlState : public HiState {
71 public:
PlayerControlState(PlayerControlHandle & handle,PlayerControlCurState & curState,std::string name)72     PlayerControlState(PlayerControlHandle& handle, PlayerControlCurState& curState, std::string name)
73         : HiState(name), playerControlHandle_(&handle), curState_(&curState) {};
74     virtual ~PlayerControlState(void);
75     virtual int HandleMessage(const MsgInfo& msg);
76     virtual HiState *FindTransition(int event);
77     virtual int Enter(void);
78     virtual int Exit(void);
79 
80 protected:
81     PlayerControlHandle *playerControlHandle_;
82     PlayerControlCurState *curState_;
83 };
84 
85 class PlayerControlSMObserver : public HiStateMachineObserver {
86 public:
PlayerControlSMObserver(PlayerControlHandle & handle)87     explicit PlayerControlSMObserver(PlayerControlHandle& handle)
88         : HiStateMachineObserver(), playerControlHandle_(&handle) {};
89     virtual ~PlayerControlSMObserver();
90 
91     void OnEventHandled(const HiStateMachine& stateMachine, int event, int result);
92 
93 private:
94     PlayerControlHandle *playerControlHandle_;
95 };
96 
97 class PlayerControlStateMachine : public PlayerControlCurState, public HiStateMachine {
98 public:
99     friend class PlayerControlState;
100 
101     explicit PlayerControlStateMachine(PlayerControlHandle& handle);
102     ~PlayerControlStateMachine() override;
103     int32_t Init(uint32_t maxQueueSize, uint32_t maxMsgPayloadSize);
104     int32_t Deinit(void);
105     PlayerStatus GetCurState(void) override;
106     bool EventValidAtCurState(PlayerControlMsgType type) override;
107 
108 private:
109     int32_t CreateStates(void);
110     void DestroyStates(void);
111 
112 private:
113     PlayerControlHandle *playerControlHandle_;
114     PlayerControlState *stateIdle_;
115     PlayerControlState *stateInit_;
116     PlayerControlState *statePrepare_;
117     PlayerControlState *statePlay_;
118     PlayerControlState *stateTPlay_;
119     PlayerControlState *statePause_;
120     PlayerControlState *stateError_;
121 };
122 }
123 }
124 #endif  // PLAYERCONTROL_STATEMACHINE_H
125