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