The following code demonstrates how to copy a media sample's attributes and data to another media sample.
// define helper macros for using interfaces under C
#ifndef COBJMACROS
#define COBJMACROS
#endif
// include the LEAD Multimedia TOOLKIT header
#include "ltmm.h"
/////////////////////////////////////////////////////////////////
// CopyMediaSampleAttributes
// copies data and attributes from one sample to another
// pDest - destination sample interface
// pSource - source sample interface
//
HRESULT CopyMediaSampleAttributesExample (IltmmMediaSampleDisp* pDest, IltmmMediaSampleDisp* pSource)
{
HRESULT hr;
VARIANT_BOOL f;
LARGE_INTEGER starttime;
LARGE_INTEGER stoptime;
VARIANT var;
long cbData;
long cbBuffer;
IltmmMediaTypeDisp* pMediaType;
// return an error if NULL
if(!pSource || !pDest)
return E_POINTER;
// get the source's actual data length
hr = IltmmMediaSampleDisp_get_ActualDataLength (pSource, &cbData);
if(FAILED(hr))
return hr;
// now the destination maximum buffer size
hr = IltmmMediaSampleDisp_get_BufferSize(pDest, &cbBuffer);
if(FAILED(hr))
return hr;
// if no room for the data, return an error
if(cbData > cbBuffer)
return E_OUTOFMEMORY;
// any data to copy?
if(cbData)
{
// yes, then copy away
hr = IltmmMediaSampleDisp_GetData (pSource, cbData, &var);
if(FAILED(hr))
return hr;
hr = IltmmMediaSampleDisp_SetData (pDest, cbData, var);
VariantClear(&var);
if(FAILED(hr))
return hr;
}
else
{
// no, just set the destination data length to 0
IltmmMediaSampleDisp_put_ActualDataLength (pDest, 0);
}
// copy the media type, if there is one
pMediaType = NULL;
IltmmMediaSampleDisp_GetMediaType (pSource, &pMediaType);
if(pMediaType)
{
IltmmMediaSampleDisp_SetMediaType (pDest, pMediaType);
IUnknown_Release(pMediaType);
}
// copy discontinuity
f = VARIANT_FALSE;
IltmmMediaSampleDisp_get_Discontinuity(pSource, &f);
IltmmMediaSampleDisp_put_Discontinuity (pDest, f);
// copy preroll
f = VARIANT_FALSE;
IltmmMediaSampleDisp_get_Preroll(pSource, &f);
IltmmMediaSampleDisp_put_Preroll (pDest, f);
// copy syncpoint
f = VARIANT_FALSE;
IltmmMediaSampleDisp_get_SyncPoint(pSource, &f);
IltmmMediaSampleDisp_put_SyncPoint (pDest, f);
// is the time available?
hr = IltmmMediaSampleDisp_GetTime (pSource, &starttime.HighPart, (long*) &starttime.LowPart, &stoptime.HighPart, (long*) &stoptime.LowPart);
if(FAILED(hr))
{
// no, then reset the destination time
IltmmMediaSampleDisp_ResetTime (pDest);
}
else
{
// yes, copy to the destination
IltmmMediaSampleDisp_SetTime (pDest, starttime.HighPart, starttime.LowPart, stoptime.HighPart, stoptime.LowPart);
}
// is the media time available?
hr = IltmmMediaSampleDisp_GetMediaTime (pSource, &starttime.HighPart, (long*) &starttime.LowPart, &stoptime.HighPart, (long*) &stoptime.LowPart);
if(FAILED(hr))
{
// no, then reset the destination media time
IltmmMediaSampleDisp_ResetMediaTime (pDest);
}
else
{
// yes, copy to the destination
IltmmMediaSampleDisp_SetMediaTime (pDest, starttime.HighPart, starttime.LowPart, stoptime.HighPart, stoptime.LowPart);
}
return S_OK;
}
Help Collections
Raster .NET | C API | C++ Class Library | HTML5 JavaScript
Document .NET | C API | C++ Class Library | HTML5 JavaScript
Medical .NET | C API | C++ Class Library | HTML5 JavaScript
Medical Web Viewer .NET
Multimedia
Direct Show .NET | C API | Filters
Media Foundation .NET | C API | Transforms
Supported Platforms
.NET, Java, Android, and iOS/macOS Assemblies
Imaging, Medical, and Document
C API/C++ Class Libraries
Imaging, Medical, and Document
HTML5 JavaScript Libraries
Imaging, Medical, and Document