• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 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 #include "player_listener_proxy.h"
17 #include "media_log.h"
18 #include "media_errors.h"
19 #include "media_parcel.h"
20 #include "player_xcollie.h"
21 
22 namespace {
23 constexpr OHOS::HiviewDFX::HiLogLabel LABEL = {LOG_CORE, LOG_DOMAIN, "PlayerListenerProxy"};
24 }
25 
26 namespace OHOS {
27 namespace Media {
PlayerListenerProxy(const sptr<IRemoteObject> & impl)28 PlayerListenerProxy::PlayerListenerProxy(const sptr<IRemoteObject> &impl)
29     : IRemoteProxy<IStandardPlayerListener>(impl)
30 {
31     MEDIA_LOGD("0x%{public}06" PRIXPTR " Instances create", FAKE_POINTER(this));
32 }
33 
~PlayerListenerProxy()34 PlayerListenerProxy::~PlayerListenerProxy()
35 {
36     MEDIA_LOGD("0x%{public}06" PRIXPTR " Instances destroy", FAKE_POINTER(this));
37 }
38 
OnError(int32_t errorCode,const std::string & errorMsg)39 void PlayerListenerProxy::OnError(int32_t errorCode, const std::string &errorMsg)
40 {
41     MessageParcel data;
42     MessageParcel reply;
43     MessageOption option(MessageOption::TF_ASYNC);
44 
45     bool token = data.WriteInterfaceToken(PlayerListenerProxy::GetDescriptor());
46     CHECK_AND_RETURN_LOG(token, "Failed to write descriptor!");
47 
48     data.WriteInt32(errorCode);
49     data.WriteString(errorMsg);
50     int error = SendRequest(PlayerListenerMsg::ON_ERROR_MSG, data, reply, option);
51     CHECK_AND_RETURN_LOG(error == MSERR_OK, "on error failed, error: %{public}d", error);
52 }
53 
OnInfo(PlayerOnInfoType type,int32_t extra,const Format & infoBody)54 void PlayerListenerProxy::OnInfo(PlayerOnInfoType type, int32_t extra, const Format &infoBody)
55 {
56     if (type == INFO_TYPE_ERROR_MSG) {
57         int32_t errorCode = -1;
58         std::string errorMsg;
59         infoBody.GetIntValue(std::string(PlayerKeys::PLAYER_ERROR_TYPE), errorCode);
60         infoBody.GetStringValue(std::string(PlayerKeys::PLAYER_ERROR_MSG), errorMsg);
61         return OnError(errorCode, errorMsg);
62     }
63 
64     MessageParcel data;
65     MessageParcel reply;
66     MessageOption option(MessageOption::TF_ASYNC);
67 
68     bool token = data.WriteInterfaceToken(PlayerListenerProxy::GetDescriptor());
69     CHECK_AND_RETURN_LOG(token, "Failed to write descriptor!");
70 
71     data.WriteInt32(type);
72     data.WriteInt32(extra);
73     MediaParcel::Marshalling(data, infoBody);
74     int error = SendRequest(PlayerListenerMsg::ON_INFO, data, reply, option);
75     CHECK_AND_RETURN_LOG(error == MSERR_OK, "on info failed, error: %{public}d", error);
76 }
77 
PlayerListenerCallback(const sptr<IStandardPlayerListener> & listener)78 PlayerListenerCallback::PlayerListenerCallback(const sptr<IStandardPlayerListener> &listener) : listener_(listener)
79 {
80     MEDIA_LOGD("0x%{public}06" PRIXPTR " Instances create", FAKE_POINTER(this));
81 }
82 
~PlayerListenerCallback()83 PlayerListenerCallback::~PlayerListenerCallback()
84 {
85     MEDIA_LOGD("0x%{public}06" PRIXPTR " Instances destroy", FAKE_POINTER(this));
86 }
87 
OnError(int32_t errorCode,const std::string & errorMsg)88 void PlayerListenerCallback::OnError(int32_t errorCode, const std::string &errorMsg)
89 {
90     MEDIA_LOGE("player callback onError, errorCode: %{public}d, errorMsg: %{public}s", errorCode, errorMsg.c_str());
91     CHECK_AND_RETURN(listener_ != nullptr);
92     listener_->OnError(errorCode, errorMsg);
93 }
94 
OnInfo(PlayerOnInfoType type,int32_t extra,const Format & infoBody)95 void PlayerListenerCallback::OnInfo(PlayerOnInfoType type, int32_t extra, const Format &infoBody)
96 {
97     CHECK_AND_RETURN(listener_ != nullptr);
98     listener_->OnInfo(type, extra, infoBody);
99 }
100 
SendRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)101 int32_t PlayerListenerProxy::SendRequest(uint32_t code, MessageParcel &data,
102     MessageParcel &reply, MessageOption &option)
103 {
104     int32_t error = MSERR_OK;
105     LISTENER(error = Remote()->SendRequest(code, data, reply, option), "PlayerListenerProxy::SendRequest", false)
106     return error;
107 }
108 } // namespace Media
109 } // namespace OHOS
110