示例#1
0
/**
 ************************************************************************
 * @brief      This function gets the file URL (associated to the context).
 * @note
 * @param      context: (IN) Context of the core file reader
 * @param      url: (OUT) The buffer containing the URL (allocated by
 *             M4OSA_fileCommonGetURL)
 * @return     M4NO_ERROR: there is no error
 * @return     M4ERR_PARAMETER: at least one parameter is NULL
 * @return     M4ERR_BAD_CONTEXT: provided context is not a valid one
 * @return     M4ERR_ALLOC: there is no more memory available
 ************************************************************************
*/
M4OSA_ERR M4OSA_fileCommonGetURL(M4OSA_Context pContext, M4OSA_Char** pUrl)
{
    M4OSA_FileContext* pFileContext = pContext;
    M4OSA_UInt32    uiLength;

    M4OSA_TRACE3_2("M4OSA_fileCommonGetURL\tM4OSA_Context 0x%x\tM4OSA_Char** 0x%x",
                    pContext, pUrl);

    M4OSA_DEBUG_IF2(M4OSA_NULL == pContext,    M4ERR_PARAMETER,
                              "M4OSA_fileCommonGetURL: pContext is M4OSA_NULL");
    M4OSA_DEBUG_IF2(M4OSA_NULL == pUrl,    M4ERR_PARAMETER,
                                  "M4OSA_fileCommonGetURL: pUrl is M4OSA_NULL");

    uiLength = strlen((const char *)pFileContext->url_name)+1;

    /* Allocate the memory to store the url_name */
    *pUrl = (M4OSA_Char*)M4OSA_32bitAlignedMalloc(uiLength, M4OSA_FILE_COMMON,
                                    (M4OSA_Char*)"M4OSA_fileCommonGetURL: url");
    if(M4OSA_NULL == *pUrl)
    {
        M4OSA_DEBUG(M4ERR_ALLOC, "M4OSA_fileCommonGetURL");
        return M4ERR_ALLOC;
    }

    M4OSA_chrNCopy(*pUrl, pFileContext->url_name, uiLength);

    return M4NO_ERROR;
}
示例#2
0
/**
 ************************************************************************
 * @brief      This function gets a string containing the file name associated
 *             to the input URL.
 * @note       The user should not forget to delete the output string using
 *             M4OSA_strDestroy
 * @param      pUrl:            (IN) The buffer containing the URL
 * @param      pFileName:    (OUT) The string containing the URL. It is
 *                            allocated inside this function
 * @return     M4NO_ERROR: there is no error
 * @return     M4ERR_NOT_IMPLEMENTED: the URL does not match with the supported
 *             file
 * @return     M4ERR_ALLOC: there is no more memory available
 ************************************************************************
*/
M4OSA_ERR M4OSA_fileCommonGetFilename(M4OSA_Char* pUrl, M4OSA_Char** pFileName)
{
    M4OSA_Int32 i            = 0;
    M4OSA_Int32 iUrlLen        = 0;
    M4OSA_Int32 FileNameLen = 0;

    M4OSA_Char* ptrUrl        = M4OSA_NULL;
    M4OSA_Char* ptrFilename    = M4OSA_NULL;

    M4OSA_TRACE3_2("M4OSA_fileCommonGetURL\tM4OSA_Char* %s\tM4OSA_Char** 0x%x",
                                                               pUrl, pFileName);

    M4OSA_DEBUG_IF2(M4OSA_NULL == pUrl,    M4ERR_PARAMETER,
                             "M4OSA_fileCommonGetFilename: pUrl is M4OSA_NULL");
    M4OSA_DEBUG_IF2(M4OSA_NULL == pFileName,    M4ERR_PARAMETER,
                        "M4OSA_fileCommonGetFilename: pFileName is M4OSA_NULL");

    *pFileName = M4OSA_NULL;

    /*Parse URL*/
    iUrlLen = strlen((const char *)pUrl);
    for(i=iUrlLen-1; i>=0; i--)
    {
        if (pUrl[i] != '\\' && pUrl[i] != '/')
        {
            FileNameLen++;
        }
        else
        {
            break; /* find the beginning of the file name */
        }
    }

    ptrFilename = (M4OSA_Char*) M4OSA_32bitAlignedMalloc(FileNameLen+1, M4OSA_FILE_COMMON,
                    (M4OSA_Char*)"M4OSA_fileCommonGetFilename: Filename string");
    if (ptrFilename == M4OSA_NULL)
    {
        M4OSA_DEBUG(M4ERR_ALLOC, "M4OSA_fileCommonGetFilename");
        return M4ERR_ALLOC;
    }

    ptrUrl = pUrl + (iUrlLen - FileNameLen);
    M4OSA_chrNCopy(ptrFilename, ptrUrl, FileNameLen+1);

    *pFileName = ptrFilename;

    return M4NO_ERROR;
}
示例#3
0
/**
 ************************************************************************
 * @brief      This function opens the provided URL and returns its context.
 *             If an error occured, the context is set to NULL.
 * @param      core_id: (IN) Core ID of the caller (M4OSA_FILE_READER or M4OSA_FILE_WRITER)
 * @param      context: (OUT) Context of the core file reader
 * @param      url: (IN) URL of the input file
 * @param      fileModeAccess: (IN) File mode access
 * @return     M4NO_ERROR: there is no error
 * @return     M4ERR_PARAMETER: at least one parameter is NULL
 * @return     M4ERR_ALLOC: there is no more memory available
 * @return     M4ERR_NOT_IMPLEMENTED: the URL does not match with the supported
 *             file
 * @return     M4ERR_FILE_NOT_FOUND: the file cannot be found
 * @return     M4ERR_FILE_LOCKED: the file is locked by an other
 *             application/process
 * @return     M4ERR_FILE_BAD_MODE_ACCESS: the file mode access is not correct
 ************************************************************************
*/
M4OSA_ERR M4OSA_fileCommonOpen(M4OSA_UInt16 core_id, M4OSA_Context* pContext,
                               M4OSA_Char* pUrl, M4OSA_FileModeAccess fileModeAccess)
{

    M4OSA_Int32 i            = 0;
    M4OSA_Int32 iMode        = 0;
    M4OSA_Int32 iSize        = 0;
    M4OSA_Int32 iSavePos    = 0;

    M4OSA_Char  mode[4]            = "";
    M4OSA_Char* pReadString        = (M4OSA_Char*)"r";
    M4OSA_Char* pWriteString    = (M4OSA_Char*)"w";
    M4OSA_Char* pAppendString    = (M4OSA_Char*)"a";
    M4OSA_Char* pBinaryString    = (M4OSA_Char*)"b";
    M4OSA_Char* pPlusString        = (M4OSA_Char*)"+";

    M4OSA_ERR err = M4NO_ERROR;

    FILE* pFileHandler = M4OSA_NULL;
    M4OSA_FileContext *pFileContext    = M4OSA_NULL;


#ifdef UTF_CONVERSION
    /*FB: to test the UTF16->UTF8 conversion into Video Artist*/
    /*Convert the URL from UTF16 to UTF8*/
    M4OSA_Void* tempConversionBuf;
    M4OSA_UInt32 tempConversionSize = 1000;

    tempConversionBuf = (M4OSA_Char*)M4OSA_32bitAlignedMalloc(tempConversionSize +1, 0, "conversion buf");
    if(tempConversionBuf == M4OSA_NULL)
    {
        M4OSA_TRACE1_0("Error when allocating conversion buffer\n");
        return M4ERR_PARAMETER;
    }
    M4OSA_ToUTF8_OSAL(pUrl, tempConversionBuf, &tempConversionSize);
    ((M4OSA_Char*)tempConversionBuf)[tempConversionSize ] = '\0';

    printf("file open %s\n", tempConversionBuf);
#endif /*UTF CONVERSION*/

    M4OSA_TRACE3_4("M4OSA_fileCommonOpen\t\tM4OSA_UInt16 %d\tM4OSA_Context* 0x%x\t"
        "M4OSA_Char* %s\tfileModeAccess %d", core_id, pContext, pUrl, fileModeAccess);

    M4OSA_DEBUG_IF2(M4OSA_NULL == pContext,    M4ERR_PARAMETER,    "M4OSA_fileCommonOpen: pContext is M4OSA_NULL");
    M4OSA_DEBUG_IF2(M4OSA_NULL == pUrl,        M4ERR_PARAMETER,    "M4OSA_fileCommonOpen: pUrl  is M4OSA_NULL");
    M4OSA_DEBUG_IF2(0 == fileModeAccess,    M4ERR_PARAMETER,    "M4OSA_fileCommonOpen: fileModeAccess is 0");

    /* Read mode not set for the reader */
    M4OSA_DEBUG_IF1((M4OSA_FILE_READER == core_id) && !(fileModeAccess & M4OSA_kFileRead),
        M4ERR_FILE_BAD_MODE_ACCESS, "M4OSA_fileCommonOpen: M4OSA_kFileRead");

    /* Read mode not set for the reader */
    M4OSA_DEBUG_IF1((M4OSA_FILE_READER == core_id) && !(fileModeAccess & M4OSA_kFileRead),
        M4ERR_FILE_BAD_MODE_ACCESS, "M4OSA_fileCommonOpen: M4OSA_kFileRead");

    /* M4OSAfileReadOpen cannot be used with Write file mode access */
    M4OSA_DEBUG_IF1((M4OSA_FILE_READER == core_id) && (fileModeAccess & M4OSA_kFileWrite),
        M4ERR_FILE_BAD_MODE_ACCESS, "M4OSA_fileCommonOpen: M4OSA_kFileWrite");

    /* Append and Create flags cannot be used with Read */
    M4OSA_DEBUG_IF1((M4OSA_FILE_READER == core_id) && (fileModeAccess & M4OSA_kFileAppend),
        M4ERR_FILE_BAD_MODE_ACCESS, "M4OSA_fileCommonOpen: M4OSA_kFileAppend");

    M4OSA_DEBUG_IF1((M4OSA_FILE_READER == core_id) && (fileModeAccess & M4OSA_kFileCreate),
        M4ERR_FILE_BAD_MODE_ACCESS, "M4OSA_fileCommonOpen: M4OSA_kFileCreate");

    /* Write mode not set for the writer */
    M4OSA_DEBUG_IF1((M4OSA_FILE_WRITER == core_id) && !(fileModeAccess & M4OSA_kFileWrite),
        M4ERR_FILE_BAD_MODE_ACCESS, "M4OSA_fileCommonOpen: M4OSA_kFileWrite");

    /* Create flag necessary for opening file */
    if ((fileModeAccess & M4OSA_kFileRead) &&
        (fileModeAccess & M4OSA_kFileWrite)&&(fileModeAccess & M4OSA_kFileCreate))
    {
        strncat((char *)mode, (const char *)pWriteString, (size_t)1);
        strncat((char *)mode, (const char *)pPlusString, (size_t)1);
    }
    else
    {
        if(fileModeAccess & M4OSA_kFileAppend)
        {
            strncat((char *)mode, (const char *)pAppendString, (size_t)1);
        }
        else if(fileModeAccess & M4OSA_kFileRead)
        {
            strncat((char *)mode, (const char *)pReadString, (size_t)1);
        }
        else if(fileModeAccess & M4OSA_kFileWrite)
        {
            strncat((char *)mode, (const char *)pWriteString, (size_t)1);
        }

        if((fileModeAccess & M4OSA_kFileRead)&&(fileModeAccess & M4OSA_kFileWrite))
        {
            strncat((char *)mode,(const char *)pPlusString, (size_t)1);
        }
    }

    if(!(fileModeAccess & M4OSA_kFileIsTextMode))
    {
        strncat((char *)mode, (const char *)pBinaryString,(size_t)1);
    }

    /*Open the file*/

#ifdef UTF_CONVERSION
    /*Open the converted path*/
    pFileHandler = fopen((const char *)tempConversionBuf, (const char *)mode);
    /*Free the temporary decoded buffer*/
    free(tempConversionBuf);
#else /* UTF_CONVERSION */
    pFileHandler = fopen((const char *)pUrl, (const char *)mode);
#endif /* UTF_CONVERSION */

    if (M4OSA_NULL == pFileHandler)
    {
        switch(errno)
        {
        case ENOENT:
            {
                M4OSA_DEBUG(M4ERR_FILE_NOT_FOUND, "M4OSA_fileCommonOpen: No such file or directory");
                M4OSA_TRACE1_1("File not found: %s", pUrl);
                return M4ERR_FILE_NOT_FOUND;
            }
        case EACCES:
            {
                M4OSA_DEBUG(M4ERR_FILE_LOCKED, "M4OSA_fileCommonOpen: Permission denied");
                return M4ERR_FILE_LOCKED;
            }
         case EINVAL:
         {
            M4OSA_DEBUG(M4ERR_FILE_BAD_MODE_ACCESS, "M4OSA_fileCommonOpen: Invalid Argument");
            return M4ERR_FILE_BAD_MODE_ACCESS;
         }
        case EMFILE:
         case ENOSPC:
        case ENOMEM:
            {
                M4OSA_DEBUG(M4ERR_ALLOC, "M4OSA_fileCommonOpen: Too many open files");
                return M4ERR_ALLOC;
            }
        default:
            {
                M4OSA_DEBUG(M4ERR_NOT_IMPLEMENTED, "M4OSA_fileCommonOpen");
                return M4ERR_NOT_IMPLEMENTED;
            }
        }
    }

    /* Allocate the file context */
    pFileContext = (M4OSA_FileContext*) M4OSA_32bitAlignedMalloc(sizeof(M4OSA_FileContext),
                    core_id, (M4OSA_Char*)"M4OSA_fileCommonOpen: file context");
    if (M4OSA_NULL == pFileContext)
    {
        fclose(pFileHandler);
        M4OSA_DEBUG(M4ERR_ALLOC, "M4OSA_fileCommonOpen");
        return M4ERR_ALLOC;
    }

    pFileContext->file_desc        = pFileHandler;
#ifndef ANDROID_DEFAULT_CODE
    M4OSA_TRACE1_2("open file %s:%x", (char*)pUrl, pFileHandler);
#endif
    pFileContext->access_mode    = fileModeAccess;
    pFileContext->current_seek    = SeekNone;
    pFileContext->b_is_end_of_file    = M4OSA_FALSE;

    /**
     * Note: Never use this expression "i = (value1 == value2) ? x: y;"
     * because that doens't compile on other platforms (ADS for example)
     * Use: if(value1 == value2)
     *        { i= x; ..etc
     */
    pFileContext->coreID_write = 0;
    pFileContext->coreID_read = 0;
    pFileContext->m_DescrModeAccess = M4OSA_kDescNoneAccess;

    if (M4OSA_FILE_READER == core_id)
    {
        pFileContext->coreID_read = core_id;
        pFileContext->m_DescrModeAccess = M4OSA_kDescReadAccess;
    }
    else if (M4OSA_FILE_WRITER == core_id)
    {
        pFileContext->coreID_write = core_id;
        pFileContext->m_DescrModeAccess = M4OSA_kDescWriteAccess;
    }

    pFileContext->read_position = 0;
    pFileContext->write_position = 0;

    /* Allocate the memory to store the URL string */
    pFileContext->url_name = (M4OSA_Char*) M4OSA_32bitAlignedMalloc(strlen((const char *)pUrl)+1,
                        core_id, (M4OSA_Char*)"M4OSA_fileCommonOpen: URL name");
    if (M4OSA_NULL == pFileContext->url_name)
    {
        fclose(pFileHandler);
        free(pFileContext);
        M4OSA_DEBUG(M4ERR_ALLOC, "M4OSA_fileCommonOpen");
        return M4ERR_ALLOC;
    }
    M4OSA_chrNCopy(pFileContext->url_name, pUrl, strlen((const char *)pUrl)+1);

    /* Get the file name */
    err = M4OSA_fileCommonGetFilename(pUrl, &pFileContext->file_name);
    if(M4NO_ERROR != err)
    {
        fclose(pFileHandler);
        free(pFileContext->url_name);
        free(pFileContext);
        M4OSA_DEBUG(err, "M4OSA_fileCommonOpen");
        return err;
    }

#ifdef M4OSA_FILE_BLOCK_WITH_SEMAPHORE
    M4OSA_semaphoreOpen(&(pFileContext->semaphore_context), 1); /* Allocate the semaphore */
#endif /* M4OSA_FILE_BLOCK_WITH_SEMAPHORE */



#ifdef USE_STAGEFRIGHT_CODECS
    // Workaround for file system bug on Stingray/Honeycomb where a file re-created will keep
    // the original file's size filled with 0s. Do not seek to the end to avoid ill effects
    if(fileModeAccess & M4OSA_kFileAppend) {
        /* Get the file size */
        iSavePos = ftell(pFileHandler);            /*    1- Check the first position */
        fseek(pFileHandler, 0, SEEK_END);        /*    2- Go to the end of the file*/
        iSize = ftell(pFileHandler);            /*    3- Check the file size        */
        fseek(pFileHandler, iSavePos, SEEK_SET);/*    4- go to the first position */
    } else {
        iSize = 0;
    }
#else /* USE_STAGEFRIGHT_CODECS */
    /* Get the file size */
    iSavePos = ftell(pFileHandler);            /*    1- Check the first position */
    fseek(pFileHandler, 0, SEEK_END);        /*    2- Go to the end of the file*/
    iSize = ftell(pFileHandler);            /*    3- Check the file size        */
    fseek(pFileHandler, iSavePos, SEEK_SET);/*    4- go to the first position */
#endif /* USE_STAGEFRIGHT_CODECS */



    /* Warning possible overflow if the file is higher than 2GBytes */
    pFileContext->file_size = iSize;

    *pContext = pFileContext;

    return M4NO_ERROR;
}
void*
videoEditJava_getString(
                bool*                               pResult,
                JNIEnv*                             pEnv,
                jobject                             object,
                jfieldID                            stringFieldId,
                M4OSA_UInt32*                       pLength)
{
    void*        pString = M4OSA_NULL;
    jstring      string  = NULL;
    M4OSA_UInt32 length  = 0;
    M4OSA_Char*  pLocal  = M4OSA_NULL;
    M4OSA_ERR    result  = M4NO_ERROR;

    // Check if the previous action succeeded.
    if (*pResult)
    {
        // Log the function call.
        VIDEOEDIT_LOG_FUNCTION(ANDROID_LOG_INFO, "VIDEO_EDITOR_JAVA", "videoEditJava_getString()");

        // Check if an object containing a string was specified.
        if (NULL != stringFieldId)
        {
            // Retrieve the string object.
            string = (jstring)pEnv->GetObjectField(object, stringFieldId);

            // Clear any resulting exceptions.
            pEnv->ExceptionClear();
        }
        else
        {
            // The string itself was specified.
            string = (jstring)object;
        }

        // Check if the string could be retrieved.
        if (NULL != string)
        {
            // Get a local copy of the string.
            pLocal = (M4OSA_Char*)pEnv->GetStringUTFChars(string, M4OSA_NULL);
            if (M4OSA_NULL != pLocal)
            {
                // Determine the length of the path
                // (add one extra character for the zero terminator).
                length = strlen((const char *)pLocal) + 1;

                // Allocate memory for the string.
                pString = videoEditOsal_alloc(pResult, pEnv, length, "String");
                if (*pResult)
                {
                    // Copy the string.
                    result = M4OSA_chrNCopy((M4OSA_Char*)pString, pLocal, length);

                    // Check if the copy succeeded.
                    videoEditJava_checkAndThrowRuntimeException(pResult, pEnv,
                     (M4NO_ERROR != result), result);

                    // Check if the string could not be copied.
                    if (!(*pResult))
                    {
                        // Free the allocated memory.
                        videoEditOsal_free(pString);
                        pString = M4OSA_NULL;
                    }
                }

                // Release the local copy of the string.
                pEnv->ReleaseStringUTFChars(string, (const char *)pLocal);
            }
        }

        // Check if the string was empty or could be copied.
        if (*pResult)
        {
            // Check if the length was requested.
            if (M4OSA_NULL != pLength)
            {
                // Return the length.
                (*pLength) = length;
            }
        }
    }

    // Return the string.
    return(pString);
}