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 "avcodec_listener_proxy.h"
17 #include "media_errors.h"
18 #include "media_log.h"
19 #include "media_parcel.h"
20
21 namespace {
22 constexpr OHOS::HiviewDFX::HiLogLabel LABEL = {LOG_CORE, LOG_DOMAIN, "AVCodecListenerProxy"};
23 }
24
25 namespace OHOS {
26 namespace Media {
AVCodecListenerProxy(const sptr<IRemoteObject> & impl)27 AVCodecListenerProxy::AVCodecListenerProxy(const sptr<IRemoteObject> &impl)
28 : IRemoteProxy<IStandardAVCodecListener>(impl)
29 {
30 MEDIA_LOGD("0x%{public}06" PRIXPTR " Instances create", FAKE_POINTER(this));
31 }
32
~AVCodecListenerProxy()33 AVCodecListenerProxy::~AVCodecListenerProxy()
34 {
35 MEDIA_LOGD("0x%{public}06" PRIXPTR " Instances destroy", FAKE_POINTER(this));
36 }
37
OnError(AVCodecErrorType errorType,int32_t errorCode)38 void AVCodecListenerProxy::OnError(AVCodecErrorType errorType, int32_t errorCode)
39 {
40 MessageParcel data;
41 MessageParcel reply;
42 MessageOption option(MessageOption::TF_ASYNC);
43 if (!data.WriteInterfaceToken(AVCodecListenerProxy::GetDescriptor())) {
44 MEDIA_LOGE("Failed to write descriptor");
45 return;
46 }
47 data.WriteInt32(static_cast<int32_t>(errorType));
48 data.WriteInt32(errorCode);
49 int error = Remote()->SendRequest(AVCodecListenerMsg::ON_ERROR, data, reply, option);
50 if (error != MSERR_OK) {
51 MEDIA_LOGE("OnError failed, error: %{public}d", error);
52 }
53 }
54
OnOutputFormatChanged(const Format & format)55 void AVCodecListenerProxy::OnOutputFormatChanged(const Format &format)
56 {
57 MessageParcel data;
58 MessageParcel reply;
59 MessageOption option(MessageOption::TF_ASYNC);
60 if (!data.WriteInterfaceToken(AVCodecListenerProxy::GetDescriptor())) {
61 MEDIA_LOGE("Failed to write descriptor");
62 return;
63 }
64 (void)MediaParcel::Marshalling(data, format);
65 int error = Remote()->SendRequest(AVCodecListenerMsg::ON_OUTPUT_FORMAT_CHANGED, data, reply, option);
66 if (error != MSERR_OK) {
67 MEDIA_LOGE("OnOutputFormatChanged failed, error: %{public}d", error);
68 }
69 }
70
OnInputBufferAvailable(uint32_t index)71 void AVCodecListenerProxy::OnInputBufferAvailable(uint32_t index)
72 {
73 MessageParcel data;
74 MessageParcel reply;
75 MessageOption option(MessageOption::TF_ASYNC);
76 if (!data.WriteInterfaceToken(AVCodecListenerProxy::GetDescriptor())) {
77 MEDIA_LOGE("Failed to write descriptor");
78 return;
79 }
80 data.WriteUint32(index);
81 int error = Remote()->SendRequest(AVCodecListenerMsg::ON_INPUT_BUFFER_AVAILABLE, data, reply, option);
82 if (error != MSERR_OK) {
83 MEDIA_LOGE("OnInputBufferAvailable failed, error: %{public}d", error);
84 }
85 }
86
OnOutputBufferAvailable(uint32_t index,AVCodecBufferInfo info,AVCodecBufferFlag flag)87 void AVCodecListenerProxy::OnOutputBufferAvailable(uint32_t index, AVCodecBufferInfo info, AVCodecBufferFlag flag)
88 {
89 MessageParcel data;
90 MessageParcel reply;
91 MessageOption option(MessageOption::TF_ASYNC);
92 if (!data.WriteInterfaceToken(AVCodecListenerProxy::GetDescriptor())) {
93 MEDIA_LOGE("Failed to write descriptor");
94 return;
95 }
96 data.WriteUint32(index);
97 data.WriteInt64(info.presentationTimeUs);
98 data.WriteInt32(info.size);
99 data.WriteInt32(info.offset);
100 data.WriteInt32(static_cast<int32_t>(flag));
101 int error = Remote()->SendRequest(AVCodecListenerMsg::ON_OUTPUT_BUFFER_AVAILABLE, data, reply, option);
102 if (error != MSERR_OK) {
103 MEDIA_LOGE("OnOutputBufferAvailable failed, error: %{public}d", error);
104 }
105 }
106
AVCodecListenerCallback(const sptr<IStandardAVCodecListener> & listener)107 AVCodecListenerCallback::AVCodecListenerCallback(const sptr<IStandardAVCodecListener> &listener)
108 : listener_(listener)
109 {
110 MEDIA_LOGD("0x%{public}06" PRIXPTR " Instances create", FAKE_POINTER(this));
111 }
112
~AVCodecListenerCallback()113 AVCodecListenerCallback::~AVCodecListenerCallback()
114 {
115 MEDIA_LOGD("0x%{public}06" PRIXPTR " Instances destroy", FAKE_POINTER(this));
116 }
117
OnError(AVCodecErrorType errorType,int32_t errorCode)118 void AVCodecListenerCallback::OnError(AVCodecErrorType errorType, int32_t errorCode)
119 {
120 if (listener_ != nullptr) {
121 listener_->OnError(errorType, errorCode);
122 }
123 }
124
OnOutputFormatChanged(const Format & format)125 void AVCodecListenerCallback::OnOutputFormatChanged(const Format &format)
126 {
127 if (listener_ != nullptr) {
128 listener_->OnOutputFormatChanged(format);
129 }
130 }
131
OnInputBufferAvailable(uint32_t index)132 void AVCodecListenerCallback::OnInputBufferAvailable(uint32_t index)
133 {
134 if (listener_ != nullptr) {
135 listener_->OnInputBufferAvailable(index);
136 }
137 }
138
OnOutputBufferAvailable(uint32_t index,AVCodecBufferInfo info,AVCodecBufferFlag flag)139 void AVCodecListenerCallback::OnOutputBufferAvailable(uint32_t index, AVCodecBufferInfo info, AVCodecBufferFlag flag)
140 {
141 if (listener_ != nullptr) {
142 listener_->OnOutputBufferAvailable(index, info, flag);
143 }
144 }
145 } // namespace Media
146 } // namespace OHOS
147