diff --git a/.gitignore b/.gitignore
new file mode 100644
index 0000000000000000000000000000000000000000..cf007af3df1352a594c13700cac7b4106c12efc0
--- /dev/null
+++ b/.gitignore
@@ -0,0 +1,152 @@
+## c/c++
+# Prerequisites
+*.d
+
+# Compiled Object files
+*.ko
+*.slo
+*.lo
+*.o
+*.obj
+*.elf
+
+# Linker output
+*.ilk
+*.map
+*.exp
+
+# Precompiled Headers
+*.gch
+*.pch
+
+# Compiled Dynamic libraries
+*.so
+*.so.*
+*.dylib
+*.dll
+
+# module files
+*.mod
+*.smod
+*.cmd
+.tmp_versions/
+modules.order
+Module.symvers
+Mkfile.old
+dkms.conf
+
+# Compiled Static libraries
+*.lai
+*.la
+*.a
+*.lib
+*.lo
+
+# Executables
+*.exe
+*.out
+*.app
+*.i*86
+*.x86_64
+*.hex
+
+# Debug files
+*.dSYM/
+*.su
+*.idb
+*.pdb
+
+## cmake
+CMakeLists.txt.user
+CMakeCache.txt
+CMakeFiles
+CMakeScripts
+Testing
+Makefile
+cmake_install.cmake
+install_manifest.txt
+compile_commands.json
+CTestTestfile.cmake
+_deps
+
+## ninja
+.ninja_deps
+.ninja_log
+
+## vscode
+.vscode
+*.code-workspace
+
+# Local History for Visual Studio Code
+.history/
+
+## jetbrains
+# User-specific stuff
+.idea/**/workspace.xml
+.idea/**/tasks.xml
+.idea/**/usage.statistics.xml
+.idea/**/dictionaries
+.idea/**/shelf
+
+# Generated files
+.idea/**/contentModel.xml
+
+# Sensitive or high-churn files
+.idea/**/dataSources/
+.idea/**/dataSources.ids
+.idea/**/dataSources.local.xml
+.idea/**/sqlDataSources.xml
+.idea/**/dynamic.xml
+.idea/**/uiDesigner.xml
+.idea/**/dbnavigator.xml
+
+# Gradle
+.idea/**/gradle.xml
+.idea/**/libraries
+
+# Gradle and Maven with auto-import
+# When using Gradle or Maven with auto-import, you should exclude module files,
+# since they will be recreated, and may cause churn. Uncomment if using
+# auto-import.
+# .idea/artifacts
+# .idea/compiler.xml
+# .idea/jarRepositories.xml
+# .idea/modules.xml
+# .idea/*.iml
+# .idea/modules
+# *.iml
+# *.ipr
+
+# CMake
+cmake-build-*/
+
+# Mongo Explorer plugin
+.idea/**/mongoSettings.xml
+
+# File-based project format
+*.iws
+
+# IntelliJ
+out/
+
+# mpeltonen/sbt-idea plugin
+.idea_modules/
+
+# JIRA plugin
+atlassian-ide-plugin.xml
+
+# Cursive Clojure plugin
+.idea/replstate.xml
+
+# Crashlytics plugin (for Android Studio and IntelliJ)
+com_crashlytics_export_strings.xml
+crashlytics.properties
+crashlytics-build.properties
+fabric.properties
+
+# Editor-based Rest Client
+.idea/httpRequests
+
+# Android studio 3.1+ serialized cache file
+.idea/caches/build_file_checksums.ser
+.clang-format
diff --git a/BUILD.gn b/BUILD.gn
new file mode 100755
index 0000000000000000000000000000000000000000..5e2e783b8de031832775c3488944df6dd7135505
--- /dev/null
+++ b/BUILD.gn
@@ -0,0 +1,22 @@
+# Copyright (C) 2023 Huawei Device Co., Ltd.
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+import("//build/config/ohos/config.gni")
+import("//build/ohos.gni")
+
+group("av_codec_packages") {
+ public_deps = [
+ "interfaces/inner_api/native:av_codec_client",
+ "services/services:av_codec_service",
+ ]
+}
diff --git a/LICENSE b/LICENSE
new file mode 100644
index 0000000000000000000000000000000000000000..261eeb9e9f8b2b4b0d119366dda99c6fd7d35c64
--- /dev/null
+++ b/LICENSE
@@ -0,0 +1,201 @@
+ Apache License
+ Version 2.0, January 2004
+ http://www.apache.org/licenses/
+
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
+
+ 1. Definitions.
+
+ "License" shall mean the terms and conditions for use, reproduction,
+ and distribution as defined by Sections 1 through 9 of this document.
+
+ "Licensor" shall mean the copyright owner or entity authorized by
+ the copyright owner that is granting the License.
+
+ "Legal Entity" shall mean the union of the acting entity and all
+ other entities that control, are controlled by, or are under common
+ control with that entity. For the purposes of this definition,
+ "control" means (i) the power, direct or indirect, to cause the
+ direction or management of such entity, whether by contract or
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
+ outstanding shares, or (iii) beneficial ownership of such entity.
+
+ "You" (or "Your") shall mean an individual or Legal Entity
+ exercising permissions granted by this License.
+
+ "Source" form shall mean the preferred form for making modifications,
+ including but not limited to software source code, documentation
+ source, and configuration files.
+
+ "Object" form shall mean any form resulting from mechanical
+ transformation or translation of a Source form, including but
+ not limited to compiled object code, generated documentation,
+ and conversions to other media types.
+
+ "Work" shall mean the work of authorship, whether in Source or
+ Object form, made available under the License, as indicated by a
+ copyright notice that is included in or attached to the work
+ (an example is provided in the Appendix below).
+
+ "Derivative Works" shall mean any work, whether in Source or Object
+ form, that is based on (or derived from) the Work and for which the
+ editorial revisions, annotations, elaborations, or other modifications
+ represent, as a whole, an original work of authorship. For the purposes
+ of this License, Derivative Works shall not include works that remain
+ separable from, or merely link (or bind by name) to the interfaces of,
+ the Work and Derivative Works thereof.
+
+ "Contribution" shall mean any work of authorship, including
+ the original version of the Work and any modifications or additions
+ to that Work or Derivative Works thereof, that is intentionally
+ submitted to Licensor for inclusion in the Work by the copyright owner
+ or by an individual or Legal Entity authorized to submit on behalf of
+ the copyright owner. For the purposes of this definition, "submitted"
+ means any form of electronic, verbal, or written communication sent
+ to the Licensor or its representatives, including but not limited to
+ communication on electronic mailing lists, source code control systems,
+ and issue tracking systems that are managed by, or on behalf of, the
+ Licensor for the purpose of discussing and improving the Work, but
+ excluding communication that is conspicuously marked or otherwise
+ designated in writing by the copyright owner as "Not a Contribution."
+
+ "Contributor" shall mean Licensor and any individual or Legal Entity
+ on behalf of whom a Contribution has been received by Licensor and
+ subsequently incorporated within the Work.
+
+ 2. Grant of Copyright License. Subject to the terms and conditions of
+ this License, each Contributor hereby grants to You a perpetual,
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
+ copyright license to reproduce, prepare Derivative Works of,
+ publicly display, publicly perform, sublicense, and distribute the
+ Work and such Derivative Works in Source or Object form.
+
+ 3. Grant of Patent License. Subject to the terms and conditions of
+ this License, each Contributor hereby grants to You a perpetual,
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
+ (except as stated in this section) patent license to make, have made,
+ use, offer to sell, sell, import, and otherwise transfer the Work,
+ where such license applies only to those patent claims licensable
+ by such Contributor that are necessarily infringed by their
+ Contribution(s) alone or by combination of their Contribution(s)
+ with the Work to which such Contribution(s) was submitted. If You
+ institute patent litigation against any entity (including a
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
+ or a Contribution incorporated within the Work constitutes direct
+ or contributory patent infringement, then any patent licenses
+ granted to You under this License for that Work shall terminate
+ as of the date such litigation is filed.
+
+ 4. Redistribution. You may reproduce and distribute copies of the
+ Work or Derivative Works thereof in any medium, with or without
+ modifications, and in Source or Object form, provided that You
+ meet the following conditions:
+
+ (a) You must give any other recipients of the Work or
+ Derivative Works a copy of this License; and
+
+ (b) You must cause any modified files to carry prominent notices
+ stating that You changed the files; and
+
+ (c) You must retain, in the Source form of any Derivative Works
+ that You distribute, all copyright, patent, trademark, and
+ attribution notices from the Source form of the Work,
+ excluding those notices that do not pertain to any part of
+ the Derivative Works; and
+
+ (d) If the Work includes a "NOTICE" text file as part of its
+ distribution, then any Derivative Works that You distribute must
+ include a readable copy of the attribution notices contained
+ within such NOTICE file, excluding those notices that do not
+ pertain to any part of the Derivative Works, in at least one
+ of the following places: within a NOTICE text file distributed
+ as part of the Derivative Works; within the Source form or
+ documentation, if provided along with the Derivative Works; or,
+ within a display generated by the Derivative Works, if and
+ wherever such third-party notices normally appear. The contents
+ of the NOTICE file are for informational purposes only and
+ do not modify the License. You may add Your own attribution
+ notices within Derivative Works that You distribute, alongside
+ or as an addendum to the NOTICE text from the Work, provided
+ that such additional attribution notices cannot be construed
+ as modifying the License.
+
+ You may add Your own copyright statement to Your modifications and
+ may provide additional or different license terms and conditions
+ for use, reproduction, or distribution of Your modifications, or
+ for any such Derivative Works as a whole, provided Your use,
+ reproduction, and distribution of the Work otherwise complies with
+ the conditions stated in this License.
+
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
+ any Contribution intentionally submitted for inclusion in the Work
+ by You to the Licensor shall be under the terms and conditions of
+ this License, without any additional terms or conditions.
+ Notwithstanding the above, nothing herein shall supersede or modify
+ the terms of any separate license agreement you may have executed
+ with Licensor regarding such Contributions.
+
+ 6. Trademarks. This License does not grant permission to use the trade
+ names, trademarks, service marks, or product names of the Licensor,
+ except as required for reasonable and customary use in describing the
+ origin of the Work and reproducing the content of the NOTICE file.
+
+ 7. Disclaimer of Warranty. Unless required by applicable law or
+ agreed to in writing, Licensor provides the Work (and each
+ Contributor provides its Contributions) on an "AS IS" BASIS,
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
+ implied, including, without limitation, any warranties or conditions
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
+ PARTICULAR PURPOSE. You are solely responsible for determining the
+ appropriateness of using or redistributing the Work and assume any
+ risks associated with Your exercise of permissions under this License.
+
+ 8. Limitation of Liability. In no event and under no legal theory,
+ whether in tort (including negligence), contract, or otherwise,
+ unless required by applicable law (such as deliberate and grossly
+ negligent acts) or agreed to in writing, shall any Contributor be
+ liable to You for damages, including any direct, indirect, special,
+ incidental, or consequential damages of any character arising as a
+ result of this License or out of the use or inability to use the
+ Work (including but not limited to damages for loss of goodwill,
+ work stoppage, computer failure or malfunction, or any and all
+ other commercial damages or losses), even if such Contributor
+ has been advised of the possibility of such damages.
+
+ 9. Accepting Warranty or Additional Liability. While redistributing
+ the Work or Derivative Works thereof, You may choose to offer,
+ and charge a fee for, acceptance of support, warranty, indemnity,
+ or other liability obligations and/or rights consistent with this
+ License. However, in accepting such obligations, You may act only
+ on Your own behalf and on Your sole responsibility, not on behalf
+ of any other Contributor, and only if You agree to indemnify,
+ defend, and hold each Contributor harmless for any liability
+ incurred by, or claims asserted against, such Contributor by reason
+ of your accepting any such warranty or additional liability.
+
+ END OF TERMS AND CONDITIONS
+
+ APPENDIX: How to apply the Apache License to your work.
+
+ To apply the Apache License to your work, attach the following
+ boilerplate notice, with the fields enclosed by brackets "[]"
+ replaced with your own identifying information. (Don't include
+ the brackets!) The text should be enclosed in the appropriate
+ comment syntax for the file format. We also recommend that a
+ file or class name and description of purpose be included on the
+ same "printed page" as the copyright notice for easier
+ identification within third-party archives.
+
+ Copyright [yyyy] [name of copyright owner]
+
+ Licensed under the Apache License, Version 2.0 (the "License");
+ you may not use this file except in compliance with the License.
+ You may obtain a copy of the License at
+
+ http://www.apache.org/licenses/LICENSE-2.0
+
+ Unless required by applicable law or agreed to in writing, software
+ distributed under the License is distributed on an "AS IS" BASIS,
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ See the License for the specific language governing permissions and
+ limitations under the License.
diff --git a/OAT.xml b/OAT.xml
new file mode 100644
index 0000000000000000000000000000000000000000..8acf17eec02762d69f84f852d0e772697505aaf5
--- /dev/null
+++ b/OAT.xml
@@ -0,0 +1,73 @@
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/README.md b/README.md
index e1086acb12d9843f2dd4aa51444fa9a9bef4ee24..d034c7583bcb826412c90694e3d78f5ef4d612d4 100644
--- a/README.md
+++ b/README.md
@@ -1,4 +1,4 @@
-# multimedia_av_codec
+# av_codec
#### Description
{**When you're done, you can delete the content in this README and update the file with details for others getting started with your repository**}
diff --git a/bundle.json b/bundle.json
new file mode 100644
index 0000000000000000000000000000000000000000..4eb11d490b180eebec5c573c855e825c998d3066
--- /dev/null
+++ b/bundle.json
@@ -0,0 +1,84 @@
+{
+ "name": "@ohos/av_codec",
+ "description": "Media standard provides atomic capabilities",
+ "version": "3.1",
+ "license": "Apache License 2.0",
+ "publishAs": "code-segment",
+ "segment": {
+ "destPath": "foundation/multimedia/av_codec"
+ },
+ "dirs": {},
+ "scripts": {},
+ "component": {
+ "name": "av_codec",
+ "subsystem": "multimedia",
+ "syscap": [
+ "SystemCapability.Multimedia.Media.Muxer",
+ "SystemCapability.Multimedia.Media.Spliter",
+ "SystemCapability.Multimedia.Media.Core",
+ "SystemCapability.Multimedia.Media.AudioDecoder",
+ "SystemCapability.Multimedia.Media.AudioEncoder",
+ "SystemCapability.Multimedia.Media.VideoDecoder",
+ "SystemCapability.Multimedia.Media.VideoEncoder",
+ "SystemCapability.Multimedia.Media.CodecBase"
+ ],
+ "features": [],
+ "adapted_system_type": [ "standard" ],
+ "rom": "10000KB",
+ "ram": "10000KB",
+ "hisysevent_config": [
+ "//foundation/multimedia/av_codec/hisysevent.yaml"
+ ],
+ "deps": {
+ "components": [
+ "hiviewdfx_hilog_native",
+ "ipc",
+ "hisysevent_native",
+ "c_utils",
+ "hilog_native",
+ "hitrace_native"
+ ],
+ "third_party": [
+ "glib",
+ "gstreamer",
+ "libffi",
+ "ffmpeg",
+ "common"
+ ]
+ },
+ "build": {
+ "group_type": {
+ "base_group": [],
+ "fwk_group": [
+ "//foundation/multimedia/av_codec/interfaces/kits/c:capi_packages",
+ "//foundation/multimedia/av_codec/interfaces/inner_api/native:av_codec_client"
+ ],
+ "service_group": [
+ "//foundation/multimedia/av_codec/services:av_codec_services_package",
+ "//foundation/multimedia/av_codec/sa_profile:av_codec_service_profile"
+ ]
+ },
+ "inner_kits": [
+ {
+ "type": "so",
+ "name": "//foundation/multimedia/av_codec/interfaces/inner_api/native:av_codec_client",
+ "header": {
+ "header_files": [
+ "avcodec_audio_decoder.h",
+ "avcodec_audio_encoder.h",
+ "avcodec_video_decoder.h",
+ "avcodec_video_encoder.h",
+ "avdemuxer.h",
+ "avmuxer.h"
+ ],
+ "header_base": "//foundation/multimedia/av_codec/interfaces/inner_api/native"
+ }
+ }
+ ],
+ "test": [
+ "//foundation/multimedia/av_codec/test:av_codec_demo_test",
+ "//foundation/multimedia/av_codec/test:av_codec_unit_test"
+ ]
+ }
+ }
+ }
diff --git a/cfi_blocklist.txt b/cfi_blocklist.txt
new file mode 100644
index 0000000000000000000000000000000000000000..3f96933517c0cd3e2553a169859058b0fa692e11
--- /dev/null
+++ b/cfi_blocklist.txt
@@ -0,0 +1,2 @@
+src:*services/engine/*
+src:*frameworks/native/capi/*
\ No newline at end of file
diff --git a/config.gni b/config.gni
new file mode 100644
index 0000000000000000000000000000000000000000..f8b9413fc67601585414155a706e6e9872fccae5
--- /dev/null
+++ b/config.gni
@@ -0,0 +1,66 @@
+# Copyright (C) 2023-2023 Huawei Device Co., Ltd.
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+declare_args() {
+ multimedia_av_codec_support_capi = true
+ multimedia_av_codec_support_codec = true
+ multimedia_av_codec_support_codeclist = true
+ multimedia_av_codec_support_demuxer = true
+ multimedia_av_codec_support_source = true
+ multimedia_av_codec_support_muxer = true
+ multimedia_av_codec_support_ffmpeg_demuxer = true
+ multimedia_av_codec_support_test = true
+}
+
+av_codec_root_dir = "//foundation/multimedia/av_codec"
+av_codec_defines = []
+
+if (multimedia_av_codec_support_capi) {
+ av_codec_defines += [ "SUPPORT_CAPI" ]
+} else {
+ av_codec_defines += [ "UNSUPPORT_CAPI" ]
+}
+
+if (multimedia_av_codec_support_codec) {
+ av_codec_defines += [ "SUPPORT_CODEC" ]
+} else {
+ av_codec_defines += [ "UNSUPPORT_CODEC" ]
+}
+
+if (multimedia_av_codec_support_codeclist) {
+ av_codec_defines += [ "SUPPORT_CODECLIST" ]
+} else {
+ av_codec_defines += [ "UNSUPPORT_CODECLIST" ]
+}
+
+if (multimedia_av_codec_support_demuxer) {
+ av_codec_defines += [ "SUPPORT_DEMUXER" ]
+} else {
+ av_codec_defines += [ "UNSUPPORT_DEMUXER" ]
+}
+
+if (multimedia_av_codec_support_muxer) {
+ av_codec_defines += [ "SUPPORT_MUXER" ]
+} else {
+ av_codec_defines += [ "UNSUPPORT_MUXER" ]
+}
+
+if (multimedia_av_codec_support_source) {
+ av_codec_defines += [ "SUPPORT_SOURCE" ]
+} else {
+ av_codec_defines += [ "UNSUPPORT_SOURCE" ]
+}
+
+if (multimedia_av_codec_support_ffmpeg_demuxer) {
+ av_codec_defines += [ "FFMPEG_DEMUXER_ENABLE" ]
+}
diff --git a/frameworks/native/avcodec/avcodec_audio_decoder_impl.cpp b/frameworks/native/avcodec/avcodec_audio_decoder_impl.cpp
new file mode 100644
index 0000000000000000000000000000000000000000..817efc533285fc65cc040014edc481f7d165abd8
--- /dev/null
+++ b/frameworks/native/avcodec/avcodec_audio_decoder_impl.cpp
@@ -0,0 +1,154 @@
+/*
+ * Copyright (C) 2021 Huawei Device Co., Ltd.
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include "avcodec_audio_decoder_impl.h"
+#include "i_avcodec_service.h"
+#include "avcodec_log.h"
+#include "avcodec_errors.h"
+
+namespace {
+ constexpr OHOS::HiviewDFX::HiLogLabel LABEL = {LOG_CORE, LOG_DOMAIN, "AVCodecAudioDecoderImpl"};
+}
+
+namespace OHOS {
+namespace Media {
+std::shared_ptr AudioDecoderFactory::CreateByMime(const std::string &mime)
+{
+ std::shared_ptr impl = std::make_shared();
+
+ int32_t ret = impl->Init(AVCODEC_TYPE_AUDIO_DECODER, true, mime);
+ CHECK_AND_RETURN_RET_LOG(ret == AVCS_ERR_OK, nullptr, "failed to init AVCodecAudioDecoderImpl");
+
+ return impl;
+}
+
+std::shared_ptr AudioDecoderFactory::CreateByName(const std::string &name)
+{
+ std::shared_ptr impl = std::make_shared();
+
+ int32_t ret = impl->Init(AVCODEC_TYPE_AUDIO_DECODER, false, name);
+ CHECK_AND_RETURN_RET_LOG(ret == AVCS_ERR_OK, nullptr, "failed to init AVCodecAudioDecoderImpl");
+
+ return impl;
+}
+
+int32_t AVCodecAudioDecoderImpl::Init(AVCodecType type, bool isMimeType, const std::string &name)
+{
+ codecService_ = AVCodecServiceFactory::GetInstance().CreateCodecService();
+ CHECK_AND_RETURN_RET_LOG(codecService_ != nullptr, AVCS_ERR_UNKNOWN, "failed to create codec service");
+
+ return codecService_->Init(type, isMimeType, name);
+}
+
+AVCodecAudioDecoderImpl::AVCodecAudioDecoderImpl()
+{
+ AVCODEC_LOGD("AVCodecAudioDecoderImpl:0x%{public}06" PRIXPTR " Instances create", FAKE_POINTER(this));
+}
+
+AVCodecAudioDecoderImpl::~AVCodecAudioDecoderImpl()
+{
+ if (codecService_ != nullptr) {
+ (void)AVCodecServiceFactory::GetInstance().DestroyCodecService(codecService_);
+ codecService_ = nullptr;
+ }
+ AVCODEC_LOGD("AVCodecAudioDecoderImpl:0x%{public}06" PRIXPTR " Instances destroy", FAKE_POINTER(this));
+}
+
+int32_t AVCodecAudioDecoderImpl::Configure(const Format &format)
+{
+ CHECK_AND_RETURN_RET_LOG(codecService_ != nullptr, AVCS_ERR_INVALID_OPERATION, "service died");
+ return codecService_->Configure(format);
+}
+
+int32_t AVCodecAudioDecoderImpl::Prepare()
+{
+ CHECK_AND_RETURN_RET_LOG(codecService_ != nullptr, AVCS_ERR_INVALID_OPERATION, "service died");
+ return AVCS_ERR_OK;
+}
+
+int32_t AVCodecAudioDecoderImpl::Start()
+{
+ CHECK_AND_RETURN_RET_LOG(codecService_ != nullptr, AVCS_ERR_INVALID_OPERATION, "service died");
+ return codecService_->Start();
+}
+
+int32_t AVCodecAudioDecoderImpl::Stop()
+{
+ CHECK_AND_RETURN_RET_LOG(codecService_ != nullptr, AVCS_ERR_INVALID_OPERATION, "service died");
+ return codecService_->Stop();
+}
+
+int32_t AVCodecAudioDecoderImpl::Flush()
+{
+ CHECK_AND_RETURN_RET_LOG(codecService_ != nullptr, AVCS_ERR_INVALID_OPERATION, "service died");
+ return codecService_->Flush();
+}
+
+int32_t AVCodecAudioDecoderImpl::Reset()
+{
+ CHECK_AND_RETURN_RET_LOG(codecService_ != nullptr, AVCS_ERR_INVALID_OPERATION, "service died");
+ return codecService_->Reset();
+}
+
+int32_t AVCodecAudioDecoderImpl::Release()
+{
+ CHECK_AND_RETURN_RET_LOG(codecService_ != nullptr, AVCS_ERR_INVALID_OPERATION, "service died");
+ return codecService_->Release();
+}
+
+std::shared_ptr AVCodecAudioDecoderImpl::GetInputBuffer(uint32_t index)
+{
+ CHECK_AND_RETURN_RET_LOG(codecService_ != nullptr, nullptr, "service died");
+ return codecService_->GetInputBuffer(index);
+}
+
+int32_t AVCodecAudioDecoderImpl::QueueInputBuffer(uint32_t index, AVCodecBufferInfo info, AVCodecBufferFlag flag)
+{
+ CHECK_AND_RETURN_RET_LOG(codecService_ != nullptr, AVCS_ERR_INVALID_OPERATION, "service died");
+ return codecService_->QueueInputBuffer(index, info, flag);
+}
+
+std::shared_ptr AVCodecAudioDecoderImpl::GetOutputBuffer(uint32_t index)
+{
+ CHECK_AND_RETURN_RET_LOG(codecService_ != nullptr, nullptr, "service died");
+ return codecService_->GetOutputBuffer(index);
+}
+
+int32_t AVCodecAudioDecoderImpl::GetOutputFormat(Format &format)
+{
+ CHECK_AND_RETURN_RET_LOG(codecService_ != nullptr, AVCS_ERR_INVALID_OPERATION, "service died");
+ return codecService_->GetOutputFormat(format);
+}
+
+int32_t AVCodecAudioDecoderImpl::ReleaseOutputBuffer(uint32_t index)
+{
+ CHECK_AND_RETURN_RET_LOG(codecService_ != nullptr, AVCS_ERR_INVALID_OPERATION, "service died");
+ return codecService_->ReleaseOutputBuffer(index);
+}
+
+int32_t AVCodecAudioDecoderImpl::SetParameter(const Format &format)
+{
+ CHECK_AND_RETURN_RET_LOG(codecService_ != nullptr, AVCS_ERR_INVALID_OPERATION, "service died");
+ return codecService_->SetParameter(format);
+}
+
+int32_t AVCodecAudioDecoderImpl::SetCallback(const std::shared_ptr &callback)
+{
+ CHECK_AND_RETURN_RET_LOG(codecService_ != nullptr, AVCS_ERR_INVALID_OPERATION, "service died");
+ CHECK_AND_RETURN_RET_LOG(callback != nullptr, AVCS_ERR_INVALID_VAL, "callback is nullptr");
+ return codecService_->SetCallback(callback);
+}
+} // nmamespace Media
+} // namespace OHOS
diff --git a/frameworks/native/avcodec/avcodec_audio_decoder_impl.h b/frameworks/native/avcodec/avcodec_audio_decoder_impl.h
new file mode 100644
index 0000000000000000000000000000000000000000..6c7fba69e0b4b6c3750a585a22ce1e8b6eaec190
--- /dev/null
+++ b/frameworks/native/avcodec/avcodec_audio_decoder_impl.h
@@ -0,0 +1,50 @@
+/*
+ * Copyright (C) 2021 Huawei Device Co., Ltd.
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+#ifndef AVCODEC_AUDIO_DECODER_IMPL_H
+#define AVCODEC_AUDIO_DECODER_IMPL_H
+
+#include "avcodec_audio_decoder.h"
+#include "nocopyable.h"
+#include "i_avcodec_service.h"
+
+namespace OHOS {
+namespace Media {
+class AVCodecAudioDecoderImpl : public AVCodecAudioDecoder, public NoCopyable {
+public:
+ AVCodecAudioDecoderImpl();
+ ~AVCodecAudioDecoderImpl();
+
+ int32_t Configure(const Format &format) override;
+ int32_t Prepare() override;
+ int32_t Start() override;
+ int32_t Stop() override;
+ int32_t Flush() override;
+ int32_t Reset() override;
+ int32_t Release() override;
+ std::shared_ptr GetInputBuffer(uint32_t index) override;
+ int32_t QueueInputBuffer(uint32_t index, AVCodecBufferInfo info, AVCodecBufferFlag flag) override;
+ std::shared_ptr GetOutputBuffer(uint32_t index) override;
+ int32_t GetOutputFormat(Format &format) override;
+ int32_t ReleaseOutputBuffer(uint32_t index) override;
+ int32_t SetParameter(const Format &format) override;
+ int32_t SetCallback(const std::shared_ptr &callback) override;
+ int32_t Init(AVCodecType type, bool isMimeType, const std::string &name);
+
+private:
+ std::shared_ptr codecService_ = nullptr;
+};
+} // namespace Media
+} // namespace OHOS
+#endif // AVCODEC_AUDIO_DECODER_IMPL_H
\ No newline at end of file
diff --git a/frameworks/native/avcodec/avcodec_audio_encoder_impl.cpp b/frameworks/native/avcodec/avcodec_audio_encoder_impl.cpp
new file mode 100644
index 0000000000000000000000000000000000000000..14dde42aab897f14217e1246c8e48c540a28e343
--- /dev/null
+++ b/frameworks/native/avcodec/avcodec_audio_encoder_impl.cpp
@@ -0,0 +1,154 @@
+/*
+ * Copyright (C) 2021 Huawei Device Co., Ltd.
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include "avcodec_audio_encoder_impl.h"
+#include "i_avcodec_service.h"
+#include "avcodec_log.h"
+#include "avcodec_errors.h"
+
+namespace {
+ constexpr OHOS::HiviewDFX::HiLogLabel LABEL = {LOG_CORE, LOG_DOMAIN, "AVCodecAudioEncoderImpl"};
+}
+
+namespace OHOS {
+namespace Media {
+std::shared_ptr AudioEncoderFactory::CreateByMime(const std::string &mime)
+{
+ std::shared_ptr impl = std::make_shared();
+
+ int32_t ret = impl->Init(AVCODEC_TYPE_AUDIO_ENCODER, true, mime);
+ CHECK_AND_RETURN_RET_LOG(ret == AVCS_ERR_OK, nullptr, "failed to init AVCodecAudioEncoderImpl");
+
+ return impl;
+}
+
+std::shared_ptr AudioEncoderFactory::CreateByName(const std::string &name)
+{
+ std::shared_ptr impl = std::make_shared();
+
+ int32_t ret = impl->Init(AVCODEC_TYPE_AUDIO_ENCODER, false, name);
+ CHECK_AND_RETURN_RET_LOG(ret == AVCS_ERR_OK, nullptr, "failed to init AVCodecAudioEncoderImpl");
+
+ return impl;
+}
+
+int32_t AVCodecAudioEncoderImpl::Init(AVCodecType type, bool isMimeType, const std::string &name)
+{
+ codecService_ = AVCodecServiceFactory::GetInstance().CreateCodecService();
+ CHECK_AND_RETURN_RET_LOG(codecService_ != nullptr, AVCS_ERR_UNKNOWN, "failed to create codec service");
+
+ return codecService_->Init(type, isMimeType, name);
+}
+
+AVCodecAudioEncoderImpl::AVCodecAudioEncoderImpl()
+{
+ AVCODEC_LOGD("AVCodecAudioEncoderImpl:0x%{public}06" PRIXPTR " Instances create", FAKE_POINTER(this));
+}
+
+AVCodecAudioEncoderImpl::~AVCodecAudioEncoderImpl()
+{
+ if (codecService_ != nullptr) {
+ (void)AVCodecServiceFactory::GetInstance().DestroyCodecService(codecService_);
+ codecService_ = nullptr;
+ }
+ AVCODEC_LOGD("AVCodecAudioEncoderImpl:0x%{public}06" PRIXPTR " Instances destroy", FAKE_POINTER(this));
+}
+
+int32_t AVCodecAudioEncoderImpl::Configure(const Format &format)
+{
+ CHECK_AND_RETURN_RET_LOG(codecService_ != nullptr, AVCS_ERR_INVALID_OPERATION, "service died");
+ return codecService_->Configure(format);
+}
+
+int32_t AVCodecAudioEncoderImpl::Prepare()
+{
+ CHECK_AND_RETURN_RET_LOG(codecService_ != nullptr, AVCS_ERR_INVALID_OPERATION, "service died");
+ return AVCS_ERR_OK;
+}
+
+int32_t AVCodecAudioEncoderImpl::Start()
+{
+ CHECK_AND_RETURN_RET_LOG(codecService_ != nullptr, AVCS_ERR_INVALID_OPERATION, "service died");
+ return codecService_->Start();
+}
+
+int32_t AVCodecAudioEncoderImpl::Stop()
+{
+ CHECK_AND_RETURN_RET_LOG(codecService_ != nullptr, AVCS_ERR_INVALID_OPERATION, "service died");
+ return codecService_->Stop();
+}
+
+int32_t AVCodecAudioEncoderImpl::Flush()
+{
+ CHECK_AND_RETURN_RET_LOG(codecService_ != nullptr, AVCS_ERR_INVALID_OPERATION, "service died");
+ return codecService_->Flush();
+}
+
+int32_t AVCodecAudioEncoderImpl::Reset()
+{
+ CHECK_AND_RETURN_RET_LOG(codecService_ != nullptr, AVCS_ERR_INVALID_OPERATION, "service died");
+ return codecService_->Reset();
+}
+
+int32_t AVCodecAudioEncoderImpl::Release()
+{
+ CHECK_AND_RETURN_RET_LOG(codecService_ != nullptr, AVCS_ERR_INVALID_OPERATION, "service died");
+ return codecService_->Release();
+}
+
+std::shared_ptr AVCodecAudioEncoderImpl::GetInputBuffer(uint32_t index)
+{
+ CHECK_AND_RETURN_RET_LOG(codecService_ != nullptr, nullptr, "service died");
+ return codecService_->GetInputBuffer(index);
+}
+
+int32_t AVCodecAudioEncoderImpl::QueueInputBuffer(uint32_t index, AVCodecBufferInfo info, AVCodecBufferFlag flag)
+{
+ CHECK_AND_RETURN_RET_LOG(codecService_ != nullptr, AVCS_ERR_INVALID_OPERATION, "service died");
+ return codecService_->QueueInputBuffer(index, info, flag);
+}
+
+std::shared_ptr AVCodecAudioEncoderImpl::GetOutputBuffer(uint32_t index)
+{
+ CHECK_AND_RETURN_RET_LOG(codecService_ != nullptr, nullptr, "service died");
+ return codecService_->GetOutputBuffer(index);
+}
+
+int32_t AVCodecAudioEncoderImpl::GetOutputFormat(Format &format)
+{
+ CHECK_AND_RETURN_RET_LOG(codecService_ != nullptr, AVCS_ERR_INVALID_OPERATION, "service died");
+ return codecService_->GetOutputFormat(format);
+}
+
+int32_t AVCodecAudioEncoderImpl::ReleaseOutputBuffer(uint32_t index)
+{
+ CHECK_AND_RETURN_RET_LOG(codecService_ != nullptr, AVCS_ERR_INVALID_OPERATION, "service died");
+ return codecService_->ReleaseOutputBuffer(index);
+}
+
+int32_t AVCodecAudioEncoderImpl::SetParameter(const Format &format)
+{
+ CHECK_AND_RETURN_RET_LOG(codecService_ != nullptr, AVCS_ERR_INVALID_OPERATION, "service died");
+ return codecService_->SetParameter(format);
+}
+
+int32_t AVCodecAudioEncoderImpl::SetCallback(const std::shared_ptr &callback)
+{
+ CHECK_AND_RETURN_RET_LOG(codecService_ != nullptr, AVCS_ERR_INVALID_OPERATION, "service died");
+ CHECK_AND_RETURN_RET_LOG(callback != nullptr, AVCS_ERR_INVALID_VAL, "callback is nullptr");
+ return codecService_->SetCallback(callback);
+}
+} // namespace Media
+} // namespace OHOS
diff --git a/frameworks/native/avcodec/avcodec_audio_encoder_impl.h b/frameworks/native/avcodec/avcodec_audio_encoder_impl.h
new file mode 100644
index 0000000000000000000000000000000000000000..647767050eeb546363cd9db822702cdb2289ffd3
--- /dev/null
+++ b/frameworks/native/avcodec/avcodec_audio_encoder_impl.h
@@ -0,0 +1,51 @@
+/*
+ * Copyright (C) 2021 Huawei Device Co., Ltd.
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#ifndef AVCODEC_AUDIO_ENCODER_IMPL_H
+#define AVCODEC_AUDIO_ENCODER_IMPL_H
+
+#include "avcodec_audio_encoder.h"
+#include "nocopyable.h"
+#include "i_avcodec_service.h"
+
+namespace OHOS {
+namespace Media {
+class AVCodecAudioEncoderImpl : public AVCodecAudioEncoder, public NoCopyable {
+public:
+ AVCodecAudioEncoderImpl();
+ ~AVCodecAudioEncoderImpl();
+
+ int32_t Configure(const Format &format) override;
+ int32_t Prepare() override;
+ int32_t Start() override;
+ int32_t Stop() override;
+ int32_t Flush() override;
+ int32_t Reset() override;
+ int32_t Release() override;
+ std::shared_ptr GetInputBuffer(uint32_t index) override;
+ int32_t QueueInputBuffer(uint32_t index, AVCodecBufferInfo info, AVCodecBufferFlag flag) override;
+ std::shared_ptr GetOutputBuffer(uint32_t index) override;
+ int32_t GetOutputFormat(Format &format) override;
+ int32_t ReleaseOutputBuffer(uint32_t index) override;
+ int32_t SetParameter(const Format &format) override;
+ int32_t SetCallback(const std::shared_ptr &callback) override;
+ int32_t Init(AVCodecType type, bool isMimeType, const std::string &name);
+
+private:
+ std::shared_ptr codecService_ = nullptr;
+};
+} // namespace Media
+} // namespace OHOS
+#endif // AVCODEC_AUDIO_ENCODER_IMPL_H
\ No newline at end of file
diff --git a/frameworks/native/avcodec/avcodec_video_decoder_impl.cpp b/frameworks/native/avcodec/avcodec_video_decoder_impl.cpp
new file mode 100644
index 0000000000000000000000000000000000000000..631b6bc3ffcde26eb18d248733909a2d762b3355
--- /dev/null
+++ b/frameworks/native/avcodec/avcodec_video_decoder_impl.cpp
@@ -0,0 +1,211 @@
+/*
+ * Copyright (C) 2021 Huawei Device Co., Ltd.
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include "avcodec_video_decoder_impl.h"
+#include "i_avcodec_service.h"
+#include "avcodec_log.h"
+#include "avcodec_errors.h"
+#include "avcodec_dfx.h"
+
+namespace {
+ constexpr OHOS::HiviewDFX::HiLogLabel LABEL = {LOG_CORE, LOG_DOMAIN, "AVCodecVideoDecoderImpl"};
+}
+
+namespace OHOS {
+namespace Media {
+std::shared_ptr VideoDecoderFactory::CreateByMime(const std::string &mime)
+{
+ AVCODEC_SYNC_TRACE;
+
+ std::shared_ptr impl = std::make_shared();
+
+ int32_t ret = impl->Init(AVCODEC_TYPE_VIDEO_DECODER, true, mime);
+ CHECK_AND_RETURN_RET_LOG(ret == AVCS_ERR_OK,
+ nullptr, "AVCodec video decoder impl init failed");
+
+ return impl;
+}
+
+std::shared_ptr VideoDecoderFactory::CreateByName(const std::string &name)
+{
+ AVCODEC_SYNC_TRACE;
+
+ std::shared_ptr impl = std::make_shared();
+
+ int32_t ret = impl->Init(AVCODEC_TYPE_VIDEO_DECODER, false, name);
+ CHECK_AND_RETURN_RET_LOG(ret == AVCS_ERR_OK,
+ nullptr, "AVCodec video decoder impl init failed");
+
+ return impl;
+}
+
+int32_t AVCodecVideoDecoderImpl::Init(AVCodecType type, bool isMimeType, const std::string &name)
+{
+ AVCODEC_SYNC_TRACE;
+ codecService_ = AVCodecServiceFactory::GetInstance().CreateCodecService();
+ CHECK_AND_RETURN_RET_LOG(codecService_ != nullptr,
+ AVCS_ERR_INVALID_OPERATION, "Codec service create failed");
+
+ return codecService_->Init(type, isMimeType, name);
+}
+
+AVCodecVideoDecoderImpl::AVCodecVideoDecoderImpl()
+{
+ AVCODEC_LOGD("AVCodecVideoDecoderImpl:0x%{public}06" PRIXPTR " Instances create", FAKE_POINTER(this));
+}
+
+AVCodecVideoDecoderImpl::~AVCodecVideoDecoderImpl()
+{
+ if (codecService_ != nullptr) {
+ (void)AVCodecServiceFactory::GetInstance().DestroyCodecService(codecService_);
+ codecService_ = nullptr;
+ }
+ AVCODEC_LOGD("AVCodecVideoDecoderImpl:0x%{public}06" PRIXPTR " Instances destroy", FAKE_POINTER(this));
+}
+
+int32_t AVCodecVideoDecoderImpl::Configure(const Format &format)
+{
+ CHECK_AND_RETURN_RET_LOG(codecService_ != nullptr,
+ AVCS_ERR_INVALID_OPERATION, "Codec service is nullptr");
+
+ AVCODEC_SYNC_TRACE;
+ return codecService_->Configure(format);
+}
+
+int32_t AVCodecVideoDecoderImpl::Prepare()
+{
+ return AVCS_ERR_OK;
+}
+
+int32_t AVCodecVideoDecoderImpl::Start()
+{
+ CHECK_AND_RETURN_RET_LOG(codecService_ != nullptr,
+ AVCS_ERR_INVALID_OPERATION, "Codec service is nullptr");
+
+ AVCODEC_SYNC_TRACE;
+ return codecService_->Start();
+}
+
+int32_t AVCodecVideoDecoderImpl::Stop()
+{
+ CHECK_AND_RETURN_RET_LOG(codecService_ != nullptr,
+ AVCS_ERR_INVALID_OPERATION, "Codec service is nullptr");
+
+ AVCODEC_SYNC_TRACE;
+ return codecService_->Stop();
+}
+
+int32_t AVCodecVideoDecoderImpl::Flush()
+{
+ CHECK_AND_RETURN_RET_LOG(codecService_ != nullptr,
+ AVCS_ERR_INVALID_OPERATION, "Codec service is nullptr");
+
+ AVCODEC_SYNC_TRACE;
+ return codecService_->Flush();
+}
+
+int32_t AVCodecVideoDecoderImpl::Reset()
+{
+ CHECK_AND_RETURN_RET_LOG(codecService_ != nullptr,
+ AVCS_ERR_INVALID_OPERATION, "Codec service is nullptr");
+
+ AVCODEC_SYNC_TRACE;
+ return codecService_->Reset();
+}
+
+int32_t AVCodecVideoDecoderImpl::Release()
+{
+ CHECK_AND_RETURN_RET_LOG(codecService_ != nullptr,
+ AVCS_ERR_INVALID_OPERATION, "Codec service is nullptr");
+
+ AVCODEC_SYNC_TRACE;
+ return codecService_->Release();
+}
+
+int32_t AVCodecVideoDecoderImpl::SetOutputSurface(sptr surface)
+{
+ CHECK_AND_RETURN_RET_LOG(codecService_ != nullptr,
+ AVCS_ERR_INVALID_OPERATION, "Codec service is nullptr");
+
+ AVCODEC_SYNC_TRACE;
+ return codecService_->SetOutputSurface(surface);
+}
+
+std::shared_ptr AVCodecVideoDecoderImpl::GetInputBuffer(uint32_t index)
+{
+ CHECK_AND_RETURN_RET_LOG(codecService_ != nullptr,
+ nullptr, "Codec service is nullptr");
+
+ AVCODEC_SYNC_TRACE;
+ return codecService_->GetInputBuffer(index);
+}
+
+int32_t AVCodecVideoDecoderImpl::QueueInputBuffer(uint32_t index, AVCodecBufferInfo info, AVCodecBufferFlag flag)
+{
+ CHECK_AND_RETURN_RET_LOG(codecService_ != nullptr,
+ AVCS_ERR_INVALID_OPERATION, "Codec service is nullptr");
+
+ AVCODEC_SYNC_TRACE;
+ return codecService_->QueueInputBuffer(index, info, flag);
+}
+
+std::shared_ptr AVCodecVideoDecoderImpl::GetOutputBuffer(uint32_t index)
+{
+ CHECK_AND_RETURN_RET_LOG(codecService_ != nullptr,
+ nullptr, "Codec service is nullptr");
+
+ AVCODEC_SYNC_TRACE;
+ return codecService_->GetOutputBuffer(index);
+}
+
+int32_t AVCodecVideoDecoderImpl::GetOutputFormat(Format &format)
+{
+ CHECK_AND_RETURN_RET_LOG(codecService_ != nullptr,
+ AVCS_ERR_INVALID_OPERATION, "Codec service is nullptr");
+
+ AVCODEC_SYNC_TRACE;
+ return codecService_->GetOutputFormat(format);
+}
+
+int32_t AVCodecVideoDecoderImpl::ReleaseOutputBuffer(uint32_t index, bool render)
+{
+ CHECK_AND_RETURN_RET_LOG(codecService_ != nullptr,
+ AVCS_ERR_INVALID_OPERATION, "Codec service is nullptr");
+
+ AVCODEC_SYNC_TRACE;
+ return codecService_->ReleaseOutputBuffer(index, render);
+}
+
+int32_t AVCodecVideoDecoderImpl::SetParameter(const Format &format)
+{
+ CHECK_AND_RETURN_RET_LOG(codecService_ != nullptr,
+ AVCS_ERR_INVALID_OPERATION, "Codec service is nullptr");
+
+ AVCODEC_SYNC_TRACE;
+ return codecService_->SetParameter(format);
+}
+
+int32_t AVCodecVideoDecoderImpl::SetCallback(const std::shared_ptr &callback)
+{
+ CHECK_AND_RETURN_RET_LOG(codecService_ != nullptr,
+ AVCS_ERR_INVALID_OPERATION, "Codec service is nullptr");
+ CHECK_AND_RETURN_RET_LOG(callback != nullptr,
+ AVCS_ERR_INVALID_VAL, "Callback is nullptr");
+
+ AVCODEC_SYNC_TRACE;
+ return codecService_->SetCallback(callback);
+}
+} // namespace Media
+} // namespace OHOS
diff --git a/frameworks/native/avcodec/avcodec_video_decoder_impl.h b/frameworks/native/avcodec/avcodec_video_decoder_impl.h
new file mode 100644
index 0000000000000000000000000000000000000000..9cb3541e636cf700e996e5f1afd0112493e6a264
--- /dev/null
+++ b/frameworks/native/avcodec/avcodec_video_decoder_impl.h
@@ -0,0 +1,51 @@
+/*
+ * Copyright (C) 2021 Huawei Device Co., Ltd.
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+#ifndef AVCODEC_VIDEO_DECODER_IMPL_H
+#define AVCODEC_VIDEO_DECODER_IMPL_H
+
+#include "avcodec_video_decoder.h"
+#include "nocopyable.h"
+#include "i_avcodec_service.h"
+
+namespace OHOS {
+namespace Media {
+class AVCodecVideoDecoderImpl : public AVCodecVideoDecoder, public NoCopyable {
+public:
+ AVCodecVideoDecoderImpl();
+ ~AVCodecVideoDecoderImpl();
+
+ int32_t Configure(const Format &format) override;
+ int32_t Prepare() override;
+ int32_t Start() override;
+ int32_t Stop() override;
+ int32_t Flush() override;
+ int32_t Reset() override;
+ int32_t Release() override;
+ int32_t SetOutputSurface(sptr surface) override;
+ std::shared_ptr GetInputBuffer(uint32_t index) override;
+ int32_t QueueInputBuffer(uint32_t index, AVCodecBufferInfo info, AVCodecBufferFlag flag) override;
+ std::shared_ptr GetOutputBuffer(uint32_t index) override;
+ int32_t GetOutputFormat(Format &format) override;
+ int32_t ReleaseOutputBuffer(uint32_t index, bool render) override;
+ int32_t SetParameter(const Format &format) override;
+ int32_t SetCallback(const std::shared_ptr &callback) override;
+ int32_t Init(AVCodecType type, bool isMimeType, const std::string &name);
+
+private:
+ std::shared_ptr codecService_ = nullptr;
+};
+} // namespace Media
+} // namespace OHOS
+#endif // AVCODEC_VIDEO_DECODER_IMPL_H
\ No newline at end of file
diff --git a/frameworks/native/avcodec/avcodec_video_encoder_impl.cpp b/frameworks/native/avcodec/avcodec_video_encoder_impl.cpp
new file mode 100644
index 0000000000000000000000000000000000000000..526be80ff2e517448c82dbc851403835d2326a30
--- /dev/null
+++ b/frameworks/native/avcodec/avcodec_video_encoder_impl.cpp
@@ -0,0 +1,225 @@
+/*
+ * Copyright (C) 2021 Huawei Device Co., Ltd.
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include "avcodec_video_encoder_impl.h"
+#include "i_avcodec_service.h"
+#include "avcodec_log.h"
+#include "avcodec_errors.h"
+#include "avcodec_dfx.h"
+
+namespace {
+ constexpr OHOS::HiviewDFX::HiLogLabel LABEL = {LOG_CORE, LOG_DOMAIN, "AVCodecVideoEncoderImpl"};
+}
+
+namespace OHOS {
+namespace Media {
+std::shared_ptr VideoEncoderFactory::CreateByMime(const std::string &mime)
+{
+ AVCODEC_SYNC_TRACE;
+
+ std::shared_ptr impl = std::make_shared();
+
+ int32_t ret = impl->Init(AVCODEC_TYPE_VIDEO_ENCODER, true, mime);
+ CHECK_AND_RETURN_RET_LOG(ret == AVCS_ERR_OK, nullptr,
+ "AVCodec video encoder impl init failed");
+
+ return impl;
+}
+
+std::shared_ptr VideoEncoderFactory::CreateByName(const std::string &name)
+{
+ AVCODEC_SYNC_TRACE;
+
+ std::shared_ptr impl = std::make_shared();
+
+ int32_t ret = impl->Init(AVCODEC_TYPE_VIDEO_ENCODER, false, name);
+ CHECK_AND_RETURN_RET_LOG(ret == AVCS_ERR_OK, nullptr,
+ "AVCodec video encoder impl init failed");
+
+ return impl;
+}
+
+int32_t AVCodecVideoEncoderImpl::Init(AVCodecType type, bool isMimeType, const std::string &name)
+{
+ AVCODEC_SYNC_TRACE;
+ codecService_ = AVCodecServiceFactory::GetInstance().CreateCodecService();
+ CHECK_AND_RETURN_RET_LOG(codecService_ != nullptr,
+ AVCS_ERR_UNKNOWN, "Codec service create failed");
+
+ return codecService_->Init(type, isMimeType, name);
+}
+
+AVCodecVideoEncoderImpl::AVCodecVideoEncoderImpl()
+{
+ AVCODEC_LOGD("AVCodecVideoEncoderImpl:0x%{public}06" PRIXPTR " Instances create", FAKE_POINTER(this));
+}
+
+AVCodecVideoEncoderImpl::~AVCodecVideoEncoderImpl()
+{
+ if (codecService_ != nullptr) {
+ (void)AVCodecServiceFactory::GetInstance().DestroyCodecService(codecService_);
+ codecService_ = nullptr;
+ }
+ AVCODEC_LOGD("AVCodecVideoEncoderImpl:0x%{public}06" PRIXPTR " Instances destroy", FAKE_POINTER(this));
+}
+
+int32_t AVCodecVideoEncoderImpl::Configure(const Format &format)
+{
+ CHECK_AND_RETURN_RET_LOG(codecService_ != nullptr,
+ AVCS_ERR_INVALID_OPERATION, "Codec service is nullptr");
+
+ AVCODEC_SYNC_TRACE;
+ return codecService_->Configure(format);
+}
+
+int32_t AVCodecVideoEncoderImpl::Prepare()
+{
+ CHECK_AND_RETURN_RET_LOG(codecService_ != nullptr,
+ AVCS_ERR_INVALID_OPERATION, "Codec service is nullptr");
+
+ AVCODEC_SYNC_TRACE;
+ return AVCS_ERR_OK;
+}
+
+int32_t AVCodecVideoEncoderImpl::Start()
+{
+ CHECK_AND_RETURN_RET_LOG(codecService_ != nullptr,
+ AVCS_ERR_INVALID_OPERATION, "Codec service is nullptr");
+
+ AVCODEC_SYNC_TRACE;
+ return codecService_->Start();
+}
+
+int32_t AVCodecVideoEncoderImpl::Stop()
+{
+ CHECK_AND_RETURN_RET_LOG(codecService_ != nullptr,
+ AVCS_ERR_INVALID_OPERATION, "Codec service is nullptr");
+
+ AVCODEC_SYNC_TRACE;
+ return codecService_->Stop();
+}
+
+int32_t AVCodecVideoEncoderImpl::Flush()
+{
+ CHECK_AND_RETURN_RET_LOG(codecService_ != nullptr,
+ AVCS_ERR_INVALID_OPERATION, "Codec service is nullptr");
+
+ AVCODEC_SYNC_TRACE;
+ return codecService_->Flush();
+}
+
+int32_t AVCodecVideoEncoderImpl::NotifyEos()
+{
+ CHECK_AND_RETURN_RET_LOG(codecService_ != nullptr,
+ AVCS_ERR_INVALID_OPERATION, "Codec service is nullptr");
+
+ AVCODEC_SYNC_TRACE;
+ return codecService_->NotifyEos();
+}
+
+int32_t AVCodecVideoEncoderImpl::Reset()
+{
+ CHECK_AND_RETURN_RET_LOG(codecService_ != nullptr,
+ AVCS_ERR_INVALID_OPERATION, "Codec service is nullptr");
+
+ AVCODEC_SYNC_TRACE;
+ return codecService_->Reset();
+}
+
+int32_t AVCodecVideoEncoderImpl::Release()
+{
+ CHECK_AND_RETURN_RET_LOG(codecService_ != nullptr,
+ AVCS_ERR_INVALID_OPERATION, "Codec service is nullptr");
+
+ AVCODEC_SYNC_TRACE;
+ return codecService_->Release();
+}
+
+sptr AVCodecVideoEncoderImpl::CreateInputSurface()
+{
+ CHECK_AND_RETURN_RET_LOG(codecService_ != nullptr,
+ nullptr, "Codec service is nullptr");
+
+ AVCODEC_SYNC_TRACE;
+ surface_ = codecService_->CreateInputSurface();
+ return surface_;
+}
+
+std::shared_ptr AVCodecVideoEncoderImpl::GetInputBuffer(uint32_t index)
+{
+ CHECK_AND_RETURN_RET_LOG(codecService_ != nullptr,
+ nullptr, "Codec service is nullptr");
+
+ AVCODEC_SYNC_TRACE;
+ return codecService_->GetInputBuffer(index);
+}
+
+int32_t AVCodecVideoEncoderImpl::QueueInputBuffer(uint32_t index, AVCodecBufferInfo info, AVCodecBufferFlag flag)
+{
+ CHECK_AND_RETURN_RET_LOG(codecService_ != nullptr,
+ AVCS_ERR_INVALID_OPERATION, "Codec service is nullptr");
+
+ AVCODEC_SYNC_TRACE;
+ return codecService_->QueueInputBuffer(index, info, flag);
+}
+
+std::shared_ptr AVCodecVideoEncoderImpl::GetOutputBuffer(uint32_t index)
+{
+ CHECK_AND_RETURN_RET_LOG(codecService_ != nullptr,
+ nullptr, "Codec service is nullptr");
+
+ AVCODEC_SYNC_TRACE;
+ return codecService_->GetOutputBuffer(index);
+}
+
+int32_t AVCodecVideoEncoderImpl::GetOutputFormat(Format &format)
+{
+ CHECK_AND_RETURN_RET_LOG(codecService_ != nullptr,
+ AVCS_ERR_INVALID_OPERATION, "Codec service is nullptr");
+
+ AVCODEC_SYNC_TRACE;
+ return codecService_->GetOutputFormat(format);
+}
+
+int32_t AVCodecVideoEncoderImpl::ReleaseOutputBuffer(uint32_t index)
+{
+ CHECK_AND_RETURN_RET_LOG(codecService_ != nullptr,
+ AVCS_ERR_INVALID_OPERATION, "Codec service is nullptr");
+
+ AVCODEC_SYNC_TRACE;
+ return codecService_->ReleaseOutputBuffer(index);
+}
+
+int32_t AVCodecVideoEncoderImpl::SetParameter(const Format &format)
+{
+ CHECK_AND_RETURN_RET_LOG(codecService_ != nullptr,
+ AVCS_ERR_INVALID_OPERATION, "Codec service is nullptr");
+
+ AVCODEC_SYNC_TRACE;
+ return codecService_->SetParameter(format);
+}
+
+int32_t AVCodecVideoEncoderImpl::SetCallback(const std::shared_ptr &callback)
+{
+ CHECK_AND_RETURN_RET_LOG(codecService_ != nullptr,
+ AVCS_ERR_INVALID_OPERATION, "Codec service is nullptr");
+ CHECK_AND_RETURN_RET_LOG(callback != nullptr,
+ AVCS_ERR_INVALID_VAL, "Callback is nullptr");
+
+ AVCODEC_SYNC_TRACE;
+ return codecService_->SetCallback(callback);
+}
+} // namespace Media
+} // namespace OHOS
diff --git a/frameworks/native/avcodec/avcodec_video_encoder_impl.h b/frameworks/native/avcodec/avcodec_video_encoder_impl.h
new file mode 100644
index 0000000000000000000000000000000000000000..bbc36d0a0f73e4139f2d2c6858b936752609d0db
--- /dev/null
+++ b/frameworks/native/avcodec/avcodec_video_encoder_impl.h
@@ -0,0 +1,53 @@
+/*
+ * Copyright (C) 2021 Huawei Device Co., Ltd.
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+#ifndef AVCODEC_VIDEO_ENCODER_IMPL_H
+#define AVCODEC_VIDEO_ENCODER_IMPL_H
+
+#include "avcodec_video_encoder.h"
+#include "nocopyable.h"
+#include "i_avcodec_service.h"
+
+namespace OHOS {
+namespace Media {
+class AVCodecVideoEncoderImpl : public AVCodecVideoEncoder, public NoCopyable {
+public:
+ AVCodecVideoEncoderImpl();
+ ~AVCodecVideoEncoderImpl();
+
+ int32_t Configure(const Format &format) override;
+ int32_t Prepare() override;
+ int32_t Start() override;
+ int32_t Stop() override;
+ int32_t Flush() override;
+ int32_t Reset() override;
+ int32_t Release() override;
+ int32_t NotifyEos() override;
+ sptr CreateInputSurface() override;
+ std::shared_ptr GetInputBuffer(uint32_t index) override;
+ int32_t QueueInputBuffer(uint32_t index, AVCodecBufferInfo info, AVCodecBufferFlag flag) override;
+ std::shared_ptr GetOutputBuffer(uint32_t index) override;
+ int32_t GetOutputFormat(Format &format) override;
+ int32_t ReleaseOutputBuffer(uint32_t index) override;
+ int32_t SetParameter(const Format &format) override;
+ int32_t SetCallback(const std::shared_ptr &callback) override;
+ int32_t Init(AVCodecType type, bool isMimeType, const std::string &name);
+
+private:
+ std::shared_ptr codecService_ = nullptr;
+ sptr surface_ = nullptr;
+};
+} // namespace Media
+} // namespace OHOS
+#endif // AVCODEC_VIDEO_ENCODER_IMPL_H
\ No newline at end of file
diff --git a/frameworks/native/avcodeclist/avcodeclist_impl.cpp b/frameworks/native/avcodeclist/avcodeclist_impl.cpp
new file mode 100644
index 0000000000000000000000000000000000000000..5735787740257e20fa4f13957d0790bc5b9f685f
--- /dev/null
+++ b/frameworks/native/avcodeclist/avcodeclist_impl.cpp
@@ -0,0 +1,72 @@
+/*
+ * Copyright (C) 2023 Huawei Device Co., Ltd.
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include "avcodec_log.h"
+#include "avcodec_errors.h"
+#include "i_avcodec_service.h"
+#include "avcodeclist_impl.h"
+
+namespace {
+constexpr OHOS::HiviewDFX::HiLogLabel LABEL = {LOG_CORE, LOG_DOMAIN, "AVCodecListImpl"};
+}
+namespace OHOS {
+namespace Media {
+std::shared_ptr AVCodecListFactory::CreateAVCodecList()
+{
+ std::shared_ptr impl = std::make_shared();
+
+ int32_t ret = impl->Init();
+ CHECK_AND_RETURN_RET_LOG(ret == AVCS_ERR_OK, nullptr, "failed to init AVCodecListImpl");
+
+ return impl;
+}
+
+int32_t AVCodecListImpl::Init()
+{
+ codecListService_ = AVCodecServiceFactory::GetInstance().CreateCodecListService();
+ CHECK_AND_RETURN_RET_LOG(codecListService_ != nullptr, AVCS_ERR_UNKNOWN, "failed to create AVCodecList service");
+ return AVCS_ERR_OK;
+}
+
+AVCodecListImpl::AVCodecListImpl()
+{
+ AVCODEC_LOGD("AVCodecListImpl:0x%{public}06" PRIXPTR " Instances create", FAKE_POINTER(this));
+}
+
+AVCodecListImpl::~AVCodecListImpl()
+{
+ if (codecListService_ != nullptr) {
+ (void)AVCodecServiceFactory::GetInstance().DestroyCodecListService(codecListService_);
+ codecListService_ = nullptr;
+ }
+ AVCODEC_LOGD("AVCodecListImpl:0x%{public}06" PRIXPTR " Instances destroy", FAKE_POINTER(this));
+}
+
+std::string AVCodecListImpl::FindDecoder(const Format &format)
+{
+ return codecListService_->FindDecoder(format);
+}
+
+std::string AVCodecListImpl::FindEncoder(const Format &format)
+{
+ return codecListService_->FindEncoder(format);
+}
+
+CapabilityData AVCodecListImpl::CreateCapability(std::string codecName)
+{
+ return codecListService_->CreateCapability(codecName);
+}
+} // namespace Media
+} // namespace OHOS
\ No newline at end of file
diff --git a/frameworks/native/avcodeclist/avcodeclist_impl.h b/frameworks/native/avcodeclist/avcodeclist_impl.h
new file mode 100644
index 0000000000000000000000000000000000000000..0bd09314e695364b44cae0e3b2c2fd865d2bf518
--- /dev/null
+++ b/frameworks/native/avcodeclist/avcodeclist_impl.h
@@ -0,0 +1,41 @@
+/*
+ * Copyright (C) 2023 Huawei Device Co., Ltd.
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+#ifndef AVCODEC_LIST_IMPL_H
+#define AVCODEC_LIST_IMPL_H
+
+#include "avcodec_info.h"
+#include "avcodec_list.h"
+#include "nocopyable.h"
+#include "i_codeclist_service.h"
+
+namespace OHOS {
+namespace Media {
+class AVCodecListImpl : public AVCodecList, public NoCopyable {
+public:
+ AVCodecListImpl();
+ ~AVCodecListImpl();
+ int32_t Init();
+ // AVCodecList
+ std::string FindDecoder(const Format &format) override;
+ std::string FindEncoder(const Format &format) override;
+ CapabilityData CreateCapability(const std::string codecName) override;
+
+private:
+ std::shared_ptr codecListService_ = nullptr;
+};
+} // namespace Media
+} // namespace OHOS
+#endif // AVCODEC_LIST_IMPL_H
+
diff --git a/frameworks/native/avdemuxer/avdemuxer_impl.cpp b/frameworks/native/avdemuxer/avdemuxer_impl.cpp
new file mode 100644
index 0000000000000000000000000000000000000000..8daba6e51a3fc5ba8d4c70dfd0623222d81a6421
--- /dev/null
+++ b/frameworks/native/avdemuxer/avdemuxer_impl.cpp
@@ -0,0 +1,156 @@
+/*
+ * Copyright (C) 2023 Huawei Device Co., Ltd.
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include "avdemuxer_impl.h"
+#include
+#include
+#include
+#include
+#include "securec.h"
+#include "avcodec_log.h"
+#include "avsharedmemorybase.h"
+#include "avcodec_dfx.h"
+#include "i_avcodec_service.h"
+#include "avcodec_errors.h"
+
+static int LOOP_LOG_MAX_COUNT = 1000;
+
+namespace {
+ constexpr OHOS::HiviewDFX::HiLogLabel LABEL = {LOG_CORE, LOG_DOMAIN, "AVDemuxerImpl"};
+}
+
+namespace OHOS {
+namespace Media {
+std::shared_ptr AVDemuxerFactory::CreateWithSource(AVSource &source)
+{
+ AVCodecTrace trace("AVDemuxerFactory::CreateWithSource");
+
+ AVCODEC_LOGI("create demuxerImpl from source %{public}s", source.sourceUri.c_str());
+
+ std::shared_ptr demuxerImpl = std::make_shared();
+ CHECK_AND_RETURN_RET_LOG(demuxerImpl != nullptr, nullptr, "New AVDemuxerImpl failed when create demuxer");
+
+ int32_t ret = demuxerImpl->Init(source);
+ CHECK_AND_RETURN_RET_LOG(ret == AVCS_ERR_OK, nullptr, "Init AVDemuxerImpl failed when create demuxer");
+
+ return demuxerImpl;
+}
+
+int32_t AVDemuxerImpl::Init(AVSource &source)
+{
+ AVCodecTrace trace("AVDemuxer::Init");
+
+ demuxerClient_ = AVCodecServiceFactory::GetInstance().CreateDemuxerService();
+ CHECK_AND_RETURN_RET_LOG(demuxerClient_ != nullptr,
+ AVCS_ERR_CREATE_DEMUXER_SUB_SERVICE_FAILED, "Create demuxer service failed when init demuxerImpl");
+
+ uintptr_t sourceAddr = source.GetSourceAddr();
+
+ sourceUri_ = source.sourceUri;
+ CHECK_AND_RETURN_RET_LOG(demuxerClient_ != nullptr, AVCS_ERR_INVALID_OPERATION,
+ "demuxer service died when load add sourceTrack!");
+
+ return demuxerClient_->Init(sourceAddr);
+}
+
+AVDemuxerImpl::AVDemuxerImpl()
+{
+ AVCODEC_LOGI("init demuxerImpl");
+ AVCODEC_LOGD("AVDemuxerImpl:0x%{public}06" PRIXPTR " Instances create", FAKE_POINTER(this));
+}
+
+AVDemuxerImpl::~AVDemuxerImpl()
+{
+ AVCODEC_LOGI("uninit demuxerImpl for source %{public}s", sourceUri_.c_str());
+ if (demuxerClient_ != nullptr) {
+ (void)AVCodecServiceFactory::GetInstance().DestroyDemuxerService(demuxerClient_);
+ demuxerClient_ = nullptr;
+ }
+ AVCODEC_LOGD("AVDemuxerImpl:0x%{public}06" PRIXPTR " Instances destroy", FAKE_POINTER(this));
+}
+
+int32_t AVDemuxerImpl::SelectSourceTrackByID(uint32_t trackIndex)
+{
+ AVCodecTrace trace("AVDemuxer::SelectSourceTrackByID");
+
+ AVCODEC_LOGI("select source track: trackIndex=%{public}d", trackIndex);
+
+ CHECK_AND_RETURN_RET_LOG(demuxerClient_ != nullptr, AVCS_ERR_INVALID_OPERATION,
+ "demuxer service died when load add sourceTrack!");
+ return demuxerClient_->SelectSourceTrackByID(trackIndex);
+}
+
+int32_t AVDemuxerImpl::UnselectSourceTrackByID(uint32_t trackIndex)
+{
+ AVCodecTrace trace("AVDemuxer::UnselectSourceTrackByID");
+
+ AVCODEC_LOGI("unselect source track: trackIndex=%{public}d", trackIndex);
+
+ CHECK_AND_RETURN_RET_LOG(demuxerClient_ != nullptr, AVCS_ERR_INVALID_OPERATION,
+ "demuxer service died when remove sourceTrack!");
+ return demuxerClient_->UnselectSourceTrackByID(trackIndex);
+}
+
+int32_t AVDemuxerImpl::CopyNextSample(uint32_t &trackIndex, uint8_t *buffer,
+ AVCodecBufferInfo &bufferInfo, AVCodecBufferFlag &flag)
+{
+ AVCodecTrace trace("AVDemuxer::CopyNextSample");
+
+ AVCODEC_LOGI("CopyNextSample");
+
+ CHECK_AND_RETURN_RET_LOG(demuxerClient_ != nullptr, AVCS_ERR_INVALID_OPERATION,
+ "demuxer service died when copy sample!");
+
+ CHECK_AND_RETURN_RET_LOG(buffer != nullptr, AVCS_ERR_INVALID_VAL,
+ "Copy sample failed because input buffer is nullptr!");
+
+ if (trackLogCount < LOOP_LOG_MAX_COUNT) {
+ if (trackLogCount==0) {
+ AVCodecTrace::TraceBegin(std::string(__FUNCTION__), FAKE_POINTER(this));
+ }
+ trackLogCount++;
+ }
+
+ std::shared_ptr memory =
+ std::make_shared(bufferInfo.size, AVSharedMemory::FLAGS_READ_WRITE, "sampleBuffer");
+ int32_t ret = memory->Init();
+ CHECK_AND_RETURN_RET_LOG(ret == AVCS_ERR_OK, AVCS_ERR_NO_MEMORY, "Copy sample failed by demuxerService!");
+
+ ret = demuxerClient_->CopyNextSample(trackIndex, memory->GetBase(), bufferInfo, flag);
+ CHECK_AND_RETURN_RET_LOG(ret == AVCS_ERR_OK, AVCS_ERR_INVALID_OPERATION, "Copy sample failed by demuxerService!");
+
+ errno_t rc = memcpy_s(buffer, memory->GetSize(), memory->GetBase(), memory->GetSize());
+ CHECK_AND_RETURN_RET_LOG(rc == EOK, AVCS_ERR_UNKNOWN, "memcpy_s failed");
+
+ if (trackLogCount == LOOP_LOG_MAX_COUNT) {
+ AVCodecTrace::TraceEnd(std::string(__FUNCTION__), FAKE_POINTER(this));
+ }
+ return AVCS_ERR_OK;
+}
+
+int32_t AVDemuxerImpl::SeekToTime(int64_t mSeconds, AVSeekMode mode)
+{
+ AVCodecTrace trace("AVDemuxer::SeekToTime");
+
+ AVCODEC_LOGI("seek to time: mSeconds=%{public}" PRId64 "; mode=%{public}d", mSeconds, mode);
+
+ CHECK_AND_RETURN_RET_LOG(demuxerClient_ != nullptr, AVCS_ERR_INVALID_OPERATION, "demuxer service died when seek!");
+
+ CHECK_AND_RETURN_RET_LOG(mSeconds >= 0, AVCS_ERR_INVALID_VAL, "Seek failed because input mSeconds is negative!");
+
+ return demuxerClient_->SeekToTime(mSeconds, mode);
+}
+} // namespace Media
+} // namespace OHOS
\ No newline at end of file
diff --git a/frameworks/native/avdemuxer/avdemuxer_impl.h b/frameworks/native/avdemuxer/avdemuxer_impl.h
new file mode 100644
index 0000000000000000000000000000000000000000..a9d6f3d91cf1ac233cc964e9a1fc86853e368b4d
--- /dev/null
+++ b/frameworks/native/avdemuxer/avdemuxer_impl.h
@@ -0,0 +1,46 @@
+/*
+ * Copyright (C) 2023 Huawei Device Co., Ltd.
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#ifndef AVDEMUXER_IMPL_H
+#define AVDEMUXER_IMPL_H
+
+#include
+#include "avdemuxer.h"
+#include "i_demuxer_service.h"
+#include "nocopyable.h"
+#include "avsource_impl.h"
+
+namespace OHOS {
+namespace Media {
+class AVDemuxerImpl : public AVDemuxer, public NoCopyable {
+public:
+ AVDemuxerImpl();
+ ~AVDemuxerImpl();
+
+ int32_t SelectSourceTrackByID(uint32_t trackIndex) override;
+ int32_t UnselectSourceTrackByID(uint32_t trackIndex) override;
+ int32_t CopyNextSample(uint32_t &trackIndex, uint8_t *buffer,
+ AVCodecBufferInfo &bufferInfo, AVCodecBufferFlag &flag) override;
+ int32_t SeekToTime(int64_t mSeconds, const AVSeekMode mode) override;
+ int32_t Init(AVSource &source);
+
+private:
+ std::shared_ptr demuxerClient_ = nullptr;
+ uint16_t trackLogCount = 0;
+ std::string sourceUri_;
+};
+} // namespace Media
+} // namespace OHOS
+#endif // AVDEMUXER_IMPL_H
\ No newline at end of file
diff --git a/frameworks/native/avmuxer/avmuxer_impl.cpp b/frameworks/native/avmuxer/avmuxer_impl.cpp
new file mode 100644
index 0000000000000000000000000000000000000000..45b0b4faedfe9d325c466cb88039cfbe95f798fd
--- /dev/null
+++ b/frameworks/native/avmuxer/avmuxer_impl.cpp
@@ -0,0 +1,127 @@
+/*
+ * Copyright (C) 2023 Huawei Device Co., Ltd.
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include "avmuxer_impl.h"
+#include
+#include
+#include "securec.h"
+#include "i_avcodec_service.h"
+#include "avcodec_log.h"
+#include "avsharedmemorybase.h"
+#include "avcodec_dfx.h"
+#include "avcodec_errors.h"
+
+namespace {
+ constexpr OHOS::HiviewDFX::HiLogLabel LABEL = {LOG_CORE, LOG_DOMAIN, "AVMuxerImpl"};
+}
+
+namespace OHOS {
+namespace Media {
+std::shared_ptr AVMuxerFactory::CreateAVMuxer(int32_t fd, OutputFormat format)
+{
+ AVCodecTrace trace("AVMuxerFactory::CreateAVMuxer");
+ CHECK_AND_RETURN_RET_LOG((fcntl(fd, F_GETFL, 0) & O_RDWR) == O_RDWR, nullptr, "No permission to read and write fd");
+ CHECK_AND_RETURN_RET_LOG(lseek(fd, 0, SEEK_CUR) != -1, nullptr, "The fd is not seekable");
+
+ std::shared_ptr impl = std::make_shared(fd, format);
+
+ int32_t ret = impl->Init();
+ CHECK_AND_RETURN_RET_LOG(ret == AVCS_ERR_OK, nullptr, "Init avmuxer implementation failed");
+ return impl;
+}
+
+AVMuxerImpl::AVMuxerImpl(int32_t fd, OutputFormat format) : fd_(fd), format_(format)
+{
+ (void)fd_;
+ (void)format_;
+ AVCODEC_LOGD("AVMuxerImpl:0x%{public}06" PRIXPTR " Instances create", FAKE_POINTER(this));
+}
+
+AVMuxerImpl::~AVMuxerImpl()
+{
+ if (muxerService_ != nullptr) {
+ (void)muxerService_->Release();
+ (void)AVCodecServiceFactory::GetInstance().DestroyMuxerService(muxerService_);
+ muxerService_ = nullptr;
+ }
+ AVCODEC_LOGD("AVMuxerImpl:0x%{public}06" PRIXPTR " Instances destroy", FAKE_POINTER(this));
+}
+
+int32_t AVMuxerImpl::Init()
+{
+ AVCodecTrace trace("AVMuxer::Init");
+ AVCODEC_LOGI("Init");
+ muxerService_ = AVCodecServiceFactory::GetInstance().CreateMuxerService();
+ CHECK_AND_RETURN_RET_LOG(muxerService_ != nullptr, AVCS_ERR_NO_MEMORY, "Create AVMuxer Service failed");
+ return muxerService_->InitParameter(fd_, format_);
+}
+
+int32_t AVMuxerImpl::SetLocation(float latitude, float longitude)
+{
+ AVCodecTrace trace("AVMuxer::SetLocation");
+ AVCODEC_LOGI("SetLocation");
+ CHECK_AND_RETURN_RET_LOG(muxerService_ != nullptr, AVCS_ERR_INVALID_OPERATION, "AVMuxer Service does not exist");
+ return muxerService_->SetLocation(latitude, longitude);
+}
+
+int32_t AVMuxerImpl::SetRotation(int32_t rotation)
+{
+ AVCodecTrace trace("AVMuxer::SetRotation");
+ AVCODEC_LOGI("SetRotation");
+ CHECK_AND_RETURN_RET_LOG(muxerService_ != nullptr, AVCS_ERR_INVALID_OPERATION, "AVMuxer Service does not exist");
+ return muxerService_->SetRotation(rotation);
+}
+
+int32_t AVMuxerImpl::AddTrack(int32_t &trackIndex, const MediaDescription &trackDesc)
+{
+ AVCodecTrace trace("AVMuxer::AddTrack");
+ AVCODEC_LOGI("AddTrack");
+ CHECK_AND_RETURN_RET_LOG(muxerService_ != nullptr, AVCS_ERR_INVALID_OPERATION, "AVMuxer Service does not exist");
+ return muxerService_->AddTrack(trackIndex, trackDesc);
+}
+
+int32_t AVMuxerImpl::Start()
+{
+ AVCodecTrace trace("AVMuxer::Start");
+ AVCODEC_LOGI("Start");
+ CHECK_AND_RETURN_RET_LOG(muxerService_ != nullptr, AVCS_ERR_INVALID_OPERATION, "AVMuxer Service does not exist");
+ return muxerService_->Start();
+}
+
+int32_t AVMuxerImpl::WriteSampleBuffer(uint8_t *sampleBuffer, const TrackSampleInfo &info)
+{
+ AVCodecTrace trace("AVMuxer::WriteSampleBuffer");
+ CHECK_AND_RETURN_RET_LOG(muxerService_ != nullptr, AVCS_ERR_INVALID_OPERATION, "AVMuxer Service does not exist");
+ CHECK_AND_RETURN_RET_LOG(sampleBuffer != nullptr && info.timeUs >= 0, AVCS_ERR_INVALID_VAL, "Invalid memory");
+
+ std::shared_ptr sharedSampleBuffer =
+ std::make_shared(info.size, AVSharedMemory::FLAGS_READ_ONLY, "sampleBuffer");
+ int32_t ret = sharedSampleBuffer->Init();
+ CHECK_AND_RETURN_RET_LOG(ret == AVCS_ERR_OK, AVCS_ERR_NO_MEMORY, "create AVSharedMemoryBase failed");
+ errno_t rc = memcpy_s(sharedSampleBuffer->GetBase(), sharedSampleBuffer->GetSize(), sampleBuffer, info.size);
+ CHECK_AND_RETURN_RET_LOG(rc == EOK, AVCS_ERR_UNKNOWN, "memcpy_s failed");
+
+ return muxerService_->WriteSampleBuffer(sharedSampleBuffer, info);
+}
+
+int32_t AVMuxerImpl::Stop()
+{
+ AVCodecTrace trace("AVMuxer::Stop");
+ AVCODEC_LOGI("Stop");
+ CHECK_AND_RETURN_RET_LOG(muxerService_ != nullptr, AVCS_ERR_INVALID_OPERATION, "AVMuxer Service does not exist");
+ return muxerService_->Stop();
+}
+} // namespace Media
+} // namespace OHOS
\ No newline at end of file
diff --git a/frameworks/native/avmuxer/avmuxer_impl.h b/frameworks/native/avmuxer/avmuxer_impl.h
new file mode 100644
index 0000000000000000000000000000000000000000..a909f08f6b1bd9a3f16a82881ae5bd8a91817be9
--- /dev/null
+++ b/frameworks/native/avmuxer/avmuxer_impl.h
@@ -0,0 +1,43 @@
+/*
+ * Copyright (C) 2023 Huawei Device Co., Ltd.
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+#ifndef AVMUXER_IMPL_H
+#define AVMUXER_IMPL_H
+
+#include "avmuxer.h"
+#include "i_muxer_service.h"
+#include "nocopyable.h"
+
+namespace OHOS {
+namespace Media {
+class AVMuxerImpl : public AVMuxer, public NoCopyable {
+public:
+ AVMuxerImpl(int32_t fd, OutputFormat format);
+ ~AVMuxerImpl() override;
+ int32_t Init();
+ int32_t SetLocation(float latitude, float longitude) override;
+ int32_t SetRotation(int32_t rotation) override;
+ int32_t AddTrack(int32_t &trackIndex, const MediaDescription &trackDesc) override;
+ int32_t Start() override;
+ int32_t WriteSampleBuffer(uint8_t *sampleBuffer, const TrackSampleInfo &info) override;
+ int32_t Stop() override;
+
+private:
+ std::shared_ptr muxerService_ = nullptr;
+ int32_t fd_ = -1;
+ OutputFormat format_ = OUTPUT_FORMAT_DEFAULT;
+};
+} // namespace Media
+} // namespace OHOS
+#endif // AVMUXER_IMPL_H
\ No newline at end of file
diff --git a/frameworks/native/avsource/avsource_impl.cpp b/frameworks/native/avsource/avsource_impl.cpp
new file mode 100644
index 0000000000000000000000000000000000000000..1310f3458649c309064365e148e8e5fa782d16b1
--- /dev/null
+++ b/frameworks/native/avsource/avsource_impl.cpp
@@ -0,0 +1,265 @@
+/*
+ * Copyright (C) 2023 Huawei Device Co., Ltd.
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+#include
+#include
+#include
+#include "avcodec_common.h"
+#include "i_avcodec_service.h"
+#include "avcodec_errors.h"
+#include "avcodec_log.h"
+#include "avcodec_dfx.h"
+#include "avsource_impl.h"
+
+namespace {
+ constexpr OHOS::HiviewDFX::HiLogLabel LABEL = {LOG_CORE, LOG_DOMAIN, "AVSourceImpl"};
+}
+
+namespace OHOS {
+namespace Media {
+std::vector setTrackFormatSupportedList = {
+ AVSourceTrackFormat::VIDEO_BIT_STREAM_FORMAT,
+};
+
+std::shared_ptr AVSourceFactory::CreateWithURI(const std::string &uri)
+{
+ AVCodecTrace trace("AVSourceFactory::CreateWithURI");
+
+ AVCODEC_LOGI("create source with uri: uri=%{private}s", uri.c_str());
+
+ std::shared_ptr sourceImpl = std::make_shared();
+ CHECK_AND_RETURN_RET_LOG(sourceImpl != nullptr, nullptr, "New AVSourceImpl failed when create source with uri");
+
+ int32_t ret = sourceImpl->Init(uri);
+
+ CHECK_AND_RETURN_RET_LOG(ret == AVCS_ERR_OK, nullptr, "Init AVSourceImpl failed when create source with uri");
+
+ return sourceImpl;
+}
+
+std::shared_ptr AVSourceFactory::CreateWithFD(int32_t fd, int64_t offset, int64_t size)
+{
+ AVCodecTrace trace("AVSourceFactory::CreateWithFD");
+
+ AVCODEC_LOGI("create source with fd: fd=%{private}d, offset=%{public}" PRId64 ", size=%{public}" PRId64,
+ fd, offset, size);
+
+ CHECK_AND_RETURN_RET_LOG(fd > 2, nullptr,
+ "Create source with uri failed because input fd is illegal, fd must be greater than 2!");
+ CHECK_AND_RETURN_RET_LOG(size >= 0, nullptr, "Create source with uri failed because input size is negative");
+
+ CHECK_AND_RETURN_RET_LOG((fcntl(fd, F_GETFL, 0) & O_RDWR) == O_RDWR, nullptr, "No permission to read and write fd");
+ CHECK_AND_RETURN_RET_LOG(lseek(fd, 0, SEEK_CUR) != -1, nullptr, "The fd is not seekable");
+
+ std::string uri = "fd://" + std::to_string(fd) + "?offset=" + \
+ std::to_string(offset) + "&size=" + std::to_string(size);
+
+ std::shared_ptr sourceImpl = std::make_shared();
+ CHECK_AND_RETURN_RET_LOG(sourceImpl != nullptr, nullptr, "New AVSourceImpl failed when create source with uri");
+
+ int32_t ret = sourceImpl->Init(uri);
+
+ CHECK_AND_RETURN_RET_LOG(ret == AVCS_ERR_OK, nullptr, "Init AVSourceImpl failed when create source with uri");
+
+ return sourceImpl;
+}
+
+int32_t AVSourceImpl::Init(const std::string &uri)
+{
+ AVCodecTrace trace("AVSource::Init");
+
+ sourceClient_ = AVCodecServiceFactory::GetInstance().CreateSourceService();
+ CHECK_AND_RETURN_RET_LOG(sourceClient_ != nullptr, AVCS_ERR_CREATE_SOURCE_SUB_SERVICE_FAILED,
+ "Create source service failed when init sourceImpl");
+
+ int32_t ret = sourceClient_->Init(uri);
+ if (ret == AVCS_ERR_OK) {
+ uint32_t trackCount = 0;
+ ret = sourceClient_->GetTrackCount(trackCount);
+ trackCount_ = trackCount;
+ }
+ return ret;
+}
+
+AVSourceImpl::AVSourceImpl()
+{
+ AVCODEC_LOGI("init sourceImpl");
+ AVCODEC_LOGD("AVSourceImpl:0x%{public}06" PRIXPTR " Instances create", FAKE_POINTER(this));
+}
+
+AVSourceImpl::~AVSourceImpl()
+{
+ AVCODEC_LOGI("uninit sourceImpl for source %{public}s", sourceUri.c_str());
+ if (sourceClient_ != nullptr) {
+ (void)AVCodecServiceFactory::GetInstance().DestroySourceService(sourceClient_);
+ sourceClient_ = nullptr;
+ }
+
+ for (auto track : tracks_) {
+ track = nullptr;
+ }
+ tracks_.clear();
+
+ AVCODEC_LOGD("AVSourceImpl:0x%{public}06" PRIXPTR " Instances destroy", FAKE_POINTER(this));
+}
+
+uintptr_t AVSourceImpl::GetSourceAddr()
+{
+ CHECK_AND_RETURN_RET_LOG(sourceClient_ != nullptr, AVCS_ERR_INVALID_OPERATION,
+ "source service died when get source addr!");
+
+ return sourceClient_->GetSourceAddr();
+}
+
+int32_t AVSourceImpl::GetTrackCount(uint32_t &trackCount)
+{
+ AVCodecTrace trace("AVSource::GetTrackCount");
+
+ AVCODEC_LOGI("get track count by source");
+
+ CHECK_AND_RETURN_RET_LOG(sourceClient_ != nullptr, AVCS_ERR_INVALID_OPERATION,
+ "source service died when get source track!");
+
+ if (trackCount_ < 0) {
+ int32_t ret = sourceClient_->GetTrackCount(trackCount);
+ trackCount_ = trackCount;
+ return ret;
+ } else {
+ trackCount = trackCount_;
+ return AVCS_ERR_OK;
+ }
+}
+
+std::shared_ptr AVSourceImpl::GetSourceTrackByID(uint32_t trackIndex)
+{
+ AVCodecTrace trace("AVSource::GetSourceTrackByID");
+
+ AVCODEC_LOGI("get source track from source: trackIndex=%{public}d", trackIndex);
+
+ CHECK_AND_RETURN_RET_LOG(sourceClient_ != nullptr, nullptr, "source service died when load track!");
+
+ auto trackIndexIsValid = TrackIndexIsValid(trackIndex);
+ CHECK_AND_RETURN_RET_LOG(trackIndexIsValid, nullptr, "track index is invalid!");
+
+ std::shared_ptr track = std::make_shared(this, trackIndex);
+ tracks_.emplace_back(track);
+ return track;
+}
+
+int32_t AVSourceImpl::GetSourceFormat(Format &format)
+{
+ AVCodecTrace trace("AVSource::GetSourceFormat");
+
+ AVCODEC_LOGI("get source format: format=%{public}s", format.Stringify().c_str());
+
+ CHECK_AND_RETURN_RET_LOG(sourceClient_ != nullptr, AVCS_ERR_INVALID_OPERATION,
+ "source service died when get source format!");
+
+ return sourceClient_->GetSourceFormat(format);
+}
+
+int32_t AVSourceImpl::GetTrackFormat(Format &format, uint32_t trackIndex)
+{
+ AVCodecTrace trace("AVSource::GetTrackFormat");
+
+ AVCODEC_LOGI("get source track format: trackIndex=%{public}d, format=%{public}s",
+ trackIndex, format.Stringify().c_str());
+
+ CHECK_AND_RETURN_RET_LOG(sourceClient_ != nullptr, AVCS_ERR_INVALID_OPERATION,
+ "source service died when get track format!");
+
+ auto trackIndexIsValid = TrackIndexIsValid(trackIndex);
+ CHECK_AND_RETURN_RET_LOG(trackIndexIsValid, AVCS_ERR_INVALID_VAL, "track index is invalid!");
+
+ return sourceClient_->GetTrackFormat(format, trackIndex);
+}
+
+int32_t AVSourceImpl::SetTrackFormat(const Format &format, uint32_t trackIndex)
+{
+ AVCodecTrace trace("AVSource::SetTrackFormat");
+
+ AVCODEC_LOGI("set source track format: trackIndex=%{public}d, format=%{public}s",
+ trackIndex, format.Stringify().c_str());
+
+ CHECK_AND_RETURN_RET_LOG(sourceClient_ != nullptr, AVCS_ERR_INVALID_OPERATION,
+ "source service died when set format!");
+
+ auto trackIndexIsValid = TrackIndexIsValid(trackIndex);
+ CHECK_AND_RETURN_RET_LOG(trackIndexIsValid, AVCS_ERR_INVALID_VAL, "track index is invalid!");
+
+ auto &formatMap = format.GetFormatMap();
+ bool allKeySupported = true;
+ for (auto pair : formatMap) {
+ auto index = std::find_if(setTrackFormatSupportedList.begin(), setTrackFormatSupportedList.end(),
+ [pair](std::string_view support) { return pair.first == support; });
+ if (index == setTrackFormatSupportedList.end()) {
+ AVCODEC_LOGE("key %{punlic}s is not supported to set!", pair.first.c_str());
+ allKeySupported = false;
+ break;
+ }
+ }
+
+ CHECK_AND_RETURN_RET_LOG(allKeySupported, AVCS_ERR_INVALID_VAL,
+ "Set track format failed because input format is invalid!");
+
+ return sourceClient_->SetTrackFormat(format, trackIndex);
+}
+
+bool AVSourceImpl::TrackIndexIsValid(uint32_t trackIndex)
+{
+ if (trackCount_ < 0) {
+ uint32_t trackCount = 0;
+ int32_t ret = sourceClient_->GetTrackCount(trackCount);
+ if (ret != AVCS_ERR_OK) {
+ return false;
+ }
+ trackCount_ = trackCount;
+ }
+ return (trackIndex>=0 && (uint32_t)trackCount_>=0 && trackIndex<=(uint32_t)trackCount_);
+}
+
+AVSourceTrackImpl::AVSourceTrackImpl(AVSourceImpl *source, uint32_t trackIndex)
+{
+ AVCODEC_LOGI("init source track: trackIndex=%{public}d", trackIndex);
+
+ trackIndex_ = trackIndex;
+ sourceImpl_ = source;
+
+ AVCODEC_LOGD("AVSourceTrackImpl:0x%{public}06" PRIXPTR " Instances create", FAKE_POINTER(this));
+}
+
+AVSourceTrackImpl::~AVSourceTrackImpl()
+{
+ AVCODEC_LOGI("uninit sourceTrackImpl");
+ if (sourceImpl_ != nullptr) {
+ sourceImpl_ = nullptr;
+ }
+ AVCODEC_LOGD("AVSourceTrackImpl:0x%{public}06" PRIXPTR " Instances destroy", FAKE_POINTER(this));
+}
+
+int32_t AVSourceTrackImpl::SetTrackFormat(const Format &format)
+{
+ AVCodecTrace trace("AVSourceTrack::SetTrackFormat");
+
+ return sourceImpl_->SetTrackFormat(format, trackIndex_);
+}
+
+int32_t AVSourceTrackImpl::GetTrackFormat(Format &format)
+{
+ AVCodecTrace trace("AVSourceTrack::GetTrackFormat");
+
+ return sourceImpl_->GetTrackFormat(format, trackIndex_);
+}
+} // namespace Media
+} // namespace OHOS
\ No newline at end of file
diff --git a/frameworks/native/avsource/avsource_impl.h b/frameworks/native/avsource/avsource_impl.h
new file mode 100644
index 0000000000000000000000000000000000000000..3d173512961075c3bd4079d1e1aa812ad1ac1585
--- /dev/null
+++ b/frameworks/native/avsource/avsource_impl.h
@@ -0,0 +1,61 @@
+/*
+ * Copyright (C) 2023 Huawei Device Co., Ltd.
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#ifndef AVSOURCE_IMPL_H
+#define AVSOURCE_IMPL_H
+
+#include "avsource.h"
+#include "nocopyable.h"
+#include "i_source_service.h"
+
+namespace OHOS {
+namespace Media {
+class AVSourceImpl : public AVSource, public NoCopyable {
+public:
+ AVSourceImpl();
+ ~AVSourceImpl() override;
+
+ int32_t GetTrackCount(uint32_t &trackCount) override;
+ std::shared_ptr GetSourceTrackByID(uint32_t trackIndex) override;
+ uintptr_t GetSourceAddr() override;
+ int32_t GetSourceFormat(Format &format) override;
+ int32_t GetTrackFormat(Format &format, uint32_t trackIndex);
+ int32_t SetTrackFormat(const Format &format, uint32_t trackIndex);
+ int32_t Init(const std::string &uri);
+
+ std::string sourceUri;
+
+private:
+ std::shared_ptr sourceClient_ = nullptr;
+ std::vector> tracks_ {};
+ int32_t trackCount_ = -1;
+ bool TrackIndexIsValid(uint32_t trackIndex);
+};
+
+class AVSourceTrackImpl : public AVSourceTrack, public NoCopyable {
+public:
+ AVSourceTrackImpl(AVSourceImpl *source, uint32_t trackIndex);
+ ~AVSourceTrackImpl();
+
+ int32_t SetTrackFormat(const Format &format) override;
+ int32_t GetTrackFormat(Format &format) override;
+private:
+ uint32_t trackIndex_;
+ AVSourceImpl* sourceImpl_;
+};
+} // namespace Media
+} // namespace OHOS
+#endif // AVSOURCE_IMPL_H
+
diff --git a/frameworks/native/capi/avcodec/native_audio_decoder.cpp b/frameworks/native/capi/avcodec/native_audio_decoder.cpp
new file mode 100644
index 0000000000000000000000000000000000000000..66cf77df279bd2560e6dc0414a4a117c583a92dc
--- /dev/null
+++ b/frameworks/native/capi/avcodec/native_audio_decoder.cpp
@@ -0,0 +1,437 @@
+/*
+ * Copyright (C) 2022 Huawei Device Co., Ltd.
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include
+#include
+#include "native_avcodec_base.h"
+#include "native_avcodec_audiodecoder.h"
+#include "native_avmagic.h"
+#include "avcodec_audio_decoder.h"
+#include "avsharedmemory.h"
+#include "avcodec_log.h"
+#include "avcodec_errors.h"
+
+namespace {
+constexpr OHOS::HiviewDFX::HiLogLabel LABEL = {LOG_CORE, LOG_DOMAIN, "NativeAudioDecoder"};
+}
+
+using namespace OHOS::Media;
+class NativeAudioDecoder;
+
+struct AudioDecoderObject : public OH_AVCodec {
+ explicit AudioDecoderObject(const std::shared_ptr &decoder)
+ : OH_AVCodec(AVMagic::AVCODEC_MAGIC_AUDIO_DECODER), audioDecoder_(decoder) {}
+ ~AudioDecoderObject() = default;
+
+ const std::shared_ptr audioDecoder_;
+ std::list> memoryObjList_;
+ std::shared_ptr callback_ = nullptr;
+ std::atomic isFlushing_ = false;
+ std::atomic isStop_ = false;
+ std::atomic isEOS_ = false;
+};
+
+class NativeAudioDecoder : public AVCodecCallback {
+public:
+ NativeAudioDecoder(OH_AVCodec *codec, struct OH_AVCodecAsyncCallback cb, void *userData)
+ : codec_(codec), callback_(cb), userData_(userData) {}
+ virtual ~NativeAudioDecoder() = default;
+
+ void OnError(AVCodecErrorType errorType, int32_t errorCode) override
+ {
+ std::unique_lock lock(mutex_);
+ (void)errorType;
+ if (codec_ != nullptr && callback_.onError != nullptr) {
+ int32_t extErr = AVCSErrorToOHAVErrCode(static_cast(errorCode));
+ callback_.onError(codec_, extErr, userData_);
+ }
+ }
+
+ void OnOutputFormatChanged(const Format &format) override
+ {
+ std::unique_lock lock(mutex_);
+ if (codec_ != nullptr && callback_.onStreamChanged != nullptr) {
+ OHOS::sptr object = new(std::nothrow) OH_AVFormat(format);
+ // The object lifecycle is controlled by the current function stack
+ callback_.onStreamChanged(codec_, reinterpret_cast(object.GetRefPtr()), userData_);
+ }
+ }
+
+ void OnInputBufferAvailable(uint32_t index) override
+ {
+ std::unique_lock lock(mutex_);
+ if (codec_ != nullptr && callback_.onNeedInputData != nullptr) {
+ struct AudioDecoderObject *audioDecObj = reinterpret_cast(codec_);
+ CHECK_AND_RETURN_LOG(audioDecObj->audioDecoder_ != nullptr, "audioDecoder_ is nullptr!");
+ if (audioDecObj->isFlushing_.load() || audioDecObj->isStop_.load() || audioDecObj->isEOS_.load()) {
+ AVCODEC_LOGD("At flush, eos or stop, no buffer available");
+ return;
+ }
+
+ OH_AVMemory *data = GetInputData(codec_, index);
+ if (data != nullptr) {
+ callback_.onNeedInputData(codec_, index, data, userData_);
+ }
+ }
+ }
+
+ void OnOutputBufferAvailable(uint32_t index, AVCodecBufferInfo info, AVCodecBufferFlag flag) override
+ {
+ std::unique_lock lock(mutex_);
+ if (codec_ != nullptr && callback_.onNeedOutputData != nullptr) {
+ struct AudioDecoderObject *audioDecObj = reinterpret_cast(codec_);
+ CHECK_AND_RETURN_LOG(audioDecObj->audioDecoder_ != nullptr, "audioDecoder_ is nullptr!");
+ if (audioDecObj->isFlushing_.load() || audioDecObj->isStop_.load()) {
+ AVCODEC_LOGD("At flush or stop, ignore");
+ return;
+ }
+
+ struct OH_AVCodecBufferAttr bufferAttr;
+ bufferAttr.pts = info.presentationTimeUs;
+ bufferAttr.size = info.size;
+ bufferAttr.offset = info.offset;
+ bufferAttr.flags = flag;
+ // The bufferInfo lifecycle is controlled by the current function stack
+ OH_AVMemory *data = GetOutputData(codec_, index);
+ callback_.onNeedOutputData(codec_, index, data, &bufferAttr, userData_);
+ }
+ }
+
+ void StopCallback()
+ {
+ std::unique_lock lock(mutex_);
+ codec_ = nullptr;
+ }
+
+private:
+ OH_AVMemory *GetInputData(struct OH_AVCodec *codec, uint32_t index)
+ {
+ CHECK_AND_RETURN_RET_LOG(codec != nullptr, nullptr, "input codec is nullptr!");
+ CHECK_AND_RETURN_RET_LOG(codec->magic_ == AVMagic::AVCODEC_MAGIC_AUDIO_DECODER, nullptr, "magic error!");
+
+ struct AudioDecoderObject *audioDecObj = reinterpret_cast(codec);
+ CHECK_AND_RETURN_RET_LOG(audioDecObj->audioDecoder_ != nullptr, nullptr, "audioDecoder_ is nullptr!");
+
+ std::shared_ptr memory = audioDecObj->audioDecoder_->GetInputBuffer(index);
+ CHECK_AND_RETURN_RET_LOG(memory != nullptr, nullptr, "get input buffer is nullptr!");
+
+ for (auto &memoryObj : audioDecObj->memoryObjList_) {
+ if (memoryObj->IsEqualMemory(memory)) {
+ return reinterpret_cast(memoryObj.GetRefPtr());
+ }
+ }
+
+ OHOS::sptr object = new(std::nothrow) OH_AVMemory(memory);
+ CHECK_AND_RETURN_RET_LOG(object != nullptr, nullptr, "failed to new OH_AVMemory");
+
+ audioDecObj->memoryObjList_.push_back(object);
+ return reinterpret_cast(object.GetRefPtr());
+ }
+
+ OH_AVMemory *GetOutputData(struct OH_AVCodec *codec, uint32_t index)
+ {
+ CHECK_AND_RETURN_RET_LOG(codec != nullptr, nullptr, "input codec is nullptr!");
+ CHECK_AND_RETURN_RET_LOG(codec->magic_ == AVMagic::AVCODEC_MAGIC_AUDIO_DECODER, nullptr, "magic error!");
+
+ struct AudioDecoderObject *audioDecObj = reinterpret_cast(codec);
+ CHECK_AND_RETURN_RET_LOG(audioDecObj->audioDecoder_ != nullptr, nullptr, "audioDecoder_ is nullptr!");
+
+ std::shared_ptr memory = audioDecObj->audioDecoder_->GetOutputBuffer(index);
+ CHECK_AND_RETURN_RET_LOG(memory != nullptr, nullptr, "get output buffer is nullptr!");
+
+ for (auto &memoryObj : audioDecObj->memoryObjList_) {
+ if (memoryObj->IsEqualMemory(memory)) {
+ return reinterpret_cast(memoryObj.GetRefPtr());
+ }
+ }
+
+ OHOS::sptr object = new(std::nothrow) OH_AVMemory(memory);
+ CHECK_AND_RETURN_RET_LOG(object != nullptr, nullptr, "failed to new OH_AVMemory");
+
+ audioDecObj->memoryObjList_.push_back(object);
+ return reinterpret_cast(object.GetRefPtr());
+ }
+
+ struct OH_AVCodec *codec_;
+ struct OH_AVCodecAsyncCallback callback_;
+ void *userData_;
+ std::mutex mutex_;
+};
+
+namespace OHOS {
+namespace Media {
+#ifdef __cplusplus
+ extern "C" {
+#endif
+
+struct OH_AVCodec *OH_AudioDecoder_CreateByMime(const char *mime)
+{
+ CHECK_AND_RETURN_RET_LOG(mime != nullptr, nullptr, "input mime is nullptr!");
+
+ std::shared_ptr audioDecoder = AudioDecoderFactory::CreateByMime(mime);
+ CHECK_AND_RETURN_RET_LOG(audioDecoder != nullptr, nullptr, "failed to AudioDecoderFactory::CreateByMime");
+
+ struct AudioDecoderObject *object = new(std::nothrow) AudioDecoderObject(audioDecoder);
+ CHECK_AND_RETURN_RET_LOG(object != nullptr, nullptr, "failed to new AudioDecoderObject");
+
+ return object;
+}
+
+struct OH_AVCodec *OH_AudioDecoder_CreateByName(const char *name)
+{
+ CHECK_AND_RETURN_RET_LOG(name != nullptr, nullptr, "input name is nullptr!");
+
+ std::shared_ptr audioDecoder = AudioDecoderFactory::CreateByName(name);
+ CHECK_AND_RETURN_RET_LOG(audioDecoder != nullptr, nullptr, "failed to AudioDecoderFactory::CreateByMime");
+
+ struct AudioDecoderObject *object = new(std::nothrow) AudioDecoderObject(audioDecoder);
+ CHECK_AND_RETURN_RET_LOG(object != nullptr, nullptr, "failed to new AudioDecoderObject");
+
+ return object;
+}
+
+OH_AVErrCode OH_AudioDecoder_Destroy(struct OH_AVCodec *codec)
+{
+ CHECK_AND_RETURN_RET_LOG(codec != nullptr, AV_ERR_INVALID_VAL, "input codec is nullptr!");
+ CHECK_AND_RETURN_RET_LOG(codec->magic_ == AVMagic::AVCODEC_MAGIC_AUDIO_DECODER, AV_ERR_INVALID_VAL, "magic error!");
+
+ struct AudioDecoderObject *audioDecObj = reinterpret_cast(codec);
+
+ if (audioDecObj != nullptr && audioDecObj->audioDecoder_ != nullptr) {
+ audioDecObj->callback_->StopCallback();
+ audioDecObj->memoryObjList_.clear();
+ audioDecObj->isStop_.store(true);
+ int32_t ret = audioDecObj->audioDecoder_->Release();
+ if (ret != AVCS_ERR_OK) {
+ AVCODEC_LOGE("audioDecoder Release failed!");
+ delete codec;
+ return AV_ERR_OPERATE_NOT_PERMIT;
+ }
+ } else {
+ AVCODEC_LOGD("audioDecoder_ is nullptr!");
+ }
+
+ delete codec;
+ return AV_ERR_OK;
+}
+
+OH_AVErrCode OH_AudioDecoder_Configure(struct OH_AVCodec *codec, struct OH_AVFormat *format)
+{
+ CHECK_AND_RETURN_RET_LOG(codec != nullptr, AV_ERR_INVALID_VAL, "input codec is nullptr!");
+ CHECK_AND_RETURN_RET_LOG(codec->magic_ == AVMagic::AVCODEC_MAGIC_AUDIO_DECODER, AV_ERR_INVALID_VAL, "magic error!");
+ CHECK_AND_RETURN_RET_LOG(format != nullptr, AV_ERR_INVALID_VAL, "input format is nullptr!");
+ CHECK_AND_RETURN_RET_LOG(format->magic_ == AVMagic::AVCODEC_MAGIC_FORMAT, AV_ERR_INVALID_VAL, "magic error!");
+
+ struct AudioDecoderObject *audioDecObj = reinterpret_cast(codec);
+ CHECK_AND_RETURN_RET_LOG(audioDecObj->audioDecoder_ != nullptr, AV_ERR_INVALID_VAL, "audioDecoder is nullptr!");
+
+ int32_t ret = audioDecObj->audioDecoder_->Configure(format->format_);
+ CHECK_AND_RETURN_RET_LOG(ret == AVCS_ERR_OK, AV_ERR_OPERATE_NOT_PERMIT, "audioDecoder Configure failed!");
+
+ return AV_ERR_OK;
+}
+
+OH_AVErrCode OH_AudioDecoder_Prepare(struct OH_AVCodec *codec)
+{
+ CHECK_AND_RETURN_RET_LOG(codec != nullptr, AV_ERR_INVALID_VAL, "input codec is nullptr!");
+ CHECK_AND_RETURN_RET_LOG(codec->magic_ == AVMagic::AVCODEC_MAGIC_AUDIO_DECODER, AV_ERR_INVALID_VAL, "magic error!");
+
+ struct AudioDecoderObject *audioDecObj = reinterpret_cast(codec);
+ CHECK_AND_RETURN_RET_LOG(audioDecObj->audioDecoder_ != nullptr, AV_ERR_INVALID_VAL, "audioDecoder_ is nullptr!");
+
+ int32_t ret = audioDecObj->audioDecoder_->Prepare();
+ CHECK_AND_RETURN_RET_LOG(ret == AVCS_ERR_OK, AV_ERR_OPERATE_NOT_PERMIT, "audioDecoder Prepare failed!");
+ return AV_ERR_OK;
+}
+
+OH_AVErrCode OH_AudioDecoder_Start(struct OH_AVCodec *codec)
+{
+ CHECK_AND_RETURN_RET_LOG(codec != nullptr, AV_ERR_INVALID_VAL, "input codec is nullptr!");
+ CHECK_AND_RETURN_RET_LOG(codec->magic_ == AVMagic::AVCODEC_MAGIC_AUDIO_DECODER, AV_ERR_INVALID_VAL, "magic error!");
+
+ struct AudioDecoderObject *audioDecObj = reinterpret_cast(codec);
+ CHECK_AND_RETURN_RET_LOG(audioDecObj->audioDecoder_ != nullptr, AV_ERR_INVALID_VAL, "audioDecoder_ is nullptr!");
+ audioDecObj->isStop_.store(false);
+ audioDecObj->isEOS_.store(false);
+ int32_t ret = audioDecObj->audioDecoder_->Start();
+ CHECK_AND_RETURN_RET_LOG(ret == AVCS_ERR_OK, AV_ERR_OPERATE_NOT_PERMIT, "audioDecoder Start failed!");
+
+ return AV_ERR_OK;
+}
+
+OH_AVErrCode OH_AudioDecoder_Stop(struct OH_AVCodec *codec)
+{
+ CHECK_AND_RETURN_RET_LOG(codec != nullptr, AV_ERR_INVALID_VAL, "input codec is nullptr!");
+ CHECK_AND_RETURN_RET_LOG(codec->magic_ == AVMagic::AVCODEC_MAGIC_AUDIO_DECODER, AV_ERR_INVALID_VAL, "magic error!");
+
+ struct AudioDecoderObject *audioDecObj = reinterpret_cast(codec);
+ CHECK_AND_RETURN_RET_LOG(audioDecObj->audioDecoder_ != nullptr, AV_ERR_INVALID_VAL, "audioDecoder_ is nullptr!");
+
+ audioDecObj->isStop_.store(true);
+ AVCODEC_LOGD("set stop status to true");
+
+ int32_t ret = audioDecObj->audioDecoder_->Stop();
+ if (ret != AVCS_ERR_OK) {
+ audioDecObj->isStop_.store(false);
+ AVCODEC_LOGE("audioDecoder Stop failed!, set stop status to false");
+ return AV_ERR_OPERATE_NOT_PERMIT;
+ }
+ audioDecObj->memoryObjList_.clear();
+
+ return AV_ERR_OK;
+}
+
+OH_AVErrCode OH_AudioDecoder_Flush(struct OH_AVCodec *codec)
+{
+ CHECK_AND_RETURN_RET_LOG(codec != nullptr, AV_ERR_INVALID_VAL, "input codec is nullptr!");
+ CHECK_AND_RETURN_RET_LOG(codec->magic_ == AVMagic::AVCODEC_MAGIC_AUDIO_DECODER, AV_ERR_INVALID_VAL, "magic error!");
+
+ struct AudioDecoderObject *audioDecObj = reinterpret_cast(codec);
+ CHECK_AND_RETURN_RET_LOG(audioDecObj->audioDecoder_ != nullptr, AV_ERR_INVALID_VAL, "audioDecoder_ is nullptr!");
+ audioDecObj->isFlushing_.store(true);
+ AVCODEC_LOGD("Set flush status to true");
+ int32_t ret = audioDecObj->audioDecoder_->Flush();
+ if (ret != AVCS_ERR_OK) {
+ audioDecObj->isFlushing_.store(false);
+ AVCODEC_LOGE("audioDecoder Flush failed! Set flush status to false");
+ return AV_ERR_OPERATE_NOT_PERMIT;
+ }
+
+ audioDecObj->memoryObjList_.clear();
+ audioDecObj->isFlushing_.store(false);
+ AVCODEC_LOGD("set flush status to false");
+ return AV_ERR_OK;
+}
+
+OH_AVErrCode OH_AudioDecoder_Reset(struct OH_AVCodec *codec)
+{
+ CHECK_AND_RETURN_RET_LOG(codec != nullptr, AV_ERR_INVALID_VAL, "input codec is nullptr!");
+ CHECK_AND_RETURN_RET_LOG(codec->magic_ == AVMagic::AVCODEC_MAGIC_AUDIO_DECODER, AV_ERR_INVALID_VAL, "magic error!");
+
+ struct AudioDecoderObject *audioDecObj = reinterpret_cast(codec);
+ CHECK_AND_RETURN_RET_LOG(audioDecObj->audioDecoder_ != nullptr, AV_ERR_INVALID_VAL, "audioDecoder_ is nullptr!");
+ audioDecObj->isStop_.store(true);
+ AVCODEC_LOGD("Set stop status to true");
+
+ int32_t ret = audioDecObj->audioDecoder_->Reset();
+ if (ret != AVCS_ERR_OK) {
+ audioDecObj->isStop_.store(false);
+ AVCODEC_LOGE("audioDecoder Reset failed! Set stop status to false");
+ return AV_ERR_OPERATE_NOT_PERMIT;
+ }
+
+ audioDecObj->memoryObjList_.clear();
+ return AV_ERR_OK;
+}
+
+OH_AVErrCode OH_AudioDecoder_PushInputData(struct OH_AVCodec *codec, uint32_t index, OH_AVCodecBufferAttr attr)
+{
+ CHECK_AND_RETURN_RET_LOG(codec != nullptr, AV_ERR_INVALID_VAL, "input codec is nullptr!");
+ CHECK_AND_RETURN_RET_LOG(codec->magic_ == AVMagic::AVCODEC_MAGIC_AUDIO_DECODER, AV_ERR_INVALID_VAL, "magic error!");
+
+ struct AudioDecoderObject *audioDecObj = reinterpret_cast(codec);
+ CHECK_AND_RETURN_RET_LOG(audioDecObj->audioDecoder_ != nullptr, AV_ERR_INVALID_VAL, "audioDecoder_ is nullptr!");
+
+ struct AVCodecBufferInfo bufferInfo;
+ bufferInfo.presentationTimeUs = attr.pts;
+ bufferInfo.size = attr.size;
+ bufferInfo.offset = attr.offset;
+ AVCodecBufferFlag bufferFlag = static_cast(attr.flags);
+
+ int32_t ret = audioDecObj->audioDecoder_->QueueInputBuffer(index, bufferInfo, bufferFlag);
+ CHECK_AND_RETURN_RET_LOG(ret == AVCS_ERR_OK, AV_ERR_OPERATE_NOT_PERMIT, "audioDecoder QueueInputBuffer failed!");
+ if (bufferFlag == AVCODEC_BUFFER_FLAG_EOS) {
+ audioDecObj->isEOS_.store(true);
+ AVCODEC_LOGD("Set eos status to true");
+ }
+
+ return AV_ERR_OK;
+}
+
+OH_AVFormat *OH_AudioDecoder_GetOutputDescription(struct OH_AVCodec *codec)
+{
+ CHECK_AND_RETURN_RET_LOG(codec != nullptr, nullptr, "input codec is nullptr!");
+ CHECK_AND_RETURN_RET_LOG(codec->magic_ == AVMagic::AVCODEC_MAGIC_AUDIO_DECODER, nullptr, "magic error!");
+
+ struct AudioDecoderObject *audioDecObj = reinterpret_cast(codec);
+ CHECK_AND_RETURN_RET_LOG(audioDecObj->audioDecoder_ != nullptr, nullptr, "audioDecoder_ is nullptr!");
+
+ Format format;
+ int32_t ret = audioDecObj->audioDecoder_->GetOutputFormat(format);
+ CHECK_AND_RETURN_RET_LOG(ret == AVCS_ERR_OK, nullptr, "audioDecoder GetOutputFormat failed!");
+
+ OH_AVFormat *avFormat = OH_AVFormat_Create();
+ avFormat->format_ = format;
+
+ return avFormat;
+}
+
+OH_AVErrCode OH_AudioDecoder_FreeOutputData(struct OH_AVCodec *codec, uint32_t index)
+{
+ CHECK_AND_RETURN_RET_LOG(codec != nullptr, AV_ERR_INVALID_VAL, "input codec is nullptr!");
+ CHECK_AND_RETURN_RET_LOG(codec->magic_ == AVMagic::AVCODEC_MAGIC_AUDIO_DECODER, AV_ERR_INVALID_VAL, "magic error!");
+
+ struct AudioDecoderObject *audioDecObj = reinterpret_cast(codec);
+ CHECK_AND_RETURN_RET_LOG(audioDecObj->audioDecoder_ != nullptr, AV_ERR_INVALID_VAL, "audioDecoder_ is nullptr!");
+
+ int32_t ret = audioDecObj->audioDecoder_->ReleaseOutputBuffer(index);
+ CHECK_AND_RETURN_RET_LOG(ret == AVCS_ERR_OK, AV_ERR_OPERATE_NOT_PERMIT, "audioDecoder ReleaseOutputBuffer failed!");
+
+ return AV_ERR_OK;
+}
+
+OH_AVErrCode OH_AudioDecoder_SetParameter(struct OH_AVCodec *codec, struct OH_AVFormat *format)
+{
+ CHECK_AND_RETURN_RET_LOG(codec != nullptr, AV_ERR_INVALID_VAL, "input codec is nullptr!");
+ CHECK_AND_RETURN_RET_LOG(codec->magic_ == AVMagic::AVCODEC_MAGIC_AUDIO_DECODER, AV_ERR_INVALID_VAL, "magic error!");
+ CHECK_AND_RETURN_RET_LOG(format != nullptr, AV_ERR_INVALID_VAL, "input format is nullptr!");
+ CHECK_AND_RETURN_RET_LOG(format->magic_ == AVMagic::AVCODEC_MAGIC_FORMAT, AV_ERR_INVALID_VAL, "magic error!");
+
+ struct AudioDecoderObject *audioDecObj = reinterpret_cast(codec);
+ CHECK_AND_RETURN_RET_LOG(audioDecObj->audioDecoder_ != nullptr, AV_ERR_INVALID_VAL, "audioDecoder_ is nullptr!");
+
+ int32_t ret = audioDecObj->audioDecoder_->SetParameter(format->format_);
+ CHECK_AND_RETURN_RET_LOG(ret == AVCS_ERR_OK, AV_ERR_OPERATE_NOT_PERMIT, "audioDecoder SetParameter failed!");
+
+ return AV_ERR_OK;
+}
+
+OH_AVErrCode OH_AudioDecoder_SetCallback(
+ struct OH_AVCodec *codec, struct OH_AVCodecAsyncCallback callback, void *userData)
+{
+ CHECK_AND_RETURN_RET_LOG(codec != nullptr, AV_ERR_INVALID_VAL, "input codec is nullptr!");
+ CHECK_AND_RETURN_RET_LOG(codec->magic_ == AVMagic::AVCODEC_MAGIC_AUDIO_DECODER, AV_ERR_INVALID_VAL, "magic error!");
+
+ struct AudioDecoderObject *audioDecObj = reinterpret_cast(codec);
+ CHECK_AND_RETURN_RET_LOG(audioDecObj->audioDecoder_ != nullptr, AV_ERR_INVALID_VAL, "audioDecoder_ is nullptr!");
+
+ audioDecObj->callback_ = std::make_shared(codec, callback, userData);
+
+ int32_t ret = audioDecObj->audioDecoder_->SetCallback(audioDecObj->callback_);
+ CHECK_AND_RETURN_RET_LOG(ret == AVCS_ERR_OK, AV_ERR_OPERATE_NOT_PERMIT, "audioDecoder SetCallback failed!");
+
+ return AV_ERR_OK;
+}
+
+OH_AVErrCode OH_AudioDecoder_IsValid(OH_AVCodec *codec, bool *isVaild)
+{
+ return AV_ERR_UNSUPPORT;
+}
+
+#ifdef __cplusplus
+ };
+#endif
+} // namesapce Media
+} // OHOS
\ No newline at end of file
diff --git a/frameworks/native/capi/avcodec/native_audio_encoder.cpp b/frameworks/native/capi/avcodec/native_audio_encoder.cpp
new file mode 100644
index 0000000000000000000000000000000000000000..e682e7f5e0526ce79693e67260ad2ce2f0cf63c9
--- /dev/null
+++ b/frameworks/native/capi/avcodec/native_audio_encoder.cpp
@@ -0,0 +1,438 @@
+/*
+ * Copyright (C) 2022 Huawei Device Co., Ltd.
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include
+#include
+#include "native_avcodec_base.h"
+#include "native_avcodec_audioencoder.h"
+#include "native_avmagic.h"
+#include "avcodec_audio_encoder.h"
+#include "avsharedmemory.h"
+#include "avcodec_log.h"
+#include "avcodec_errors.h"
+
+namespace {
+constexpr OHOS::HiviewDFX::HiLogLabel LABEL = {LOG_CORE, LOG_DOMAIN, "NativeAudioEncoder"};
+}
+
+using namespace OHOS::Media;
+class NativeAudioEncoderCallback;
+
+struct AudioEncoderObject : public OH_AVCodec {
+ explicit AudioEncoderObject(const std::shared_ptr &encoder)
+ : OH_AVCodec(AVMagic::AVCODEC_MAGIC_AUDIO_ENCODER), audioEncoder_(encoder)
+ {
+ }
+ ~AudioEncoderObject() = default;
+
+ const std::shared_ptr audioEncoder_;
+ std::list> memoryObjList_;
+ std::shared_ptr callback_ = nullptr;
+ std::atomic isFlushing_ = false;
+ std::atomic isStop_ = false;
+ std::atomic isEOS_ = false;
+};
+
+class NativeAudioEncoderCallback : public AVCodecCallback {
+public:
+ NativeAudioEncoderCallback(OH_AVCodec *codec, struct OH_AVCodecAsyncCallback cb, void *userData)
+ : codec_(codec), callback_(cb), userData_(userData)
+ {
+ }
+ virtual ~NativeAudioEncoderCallback() = default;
+
+ void OnError(AVCodecErrorType errorType, int32_t errorCode) override
+ {
+ std::unique_lock lock(mutex_);
+ (void)errorType;
+ if (codec_ != nullptr && callback_.onError != nullptr) {
+ int32_t extErr = AVCSErrorToOHAVErrCode(static_cast(errorCode));
+ callback_.onError(codec_, extErr, userData_);
+ }
+ }
+
+ void OnOutputFormatChanged(const Format &format) override
+ {
+ std::unique_lock lock(mutex_);
+ if (codec_ != nullptr && callback_.onStreamChanged != nullptr) {
+ OHOS::sptr object = new (std::nothrow) OH_AVFormat(format);
+ // The object lifecycle is controlled by the current function stack
+ callback_.onStreamChanged(codec_, reinterpret_cast(object.GetRefPtr()), userData_);
+ }
+ }
+
+ void OnInputBufferAvailable(uint32_t index) override
+ {
+ std::unique_lock lock(mutex_);
+ if (codec_ != nullptr && callback_.onNeedInputData != nullptr) {
+ struct AudioEncoderObject *audioEncObj = reinterpret_cast(codec_);
+ CHECK_AND_RETURN_LOG(audioEncObj->audioEncoder_ != nullptr, "audioEncoder_ is nullptr!");
+ if (audioEncObj->isFlushing_.load() || audioEncObj->isStop_.load() || audioEncObj->isEOS_.load()) {
+ AVCODEC_LOGD("At flush, eos or stop, no buffer available");
+ return;
+ }
+
+ OH_AVMemory *data = GetInputData(codec_, index);
+ if (data != nullptr) {
+ callback_.onNeedInputData(codec_, index, data, userData_);
+ }
+ }
+ }
+
+ void OnOutputBufferAvailable(uint32_t index, AVCodecBufferInfo info, AVCodecBufferFlag flag) override
+ {
+ std::unique_lock lock(mutex_);
+ if (codec_ != nullptr && callback_.onNeedOutputData != nullptr) {
+ struct AudioEncoderObject *audioEncObj = reinterpret_cast(codec_);
+ CHECK_AND_RETURN_LOG(audioEncObj->audioEncoder_ != nullptr, "audioEncoder_ is nullptr!");
+ if (audioEncObj->isFlushing_.load() || audioEncObj->isStop_.load()) {
+ AVCODEC_LOGD("At flush or stop, ignore");
+ return;
+ }
+ struct OH_AVCodecBufferAttr bufferAttr;
+ bufferAttr.pts = info.presentationTimeUs;
+ bufferAttr.size = info.size;
+ bufferAttr.offset = info.offset;
+ bufferAttr.flags = flag;
+ // The bufferInfo lifecycle is controlled by the current function stack
+ OH_AVMemory *data = GetOutputData(codec_, index);
+ callback_.onNeedOutputData(codec_, index, data, &bufferAttr, userData_);
+ }
+ }
+
+ void StopCallback()
+ {
+ std::unique_lock lock(mutex_);
+ codec_ = nullptr;
+ }
+
+private:
+ OH_AVMemory *GetInputData(struct OH_AVCodec *codec, uint32_t index)
+ {
+ CHECK_AND_RETURN_RET_LOG(codec != nullptr, nullptr, "input codec is nullptr!");
+ CHECK_AND_RETURN_RET_LOG(codec->magic_ == AVMagic::AVCODEC_MAGIC_AUDIO_ENCODER, nullptr, "magic error!");
+
+ struct AudioEncoderObject *audioEncObj = reinterpret_cast(codec);
+ CHECK_AND_RETURN_RET_LOG(audioEncObj->audioEncoder_ != nullptr, nullptr, "audioEncoder_ is nullptr!");
+
+ std::shared_ptr memory = audioEncObj->audioEncoder_->GetInputBuffer(index);
+ CHECK_AND_RETURN_RET_LOG(memory != nullptr, nullptr, "get input buffer is nullptr!");
+
+ for (auto &memoryObj : audioEncObj->memoryObjList_) {
+ if (memoryObj->IsEqualMemory(memory)) {
+ return reinterpret_cast(memoryObj.GetRefPtr());
+ }
+ }
+
+ OHOS::sptr object = new (std::nothrow) OH_AVMemory(memory);
+ CHECK_AND_RETURN_RET_LOG(object != nullptr, nullptr, "failed to new OH_AVMemory");
+
+ audioEncObj->memoryObjList_.push_back(object);
+ return reinterpret_cast(object.GetRefPtr());
+ }
+
+ OH_AVMemory *GetOutputData(struct OH_AVCodec *codec, uint32_t index)
+ {
+ CHECK_AND_RETURN_RET_LOG(codec != nullptr, nullptr, "input codec is nullptr!");
+ CHECK_AND_RETURN_RET_LOG(codec->magic_ == AVMagic::AVCODEC_MAGIC_AUDIO_ENCODER, nullptr, "magic error!");
+
+ struct AudioEncoderObject *audioEncObj = reinterpret_cast(codec);
+ CHECK_AND_RETURN_RET_LOG(audioEncObj->audioEncoder_ != nullptr, nullptr, "audioEncoder_ is nullptr!");
+
+ std::shared_ptr memory = audioEncObj->audioEncoder_->GetOutputBuffer(index);
+ CHECK_AND_RETURN_RET_LOG(memory != nullptr, nullptr, "get output buffer is nullptr!");
+
+ for (auto &memoryObj : audioEncObj->memoryObjList_) {
+ if (memoryObj->IsEqualMemory(memory)) {
+ return reinterpret_cast(memoryObj.GetRefPtr());
+ }
+ }
+
+ OHOS::sptr object = new (std::nothrow) OH_AVMemory(memory);
+ CHECK_AND_RETURN_RET_LOG(object != nullptr, nullptr, "failed to new OH_AVMemory");
+
+ audioEncObj->memoryObjList_.push_back(object);
+ return reinterpret_cast(object.GetRefPtr());
+ }
+
+ struct OH_AVCodec *codec_;
+ struct OH_AVCodecAsyncCallback callback_;
+ void *userData_;
+ std::mutex mutex_;
+};
+
+namespace OHOS {
+namespace Media {
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+struct OH_AVCodec *OH_AudioEncoder_CreateByMime(const char *mime)
+{
+ CHECK_AND_RETURN_RET_LOG(mime != nullptr, nullptr, "input mime is nullptr!");
+
+ std::shared_ptr audioEncoder = AudioEncoderFactory::CreateByMime(mime);
+ CHECK_AND_RETURN_RET_LOG(audioEncoder != nullptr, nullptr, "failed to AudioEncoderFactory::CreateByMime");
+
+ struct AudioEncoderObject *object = new (std::nothrow) AudioEncoderObject(audioEncoder);
+ CHECK_AND_RETURN_RET_LOG(object != nullptr, nullptr, "failed to new AudioEncoderObject");
+
+ return object;
+}
+
+struct OH_AVCodec *OH_AudioEncoder_CreateByName(const char *name)
+{
+ CHECK_AND_RETURN_RET_LOG(name != nullptr, nullptr, "input name is nullptr!");
+
+ std::shared_ptr audioEncoder = AudioEncoderFactory::CreateByName(name);
+ CHECK_AND_RETURN_RET_LOG(audioEncoder != nullptr, nullptr, "failed to AudioEncoderFactory::CreateByMime");
+
+ struct AudioEncoderObject *object = new (std::nothrow) AudioEncoderObject(audioEncoder);
+ CHECK_AND_RETURN_RET_LOG(object != nullptr, nullptr, "failed to new AudioEncoderObject");
+
+ return object;
+}
+
+OH_AVErrCode OH_AudioEncoder_Destroy(struct OH_AVCodec *codec)
+{
+ CHECK_AND_RETURN_RET_LOG(codec != nullptr, AV_ERR_INVALID_VAL, "input codec is nullptr!");
+ CHECK_AND_RETURN_RET_LOG(codec->magic_ == AVMagic::AVCODEC_MAGIC_AUDIO_ENCODER, AV_ERR_INVALID_VAL, "magic error!");
+
+ struct AudioEncoderObject *audioEncObj = reinterpret_cast(codec);
+
+ if (audioEncObj != nullptr && audioEncObj->audioEncoder_ != nullptr) {
+ audioEncObj->callback_->StopCallback();
+ audioEncObj->memoryObjList_.clear();
+ int32_t ret = audioEncObj->audioEncoder_->Release();
+ if (ret != AVCS_ERR_OK) {
+ AVCODEC_LOGE("audioEncoder Release failed!");
+ delete codec;
+ return AV_ERR_OPERATE_NOT_PERMIT;
+ }
+ } else {
+ AVCODEC_LOGD("audioEncoder_ is nullptr!");
+ }
+
+ delete codec;
+ return AV_ERR_OK;
+}
+
+OH_AVErrCode OH_AudioEncoder_Configure(struct OH_AVCodec *codec, struct OH_AVFormat *format)
+{
+ CHECK_AND_RETURN_RET_LOG(codec != nullptr, AV_ERR_INVALID_VAL, "input codec is nullptr!");
+ CHECK_AND_RETURN_RET_LOG(codec->magic_ == AVMagic::AVCODEC_MAGIC_AUDIO_ENCODER, AV_ERR_INVALID_VAL, "magic error!");
+ CHECK_AND_RETURN_RET_LOG(format != nullptr, AV_ERR_INVALID_VAL, "input format is nullptr!");
+ CHECK_AND_RETURN_RET_LOG(format->magic_ == AVMagic::AVCODEC_MAGIC_FORMAT, AV_ERR_INVALID_VAL, "magic error!");
+
+ struct AudioEncoderObject *audioEncObj = reinterpret_cast(codec);
+ CHECK_AND_RETURN_RET_LOG(audioEncObj->audioEncoder_ != nullptr, AV_ERR_INVALID_VAL, "audioEncoder is nullptr!");
+
+ int32_t ret = audioEncObj->audioEncoder_->Configure(format->format_);
+ CHECK_AND_RETURN_RET_LOG(ret == AVCS_ERR_OK, AV_ERR_OPERATE_NOT_PERMIT, "audioEncoder Configure failed!");
+
+ return AV_ERR_OK;
+}
+
+OH_AVErrCode OH_AudioEncoder_Prepare(struct OH_AVCodec *codec)
+{
+ CHECK_AND_RETURN_RET_LOG(codec != nullptr, AV_ERR_INVALID_VAL, "input codec is nullptr!");
+ CHECK_AND_RETURN_RET_LOG(codec->magic_ == AVMagic::AVCODEC_MAGIC_AUDIO_ENCODER, AV_ERR_INVALID_VAL, "magic error!");
+
+ struct AudioEncoderObject *audioEncObj = reinterpret_cast(codec);
+ CHECK_AND_RETURN_RET_LOG(audioEncObj->audioEncoder_ != nullptr, AV_ERR_INVALID_VAL, "audioEncoder_ is nullptr!");
+
+ int32_t ret = audioEncObj->audioEncoder_->Prepare();
+ CHECK_AND_RETURN_RET_LOG(ret == AVCS_ERR_OK, AV_ERR_OPERATE_NOT_PERMIT, "audioEncoder Prepare failed!");
+
+ return AV_ERR_OK;
+}
+
+OH_AVErrCode OH_AudioEncoder_Start(struct OH_AVCodec *codec)
+{
+ CHECK_AND_RETURN_RET_LOG(codec != nullptr, AV_ERR_INVALID_VAL, "input codec is nullptr!");
+ CHECK_AND_RETURN_RET_LOG(codec->magic_ == AVMagic::AVCODEC_MAGIC_AUDIO_ENCODER, AV_ERR_INVALID_VAL, "magic error!");
+
+ struct AudioEncoderObject *audioEncObj = reinterpret_cast(codec);
+ CHECK_AND_RETURN_RET_LOG(audioEncObj->audioEncoder_ != nullptr, AV_ERR_INVALID_VAL, "audioEncoder_ is nullptr!");
+ audioEncObj->isStop_.store(false);
+ audioEncObj->isEOS_.store(false);
+ AVCODEC_LOGD("Set stop and eos status to false");
+ int32_t ret = audioEncObj->audioEncoder_->Start();
+ CHECK_AND_RETURN_RET_LOG(ret == AVCS_ERR_OK, AV_ERR_OPERATE_NOT_PERMIT, "audioEncoder Start failed!");
+
+ return AV_ERR_OK;
+}
+
+OH_AVErrCode OH_AudioEncoder_Stop(struct OH_AVCodec *codec)
+{
+ CHECK_AND_RETURN_RET_LOG(codec != nullptr, AV_ERR_INVALID_VAL, "input codec is nullptr!");
+ CHECK_AND_RETURN_RET_LOG(codec->magic_ == AVMagic::AVCODEC_MAGIC_AUDIO_ENCODER, AV_ERR_INVALID_VAL, "magic error!");
+
+ struct AudioEncoderObject *audioEncObj = reinterpret_cast(codec);
+ CHECK_AND_RETURN_RET_LOG(audioEncObj->audioEncoder_ != nullptr, AV_ERR_INVALID_VAL, "audioEncoder_ is nullptr!");
+ audioEncObj->isStop_.store(true);
+ AVCODEC_LOGD("Set stop status to true");
+
+ int32_t ret = audioEncObj->audioEncoder_->Stop();
+ if (ret != AVCS_ERR_OK) {
+ audioEncObj->isStop_.store(false);
+ AVCODEC_LOGE("audioEncoder Stop failed! Set stop status to false");
+ return AV_ERR_OPERATE_NOT_PERMIT;
+ }
+ audioEncObj->memoryObjList_.clear();
+
+ return AV_ERR_OK;
+}
+
+OH_AVErrCode OH_AudioEncoder_Flush(struct OH_AVCodec *codec)
+{
+ CHECK_AND_RETURN_RET_LOG(codec != nullptr, AV_ERR_INVALID_VAL, "input codec is nullptr!");
+ CHECK_AND_RETURN_RET_LOG(codec->magic_ == AVMagic::AVCODEC_MAGIC_AUDIO_ENCODER, AV_ERR_INVALID_VAL, "magic error!");
+
+ struct AudioEncoderObject *audioEncObj = reinterpret_cast(codec);
+ CHECK_AND_RETURN_RET_LOG(audioEncObj->audioEncoder_ != nullptr, AV_ERR_INVALID_VAL, "audioEncoder_ is nullptr!");
+
+ audioEncObj->isFlushing_.store(true);
+ AVCODEC_LOGD("Set flush status to true");
+
+ int32_t ret = audioEncObj->audioEncoder_->Flush();
+ if (ret != AVCS_ERR_OK) {
+ audioEncObj->isFlushing_.store(false);
+ AVCODEC_LOGE("audioEncObj Flush failed! Set flush status to false");
+ return AV_ERR_OPERATE_NOT_PERMIT;
+ }
+ audioEncObj->memoryObjList_.clear();
+ audioEncObj->isFlushing_.store(false);
+ AVCODEC_LOGD("Set flush status to false");
+ return AV_ERR_OK;
+}
+
+OH_AVErrCode OH_AudioEncoder_Reset(struct OH_AVCodec *codec)
+{
+ CHECK_AND_RETURN_RET_LOG(codec != nullptr, AV_ERR_INVALID_VAL, "input codec is nullptr!");
+ CHECK_AND_RETURN_RET_LOG(codec->magic_ == AVMagic::AVCODEC_MAGIC_AUDIO_ENCODER, AV_ERR_INVALID_VAL, "magic error!");
+
+ struct AudioEncoderObject *audioEncObj = reinterpret_cast(codec);
+ CHECK_AND_RETURN_RET_LOG(audioEncObj->audioEncoder_ != nullptr, AV_ERR_INVALID_VAL, "audioEncoder_ is nullptr!");
+ audioEncObj->isStop_.store(true);
+ int32_t ret = audioEncObj->audioEncoder_->Reset();
+ if (ret != AVCS_ERR_OK) {
+ audioEncObj->isStop_.store(false);
+ AVCODEC_LOGE("audioEncoder Reset failed! Set stop status to false");
+ return AV_ERR_OPERATE_NOT_PERMIT;
+ }
+ audioEncObj->memoryObjList_.clear();
+ return AV_ERR_OK;
+}
+
+OH_AVErrCode OH_AudioEncoder_PushInputData(struct OH_AVCodec *codec, uint32_t index, OH_AVCodecBufferAttr attr)
+{
+ CHECK_AND_RETURN_RET_LOG(codec != nullptr, AV_ERR_INVALID_VAL, "input codec is nullptr!");
+ CHECK_AND_RETURN_RET_LOG(codec->magic_ == AVMagic::AVCODEC_MAGIC_AUDIO_ENCODER, AV_ERR_INVALID_VAL, "magic error!");
+
+ struct AudioEncoderObject *audioEncObj = reinterpret_cast(codec);
+ CHECK_AND_RETURN_RET_LOG(audioEncObj->audioEncoder_ != nullptr, AV_ERR_INVALID_VAL, "audioEncoder_ is nullptr!");
+
+ struct AVCodecBufferInfo bufferInfo;
+ bufferInfo.presentationTimeUs = attr.pts;
+ bufferInfo.size = attr.size;
+ bufferInfo.offset = attr.offset;
+ AVCodecBufferFlag bufferFlag = static_cast(attr.flags);
+
+ int32_t ret = audioEncObj->audioEncoder_->QueueInputBuffer(index, bufferInfo, bufferFlag);
+ CHECK_AND_RETURN_RET_LOG(ret == AVCS_ERR_OK, AV_ERR_OPERATE_NOT_PERMIT, "audioEncoder QueueInputBuffer failed!");
+ if (bufferFlag == AVCODEC_BUFFER_FLAG_EOS) {
+ audioEncObj->isEOS_.store(true);
+ AVCODEC_LOGD("Set eos status to true");
+ }
+
+ return AV_ERR_OK;
+}
+
+OH_AVFormat *OH_AudioEncoder_GetOutputDescription(struct OH_AVCodec *codec)
+{
+ CHECK_AND_RETURN_RET_LOG(codec != nullptr, nullptr, "input codec is nullptr!");
+ CHECK_AND_RETURN_RET_LOG(codec->magic_ == AVMagic::AVCODEC_MAGIC_AUDIO_ENCODER, nullptr, "magic error!");
+
+ struct AudioEncoderObject *audioEncObj = reinterpret_cast(codec);
+ CHECK_AND_RETURN_RET_LOG(audioEncObj->audioEncoder_ != nullptr, nullptr, "audioEncoder_ is nullptr!");
+
+ Format format;
+ int32_t ret = audioEncObj->audioEncoder_->GetOutputFormat(format);
+ CHECK_AND_RETURN_RET_LOG(ret == AVCS_ERR_OK, nullptr, "audioEncoder GetOutputFormat failed!");
+
+ OH_AVFormat *avFormat = OH_AVFormat_Create();
+ avFormat->format_ = format;
+
+ return avFormat;
+}
+
+OH_AVErrCode OH_AudioEncoder_FreeOutputData(struct OH_AVCodec *codec, uint32_t index)
+{
+ CHECK_AND_RETURN_RET_LOG(codec != nullptr, AV_ERR_INVALID_VAL, "input codec is nullptr!");
+ CHECK_AND_RETURN_RET_LOG(codec->magic_ == AVMagic::AVCODEC_MAGIC_AUDIO_ENCODER, AV_ERR_INVALID_VAL, "magic error!");
+
+ struct AudioEncoderObject *audioEncObj = reinterpret_cast(codec);
+ CHECK_AND_RETURN_RET_LOG(audioEncObj->audioEncoder_ != nullptr, AV_ERR_INVALID_VAL, "audioEncoder_ is nullptr!");
+
+ int32_t ret = audioEncObj->audioEncoder_->ReleaseOutputBuffer(index);
+ CHECK_AND_RETURN_RET_LOG(ret == AVCS_ERR_OK, AV_ERR_OPERATE_NOT_PERMIT, "audioEncoder ReleaseOutputBuffer failed!");
+
+ return AV_ERR_OK;
+}
+
+OH_AVErrCode OH_AudioEncoder_SetParameter(struct OH_AVCodec *codec, struct OH_AVFormat *format)
+{
+ CHECK_AND_RETURN_RET_LOG(codec != nullptr, AV_ERR_INVALID_VAL, "input codec is nullptr!");
+ CHECK_AND_RETURN_RET_LOG(codec->magic_ == AVMagic::AVCODEC_MAGIC_AUDIO_ENCODER, AV_ERR_INVALID_VAL, "magic error!");
+ CHECK_AND_RETURN_RET_LOG(format != nullptr, AV_ERR_INVALID_VAL, "input format is nullptr!");
+ CHECK_AND_RETURN_RET_LOG(format->magic_ == AVMagic::AVCODEC_MAGIC_FORMAT, AV_ERR_INVALID_VAL, "magic error!");
+
+ struct AudioEncoderObject *audioEncObj = reinterpret_cast(codec);
+ CHECK_AND_RETURN_RET_LOG(audioEncObj->audioEncoder_ != nullptr, AV_ERR_INVALID_VAL, "audioEncoder_ is nullptr!");
+
+ int32_t ret = audioEncObj->audioEncoder_->SetParameter(format->format_);
+ CHECK_AND_RETURN_RET_LOG(ret == AVCS_ERR_OK, AV_ERR_OPERATE_NOT_PERMIT, "audioEncoder SetParameter failed!");
+
+ return AV_ERR_OK;
+}
+
+OH_AVErrCode OH_AudioEncoder_SetCallback(struct OH_AVCodec *codec, struct OH_AVCodecAsyncCallback callback,
+ void *userData)
+{
+ CHECK_AND_RETURN_RET_LOG(codec != nullptr, AV_ERR_INVALID_VAL, "input codec is nullptr!");
+ CHECK_AND_RETURN_RET_LOG(codec->magic_ == AVMagic::AVCODEC_MAGIC_AUDIO_ENCODER, AV_ERR_INVALID_VAL, "magic error!");
+
+ struct AudioEncoderObject *audioEncObj = reinterpret_cast(codec);
+ CHECK_AND_RETURN_RET_LOG(audioEncObj->audioEncoder_ != nullptr, AV_ERR_INVALID_VAL, "audioEncoder_ is nullptr!");
+
+ audioEncObj->callback_ = std::make_shared(codec, callback, userData);
+
+ int32_t ret = audioEncObj->audioEncoder_->SetCallback(audioEncObj->callback_);
+ CHECK_AND_RETURN_RET_LOG(ret == AVCS_ERR_OK, AV_ERR_OPERATE_NOT_PERMIT, "audioEncoder SetCallback failed!");
+
+ return AV_ERR_OK;
+}
+
+OH_AVErrCode OH_AudioEncoder_IsValid(OH_AVCodec *codec, bool *isVaild)
+{
+ return AV_ERR_UNSUPPORT;
+}
+
+#ifdef __cplusplus
+};
+#endif
+} // namespace Media
+} // namespace OHOS
\ No newline at end of file
diff --git a/frameworks/native/capi/avcodec/native_avcodec_base.cpp b/frameworks/native/capi/avcodec/native_avcodec_base.cpp
new file mode 100644
index 0000000000000000000000000000000000000000..c24eb5f8b6587fe3c280f161384e06c3da10efff
--- /dev/null
+++ b/frameworks/native/capi/avcodec/native_avcodec_base.cpp
@@ -0,0 +1,67 @@
+/*
+ * Copyright (C) 2022 Huawei Device Co., Ltd.
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include "native_avcodec_base.h"
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+const char *OH_AVCODEC_MIMETYPE_VIDEO_AVC = "video/avc";
+const char *OH_AVCODEC_MIMETYPE_VIDEO_MPEG4 = "video/mp4v-es";
+const char *OH_AVCODEC_MIMETYPE_AUDIO_AAC = "audio/mp4a-latm";
+const char *OH_AVCODEC_MIMETYPE_AUDIO_MPEG = "audio/mpeg";
+const char *OH_AVCODEC_MIMETYPE_IMAGE_JPG = "image/jpeg";
+const char *OH_AVCODEC_MIMETYPE_IMAGE_PNG = "image/png";
+const char *OH_AVCODEC_MIMETYPE_IMAGE_BMP = "image/bmp";
+const char *OH_ED_KEY_TIME_STAMP = "timeStamp";
+const char *OH_ED_KEY_EOS = "endOfStream";
+const char *OH_MD_KEY_TRACK_TYPE = "track_type";
+const char *OH_MD_KEY_CODEC_MIME = "codec_mime";
+const char *OH_MD_KEY_DURATION = "duration";
+const char *OH_MD_KEY_BITRATE = "bitrate";
+const char *OH_MD_KEY_MAX_INPUT_SIZE = "max_input_size";
+const char *OH_MD_KEY_WIDTH = "width";
+const char *OH_MD_KEY_HEIGHT = "height";
+const char *OH_MD_KEY_PIXEL_FORMAT = "pixel_format";
+const char *OH_MD_KEY_AUDIO_SAMPLE_FORMAT = "audio_sample_format";
+const char *OH_MD_KEY_FRAME_RATE = "frame_rate";
+const char *OH_MD_KEY_VIDEO_ENCODE_BITRATE_MODE = "video_encode_bitrate_mode";
+const char *OH_MD_KEY_PROFILE = "codec_profile";
+const char *OH_MD_KEY_AUD_CHANNEL_COUNT = "channel_count";
+const char *OH_MD_KEY_AUD_SAMPLE_RATE = "sample_rate";
+const char *OH_MD_KEY_I_FRAME_INTERVAL = "i_frame_interval";
+const char *OH_MD_KEY_ROTATION = "rotation_angle";
+const char *OH_MD_KEY_CODEC_CONFIG = "codec_config";
+
+const char *OH_MD_KEY_TRACK_INDEX = "track_index";
+const char *OH_MD_KEY_TRACK_SAMPLE_COUNT = "track_sample_count";
+const char *OH_MD_KEY_BIT_STREAM_FORMAT = "bit_stream_format";
+const char *OH_MD_KEY_TITLE = "title";
+const char *OH_MD_KEY_ARTIST = "artist";
+const char *OH_MD_KEY_ALBUM = "album";
+const char *OH_MD_KEY_ALBUM_ARTIST = "album_artist";
+const char *OH_MD_KEY_DATE = "date";
+const char *OH_MD_KEY_COMMENT = "comment";
+const char *OH_MD_KEY_GENRE = "genre";
+const char *OH_MD_KEY_COPYRIGHT = "copyright";
+const char *OH_MD_KEY_LANGUAGE = "language";
+const char *OH_MD_KEY_DESCRIPTION = "description";
+const char *OH_MD_KEY_LYRICS = "lyrics";
+const char *OH_MD_KEY_START_TIME = "start_time";
+const char *OH_MD_KEY_TYPE = "media_type";
+#ifdef __cplusplus
+}
+#endif
diff --git a/frameworks/native/capi/avcodec/native_codeclist.cpp b/frameworks/native/capi/avcodec/native_codeclist.cpp
new file mode 100644
index 0000000000000000000000000000000000000000..5aea04dd539aacdc8ec669586f4574973227b768
--- /dev/null
+++ b/frameworks/native/capi/avcodec/native_codeclist.cpp
@@ -0,0 +1,61 @@
+/*
+ * Copyright (C) 2022 Huawei Device Co., Ltd.
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include "native_avmagic.h"
+#include "avcodec_list.h"
+#include "avcodec_errors.h"
+#include "avcodec_log.h"
+#include "native_avcodec_list.h"
+
+namespace {
+constexpr OHOS::HiviewDFX::HiLogLabel LABEL = {LOG_CORE, LOG_DOMAIN, "NativeCodecList"};
+}
+
+using namespace OHOS::Media;
+const char *OH_AVCodec_FindEncoder(const OH_AVFormat *format)
+{
+ std::shared_ptr codeclist = AVCodecListFactory::CreateAVCodecList();
+ std::string strname = codeclist->FindEncoder(format->format_);
+ char *ret = new char[strname.length() + 1];
+ std::strcpy(ret, strname.c_str());
+ AVCODEC_LOGD("get codecname: %{public}s", ret);
+ return ret;
+}
+
+const char *OH_AVCodec_FindDecoder(const OH_AVFormat *format)
+{
+ std::shared_ptr codeclist = AVCodecListFactory::CreateAVCodecList();
+ std::string strname = codeclist->FindDecoder(format->format_);
+ char *ret = new char[strname.length() + 1];
+ std::strcpy(ret, strname.c_str());
+ AVCODEC_LOGD("get codecname: %{public}s", ret);
+ return ret;
+}
+
+OH_AVCapability *OH_AVCodec_CreateCapability(const char *name)
+{
+ std::shared_ptr codeclist = AVCodecListFactory::CreateAVCodecList();
+ CapabilityData capabilityData = codeclist->CreateCapability(name);
+ return new (std::nothrow) OH_AVCapability(capabilityData);
+}
+
+OH_AVErrCode OH_AVCodec_DestroyCapability(OH_AVCapability *capability)
+{
+ if (capability == nullptr) {
+ return AV_ERR_INVALID_VAL;
+ }
+ delete capability;
+ return AV_ERR_OK;
+}
diff --git a/frameworks/native/capi/avcodec/native_video_decoder.cpp b/frameworks/native/capi/avcodec/native_video_decoder.cpp
new file mode 100644
index 0000000000000000000000000000000000000000..56a589fb85692bb18838c7618e68cf5bb0748a35
--- /dev/null
+++ b/frameworks/native/capi/avcodec/native_video_decoder.cpp
@@ -0,0 +1,474 @@
+/*
+ * Copyright (C) 2022 Huawei Device Co., Ltd.
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include
+#include
+#include "native_avcodec_base.h"
+#include "native_avcodec_videodecoder.h"
+#include "native_avmagic.h"
+#include "native_window.h"
+#include "avcodec_video_decoder.h"
+#include "avsharedmemory.h"
+#include "avcodec_log.h"
+#include "avcodec_errors.h"
+
+namespace {
+constexpr OHOS::HiviewDFX::HiLogLabel LABEL = {LOG_CORE, LOG_DOMAIN, "NativeVideoDecoder"};
+}
+
+using namespace OHOS::Media;
+class NativeVideoDecoderCallback;
+
+struct VideoDecoderObject : public OH_AVCodec {
+ explicit VideoDecoderObject(const std::shared_ptr &decoder)
+ : OH_AVCodec(AVMagic::AVCODEC_MAGIC_VIDEO_DECODER), videoDecoder_(decoder)
+ {
+ }
+ ~VideoDecoderObject() = default;
+
+ const std::shared_ptr videoDecoder_;
+ std::list> memoryObjList_;
+ std::shared_ptr callback_ = nullptr;
+ std::atomic isFlushing_ = false;
+ std::atomic isStop_ = false;
+ std::atomic isEOS_ = false;
+};
+
+class NativeVideoDecoderCallback : public AVCodecCallback {
+public:
+ NativeVideoDecoderCallback(OH_AVCodec *codec, struct OH_AVCodecAsyncCallback cb, void *userData)
+ : codec_(codec), callback_(cb), userData_(userData)
+ {
+ }
+ virtual ~NativeVideoDecoderCallback() = default;
+
+ void OnError(AVCodecErrorType errorType, int32_t errorCode) override
+ {
+ std::unique_lock lock(mutex_);
+ (void)errorType;
+
+ CHECK_AND_RETURN_LOG(codec_ != nullptr, "Codec is nullptr");
+ CHECK_AND_RETURN_LOG(callback_.onError != nullptr, "Callback is nullptr");
+ int32_t extErr = AVCSErrorToOHAVErrCode(static_cast(errorCode));
+ callback_.onError(codec_, extErr, userData_);
+ }
+
+ void OnOutputFormatChanged(const Format &format) override
+ {
+ std::unique_lock lock(mutex_);
+
+ CHECK_AND_RETURN_LOG(codec_ != nullptr, "Codec is nullptr");
+ CHECK_AND_RETURN_LOG(callback_.onStreamChanged != nullptr, "Callback is nullptr");
+ OHOS::sptr object = new (std::nothrow) OH_AVFormat(format);
+ callback_.onStreamChanged(codec_, reinterpret_cast(object.GetRefPtr()), userData_);
+ }
+
+ void OnInputBufferAvailable(uint32_t index) override
+ {
+ std::unique_lock lock(mutex_);
+
+ CHECK_AND_RETURN_LOG(codec_ != nullptr, "Codec is nullptr");
+ CHECK_AND_RETURN_LOG(callback_.onNeedInputData != nullptr, "Callback is nullptr");
+
+ struct VideoDecoderObject *videoDecObj = reinterpret_cast(codec_);
+ CHECK_AND_RETURN_LOG(videoDecObj->videoDecoder_ != nullptr, "Video decoder is nullptr!");
+
+ if (videoDecObj->isFlushing_.load() || videoDecObj->isStop_.load() || videoDecObj->isEOS_.load()) {
+ AVCODEC_LOGD("At flush, eos or stop, no buffer available");
+ return;
+ }
+
+ OH_AVMemory *data = GetInputData(codec_, index);
+ CHECK_AND_RETURN_LOG(data != nullptr, "Data is nullptr, get input data failed");
+ callback_.onNeedInputData(codec_, index, data, userData_);
+ }
+
+ void OnOutputBufferAvailable(uint32_t index, AVCodecBufferInfo info, AVCodecBufferFlag flag) override
+ {
+ std::unique_lock lock(mutex_);
+ CHECK_AND_RETURN_LOG(codec_ != nullptr, "Codec is nullptr");
+ CHECK_AND_RETURN_LOG(callback_.onNeedOutputData != nullptr, "Callback is nullptr");
+
+ struct VideoDecoderObject *videoDecObj = reinterpret_cast(codec_);
+ CHECK_AND_RETURN_LOG(videoDecObj->videoDecoder_ != nullptr, "Video decoder is nullptr!");
+
+ if (videoDecObj->isFlushing_.load() || videoDecObj->isStop_.load()) {
+ AVCODEC_LOGD("At flush or stop, ignore");
+ return;
+ }
+
+ struct OH_AVCodecBufferAttr bufferAttr;
+ bufferAttr.pts = info.presentationTimeUs;
+ bufferAttr.size = info.size;
+ bufferAttr.offset = info.offset;
+ bufferAttr.flags = flag;
+ callback_.onNeedOutputData(codec_, index, nullptr, &bufferAttr, userData_);
+ }
+
+ void StopCallback()
+ {
+ std::unique_lock lock(mutex_);
+ codec_ = nullptr;
+ }
+
+private:
+ OH_AVMemory *GetInputData(struct OH_AVCodec *codec, uint32_t index)
+ {
+ CHECK_AND_RETURN_RET_LOG(codec != nullptr, nullptr, "Codec is nullptr!");
+ CHECK_AND_RETURN_RET_LOG(codec->magic_ == AVMagic::AVCODEC_MAGIC_VIDEO_DECODER, nullptr, "Codec magic error!");
+
+ struct VideoDecoderObject *videoDecObj = reinterpret_cast(codec);
+ CHECK_AND_RETURN_RET_LOG(videoDecObj->videoDecoder_ != nullptr, nullptr, "Video decoder is nullptr!");
+
+ std::shared_ptr memory = videoDecObj->videoDecoder_->GetInputBuffer(index);
+ CHECK_AND_RETURN_RET_LOG(memory != nullptr, nullptr, "Memory is nullptr, get input buffer failed!");
+
+ for (auto &memoryObj : videoDecObj->memoryObjList_) {
+ if (memoryObj->IsEqualMemory(memory)) {
+ return reinterpret_cast(memoryObj.GetRefPtr());
+ }
+ }
+
+ OHOS::sptr object = new (std::nothrow) OH_AVMemory(memory);
+ CHECK_AND_RETURN_RET_LOG(object != nullptr, nullptr, "AV memory create failed");
+
+ videoDecObj->memoryObjList_.push_back(object);
+ return reinterpret_cast(object.GetRefPtr());
+ }
+
+ OH_AVMemory *GetOutputData(struct OH_AVCodec *codec, uint32_t index)
+ {
+ CHECK_AND_RETURN_RET_LOG(codec != nullptr, nullptr, "Codec is nullptr!");
+ CHECK_AND_RETURN_RET_LOG(codec->magic_ == AVMagic::AVCODEC_MAGIC_VIDEO_DECODER, nullptr, "Codec magic error!");
+
+ struct VideoDecoderObject *videoDecObj = reinterpret_cast(codec);
+ CHECK_AND_RETURN_RET_LOG(videoDecObj->videoDecoder_ != nullptr, nullptr, "Video decoder is nullptr!");
+
+ std::shared_ptr memory = videoDecObj->videoDecoder_->GetOutputBuffer(index);
+ CHECK_AND_RETURN_RET_LOG(memory != nullptr, nullptr, "Memory is nullptr, get output buffer failed!");
+
+ for (auto &memoryObj : videoDecObj->memoryObjList_) {
+ if (memoryObj->IsEqualMemory(memory)) {
+ return reinterpret_cast(memoryObj.GetRefPtr());
+ }
+ }
+
+ OHOS::sptr object = new (std::nothrow) OH_AVMemory(memory);
+ CHECK_AND_RETURN_RET_LOG(object != nullptr, nullptr, "AV memory create failed");
+
+ videoDecObj->memoryObjList_.push_back(object);
+ return reinterpret_cast(object.GetRefPtr());
+ }
+
+ struct OH_AVCodec *codec_;
+ struct OH_AVCodecAsyncCallback callback_;
+ void *userData_;
+ std::mutex mutex_;
+};
+
+struct OH_AVCodec *OH_VideoDecoder_CreateByMime(const char *mime)
+{
+ CHECK_AND_RETURN_RET_LOG(mime != nullptr, nullptr, "Mime is nullptr!");
+
+ std::shared_ptr videoDecoder = VideoDecoderFactory::CreateByMime(mime);
+ CHECK_AND_RETURN_RET_LOG(videoDecoder != nullptr, nullptr, "Video decoder create by mime failed!");
+
+ struct VideoDecoderObject *object = new (std::nothrow) VideoDecoderObject(videoDecoder);
+ CHECK_AND_RETURN_RET_LOG(object != nullptr, nullptr, "Video decoder create by mime failed!");
+
+ return object;
+}
+
+struct OH_AVCodec *OH_VideoDecoder_CreateByName(const char *name)
+{
+ CHECK_AND_RETURN_RET_LOG(name != nullptr, nullptr, "Name is nullptr!");
+
+ std::shared_ptr videoDecoder = VideoDecoderFactory::CreateByName(name);
+ CHECK_AND_RETURN_RET_LOG(videoDecoder != nullptr, nullptr, "Video decoder create by name failed!");
+
+ struct VideoDecoderObject *object = new (std::nothrow) VideoDecoderObject(videoDecoder);
+ CHECK_AND_RETURN_RET_LOG(object != nullptr, nullptr, "Video decoder create by name failed!");
+
+ return object;
+}
+
+OH_AVErrCode OH_VideoDecoder_Destroy(struct OH_AVCodec *codec)
+{
+ CHECK_AND_RETURN_RET_LOG(codec != nullptr, AV_ERR_INVALID_VAL, "Codec is nullptr!");
+ CHECK_AND_RETURN_RET_LOG(codec->magic_ == AVMagic::AVCODEC_MAGIC_VIDEO_DECODER, AV_ERR_INVALID_VAL,
+ "Codec magic error!");
+
+ struct VideoDecoderObject *videoDecObj = reinterpret_cast(codec);
+
+ if (videoDecObj != nullptr && videoDecObj->videoDecoder_ != nullptr) {
+ videoDecObj->callback_->StopCallback();
+ videoDecObj->memoryObjList_.clear();
+ videoDecObj->isStop_.store(false);
+ int32_t ret = videoDecObj->videoDecoder_->Release();
+ if (ret != AVCS_ERR_OK) {
+ AVCODEC_LOGE("Video decoder destroy failed!");
+ delete codec;
+ return AV_ERR_OPERATE_NOT_PERMIT;
+ }
+ } else {
+ AVCODEC_LOGD("Video decoder is nullptr!");
+ }
+
+ delete codec;
+ return AV_ERR_OK;
+}
+
+OH_AVErrCode OH_VideoDecoder_Configure(struct OH_AVCodec *codec, struct OH_AVFormat *format)
+{
+ CHECK_AND_RETURN_RET_LOG(codec != nullptr, AV_ERR_INVALID_VAL, "Codec is nullptr!");
+ CHECK_AND_RETURN_RET_LOG(codec->magic_ == AVMagic::AVCODEC_MAGIC_VIDEO_DECODER, AV_ERR_INVALID_VAL,
+ "Codec magic error!");
+ CHECK_AND_RETURN_RET_LOG(format != nullptr, AV_ERR_INVALID_VAL, "Format is nullptr!");
+ CHECK_AND_RETURN_RET_LOG(format->magic_ == AVMagic::AVCODEC_MAGIC_FORMAT, AV_ERR_INVALID_VAL,
+ "Format magic error!");
+
+ struct VideoDecoderObject *videoDecObj = reinterpret_cast(codec);
+ CHECK_AND_RETURN_RET_LOG(videoDecObj->videoDecoder_ != nullptr, AV_ERR_INVALID_VAL, "Video decoder is nullptr!");
+
+ int32_t ret = videoDecObj->videoDecoder_->Configure(format->format_);
+ CHECK_AND_RETURN_RET_LOG(ret == AVCS_ERR_OK, AV_ERR_OPERATE_NOT_PERMIT, "Video decoder configure failed!");
+
+ return AV_ERR_OK;
+}
+
+OH_AVErrCode OH_VideoDecoder_Prepare(struct OH_AVCodec *codec)
+{
+ CHECK_AND_RETURN_RET_LOG(codec != nullptr, AV_ERR_INVALID_VAL, "Codec is nullptr!");
+ CHECK_AND_RETURN_RET_LOG(codec->magic_ == AVMagic::AVCODEC_MAGIC_VIDEO_DECODER, AV_ERR_INVALID_VAL,
+ "Codec magic error!");
+
+ struct VideoDecoderObject *videoDecObj = reinterpret_cast(codec);
+ CHECK_AND_RETURN_RET_LOG(videoDecObj->videoDecoder_ != nullptr, AV_ERR_INVALID_VAL, "Video decoder is nullptr!");
+
+ int32_t ret = videoDecObj->videoDecoder_->Prepare();
+ CHECK_AND_RETURN_RET_LOG(ret == AVCS_ERR_OK, AV_ERR_OPERATE_NOT_PERMIT, "Video decoder prepare failed!");
+
+ return AV_ERR_OK;
+}
+
+OH_AVErrCode OH_VideoDecoder_Start(struct OH_AVCodec *codec)
+{
+ CHECK_AND_RETURN_RET_LOG(codec != nullptr, AV_ERR_INVALID_VAL, "Codec is nullptr!");
+ CHECK_AND_RETURN_RET_LOG(codec->magic_ == AVMagic::AVCODEC_MAGIC_VIDEO_DECODER, AV_ERR_INVALID_VAL,
+ "Codec magic error!");
+
+ struct VideoDecoderObject *videoDecObj = reinterpret_cast(codec);
+ CHECK_AND_RETURN_RET_LOG(videoDecObj->videoDecoder_ != nullptr, AV_ERR_INVALID_VAL, "Video decoder is nullptr!");
+
+ videoDecObj->isStop_.store(false);
+ videoDecObj->isEOS_.store(false);
+ int32_t ret = videoDecObj->videoDecoder_->Start();
+ CHECK_AND_RETURN_RET_LOG(ret == AVCS_ERR_OK, AV_ERR_OPERATE_NOT_PERMIT, "Video decoder start failed!");
+
+ return AV_ERR_OK;
+}
+
+OH_AVErrCode OH_VideoDecoder_Stop(struct OH_AVCodec *codec)
+{
+ CHECK_AND_RETURN_RET_LOG(codec != nullptr, AV_ERR_INVALID_VAL, "Codec is nullptr!");
+ CHECK_AND_RETURN_RET_LOG(codec->magic_ == AVMagic::AVCODEC_MAGIC_VIDEO_DECODER, AV_ERR_INVALID_VAL,
+ "Codec magic error!");
+
+ struct VideoDecoderObject *videoDecObj = reinterpret_cast(codec);
+ CHECK_AND_RETURN_RET_LOG(videoDecObj->videoDecoder_ != nullptr, AV_ERR_INVALID_VAL, "Video decoder is nullptr!");
+
+ videoDecObj->isStop_.store(true);
+ int32_t ret = videoDecObj->videoDecoder_->Stop();
+ if (ret != AVCS_ERR_OK) {
+ videoDecObj->isStop_.store(false);
+ AVCODEC_LOGE("Video decoder stop failed");
+ return AV_ERR_OPERATE_NOT_PERMIT;
+ }
+ videoDecObj->memoryObjList_.clear();
+
+ return AV_ERR_OK;
+}
+
+OH_AVErrCode OH_VideoDecoder_Flush(struct OH_AVCodec *codec)
+{
+ CHECK_AND_RETURN_RET_LOG(codec != nullptr, AV_ERR_INVALID_VAL, "Codec is nullptr!");
+ CHECK_AND_RETURN_RET_LOG(codec->magic_ == AVMagic::AVCODEC_MAGIC_VIDEO_DECODER, AV_ERR_INVALID_VAL,
+ "Codec magic error!");
+
+ struct VideoDecoderObject *videoDecObj = reinterpret_cast(codec);
+ CHECK_AND_RETURN_RET_LOG(videoDecObj->videoDecoder_ != nullptr, AV_ERR_INVALID_VAL, "Video decoder is nullptr!");
+
+ videoDecObj->isFlushing_.store(true);
+ int32_t ret = videoDecObj->videoDecoder_->Flush();
+ videoDecObj->isFlushing_.store(false);
+ CHECK_AND_RETURN_RET_LOG(ret == AVCS_ERR_OK, AV_ERR_OPERATE_NOT_PERMIT, "Video decoder flush failed!");
+ videoDecObj->memoryObjList_.clear();
+
+ return AV_ERR_OK;
+}
+
+OH_AVErrCode OH_VideoDecoder_Reset(struct OH_AVCodec *codec)
+{
+ CHECK_AND_RETURN_RET_LOG(codec != nullptr, AV_ERR_INVALID_VAL, "Codec is nullptr!");
+ CHECK_AND_RETURN_RET_LOG(codec->magic_ == AVMagic::AVCODEC_MAGIC_VIDEO_DECODER, AV_ERR_INVALID_VAL,
+ "Codec magic error!");
+
+ struct VideoDecoderObject *videoDecObj = reinterpret_cast(codec);
+ CHECK_AND_RETURN_RET_LOG(videoDecObj->videoDecoder_ != nullptr, AV_ERR_INVALID_VAL, "Video decoder is nullptr!");
+
+ videoDecObj->isStop_.store(true);
+ int32_t ret = videoDecObj->videoDecoder_->Reset();
+ if (ret != AVCS_ERR_OK) {
+ videoDecObj->isStop_.store(false);
+ AVCODEC_LOGE("Video decoder reset failed!");
+ return AV_ERR_OPERATE_NOT_PERMIT;
+ }
+ videoDecObj->memoryObjList_.clear();
+
+ return AV_ERR_OK;
+}
+
+OH_AVErrCode OH_VideoDecoder_SetSurface(OH_AVCodec *codec, OHNativeWindow *window)
+{
+ CHECK_AND_RETURN_RET_LOG(codec != nullptr, AV_ERR_INVALID_VAL, "Codec is nullptr!");
+ CHECK_AND_RETURN_RET_LOG(codec->magic_ == AVMagic::AVCODEC_MAGIC_VIDEO_DECODER, AV_ERR_INVALID_VAL,
+ "Codec magic error!");
+ CHECK_AND_RETURN_RET_LOG(window != nullptr, AV_ERR_INVALID_VAL, "Window is nullptr!");
+ CHECK_AND_RETURN_RET_LOG(window->surface != nullptr, AV_ERR_INVALID_VAL, "Surface is nullptr!");
+
+ struct VideoDecoderObject *videoDecObj = reinterpret_cast(codec);
+ CHECK_AND_RETURN_RET_LOG(videoDecObj->videoDecoder_ != nullptr, AV_ERR_INVALID_VAL, "Video decoder is nullptr!");
+
+ int32_t ret = videoDecObj->videoDecoder_->SetOutputSurface(window->surface);
+ CHECK_AND_RETURN_RET_LOG(ret == AVCS_ERR_OK, AV_ERR_OPERATE_NOT_PERMIT, "Video decoder set output surface failed!");
+
+ return AV_ERR_OK;
+}
+
+OH_AVErrCode OH_VideoDecoder_PushInputData(struct OH_AVCodec *codec, uint32_t index, OH_AVCodecBufferAttr attr)
+{
+ CHECK_AND_RETURN_RET_LOG(codec != nullptr, AV_ERR_INVALID_VAL, "Codec is nullptr!");
+ CHECK_AND_RETURN_RET_LOG(codec->magic_ == AVMagic::AVCODEC_MAGIC_VIDEO_DECODER, AV_ERR_INVALID_VAL,
+ "Codec magic error!");
+
+ struct VideoDecoderObject *videoDecObj = reinterpret_cast(codec);
+ CHECK_AND_RETURN_RET_LOG(videoDecObj->videoDecoder_ != nullptr, AV_ERR_INVALID_VAL, "Video decoder is nullptr!");
+
+ struct AVCodecBufferInfo bufferInfo;
+ bufferInfo.presentationTimeUs = attr.pts;
+ bufferInfo.size = attr.size;
+ bufferInfo.offset = attr.offset;
+ enum AVCodecBufferFlag bufferFlag = static_cast(attr.flags);
+
+ int32_t ret = videoDecObj->videoDecoder_->QueueInputBuffer(index, bufferInfo, bufferFlag);
+ CHECK_AND_RETURN_RET_LOG(ret == AVCS_ERR_OK, AV_ERR_OPERATE_NOT_PERMIT, "Video decoder push input data failed!");
+ if (bufferFlag == AVCODEC_BUFFER_FLAG_EOS) {
+ videoDecObj->isEOS_.store(true);
+ }
+
+ return AV_ERR_OK;
+}
+
+OH_AVFormat *OH_VideoDecoder_GetOutputDescription(struct OH_AVCodec *codec)
+{
+ CHECK_AND_RETURN_RET_LOG(codec != nullptr, nullptr, "Codec is nullptr!");
+ CHECK_AND_RETURN_RET_LOG(codec->magic_ == AVMagic::AVCODEC_MAGIC_VIDEO_DECODER, nullptr, "Codec magic error!");
+
+ struct VideoDecoderObject *videoDecObj = reinterpret_cast(codec);
+ CHECK_AND_RETURN_RET_LOG(videoDecObj->videoDecoder_ != nullptr, nullptr, "Video decoder is nullptr!");
+
+ Format format;
+ int32_t ret = videoDecObj->videoDecoder_->GetOutputFormat(format);
+ CHECK_AND_RETURN_RET_LOG(ret == AVCS_ERR_OK, nullptr, "Video decoder get output description failed!");
+
+ OH_AVFormat *avFormat = OH_AVFormat_Create();
+ CHECK_AND_RETURN_RET_LOG(avFormat != nullptr, nullptr, "Video decoder get output description failed!");
+ avFormat->format_ = format;
+
+ return avFormat;
+}
+
+OH_AVErrCode OH_VideoDecoder_RenderOutputData(struct OH_AVCodec *codec, uint32_t index)
+{
+ CHECK_AND_RETURN_RET_LOG(codec != nullptr, AV_ERR_INVALID_VAL, "Codec is nullptr!");
+ CHECK_AND_RETURN_RET_LOG(codec->magic_ == AVMagic::AVCODEC_MAGIC_VIDEO_DECODER, AV_ERR_INVALID_VAL,
+ "Codec magic error!");
+
+ struct VideoDecoderObject *videoDecObj = reinterpret_cast(codec);
+ CHECK_AND_RETURN_RET_LOG(videoDecObj->videoDecoder_ != nullptr, AV_ERR_INVALID_VAL, "Video decoder is nullptr!");
+
+ int32_t ret = videoDecObj->videoDecoder_->ReleaseOutputBuffer(index, true);
+ CHECK_AND_RETURN_RET_LOG(ret == AVCS_ERR_OK, AV_ERR_OPERATE_NOT_PERMIT, "Video decoder render output data failed!");
+
+ return AV_ERR_OK;
+}
+
+OH_AVErrCode OH_VideoDecoder_FreeOutputData(struct OH_AVCodec *codec, uint32_t index)
+{
+ AVCODEC_LOGD("In OH_VideoDecoder_FreeOutputData");
+ CHECK_AND_RETURN_RET_LOG(codec != nullptr, AV_ERR_INVALID_VAL, "Codec is nullptr!");
+ CHECK_AND_RETURN_RET_LOG(codec->magic_ == AVMagic::AVCODEC_MAGIC_VIDEO_DECODER, AV_ERR_INVALID_VAL,
+ "Codec magic error!");
+
+ struct VideoDecoderObject *videoDecObj = reinterpret_cast(codec);
+ CHECK_AND_RETURN_RET_LOG(videoDecObj->videoDecoder_ != nullptr, AV_ERR_INVALID_VAL, "Video decoder is nullptr!");
+
+ int32_t ret = videoDecObj->videoDecoder_->ReleaseOutputBuffer(index, false);
+ CHECK_AND_RETURN_RET_LOG(ret == AVCS_ERR_OK, AV_ERR_OPERATE_NOT_PERMIT, "Video decoder free output data failed!");
+
+ return AV_ERR_OK;
+}
+
+OH_AVErrCode OH_VideoDecoder_SetParameter(struct OH_AVCodec *codec, struct OH_AVFormat *format)
+{
+ CHECK_AND_RETURN_RET_LOG(codec != nullptr, AV_ERR_INVALID_VAL, "Codec is nullptr!");
+ CHECK_AND_RETURN_RET_LOG(codec->magic_ == AVMagic::AVCODEC_MAGIC_VIDEO_DECODER, AV_ERR_INVALID_VAL,
+ "Codec magic error!");
+ CHECK_AND_RETURN_RET_LOG(format != nullptr, AV_ERR_INVALID_VAL, "Format is nullptr!");
+ CHECK_AND_RETURN_RET_LOG(format->magic_ == AVMagic::AVCODEC_MAGIC_FORMAT, AV_ERR_INVALID_VAL,
+ "Format magic error!");
+
+ struct VideoDecoderObject *videoDecObj = reinterpret_cast(codec);
+ CHECK_AND_RETURN_RET_LOG(videoDecObj->videoDecoder_ != nullptr, AV_ERR_INVALID_VAL, "Video decoder is nullptr!");
+
+ int32_t ret = videoDecObj->videoDecoder_->SetParameter(format->format_);
+ CHECK_AND_RETURN_RET_LOG(ret == AVCS_ERR_OK, AV_ERR_OPERATE_NOT_PERMIT, "Video decoder set parameter failed!");
+
+ return AV_ERR_OK;
+}
+
+OH_AVErrCode OH_VideoDecoder_SetCallback(struct OH_AVCodec *codec, struct OH_AVCodecAsyncCallback callback,
+ void *userData)
+{
+ CHECK_AND_RETURN_RET_LOG(codec != nullptr, AV_ERR_INVALID_VAL, "Codec is nullptr!");
+ CHECK_AND_RETURN_RET_LOG(codec->magic_ == AVMagic::AVCODEC_MAGIC_VIDEO_DECODER, AV_ERR_INVALID_VAL,
+ "Codec magic error!");
+
+ struct VideoDecoderObject *videoDecObj = reinterpret_cast(codec);
+ CHECK_AND_RETURN_RET_LOG(videoDecObj->videoDecoder_ != nullptr, AV_ERR_INVALID_VAL, "Video decoder is nullptr!");
+
+ videoDecObj->callback_ = std::make_shared(codec, callback, userData);
+
+ int32_t ret = videoDecObj->videoDecoder_->SetCallback(videoDecObj->callback_);
+ CHECK_AND_RETURN_RET_LOG(ret == AVCS_ERR_OK, AV_ERR_OPERATE_NOT_PERMIT, "Video decoder set callback failed!");
+
+ return AV_ERR_OK;
+}
+
+OH_AVErrCode OH_VideoDecoder_IsValid(OH_AVCodec *codec, bool *isVaild)
+{
+ (void)codec;
+ return AV_ERR_UNSUPPORT;
+}
\ No newline at end of file
diff --git a/frameworks/native/capi/avcodec/native_video_encoder.cpp b/frameworks/native/capi/avcodec/native_video_encoder.cpp
new file mode 100644
index 0000000000000000000000000000000000000000..ebff642a46e93108d201fe2f7df38722c96d9313
--- /dev/null
+++ b/frameworks/native/capi/avcodec/native_video_encoder.cpp
@@ -0,0 +1,448 @@
+/*
+ * Copyright (C) 2022 Huawei Device Co., Ltd.
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include
+#include
+#include "native_avcodec_base.h"
+#include "native_avcodec_videoencoder.h"
+#include "native_avmagic.h"
+#include "native_window.h"
+#include "avcodec_video_encoder.h"
+#include "avsharedmemory.h"
+#include "avcodec_log.h"
+#include "avcodec_errors.h"
+
+namespace {
+constexpr OHOS::HiviewDFX::HiLogLabel LABEL = {LOG_CORE, LOG_DOMAIN, "NativeVideoEncoder"};
+}
+
+using namespace OHOS::Media;
+class NativeVideoEncoderCallback;
+
+struct VideoEncoderObject : public OH_AVCodec {
+ explicit VideoEncoderObject(const std::shared_ptr &encoder)
+ : OH_AVCodec(AVMagic::AVCODEC_MAGIC_VIDEO_ENCODER), videoEncoder_(encoder) {}
+ ~VideoEncoderObject() = default;
+
+ const std::shared_ptr videoEncoder_;
+ std::list> memoryObjList_;
+ OHOS::sptr outputFormat_ = nullptr;
+ std::shared_ptr callback_ = nullptr;
+ std::atomic isFlushing_ = false;
+ std::atomic isStop_ = false;
+ std::atomic isEOS_ = false;
+};
+
+class NativeVideoEncoderCallback : public AVCodecCallback {
+public:
+ NativeVideoEncoderCallback(OH_AVCodec *codec, struct OH_AVCodecAsyncCallback cb, void *userData)
+ : codec_(codec), callback_(cb), userData_(userData) {}
+ virtual ~NativeVideoEncoderCallback() = default;
+
+ void OnError(AVCodecErrorType errorType, int32_t errorCode) override
+ {
+ std::unique_lock lock(mutex_);
+ (void)errorType;
+
+ CHECK_AND_RETURN_LOG(codec_ != nullptr, "Codec is nullptr");
+ CHECK_AND_RETURN_LOG(callback_.onError != nullptr, "Callback is nullptr");
+ int32_t extErr = AVCSErrorToOHAVErrCode(static_cast(errorCode));
+ callback_.onError(codec_, extErr, userData_);
+ }
+
+ void OnOutputFormatChanged(const Format &format) override
+ {
+ std::unique_lock lock(mutex_);
+
+ CHECK_AND_RETURN_LOG(codec_ != nullptr, "Codec is nullptr");
+ CHECK_AND_RETURN_LOG(callback_.onStreamChanged != nullptr, "Callback is nullptr");
+ OHOS::sptr object = new(std::nothrow) OH_AVFormat(format);
+ // The object lifecycle is controlled by the current function stack
+ callback_.onStreamChanged(codec_, reinterpret_cast(object.GetRefPtr()), userData_);
+ }
+
+ void OnInputBufferAvailable(uint32_t index) override
+ {
+ std::unique_lock lock(mutex_);
+
+ CHECK_AND_RETURN_LOG(codec_ != nullptr, "Codec is nullptr");
+ CHECK_AND_RETURN_LOG(callback_.onNeedInputData != nullptr, "Callback is nullptr");
+
+ struct VideoEncoderObject *videoEncObj = reinterpret_cast(codec_);
+ CHECK_AND_RETURN_LOG(videoEncObj->videoEncoder_ != nullptr, "Context video decoder is nullptr!");
+
+ if (videoEncObj->isFlushing_.load() || videoEncObj->isStop_.load() || videoEncObj->isEOS_.load()) {
+ AVCODEC_LOGD("At flush, eos or stop, no buffer available");
+ return;
+ }
+ callback_.onNeedInputData(codec_, index, nullptr, userData_);
+ }
+
+ void OnOutputBufferAvailable(uint32_t index, AVCodecBufferInfo info, AVCodecBufferFlag flag) override
+ {
+ std::unique_lock lock(mutex_);
+
+ CHECK_AND_RETURN_LOG(codec_ != nullptr, "Codec is nullptr");
+ CHECK_AND_RETURN_LOG(callback_.onNeedOutputData != nullptr, "Callback is nullptr");
+
+ struct VideoEncoderObject *videoEncObj = reinterpret_cast