internetradio2.0/irfilerecognizer/src/irrecognizer.cpp
changeset 14 896e9dbc5f19
child 15 065198191975
equal deleted inserted replaced
12:608f67c22514 14:896e9dbc5f19
       
     1 /*
       
     2 * Copyright (c) 2006-2007 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:  ?Description
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 #include <implementationproxy.h>
       
    20 #include <f32file.h>
       
    21 
       
    22 #include "irdebug.h"
       
    23 #include "irrecognizer.h"
       
    24 
       
    25 // CONSTANTS
       
    26 //voilate PC Lint Warning 569: Loss of information (initialization) 
       
    27 //(32 bits to 31 bits)
       
    28 // Uid of the recogniser
       
    29 //const TUid KUidIRRecognizer={0x2000B499}
       
    30 
       
    31 // If the file name length > 4, the file extension might be valid                  
       
    32 const TInt KPlsFileExtensionsMightBeValid = 4;  
       
    33 const TInt KM3uFileExtensionsMightBeValid = 4;
       
    34 
       
    35 //extension of File to be recognised.
       
    36 _LIT(KPlsExtension, ".pls");
       
    37 //Mime type of the .pls file
       
    38 _LIT8(KPlsMimeType,"audio/x-scpls");
       
    39 
       
    40 _LIT(KM3uExtension, ".m3u");
       
    41 
       
    42 
       
    43 // ================= MEMBER FUNCTIONS =======================
       
    44 
       
    45 // ---------------------------------------------------------------------------
       
    46 // RecognizerEx::RecognizerEx()
       
    47 // constructs the object
       
    48 // ---------------------------------------------------------------------------
       
    49 //
       
    50 CApaRecognizerEx::CApaRecognizerEx():CApaDataRecognizerType(KIRRecognizerDllUid, 
       
    51 	CApaDataRecognizerType::EHigh)
       
    52     {
       
    53     IRLOG_DEBUG( "CApaRecognizerEx::CApaRecognizerEx()" );
       
    54     // It only supports 1 mime type
       
    55     iCountDataTypes = 1;
       
    56     IRLOG_DEBUG( "CApaRecognizerEx::CApaRecognizerEx() - Exiting." );
       
    57     }
       
    58 
       
    59 // ---------------------------------------------------------------------------
       
    60 // RecognizerEx::~RecognizerEx()
       
    61 // Destroys the object
       
    62 // ---------------------------------------------------------------------------
       
    63 //
       
    64 CApaRecognizerEx::~CApaRecognizerEx()
       
    65     {
       
    66     IRLOG_DEBUG( "CApaRecognizerEx::~CApaRecognizerEx" );
       
    67     // no implementation
       
    68     }
       
    69 
       
    70 
       
    71 // ---------------------------------------------------------------------------
       
    72 // RecognizerEx::CreateRecognizerL()
       
    73 // Returns pointer to the new object
       
    74 // ---------------------------------------------------------------------------
       
    75 //
       
    76 CApaDataRecognizerType* CApaRecognizerEx::CreateRecognizerL()
       
    77     {
       
    78     IRLOG_DEBUG( "CApaRecognizerEx::CreateRecognizerL" );
       
    79     return new (ELeave) CApaRecognizerEx();
       
    80     }
       
    81 
       
    82 
       
    83 // ---------------------------------------------------------------------------
       
    84 // RecognizerEx::PreferredBufSize()
       
    85 // Returns preferred buffer size
       
    86 // ---------------------------------------------------------------------------
       
    87 //
       
    88 TUint CApaRecognizerEx::PreferredBufSize()
       
    89     {
       
    90     IRLOG_DEBUG( "CApaRecognizerEx::PreferredBufSize" );
       
    91     return 0;
       
    92     }
       
    93 
       
    94 // ---------------------------------------------------------------------------
       
    95 // RecognizerEx::SupportedDataTypeL()
       
    96 // Returns supported mime type
       
    97 // ---------------------------------------------------------------------------
       
    98 //
       
    99 TDataType CApaRecognizerEx::SupportedDataTypeL(TInt aIndex) const
       
   100     {
       
   101     IRLOG_DEBUG( "CApaRecognizerEx::SupportedDataTypeL" );
       
   102     if (0 == aIndex)
       
   103 	{
       
   104         return TDataType(KPlsMimeType);
       
   105 	}
       
   106 	else
       
   107     {
       
   108         ASSERT(0);
       
   109         return TDataType(KNullDesC8);
       
   110     }
       
   111     }
       
   112 
       
   113 // ---------------------------------------------------------------------------
       
   114 // RecognizerEx::DoRecognizeL()
       
   115 // Recognizes the file by name and/or head buffer
       
   116 // ---------------------------------------------------------------------------
       
   117 //
       
   118 void CApaRecognizerEx::DoRecognizeL(const TDesC& aName, 
       
   119 	const TDesC8& aBuffer)
       
   120     {
       
   121 	IRLOG_DEBUG( "CApaRecognizerEx::DoRecognizeL" );    
       
   122     // To keep code simple, we only check file name extension
       
   123     TInt len = aBuffer.Length();
       
   124     if (aName.Length()>KPlsFileExtensionsMightBeValid)
       
   125 		{
       
   126 		//Compare the extension of the file to be recognised with .pls
       
   127 		if (aName.Right(KPlsFileExtensionsMightBeValid).CompareF(
       
   128 			KPlsExtension)==0)
       
   129 	    {
       
   130 			iConfidence=ECertain;   // is certainly something totally new
       
   131 			iDataType=TDataType(KPlsMimeType);
       
   132 			IRLOG_DEBUG( "CApaRecognizerEx::DoRecognizeL - Exiting (1)." );
       
   133 			return;
       
   134 		}
       
   135 		else if (aName.Right(KM3uFileExtensionsMightBeValid).CompareF(
       
   136             KM3uExtension)==0)
       
   137 		{
       
   138 		    RecognizeM3uFileL(aName);
       
   139 		    IRLOG_DEBUG( "CApaRecognizerEx::DoRecognizeL - Exiting (2)." );
       
   140 		    return;
       
   141 		}
       
   142 	    IRLOG_DEBUG( "CApaRecognizerEx::DoRecognizeL - Exiting (3)." );					
       
   143 		}
       
   144     }
       
   145 
       
   146 void CApaRecognizerEx::RecognizeM3uFileL(const TDesC& aFileName)
       
   147 {
       
   148     _LIT8(KHttpProtocol, "http");
       
   149     _LIT8(KMmsProtocol, "mms");
       
   150     _LIT8(KRtspProtocol, "rtsp");
       
   151 
       
   152     RFs fs;
       
   153     User::LeaveIfError(fs.Connect());
       
   154     RFile file;
       
   155     TInt ret = file.Open(fs, aFileName, EFileRead);
       
   156     if (KErrNone != ret)
       
   157     {
       
   158         fs.Close();
       
   159         return;
       
   160     }
       
   161     
       
   162     RBuf8 content;
       
   163     CleanupClosePushL(content);
       
   164     TInt maxLen = 0;
       
   165     file.Size(maxLen);
       
   166     content.CreateL(maxLen);
       
   167     file.Read(content);
       
   168     
       
   169     //try to use descriptor method to parse the buffer
       
   170     if (CheckStreamingLinksL(content, KHttpProtocol) ||
       
   171         CheckStreamingLinksL(content, KMmsProtocol) ||
       
   172         CheckStreamingLinksL(content, KRtspProtocol))
       
   173     {
       
   174         iConfidence = ECertain;
       
   175         iDataType = TDataType(KPlsMimeType);
       
   176     }
       
   177     
       
   178     CleanupStack::PopAndDestroy();
       
   179     file.Close();
       
   180     fs.Close();
       
   181 }
       
   182 
       
   183 TBool CApaRecognizerEx::CheckStreamingLinksL(TDes8& aBuffer, const TDesC8& aProtocol)
       
   184 {
       
   185     TBool ret = EFalse;
       
   186     _LIT8(KNewLine, "\n");
       
   187     
       
   188     //initially, remain points to aBuffer
       
   189     TPtrC8 remain(aBuffer);
       
   190     TInt newLinePos = remain.Find(KNewLine);
       
   191     
       
   192     while (KErrNotFound != newLinePos)
       
   193     {
       
   194         //copy a new line to RBuf8
       
   195         RBuf8 left;
       
   196         CleanupClosePushL(left);
       
   197         left.CreateL(remain.Left(newLinePos));
       
   198         left.TrimLeft();
       
   199         left.LowerCase();
       
   200         
       
   201         //after trim left, does this line start with aProtocol?
       
   202         if (left.Left(aProtocol.Length()) == aProtocol)
       
   203         {
       
   204             ret = ETrue;
       
   205         }
       
   206         CleanupStack::PopAndDestroy();
       
   207         
       
   208         if (ret)
       
   209         {
       
   210             break;
       
   211         }
       
   212         else
       
   213         {
       
   214             //remain points to the right part
       
   215             remain.Set(remain.Right(remain.Length() - newLinePos - 1));
       
   216             newLinePos = remain.Find(KNewLine);
       
   217         }
       
   218     }
       
   219     
       
   220     if (!ret && remain.Length() > 0)
       
   221     {
       
   222         //last line doesn't end with '\n'
       
   223         RBuf8 last;
       
   224         CleanupClosePushL(last);
       
   225         last.CreateL(remain);
       
   226         last.TrimLeft();
       
   227         last.LowerCase();
       
   228         if (last.Left(aProtocol.Length()) == aProtocol)
       
   229         {
       
   230             ret = ETrue;
       
   231         }
       
   232         CleanupStack::PopAndDestroy();
       
   233     }
       
   234     
       
   235     return ret;
       
   236 }
       
   237 
       
   238 // -----------------------------------------------------------------------------
       
   239 // ImplementationTable
       
   240 // violates PC lint error :: Warning 611: Suspicious cast
       
   241 // required by the ECOM framework to correctly identify the instantiation 
       
   242 // method pointer to provide to a client's resolution request.
       
   243 // -----------------------------------------------------------------------------
       
   244 //
       
   245 const TImplementationProxy ImplementationTable[] =
       
   246     {
       
   247     IMPLEMENTATION_PROXY_ENTRY(KIRRecognizerImplementationUid, 
       
   248     	CApaRecognizerEx::CreateRecognizerL)
       
   249     };
       
   250     
       
   251 // ---------------------------------------------------------------------------
       
   252 // ImplementationGroupProxy
       
   253 // Provides access to the implementationtable
       
   254 // ---------------------------------------------------------------------------
       
   255 //
       
   256 EXPORT_C const TImplementationProxy* ImplementationGroupProxy(
       
   257 	TInt& aTableCount)
       
   258     {
       
   259     IRLOG_DEBUG( "ImplementationGroupProxy" );
       
   260     aTableCount = sizeof(ImplementationTable) / sizeof(TImplementationProxy);
       
   261     IRLOG_DEBUG( "ImplementationGroupProxy - Exiting." );
       
   262     return ImplementationTable;
       
   263     }
       
   264 
       
   265 
       
   266