Replace Audio using IltmmMultiStreamSource and IltmmMultiStreamTarget Example for C

LPOLESTR T2OLE(LPCTSTR lpt) 
{ 
   static OLECHAR lpw[512]; 
 
   if(!lpt) 
      return NULL; 
 
   lpw[0] = L'\0'; 
 
   MultiByteToWideChar(CP_ACP, 0, lpt, -1, lpw, 512); 
 
   return lpw; 
} 
 
void PrepareSampleAndTarget(IltmmConvert* pSourceConvert, char pszSourceFile[256], char mtType[40], IltmmSampleTarget** ppSampleTarget, IltmmMultiStreamSource* pVideoAndAudioSource, long lStream) 
{ 
   IltmmMediaTypeDisp *pInsertedMeidaType; 
   IltmmMediaTypeDisp *pmt; 
   BSTR bstrSourceFile; 
   HRESULT hr; 
   BSTR bstrType; 
 
   bstrSourceFile = T2OLE(pszSourceFile); 
   IltmmConvert_put_SourceFile(pSourceConvert, bstrSourceFile); 
   SysFreeString(bstrSourceFile); 
 
   hr = CoCreateInstance(&CLSID_ltmmSampleTarget, NULL, CLSCTX_INPROC_SERVER, &IID_IltmmSampleTarget, (void**) ppSampleTarget); 
   if(FAILED(hr)) 
   { 
      return; 
   } 
 
   hr = CoCreateInstance(&CLSID_ltmmMediaType, NULL, CLSCTX_INPROC_SERVER, &IID_IltmmMediaTypeDisp, (void**) &pmt); 
   if(FAILED(hr)) 
   { 
      return; 
   } 
 
   // create a ltmmMediaType and indicate the accepted type 
   bstrType = T2OLE(mtType); 
   IltmmMediaTypeDisp_put_Type(pmt, bstrType); 
   SysFreeString(bstrType); 
 
   // make the SampleTarget object accept connections of this media type 
   hr = IltmmSampleTarget_SetAcceptedMediaType(*ppSampleTarget, pmt); 
 
   IltmmMediaTypeDisp_Release(pmt); 
   pmt = NULL; 
 
   // set the output target object to be sample target 
   IltmmConvert_put_TargetObject(pSourceConvert, *ppSampleTarget); 
 
   // start the conversion, so I know the exact media type 
   IltmmConvert_StartConvert(pSourceConvert); 
 
   // set the media type for the source stream to be the same as the media type in the input file (video or audio) 
   IltmmSampleTarget_GetConnectedMediaType(*ppSampleTarget, &pmt); 
   IltmmMultiStreamSource_SetMediaType(pVideoAndAudioSource, lStream, pmt); 
 
   // check set sucess: 
   IltmmMultiStreamSource_GetMediaType(pVideoAndAudioSource, lStream, &pInsertedMeidaType); 
   if (pInsertedMeidaType) 
      IltmmMediaTypeDisp_Release(pInsertedMeidaType); 
} 
 
void CopySamples(IltmmConvert* pSourceCtrl, IltmmSampleTarget* pSampleTarget, IltmmMultiStreamSource *pVideoAndAudioSource, long lStream) 
{ 
   IltmmMediaSampleDisp*  pmsSrc = NULL; 
   IltmmMediaSampleDisp*  pmsDst = NULL; 
   long              lStartTimeHi; 
   long              lStartTimeLo; 
   long              lStopTimeHi; 
   long              lStopTimeLo; 
   HRESULT hr; 
   VARIANT  vBuffer; 
   long     lActualDataLength; 
   VARIANT_BOOL vBool; 
 
   for ( ; ; ) 
   { 
      hr = IltmmSampleTarget_GetSample(pSampleTarget, 3000, &pmsSrc); 
      if (!pmsSrc) 
         break; 
 
      // get a source sample 
      hr = IltmmMultiStreamSource_GetSampleBuffer(pVideoAndAudioSource, lStream, 2000, &pmsDst); 
 
      // copy the data to the source sample 
 
      hr = IltmmMediaSampleDisp_get_Buffer(pmsSrc, &vBuffer); 
      hr = IltmmMediaSampleDisp_get_ActualDataLength(pmsSrc, &lActualDataLength); 
      hr = IltmmMediaSampleDisp_SetData(pmsDst, lActualDataLength, vBuffer); 
 
      // copy the sample time 
      IltmmMediaSampleDisp_GetTime(pmsSrc, &lStartTimeHi, &lStartTimeLo, &lStopTimeHi, &lStopTimeLo); 
      IltmmMediaSampleDisp_SetTime(pmsDst, lStartTimeHi, lStartTimeLo, lStopTimeHi, lStopTimeLo); 
 
      // copy the other flags 
      IltmmMediaSampleDisp_get_Discontinuity(pmsSrc, &vBool); 
      IltmmMediaSampleDisp_put_Discontinuity(pmsDst, vBool); 
 
      IltmmMediaSampleDisp_get_Preroll(pmsSrc, &vBool); 
      IltmmMediaSampleDisp_put_Preroll(pmsDst, vBool); 
 
      IltmmMediaSampleDisp_get_SyncPoint(pmsSrc, &vBool); 
      IltmmMediaSampleDisp_put_SyncPoint(pmsDst, vBool); 
 
      //release the source sample 
      IltmmMediaSampleDisp_Release(pmsSrc); 
      pmsSrc = NULL; 
 
      // deliver the source sample 
      IltmmMultiStreamSource_DeliverSample(pVideoAndAudioSource, lStream, 2000, pmsDst); 
 
      // release the source sample 
      IltmmMediaSampleDisp_Release(pmsDst); 
      pmsDst = NULL; 
   } 
    
   // stop the conversion in the source control 
   IltmmConvert_StopConvert(pSourceCtrl); 
 
   // deliver end of stream for the current source stream 
   hr = IltmmMultiStreamSource_DeliverEndOfStream(pVideoAndAudioSource, lStream, 5000); 
} 
 
int ReplaceAudiousingIltmmMultiStreamSourceandIltmmMultiStreamTarget_Example ( ) 
{ 
   IltmmConvert* pVideoConvert; 
   IltmmConvert* pAudioConvert; 
   IltmmConvert* pDestConvert; 
   HRESULT hr; 
   IltmmSampleTarget* pVideoTarget = NULL; 
   IltmmSampleTarget* pAudioTarget = NULL; 
   IltmmMultiStreamSource *pVideoAndAudioSource = NULL; 
 
   BSTR bstr; 
   long lState; 
 
   pVideoConvert = NULL; 
   pAudioConvert = NULL; 
   pDestConvert = NULL; 
 
   CoInitialize(NULL); 
 
   hr = CoCreateInstance(&CLSID_ltmmConvert, NULL, CLSCTX_INPROC_SERVER, &IID_IltmmConvert, (void**) &pVideoConvert); 
   if(FAILED(hr)) 
   { 
      MessageBox(0, "Can't instantiate convert library", "Error", 0); 
      return -1; 
   } 
 
   hr = CoCreateInstance(&CLSID_ltmmConvert, NULL, CLSCTX_INPROC_SERVER, &IID_IltmmConvert, (void**) &pAudioConvert); 
   if(FAILED(hr)) 
   { 
      MessageBox(0, "Can't instantiate convert library", "Error", 0); 
      return -1; 
   } 
 
   hr = CoCreateInstance(&CLSID_ltmmConvert, NULL, CLSCTX_INPROC_SERVER, &IID_IltmmConvert, (void**) &pDestConvert); 
   if(FAILED(hr)) 
   { 
      MessageBox(0, "Can't instantiate convert library", "Error", 0); 
      return -1; 
   } 
 
   hr = CoCreateInstance(&CLSID_ltmmMultiStreamSource, NULL, CLSCTX_INPROC_SERVER, &IID_IltmmMultiStreamSource, (void**) &pVideoAndAudioSource); 
   if(FAILED(hr)) 
   { 
      MessageBox(0, "Can't instantiate convert library", "Error", 0); 
      return -1; 
   } 
 
   IltmmMultiStreamSource_put_StreamCount(pVideoAndAudioSource, 2); 
   /*   
   Prepare pVideoConvert and set its source file to VideoFile. 
   Convert only the video stream 
   Create a new pVideoTarget object and set as the sample target object in pVideoConvert 
   Set the stream media type for pin 0 of VideoAndAudioSource to be the same as the video media type 
   Also starts the conversion in pVideoConvert 
   */ 
 
   PrepareSampleAndTarget(pVideoConvert, "c:\\Video.avi", (char*)ltmmMEDIATYPE_Video, &pVideoTarget, pVideoAndAudioSource, 0); 
 
   /* 
   Prepare AudioCtrl and set its source file to AudioFile. 
   Convert only the audio stream 
   Create a new AudioTarget object and set as the sample target object in AudioCtrl 
   Set the stream media type for pin 1 of VideoAndAudioSource to be the same as the audio media type 
   Also starts the conversion in AudioCtrl 
   */ 
 
   PrepareSampleAndTarget(pAudioConvert, "C:\\Voice.wav", (char*)ltmmMEDIATYPE_Audio, &pAudioTarget, pVideoAndAudioSource, 1); 
 
   // prepare the destination file 
   IltmmConvert_put_SourceObject(pDestConvert, pVideoAndAudioSource); 
   IltmmConvert_put_TargetFormat(pDestConvert, ltmmConvert_TargetFormat_Avi); 
 
   bstr = T2OLE("C:\\Video_And_Voice.avi"); 
   IltmmConvert_put_TargetFile(pDestConvert, bstr); 
 
   SysFreeString(bstr); 
   IltmmConvert_StartConvert(pDestConvert); 
 
   // Copy all the video samples from the Video file to the destination file 
   CopySamples(pVideoConvert, pVideoTarget, pVideoAndAudioSource, 0); 
 
   // Copy all the audio samples from the Audio file to the destination file 
   CopySamples(pAudioConvert, pAudioTarget, pVideoAndAudioSource, 1); 
 
   // wait for the destination control to stop the conversion 
   IltmmConvert_get_State(pDestConvert, &lState); 
   if( lState == ltmmConvert_State_Running ) 
   { 
      IltmmConvert_StopConvert(pDestConvert); 
   } 
 
   IltmmConvert_ResetSource(pDestConvert); 
 
   if (pVideoConvert) 
   { 
      IltmmConvert_Release(pVideoConvert); 
      pVideoConvert = NULL; 
   } 
 
   if (pAudioConvert) 
   { 
      IltmmConvert_Release(pAudioConvert); 
      pAudioConvert = NULL; 
   } 
 
   if (pDestConvert) 
   { 
      IltmmConvert_Release(pDestConvert); 
      pDestConvert = NULL; 
   } 
 
   if (pVideoAndAudioSource) 
   { 
      IltmmMultiStreamSource_Release(pVideoAndAudioSource); 
      pVideoAndAudioSource = NULL; 
   } 
 
   CoUninitialize(); 
 
   return 0; 
} 
Help Version 23.0.2024.2.29
Products | Support | Contact Us | Intellectual Property Notices
© 1991-2024 LEAD Technologies, Inc. All Rights Reserved.

LEADTOOLS Multimedia C API Help
Products | Support | Contact Us | Intellectual Property Notices
© 1991-2023 LEAD Technologies, Inc. All Rights Reserved.