internetradio2.0/filerecognizersrc/irrecognizer.cpp
changeset 8 3b03c28289e6
parent 3 ee64f059b8e1
child 11 f683e24efca3
equal deleted inserted replaced
5:0930554dc389 8:3b03c28289e6
    15 *
    15 *
    16 */
    16 */
    17 
    17 
    18 
    18 
    19 #include <implementationproxy.h>
    19 #include <implementationproxy.h>
       
    20 #include <f32file.h>
    20 
    21 
    21 #include "irdebug.h"
    22 #include "irdebug.h"
    22 #include "irrecognizer.h"
    23 #include "irrecognizer.h"
    23 
    24 
    24 // CONSTANTS
    25 // CONSTANTS
    25 //voilate PC Lint Warning 569: Loss of information (initialization) 
    26 //voilate PC Lint Warning 569: Loss of information (initialization) 
    26 //(32 bits to 31 bits)
    27 //(32 bits to 31 bits)
    27 // Uid of the recogniser
    28 // Uid of the recogniser
    28 //const TUid KUidIRRecognizer={0x2000B499}
    29 //const TUid KUidIRRecognizer={0x2000B499}
    29 // maximum amount of buffer space we will ever use
    30 
    30 const TInt KMaxBufferLength=4*1024;
       
    31 // If the file name length > 4, the file extension might be valid                  
    31 // If the file name length > 4, the file extension might be valid                  
    32 const TInt KPlsFileExtensionsMightBeValid = 4;  
    32 const TInt KPlsFileExtensionsMightBeValid = 4;  
    33 const TInt KM3uFileExtensionsMightBeValid = 4;
    33 const TInt KM3uFileExtensionsMightBeValid = 4;
    34 
    34 
    35 //extension of File to be recognised.
    35 //extension of File to be recognised.
    36 _LIT(KPlsExtension, ".pls");
    36 _LIT(KPlsExtension, ".pls");
    37 //Mime type of the .pls file
    37 //Mime type of the .pls file
    38 _LIT8(KPlsMimeType,"audio/x-scpls");
    38 _LIT8(KPlsMimeType,"audio/x-scpls");
    39 
    39 
    40 _LIT(KM3uExtension, ".m3u");
    40 _LIT(KM3uExtension, ".m3u");
    41 _LIT8(KM3uMimeType, "audio/x-mpegurl");
       
    42 
    41 
    43 
    42 
    44 // ================= MEMBER FUNCTIONS =======================
    43 // ================= MEMBER FUNCTIONS =======================
    45 
    44 
    46 // ---------------------------------------------------------------------------
    45 // ---------------------------------------------------------------------------
    50 //
    49 //
    51 CApaRecognizerEx::CApaRecognizerEx():CApaDataRecognizerType(KIRRecognizerDllUid, 
    50 CApaRecognizerEx::CApaRecognizerEx():CApaDataRecognizerType(KIRRecognizerDllUid, 
    52 	CApaDataRecognizerType::EHigh)
    51 	CApaDataRecognizerType::EHigh)
    53     {
    52     {
    54     IRLOG_DEBUG( "CApaRecognizerEx::CApaRecognizerEx()" );
    53     IRLOG_DEBUG( "CApaRecognizerEx::CApaRecognizerEx()" );
    55     // It supports 2 mime type
    54     // It only supports 1 mime type
    56     iCountDataTypes = 2;
    55     iCountDataTypes = 1;
    57     IRLOG_DEBUG( "CApaRecognizerEx::CApaRecognizerEx() - Exiting." );
    56     IRLOG_DEBUG( "CApaRecognizerEx::CApaRecognizerEx() - Exiting." );
    58     }
    57     }
    59 
    58 
    60 // ---------------------------------------------------------------------------
    59 // ---------------------------------------------------------------------------
    61 // RecognizerEx::~RecognizerEx()
    60 // RecognizerEx::~RecognizerEx()
    87 // ---------------------------------------------------------------------------
    86 // ---------------------------------------------------------------------------
    88 //
    87 //
    89 TUint CApaRecognizerEx::PreferredBufSize()
    88 TUint CApaRecognizerEx::PreferredBufSize()
    90     {
    89     {
    91     IRLOG_DEBUG( "CApaRecognizerEx::PreferredBufSize" );
    90     IRLOG_DEBUG( "CApaRecognizerEx::PreferredBufSize" );
    92     return KMaxBufferLength;
    91     return 0;
    93     }
    92     }
    94 
    93 
    95 // ---------------------------------------------------------------------------
    94 // ---------------------------------------------------------------------------
    96 // RecognizerEx::SupportedDataTypeL()
    95 // RecognizerEx::SupportedDataTypeL()
    97 // Returns supported mime type
    96 // Returns supported mime type
   102     IRLOG_DEBUG( "CApaRecognizerEx::SupportedDataTypeL" );
   101     IRLOG_DEBUG( "CApaRecognizerEx::SupportedDataTypeL" );
   103     if (0 == aIndex)
   102     if (0 == aIndex)
   104 	{
   103 	{
   105         return TDataType(KPlsMimeType);
   104         return TDataType(KPlsMimeType);
   106 	}
   105 	}
   107     else if (1 == aIndex)
   106 	else
   108     {
       
   109         return TDataType(KM3uMimeType);
       
   110     }
       
   111     else
       
   112     {
   107     {
   113         ASSERT(0);
   108         ASSERT(0);
   114         return TDataType(KNullDesC8);
   109         return TDataType(KNullDesC8);
   115     }
   110     }
   116     }
   111     }
   138 			return;
   133 			return;
   139 		}
   134 		}
   140 		else if (aName.Right(KM3uFileExtensionsMightBeValid).CompareF(
   135 		else if (aName.Right(KM3uFileExtensionsMightBeValid).CompareF(
   141             KM3uExtension)==0)
   136             KM3uExtension)==0)
   142 		{
   137 		{
   143 		    RecognizeM3uFileL(aBuffer);
   138 		    RecognizeM3uFileL(aName);
   144 		    IRLOG_DEBUG( "CApaRecognizerEx::DoRecognizeL - Exiting (2)." );
   139 		    IRLOG_DEBUG( "CApaRecognizerEx::DoRecognizeL - Exiting (2)." );
   145 		    return;
   140 		    return;
   146 		}
   141 		}
   147 	    IRLOG_DEBUG( "CApaRecognizerEx::DoRecognizeL - Exiting (3)." );					
   142 	    IRLOG_DEBUG( "CApaRecognizerEx::DoRecognizeL - Exiting (3)." );					
   148 		}
   143 		}
   149     }
   144     }
   150 
   145 
   151 void CApaRecognizerEx::RecognizeM3uFileL(const TDesC8& /*aBuffer*/)
   146 void CApaRecognizerEx::RecognizeM3uFileL(const TDesC& aFileName)
   152 {
   147 {
   153    // _LIT8(KHttpProtocol, "http");
   148     _LIT8(KHttpProtocol, "http");
   154    // _LIT8(KMmsProtocol, "mms");
   149     _LIT8(KMmsProtocol, "mms");
   155    // _LIT8(KRtspProtocol, "rtsp");
   150     _LIT8(KRtspProtocol, "rtsp");
       
   151 
       
   152     RFs fs;
       
   153     User::LeaveIfError(fs.Connect());
       
   154     RFile file;
       
   155     file.Open(fs, aFileName, EFileRead);
       
   156     RBuf8 content;
       
   157     TInt maxLen = 0;
       
   158     file.Size(maxLen);
       
   159     content.Create(maxLen);
       
   160     file.Read(content);
   156     
   161     
   157     //try to use descriptor method to parse the buffer
   162     //try to use descriptor method to parse the buffer
   158     iConfidence = ECertain;
   163     if (CheckStreamingLinks(content, KHttpProtocol) ||
   159     iDataType = TDataType(KM3uMimeType);
   164         CheckStreamingLinks(content, KMmsProtocol) ||
       
   165         CheckStreamingLinks(content, KRtspProtocol))
       
   166     {
       
   167         iConfidence = ECertain;
       
   168         iDataType = TDataType(KPlsMimeType);
       
   169     }
       
   170     
       
   171     content.Close();
       
   172     file.Close();
       
   173     fs.Close();
       
   174 }
       
   175 
       
   176 TBool CApaRecognizerEx::CheckStreamingLinks(TDes8& aBuffer, const TDesC8& aProtocol)
       
   177 {
       
   178     TBool ret = EFalse;
       
   179     _LIT8(KNewLine, "\n");
       
   180     
       
   181     //initially, remain points to aBuffer
       
   182     TPtrC8 remain(aBuffer);
       
   183     TInt newLinePos = remain.Find(KNewLine);
       
   184     
       
   185     while (KErrNotFound != newLinePos)
       
   186     {
       
   187         //copy a new line to RBuf8
       
   188         RBuf8 left;
       
   189         left.Create(remain.Left(newLinePos));
       
   190         left.TrimLeft();
       
   191         left.LowerCase();
       
   192         
       
   193         //after trim left, does this line start with aProtocol?
       
   194         if (left.Left(aProtocol.Length()) == aProtocol)
       
   195         {
       
   196             ret = ETrue;
       
   197         }
       
   198         left.Close();
       
   199         
       
   200         if (ret)
       
   201         {
       
   202             break;
       
   203         }
       
   204         else
       
   205         {
       
   206             //remain points to the right part
       
   207             remain.Set(remain.Right(remain.Length() - newLinePos - 1));
       
   208             newLinePos = remain.Find(KNewLine);
       
   209         }
       
   210     }
       
   211     
       
   212     if (!ret && remain.Length() > 0)
       
   213     {
       
   214         //last line doesn't end with '\n'
       
   215         RBuf8 last;
       
   216         last.Create(remain);
       
   217         last.TrimLeft();
       
   218         last.LowerCase();
       
   219         if (last.Left(aProtocol.Length()) == aProtocol)
       
   220         {
       
   221             ret = ETrue;
       
   222         }
       
   223         last.Close();
       
   224     }
       
   225     
       
   226     return ret;
   160 }
   227 }
   161 
   228 
   162 // -----------------------------------------------------------------------------
   229 // -----------------------------------------------------------------------------
   163 // ImplementationTable
   230 // ImplementationTable
   164 // violates PC lint error :: Warning 611: Suspicious cast
   231 // violates PC lint error :: Warning 611: Suspicious cast