1fa7767c5Sopenharmony_ci/*
2fa7767c5Sopenharmony_ci * Copyright (c) 2021-2021 Huawei Device Co., Ltd.
3fa7767c5Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
4fa7767c5Sopenharmony_ci * you may not use this file except in compliance with the License.
5fa7767c5Sopenharmony_ci * You may obtain a copy of the License at
6fa7767c5Sopenharmony_ci *
7fa7767c5Sopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
8fa7767c5Sopenharmony_ci *
9fa7767c5Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
10fa7767c5Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
11fa7767c5Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12fa7767c5Sopenharmony_ci * See the License for the specific language governing permissions and
13fa7767c5Sopenharmony_ci * limitations under the License.
14fa7767c5Sopenharmony_ci */
15fa7767c5Sopenharmony_ci
16fa7767c5Sopenharmony_ci#ifndef HISTREAMER_HIPLAYER_PLAYING_STATE_H
17fa7767c5Sopenharmony_ci#define HISTREAMER_HIPLAYER_PLAYING_STATE_H
18fa7767c5Sopenharmony_ci
19fa7767c5Sopenharmony_ci#include <memory>
20fa7767c5Sopenharmony_ci#include "foundation/log.h"
21fa7767c5Sopenharmony_ci#include "pipeline/core/error_code.h"
22fa7767c5Sopenharmony_ci#include "play_executor.h"
23fa7767c5Sopenharmony_ci#include "state.h"
24fa7767c5Sopenharmony_ci
25fa7767c5Sopenharmony_cinamespace OHOS {
26fa7767c5Sopenharmony_cinamespace Media {
27fa7767c5Sopenharmony_ciclass PlayingState : public State {
28fa7767c5Sopenharmony_cipublic:
29fa7767c5Sopenharmony_ci    explicit PlayingState(StateId stateId, PlayExecutor& executor) : State(stateId, "PlayingState", executor)
30fa7767c5Sopenharmony_ci    {
31fa7767c5Sopenharmony_ci    }
32fa7767c5Sopenharmony_ci
33fa7767c5Sopenharmony_ci    ~PlayingState() override = default;
34fa7767c5Sopenharmony_ci
35fa7767c5Sopenharmony_ci    std::tuple<ErrorCode, Action> Enter(Intent intent) override
36fa7767c5Sopenharmony_ci    {
37fa7767c5Sopenharmony_ci        MEDIA_LOG_D("Enter state: " PUBLIC_LOG_S, name_.c_str());
38fa7767c5Sopenharmony_ci        ErrorCode ret;
39fa7767c5Sopenharmony_ci        if (intent == Intent::RESUME) {
40fa7767c5Sopenharmony_ci            ret = executor_.DoResume();
41fa7767c5Sopenharmony_ci        } else {
42fa7767c5Sopenharmony_ci            ret = executor_.DoPlay();
43fa7767c5Sopenharmony_ci        }
44fa7767c5Sopenharmony_ci        return {ret, Action::ACTION_BUTT};
45fa7767c5Sopenharmony_ci    }
46fa7767c5Sopenharmony_ci
47fa7767c5Sopenharmony_ci    std::tuple<ErrorCode, Action> Play() override
48fa7767c5Sopenharmony_ci    {
49fa7767c5Sopenharmony_ci        return {ErrorCode::SUCCESS, Action::ACTION_BUTT};
50fa7767c5Sopenharmony_ci    }
51fa7767c5Sopenharmony_ci
52fa7767c5Sopenharmony_ci    std::tuple<ErrorCode, Action> Seek(const Plugin::Any& param) override
53fa7767c5Sopenharmony_ci    {
54fa7767c5Sopenharmony_ci        MEDIA_LOG_D("Seek in playing state.");
55fa7767c5Sopenharmony_ci        std::tuple<ErrorCode, Action> err {ErrorCode::ERROR_INVALID_PARAMETER_TYPE, Action::ACTION_BUTT};
56fa7767c5Sopenharmony_ci        FALSE_RETURN_V(Plugin::Any::IsSameTypeWith<SeekInfo>(param), err);
57fa7767c5Sopenharmony_ci        auto info = Plugin::AnyCast<SeekInfo>(param);
58fa7767c5Sopenharmony_ci        auto ret = executor_.DoSeek(info.hstTime, info.mode, true);
59fa7767c5Sopenharmony_ci        return {ret, Action::ACTION_BUTT};
60fa7767c5Sopenharmony_ci    }
61fa7767c5Sopenharmony_ci
62fa7767c5Sopenharmony_ci    std::tuple<ErrorCode, Action> Pause() override
63fa7767c5Sopenharmony_ci    {
64fa7767c5Sopenharmony_ci        return {ErrorCode::SUCCESS, Action::TRANS_TO_PAUSE};
65fa7767c5Sopenharmony_ci    }
66fa7767c5Sopenharmony_ci
67fa7767c5Sopenharmony_ci    std::tuple<ErrorCode, Action> Stop() override
68fa7767c5Sopenharmony_ci    {
69fa7767c5Sopenharmony_ci        return {ErrorCode::SUCCESS, Action::TRANS_TO_STOPPED};
70fa7767c5Sopenharmony_ci    }
71fa7767c5Sopenharmony_ci
72fa7767c5Sopenharmony_ci    std::tuple<ErrorCode, Action> OnComplete() override
73fa7767c5Sopenharmony_ci    {
74fa7767c5Sopenharmony_ci        auto ret = executor_.DoOnComplete();
75fa7767c5Sopenharmony_ci        if (executor_.IsSingleLoop()) {
76fa7767c5Sopenharmony_ci            MEDIA_LOG_I("will seeking to 0 and continue playing");
77fa7767c5Sopenharmony_ci            auto seekRes = executor_.DoSeek(0, Plugin::SeekMode::SEEK_PREVIOUS_SYNC, false);
78fa7767c5Sopenharmony_ci            return {seekRes, Action::ACTION_BUTT};
79fa7767c5Sopenharmony_ci        } else {
80fa7767c5Sopenharmony_ci            return {ret, Action::TRANS_TO_EOS};
81fa7767c5Sopenharmony_ci        }
82fa7767c5Sopenharmony_ci    }
83fa7767c5Sopenharmony_ci};
84fa7767c5Sopenharmony_ci} // namespace Media
85fa7767c5Sopenharmony_ci} // namespace OHOS
86fa7767c5Sopenharmony_ci#endif
87