void MFPlayer::init(HWND handle) { HRESULT ret; // Initialize M$ bullshit //ret = CoInitializeEx(NULL, COINIT_MULTITHREADED); //assert(ret == S_OK); ret = MFStartup(MF_VERSION); assert(ret == S_OK); // Create factory IMFMediaEngineClassFactory *pMediaEngineClassFactory = nullptr; ret = CoCreateInstance(CLSID_MFMediaEngineClassFactory, nullptr, CLSCTX_ALL, IID_PPV_ARGS(&pMediaEngineClassFactory)); assert(ret == S_OK); // Create notify m_pPlayerNodify = new MFPlayerNotify(shared_from_this()); // Create attributes IMFAttributes *pAttributes = nullptr; ret = MFCreateAttributes(&pAttributes, 1); assert(ret == S_OK); ret = pAttributes->SetUnknown(MF_MEDIA_ENGINE_CALLBACK, m_pPlayerNodify); assert(ret == S_OK); ret = pAttributes->SetUINT64(MF_MEDIA_ENGINE_PLAYBACK_HWND, reinterpret_cast<UINT64>(handle)); assert(ret == S_OK); // Create player ret = pMediaEngineClassFactory->CreateInstance(0, pAttributes, &m_pMediaEngine); assert(ret == S_OK); // Release bullshits pAttributes->Release(); pMediaEngineClassFactory->Release(); }
void MFPlayer::init(const OTextureRef& pRenderTarget) { m_pRenderTarget = pRenderTarget; auto pRendererD3D11 = std::dynamic_pointer_cast<ORendererD3D11>(oRenderer); HRESULT ret; // Initialize M$ bullshit //ret = CoInitializeEx(NULL, COINIT_MULTITHREADED); //assert(ret == S_OK); ret = MFStartup(MF_VERSION); assert(ret == S_OK); // Create factory IMFMediaEngineClassFactory *pMediaEngineClassFactory = nullptr; ret = CoCreateInstance(CLSID_MFMediaEngineClassFactory, nullptr, CLSCTX_ALL, IID_PPV_ARGS(&pMediaEngineClassFactory)); assert(ret == S_OK); // Create notify m_pPlayerNodify = new MFPlayerNotify(shared_from_this()); // Create attributes IMFAttributes *pAttributes = nullptr; ret = MFCreateAttributes(&pAttributes, 1); assert(ret == S_OK); ret = pAttributes->SetUnknown(MF_MEDIA_ENGINE_CALLBACK, m_pPlayerNodify); assert(ret == S_OK); ID3D10Multithread *pMultithread = nullptr; ID3D11Device *pDevice = pRendererD3D11->getDevice(); ret = pDevice->QueryInterface(IID_PPV_ARGS(&pMultithread)); assert(ret == S_OK); pMultithread->SetMultithreadProtected(TRUE); pMultithread->Release(); UINT resetToken = 0; ret = MFCreateDXGIDeviceManager(&resetToken, &m_pDXGIManager); assert(ret == S_OK); ret = m_pDXGIManager->ResetDevice(pRendererD3D11->getDevice(), resetToken); assert(ret == S_OK); ret = pAttributes->SetUnknown(MF_MEDIA_ENGINE_DXGI_MANAGER, m_pDXGIManager); assert(ret == S_OK); ret = pAttributes->SetUINT32(MF_MEDIA_ENGINE_VIDEO_OUTPUT_FORMAT, DXGI_FORMAT_R8G8B8A8_UNORM); assert(ret == S_OK); // Create player ret = pMediaEngineClassFactory->CreateInstance(MF_MEDIA_ENGINE_WAITFORSTABLE_STATE, pAttributes, &m_pMediaEngine); assert(ret == S_OK); // Release bullshits pAttributes->Release(); pMediaEngineClassFactory->Release(); }
IMFMediaType* MFDecoderSourceReader::setSource(IMFMediaSource *source, const QAudioFormat &audioFormat) { IMFMediaType *mediaType = NULL; if (m_source == source) return mediaType; if (m_source) { m_source->Release(); m_source = NULL; } if (m_sourceReader) { m_sourceReader->Release(); m_sourceReader = NULL; } if (!source) return mediaType; IMFAttributes *attr = NULL; MFCreateAttributes(&attr, 1); if (SUCCEEDED(attr->SetUnknown(MF_SOURCE_READER_ASYNC_CALLBACK, this))) { if (SUCCEEDED(MFCreateSourceReaderFromMediaSource(source, attr, &m_sourceReader))) { m_source = source; m_source->AddRef(); m_sourceReader->SetStreamSelection(DWORD(MF_SOURCE_READER_ALL_STREAMS), FALSE); m_sourceReader->SetStreamSelection(DWORD(MF_SOURCE_READER_FIRST_AUDIO_STREAM), TRUE); IMFMediaType *pPartialType = NULL; MFCreateMediaType(&pPartialType); pPartialType->SetGUID(MF_MT_MAJOR_TYPE, MFMediaType_Audio); if (audioFormat.sampleType() == QAudioFormat::Float) { pPartialType->SetGUID(MF_MT_SUBTYPE, MFAudioFormat_Float); } else { pPartialType->SetGUID(MF_MT_SUBTYPE, MFAudioFormat_PCM); } m_sourceReader->SetCurrentMediaType(DWORD(MF_SOURCE_READER_FIRST_AUDIO_STREAM), NULL, pPartialType); pPartialType->Release(); m_sourceReader->GetCurrentMediaType(DWORD(MF_SOURCE_READER_FIRST_AUDIO_STREAM), &mediaType); // Ensure the stream is selected. m_sourceReader->SetStreamSelection(DWORD(MF_SOURCE_READER_FIRST_AUDIO_STREAM), TRUE); } attr->Release(); } return mediaType; }
unsigned char *BBWin8Game::LoadAudioData( String path,int *length,int *channels,int *format,int *hertz ){ String url=PathToFilePath( path ); DXASS( MFStartup( MF_VERSION ) ); IMFAttributes *attrs; DXASS( MFCreateAttributes( &attrs,1 ) ); DXASS( attrs->SetUINT32( MF_LOW_LATENCY,TRUE ) ); IMFSourceReader *reader; DXASS( MFCreateSourceReaderFromURL( url.ToCString<wchar_t>(),attrs,&reader ) ); attrs->Release(); IMFMediaType *mediaType; DXASS( MFCreateMediaType( &mediaType ) ); DXASS( mediaType->SetGUID( MF_MT_MAJOR_TYPE,MFMediaType_Audio ) ); DXASS( mediaType->SetGUID( MF_MT_SUBTYPE,MFAudioFormat_PCM ) ); DXASS( reader->SetCurrentMediaType( MF_SOURCE_READER_FIRST_AUDIO_STREAM,0,mediaType ) ); mediaType->Release(); IMFMediaType *outputMediaType; DXASS( reader->GetCurrentMediaType( MF_SOURCE_READER_FIRST_AUDIO_STREAM,&outputMediaType ) ); WAVEFORMATEX *wformat; uint32 formatByteCount=0; DXASS( MFCreateWaveFormatExFromMFMediaType( outputMediaType,&wformat,&formatByteCount ) ); *channels=wformat->nChannels; *format=wformat->wBitsPerSample/8; *hertz=wformat->nSamplesPerSec; CoTaskMemFree( wformat ); outputMediaType->Release(); /* PROPVARIANT var; DXASS( reader->GetPresentationAttribute( MF_SOURCE_READER_MEDIASOURCE,MF_PD_DURATION,&var ) ); LONGLONG duration=var.uhVal.QuadPart; float64 durationInSeconds=(duration / (float64)(10000 * 1000)); m_maxStreamLengthInBytes=(uint32)( durationInSeconds * m_waveFormat.nAvgBytesPerSec ); */ std::vector<unsigned char*> bufs; std::vector<uint32> lens; uint32 len=0; for( ;; ){ uint32 flags=0; IMFSample *sample; DXASS( reader->ReadSample( MF_SOURCE_READER_FIRST_AUDIO_STREAM,0,0,reinterpret_cast<DWORD*>(&flags),0,&sample ) ); if( flags & MF_SOURCE_READERF_ENDOFSTREAM ){ break; } if( sample==0 ){ abort(); } IMFMediaBuffer *mediaBuffer; DXASS( sample->ConvertToContiguousBuffer( &mediaBuffer ) ); uint8 *audioData=0; uint32 sampleBufferLength=0; DXASS( mediaBuffer->Lock( &audioData,0,reinterpret_cast<DWORD*>( &sampleBufferLength ) ) ); unsigned char *buf=(unsigned char*)malloc( sampleBufferLength ); memcpy( buf,audioData,sampleBufferLength ); bufs.push_back( buf ); lens.push_back( sampleBufferLength ); len+=sampleBufferLength; DXASS( mediaBuffer->Unlock() ); mediaBuffer->Release(); sample->Release(); } reader->Release(); *length=len/(*channels * *format); unsigned char *data=(unsigned char*)malloc( len ); unsigned char *p=data; for( int i=0;i<bufs.size();++i ){ memcpy( p,bufs[i],lens[i] ); free( bufs[i] ); p+=lens[i]; } gc_force_sweep=true; return data; }
HRESULT CaptureManager::InitializeCaptureManager(HWND hwndPreview, IUnknown* pUnk) { HRESULT hr = S_OK; IMFAttributes* pAttributes = NULL; IMFCaptureEngineClassFactory* pFactory = NULL; DestroyCaptureEngine(); m_hEvent = CreateEvent(NULL, FALSE, FALSE, NULL); if (NULL == m_hEvent) { hr = HRESULT_FROM_WIN32(GetLastError()); goto Exit; } m_pCallback = new (std::nothrow) CaptureEngineCB(m_hwndEvent); if (m_pCallback == NULL) { hr = E_OUTOFMEMORY; goto Exit; } m_pCallback->m_pManager = this; m_hwndPreview = hwndPreview; //Create a D3D Manager hr = CreateD3DManager(); if (FAILED(hr)) { goto Exit; } hr = MFCreateAttributes(&pAttributes, 1); if (FAILED(hr)) { goto Exit; } hr = pAttributes->SetUnknown(MF_CAPTURE_ENGINE_D3D_MANAGER, g_pDXGIMan); if (FAILED(hr)) { goto Exit; } // Create the factory object for the capture engine. hr = CoCreateInstance(CLSID_MFCaptureEngineClassFactory, NULL, CLSCTX_INPROC_SERVER, IID_PPV_ARGS(&pFactory)); if (FAILED(hr)) { goto Exit; } // Create and initialize the capture engine. hr = pFactory->CreateInstance(CLSID_MFCaptureEngine, IID_PPV_ARGS(&m_pEngine)); if (FAILED(hr)) { goto Exit; } hr = m_pEngine->Initialize(m_pCallback, pAttributes, NULL, pUnk); if (FAILED(hr)) { goto Exit; } Exit: if (NULL != pAttributes) { pAttributes->Release(); pAttributes = NULL; } if (NULL != pFactory) { pFactory->Release(); pFactory = NULL; } return hr; }