From 4a84cd1508c01684d9b8cabc215c76488ac6a876 Mon Sep 17 00:00:00 2001 From: lvjintao Date: Wed, 20 Sep 2023 09:31:13 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BB=A3=E7=A0=81=E6=8F=90=E4=BA=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: lvjintao --- bundle.json | 11 +++ .../native/capi/common/native_avformat.cpp | 76 +++++++++++++++++++ .../native/capi/common/native_avmagic.h | 6 ++ .../native/capi/common/native_avmemory.cpp | 36 ++++++++- interfaces/inner_api/native/format.h | 8 ++ interfaces/kits/c/native_avformat.h | 75 ++++++++++++++++++ interfaces/kits/c/native_avmemory.h | 11 +++ services/utils/format.cpp | 11 +++ 8 files changed, 230 insertions(+), 4 deletions(-) diff --git a/bundle.json b/bundle.json index b327caf91..cb12b6df3 100644 --- a/bundle.json +++ b/bundle.json @@ -90,6 +90,17 @@ ], "header_base": "//foundation/multimedia/av_codec/interfaces/inner_api/native" } + }, + { + "type": "so", + "name": "//foundation/multimedia/av_codec/interfaces/kits/c:native_media_core", + "header": { + "header_files": [ + "native_avformat.h", + "native_avmemory.h" + ], + "header_base": "//foundation/multimedia/av_codec/interfaces/kits/c" + } } ], "test": [ diff --git a/frameworks/native/capi/common/native_avformat.cpp b/frameworks/native/capi/common/native_avformat.cpp index 897b30d2e..e8c86fb21 100644 --- a/frameworks/native/capi/common/native_avformat.cpp +++ b/frameworks/native/capi/common/native_avformat.cpp @@ -47,6 +47,15 @@ OH_AVFormat::~OH_AVFormat() free(dumpInfo_); dumpInfo_ = nullptr; } + + if (keysSize_ != 0) { + for (int i = 0; i < keysSize_; i++) { + free(keys_[i]); + } + + free(keys_); + keys_ = nullptr; + } } struct OH_AVFormat *OH_AVFormat_Create(void) @@ -54,6 +63,17 @@ struct OH_AVFormat *OH_AVFormat_Create(void) return new(std::nothrow) OH_AVFormat(); } +struct OH_AVFormat *OH_AVFormat_CreateArray(int32_t size) +{ + uint32_t keyLength = size > MAX_DUMP_LENGTH ? MAX_DUMP_LENGTH : size; + return new(std::nothrow) OH_AVFormat[keyLength]; +} + +struct OH_AVFormat *OH_AVFormat_GetFormatFromArray(OH_AVFormat * avformat, int32_t index) +{ + return &avformat[index]; +} + struct OH_AVFormat *OH_AVFormat_CreateAudioFormat(const char *mimeType, int32_t sampleRate, int32_t channelCount) @@ -85,6 +105,11 @@ void OH_AVFormat_Destroy(struct OH_AVFormat *format) delete format; } +void OH_AVFormat_DestroyArray(struct OH_AVFormat *format) +{ + delete []format; +} + bool OH_AVFormat_Copy(struct OH_AVFormat *to, struct OH_AVFormat *from) { CHECK_AND_RETURN_RET_LOG(to != nullptr, false, "to format is nullptr!"); @@ -237,6 +262,14 @@ bool OH_AVFormat_GetBuffer(struct OH_AVFormat *format, const char *key, uint8_t return format->format_.GetBuffer(key, addr, *size); } +OH_AVFormatDataType OH_AVFormat_GetValueType(struct OH_AVFormat *format, const char *key) +{ + CHECK_AND_RETURN_RET_LOG(format != nullptr, AV_FORMAT_TYPE_NONE, "input format is nullptr!"); + CHECK_AND_RETURN_RET_LOG(format->magic_ == AVMagic::AVCODEC_MAGIC_FORMAT, AV_FORMAT_TYPE_NONE, "magic error!"); + CHECK_AND_RETURN_RET_LOG(key != nullptr, AV_FORMAT_TYPE_NONE, "key is nullptr!"); + return static_cast(format->format_.GetValueType(key)); +} + const char *OH_AVFormat_DumpInfo(struct OH_AVFormat *format) { CHECK_AND_RETURN_RET_LOG(format != nullptr, nullptr, "input format is nullptr!"); @@ -258,3 +291,46 @@ const char *OH_AVFormat_DumpInfo(struct OH_AVFormat *format) } return format->dumpInfo_; } + +void OH_AVFormat_FreeFormatKeys(struct OH_AVFormat *format) +{ + if (format->keysSize_ != 0) { + for (int i = 0; i < format->keysSize_; i++) { + free(format->keys_[i]); + } + + free(format->keys_); + format->keys_ = nullptr; + format->keysSize_ = 0; + } +} + +char **OH_AVFormat_GetFormatKeys(struct OH_AVFormat *format, int32_t *keysSize) +{ + CHECK_AND_RETURN_RET_LOG(format != nullptr, nullptr, "input format is nullptr!"); + OH_AVFormat_FreeFormatKeys(format); + std::vector keys = format->format_.GetFormatKeys(); + if (keys.size() == 0) { + return nullptr; + } + + *keysSize = keys.size(); + format->keys_ = static_cast(malloc((*keysSize) * sizeof(char*))); + CHECK_AND_RETURN_RET_LOG(format->keys_ != nullptr, nullptr, "malloc keys_ info nullptr!"); + format->keysSize_ = *keysSize; + for (int i = 0; i < *keysSize; i++) { + uint32_t keyLength = keys[i].size() > MAX_DUMP_LENGTH ? MAX_DUMP_LENGTH : keys[i].size(); + format->keys_[i] = static_cast(malloc((keyLength + 1) * sizeof(char))); + } + + for (int i = 0; i < *keysSize; i++) { + uint32_t keyLength = keys[i].size() > MAX_DUMP_LENGTH ? MAX_DUMP_LENGTH : keys[i].size(); + if (strcpy_s(format->keys_[i], keyLength + 1, keys[i].c_str()) != EOK) { + AVCODEC_LOGE("Failed to strcpy_s"); + OH_AVFormat_FreeFormatKeys(format); + break; + } + } + + return format->keys_; +} \ No newline at end of file diff --git a/frameworks/native/capi/common/native_avmagic.h b/frameworks/native/capi/common/native_avmagic.h index 9e11b6672..e1eae3f2c 100644 --- a/frameworks/native/capi/common/native_avmagic.h +++ b/frameworks/native/capi/common/native_avmagic.h @@ -50,14 +50,20 @@ struct OH_AVFormat : public AVObjectMagic { OHOS::MediaAVCodec::Format format_; char *outString_ = nullptr; char *dumpInfo_ = nullptr; + char **keys_ = nullptr; + int32_t keysSize_ = 0; }; struct OH_AVMemory : public AVObjectMagic { + OH_AVMemory(); explicit OH_AVMemory(const std::shared_ptr &mem); ~OH_AVMemory() override; bool IsEqualMemory(const std::shared_ptr &mem); const std::shared_ptr memory_; + uint8_t *addr; + int32_t size; bool isUserCreated = false; + bool isCreatedByAddr = false; }; struct OH_AVCodec : public AVObjectMagic { diff --git a/frameworks/native/capi/common/native_avmemory.cpp b/frameworks/native/capi/common/native_avmemory.cpp index 70fd3bb94..8c4a1f186 100644 --- a/frameworks/native/capi/common/native_avmemory.cpp +++ b/frameworks/native/capi/common/native_avmemory.cpp @@ -25,6 +25,11 @@ constexpr OHOS::HiviewDFX::HiLogLabel LABEL = {LOG_CORE, LOG_DOMAIN, "OH_AVMemor using namespace OHOS::MediaAVCodec; +OH_AVMemory::OH_AVMemory() + : AVObjectMagic(AVMagic::AVCODEC_MAGIC_SHARED_MEMORY) +{ +} + OH_AVMemory::OH_AVMemory(const std::shared_ptr &mem) : AVObjectMagic(AVMagic::AVCODEC_MAGIC_SHARED_MEMORY), memory_(mem) { @@ -54,20 +59,43 @@ struct OH_AVMemory *OH_AVMemory_Create(int32_t size) return mem; } + +struct OH_AVMemory *OH_AVMemory_CreateByAddr(uint8_t *addr, int32_t size) +{ + CHECK_AND_RETURN_RET_LOG(addr != nullptr, nullptr, "addr is nullptr!"); + CHECK_AND_RETURN_RET_LOG(size >= 0, nullptr, "size %{public}d is error!", size); + struct OH_AVMemory *mem = new(std::nothrow) OH_AVMemory(); + CHECK_AND_RETURN_RET_LOG(mem != nullptr, nullptr, "failed to new OH_AVMemory"); + mem->addr = addr; + mem->size = size; + mem->isCreatedByAddr = true; + mem->isUserCreated = true; + + return mem; +} + uint8_t *OH_AVMemory_GetAddr(struct OH_AVMemory *mem) { CHECK_AND_RETURN_RET_LOG(mem != nullptr, nullptr, "input mem is nullptr!"); CHECK_AND_RETURN_RET_LOG(mem->magic_ == AVMagic::AVCODEC_MAGIC_SHARED_MEMORY, nullptr, "magic error!"); - CHECK_AND_RETURN_RET_LOG(mem->memory_ != nullptr, nullptr, "memory is nullptr!"); - return mem->memory_->GetBase(); + if (mem->isCreatedByAddr) { + return mem->addr; + } else { + CHECK_AND_RETURN_RET_LOG(mem->memory_ != nullptr, nullptr, "memory is nullptr!"); + return mem->memory_->GetBase(); + } } int32_t OH_AVMemory_GetSize(struct OH_AVMemory *mem) { CHECK_AND_RETURN_RET_LOG(mem != nullptr, -1, "input mem is nullptr!"); CHECK_AND_RETURN_RET_LOG(mem->magic_ == AVMagic::AVCODEC_MAGIC_SHARED_MEMORY, -1, "magic error!"); - CHECK_AND_RETURN_RET_LOG(mem->memory_ != nullptr, -1, "memory is nullptr!"); - return mem->memory_->GetSize(); + if (mem->isCreatedByAddr) { + return mem->size; + } else { + CHECK_AND_RETURN_RET_LOG(mem->memory_ != nullptr, -1, "memory is nullptr!"); + return mem->memory_->GetSize(); + } } OH_AVErrCode OH_AVMemory_Destroy(struct OH_AVMemory *mem) diff --git a/interfaces/inner_api/native/format.h b/interfaces/inner_api/native/format.h index b723379db..50a463e29 100644 --- a/interfaces/inner_api/native/format.h +++ b/interfaces/inner_api/native/format.h @@ -247,6 +247,14 @@ public: */ std::string Stringify() const; + /** + * @brief get all format keys. + * + * @return Returns a vector. + * @since 1.0 + * @version 1.0 + */ + std::vector GetFormatKeys() const; private: FormatDataMap formatMap_; }; diff --git a/interfaces/kits/c/native_avformat.h b/interfaces/kits/c/native_avformat.h index 4335cee33..4bba157b4 100644 --- a/interfaces/kits/c/native_avformat.h +++ b/interfaces/kits/c/native_avformat.h @@ -55,6 +55,29 @@ typedef enum OH_AVPixelFormat { AV_PIXEL_FORMAT_RGBA = 5, } OH_AVPixelFormat; +/** + * @brief key vaLue type + * @syscap SystemCapability.Multimedia.Media.Core + * @since 9 + * @version 1.0 + */ +typedef enum OH_AVFormatDataType { + /* None */ + AV_FORMAT_TYPE_NONE, + /* Int32 */ + AV_FORMAT_TYPE_INT32, + /* Int64 */ + AV_FORMAT_TYPE_INT64, + /* Float */ + AV_FORMAT_TYPE_FLOAT, + /* Double */ + AV_FORMAT_TYPE_DOUBLE, + /* String */ + AV_FORMAT_TYPE_STRING, + /* Addr */ + AV_FORMAT_TYPE_ADDR, +} OH_AVFormatDataType; + /** * @briefCreate an OH_AVFormat handle pointer to read and write data * @syscap SystemCapability.Multimedia.Media.Core @@ -64,6 +87,26 @@ typedef enum OH_AVPixelFormat { */ struct OH_AVFormat *OH_AVFormat_Create(void); +/** + * @briefCreate an OH_AVFormat array handle pointer to read and write data + * @syscap SystemCapability.Multimedia.Media.Core + * @param size the array length + * @return Returns a pointer to an OH_AVFormat array instance + * @since 9 + * @version 1.0 + */ +struct OH_AVFormat *OH_AVFormat_CreateArray(int32_t size); + +/** + * @brief Get an OH_AVFormat handle pointer to read and write data from array + * @syscap SystemCapability.Multimedia.Media.Core + * @param index the array index + * @return Returns a pointer to an OH_AVFormat instance + * @since 9 + * @version 1.0 + */ +struct OH_AVFormat *OH_AVFormat_GetFormatFromArray(OH_AVFormat *avformat, int32_t index); + /** * @briefCreate an audio OH_AVFormat handle pointer to read and write data * @syscap SystemCapability.Multimedia.Media.Core @@ -102,6 +145,16 @@ struct OH_AVFormat *OH_AVFormat_CreateVideoFormat(const char *mimeType, */ void OH_AVFormat_Destroy(struct OH_AVFormat *format); +/** + * @brief Destroy the specified OH_AVFormat Array handle resource + * @syscap SystemCapability.Multimedia.Media.Core + * @param format pointer to an OH_AVFormat instance + * @return void + * @since 9 + * @version 1.0 + */ +void OH_AVFormat_DestroyArray(struct OH_AVFormat *format); + /** * @brief Copy OH_AVFormat handle resource * @syscap SystemCapability.Multimedia.Media.Core @@ -261,6 +314,17 @@ bool OH_AVFormat_GetStringValue(struct OH_AVFormat *format, const char *key, con */ bool OH_AVFormat_GetBuffer(struct OH_AVFormat *format, const char *key, uint8_t **addr, size_t *size); +/** + * @brief Get the value type for the key if the key exists in this OH_AVFormat. + * @syscap SystemCapability.Multimedia.Media.Core + * @param format pointer to an OH_AVFormat instance + * @param key the name of key + * @return The return value is TRUE for success, FALSE for failure + * @since 9 + * @version 1.0 + */ +OH_AVFormatDataType OH_AVFormat_GetValueType(struct OH_AVFormat *format, const char *key); + /** * @brief Output the information contained in OH_AVFormat as a string. * @syscap SystemCapability.Multimedia.Media.Core @@ -271,6 +335,17 @@ bool OH_AVFormat_GetBuffer(struct OH_AVFormat *format, const char *key, uint8_t */ const char *OH_AVFormat_DumpInfo(struct OH_AVFormat *format); +/** + * @brief Output the information contained in OH_AVFormat keys as a string array. + * @syscap SystemCapability.Multimedia.Media.Core + * @param format pointer to an OH_AVFormat instance + * @param keySize get the string array size. + * @return Returns a string array + * @since 9 + * @version 1.0 + */ +char **OH_AVFormat_GetFormatKeys(struct OH_AVFormat *format, int32_t *keysSize); + #ifdef __cplusplus } #endif diff --git a/interfaces/kits/c/native_avmemory.h b/interfaces/kits/c/native_avmemory.h index a165d290a..b9a9336dc 100644 --- a/interfaces/kits/c/native_avmemory.h +++ b/interfaces/kits/c/native_avmemory.h @@ -34,6 +34,17 @@ typedef struct OH_AVMemory OH_AVMemory; */ OH_AVMemory *OH_AVMemory_Create(int32_t size); +/** + * @brief Create an OH_AVMemory instance + * @syscap SystemCapability.Multimedia.Media.Core + * @param size the memory's address. + * @param size the memory's size, bytes. + * @return Returns a pointer to an OH_AVMemory instance, needs to be freed by OH_AVMemory_Destroy. + * @since 10 + */ +OH_AVMemory *OH_AVMemory_CreateByAddr(uint8_t *addr, int32_t size); + + /** * @brief Get the memory's virtual address * @syscap SystemCapability.Multimedia.Media.Core diff --git a/services/utils/format.cpp b/services/utils/format.cpp index 2bac1ce9f..bc8a58ba9 100644 --- a/services/utils/format.cpp +++ b/services/utils/format.cpp @@ -322,5 +322,16 @@ std::string Format::Stringify() const } return outString; } + + +std::vector Format::GetFormatKeys() const +{ + std::vector outString; + for (auto iter = formatMap_.begin(); iter != formatMap_.end(); iter++) { + outString.push_back(iter->first); + } + + return outString; +} } // namespace MediaAVCodec } // namespace OHOS \ No newline at end of file -- Gitee