1 /*
2  * Copyright (c) 2021-2022 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 #include "foundation/multimedia/player_framework/interfaces/inner_api/native/player.h"
17 #include "platform/common/rs_log.h"
18 
19 namespace OHOS::Ace {
20 namespace {
21 
22 constexpr int32_t MILLISECONDS_TO_SECONDS = 1000;
23 
24 } // namespace
25 
26 const std::map<OHOS::Media::PlayerStates, std::string> STATE_MAP = {
27     {OHOS::Media::PlayerStates::PLAYER_STATE_ERROR, "Error"},
28     {OHOS::Media::PlayerStates::PLAYER_IDLE, "Idle"},
29     {OHOS::Media::PlayerStates::PLAYER_INITIALIZED, "Initialized"},
30     {OHOS::Media::PlayerStates::PLAYER_PREPARED, "Prepared"},
31     {OHOS::Media::PlayerStates::PLAYER_STARTED, "Started"},
32     {OHOS::Media::PlayerStates::PLAYER_PAUSED, "Paused"},
33     {OHOS::Media::PlayerStates::PLAYER_STOPPED, "Stopped"},
34     {OHOS::Media::PlayerStates::PLAYER_PLAYBACK_COMPLETE, "Complete"},
35 };
36 
37 struct MediaCallback : public OHOS::Media::PlayerCallback {
38 
39 public:
40     MediaCallback() = default;
41     ~MediaCallback() = default;
42 
OnErrorMediaCallback43     void OnError(int32_t errorCode, const std::string &errorMsg) override
44     {
45         std::cout << "OnError callback, errorCode: "<< errorCode << "errorMsg: " << errorMsg.c_str() << std::endl;
46     }
47 
48     void OnInfo(OHOS::Media::PlayerOnInfoType type, int32_t extra, const OHOS::Media::Format &InfoBody = {}) override
49     {
50         switch (type) {
51             case OHOS::Media::INFO_TYPE_SEEKDONE:
52                 std::cout << "PlayerCallback: OnSeekDone currentPositon is " << extra << std::endl;
53                 break;
54             case OHOS::Media::INFO_TYPE_EOS:
55                 std::cout << "PlayerCallback: OnEndOfStream isLooping is " << extra << std::endl;
56                 break;
57             case OHOS::Media::INFO_TYPE_STATE_CHANGE:
58                 state_ = static_cast<OHOS::Media::PlayerStates>(extra);
59                 PrintState(state_);
60                 break;
61             case OHOS::Media::INFO_TYPE_POSITION_UPDATE:
62                 // std::cout << "OnPositionUpdated positon is " << extra << std::endl;
63                 break;
64             case OHOS::Media::INFO_TYPE_MESSAGE:
65                 std::cout << "PlayerCallback: OnMessage is " << extra << std::endl;
66                 break;
67             default:
68                 break;
69             }
70     }
71 
72 private:
PrintStateMediaCallback73     void PrintState(OHOS::Media::PlayerStates state) const
74     {
75         if (STATE_MAP.count(state) != 0) {
76             std::cout << "State:" << (STATE_MAP.at(state)).c_str() << std::endl;
77         } else {
78             std::cout << "Invalid state" << std::endl;
79         }
80     }
81     Media::PlayerStates state_ = Media::PLAYER_STATE_ERROR;
82 };
83 
84 } // namespace OHOS::Ace