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_encoder_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, "AVCodecAudioEncoderImpl"};
25 }
26
27 namespace OHOS {
28 namespace MediaAVCodec {
CreateByMime(const std::string & mime)29 std::shared_ptr<AVCodecAudioEncoder> AudioEncoderFactory::CreateByMime(const std::string &mime)
30 {
31 AVCODEC_SYNC_TRACE;
32 std::shared_ptr<AVCodecAudioEncoderImpl> impl = std::make_shared<AVCodecAudioEncoderImpl>();
33
34 int32_t ret = impl->Init(AVCODEC_TYPE_AUDIO_ENCODER, true, mime);
35 CHECK_AND_RETURN_RET_LOG(ret == AVCS_ERR_OK, nullptr, "failed to init AVCodecAudioEncoderImpl");
36
37 return impl;
38 }
39
CreateByName(const std::string & name)40 std::shared_ptr<AVCodecAudioEncoder> AudioEncoderFactory::CreateByName(const std::string &name)
41 {
42 AVCODEC_SYNC_TRACE;
43 std::shared_ptr<AVCodecAudioEncoderImpl> impl = std::make_shared<AVCodecAudioEncoderImpl>();
44
45 int32_t ret = impl->Init(AVCODEC_TYPE_AUDIO_ENCODER, false, name);
46 CHECK_AND_RETURN_RET_LOG(ret == AVCS_ERR_OK, nullptr, "failed to init AVCodecAudioEncoderImpl");
47
48 return impl;
49 }
50
Init(AVCodecType type,bool isMimeType,const std::string & name)51 int32_t AVCodecAudioEncoderImpl::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
57 return codecService_->Init(type, isMimeType, name);
58 }
59
AVCodecAudioEncoderImpl()60 AVCodecAudioEncoderImpl::AVCodecAudioEncoderImpl()
61 {
62 AVCODEC_LOGD("AVCodecAudioEncoderImpl:0x%{public}06" PRIXPTR " Instances create", FAKE_POINTER(this));
63 }
64
~AVCodecAudioEncoderImpl()65 AVCodecAudioEncoderImpl::~AVCodecAudioEncoderImpl()
66 {
67 codecService_ = nullptr;
68 AVCODEC_LOGD("AVCodecAudioEncoderImpl:0x%{public}06" PRIXPTR " Instances destroy", FAKE_POINTER(this));
69 }
70
Configure(const Format & format)71 int32_t AVCodecAudioEncoderImpl::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 AVCodecAudioEncoderImpl::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 AVCodecAudioEncoderImpl::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 AVCodecAudioEncoderImpl::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 AVCodecAudioEncoderImpl::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 AVCodecAudioEncoderImpl::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 AVCodecAudioEncoderImpl::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 AVCodecAudioEncoderImpl::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 AVCodecAudioEncoderImpl::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 AVCodecAudioEncoderImpl::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 AVCodecAudioEncoderImpl::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 AVCodecAudioEncoderImpl::SetCallback(const std::shared_ptr<AVCodecCallback> &callback)
149 {
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