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_HIPLAYER_STATE_H
17 #define HISTREAMER_HIPLAYER_STATE_H
18 
19 #include <map>
20 #include <memory>
21 #include <string>
22 #include <tuple>
23 #include "foundation/log.h"
24 #include "pipeline/core/error_code.h"
25 #include "play_executor.h"
26 #include "plugin/common/any.h"
27 
28 namespace OHOS {
29 namespace Media {
30 enum class StateId {
31     IDLE = 0,
32     INIT = 1,
33     PREPARING = 2,
34     READY = 3,
35     PAUSE = 4,
36     PLAYING = 5,
37     STOPPED = 6,
38     EOS = 7,
39     BUTT,
40 };
41 
42 enum class Intent {
43     SET_SOURCE,
44     SEEK,
45     PREPARE,
46     PLAY,
47     PAUSE,
48     RESUME,
49     STOP,
50     RESET,
51     SET_ATTRIBUTE,
52     NOTIFY_READY,
53     NOTIFY_COMPLETE,
54     NOTIFY_ERROR,
55     INTENT_BUTT
56 };
57 
58 enum class Action {
59     TRANS_TO_IDLE,
60     TRANS_TO_INIT,
61     TRANS_TO_PREPARING,
62     TRANS_TO_READY,
63     TRANS_TO_PLAYING,
64     TRANS_TO_PAUSE,
65     TRANS_TO_STOPPED,
66     TRANS_TO_EOS,
67     ACTION_PENDING,
68     ACTION_BUTT
69 };
70 
71 class State {
72 public:
73     State(StateId stateId, std::string name, PlayExecutor& executor);
74     virtual ~State() = default;
75     virtual std::tuple<ErrorCode, Action> Enter(Intent intent);
76     virtual void Exit();
77     std::tuple<ErrorCode, Action> Execute(Intent intent, const Plugin::Any& param);
78     const std::string& GetName();
79     StateId GetStateId();
80     virtual std::tuple<ErrorCode, Action> SetSource(const Plugin::Any& param);
81     virtual std::tuple<ErrorCode, Action> Prepare();
82     virtual std::tuple<ErrorCode, Action> Play();
83     virtual std::tuple<ErrorCode, Action> Stop();
84     virtual std::tuple<ErrorCode, Action> Reset();
85     virtual std::tuple<ErrorCode, Action> Pause();
86     virtual std::tuple<ErrorCode, Action> Resume();
87     virtual std::tuple<ErrorCode, Action> Seek(const Plugin::Any& param);
88     virtual std::tuple<ErrorCode, Action> SetAttribute();
89     virtual std::tuple<ErrorCode, Action> OnReady();
90     virtual std::tuple<ErrorCode, Action> OnError(const Plugin::Any& param) final;
91     virtual std::tuple<ErrorCode, Action> OnComplete();
92 
93     static const char* GetStateName(StateId state);
94     static const char* GetIntentName(Intent intent);
95     static const char* GetActionName(Action action);
96 
97 protected:
98     std::tuple<ErrorCode, Action> DispatchIntent(Intent intent, const Plugin::Any& param);
99 
100     const StateId stateId_;
101     const std::string name_;
102     PlayExecutor& executor_;
103 };
104 } // namespace Media
105 } // namespace OHOS
106 #endif
107