Esempio n. 1
0
static HRESULT ReadAAFFile(const aafWChar * pFileName, testType_t testType)
{
  IAAFFile *					pFile = NULL;
  IAAFHeader *				pHeader = NULL;
  IAAFDictionary*					pDictionary = NULL;
  IAAFEssenceAccess*			pEssenceAccess = NULL;
  IAAFEssenceMultiAccess*		pMultiEssence = NULL;
  IAAFEssenceFormat			*fmtTemplate =  NULL;
  IEnumAAFMobs*				pMobIter = NULL;
  IAAFMob*					pMob = NULL;
  IAAFMasterMob*				pMasterMob = NULL;
  IAAFEssenceFormat*			pFormat = NULL;

  aafNumSlots_t				numMobs, numSlots;
  aafSearchCrit_t				criteria;
  aafRational_t				readSampleRate;
  aafMobID_t					mobID;
  aafWChar					namebuf[1204];
  unsigned char				AAFDataBuf[4096];
  aafUInt32					AAFBytesRead, samplesRead;
  FILE*						pWavFile = NULL;
  unsigned char				WAVDataBuf[4096], *dataPtr;
  size_t						WAVBytesRead;
  aafUInt32					dataOffset, dataLen;
  aafUInt16					bitsPerSample, numCh;

  check(AAFFileOpenExistingRead ( pFileName, 0, &pFile));
  check(pFile->GetHeader(&pHeader));

  // Get the AAF Dictionary so that we can create valid AAF objects.
  check(pHeader->GetDictionary(&pDictionary));


  // Here we check on the number of mobs in the file. 
  // Get the number of master mobs in the file (should be one)
  check(pHeader->CountMobs(kAAFMasterMob, &numMobs));
  // ** causes leak

  if (1 == numMobs )
    {
      printf("Found %d Master Mobs\n", numMobs);
      criteria.searchTag = kAAFByMobKind;
      criteria.tags.mobKind = kAAFMasterMob;
      check(pHeader->GetMobs(&criteria, &pMobIter));
      while(AAFRESULT_SUCCESS == pMobIter->NextOne(&pMob))
	{
	  char mobIDstr[256];
	  char mobName[256];


	  check(pMob->GetMobID (&mobID));
	  check(pMob->GetName (namebuf, sizeof(namebuf)));
	  convert(mobName, sizeof(mobName), namebuf);
	  MobIDtoString(mobID, mobIDstr);
	  printf("    MasterMob Name = '%s'\n", mobName);
	  printf("        (mobID %s)\n", mobIDstr);
	  // Make sure we have two slots 
	  check(pMob->CountSlots(&numSlots));
	  if (2 == numSlots)
	    {
				// The essence data is in SlotID 1
				// Get a Master Mob interface
	      check(pMob->QueryInterface(IID_IAAFMasterMob, (void **)&pMasterMob));

				// Open the Essence Data
	      check(pMasterMob->OpenEssence(	1,     // SlotID 1
						NULL,  // mediaCriteria (Don't care)
						kAAFMediaOpenReadOnly,	// Open mode
						kAAFCompressionDisable,// Compress disabled
						&pEssenceAccess));

				// Open and read the Wave file (for comparison)
	      pWavFile = fopen("Laser.wav", "r");
	      if (pWavFile)
		{
		  // read in the essence data
		  WAVBytesRead = fread(WAVDataBuf, sizeof(unsigned char), sizeof(WAVDataBuf), pWavFile);
		  fclose(pWavFile);
		  pWavFile = NULL;
		  check(loadWAVEHeader(WAVDataBuf,
				       &bitsPerSample,
				       &numCh,
				       &readSampleRate,
				       &dataOffset,
				       &dataLen));
		  dataPtr = WAVDataBuf + dataOffset;

		  aafUInt32			sampleBits;
		  aafInt32			bytesRead;
					
		  check(pEssenceAccess->GetEmptyFileFormat (&fmtTemplate));
		  check(fmtTemplate->AddFormatSpecifier (kAAFAudioSampleBits, 0, NULL));
		  check(pEssenceAccess->GetFileFormat (fmtTemplate, &pFormat));
		  fmtTemplate->Release();
		  fmtTemplate = NULL;
					
		  check(pFormat->GetFormatSpecifier (kAAFAudioSampleBits, sizeof(sampleBits),
						     (aafDataBuffer_t)&sampleBits, &bytesRead));
		  pFormat->Release();
		  pFormat = NULL;
		  if(sampleBits != bitsPerSample)
		    {
		      printf("***Wrong sample size read ( was %d , should be %d)\n",
			     sampleBits, bitsPerSample);
		    }

		  // Read the Data from the AAF file
		  if(testType == testStandardCalls)
		    {
		      check(pEssenceAccess->ReadSamples( dataLen, //!!! Hardcoded	// Number of Samples 
							 sizeof(AAFDataBuf),	// Maximum buffer size
							 AAFDataBuf,	  // Buffer for the data
							 &samplesRead,      // Actual number of samples read
							 &AAFBytesRead));	// Actual number of bytes read
		    }
		  else if(testType == testMultiCalls)
		    {
		      aafmMultiXfer_t		xfer;
		      aafmMultiResult_t	result;

		      check(pEssenceAccess->QueryInterface(IID_IAAFEssenceMultiAccess, (void **)&pMultiEssence));
		      xfer.numSamples = dataLen;	//!!! Hardcoded	// Number of Samples 
		      xfer.buflen = sizeof(AAFDataBuf);
		      xfer.buffer = AAFDataBuf;
		      result.bytesXfered = 0;
		      check(pMultiEssence->ReadMultiSamples(1, &xfer, &result));
		      samplesRead = result.samplesXfered;
		      AAFBytesRead = result.bytesXfered;
		      pMultiEssence->Release();
		      pMultiEssence = NULL;
		    }

		  // Now compare the data read from the AAF file to the actual WAV file
		  if (dataLen != AAFBytesRead)
		    {
		      printf("***Wrong number of bytes read (was %u , should be %zu)\n",
			     AAFBytesRead, WAVBytesRead);
		    }
		  if (memcmp( dataPtr, AAFDataBuf, dataLen) != 0)
		    {
		      printf("*** Data Read is different than the data in the WAV file ***\n");
		    }
		}
	      else
		{
		  printf("***Failed to open Wave file Laser.wav for comparison\n");
		}
	    }
	  else
	    {
	      printf("***Wrong number of slots in the Master Mob (was %d should be %d)\n",
		     numSlots, 2);
	    }
	  if (pMasterMob)
	    {
	      pMasterMob->Release();
	      pMasterMob = NULL;
	    }

	  pMob->Release();
	  pMob = NULL;
	  if (pEssenceAccess)
	    {
	      pEssenceAccess->Release();
	      pEssenceAccess = NULL;
	    }
	} // while pMobIter->NextOne
      pMobIter->Release();
      pMobIter = NULL;

    }
  else
    {
      printf("***Wrong number of Master mobs in the file (was %d should be %d)\n",
	     numMobs, 1);
    }
  printf("--------\n");

 cleanup:
  // Cleanup and return

  if (pWavFile)
    fclose(pWavFile);
  if (pMultiEssence)
    pMultiEssence->Release();
  pMultiEssence=NULL;
  if(fmtTemplate)
    {
      fmtTemplate->Release();
      fmtTemplate = NULL;
    }
  if (pEssenceAccess)
    {
      pEssenceAccess->Release();
      pEssenceAccess = NULL;
    }
  if (pDictionary)
    pDictionary->Release();
  pDictionary=NULL;

  if (pHeader)
    pHeader->Release();
  pHeader=NULL;
  if (pMobIter)
    pMobIter->Release();
  pMobIter=NULL;
  if (pFormat)
    pFormat->Release();
  pFormat=NULL;

  if (pFile) 
    {
      pFile->Close();
      pFile->Release();
      pFile=NULL;
    }

  return moduleErrorTmp;
}
Esempio n. 2
0
static HRESULT ProcessAAFFile(const aafWChar * pFileName, testType_t testType)
{
	IAAFFile *					pFile = NULL;
	IAAFHeader *				pHeader = NULL;
	IAAFDictionary*				pDictionary = NULL;
	IEnumAAFMobs*				pMobIter = NULL;
	aafNumSlots_t				numMobs, numSlots;
	aafSearchCrit_t				criteria;
	aafMobID_t					mobID;
	aafWChar					namebuf[1204];
	const aafWChar*				slotName = L"A slot in Composition Mob";
	IAAFComponent*				pComponent = NULL;
	IAAFComponent*				aComponent = NULL;
	IEnumAAFMobSlots*			pMobSlotIter = NULL;
	IAAFMobSlot*				pMobSlot = NULL;
	IAAFTimelineMobSlot*		newSlot = NULL;
	IAAFSegment*				seg = NULL;
	IAAFSegment*				pSegment = NULL;
	IAAFMob*					pCompMob = NULL;
	IAAFMob*					pMob = NULL;
	aafPosition_t				zeroPos = 0;
	IAAFSequence*				pAudioSequence = NULL;
	IAAFSourceClip*				pSourceClip = NULL;
	aafLength_t					duration;
	IAAFTimelineMobSlot* pTimelineMobSlot = NULL;
  IAAFClassDef *pCompositionMobDef = NULL;
  IAAFClassDef *pSequenceDef = NULL;
  IAAFClassDef *pSourceClipDef = NULL;
  IAAFDataDef *pSoundDef = NULL;
  IAAFDataDef *pDataDef = NULL;
	

	// Set the edit rate information
	aafRational_t				editRate;
	editRate.numerator = 48000;
	editRate.denominator = 1;

	// Set search condition to true
	bool lookingForAudio = true;

	// Call the routine (from ExportAudioExample) to make the file for processing
	check(CreateAAFFile(pwFileName, NULL, testStandardCalls, &pFile));

	/* Get the Header and iterate through the Master Mobs in the existing file */
	check(pFile->GetHeader(&pHeader));
	check(pHeader->GetDictionary(&pDictionary));

	
  /* Lookup class definitions for the objects we want to create. */
  check(pDictionary->LookupClassDef(AUID_AAFCompositionMob, &pCompositionMobDef));
  check(pDictionary->LookupClassDef(AUID_AAFSequence, &pSequenceDef));
  check(pDictionary->LookupClassDef(AUID_AAFSourceClip, &pSourceClipDef));

  /* Lookup any necessary data definitions. */
  check(pDictionary->LookupDataDef(kAAFDataDef_Sound, &pSoundDef));
  

	// Get the number of master mobs in the existing file (must not be zero)
	check(pHeader->CountMobs(kAAFMasterMob, &numMobs));
	if (numMobs != 0)
	{
		printf("Found %d Master Mobs\n", numMobs);
		criteria.searchTag = kAAFByMobKind;
		criteria.tags.mobKind = kAAFMasterMob;
		check(pHeader->GetMobs(&criteria, &pMobIter));

		/* Create a Composition Mob */
		check(pCompositionMobDef->
			  CreateInstance(IID_IAAFMob,
							 (IUnknown **)&pCompMob));
		/* Append the Mob to the Header */
		check(pHeader->AddMob(pCompMob));
 
		/* Create a TimelineMobSlot with an audio sequence */
		check(pSequenceDef->
			  CreateInstance(IID_IAAFSequence,
							 (IUnknown **)&pAudioSequence));
		check(pAudioSequence->QueryInterface(IID_IAAFSegment, (void **)&seg));

		check(pAudioSequence->QueryInterface(IID_IAAFComponent,
											 (void **)&aComponent));

		check(aComponent->SetDataDef(pSoundDef));
		check(pCompMob->AppendNewTimelineSlot(editRate, seg, 1, slotName, zeroPos, &newSlot));
    seg->Release();
    seg = NULL;
    newSlot->Release();
    newSlot = NULL;

		// This variable is about to be overwritten so we need to release the old interface
		aComponent->Release();
		aComponent = NULL;

		while((AAFRESULT_SUCCESS == pMobIter->NextOne(&pMob)))
		{
			//  Print out information about the Mob
			char mobIDstr[256];
			char mobName[256];

			check(pMob->GetMobID (&mobID));
			check(pMob->GetName (namebuf, sizeof(namebuf)));
			convert(mobName, sizeof(mobName), namebuf);
			MobIDtoString(mobID, mobIDstr);
			printf("    MasterMob Name = '%s'\n", mobName);
			printf("        (mobID %s)\n", mobIDstr);
			
			// Add a Source Clip for each Master Mob to the audio sequence by iterating
			check(pMob->GetSlots(&pMobSlotIter));
			
			/* Iterating through all Mob Slots */
			// Get the number of slots
			check(pMob->CountSlots(&numSlots));
			
			while (lookingForAudio && (AAFRESULT_SUCCESS == pMobSlotIter->NextOne(&pMobSlot)));
			{
				/* Check to see if it is an Audio Timeline Mob Slot */
				HRESULT hr;
				
				hr=pMobSlot->QueryInterface(IID_IAAFTimelineMobSlot,(void **) &pTimelineMobSlot);
				if (SUCCEEDED(hr))
				{
					printf("Found a timeline mob slot\n");
					check(pMobSlot->GetDataDef(&pDataDef));
					
					// Check that we have a sound file by examining its data definition
          aafBool bIsSoundKind = kAAFFalse;
          check(pDataDef->IsSoundKind(&bIsSoundKind));

          if (kAAFTrue == bIsSoundKind)
					{
						printf("Found a sound file\n");

						// We are no longer looking for audio data so set boolean	
						lookingForAudio = false;

						/* Get the information for the new source clip */
						check(pMob->GetMobID(&sourceRef.sourceID));
						check(pMobSlot->GetSlotID(&sourceRef.sourceSlotID));
						check(pTimelineMobSlot->GetOrigin(&sourceRef.startTime));
						check(pMobSlot->GetSegment(&pSegment));
						check(pSegment->QueryInterface(IID_IAAFComponent, (void **)&pComponent));
						check(pComponent->GetLength(&duration));
            pComponent->Release();
            pComponent = NULL;
            pSegment->Release();
            pSegment = NULL;
						
						// this loop is to be removed upon fixing of the bug
						// in essenceaccess relating to codec definitions...
						int j = 0;
						for (j=0; j<10; j++)
						{
							/* Create a new Source Clip */
							check(pSourceClipDef->
								  CreateInstance(IID_IAAFSourceClip,
												 (IUnknown **)&pSourceClip));
							// Initialize the Source Clip
							check(pSourceClip->Initialize( pSoundDef, duration, sourceRef));
							check(pSourceClip->QueryInterface(IID_IAAFComponent, (void **) &pComponent));
							check(pAudioSequence->AppendComponent(pComponent));
              pComponent->Release();
              pComponent = NULL;
							pSourceClip->Release();
							pSourceClip = NULL;
						}
					}
					pTimelineMobSlot->Release();
					pTimelineMobSlot = NULL;

          pDataDef->Release();
          pDataDef = NULL;
				}	
        
        pMobSlot->Release();
        pMobSlot = NULL;
			}

      pMobSlotIter->Release();
      pMobSlotIter = NULL;
		  pMob->Release();
		  pMob = NULL;
		}
		

    pAudioSequence->Release();
    pAudioSequence = NULL;

    pCompMob->Release();
    pCompMob = NULL;

		pMobIter->Release();
		pMobIter = NULL;
	}
	else
	{
		printf("Error with file: File has no Master mobs.\n");
	}

cleanup:
	// Cleanup and return
  if (pSourceClip)
    pSourceClip->Release();

  if (pComponent)
    pComponent->Release();

  if (pSegment)
    pSegment->Release();

  if (pTimelineMobSlot)
    pTimelineMobSlot->Release();

  if (pMobSlotIter)
    pMobSlotIter->Release();

  if (pMob)
    pMob->Release();

  if (newSlot)
    newSlot->Release();

  if (aComponent)
    aComponent->Release();

  if (seg)
    seg->Release();

  if (pAudioSequence)
    pAudioSequence->Release();

  if (pCompMob)
    pCompMob->Release();

  if (pMobIter)
		pMobIter->Release();

  if (pDataDef)
    pDataDef->Release();

  if (pSoundDef)
    pSoundDef->Release();

  if (pSourceClipDef)
    pSourceClipDef->Release();

  if (pSequenceDef)
    pSequenceDef->Release();

  if (pCompositionMobDef)
    pCompositionMobDef->Release();

	if (pDictionary)
		pDictionary->Release();

	if (pHeader)
		pHeader->Release();

	if (pFile) 
	{
	  /* Save the AAF file */
    pFile->Save();
	  /* Close the AAF file */
		pFile->Close();
		pFile->Release();
	}

	return moduleErrorTmp;
}