00001
00002
00003
00004
00005
00006 #include <streams.h>
00007 #include <mmreg.h>
00008
00009 CMediaType::~CMediaType(){
00010 FreeMediaType(*this);
00011 }
00012
00013 CMediaType::CMediaType()
00014 {
00015 InitMediaType();
00016 }
00017
00018 CMediaType::CMediaType(const GUID * type)
00019 {
00020 InitMediaType();
00021 majortype = *type;
00022 }
00023
00024 CMediaType::CMediaType(const AM_MEDIA_TYPE& rt)
00025 {
00026 CopyMediaType(this, &rt);
00027 }
00028
00029 CMediaType::CMediaType(const CMediaType& rt)
00030 {
00031 CopyMediaType(this, &rt);
00032 }
00033
00034 CMediaType&
00035 CMediaType::operator=(const AM_MEDIA_TYPE& rt)
00036 {
00037 if (&rt != this) {
00038 FreeMediaType(*this);
00039 CopyMediaType(this, &rt);
00040 }
00041 return *this;
00042 }
00043
00044 CMediaType&
00045 CMediaType::operator=(const CMediaType& rt)
00046 {
00047 *this = (AM_MEDIA_TYPE &) rt;
00048 return *this;
00049 }
00050
00051 BOOL
00052 CMediaType::operator == (const CMediaType& rt) const
00053 {
00054
00055
00056
00057
00058
00059
00060
00061 return ((IsEqualGUID(majortype,rt.majortype) == TRUE) &&
00062 (IsEqualGUID(subtype,rt.subtype) == TRUE) &&
00063 (IsEqualGUID(formattype,rt.formattype) == TRUE) &&
00064 (cbFormat == rt.cbFormat) &&
00065 ( (cbFormat == 0) ||
00066 (memcmp(pbFormat, rt.pbFormat, cbFormat) == 0)));
00067 }
00068
00069 BOOL
00070 CMediaType::operator != (const CMediaType& rt) const
00071 {
00072
00073
00074 if (*this == rt) {
00075 return FALSE;
00076 }
00077 return TRUE;
00078 }
00079
00080 BOOL
00081 CMediaType::IsValid() const
00082 {
00083 return (!IsEqualGUID(majortype,GUID_NULL));
00084 }
00085
00086 void
00087 CMediaType::SetType(const GUID* ptype)
00088 {
00089 majortype = *ptype;
00090 }
00091
00092 void
00093 CMediaType::SetSubtype(const GUID* ptype)
00094 {
00095 subtype = *ptype;
00096 }
00097
00098 ULONG
00099 CMediaType::GetSampleSize() const {
00100 if (IsFixedSize()) {
00101 return lSampleSize;
00102 } else {
00103 return 0;
00104 }
00105 }
00106
00107 void
00108 CMediaType::SetSampleSize(ULONG sz) {
00109 if (sz == 0) {
00110 SetVariableSize();
00111 } else {
00112 bFixedSizeSamples = TRUE;
00113 lSampleSize = sz;
00114 }
00115 }
00116
00117 void
00118 CMediaType::SetVariableSize() {
00119 bFixedSizeSamples = FALSE;
00120 }
00121
00122 void
00123 CMediaType::SetTemporalCompression(BOOL bCompressed) {
00124 bTemporalCompression = bCompressed;
00125 }
00126
00127 BOOL
00128 CMediaType::SetFormat(BYTE * pformat, ULONG cb)
00129 {
00130 if (NULL == AllocFormatBuffer(cb))
00131 return(FALSE);
00132
00133 ASSERT(pbFormat);
00134 memcpy(pbFormat, pformat, cb);
00135 return(TRUE);
00136 }
00137
00138 void
00139 CMediaType::SetFormatType(const GUID *pformattype)
00140 {
00141 formattype = *pformattype;
00142 }
00143
00144 void CMediaType::ResetFormatBuffer()
00145 {
00146 if (cbFormat) {
00147 CoTaskMemFree((PVOID)pbFormat);
00148 }
00149 cbFormat = 0;
00150 pbFormat = NULL;
00151 }
00152
00153 BYTE*
00154 CMediaType::AllocFormatBuffer(ULONG length)
00155 {
00156 ASSERT(length);
00157
00158
00159
00160 if (cbFormat == length) {
00161 return pbFormat;
00162 }
00163
00164
00165
00166 BYTE *pNewFormat = (PBYTE)CoTaskMemAlloc(length);
00167 if (pNewFormat == NULL) {
00168 if (length <= cbFormat) return pbFormat;
00169 return NULL;
00170 }
00171
00172
00173
00174 if (cbFormat != 0) {
00175 ASSERT(pbFormat);
00176 CoTaskMemFree((PVOID)pbFormat);
00177 }
00178
00179 cbFormat = length;
00180 pbFormat = pNewFormat;
00181 return pbFormat;
00182 }
00183
00184 BYTE*
00185 CMediaType::ReallocFormatBuffer(ULONG length)
00186 {
00187 ASSERT(length);
00188
00189
00190
00191 if (cbFormat == length) {
00192 return pbFormat;
00193 }
00194
00195
00196
00197 BYTE *pNewFormat = (PBYTE)CoTaskMemAlloc(length);
00198 if (pNewFormat == NULL) {
00199 if (length <= cbFormat) return pbFormat;
00200 return NULL;
00201 }
00202
00203
00204
00205
00206 if (cbFormat != 0) {
00207 ASSERT(pbFormat);
00208 memcpy(pNewFormat,pbFormat,min(length,cbFormat));
00209 CoTaskMemFree((PVOID)pbFormat);
00210 }
00211
00212 cbFormat = length;
00213 pbFormat = pNewFormat;
00214 return pNewFormat;
00215 }
00216
00217 void CMediaType::InitMediaType()
00218 {
00219 ZeroMemory((PVOID)this, sizeof(*this));
00220 lSampleSize = 1;
00221 bFixedSizeSamples = TRUE;
00222 }
00223
00224 BOOL
00225 CMediaType::IsPartiallySpecified(void) const
00226 {
00227 if ((majortype == GUID_NULL) ||
00228 (formattype == GUID_NULL)) {
00229 return TRUE;
00230 } else {
00231 return FALSE;
00232 }
00233 }
00234
00235 BOOL
00236 CMediaType::MatchesPartial(const CMediaType* ppartial) const
00237 {
00238 if ((ppartial->majortype != GUID_NULL) &&
00239 (majortype != ppartial->majortype)) {
00240 return FALSE;
00241 }
00242 if ((ppartial->subtype != GUID_NULL) &&
00243 (subtype != ppartial->subtype)) {
00244 return FALSE;
00245 }
00246
00247 if (ppartial->formattype != GUID_NULL) {
00248
00249 if (formattype != ppartial->formattype) {
00250 return FALSE;
00251 }
00252 if (cbFormat != ppartial->cbFormat) {
00253 return FALSE;
00254 }
00255 if ((cbFormat != 0) &&
00256 (memcmp(pbFormat, ppartial->pbFormat, cbFormat) != 0)) {
00257 return FALSE;
00258 }
00259 }
00260
00261 return TRUE;
00262
00263 }
00264
00265 void WINAPI DeleteMediaType(AM_MEDIA_TYPE *pmt)
00266 {
00267
00268
00269 if (pmt == NULL) {
00270 return;
00271 }
00272
00273 FreeMediaType(*pmt);
00274 CoTaskMemFree((PVOID)pmt);
00275 }
00276
00277 AM_MEDIA_TYPE * WINAPI CreateMediaType(AM_MEDIA_TYPE const *pSrc)
00278 {
00279 ASSERT(pSrc);
00280
00281
00282
00283 AM_MEDIA_TYPE *pMediaType =
00284 (AM_MEDIA_TYPE *)CoTaskMemAlloc(sizeof(AM_MEDIA_TYPE));
00285
00286 if (pMediaType == NULL) {
00287 return NULL;
00288 }
00289
00290
00291 CopyMediaType(pMediaType,pSrc);
00292
00293 return pMediaType;
00294 }
00295
00296 void WINAPI CopyMediaType(AM_MEDIA_TYPE *pmtTarget, const AM_MEDIA_TYPE *pmtSource)
00297 {
00298
00299
00300 ASSERT(pmtSource != pmtTarget);
00301 *pmtTarget = *pmtSource;
00302 if (pmtSource->cbFormat != 0) {
00303 ASSERT(pmtSource->pbFormat != NULL);
00304 pmtTarget->pbFormat = (PBYTE)CoTaskMemAlloc(pmtSource->cbFormat);
00305 if (pmtTarget->pbFormat == NULL) {
00306 pmtTarget->cbFormat = 0;
00307 } else {
00308 CopyMemory((PVOID)pmtTarget->pbFormat, (PVOID)pmtSource->pbFormat,
00309 pmtTarget->cbFormat);
00310 }
00311 }
00312 if (pmtTarget->pUnk != NULL) {
00313 pmtTarget->pUnk->AddRef();
00314 }
00315 }
00316
00317 void WINAPI FreeMediaType(AM_MEDIA_TYPE& mt)
00318 {
00319 if (mt.cbFormat != 0) {
00320 CoTaskMemFree((PVOID)mt.pbFormat);
00321
00322
00323 mt.cbFormat = 0;
00324 mt.pbFormat = NULL;
00325 }
00326 if (mt.pUnk != NULL) {
00327 mt.pUnk->Release();
00328 mt.pUnk = NULL;
00329 }
00330 }
00331
00332 STDAPI CreateAudioMediaType(
00333 const WAVEFORMATEX *pwfx,
00334 AM_MEDIA_TYPE *pmt,
00335 BOOL bSetFormat
00336 )
00337 {
00338 pmt->majortype = MEDIATYPE_Audio;
00339 if (pwfx->wFormatTag == WAVE_FORMAT_EXTENSIBLE) {
00340 pmt->subtype = ((PWAVEFORMATEXTENSIBLE)pwfx)->SubFormat;
00341 } else {
00342 pmt->subtype = FOURCCMap(pwfx->wFormatTag);
00343 }
00344 pmt->formattype = FORMAT_WaveFormatEx;
00345 pmt->bFixedSizeSamples = TRUE;
00346 pmt->bTemporalCompression = FALSE;
00347 pmt->lSampleSize = pwfx->nBlockAlign;
00348 pmt->pUnk = NULL;
00349 if (bSetFormat) {
00350 if (pwfx->wFormatTag == WAVE_FORMAT_PCM) {
00351 pmt->cbFormat = sizeof(WAVEFORMATEX);
00352 } else {
00353 pmt->cbFormat = sizeof(WAVEFORMATEX) + pwfx->cbSize;
00354 }
00355 pmt->pbFormat = (PBYTE)CoTaskMemAlloc(pmt->cbFormat);
00356 if (pmt->pbFormat == NULL) {
00357 return E_OUTOFMEMORY;
00358 }
00359 if (pwfx->wFormatTag == WAVE_FORMAT_PCM) {
00360 CopyMemory(pmt->pbFormat, pwfx, sizeof(PCMWAVEFORMAT));
00361 ((WAVEFORMATEX *)pmt->pbFormat)->cbSize = 0;
00362 } else {
00363 CopyMemory(pmt->pbFormat, pwfx, pmt->cbFormat);
00364 }
00365 }
00366 return S_OK;
00367 }
00368
00369 #pragma warning(disable:4514)