upnpsharing/upnpdlnaprofiler/src/upnpdlnaprofiler.cpp
changeset 0 7f85d04be362
equal deleted inserted replaced
-1:000000000000 0:7f85d04be362
       
     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:      Implementation file of CUpnpDlnaProfiler class that is used
       
    15 *                for resolving a DLNA profile of a given file.
       
    16 *
       
    17 */
       
    18 
       
    19 
       
    20 
       
    21 
       
    22 
       
    23 
       
    24 // system includes
       
    25 #include <f32file.h>     // RFile
       
    26 #include <apgcli.h>      // RApaLsSession
       
    27 
       
    28 // user includes
       
    29 #include "upnpdlnaprofiler.h"
       
    30 #include "upnpprofiler.h"
       
    31 #include "upnpaudiosolverbase.h"
       
    32 #include "upnpavsolverbase.h"
       
    33 #include "upnpimagesolverbase.h"
       
    34 #include "upnpresparameters.h"
       
    35 
       
    36 _LIT( KComponentLogfile, "dlnaprofiler.txt");
       
    37 #include "upnplog.h"
       
    38 
       
    39 // constants
       
    40 // NONE
       
    41 
       
    42 // ======== LOCAL FUNCTIONS ========
       
    43 // NONE
       
    44 
       
    45 // ======== MEMBER FUNCTIONS ========
       
    46 
       
    47 // --------------------------------------------------------------------------
       
    48 // CUpnpDlnaProfiler C++ constructor
       
    49 // --------------------------------------------------------------------------
       
    50 //
       
    51 CUpnpDlnaProfiler::CUpnpDlnaProfiler()
       
    52     {
       
    53     __LOG( "[UPnPDlnaProfiler] Constructor" );
       
    54     }
       
    55 
       
    56 
       
    57 // --------------------------------------------------------------------------
       
    58 // CUpnpDlnaProfiler::ConstructL
       
    59 // --------------------------------------------------------------------------
       
    60 //
       
    61 void CUpnpDlnaProfiler::ConstructL()
       
    62     {
       
    63     __LOG( "[UPnPDlnaProfiler] ConstructL" );
       
    64     CUpnpAudioSolverBase* baseAudioSolver = CUpnpAudioSolverBase::NewL();
       
    65     iAudioSolvers.AppendL( baseAudioSolver );
       
    66     
       
    67     CUpnpAvSolverBase* baseAvSolver = CUpnpAvSolverBase::NewL();
       
    68     iAvSolvers.AppendL( baseAvSolver );
       
    69     
       
    70     CUpnpImageSolverBase* baseImageSolver = CUpnpImageSolverBase::NewL();
       
    71     iImageSolvers.AppendL( baseImageSolver );
       
    72     }
       
    73 
       
    74 
       
    75 // --------------------------------------------------------------------------
       
    76 // CUpnpDlnaProfiler::NewL
       
    77 // --------------------------------------------------------------------------
       
    78 //
       
    79 EXPORT_C CUpnpDlnaProfiler* CUpnpDlnaProfiler::NewL()
       
    80     {
       
    81     __LOG( "[UPnPDlnaProfiler] NewL" );
       
    82     CUpnpDlnaProfiler* self = CUpnpDlnaProfiler::NewLC();
       
    83     CleanupStack::Pop( self );
       
    84     return self;
       
    85     }
       
    86 
       
    87 
       
    88 // --------------------------------------------------------------------------
       
    89 // CUpnpDlnaProfiler::NewLC
       
    90 // --------------------------------------------------------------------------
       
    91 //
       
    92 EXPORT_C CUpnpDlnaProfiler* CUpnpDlnaProfiler::NewLC()
       
    93     {
       
    94     __LOG( "[UPnPDlnaProfiler] NewLC" );
       
    95     CUpnpDlnaProfiler* self = new( ELeave ) CUpnpDlnaProfiler;
       
    96     CleanupStack::PushL( self );
       
    97     self->ConstructL();
       
    98     return self;
       
    99     }
       
   100 
       
   101 
       
   102 // --------------------------------------------------------------------------
       
   103 // CUpnpDlnaProfiler destructor
       
   104 // --------------------------------------------------------------------------
       
   105 //
       
   106 CUpnpDlnaProfiler::~CUpnpDlnaProfiler()
       
   107     {
       
   108     __LOG( "[UPnPDlnaProfiler] Destructor" );
       
   109     iAudioSolvers.ResetAndDestroy();
       
   110     iAudioSolvers.Close();
       
   111     
       
   112     iAvSolvers.ResetAndDestroy();
       
   113     iAvSolvers.Close();
       
   114     
       
   115     iImageSolvers.ResetAndDestroy();
       
   116     iImageSolvers.Close();
       
   117     }
       
   118 
       
   119 // --------------------------------------------------------------------------
       
   120 // CUpnpDlnaProfiler::SupportedProfilesL
       
   121 // Returns DlnaProfiles that are supported with currently loaded solver 
       
   122 // plugins.
       
   123 // --------------------------------------------------------------------------
       
   124 //
       
   125 EXPORT_C TInt CUpnpDlnaProfiler::SupportedProfilesL( 
       
   126                                           CDesC16ArrayFlat* aProfiles ) const
       
   127     {
       
   128     __LOG( "[UPnPDlnaProfiler] SupportedProfilesL" );
       
   129     TInt retval = KErrNone;
       
   130 
       
   131     if ( !aProfiles ) 
       
   132         {
       
   133         // Invalid parameter
       
   134         retval = KErrArgument;
       
   135         }
       
   136     else 
       
   137         {
       
   138         // append all audio profiles recognized 
       
   139         for ( TInt i( iAudioSolvers.Count() - 1 ); i >= 0 ; i-- ) 
       
   140             {
       
   141             MUpnpProfiler* currentProfiler = iAudioSolvers[i];
       
   142             currentProfiler->SupportedProfilesL( aProfiles );
       
   143             }
       
   144         // append all av profiles recognized 
       
   145         for ( TInt i( iAvSolvers.Count() - 1 ); i >= 0 ; i-- ) 
       
   146             {
       
   147             MUpnpProfiler* currentProfiler = iAvSolvers[i];
       
   148             currentProfiler->SupportedProfilesL( aProfiles );
       
   149             }
       
   150         // append all image profiles recognized 
       
   151         for ( TInt i( iImageSolvers.Count() - 1 ); i >= 0 ; i-- ) 
       
   152             {
       
   153             MUpnpProfiler* currentProfiler = iImageSolvers[i];
       
   154             currentProfiler->SupportedProfilesL( aProfiles );
       
   155             }
       
   156         }
       
   157         
       
   158     return retval;
       
   159     }
       
   160 
       
   161 // --------------------------------------------------------------------------
       
   162 // CUpnpDlnaProfiler::ProfileForFileL
       
   163 // Resolves the DLNA profile for a file given in aFileName parameter.
       
   164 // Returns NULL if DLNA profile could not be recognized with currently loaded
       
   165 // solver plugins.
       
   166 // --------------------------------------------------------------------------
       
   167 //
       
   168 EXPORT_C HBufC* CUpnpDlnaProfiler::ProfileForFileL( const TDesC& aFilename )
       
   169     {
       
   170     __LOG( "[UPnPDlnaProfiler] ProfileForFileL" );
       
   171     // check that aFileName is reasonable
       
   172     if ( aFilename.Length() <= 0 ) 
       
   173         {
       
   174         User::Leave( KErrArgument );
       
   175         }
       
   176     
       
   177     HBufC* retval = NULL;
       
   178     
       
   179     // try to open file specified by aFileName
       
   180     RFs fsSession;
       
   181     User::LeaveIfError( fsSession.Connect() ); // connect session
       
   182     CleanupClosePushL( fsSession );
       
   183     // without calling ShareProtected() RApaLsSession::RecognizeData 
       
   184     // does not work (leaves with KErrBadHandle).
       
   185     fsSession.ShareProtected();
       
   186     
       
   187     RFile file;  
       
   188     User::LeaveIfError( file.Open( fsSession, 
       
   189                                    aFilename, 
       
   190                                    EFileShareReadersOrWriters|EFileRead ) );
       
   191     CleanupClosePushL( file );
       
   192     
       
   193     // Create an AppArc server session for mime type recognition 
       
   194     RApaLsSession sess;
       
   195     User::LeaveIfError( sess.Connect() );
       
   196     CleanupClosePushL( sess );
       
   197 
       
   198     // Try to get mime type from AppArc server
       
   199     TDataRecognitionResult mimeResult;
       
   200     User::LeaveIfError( sess.RecognizeData( file, mimeResult ) );
       
   201     
       
   202     // close session handle
       
   203     CleanupStack::PopAndDestroy( &sess );
       
   204 
       
   205     // Data recognition done. Check results.
       
   206     HBufC8* mimetype = HBufC8::NewL( mimeResult.iDataType.Des8().Length() );
       
   207     mimetype->Des().Append( mimeResult.iDataType.Des8() );
       
   208         
       
   209     if ( mimetype && mimetype->Length() > 0 ) 
       
   210         {
       
   211         CleanupStack::PushL( mimetype );
       
   212         retval = DlnaProfileFromSolversL( aFilename, *mimetype, file );
       
   213         CleanupStack::PopAndDestroy( mimetype );
       
   214         }
       
   215 
       
   216     // close file and filesession handles
       
   217     CleanupStack::PopAndDestroy( &file );
       
   218     CleanupStack::PopAndDestroy( &fsSession );
       
   219             
       
   220     return retval;
       
   221     }
       
   222     
       
   223 // --------------------------------------------------------------------------
       
   224 // CUpnpDlnaProfiler::ProfileForFileL
       
   225 // Resolves the DLNA profile for a file given in aFileName parameter.
       
   226 // Returns NULL if DLNA profile could not be recognized with currently loaded
       
   227 // solver plugins. Takes RFile and CUpnpResParameters parameters in order to 
       
   228 // minimize the amount of file opening.
       
   229 // --------------------------------------------------------------------------
       
   230 //
       
   231 EXPORT_C HBufC* CUpnpDlnaProfiler::ProfileForFileL( 
       
   232                                         const TDesC& aFilename, 
       
   233                                         RFile& aFile,
       
   234                                         CUpnpResParameters& aParameters ) 
       
   235     {
       
   236     __LOG( "[UPnPDlnaProfiler] ProfileForFileL" );
       
   237     // check that aFileName is reasonable
       
   238     if ( aFilename.Length() <= 0 || aParameters.MimeType().Length() <= 0 ) 
       
   239         {
       
   240         User::Leave( KErrArgument );
       
   241         }
       
   242     
       
   243     HBufC* retval = NULL;
       
   244 
       
   245     retval = DlnaProfileFromSolversL( aFilename, 
       
   246                                       aParameters.MimeType(), 
       
   247                                       aFile );
       
   248             
       
   249     return retval;    
       
   250     }
       
   251     
       
   252 // --------------------------------------------------------------------------
       
   253 // CUpnpDlnaProfiler::DlnaProfileFromSolversL
       
   254 // Checks the mime type given as parameter and forwards the recognition task 
       
   255 // to solver plugins corresponding the mimetype.
       
   256 // --------------------------------------------------------------------------
       
   257 //
       
   258 HBufC* CUpnpDlnaProfiler::DlnaProfileFromSolversL( 
       
   259                                 const TDesC& aFilename, 
       
   260                                 const TDesC8& aMimetype, 
       
   261                                 RFile& aFile )
       
   262     {
       
   263     __LOG( "[UPnPDlnaProfiler] DlnaProfileFromSolversL" );
       
   264     HBufC* retval = NULL;
       
   265 
       
   266     TPtrC8 mediatype = aMimetype.Left( KMimeStartLength );
       
   267     if ( mediatype.Compare( KAudio() ) == 0 ) 
       
   268         {
       
   269         for ( TInt i( iAudioSolvers.Count() - 1 ); i >= 0 && !retval ; i-- ) 
       
   270             {
       
   271             MUpnpProfiler* currentProfiler = iAudioSolvers[i];
       
   272             retval = currentProfiler->ProfileForFileL( aFilename, 
       
   273                                                        aMimetype, 
       
   274                                                        aFile );
       
   275             }
       
   276         }
       
   277     else if ( mediatype.Compare( KVideo() ) == 0 ) 
       
   278         {
       
   279         for ( TInt i( iAvSolvers.Count() - 1 ); i >= 0 && !retval ; i-- ) 
       
   280             {
       
   281             MUpnpProfiler* currentProfiler = iAvSolvers[i];
       
   282             retval = currentProfiler->ProfileForFileL( aFilename, 
       
   283                                                        aMimetype, 
       
   284                                                        aFile );
       
   285             }
       
   286         }
       
   287     else if ( mediatype.Compare( KImage() ) == 0 ) 
       
   288         {
       
   289         for ( TInt i( iImageSolvers.Count() - 1 ); i >= 0 && !retval ; i-- ) 
       
   290             {
       
   291             MUpnpProfiler* currentProfiler = iImageSolvers[i];
       
   292             retval = currentProfiler->ProfileForFileL( aFilename, 
       
   293                                                        aMimetype, 
       
   294                                                        aFile );
       
   295             }
       
   296         }
       
   297     else 
       
   298         {
       
   299         retval = NULL;
       
   300         }
       
   301         
       
   302     return retval;
       
   303     }
       
   304     
       
   305 // end of file