os/security/contentmgmt/cafstreamingsupport/source/keystreamdecoder.cpp
author sl@SLION-WIN7.fritz.box
Fri, 15 Jun 2012 03:10:57 +0200
changeset 0 bde4ae8d615e
permissions -rw-r--r--
First public contribution.
     1 // Copyright (c) 2007-2009 Nokia Corporation and/or its subsidiary(-ies).
     2 // All rights reserved.
     3 // This component and the accompanying materials are made available
     4 // under the terms of the License "Eclipse Public License v1.0"
     5 // which accompanies this distribution, and is available
     6 // at the URL "http://www.eclipse.org/legal/epl-v10.html".
     7 //
     8 // Initial Contributors:
     9 // Nokia Corporation - initial contribution.
    10 //
    11 // Contributors:
    12 //
    13 // Description:
    14 //
    15 
    16 #include <caf/streaming/keystreamdecoder.h>
    17 #include <caf/streaming/streamagentfactory.h>
    18 #include "streamagentresolver.h"
    19 #include <caf/streaming/protectedstreamdesc.h>
    20 #include "scaflog.h"
    21 #include <caf/supplier.h>
    22 #include <caf/metadataarray.h>
    23 
    24 using namespace StreamAccess;
    25  
    26 EXPORT_C CKeyStreamDecoder* CKeyStreamDecoder::NewL(const CProtectedStreamDesc& aProtectedStreamDesc, const CSdpMediaField& aSdpKeyStream, const CSdpDocument& aSdpDoc)
    27 	{
    28 	CKeyStreamDecoder* self = CKeyStreamDecoder::NewLC(aProtectedStreamDesc,aSdpKeyStream,aSdpDoc);
    29 	CleanupStack::Pop(self);
    30 	return self;
    31 	}
    32 
    33 EXPORT_C CKeyStreamDecoder* CKeyStreamDecoder::NewLC(const CProtectedStreamDesc& aProtectedStreamDesc, const CSdpMediaField& aSdpKeyStream, const CSdpDocument& aSdpDoc)
    34 	{
    35 	CKeyStreamDecoder* self = new(ELeave) CKeyStreamDecoder();
    36 	CleanupStack::PushL(self);
    37 	self->ConstructL(aProtectedStreamDesc,aSdpKeyStream,aSdpDoc);
    38 	return self;
    39 	}
    40 
    41 CKeyStreamDecoder::CKeyStreamDecoder()
    42 	{
    43 	}
    44 
    45 CKeyStreamDecoder::~CKeyStreamDecoder()
    46 	{
    47 	delete iAgentKeyStreamDecoder;
    48 	delete iStreamAgentFactory;
    49 	
    50 	REComSession::DestroyedImplementation(iEcomKey);
    51 	REComSession::FinalClose();	
    52 	}
    53 
    54 void CKeyStreamDecoder::ConstructL(const CProtectedStreamDesc& aProtectedStreamDesc, const CSdpMediaField& aSdpKeyStream, const CSdpDocument& aSdpDoc)
    55 	{
    56 	DEBUG_PRINTF(_L("Creating a key stream decoder object."));
    57 	
    58 	// Create an instance of the Stream Agent Resolver
    59 	CStreamAgentResolver* resolver = CStreamAgentResolver::NewLC();
    60 	
    61 	/* Retrieve a reference to the CStreamAgentInfo object, associated to the resolved stream agent plugin 
    62 	 * capable of supporting the supplied SDP media description 
    63 	 */
    64 	CStreamAgentInfo& agentInfo = resolver->ResolveSdpKeyStreamL(aSdpKeyStream);
    65 	
    66 	/* Create a handle to the factory object (CStreamAgentFactory) associated to the
    67 	 * resolved stream agent, so that it can be successfully destroyed and freed having been used 
    68 	 */
    69 	iStreamAgentFactory = static_cast<CStreamAgentFactory *>(REComSession::CreateImplementationL(agentInfo.ImplementationUid(), iEcomKey));
    70 	
    71 	// Instantiate the necessary implementation of key stream sink
    72 	CKeyStreamSink* keyStreamSink = aProtectedStreamDesc.CreateKeyStreamSinkLC();
    73 	
    74 	// Retrieve a reference to the stream agents factory
    75 	CStreamAgentFactory& agentFactory = agentInfo.StreamAgentFactory();
    76 	
    77 	/* The factory is then used to generate an instance of the CAgentKeyStreamDecoder, using the instantiated key
    78 	 * stream sink and the SDP media description
    79 	 */
    80 	iAgentKeyStreamDecoder = agentFactory.GetKeyStreamDecoderL(*keyStreamSink,aSdpKeyStream,aSdpDoc);
    81 	
    82 	CleanupStack::PopAndDestroy(2,resolver); //< keyStreamSink, resolver
    83 	
    84 	DEBUG_PRINTF(_L("The key stream decoder object has been created successfully."));
    85 	}
    86 	
    87 EXPORT_C void CKeyStreamDecoder::GetAttributeL(const TAttribute& aAttribute, TBool& aValue) const
    88 	{
    89 	iAgentKeyStreamDecoder->GetAttributeL(aAttribute, aValue);
    90 	}
    91 
    92 EXPORT_C HBufC* CKeyStreamDecoder::GetStringAttributeLC(const TStringAttribute& aAttribute) const
    93 	{
    94 	return iAgentKeyStreamDecoder->GetStringAttributeLC(aAttribute);
    95 	}
    96 
    97 
    98 EXPORT_C ContentAccess::CImportFile* CKeyStreamDecoder::CreateImportSessionLC(const TDesC8& aContentMimeType, const TDesC& aSuggestedName, const TDesC& aOutputDirectory) const
    99 	{
   100 	return DoCreateImportSessionLC(aContentMimeType, aSuggestedName, aOutputDirectory);
   101 	}
   102 
   103 EXPORT_C ContentAccess::CImportFile* CKeyStreamDecoder::CreateImportSessionLC(const TDesC8& aContentMimeType) const
   104 	{
   105 	return DoCreateImportSessionLC(aContentMimeType, KNullDesC(), KNullDesC());
   106 	}
   107 
   108 ContentAccess::CImportFile* CKeyStreamDecoder::DoCreateImportSessionLC(const TDesC8& aContentMimeType, const TDesC& aSuggestedName, const TDesC& aOutputDirectory) const
   109 	{
   110 	DEBUG_PRINTF(_L("Creating an import session for post-acquisition content."));
   111 	
   112 	// Handle to the post-acquisition rights object file owned by the agent
   113 	RFile   fPostAcq;
   114 	// The content Id of the pos-acquisition rights object.
   115 	RBuf8 contentId;
   116 	contentId.CreateL(KMimeTypesMaxLength);
   117 	contentId.CleanupClosePushL();
   118 	// The mime type which will be used to import the rights object
   119 	RBuf8 mimeTypeRights; 
   120 	mimeTypeRights.CreateL(KMimeTypesMaxLength);
   121 	mimeTypeRights.CleanupClosePushL();
   122 	// The mime type which will be used to import the content file.
   123 	RBuf8 mimeTypeContent;
   124 	mimeTypeContent.CreateL(KMimeTypesMaxLength);
   125 	mimeTypeContent.CleanupClosePushL();
   126 	
   127 	iAgentKeyStreamDecoder->GetPostDeliveryRightsL(fPostAcq, contentId, mimeTypeRights, mimeTypeContent);
   128 	CleanupClosePushL(fPostAcq);
   129 	
   130 	DEBUG_PRINTF2(_L8("Post-acquisition RO's Mime Type: (%S)"), &mimeTypeRights);
   131 	DEBUG_PRINTF2(_L8("Post-acquisition Content's Mime Type: (%S)"), &mimeTypeContent);
   132 	
   133 	// Create a supplier
   134 	ContentAccess::CSupplier* supplier = ContentAccess::CSupplier::NewLC();
   135 	// Check if there is any CAF agent supporting postacquisition rights object import
   136 	if(!supplier->IsImportSupported(mimeTypeRights))
   137 		{
   138 		DEBUG_PRINTF2(_L8("A CAF agent capable of importing (%S) mime type can not be found!"), &mimeTypeRights);
   139 		User::Leave(KErrNotSupported);
   140 		}
   141 	// if everything went successfully, create a file import object and import the rights object
   142 	// Create meta-data array
   143 	ContentAccess::CMetaDataArray* metaDataArray = ContentAccess::CMetaDataArray::NewLC();
   144 
   145 	// The content Id is provided as suggested name to allow the agent to generate the output file
   146 	HBufC* roName = HBufC::NewLC(contentId.Length());
   147 	TPtr roPtr (roName->Des());
   148 	roPtr.Copy(contentId);
   149 	// Create the import object for the rights object file
   150 	ContentAccess::CImportFile* importRo =  supplier->ImportFileL(mimeTypeRights, *metaDataArray, *roName);
   151 	CleanupStack::PopAndDestroy(roName);
   152 	CleanupStack::PushL(importRo);
   153 	
   154 	TBuf8<128> buf;
   155 	TInt dataLen = 0;
   156 	// Start importing the content of the post-acquisition RO file
   157 	do
   158 		{
   159 		User::LeaveIfError(fPostAcq.Read(buf));
   160 		dataLen = buf.Length();
   161 		User::LeaveIfError(importRo->WriteData(buf));
   162 		} while(dataLen > 0);
   163 	// Post-acquisition rigths object import is being completed
   164 	User::LeaveIfError(importRo->WriteDataComplete());
   165 	CleanupStack::PopAndDestroy(importRo);
   166 	
   167 	//The meta data field of the content Id. A content Id is passed to an agent through this meta-data field.
   168 	_LIT8(KMetaDataFieldCid, "cid");
   169 	// Add the (postacq) RO's content id used to match the content with the RO
   170 	metaDataArray->AddL(KMetaDataFieldCid(), contentId);
   171 	
   172 	//The meta data field of the mime type. A mime type is passed to an agent through this meta-data field.
   173 	_LIT8(KMetaDataFieldMimeType, "mimetype");
   174 	// Add the mime type of the protected streamed data (e.g. video/3gpp)
   175 	metaDataArray->AddL(KMetaDataFieldMimeType(), aContentMimeType);
   176 	
   177 	// Create the import object for the protected streamed content
   178 	ContentAccess::CImportFile* importContent(0);
   179 	if((aSuggestedName.Length() > 0) && (aOutputDirectory.Length() > 0))
   180 		{
   181 		// A file name is suggested. The client wants the agent to generate the output files.
   182 		DEBUG_PRINTF2(_L("Output filename (%S) is suggested."), &aSuggestedName);
   183 		supplier->SetOutputDirectoryL(aOutputDirectory);
   184 		importContent =  supplier->ImportFileL(mimeTypeContent, *metaDataArray, aSuggestedName);
   185 		}
   186 	else
   187 		{
   188 		// No suggested file name is passed to the agent, the client will provide output files for the agent to use
   189 		DEBUG_PRINTF(_L("Output filename is not suggested."));
   190 		importContent =  supplier->ImportFileL(mimeTypeContent, *metaDataArray);
   191 		}
   192 	
   193 	CleanupStack::PopAndDestroy(6, &contentId);
   194 	CleanupStack::PushL(importContent);
   195 	
   196 	DEBUG_PRINTF(_L("Import session has been created successfully."));
   197 	
   198 	return importContent;
   199 	}