1 /*
2 * Copyright (c) Huawei Technologies Co., Ltd. 2020-2020. All rights reserved.
3 */
4 /*
5 * Copyright (C) 2023 Huawei Device Co., Ltd.
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 */
18
19 #include "sample_callback.h"
20 #include "av_codec_sample_log.h"
21
22 namespace {
23 constexpr int LIMIT_LOGD_FREQUENCY = 50;
24 }
25
26 // 自定义写入数据函数
OnRenderWriteData(OH_AudioRenderer * renderer,void * userData,void * buffer,int32_t length)27 int32_t SampleCallback::OnRenderWriteData(OH_AudioRenderer *renderer, void *userData, void *buffer, int32_t length)
28 {
29 (void)renderer;
30 (void)length;
31 CodecUserData *codecUserData = static_cast<CodecUserData *>(userData);
32
33 // 将待播放的数据,按length长度写入buffer
34 uint8_t *dest = (uint8_t *)buffer;
35 size_t index = 0;
36 std::unique_lock<std::mutex> lock(codecUserData->outputMutex);
37 // 从队列中取出需要播放的长度为length的数据
38 while (!codecUserData->renderQueue.empty() && index < length) {
39 dest[index++] = codecUserData->renderQueue.front();
40 codecUserData->renderQueue.pop();
41 }
42 AVCODEC_SAMPLE_LOGD("render BufferLength:%{public}d Out buffer count: %{public}u, renderQueue.size: %{public}u "
43 "renderReadSize: %{public}u",
44 length, codecUserData->outputFrameCount, codecUserData->renderQueue.size(), index);
45 if (codecUserData->renderQueue.size() < length) {
46 codecUserData->renderCond.notify_all();
47 }
48 return 0;
49 }
50 // 自定义音频流事件函数
OnRenderStreamEvent(OH_AudioRenderer * renderer,void * userData,OH_AudioStream_Event event)51 int32_t SampleCallback::OnRenderStreamEvent(OH_AudioRenderer *renderer, void *userData, OH_AudioStream_Event event)
52 {
53 (void)renderer;
54 (void)userData;
55 (void)event;
56 // 根据event表示的音频流事件信息,更新播放器状态和界面
57 return 0;
58 }
59 // 自定义音频中断事件函数
OnRenderInterruptEvent(OH_AudioRenderer * renderer,void * userData,OH_AudioInterrupt_ForceType type,OH_AudioInterrupt_Hint hint)60 int32_t SampleCallback::OnRenderInterruptEvent(OH_AudioRenderer *renderer,
61 void *userData, OH_AudioInterrupt_ForceType type, OH_AudioInterrupt_Hint hint)
62 {
63 (void)renderer;
64 (void)userData;
65 (void)type;
66 (void)hint;
67 // 根据type和hint表示的音频中断信息,更新播放器状态和界面
68 return 0;
69 }
70 // 自定义异常回调函数
OnRenderError(OH_AudioRenderer * renderer,void * userData,OH_AudioStream_Result error)71 int32_t SampleCallback::OnRenderError(OH_AudioRenderer *renderer, void *userData, OH_AudioStream_Result error)
72 {
73 (void)renderer;
74 (void)userData;
75 (void)error;
76 AVCODEC_SAMPLE_LOGE("OnRenderError");
77 // 根据error表示的音频异常信息,做出相应的处理
78 return 0;
79 }
80
OnCodecError(OH_AVCodec * codec,int32_t errorCode,void * userData)81 void SampleCallback::OnCodecError(OH_AVCodec *codec, int32_t errorCode, void *userData)
82 {
83 (void)codec;
84 (void)errorCode;
85 (void)userData;
86 AVCODEC_SAMPLE_LOGE("On codec error, error code: %{public}d", errorCode);
87 }
88
OnCodecFormatChange(OH_AVCodec * codec,OH_AVFormat * format,void * userData)89 void SampleCallback::OnCodecFormatChange(OH_AVCodec *codec, OH_AVFormat *format, void *userData)
90 {
91 AVCODEC_SAMPLE_LOGI("On codec format change");
92 }
93
OnNeedInputBuffer(OH_AVCodec * codec,uint32_t index,OH_AVBuffer * buffer,void * userData)94 void SampleCallback::OnNeedInputBuffer(OH_AVCodec *codec, uint32_t index, OH_AVBuffer *buffer, void *userData)
95 {
96 if (userData == nullptr) {
97 return;
98 }
99 (void)codec;
100 CodecUserData *codecUserData = static_cast<CodecUserData *>(userData);
101 std::unique_lock<std::mutex> lock(codecUserData->inputMutex);
102 codecUserData->inputBufferInfoQueue.emplace(index, buffer);
103 codecUserData->inputCond.notify_all();
104 }
105
OnNewOutputBuffer(OH_AVCodec * codec,uint32_t index,OH_AVBuffer * buffer,void * userData)106 void SampleCallback::OnNewOutputBuffer(OH_AVCodec *codec, uint32_t index, OH_AVBuffer *buffer, void *userData)
107 {
108 if (userData == nullptr) {
109 return;
110 }
111 (void)codec;
112 CodecUserData *codecUserData = static_cast<CodecUserData *>(userData);
113 std::unique_lock<std::mutex> lock(codecUserData->outputMutex);
114 codecUserData->outputBufferInfoQueue.emplace(index, buffer);
115 codecUserData->outputCond.notify_all();
116 }
117