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_dfx.h"
21 #include "codec_server.h"
22
23 namespace {
24 constexpr OHOS::HiviewDFX::HiLogLabel LABEL = {LOG_CORE, LOG_DOMAIN, "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 codecService_ = CodecServer::Create();
55 CHECK_AND_RETURN_RET_LOG(codecService_ != nullptr, AVCS_ERR_UNKNOWN, "failed to create codec service");
56 return codecService_->Init(type, isMimeType, name);
57 }
58
AVCodecAudioDecoderImpl()59 AVCodecAudioDecoderImpl::AVCodecAudioDecoderImpl()
60 {
61 AVCODEC_LOGD("AVCodecAudioDecoderImpl:0x%{public}06" PRIXPTR " Instances create", FAKE_POINTER(this));
62 }
63
~AVCodecAudioDecoderImpl()64 AVCodecAudioDecoderImpl::~AVCodecAudioDecoderImpl()
65 {
66 codecService_ = nullptr;
67 AVCODEC_LOGD("AVCodecAudioDecoderImpl:0x%{public}06" PRIXPTR " Instances destroy", FAKE_POINTER(this));
68 }
69
Configure(const Format & format)70 int32_t AVCodecAudioDecoderImpl::Configure(const Format &format)
71 {
72 AVCODEC_SYNC_TRACE;
73 CHECK_AND_RETURN_RET_LOG(codecService_ != nullptr, AVCS_ERR_INVALID_OPERATION, "service died");
74 return codecService_->Configure(format);
75 }
76
Prepare()77 int32_t AVCodecAudioDecoderImpl::Prepare()
78 {
79 AVCODEC_SYNC_TRACE;
80 CHECK_AND_RETURN_RET_LOG(codecService_ != nullptr, AVCS_ERR_INVALID_OPERATION, "service died");
81 return AVCS_ERR_OK;
82 }
83
Start()84 int32_t AVCodecAudioDecoderImpl::Start()
85 {
86 AVCODEC_SYNC_TRACE;
87 CHECK_AND_RETURN_RET_LOG(codecService_ != nullptr, AVCS_ERR_INVALID_OPERATION, "service died");
88 return codecService_->Start();
89 }
90
Stop()91 int32_t AVCodecAudioDecoderImpl::Stop()
92 {
93 AVCODEC_SYNC_TRACE;
94 CHECK_AND_RETURN_RET_LOG(codecService_ != nullptr, AVCS_ERR_INVALID_OPERATION, "service died");
95 return codecService_->Stop();
96 }
97
Flush()98 int32_t AVCodecAudioDecoderImpl::Flush()
99 {
100 AVCODEC_SYNC_TRACE;
101 CHECK_AND_RETURN_RET_LOG(codecService_ != nullptr, AVCS_ERR_INVALID_OPERATION, "service died");
102 return codecService_->Flush();
103 }
104
Reset()105 int32_t AVCodecAudioDecoderImpl::Reset()
106 {
107 AVCODEC_SYNC_TRACE;
108 CHECK_AND_RETURN_RET_LOG(codecService_ != nullptr, AVCS_ERR_INVALID_OPERATION, "service died");
109 return codecService_->Reset();
110 }
111
Release()112 int32_t AVCodecAudioDecoderImpl::Release()
113 {
114 AVCODEC_SYNC_TRACE;
115 CHECK_AND_RETURN_RET_LOG(codecService_ != nullptr, AVCS_ERR_INVALID_OPERATION, "service died");
116 return codecService_->Release();
117 }
118
QueueInputBuffer(uint32_t index,AVCodecBufferInfo info,AVCodecBufferFlag flag)119 int32_t AVCodecAudioDecoderImpl::QueueInputBuffer(uint32_t index, AVCodecBufferInfo info, AVCodecBufferFlag flag)
120 {
121 AVCODEC_SYNC_TRACE;
122 CHECK_AND_RETURN_RET_LOG(codecService_ != nullptr, AVCS_ERR_INVALID_OPERATION, "service died");
123 return codecService_->QueueInputBuffer(index, info, flag);
124 }
125
GetOutputFormat(Format & format)126 int32_t AVCodecAudioDecoderImpl::GetOutputFormat(Format &format)
127 {
128 AVCODEC_SYNC_TRACE;
129 CHECK_AND_RETURN_RET_LOG(codecService_ != nullptr, AVCS_ERR_INVALID_OPERATION, "service died");
130 return codecService_->GetOutputFormat(format);
131 }
132
ReleaseOutputBuffer(uint32_t index)133 int32_t AVCodecAudioDecoderImpl::ReleaseOutputBuffer(uint32_t index)
134 {
135 AVCODEC_SYNC_TRACE;
136 CHECK_AND_RETURN_RET_LOG(codecService_ != nullptr, AVCS_ERR_INVALID_OPERATION, "service died");
137 return codecService_->ReleaseOutputBuffer(index);
138 }
139
SetParameter(const Format & format)140 int32_t AVCodecAudioDecoderImpl::SetParameter(const Format &format)
141 {
142 AVCODEC_SYNC_TRACE;
143 CHECK_AND_RETURN_RET_LOG(codecService_ != nullptr, AVCS_ERR_INVALID_OPERATION, "service died");
144 return codecService_->SetParameter(format);
145 }
146
SetCallback(const std::shared_ptr<AVCodecCallback> & callback)147 int32_t AVCodecAudioDecoderImpl::SetCallback(const std::shared_ptr<AVCodecCallback> &callback)
148 {
149 AVCODEC_SYNC_TRACE;
150 CHECK_AND_RETURN_RET_LOG(codecService_ != nullptr, AVCS_ERR_INVALID_OPERATION, "service died");
151 CHECK_AND_RETURN_RET_LOG(callback != nullptr, AVCS_ERR_INVALID_VAL, "callback is nullptr");
152 return codecService_->SetCallback(callback);
153 }
154 } // namespace MediaAVCodec
155 } // namespace OHOS
156