From 7969fde3bcdfe979cb9f3a308510f638b2831605 Mon Sep 17 00:00:00 2001 From: rsp4jack Date: Sat, 2 Nov 2024 21:41:13 +0800 Subject: [PATCH] Fix compatibility with the old interface --- arm-wt-22k/src/hostmm_ng.c | 53 +++++++++++++++++++++++++++++++++++++- example/sonivoxrender.c | 11 +++++--- 2 files changed, 59 insertions(+), 5 deletions(-) diff --git a/arm-wt-22k/src/hostmm_ng.c b/arm-wt-22k/src/hostmm_ng.c index 5ac999a1..82824a87 100644 --- a/arm-wt-22k/src/hostmm_ng.c +++ b/arm-wt-22k/src/hostmm_ng.c @@ -53,8 +53,13 @@ static const union { // --- end typedef struct eas_hw_file_tag { - FILE* handle; + void* handle; EAS_BOOL own; + + // legacy interface for compatibility + int pos; + int (*readAt)(void *handle, void *buf, int offset, int size); + int (*size)(void *handle); } EAS_HW_FILE; EAS_RESULT EAS_HWInit(EAS_HW_DATA_HANDLE* pHWInstData) @@ -100,6 +105,8 @@ EAS_RESULT EAS_HWOpenFile(EAS_HW_DATA_HANDLE hwInstData, EAS_FILE_LOCATOR locato *pFile = malloc(sizeof(EAS_HW_FILE)); (*pFile)->handle = locator->handle; (*pFile)->own = EAS_FALSE; + (*pFile)->readAt = locator->readAt; + (*pFile)->size = locator->size; return EAS_SUCCESS; } @@ -110,6 +117,18 @@ EAS_RESULT EAS_HWReadFile(EAS_HW_DATA_HANDLE hwInstData, EAS_FILE_HANDLE file, v return EAS_ERROR_INVALID_HANDLE; } + if (file->readAt != NULL) { + int count = file->readAt(file->handle, pBuffer, file->pos, n); + file->pos += count; + if (pBytesRead != NULL) { + *pBytesRead = count; + } + if (count < n) { + return EAS_EOF; + } + return EAS_SUCCESS; + } + size_t count = fread(pBuffer, 1, n, file->handle); if (pBytesRead != NULL) { *pBytesRead = count; @@ -172,6 +191,13 @@ EAS_RESULT EAS_HWFilePos(EAS_HW_DATA_HANDLE hwInstData, EAS_FILE_HANDLE file, EA return EAS_ERROR_INVALID_HANDLE; } + if (file->readAt != NULL) { + if (pPosition != NULL) { + *pPosition = file->pos; + } + return EAS_SUCCESS; + } + long pos = ftell(file->handle); if (pPosition != NULL) { *pPosition = pos; @@ -186,6 +212,11 @@ EAS_RESULT EAS_HWFileSeek(EAS_HW_DATA_HANDLE hwInstData, EAS_FILE_HANDLE file, E return EAS_ERROR_INVALID_HANDLE; } + if (file->readAt != NULL) { + file->pos = position; + return EAS_SUCCESS; + } + /* validate new position */ if (fseek(file->handle, position, SEEK_SET) != 0) { return EAS_ERROR_FILE_SEEK; @@ -201,6 +232,11 @@ EAS_RESULT EAS_HWFileSeekOfs(EAS_HW_DATA_HANDLE hwInstData, EAS_FILE_HANDLE file return EAS_ERROR_INVALID_HANDLE; } + if (file->readAt != NULL) { + file->pos = position; + return EAS_SUCCESS; + } + if (fseek(file->handle, position, SEEK_CUR) != 0) { return EAS_ERROR_FILE_SEEK; } @@ -211,6 +247,17 @@ EAS_RESULT EAS_HWFileSeekOfs(EAS_HW_DATA_HANDLE hwInstData, EAS_FILE_HANDLE file EAS_RESULT EAS_HWDupHandle(EAS_HW_DATA_HANDLE hwInstData, EAS_FILE_HANDLE file, EAS_FILE_HANDLE* pDupFile) { *pDupFile = NULL; + + if (file->readAt != NULL) { + *pDupFile = malloc(sizeof(EAS_HW_FILE)); + (*pDupFile)->handle = file->handle; + (*pDupFile)->own = EAS_FALSE; + (*pDupFile)->readAt = file->readAt; + (*pDupFile)->size = file->size; + (*pDupFile)->pos = file->pos; + return EAS_SUCCESS; + } + #if defined(__linux__) // todo: implement for other platforms char filePath[PATH_MAX]; char linkPath[PATH_MAX]; @@ -292,6 +339,10 @@ EAS_RESULT EAS_HWCloseFile(EAS_HW_DATA_HANDLE hwInstData, EAS_FILE_HANDLE file) return EAS_ERROR_INVALID_HANDLE; } + if (file->readAt != NULL) { + return EAS_SUCCESS; + } + if (file->own) { if (fclose(file->handle) != 0) { return EAS_ERROR_INVALID_HANDLE; diff --git a/example/sonivoxrender.c b/example/sonivoxrender.c index 6f53b343..e50a040e 100644 --- a/example/sonivoxrender.c +++ b/example/sonivoxrender.c @@ -106,6 +106,7 @@ int initializeLibrary(void) if (dls_path != NULL) { EAS_FILE mDLSFile; + memset(&mDLSFile, 0, sizeof(EAS_FILE)); mDLSFile.handle = fopen(dls_path, "rb"); if (mDLSFile.handle == NULL) { @@ -114,8 +115,8 @@ int initializeLibrary(void) goto cleanup; } - mDLSFile.readAt = Read; - mDLSFile.size = Size; + // mDLSFile.readAt = Read; + // mDLSFile.size = Size; result = EAS_LoadDLSCollection(mEASDataHandle, NULL, &mDLSFile); fclose(mDLSFile.handle); @@ -210,6 +211,8 @@ int renderFile(const char *fileName) int ok = EXIT_SUCCESS; + memset(&mEasFile, 0, sizeof(EAS_FILE)); + mEasFile.handle = fopen(fileName, "rb"); if (mEasFile.handle == NULL) { fprintf(stderr, "Failed to open %s. error: %s\n", fileName, strerror(errno)); @@ -217,8 +220,8 @@ int renderFile(const char *fileName) return ok; } - mEasFile.readAt = Read; - mEasFile.size = Size; + // mEasFile.readAt = Read; + // mEasFile.size = Size; EAS_RESULT result = EAS_OpenFile(mEASDataHandle, &mEasFile, &mEASStreamHandle); if (result != EAS_SUCCESS) {