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_PLAYING_STATE_H
17 #define HISTREAMER_HIPLAYER_PLAYING_STATE_H
18 
19 #include <memory>
20 #include "foundation/log.h"
21 #include "pipeline/core/error_code.h"
22 #include "play_executor.h"
23 #include "state.h"
24 
25 namespace OHOS {
26 namespace Media {
27 class PlayingState : public State {
28 public:
PlayingState(StateId stateId,PlayExecutor & executor)29     explicit PlayingState(StateId stateId, PlayExecutor& executor) : State(stateId, "PlayingState", executor)
30     {
31     }
32 
33     ~PlayingState() override = default;
34 
Enter(Intent intent)35     std::tuple<ErrorCode, Action> Enter(Intent intent) override
36     {
37         MEDIA_LOG_D("Enter state: " PUBLIC_LOG_S, name_.c_str());
38         ErrorCode ret;
39         if (intent == Intent::RESUME) {
40             ret = executor_.DoResume();
41         } else {
42             ret = executor_.DoPlay();
43         }
44         return {ret, Action::ACTION_BUTT};
45     }
46 
Play()47     std::tuple<ErrorCode, Action> Play() override
48     {
49         return {ErrorCode::SUCCESS, Action::ACTION_BUTT};
50     }
51 
Seek(const Plugin::Any & param)52     std::tuple<ErrorCode, Action> Seek(const Plugin::Any& param) override
53     {
54         MEDIA_LOG_D("Seek in playing state.");
55         std::tuple<ErrorCode, Action> err {ErrorCode::ERROR_INVALID_PARAMETER_TYPE, Action::ACTION_BUTT};
56         FALSE_RETURN_V(Plugin::Any::IsSameTypeWith<SeekInfo>(param), err);
57         auto info = Plugin::AnyCast<SeekInfo>(param);
58         auto ret = executor_.DoSeek(info.hstTime, info.mode, true);
59         return {ret, Action::ACTION_BUTT};
60     }
61 
Pause()62     std::tuple<ErrorCode, Action> Pause() override
63     {
64         return {ErrorCode::SUCCESS, Action::TRANS_TO_PAUSE};
65     }
66 
Stop()67     std::tuple<ErrorCode, Action> Stop() override
68     {
69         return {ErrorCode::SUCCESS, Action::TRANS_TO_STOPPED};
70     }
71 
OnComplete()72     std::tuple<ErrorCode, Action> OnComplete() override
73     {
74         auto ret = executor_.DoOnComplete();
75         if (executor_.IsSingleLoop()) {
76             MEDIA_LOG_I("will seeking to 0 and continue playing");
77             auto seekRes = executor_.DoSeek(0, Plugin::SeekMode::SEEK_PREVIOUS_SYNC, false);
78             return {seekRes, Action::ACTION_BUTT};
79         } else {
80             return {ret, Action::TRANS_TO_EOS};
81         }
82     }
83 };
84 } // namespace Media
85 } // namespace OHOS
86 #endif
87