1 /*
2 * Copyright (C) 2023 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_audio_decoder_impl.h"
17 #include "i_avcodec_service.h"
18 #include "avcodec_log.h"
19 #include "avcodec_errors.h"
20 #include "avcodec_trace.h"
21 #include "codec_server.h"
22
23 namespace {
24 constexpr OHOS::HiviewDFX::HiLogLabel LABEL = {LOG_CORE, LOG_DOMAIN_AUDIO, "AVCodecAudioDecoderImpl"};
25 }
26
27 namespace OHOS {
28 namespace MediaAVCodec {
CreateByMime(const std::string & mime)29 std::shared_ptr<AVCodecAudioDecoder> AudioDecoderFactory::CreateByMime(const std::string &mime)
30 {
31 AVCODEC_SYNC_TRACE;
32 std::shared_ptr<AVCodecAudioDecoderImpl> impl = std::make_shared<AVCodecAudioDecoderImpl>();
33
34 int32_t ret = impl->Init(AVCODEC_TYPE_AUDIO_DECODER, true, mime);
35 CHECK_AND_RETURN_RET_LOG(ret == AVCS_ERR_OK, nullptr, "failed to init AVCodecAudioDecoderImpl");
36
37 return impl;
38 }
39
CreateByName(const std::string & name)40 std::shared_ptr<AVCodecAudioDecoder> AudioDecoderFactory::CreateByName(const std::string &name)
41 {
42 AVCODEC_SYNC_TRACE;
43 std::shared_ptr<AVCodecAudioDecoderImpl> impl = std::make_shared<AVCodecAudioDecoderImpl>();
44
45 int32_t ret = impl->Init(AVCODEC_TYPE_AUDIO_DECODER, false, name);
46 CHECK_AND_RETURN_RET_LOG(ret == AVCS_ERR_OK, nullptr, "failed to init AVCodecAudioDecoderImpl");
47
48 return impl;
49 }
50
Init(AVCodecType type,bool isMimeType,const std::string & name)51 int32_t AVCodecAudioDecoderImpl::Init(AVCodecType type, bool isMimeType, const std::string &name)
52 {
53 AVCODEC_SYNC_TRACE;
54 Format format;
55 codecService_ = CodecServer::Create();
56 CHECK_AND_RETURN_RET_LOG(codecService_ != nullptr, AVCS_ERR_UNKNOWN, "failed to create codec service");
57 return codecService_->Init(type, isMimeType, name, *format.GetMeta());
58 }
59
AVCodecAudioDecoderImpl()60 AVCodecAudioDecoderImpl::AVCodecAudioDecoderImpl()
61 {
62 AVCODEC_LOGD("AVCodecAudioDecoderImpl:0x%{public}06" PRIXPTR " Instances create", FAKE_POINTER(this));
63 }
64
~AVCodecAudioDecoderImpl()65 AVCodecAudioDecoderImpl::~AVCodecAudioDecoderImpl()
66 {
67 codecService_ = nullptr;
68 AVCODEC_LOGD("AVCodecAudioDecoderImpl:0x%{public}06" PRIXPTR " Instances destroy", FAKE_POINTER(this));
69 }
70
Configure(const Format & format)71 int32_t AVCodecAudioDecoderImpl::Configure(const Format &format)
72 {
73 AVCODEC_SYNC_TRACE;
74 CHECK_AND_RETURN_RET_LOG(codecService_ != nullptr, AVCS_ERR_INVALID_OPERATION, "service died");
75 return codecService_->Configure(format);
76 }
77
Prepare()78 int32_t AVCodecAudioDecoderImpl::Prepare()
79 {
80 AVCODEC_SYNC_TRACE;
81 CHECK_AND_RETURN_RET_LOG(codecService_ != nullptr, AVCS_ERR_INVALID_OPERATION, "service died");
82 return AVCS_ERR_OK;
83 }
84
Start()85 int32_t AVCodecAudioDecoderImpl::Start()
86 {
87 AVCODEC_SYNC_TRACE;
88 CHECK_AND_RETURN_RET_LOG(codecService_ != nullptr, AVCS_ERR_INVALID_OPERATION, "service died");
89 return codecService_->Start();
90 }
91
Stop()92 int32_t AVCodecAudioDecoderImpl::Stop()
93 {
94 AVCODEC_SYNC_TRACE;
95 CHECK_AND_RETURN_RET_LOG(codecService_ != nullptr, AVCS_ERR_INVALID_OPERATION, "service died");
96 return codecService_->Stop();
97 }
98
Flush()99 int32_t AVCodecAudioDecoderImpl::Flush()
100 {
101 AVCODEC_SYNC_TRACE;
102 CHECK_AND_RETURN_RET_LOG(codecService_ != nullptr, AVCS_ERR_INVALID_OPERATION, "service died");
103 return codecService_->Flush();
104 }
105
Reset()106 int32_t AVCodecAudioDecoderImpl::Reset()
107 {
108 AVCODEC_SYNC_TRACE;
109 CHECK_AND_RETURN_RET_LOG(codecService_ != nullptr, AVCS_ERR_INVALID_OPERATION, "service died");
110 return codecService_->Reset();
111 }
112
Release()113 int32_t AVCodecAudioDecoderImpl::Release()
114 {
115 AVCODEC_SYNC_TRACE;
116 CHECK_AND_RETURN_RET_LOG(codecService_ != nullptr, AVCS_ERR_INVALID_OPERATION, "service died");
117 return codecService_->Release();
118 }
119
QueueInputBuffer(uint32_t index,AVCodecBufferInfo info,AVCodecBufferFlag flag)120 int32_t AVCodecAudioDecoderImpl::QueueInputBuffer(uint32_t index, AVCodecBufferInfo info, AVCodecBufferFlag flag)
121 {
122 AVCODEC_SYNC_TRACE;
123 CHECK_AND_RETURN_RET_LOG(codecService_ != nullptr, AVCS_ERR_INVALID_OPERATION, "service died");
124 return codecService_->QueueInputBuffer(index, info, flag);
125 }
126
GetOutputFormat(Format & format)127 int32_t AVCodecAudioDecoderImpl::GetOutputFormat(Format &format)
128 {
129 AVCODEC_SYNC_TRACE;
130 CHECK_AND_RETURN_RET_LOG(codecService_ != nullptr, AVCS_ERR_INVALID_OPERATION, "service died");
131 return codecService_->GetOutputFormat(format);
132 }
133
ReleaseOutputBuffer(uint32_t index)134 int32_t AVCodecAudioDecoderImpl::ReleaseOutputBuffer(uint32_t index)
135 {
136 AVCODEC_SYNC_TRACE;
137 CHECK_AND_RETURN_RET_LOG(codecService_ != nullptr, AVCS_ERR_INVALID_OPERATION, "service died");
138 return codecService_->ReleaseOutputBuffer(index);
139 }
140
SetParameter(const Format & format)141 int32_t AVCodecAudioDecoderImpl::SetParameter(const Format &format)
142 {
143 AVCODEC_SYNC_TRACE;
144 CHECK_AND_RETURN_RET_LOG(codecService_ != nullptr, AVCS_ERR_INVALID_OPERATION, "service died");
145 return codecService_->SetParameter(format);
146 }
147
SetCallback(const std::shared_ptr<AVCodecCallback> & callback)148 int32_t AVCodecAudioDecoderImpl::SetCallback(const std::shared_ptr<AVCodecCallback> &callback)
149 {
150 AVCODEC_SYNC_TRACE;
151 CHECK_AND_RETURN_RET_LOG(codecService_ != nullptr, AVCS_ERR_INVALID_OPERATION, "service died");
152 CHECK_AND_RETURN_RET_LOG(callback != nullptr, AVCS_ERR_INVALID_VAL, "callback is nullptr");
153 return codecService_->SetCallback(callback);
154 }
155 } // namespace MediaAVCodec
156 } // namespace OHOS
157