mmfenh/advancedaudiocontroller/audiocontrollerpluginsvariant/3gpaudioplaycontroller/Src/3GPAudioPlayControllerRecognizer.cpp
changeset 0 71ca22bcf22a
child 18 2eb3b066cc7d
equal deleted inserted replaced
-1:000000000000 0:71ca22bcf22a
       
     1 /*
       
     2 * Copyright (c) 2004 Nokia Corporation and/or its subsidiary(-ies). 
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description:  Recognizer for 3GP files.
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 // INCLUDES
       
    20 #include    <f32file.h>
       
    21 #include    <barsread.h>
       
    22 #include    <mp4lib.h>
       
    23 #include    <implementationproxy.h>
       
    24 #include    "3GPAudioPlayControllerRecognizer.h"
       
    25 #include    "DebugMacros.h"
       
    26 
       
    27 // LOCAL CONSTANTS AND MACROS
       
    28 const TUid KUidMime3gpRecognizer = {0x101FAFAD};
       
    29 const TInt KExtLength = 4;
       
    30 _LIT(KDot, ".");
       
    31 _LIT(KDot3gp, ".3gp");
       
    32 _LIT(KDot3ga, ".3ga");
       
    33 _LIT8(K3gpAudioMimeType, "audio/3gpp");
       
    34 _LIT8(K3gpVideoMimeType, "video/3gpp");
       
    35 
       
    36 _LIT(KDotMp4, ".mp4");
       
    37 _LIT(KDotM4a, ".m4a");
       
    38 _LIT8(KMp4AudioMimeType, "audio/mp4");
       
    39 _LIT8(KMp4VideoMimeType, "video/mp4");
       
    40 
       
    41 _LIT(KDot3g2, ".3g2");
       
    42 _LIT8(K3g2AudioMimeType, "audio/3gpp2");
       
    43 _LIT8(K3g2VideoMimeType, "video/3gpp2");
       
    44 
       
    45 // Needed for looking at headers for 3gp files w/ no extensions
       
    46 const TInt KPreHeaderLength = 4;
       
    47 const TInt KHeaderLen = 8;
       
    48 
       
    49 _LIT8(K3gpHeader ,"ftyp3gp");
       
    50 _LIT8(KMp4Header ,"ftypmp4");
       
    51 _LIT8(KMmp4Header ,"ftypmmp4");
       
    52 _LIT8(KM4aHeader, "ftypM4A");
       
    53 _LIT8(K3g2Header, "ftyp3g2");
       
    54 _LIT8(K3gp6Header, "ftyp3gp6"); // 3GPP Release 6 basic Profile
       
    55 _LIT8(K3gr6Header, "ftyp3gr6"); // 3GPP Rel6 Progressive Download ProfileHeader
       
    56 _LIT8(K3gs6Header, "ftyp3gs6"); // 3GPP Release 6 streaming-server Profile
       
    57 
       
    58 // ============================ MEMBER FUNCTIONS ===============================
       
    59 
       
    60 // -----------------------------------------------------------------------------
       
    61 // C3GPAudioPlayControllerRecognizer::C3GPAudioPlayControllerRecognizer()
       
    62 // Call base constructor with the recognizer's UID and confidence level
       
    63 // C++ default constructor can NOT contain any code, that
       
    64 // might leave.
       
    65 // -----------------------------------------------------------------------------
       
    66 //
       
    67 C3GPAudioPlayControllerRecognizer::C3GPAudioPlayControllerRecognizer()
       
    68     :CApaDataRecognizerType(KUidMime3gpRecognizer, CApaDataRecognizerType::EHigh)
       
    69     {
       
    70     // Set the number of data (MIME) types supported by this recognizer.
       
    71     iCountDataTypes = 6;
       
    72     }
       
    73 
       
    74 // -----------------------------------------------------------------------------
       
    75 // C3GPAudioPlayControllerRecognizer::ConstructL
       
    76 // Symbian 2nd phase constructor can leave.
       
    77 // -----------------------------------------------------------------------------
       
    78 //
       
    79 void C3GPAudioPlayControllerRecognizer::ConstructL()
       
    80     {
       
    81     }
       
    82 
       
    83 // -----------------------------------------------------------------------------
       
    84 // C3GPAudioPlayControllerRecognizer::NewL
       
    85 // Two-phased constructor.
       
    86 // -----------------------------------------------------------------------------
       
    87 //
       
    88 C3GPAudioPlayControllerRecognizer* C3GPAudioPlayControllerRecognizer::NewL()
       
    89     {
       
    90     C3GPAudioPlayControllerRecognizer* self = new(ELeave) C3GPAudioPlayControllerRecognizer();
       
    91     CleanupStack::PushL(self);
       
    92     self->ConstructL();
       
    93     CleanupStack::Pop(self);
       
    94     return self;
       
    95     }
       
    96 
       
    97 // Destructor
       
    98 C3GPAudioPlayControllerRecognizer::~C3GPAudioPlayControllerRecognizer()
       
    99     {
       
   100     }
       
   101 
       
   102 // -----------------------------------------------------------------------------
       
   103 // C3GPAudioPlayControllerRecognizer::SupportedDataTypeL
       
   104 // Return the supposed datatype depending on the
       
   105 // MimeType of the file
       
   106 // -----------------------------------------------------------------------------
       
   107 //
       
   108 TDataType C3GPAudioPlayControllerRecognizer::SupportedDataTypeL(
       
   109         TInt aIndex) const
       
   110     {
       
   111     DP1(_L("C3GPAudioPlayControllerRecognizer::SupportedDataTypeL [%d]"), 
       
   112             aIndex);
       
   113     TDataType dataType;
       
   114     switch(aIndex)
       
   115         {
       
   116         case 0:
       
   117             dataType = TDataType(K3gpAudioMimeType);
       
   118             break;
       
   119         case 1:
       
   120             dataType = TDataType(K3gpVideoMimeType);
       
   121             break;
       
   122         case 2:
       
   123             dataType = TDataType(KMp4AudioMimeType);
       
   124             break;
       
   125         case 3:
       
   126             dataType = TDataType(KMp4VideoMimeType);
       
   127             break;
       
   128         case 4:
       
   129             dataType = TDataType(K3g2AudioMimeType);
       
   130             break;
       
   131         case 5:
       
   132             dataType = TDataType(K3g2VideoMimeType);
       
   133             break;
       
   134         default:
       
   135             User::Leave(KErrArgument);
       
   136             break;
       
   137         }
       
   138     return dataType;
       
   139     }
       
   140 
       
   141 // -----------------------------------------------------------------------------
       
   142 // C3GPAudioPlayControllerRecognizer::PreferredBufSize()
       
   143 // Return the supposed minimum buffer size we need to
       
   144 // successfully recognize the data
       
   145 // -----------------------------------------------------------------------------
       
   146 //
       
   147 TUint C3GPAudioPlayControllerRecognizer::PreferredBufSize()
       
   148     {
       
   149     return 0;
       
   150     }
       
   151 
       
   152 // -----------------------------------------------------------------------------
       
   153 // C3GPAudioPlayControllerRecognizer::DoRecognizeL
       
   154 // Attempt to recognize the data
       
   155 // this recognizer only attempts to match the data and/or file suffix
       
   156 //
       
   157 // NB if the file is not recognized, this function should NOT leave :
       
   158 // it should instead set iConfidence = ENotRecognized and return
       
   159 // the function should only leave if there is an out-of-memory condition
       
   160 // -----------------------------------------------------------------------------
       
   161 //
       
   162 void C3GPAudioPlayControllerRecognizer::DoRecognizeL(
       
   163         const TDesC& aName,
       
   164         const TDesC8& aBuffer)
       
   165     {
       
   166     DP0(_L("C3GPAudioPlayControllerRecognizer::DoRecognizeL"));
       
   167 #ifdef _DEBUG
       
   168     RDebug::RawPrint(aName);
       
   169 #endif
       
   170     DP0(_L(" "));
       
   171     TPtrC8 header;
       
   172     TInt headerMatch = 0;
       
   173 
       
   174     // assume Match will fail :
       
   175     iConfidence = CApaDataRecognizerType::ENotRecognized;
       
   176     if (aName.Find(KDot) == KErrNotFound) // No extension
       
   177         {
       
   178         DP0(_L("C3GPAudioPlayControllerRecognizer::DoRecognizeL - Ext not present"));
       
   179 
       
   180         if (aBuffer.Length() < (KPreHeaderLength+KHeaderLen))
       
   181             {
       
   182             return;
       
   183             }
       
   184         else
       
   185             {
       
   186             // Check headers for 3gp (allowed to play w/out ext):
       
   187             header.Set(aBuffer.Mid(KPreHeaderLength, KHeaderLen));
       
   188             headerMatch = ( (header.FindF(K3gpHeader)  == 0) ||
       
   189                             (header.FindF(KMp4Header)  == 0) ||
       
   190                             (header.FindF(KMmp4Header) == 0) ||
       
   191                             (header.FindF(KM4aHeader)  == 0) ||
       
   192                             (header.FindF(K3g2Header)  == 0) ||
       
   193                             (header.FindF(K3gp6Header) == 0) ||
       
   194                             (header.FindF(K3gr6Header) == 0) ||
       
   195                             (header.FindF(K3gs6Header) == 0) );
       
   196 
       
   197             DP1(_L("C3GPAudioPlayControllerRecognizer::DoRecognizeL: headerMatch = %d"), headerMatch);
       
   198             if (!headerMatch)
       
   199                 {
       
   200                 return;
       
   201                 }
       
   202             }
       
   203         }
       
   204 
       
   205     TPtrC ext(aName.Right(KExtLength));
       
   206     DP0(_L("C3GPAudioPlayControllerRecognizer::DoRecognizeL: Extension:"));
       
   207 #ifdef _DEBUG
       
   208     RDebug::RawPrint(ext);
       
   209 #endif
       
   210     DP0(_L(" "));
       
   211 
       
   212     if ( (ext.CompareF(KDot3gp) == 0) || (ext.CompareF(KDotMp4) == 0) ||
       
   213          (ext.CompareF(KDotM4a) == 0) || (ext.CompareF(KDot3g2) == 0) || 
       
   214          (ext.CompareF(KDot3ga) == 0) || headerMatch)
       
   215         {
       
   216         RFile* fileHandle= NULL;
       
   217         TRAPD(handleErr, fileHandle = FilePassedByHandleL());
       
   218 
       
   219         if (handleErr != KErrNone)
       
   220             {
       
   221             DP1(_L("C3GPAudioPlayControllerRecognizer::DoRecognizeL - Can't get FH [%d]"), handleErr);
       
   222             return;
       
   223             }
       
   224 
       
   225         MP4Handle mp4Handle;    // 3GP library handle
       
   226         MP4Err err;
       
   227         // Check if file handle exists first. If not, try to open with file name.
       
   228         if (fileHandle)
       
   229             {
       
   230             err = MP4ParseOpenFileHandle(&mp4Handle, fileHandle);
       
   231             DP1(_L("C3GPAudioPlayControllerRecognizer::DoRecognizeL FileHandle, err[%d]"), err);
       
   232             }
       
   233         else
       
   234             { // file handle not present
       
   235             RBuf buf;
       
   236             TUint nameSize = aName.Length() + 1; // Add 1 extra for "zero" termination
       
   237             buf.CreateL(nameSize); // Create the RBuf 
       
   238             buf = aName; // Copy "aName" using the assignment operation
       
   239             err = MP4ParseOpen(&mp4Handle, (MP4FileName) buf.PtrZ());  
       
   240             buf.Close(); // Close Buf to free the memory
       
   241             DP1(_L("C3GPAudioPlayControllerRecognizer::DoRecognizeL FileName, err[%d]"), err);
       
   242             if (err != MP4_OK)
       
   243                 { // cannot find the file by name but the header content or extension was ok
       
   244                 err = MP4ParseOpen(&mp4Handle, NULL);
       
   245                 DP1(_L("C3GPAudioPlayControllerRecognizer::DoRecognizeL databuf, err[%d]"), err);
       
   246                 if (err == MP4_OK)
       
   247                     {
       
   248                     TUint8* p = (TUint8*)(aBuffer.Ptr());
       
   249                     TInt l = aBuffer.Length();
       
   250                     DP2(_L("C3GPAudioPlayControllerRecognizer::DoRecognizeL databuf *p[0x%x] l[%d]"), p[0], l);
       
   251                     err = MP4ParseWriteData(mp4Handle, p, l);
       
   252                     DP1(_L("C3GPAudioPlayControllerRecognizer::DoRecognizeL databuf write, err[%d]"), err);
       
   253                     }
       
   254                 }
       
   255             }
       
   256 
       
   257         if (err == MP4_OK)
       
   258             {
       
   259             mp4_u32 videoLength, videoType, videoWidth, videoHeight, timeScale;
       
   260             mp4_double frameRate;
       
   261 
       
   262             err = MP4ParseRequestVideoDescription(mp4Handle, &videoLength, &frameRate,
       
   263                     &videoType, &videoWidth, &videoHeight, &timeScale);
       
   264             if (err == MP4_NO_VIDEO)
       
   265                 {
       
   266                 mp4_u32 audioLength, audioType, timeScale, averateBitRate;
       
   267                 mp4_u8 framesPerSample;
       
   268 
       
   269                 err = MP4ParseRequestAudioDescription(mp4Handle, &audioLength, &audioType,
       
   270                         &framesPerSample, &timeScale, &averateBitRate);
       
   271 
       
   272                 if (err == MP4_OK)
       
   273                     {
       
   274                     if ( (header.FindF(K3gpHeader)  == 0) ||
       
   275                          (header.FindF(K3gp6Header) == 0) ||
       
   276                          (header.FindF(K3gr6Header) == 0) ||
       
   277                          (header.FindF(K3gs6Header) == 0) ||
       
   278                          (header.FindF(KMmp4Header) == 0) ||
       
   279                          (ext.CompareF(KDot3gp)     == 0) ||
       
   280                          (ext.CompareF(KDot3ga)     == 0) )
       
   281                         {
       
   282                         iDataType = TDataType(K3gpAudioMimeType);
       
   283                         iConfidence = ECertain;
       
   284                         DP0(_L("C3GPAudioPlayControllerRecognizer::DoRecognizeL [3gp audio]"));
       
   285                         }
       
   286                     else if ( (header.FindF(KM4aHeader) == 0) ||
       
   287                               (ext.CompareF(KDotM4a)    == 0) ||
       
   288                               (header.FindF(KMp4Header) == 0) ||
       
   289                               (ext.CompareF(KDotMp4)    == 0) )
       
   290                         {
       
   291                         iDataType = TDataType(KMp4AudioMimeType);
       
   292                         iConfidence = ECertain;
       
   293                         DP0(_L("C3GPAudioPlayControllerRecognizer::DoRecognizeL [Mp4 audio]"));
       
   294                         }
       
   295                     else if ( (header.FindF(K3g2Header) == 0) ||
       
   296                               (ext.CompareF(KDot3g2)    == 0) )
       
   297                         {
       
   298                         iDataType = TDataType(K3g2AudioMimeType);
       
   299                         iConfidence = ECertain;
       
   300                         DP0(_L("C3GPAudioPlayControllerRecognizer::DoRecognizeL [3g2 audio]"));
       
   301                         }
       
   302                     } // End if (err == MP4_OK)
       
   303                 } // End If (err == MP4_NO_VIDEO) i.e. Audio
       
   304             else if (err == MP4_OK)
       
   305                 {
       
   306                 if ( (header.FindF(K3gpHeader)  == 0) ||
       
   307                      (header.FindF(K3gp6Header) == 0) ||
       
   308                      (header.FindF(K3gr6Header) == 0) ||
       
   309                      (header.FindF(K3gs6Header) == 0) ||
       
   310                      (header.FindF(KMmp4Header) == 0) ||
       
   311                      (ext.CompareF(KDot3gp)     == 0) )
       
   312                     {
       
   313                     iDataType = TDataType(K3gpVideoMimeType);
       
   314                     iConfidence = ECertain;
       
   315                     DP0(_L("C3GPAudioPlayControllerRecognizer::DoRecognizeL [3gp video]"));
       
   316                     }
       
   317                 else if ( (header.FindF(KMp4Header) == 0) ||
       
   318                           (ext.CompareF(KDotMp4)    == 0) )
       
   319                     {
       
   320                     iDataType = TDataType(KMp4VideoMimeType);
       
   321                     iConfidence = ECertain;
       
   322                     DP0(_L("C3GPAudioPlayControllerRecognizer::DoRecognizeL [Mp4 video]"));
       
   323                     }
       
   324                 else if ( (header.FindF(K3g2Header) == 0) ||
       
   325                           (ext.CompareF(KDot3g2)    == 0) )
       
   326                     {
       
   327                     iDataType = TDataType(K3g2VideoMimeType);
       
   328                     iConfidence = ECertain;
       
   329                     DP0(_L("C3GPAudioPlayControllerRecognizer::DoRecognizeL [3g2 video]"));
       
   330                     }
       
   331                 }// end of else if (err == MP4_OK)
       
   332             }// End FileHandle or FileName Open OK
       
   333         if (mp4Handle)
       
   334             {
       
   335             MP4ParseClose(mp4Handle);
       
   336             }
       
   337         } // End of Extension Comparision If statement
       
   338     }
       
   339 
       
   340 // ========================== OTHER EXPORTED FUNCTIONS =========================
       
   341 
       
   342 // -----------------------------------------------------------------------------
       
   343 // CApaDataRecognizerType* CreateRecognizerL()
       
   344 // Used to create an instance of a CApaDataRecognizerType-derived class
       
   345 // -----------------------------------------------------------------------------
       
   346 //
       
   347 CApaDataRecognizerType* C3GPAudioPlayControllerRecognizer::CreateRecognizerL()
       
   348     {
       
   349 	return new(ELeave) C3GPAudioPlayControllerRecognizer();
       
   350 	}
       
   351 
       
   352 const TImplementationProxy ImplementationTable[] =
       
   353     {
       
   354     IMPLEMENTATION_PROXY_ENTRY(0x101FAFD6, C3GPAudioPlayControllerRecognizer::CreateRecognizerL)
       
   355     };
       
   356 
       
   357 EXPORT_C const TImplementationProxy* ImplementationGroupProxy(TInt& aTableCount)
       
   358     {
       
   359     aTableCount = sizeof(ImplementationTable) / sizeof(TImplementationProxy);
       
   360     return ImplementationTable;
       
   361     }
       
   362 
       
   363 // End of file