VideoEditorPropertiesMain.cpp revision 600acf14ff12eaf139f0ac644fb7e17849af65fa
1/*
2 * Copyright (C) 2011 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include <dlfcn.h>
18#include <stdio.h>
19#include <unistd.h>
20#include <utils/Log.h>
21#include <utils/threads.h>
22#include <VideoEditorClasses.h>
23#include <VideoEditorJava.h>
24#include <VideoEditorOsal.h>
25#include <VideoEditorLogging.h>
26#include <VideoEditorOsal.h>
27#include <marker.h>
28
29extern "C" {
30#include <M4OSA_Clock.h>
31#include <M4OSA_CharStar.h>
32#include <M4OSA_Error.h>
33#include <M4OSA_FileCommon.h>
34#include <M4OSA_FileReader.h>
35#include <M4OSA_FileWriter.h>
36#include <M4OSA_Memory.h>
37#include <M4OSA_Thread.h>
38#include <M4VSS3GPP_API.h>
39#include <M4VSS3GPP_ErrorCodes.h>
40#include <M4MCS_API.h>
41#include <M4MCS_ErrorCodes.h>
42#include <M4READER_Common.h>
43#include <M4WRITER_common.h>
44#include <M4DECODER_Common.h>
45#include <M4AD_Common.h>
46};
47
48extern "C" M4OSA_ERR M4MCS_open_normalMode(
49                M4MCS_Context                       pContext,
50                M4OSA_Void*                         pFileIn,
51                M4VIDEOEDITING_FileType             InputFileType,
52                M4OSA_Void*                         pFileOut,
53                M4OSA_Void*                         pTempFile);
54
55jobject videoEditProp_getProperties(
56                JNIEnv*                             pEnv,
57                jobject                             thiz,
58                jstring                             file);
59
60static void
61getFileAndMediaTypeFromExtension (
62                M4OSA_Char* pExtension,
63                VideoEditClasses_FileType   *pFileType,
64                M4VIDEOEDITING_FileType       *pClipType);
65
66static M4OSA_ERR
67getClipProperties(  JNIEnv*                         pEnv,
68                    jobject                         thiz,
69                    M4OSA_Char*                     pFile,
70                    M4VIDEOEDITING_FileType         clipType,
71                    M4VIDEOEDITING_ClipProperties*  pClipProperties);
72
73M4OSA_UInt32
74VideoEdit_chrCompare(M4OSA_Char* pStrIn1,
75                     M4OSA_Char* pStrIn2,
76                     M4OSA_Int32* pCmpResult);
77
78jobject videoEditProp_getProperties(
79        JNIEnv* pEnv,
80        jobject thiz,
81        jstring file)
82{
83    bool                           gotten          = true;
84    M4OSA_Char*                    pFile           = M4OSA_NULL;
85    M4OSA_Char*                    pExtension      = M4OSA_NULL;
86    M4OSA_UInt32                   index           = 0;
87    M4OSA_Int32                    cmpResult       = 0;
88    VideoEditPropClass_Properties* pProperties     = M4OSA_NULL;
89    M4VIDEOEDITING_ClipProperties* pClipProperties = M4OSA_NULL;
90    M4OSA_ERR                      result          = M4NO_ERROR;
91    M4MCS_Context                  context         = M4OSA_NULL;
92    M4OSA_FilePosition             size            = 0;
93    M4OSA_UInt32                   width           = 0;
94    M4OSA_UInt32                   height          = 0;
95    jobject                        properties      = NULL;
96    M4OSA_Context                  pOMXContext     = M4OSA_NULL;
97    M4DECODER_VideoInterface*      pOMXVidDecoderInterface = M4OSA_NULL;
98    M4AD_Interface*                pOMXAudDecoderInterface = M4OSA_NULL;
99
100    bool  initialized = true;
101    VideoEditClasses_FileType fileType = VideoEditClasses_kFileType_Unsupported;
102    M4VIDEOEDITING_FileType clipType = M4VIDEOEDITING_kFileType_Unsupported;
103
104    VIDEOEDIT_LOG_API(
105            ANDROID_LOG_INFO, "VIDEO_EDITOR_PROPERTIES",
106            "videoEditProp_getProperties()");
107
108    // Add a text marker (the condition must always be true).
109    ADD_TEXT_MARKER_FUN(NULL != pEnv)
110
111    // Initialize the classes.
112    videoEditPropClass_init(&initialized, (JNIEnv*)pEnv);
113
114    // Validate the tempPath parameter.
115    videoEditJava_checkAndThrowIllegalArgumentException(
116            &gotten, pEnv, (NULL == file), "file is null");
117
118    // Get the file path.
119    pFile = (M4OSA_Char *)videoEditJava_getString(
120            &gotten, pEnv, file, NULL, M4OSA_NULL);
121
122    result = M4OSA_fileReadOpen(&context, (M4OSA_Void*)pFile, M4OSA_kFileRead);
123
124    if(M4NO_ERROR != result) {
125        // Free the file path.
126        videoEditOsal_free(pFile);
127        pFile = M4OSA_NULL;
128    }
129
130    videoEditJava_checkAndThrowIllegalArgumentException(&gotten, pEnv,
131        (M4NO_ERROR != result), "file not found");
132
133    // Close the file and free the file context
134    if (context != NULL) {
135        result = M4OSA_fileReadClose(context);
136        context = M4OSA_NULL;
137    }
138
139    // Return if Error
140    if (M4NO_ERROR != result) {
141        return (properties); // NULL
142    }
143
144    // Check if the file path is valid.
145    if (gotten)
146    {
147        // Retrieve the extension.
148        pExtension = (M4OSA_Char *)strrchr((const char *)pFile, (int)'.');
149        if (M4OSA_NULL != pExtension)
150        {
151            // Skip the dot.
152            pExtension++;
153
154            // Get the file type and Media type from extension
155            getFileAndMediaTypeFromExtension(
156                    pExtension ,&fileType, &clipType);
157        }
158    }
159
160    // Check if the file type could be determined.
161    videoEditJava_checkAndThrowIllegalArgumentException(
162            &gotten, pEnv,
163            (VideoEditClasses_kFileType_Unsupported == fileType),
164            "file type is not supported");
165
166    // Allocate a new properties structure.
167    pProperties = (VideoEditPropClass_Properties*)videoEditOsal_alloc(
168            &gotten, pEnv,
169            sizeof(VideoEditPropClass_Properties), "Properties");
170
171    // Check if the context is valid and allocation succeeded
172    // (required because of dereferencing of pProperties).
173    if (gotten)
174    {
175        // Check if this type of file needs to be analyzed using MCS.
176        if ((VideoEditClasses_kFileType_MP3  == fileType) ||
177            (VideoEditClasses_kFileType_MP4  == fileType) ||
178            (VideoEditClasses_kFileType_3GPP == fileType) ||
179            (VideoEditClasses_kFileType_AMR  == fileType) ||
180            (VideoEditClasses_kFileType_PCM  == fileType) ||
181            (VideoEditClasses_kFileType_M4V  == fileType))
182        {
183            // Allocate a new clip properties structure.
184            pClipProperties =
185                (M4VIDEOEDITING_ClipProperties*)videoEditOsal_alloc(
186                    &gotten, pEnv,
187                    sizeof(M4VIDEOEDITING_ClipProperties), "ClipProperties");
188
189            // Check if allocation succeeded (required because of
190            // dereferencing of pClipProperties).
191            if (gotten)
192            {
193                // Add a code marker (the condition must always be true).
194                ADD_CODE_MARKER_FUN(NULL != pClipProperties)
195
196                // Log the API call.
197                VIDEOEDIT_LOG_API(
198                        ANDROID_LOG_INFO, "VIDEO_EDITOR_PROPERTIES",
199                        "getClipProperties");
200
201                // Get Video clip properties
202                result = getClipProperties(
203                        pEnv, thiz, pFile, clipType, pClipProperties);
204
205                if (M4MCS_ERR_FILE_DRM_PROTECTED == result) {
206                    // Check if the creation succeeded.
207                    videoEditJava_checkAndThrowIllegalArgumentException(
208                            &gotten, pEnv,(M4NO_ERROR != result),
209                            "Invalid File - DRM Protected ");
210                } else {
211                    // Check if the creation succeeded.
212                    videoEditJava_checkAndThrowIllegalArgumentException(
213                            &gotten, pEnv,(M4NO_ERROR != result),
214                            "Invalid File or File not found ");
215                }
216
217#ifdef USE_SOFTWARE_DECODER
218                /**
219                 * Input clip with non-multiples of 16 is not supported.
220                 */
221                if ( (pClipProperties->uiVideoWidth %16)
222                    || (pClipProperties->uiVideoHeight %16) )
223                {
224                    result = M4MCS_ERR_INPUT_VIDEO_SIZE_NON_X16;
225                    videoEditJava_checkAndThrowIllegalArgumentException(
226                            &gotten, pEnv, (M4NO_ERROR != result),
227                            "non x16 input video frame size is not supported");
228                }
229#endif /* USE_SOFTWARE_DECODER */
230            }
231
232            // Check if the properties could be retrieved.
233            if (gotten)
234            {
235                // Set the properties.
236                pProperties->uiClipDuration = pClipProperties->uiClipDuration;
237                if (M4VIDEOEDITING_kFileType_Unsupported == pClipProperties->FileType)
238                {
239                    pProperties->FileType        = VideoEditClasses_kFileType_Unsupported;
240                }
241                else
242                {
243                    pProperties->FileType        = fileType;
244                }
245                pProperties->VideoStreamType     = pClipProperties->VideoStreamType;
246                pProperties->uiClipVideoDuration = pClipProperties->uiClipVideoDuration;
247                pProperties->uiVideoBitrate      = pClipProperties->uiVideoBitrate;
248                pProperties->uiVideoWidth        = pClipProperties->uiVideoWidth;
249                pProperties->uiVideoHeight       = pClipProperties->uiVideoHeight;
250                pProperties->fAverageFrameRate   = pClipProperties->fAverageFrameRate;
251                pProperties->ProfileAndLevel     = pClipProperties->ProfileAndLevel;
252                pProperties->AudioStreamType     = pClipProperties->AudioStreamType;
253                pProperties->uiClipAudioDuration = pClipProperties->uiClipAudioDuration;
254                pProperties->uiAudioBitrate      = pClipProperties->uiAudioBitrate;
255                pProperties->uiNbChannels        = pClipProperties->uiNbChannels;
256                pProperties->uiSamplingFrequency = pClipProperties->uiSamplingFrequency;
257            }
258
259            // Free the clip properties.
260            videoEditOsal_free(pClipProperties);
261            pClipProperties = M4OSA_NULL;
262        }
263        else if ((VideoEditClasses_kFileType_JPG == fileType) ||
264            (VideoEditClasses_kFileType_GIF == fileType) ||
265            (VideoEditClasses_kFileType_PNG == fileType))
266        {
267            pProperties->uiClipDuration      = 0;
268            pProperties->FileType            = fileType;
269            pProperties->VideoStreamType     = M4VIDEOEDITING_kNoneVideo;
270            pProperties->uiClipVideoDuration = 0;
271            pProperties->uiVideoBitrate      = 0;
272            pProperties->uiVideoWidth        = width;
273            pProperties->uiVideoHeight       = height;
274            pProperties->fAverageFrameRate   = 0.0f;
275            pProperties->ProfileAndLevel     = M4VIDEOEDITING_kProfile_and_Level_Out_Of_Range;
276            pProperties->AudioStreamType     = M4VIDEOEDITING_kNoneAudio;
277            pProperties->uiClipAudioDuration = 0;
278            pProperties->uiAudioBitrate      = 0;
279            pProperties->uiNbChannels        = 0;
280            pProperties->uiSamplingFrequency = 0;
281
282            // Added for Handling invalid paths and non existent image files
283            // Open the file for reading.
284            result = M4OSA_fileReadOpen(&context, (M4OSA_Void*)pFile, M4OSA_kFileRead);
285            if (M4NO_ERROR != result)
286            {
287                pProperties->FileType = VideoEditClasses_kFileType_Unsupported;
288            }
289            result = M4OSA_fileReadClose(context);
290            context = M4OSA_NULL;
291        }
292    }
293
294    // Create a properties object.
295    videoEditPropClass_createProperties(&gotten, pEnv, pProperties, &properties);
296
297    // Log the properties.
298    VIDEOEDIT_PROP_LOG_PROPERTIES(pProperties);
299
300    // Free the properties.
301    videoEditOsal_free(pProperties);
302    pProperties = M4OSA_NULL;
303
304    // Free the file path.
305    videoEditOsal_free(pFile);
306    pFile = M4OSA_NULL;
307
308    // Add a text marker (the condition must always be true).
309    ADD_TEXT_MARKER_FUN(NULL != pEnv)
310
311    // Return the Properties object.
312    return(properties);
313}
314
315static void getFileAndMediaTypeFromExtension (
316        M4OSA_Char *pExtension,
317        VideoEditClasses_FileType *pFileType,
318        M4VIDEOEDITING_FileType *pClipType)
319{
320    M4OSA_Char extension[5] = {0, 0, 0, 0, 0};
321    VideoEditClasses_FileType fileType =
322            VideoEditClasses_kFileType_Unsupported;
323
324    M4VIDEOEDITING_FileType clipType =
325            M4VIDEOEDITING_kFileType_Unsupported;
326
327    M4OSA_UInt32 index = 0;
328    M4OSA_ERR result = M4NO_ERROR;
329    M4OSA_Int32 cmpResult = 0;
330    M4OSA_UInt32  extLength = strlen((const char *)pExtension);
331
332    // Assign default
333    *pFileType = VideoEditClasses_kFileType_Unsupported;
334    *pClipType = M4VIDEOEDITING_kFileType_Unsupported;
335
336    // Check if the length of the extension is valid.
337    if ((3 == extLength) || (4 == extLength))
338    {
339        // Convert the extension to lowercase.
340        for (index = 0; index < extLength ; index++)
341        {
342            extension[index] = tolower((int)pExtension[index]);
343        }
344
345        // Check if the extension is ".mp3".
346        if (!(VideoEdit_chrCompare(extension, (M4OSA_Char*)"mp3", &cmpResult)))
347        {
348            *pFileType = VideoEditClasses_kFileType_MP3;
349            *pClipType = M4VIDEOEDITING_kFileType_MP3;
350        }
351        // Check if the extension is ".mp4".
352        else if (!(VideoEdit_chrCompare(extension, (M4OSA_Char*)"mp4", &cmpResult)))
353        {
354            *pFileType = VideoEditClasses_kFileType_MP4;
355            *pClipType = M4VIDEOEDITING_kFileType_MP4;
356        }
357        // Check if the extension is ".3gp".
358        else if (!(VideoEdit_chrCompare(extension, (M4OSA_Char*)"3gp", &cmpResult)))
359        {
360            *pFileType = VideoEditClasses_kFileType_3GPP;
361            *pClipType = M4VIDEOEDITING_kFileType_3GPP;
362        }
363        // Check if the extension is ".m4a".
364        else if (!(VideoEdit_chrCompare(extension, (M4OSA_Char*)"m4a", &cmpResult)))
365        {
366            *pFileType = VideoEditClasses_kFileType_3GPP;
367            *pClipType = M4VIDEOEDITING_kFileType_3GPP;
368        }
369        // Check if the extension is ".3gpp".
370        else if (!(VideoEdit_chrCompare(extension, (M4OSA_Char*)"3gpp", &cmpResult)))
371        {
372            *pFileType = VideoEditClasses_kFileType_3GPP;
373            *pClipType = M4VIDEOEDITING_kFileType_3GPP;
374        }
375        // Check if the extension is ".amr".
376        else if (!(VideoEdit_chrCompare(extension, (M4OSA_Char*)"amr", &cmpResult)))
377        {
378            *pFileType = VideoEditClasses_kFileType_AMR;
379            *pClipType = M4VIDEOEDITING_kFileType_AMR;
380        }
381        // Check if the extension is ".pcm".
382        else if (!(VideoEdit_chrCompare(extension, (M4OSA_Char*)"pcm", &cmpResult)))
383        {
384            *pFileType = VideoEditClasses_kFileType_PCM;
385            *pClipType = M4VIDEOEDITING_kFileType_PCM;
386        }
387        // Check if the extension is ".jpg".
388        else if (!(VideoEdit_chrCompare(extension, (M4OSA_Char*)"jpg", &cmpResult)))
389        {
390            *pFileType = VideoEditClasses_kFileType_JPG;
391        }
392        // Check if the extension is ".jpeg".
393        else if (!(VideoEdit_chrCompare(extension, (M4OSA_Char*)"jpeg", &cmpResult)))
394        {
395            *pFileType = VideoEditClasses_kFileType_JPG;
396        }
397        // Check if the extension is ".gif".
398        else if (!(VideoEdit_chrCompare(extension, (M4OSA_Char*)"gif", &cmpResult)))
399        {
400            *pFileType = VideoEditClasses_kFileType_GIF;
401        }
402        // Check if the extension is ".png".
403        else if (!(VideoEdit_chrCompare(extension, (M4OSA_Char*)"png", &cmpResult)))
404        {
405            *pFileType = VideoEditClasses_kFileType_PNG;
406        }
407        // Check if the extension is ".m4v".
408        else if (!(VideoEdit_chrCompare(extension, (M4OSA_Char*)"m4v", &cmpResult)))
409        {
410            *pFileType = VideoEditClasses_kFileType_M4V;
411            *pClipType = M4VIDEOEDITING_kFileType_M4V;
412        }
413    }
414}
415
416static M4OSA_ERR getClipProperties(
417        JNIEnv* pEnv,
418        jobject thiz,
419        M4OSA_Char* pFile,
420        M4VIDEOEDITING_FileType clipType,
421        M4VIDEOEDITING_ClipProperties* pClipProperties)
422{
423    bool                      gotten          = true;
424    M4OSA_ERR                 result          = M4NO_ERROR;
425    M4OSA_ERR                 resultAbort     = M4NO_ERROR;
426    M4MCS_Context             context         = M4OSA_NULL;
427
428    M4OSA_FileReadPointer fileReadPtr =
429            { M4OSA_NULL, M4OSA_NULL, M4OSA_NULL,
430              M4OSA_NULL, M4OSA_NULL, M4OSA_NULL };
431
432    M4OSA_FileWriterPointer fileWritePtr =
433            { M4OSA_NULL, M4OSA_NULL, M4OSA_NULL,
434              M4OSA_NULL, M4OSA_NULL, M4OSA_NULL, M4OSA_NULL };
435
436    // Initialize the OSAL file system function pointers.
437    videoEditOsal_getFilePointers(&fileReadPtr , &fileWritePtr);
438
439    // Log the API call.
440    VIDEOEDIT_LOG_API(
441            ANDROID_LOG_INFO, "VIDEO_EDITOR_PROPERTIES",\
442            "getClipProperties - M4MCS_init()");
443
444    // Initialize the MCS context.
445    result = M4MCS_init(&context, &fileReadPtr, &fileWritePtr);
446
447    // Log the result.
448    VIDEOEDIT_PROP_LOG_RESULT(
449            ANDROID_LOG_INFO, "VIDEO_EDITOR_PROPERTIES", "%s",
450            videoEditOsal_getResultString(result));
451
452    // Check if the creation succeeded.
453    videoEditJava_checkAndThrowRuntimeException(
454            &gotten, pEnv, (M4NO_ERROR != result), result);
455
456    // Check if opening the MCS context succeeded.
457    if (gotten)
458    {
459        // Log the API call.
460        VIDEOEDIT_LOG_API(
461                ANDROID_LOG_INFO, "VIDEO_EDITOR_PROPERTIES",
462                "getClipProperties - M4MCS_open_normalMode()");
463
464        // Open the MCS in the normal opening mode to
465        // retrieve the exact duration
466        result = M4MCS_open_normalMode(
467                context, pFile, clipType, M4OSA_NULL, M4OSA_NULL);
468
469        // Log the result.
470        VIDEOEDIT_PROP_LOG_RESULT(
471                ANDROID_LOG_INFO, "VIDEO_EDITOR_PROPERTIES", "%s",
472                videoEditOsal_getResultString(result));
473
474        // Check if the creation succeeded.
475        videoEditJava_checkAndThrowRuntimeException(
476                &gotten, pEnv, (M4NO_ERROR != result), result);
477
478        // Check if the MCS could be opened.
479        if (gotten)
480        {
481            // Log the API call.
482            VIDEOEDIT_LOG_API(
483                    ANDROID_LOG_INFO, "VIDEO_EDITOR_PROPERTIES",
484                    "getClipProperties - M4MCS_getInputFileProperties()");
485
486            // Get the properties.
487            result = M4MCS_getInputFileProperties(context, pClipProperties);
488
489            // Log the result.
490            VIDEOEDIT_PROP_LOG_RESULT(
491                    ANDROID_LOG_INFO, "VIDEO_EDITOR_PROPERTIES", "%s",
492                    videoEditOsal_getResultString(result));
493
494            // Check if the creation succeeded.
495            videoEditJava_checkAndThrowRuntimeException(
496                    &gotten, pEnv, (M4NO_ERROR != result), result);
497        }
498
499        // Log the API call.
500        VIDEOEDIT_LOG_API(
501                ANDROID_LOG_INFO, "VIDEO_EDITOR_PROPERTIES",
502                "getClipProperties - M4MCS_abort()");
503
504        // Close the MCS session.
505        resultAbort = M4MCS_abort(context);
506
507       if (result == M4NO_ERROR) {
508            // Log the result.
509            VIDEOEDIT_PROP_LOG_RESULT(
510                    ANDROID_LOG_INFO, "VIDEO_EDITOR_PROPERTIES", "%s",
511                    videoEditOsal_getResultString(resultAbort));
512
513            // Check if the abort succeeded.
514            videoEditJava_checkAndThrowRuntimeException(
515                    &gotten, pEnv, (M4NO_ERROR != resultAbort), resultAbort);
516            result = resultAbort;
517        }
518    }
519
520    return result;
521}
522
523M4OSA_UInt32
524VideoEdit_chrCompare(M4OSA_Char* pStrIn1,
525                     M4OSA_Char* pStrIn2,
526                      M4OSA_Int32* pCmpResult)
527{
528    *pCmpResult = strcmp((const char *)pStrIn1, (const char *)pStrIn2);
529    return *pCmpResult;
530}
531
532
533