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