void MediaStreamer::Initialize(__in const WCHAR* url) { Microsoft::WRL::ComPtr<IMFMediaType> outputMediaType; Microsoft::WRL::ComPtr<IMFMediaType> mediaType; ThrowIfFailed( MFStartup(MF_VERSION) ); WCHAR filePath[MAX_PATH] = {0}; if ((wcslen(url) > 1 && url[1] == ':')) { // path start with "x:", is absolute path wcscat_s(filePath, url); } else if (wcslen(url) > 0 && (L'/' == url[0] || L'\\' == url[0])) { // path start with '/' or '\', is absolute path without driver name wcscat_s(filePath, m_installedLocationPath->Data()); // remove '/' or '\\' wcscat_s(filePath, (const WCHAR*)url[1]); }else { wcscat_s(filePath, m_installedLocationPath->Data()); wcscat_s(filePath, url); } ThrowIfFailed( MFCreateSourceReaderFromURL(filePath, nullptr, &m_reader) ); // Set the decoded output format as PCM // XAudio2 on Windows can process PCM and ADPCM-encoded buffers. // When using MF, this sample always decodes into PCM. ThrowIfFailed( MFCreateMediaType(&mediaType) ); ThrowIfFailed( mediaType->SetGUID(MF_MT_MAJOR_TYPE, MFMediaType_Audio) ); ThrowIfFailed( mediaType->SetGUID(MF_MT_SUBTYPE, MFAudioFormat_PCM) ); ThrowIfFailed( m_reader->SetCurrentMediaType(MF_SOURCE_READER_FIRST_AUDIO_STREAM, 0, mediaType.Get()) ); // Get the complete WAVEFORMAT from the Media Type ThrowIfFailed( m_reader->GetCurrentMediaType(MF_SOURCE_READER_FIRST_AUDIO_STREAM, &outputMediaType) ); uint32 formatSize = 0; WAVEFORMATEX* waveFormat; ThrowIfFailed( MFCreateWaveFormatExFromMFMediaType(outputMediaType.Get(), &waveFormat, &formatSize) ); CopyMemory(&m_waveFormat, waveFormat, sizeof(m_waveFormat)); CoTaskMemFree(waveFormat); // Get the total length of the stream in bytes PROPVARIANT var; ThrowIfFailed( m_reader->GetPresentationAttribute(MF_SOURCE_READER_MEDIASOURCE, MF_PD_DURATION, &var) ); LONGLONG duration = var.uhVal.QuadPart; double durationInSeconds = (duration / static_cast<double>(10000000)); // duration is in 100ns units, convert to seconds m_maxStreamLengthInBytes = static_cast<unsigned int>(durationInSeconds * m_waveFormat.nAvgBytesPerSec); // Round up the buffer size to the nearest four bytes m_maxStreamLengthInBytes = (m_maxStreamLengthInBytes + 3) / 4 * 4; }
// Create the source reader on the url (file) with the low latency attributes // DX::ThrowIfFailed( MFCreateSourceReaderFromURL(url->Data(), lowLatencyAttribute.Get(), &m_reader) ); // Set the decoded output format as PCM // XAudio2 on Windows can process PCM and ADPCM-encoded buffers. // When using MF, this sample always decodes into PCM. DX::ThrowIfFailed( MFCreateMediaType(&mediaType) ); DX::ThrowIfFailed( mediaType->SetGUID(MF_MT_MAJOR_TYPE, MFMediaType_Audio) ); DX::ThrowIfFailed( mediaType->SetGUID(MF_MT_SUBTYPE, MFAudioFormat_PCM) ); DX::ThrowIfFailed( m_reader->SetCurrentMediaType(MF_SOURCE_READER_FIRST_AUDIO_STREAM, 0, mediaType.Get()) ); // Get the complete WAVEFORMAT from the Media Type DX::ThrowIfFailed( m_reader->GetCurrentMediaType(MF_SOURCE_READER_FIRST_AUDIO_STREAM, &outputMediaType) );
void MediaStreamer::Initialize(_In_ const WCHAR* url) { Microsoft::WRL::ComPtr<IMFMediaType> outputMediaType; Microsoft::WRL::ComPtr<IMFMediaType> mediaType; DX::ThrowIfFailed( MFStartup(MF_VERSION) ); DX::ThrowIfFailed( MFCreateSourceReaderFromURL(url, nullptr, &m_reader) ); // Set the decoded output format as PCM. // XAudio2 on Windows can process PCM and ADPCM-encoded buffers. // When this sample uses Media Foundation, it always decodes into PCM. DX::ThrowIfFailed( MFCreateMediaType(&mediaType) ); DX::ThrowIfFailed( mediaType->SetGUID(MF_MT_MAJOR_TYPE, MFMediaType_Audio) ); DX::ThrowIfFailed( mediaType->SetGUID(MF_MT_SUBTYPE, MFAudioFormat_PCM) ); DX::ThrowIfFailed( m_reader->SetCurrentMediaType(MF_SOURCE_READER_FIRST_AUDIO_STREAM, 0, mediaType.Get()) ); // Get the complete WAVEFORMAT from the Media Type. DX::ThrowIfFailed( m_reader->GetCurrentMediaType(MF_SOURCE_READER_FIRST_AUDIO_STREAM, &outputMediaType) ); uint32 formatSize = 0; WAVEFORMATEX* waveFormat; DX::ThrowIfFailed( MFCreateWaveFormatExFromMFMediaType(outputMediaType.Get(), &waveFormat, &formatSize) ); CopyMemory(&m_waveFormat, waveFormat, sizeof(m_waveFormat)); CoTaskMemFree(waveFormat); // Get the total length of the stream, in bytes. PROPVARIANT var; DX::ThrowIfFailed( m_reader->GetPresentationAttribute(MF_SOURCE_READER_MEDIASOURCE, MF_PD_DURATION, &var) ); // duration is in 100ns units; convert to seconds, and round up // to the nearest whole byte. ULONGLONG duration = var.uhVal.QuadPart; m_maxStreamLengthInBytes = static_cast<unsigned int>( ((duration * static_cast<ULONGLONG>(m_waveFormat.nAvgBytesPerSec)) + 10000000) / 10000000 ); }