mmlibs/mmfw/inc/mmf/server/mmfdatapath.h
changeset 0 40261b775718
equal deleted inserted replaced
-1:000000000000 0:40261b775718
       
     1 // Copyright (c) 2001-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 "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 // source\server\mmfdatapath.h
       
    15 // 
       
    16 //
       
    17 
       
    18 #ifndef MMFDATAPATH_H
       
    19 #define MMFDATAPATH_H
       
    20 
       
    21 #include <mmf/common/mmfcontroller.h>
       
    22 #include <mmf/server/mmfdatasource.h>
       
    23 #include <mmf/server/mmfdatasink.h>
       
    24 #include <mmf/server/mmfcodec.h>
       
    25 #include <mmf/server/mmfbuffer.h>
       
    26 #include <mmf/common/mmfutilities.h>
       
    27 #include <mmf/server/mmfformat.h>
       
    28 #include <mmf/server/mmfsubthreadbase.h>
       
    29 
       
    30 /**
       
    31  * @publishedAll
       
    32  * @deprecated
       
    33  *
       
    34  * Datapath general error code used when sending events to clients
       
    35  */
       
    36 const TUid KMMFErrorCategoryDataPathGeneralError = {0x101F76DC};
       
    37 
       
    38 
       
    39 
       
    40 /**
       
    41 @publishedAll
       
    42 @deprecated
       
    43 
       
    44 Abstract utility class that moves data from a single data source to a single data sink, via a codec if required.
       
    45 
       
    46 All functions are exported form the DLL and are virtual to allow plugins to define their own data paths.
       
    47 */
       
    48 class CMMFDataPath : public CActive,
       
    49 					 public MDataSink,
       
    50 					 public MDataSource, 
       
    51 					 public MAsyncEventHandler
       
    52 	{ 
       
    53 public:
       
    54 
       
    55 	//CMMFDataPath constructor if codec Uid is not already known by CMMFController
       
    56 	//and there is no data path ambiguity - ie only one data path is possible
       
    57 	IMPORT_C static CMMFDataPath* NewL(MAsyncEventHandler& aEventHandler); 
       
    58 
       
    59 	//CMMFDataPath constructor if codec Uid is not already known by CMMFController
       
    60 	//and there is ambiguity ie more than one possible data path TMediaId used to select path 
       
    61 	IMPORT_C static CMMFDataPath* NewL(TMediaId aMediaId, MAsyncEventHandler& aEventHandler); 
       
    62 
       
    63 	//CMMFDataPath constructor if codec Uid is already known by CMMFController
       
    64 	//and there is no data path ambiguity - ie only one data path is possible
       
    65 	IMPORT_C static CMMFDataPath* NewL(TUid aCodecUid, MAsyncEventHandler& aEventHandler); 
       
    66 
       
    67 	//CMMFDataPath constructor if codec Uid is already known by CMMFController
       
    68 	//and there is ambiguity ie more than one possible data path TMediaId used to select path 
       
    69 	IMPORT_C static CMMFDataPath* NewL(TUid aCodecUid, TMediaId aMediaId, MAsyncEventHandler& aEventHandler);
       
    70 
       
    71 	IMPORT_C virtual ~CMMFDataPath();
       
    72 
       
    73 	// Called when source and sink needs to be de-referenced
       
    74 	IMPORT_C virtual void ResetL();
       
    75 
       
    76 	//from MDataSink - CMMFData path is a sink to its MDataSource
       
    77 	IMPORT_C virtual void EmptyBufferL(CMMFBuffer* aBuffer, MDataSource* aSupplier, TMediaId aMediaId); //only required for an active push MDataSource requesting a buffer empty - not implemented
       
    78 	IMPORT_C virtual void BufferFilledL(CMMFBuffer* aBuffer); //called by the CMMFDataPath's MDataSource when it has filled the buffer
       
    79 	IMPORT_C virtual TBool CanCreateSinkBuffer(); //from both MDataSource & MDataSink?
       
    80 	IMPORT_C virtual CMMFBuffer* CreateSinkBufferL(TMediaId aMediaId); //CMMFDataPath can't create buffers
       
    81 	IMPORT_C virtual CMMFBuffer* CreateSinkBufferL(TMediaId aMediaId, TBool &aReference); //CMMFDataPath can't create buffers
       
    82 
       
    83 	IMPORT_C virtual TFourCC SinkDataTypeCode(TMediaId aMediaId);
       
    84 
       
    85 	//from MDataSource
       
    86 	IMPORT_C virtual void FillBufferL(CMMFBuffer* aBuffer, MDataSink* aConsumer, TMediaId aMediaId); //only required for an active pull MDataSink requesting buffer fill - not implementated
       
    87 	IMPORT_C virtual void BufferEmptiedL(CMMFBuffer* aBuffer);//called by the CMMFDataPath's MDataSink when it has emptied the buffer
       
    88 	IMPORT_C virtual TBool CanCreateSourceBuffer(); //from both MDataSource & MDataSink?
       
    89 	IMPORT_C virtual CMMFBuffer* CreateSourceBufferL(TMediaId aMediaId); //CMMFDataPath can't create buffers
       
    90 	IMPORT_C virtual CMMFBuffer* CreateSourceBufferL(TMediaId aMediaId, TBool &aReference); //CMMFDataPath can't create buffers
       
    91 	IMPORT_C virtual TFourCC SourceDataTypeCode(TMediaId aMediaId);
       
    92 
       
    93 	IMPORT_C virtual void AddDataSourceL(MDataSource* aSource);
       
    94 	IMPORT_C virtual void AddDataSinkL(MDataSink* aSink);
       
    95 
       
    96 	//could derive these from a mixin eg MMMFControle later for active MDataSource/Sinks
       
    97 	IMPORT_C virtual void PrimeL();
       
    98 	IMPORT_C virtual void PlayL();
       
    99 	IMPORT_C virtual void Pause();
       
   100 	IMPORT_C virtual void Stop();
       
   101 
       
   102 	IMPORT_C virtual TTimeIntervalMicroSeconds Position() const;
       
   103 	IMPORT_C virtual void SetPositionL(const TTimeIntervalMicroSeconds& aPosition);
       
   104 
       
   105 	// Play Window.
       
   106 	IMPORT_C virtual void SetPlayWindowL( const TTimeIntervalMicroSeconds& aStart, const TTimeIntervalMicroSeconds& aEnd ) ;
       
   107 	IMPORT_C virtual void ClearPlayWindowL() ;
       
   108 
       
   109 	//State
       
   110 	IMPORT_C virtual TInt State();
       
   111 
       
   112 	//CActive implementations
       
   113 	IMPORT_C void RunL();
       
   114 	IMPORT_C void DoCancel();
       
   115 	IMPORT_C TInt RunError(TInt aError);
       
   116 
       
   117 	//error handling
       
   118 	IMPORT_C TInt DoSendEventToClient(TUid aEventType, TInt aErrorCode);
       
   119 
       
   120 	IMPORT_C TInt SetBlockLength(TUint aBlockLength);
       
   121 	/**
       
   122 	
       
   123 	Indicates the state of the data path.
       
   124 
       
   125 	Mimics typical MultiMedia behaviour of stopped, primed and playing
       
   126 	*/
       
   127 	enum TDataPathState
       
   128 		{
       
   129 		/** Stopped.
       
   130 		*/
       
   131 		EStopped,
       
   132 		/** Primed.
       
   133 		*/
       
   134 		EPrimed,
       
   135 		/** Playing.
       
   136 		*/
       
   137 		EPlaying,
       
   138 		/** Recording.
       
   139 		*/
       
   140 		ERecording,
       
   141 		/** Converting.
       
   142 		*/
       
   143 		EConverting
       
   144 		};
       
   145 
       
   146 	/**
       
   147 	@internalComponent
       
   148 	*/
       
   149 	class CCompleteCallback : public CActive
       
   150 		{
       
   151 	public:
       
   152 		CCompleteCallback(CMMFDataPath& aDataPath, TBool aWaitForSink);
       
   153 		~CCompleteCallback();
       
   154 
       
   155 		void SignalDataPathComplete(TInt aDataPathError);
       
   156 		void SignalSinkComplete(TInt aSinkError);
       
   157 		TRequestStatus& ActiveStatus();
       
   158 
       
   159 		void DoCancel();
       
   160 	private:
       
   161 		void RunL();
       
   162 		CMMFDataPath& iDataPath;
       
   163 		TBool iDataPathComplete;
       
   164 		TBool iSinkComplete;
       
   165 		TBool iWaitForSink;
       
   166 
       
   167 		TInt iSinkError;
       
   168 		TInt iDataPathError;
       
   169 		};
       
   170 	friend class CCompleteCallback;
       
   171 	friend class CMMFDataPath2;
       
   172 protected:
       
   173 
       
   174 	/**
       
   175 	Indicates the transfer state.
       
   176 	
       
   177 	Buffers maybe be filled, emptied, or have "one" shot initialisatings performed upon them.
       
   178 	
       
   179 	TTransferState is used within the datapath RunL which drives databuffer exchange.
       
   180 	*/
       
   181 	enum TTransferState
       
   182 		{
       
   183 		/** Waiting on a BufferEmptied callback from sink
       
   184 		*/
       
   185 		EWaitSink,
       
   186 		/** Waiting on a BufferFilled callback from source
       
   187 		*/
       
   188 		EWaitSource,
       
   189 		/** Initialize the sink.
       
   190 		*/
       
   191 		EInitializeSink,
       
   192 		/** Initialize the source.
       
   193 		*/
       
   194 		EInitializeSource,
       
   195 		/** Source buffer does not contain data.
       
   196 		*/
       
   197 		ENeedSourceData,
       
   198 		/** Sink buffer does not contain data.
       
   199 		*/
       
   200 		ENeedSinkData,
       
   201 		/** There is more source data to send to the sink and need to match sink and source.
       
   202 		*/
       
   203 		ENeedToMatchSourceToSink,
       
   204 		/** Outstanding data to send to sink.
       
   205 		*/
       
   206 		ESendDataToSink,
       
   207 		/** End of data.
       
   208 		*/
       
   209 		EEndOfData //indicates that the datapath has transferred all the data to the sink
       
   210 		};
       
   211 
       
   212 
       
   213 
       
   214 
       
   215 protected:
       
   216 	CMMFDataPath(TMediaId aMediaId, MAsyncEventHandler& aEventHandler) : 
       
   217 		CActive(EPriorityStandard), MDataSink(KUidMmfDataPath), MDataSource(KUidMmfDataPath),
       
   218 		iEventHandler(aEventHandler), iMediaId(aMediaId), iState(EStopped) 
       
   219 			{CActiveScheduler::Add(this);};
       
   220 
       
   221 	IMPORT_C void ConstructL(TUid aCodecUid = KNullUid);
       
   222 	IMPORT_C virtual void ConstructSourceL( const TDesC8& aInitData ) ;
       
   223 	IMPORT_C virtual void ConstructSinkL( const TDesC8& aInitData ) ;
       
   224 	IMPORT_C virtual void EndOfData();
       
   225 
       
   226 	//These methods use API on DevSound to determine how many samples have been played/recorded
       
   227 	//and thereby determine the real position.
       
   228 	TTimeIntervalMicroSeconds CalculateAudioOutputPosition() const;
       
   229 	TTimeIntervalMicroSeconds CalculateAudioInputPosition() const;
       
   230 
       
   231 	//These methods determine the position of either the input or the output.
       
   232 	//The position may come from a format or from an audio input/output depending on 
       
   233 	//the operation the datapath is doing (Playing (output), Recording & Converting (input))
       
   234 	TTimeIntervalMicroSeconds OutputPosition() const;
       
   235 	TTimeIntervalMicroSeconds InputPosition() const;
       
   236 
       
   237 
       
   238 	//Determines what buffers are required (see TNeedBuffer)
       
   239 	//May leave KErrNotSupported if source/sinks can't supply the necessary buffers
       
   240 	TInt DetermineBuffersToUseL(void) const; 
       
   241 
       
   242 	//Determines how many samples have been played by DevSound
       
   243 	TInt AudioSamplesPlayed() const;
       
   244 
       
   245 	//Determines how many samples have been recorded by DevSound
       
   246 	TInt AudioSamplesRecorded() const;
       
   247 
       
   248 
       
   249 private:
       
   250 	void ChangeDataPathTransferState(TTransferState aNewDataPathTransferState);
       
   251 	void CreateDataPathL(MDataSource* aSource, MDataSink* aSink);
       
   252 	
       
   253 	void InitializeSinkL();
       
   254 	void InitializeSourceL();
       
   255 	void FillSourceBufferL();
       
   256 	void FillSinkBufferL();
       
   257 	void EmptySinkBufferL();
       
   258 	void DoCleanupBuffers();
       
   259 	void ObtainSyncBuffersL(); //tries to obtain synchronous data buffers from src/snk
       
   260 
       
   261 	TTimeIntervalMicroSeconds Duration() const;
       
   262 
       
   263 	void DoStopL();
       
   264 	void DoPauseL();
       
   265 	void DoEndOfDataL();
       
   266 
       
   267 	IMPORT_C TInt SendEventToClient(const TMMFEvent& aEvent);
       
   268 
       
   269 	void SetBuffersAvailable();
       
   270 	void ResetRefBuffers();
       
   271 
       
   272 	
       
   273 
       
   274 protected:
       
   275 	/**
       
   276     Event handler.
       
   277 	*/
       
   278 	MAsyncEventHandler& iEventHandler;
       
   279 
       
   280 	/** 
       
   281 	The source of data to which the CMMFDataPath is a MDataSink for.
       
   282 	*/
       
   283 	MDataSource* iDataSource;
       
   284 	/** 
       
   285 	The sink of data for which the CMMFDataPath is a MDataSource for
       
   286 	*/
       
   287 	MDataSink* iDataSink;
       
   288 	/** 
       
   289 	Set to true when the sink is able to accept data. EFalse otherwise.
       
   290 	*/
       
   291 	TBool iSinkCanReceive;
       
   292 	/**
       
   293 	The sink's data type. Same as the codec input data type.
       
   294 	*/
       
   295 	TFourCC iSinkFourCC;
       
   296 	/** 
       
   297 	The source's data type. Same as the codec output data type.
       
   298 	*/
       
   299 	TFourCC iSourceFourCC;
       
   300 	/** 
       
   301 	Identifies which media type and stream within MDataSource.
       
   302 	*/
       
   303 	TMediaId iMediaId;
       
   304 	/** 
       
   305 	Codec in use. Null Codec is signified by == NULL
       
   306 	*/
       
   307 	CMMFCodec* iCodec;
       
   308 
       
   309 	/**
       
   310     Result of processing the codec.
       
   311 	*/
       
   312 	TCodecProcessResult iCodecProcessResult;
       
   313 
       
   314 	/**
       
   315 	Set to ETrue when the data path has a source and a sink and can send data from the source to the sink.
       
   316 	*/
       
   317 	TBool iDataPathCreated;
       
   318 	
       
   319 	/**
       
   320 	Current data path state. 
       
   321 	@see TDataPathState	
       
   322 	*/
       
   323 	TDataPathState iState;
       
   324 
       
   325 	/**
       
   326     Current transfer state.
       
   327 	@see TTransferState
       
   328 	*/
       
   329 	TTransferState iTransferState;
       
   330 
       
   331 	/**
       
   332 	Set to true if data path has to use a supplied codec in its construction.
       
   333 	*/
       
   334 	TBool iUseSuppliedCodecUid;
       
   335 
       
   336 	/** 
       
   337 	This is set to point to whichever sink buffer is in use.
       
   338 	*/
       
   339 	CMMFBuffer* iSinkBuffer;
       
   340 
       
   341 	/**
       
   342 	This is the pointer to whichever source buffer is in use
       
   343 	*/
       
   344 	CMMFBuffer* iSourceBuffer;
       
   345 
       
   346 	/**
       
   347 	The source's position in terms of frames or some other time fixed parameter.
       
   348 	*/
       
   349 	TUint iCurrentSourceFrameNumber;
       
   350 
       
   351 	/**
       
   352 	The sink's position in terms of frames or some other time fixed parameter.
       
   353 	*/
       
   354 	TUint iCurrentSinkFrameNumber;
       
   355 
       
   356 	/**
       
   357 	Indicates that all data has been obtained from the source (ETrue if there is no more source data).
       
   358 	*/
       
   359 	TBool iNoMoreSourceData;
       
   360 
       
   361 	/** 
       
   362 	Indicates that all data has been sent to the sink.
       
   363 	*/
       
   364 	TBool iAllDataSentToSink;
       
   365 
       
   366 	/** 
       
   367 	Datapath completed because of an error; usually KErrNone.
       
   368 	*/
       
   369 	TUint iDataPathCompletedErrorCode;
       
   370 
       
   371 	/**
       
   372 	Start position of the play window.
       
   373 	*/
       
   374 	TTimeIntervalMicroSeconds iPlayWindowStartPosition ;
       
   375 	
       
   376 	/**
       
   377 	End position of the play window.
       
   378 	*/
       
   379 	TTimeIntervalMicroSeconds iPlayWindowEndPosition ;
       
   380 
       
   381 	/**	
       
   382 	The position audio will start playing from.
       
   383 	When stopping, this is set to iPlayWindowStartPosition.
       
   384 	When pausing, this is set to the current position.
       
   385 	*/
       
   386 	TTimeIntervalMicroSeconds iStartPosition;
       
   387 
       
   388 	/**	
       
   389 	This value can be used to obtain the duration of the source when playing or converting.
       
   390 	This is an optimisation as this value will not change if we are playing or converting.
       
   391 	*/
       
   392 	TTimeIntervalMicroSeconds iCachedSourceDuration;
       
   393 
       
   394 
       
   395 	/** 
       
   396 	ETrue if the source buffer is reference to object owned by someone else.
       
   397 	*/
       
   398 	TBool iSrcBufRef;
       
   399 
       
   400 	/**
       
   401 	ETrue if sink buffer is reference to object owned by someone else
       
   402 	*/
       
   403 	TBool iSnkBufRef;
       
   404 
       
   405 	/**
       
   406 	Indicates asynchrous buffers from AudioInput.
       
   407 	*/
       
   408 	TBool iObtainingAsyncSourceBuffer;
       
   409 
       
   410 	/**
       
   411 	Indicates asynchrous buffers from AudioOutput.
       
   412 	*/
       
   413 	TBool iObtainingAsyncSinkBuffer;
       
   414 
       
   415 	/** 
       
   416 	Indicates DoPauseL() has been called.
       
   417 	*/
       
   418 	TBool iPauseCalled;
       
   419 
       
   420 	/**	
       
   421 	Flag to indicate that a buffer is with the source.
       
   422 
       
   423 	This is necessary as it is imperrative that when a buffer is with the source, it must
       
   424 	not be referenced in any way. The reason for this is that it is not mandated that sources
       
   425 	maintain buffer references. For example, it is valid for DevSound to return recorded data in a
       
   426 	different buffer to the one supplied to it.
       
   427     */
       
   428 	TBool iSourceBufferWithSource;
       
   429 
       
   430 	/**
       
   431 	Flag to indicate that a buffer is with the sink.
       
   432 
       
   433 	This are necessary as it is imperrative that when a buffer is with the sink, it must
       
   434 	not be referenced in any way. The reason for this is that it is not mandated that sinks
       
   435 	maintain buffer references. For example, it is valid for DevSound to request more audio data in a
       
   436 	different buffer to the one supplied to it.
       
   437 	*/
       
   438 	TBool iSinkBufferWithSink;
       
   439 
       
   440 
       
   441 	/** 
       
   442 	Holds the number of samples played on audio output at a point where we
       
   443 	want to reference play duration from.
       
   444 	*/
       
   445 	TInt iReferenceAudioSamplesPlayed;
       
   446 
       
   447 	/** 
       
   448 	Holds the number of samples recorded from audio input at a point where we
       
   449 	want to reference record duration from.
       
   450 	*/
       
   451 	TInt iReferenceAudioSamplesRecorded;
       
   452 
       
   453 	/** 
       
   454 	Pointer to internal callback completion class
       
   455 	*/
       
   456 	CCompleteCallback * iCompleteCallback;
       
   457 
       
   458 	/**
       
   459 	This indicates what buffers are required in order to operate.
       
   460 	If a real Codec is in use, buffers are required from both source and sink,
       
   461 	else only one is required and source is preferred.
       
   462 	*/
       
   463 	enum TNeedBuffer
       
   464 		{
       
   465 		/** No buffers needed.
       
   466 		*/
       
   467 		ENoBuffers = 0x0,
       
   468 		/** Sink buffer needed.
       
   469 		*/
       
   470 		ENeedSinkBuffer = 0x01,
       
   471 		/** Source buffer needed.
       
   472 		*/
       
   473 		ENeedSourceBuffer = 0x10
       
   474 		};
       
   475 
       
   476 	/** Holds the outcome of the call to DetermineBuffersToUseL
       
   477 	*/
       
   478 	TInt iBuffersToUse;
       
   479 	};
       
   480 
       
   481 #endif
       
   482