internetradio2.0/filerecognizersrc/irrecognizer.cpp
changeset 14 896e9dbc5f19
parent 12 608f67c22514
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     TInt maxLen = 0;
       
   164     file.Size(maxLen);
       
   165     content.Create(maxLen);
       
   166     file.Read(content);
       
   167     
       
   168     //try to use descriptor method to parse the buffer
       
   169     if (CheckStreamingLinks(content, KHttpProtocol) ||
       
   170         CheckStreamingLinks(content, KMmsProtocol) ||
       
   171         CheckStreamingLinks(content, KRtspProtocol))
       
   172     {
       
   173         iConfidence = ECertain;
       
   174         iDataType = TDataType(KPlsMimeType);
       
   175     }
       
   176     
       
   177     content.Close();
       
   178     file.Close();
       
   179     fs.Close();
       
   180 }
       
   181 
       
   182 TBool CApaRecognizerEx::CheckStreamingLinks(TDes8& aBuffer, const TDesC8& aProtocol)
       
   183 {
       
   184     TBool ret = EFalse;
       
   185     _LIT8(KNewLine, "\n");
       
   186     
       
   187     //initially, remain points to aBuffer
       
   188     TPtrC8 remain(aBuffer);
       
   189     TInt newLinePos = remain.Find(KNewLine);
       
   190     
       
   191     while (KErrNotFound != newLinePos)
       
   192     {
       
   193         //copy a new line to RBuf8
       
   194         RBuf8 left;
       
   195         left.Create(remain.Left(newLinePos));
       
   196         left.TrimLeft();
       
   197         left.LowerCase();
       
   198         
       
   199         //after trim left, does this line start with aProtocol?
       
   200         if (left.Left(aProtocol.Length()) == aProtocol)
       
   201         {
       
   202             ret = ETrue;
       
   203         }
       
   204         left.Close();
       
   205         
       
   206         if (ret)
       
   207         {
       
   208             break;
       
   209         }
       
   210         else
       
   211         {
       
   212             //remain points to the right part
       
   213             remain.Set(remain.Right(remain.Length() - newLinePos - 1));
       
   214             newLinePos = remain.Find(KNewLine);
       
   215         }
       
   216     }
       
   217     
       
   218     if (!ret && remain.Length() > 0)
       
   219     {
       
   220         //last line doesn't end with '\n'
       
   221         RBuf8 last;
       
   222         last.Create(remain);
       
   223         last.TrimLeft();
       
   224         last.LowerCase();
       
   225         if (last.Left(aProtocol.Length()) == aProtocol)
       
   226         {
       
   227             ret = ETrue;
       
   228         }
       
   229         last.Close();
       
   230     }
       
   231     
       
   232     return ret;
       
   233 }
       
   234 
       
   235 // -----------------------------------------------------------------------------
       
   236 // ImplementationTable
       
   237 // violates PC lint error :: Warning 611: Suspicious cast
       
   238 // required by the ECOM framework to correctly identify the instantiation 
       
   239 // method pointer to provide to a client's resolution request.
       
   240 // -----------------------------------------------------------------------------
       
   241 //
       
   242 const TImplementationProxy ImplementationTable[] =
       
   243     {
       
   244     IMPLEMENTATION_PROXY_ENTRY(KIRRecognizerImplementationUid, 
       
   245     	CApaRecognizerEx::CreateRecognizerL)
       
   246     };
       
   247     
       
   248 // ---------------------------------------------------------------------------
       
   249 // ImplementationGroupProxy
       
   250 // Provides access to the implementationtable
       
   251 // ---------------------------------------------------------------------------
       
   252 //
       
   253 EXPORT_C const TImplementationProxy* ImplementationGroupProxy(
       
   254 	TInt& aTableCount)
       
   255     {
       
   256     IRLOG_DEBUG( "ImplementationGroupProxy" );
       
   257     aTableCount = sizeof(ImplementationTable) / sizeof(TImplementationProxy);
       
   258     IRLOG_DEBUG( "ImplementationGroupProxy - Exiting." );
       
   259     return ImplementationTable;
       
   260     }
       
   261 
       
   262 
       
   263