HRESULT CTranscoder::OpenFile(const WCHAR *sURL) { if (!sURL) { return E_INVALIDARG; } HRESULT hr = S_OK; // Create the media source. hr = CreateMediaSource(sURL, &m_pSource); //Create the media session. if (SUCCEEDED(hr)) { hr = MFCreateMediaSession(NULL, &m_pSession); } // Create an empty transcode profile. if (SUCCEEDED(hr)) { hr = MFCreateTranscodeProfile(&m_pProfile); } return hr; }
// Create a new instance of the media session. void Player_::CreateSession() { // Close the old session, if any. CloseSession(); // assert(m_state == Closed); // Create the media session. THROW_IF_ERR(MFCreateMediaSession(NULL, &m_pSession)); // Start pulling events from the media session THROW_IF_ERR(m_pSession->BeginGetEvent((IMFAsyncCallback*)this, NULL)); // m_state = Ready; }
HRESULT CTedPlayer::InitClear() { HRESULT hr; if(m_spSession) { if(m_bIsPlaying) { m_bIsPlaying = false; m_spSession->Stop(); } m_spSession.Release(); } if(m_fPendingClearCustomTopoloader && m_spClearSession.p) { m_spClearSession->Shutdown(); m_spClearSession.Release(); } if(m_spClearSession.p == NULL) { CComPtr<IMFAttributes> spConfiguration = NULL; if(m_fPendingClearCustomTopoloader && GUID_NULL != m_gidCustomTopoloader) { IFC( MFCreateAttributes(&spConfiguration, 1) ); IFC( spConfiguration->SetGUID(MF_SESSION_TOPOLOADER, m_gidCustomTopoloader) ); } IFC( MFCreateMediaSession(spConfiguration, &m_spClearSession) ); IFC( m_spClearSession->BeginGetEvent(&m_xOnClearSessionEvent, NULL) ); m_fPendingClearCustomTopoloader = false; } m_spSession = m_spClearSession; IFC( InitFromSession() ); Cleanup: return hr; }
HRESULT CMFCamCapture::init() { IMFMediaType *pType = NULL; HRESULT hr; CHECK_HR(hr = CoInitializeEx(NULL, COINIT_MULTITHREADED)); CHECK_HR(hr = MFStartup(MF_VERSION)); // Configure the media type that the Sample Grabber will receive. // Setting the major and subtype is usually enough for the topology loader // to resolve the topology. CHECK_HR(hr = MFCreateMediaType(&pType)); CHECK_HR(hr = pType->SetGUID(MF_MT_MAJOR_TYPE, MFMediaType_Video)); CHECK_HR(hr = pType->SetGUID(MF_MT_SUBTYPE, MFVideoFormat_RGB24)); // Create the sample grabber sink. CHECK_HR(hr = SampleGrabberCB::CreateInstance(&m_spSampleGrabber)); CHECK_HR(hr = MFCreateSampleGrabberSinkActivate(pType, m_spSampleGrabber, &m_spSinkActivate)); // To run as fast as possible, set this attribute (requires Windows 7): CHECK_HR(hr = m_spSinkActivate->SetUINT32(MF_SAMPLEGRABBERSINK_IGNORE_CLOCK, TRUE)); // Create the Media Session. CHECK_HR(hr = MFCreateMediaSession(NULL, &m_spSession)); CHECK_HR(hr = enumVideoDevices()); CHECK_HR(hr = setupVideoDevice()); CHECK_HR(hr = setupMfSession()); done: SafeRelease(&pType); return hr; }
HRESULT tTVPMFPlayer::CreateVideoPlayer() { if( MediaSession.p ) { return S_OK; // 既に作成済み } HRESULT hr = CreateChildWindow(); if( hr != S_OK ) return hr; HWND hWnd = GetChildWindow(); if( hWnd == NULL || hWnd == INVALID_HANDLE_VALUE ) return E_FAIL; if( FAILED(hr = MFCreateMediaSession( NULL, &MediaSession )) ) { TVPThrowExceptionMessage(L"Faild to create Media session."); } if( FAILED(hr = MediaSession->BeginGetEvent( PlayerCallback, NULL )) ) { TVPThrowExceptionMessage(L"Faild to begin get event."); } CComPtr<IMFSourceResolver> pSourceResolver; if( FAILED(hr = MFCreateSourceResolver(&pSourceResolver)) ) { TVPThrowExceptionMessage(L"Faild to create source resolver."); } MF_OBJECT_TYPE ObjectType = MF_OBJECT_INVALID; CComPtr<IUnknown> pSource; if( FAILED(hr = pSourceResolver->CreateObjectFromByteStream( ByteStream, StreamName.c_str(), MF_RESOLUTION_MEDIASOURCE, NULL, &ObjectType, (IUnknown**)&pSource )) ) { //if( FAILED(hr = pSourceResolver->CreateObjectFromURL( L"C:\\krkrz\\bin\\win32\\data\\test.mp4", // MF_RESOLUTION_MEDIASOURCE, NULL, &ObjectType, (IUnknown**)&pSource)) ) { TVPThrowExceptionMessage(L"Faild to open stream."); } if( ObjectType != MF_OBJECT_MEDIASOURCE ) { TVPThrowExceptionMessage(L"Invalid media source."); } //CComPtr<IMFMediaSource> pMediaSource; if( FAILED(hr = pSource.QueryInterface(&MediaSource)) ) { TVPThrowExceptionMessage(L"Faild to query Media source."); } if( FAILED(hr = MFCreateTopology(&Topology)) ) { TVPThrowExceptionMessage(L"Faild to create Topology."); } CComPtr<IMFPresentationDescriptor> pPresentationDescriptor; if( FAILED(hr = MediaSource->CreatePresentationDescriptor(&pPresentationDescriptor)) ) { TVPThrowExceptionMessage(L"Faild to create Presentation Descriptor."); } DWORD streamCount; if( FAILED(hr = pPresentationDescriptor->GetStreamDescriptorCount(&streamCount)) ) { TVPThrowExceptionMessage(L"Faild to get stream count."); } if( streamCount < 1 ) { TVPThrowExceptionMessage(L"Not found media stream."); } for( DWORD i = 0; i < streamCount; i++ ) { if( FAILED(hr = AddBranchToPartialTopology(Topology, MediaSource, pPresentationDescriptor, i, hWnd)) ) { TVPThrowExceptionMessage(L"Faild to add nodes."); } } pPresentationDescriptor->GetUINT64(MF_PD_DURATION, (UINT64*)&HnsDuration); if( FAILED(hr = MediaSession->SetTopology( 0, Topology )) ) { TVPThrowExceptionMessage(L"Faild to set topology."); } return hr; }
HRESULT RunMediaSession(IMFTopology *pTopology) { CComPtr<IMFMediaSession> pSession = NULL; HRESULT hr = S_OK; BOOL bGetAnotherEvent = TRUE; PROPVARIANT varStartPosition; PropVariantInit(&varStartPosition); hr = MFCreateMediaSession(NULL, &pSession); if (SUCCEEDED(hr)) { hr = pSession->SetTopology(0, pTopology); } while (bGetAnotherEvent) { HRESULT hrStatus = S_OK; CComPtr<IMFMediaEvent> pEvent; MediaEventType meType = MEUnknown; MF_TOPOSTATUS TopoStatus = MF_TOPOSTATUS_INVALID; // Used with MESessionTopologyStatus event. hr = pSession->GetEvent(0, &pEvent); if (SUCCEEDED(hr)) { hr = pEvent->GetStatus(&hrStatus); } if (SUCCEEDED(hr)) { hr = pEvent->GetType(&meType); } if (SUCCEEDED(hr) && SUCCEEDED(hrStatus)) { switch (meType) { case MESessionTopologySet: wprintf(L"MESessionTopologySet\n"); break; case MESessionTopologyStatus: // Get the status code. hr = pEvent->GetUINT32(MF_EVENT_TOPOLOGY_STATUS, (UINT32*)&TopoStatus); if (SUCCEEDED(hr)) { switch (TopoStatus) { case MF_TOPOSTATUS_READY: wprintf(L"MESessionTopologyStatus: MF_TOPOSTATUS_READY\n"); hr = pSession->Start(&GUID_NULL, &varStartPosition); break; case MF_TOPOSTATUS_ENDED: wprintf(L"MESessionTopologyStatus: MF_TOPOSTATUS_ENDED\n"); break; } } break; case MESessionStarted: wprintf(L"MESessionStarted\n"); break; case MESessionEnded: wprintf(L"MESessionEnded\n"); hr = pSession->Stop(); break; case MESessionStopped: wprintf(L"MESessionStopped.\n"); hr = pSession->Close(); break; case MESessionClosed: wprintf(L"MESessionClosed\n"); bGetAnotherEvent = FALSE; break; default: wprintf(L"Media session event: %d\n", meType); break; } } if (FAILED(hr) || FAILED(hrStatus)) { bGetAnotherEvent = FALSE; } } wprintf(L"Shutting down the media session.\n"); pSession->Shutdown(); PropVariantClear(&varStartPosition); return hr; }
/** Opens file from memory */ FIntPoint FImfVideoPlayer::OpenFile( void* FileSource, int32 Size, ECodecMovie::Type CodecType, FImfSampleGrabberCallback* SampleGrabberCallback ) { FIntPoint OutDimensions = FIntPoint::ZeroValue; HRESULT HResult = S_OK; /* Create Media Session */ HResult = MFCreateMediaSession( NULL, &MediaSession ); check( SUCCEEDED( HResult ) ); /* Begin Get Event */ HResult = MediaSession->BeginGetEvent( this, NULL ); check( SUCCEEDED( HResult ) ); IMFSourceResolver* SourceResolver = NULL; IUnknown* Source = NULL; /* Create Source Resolver */ HResult = MFCreateSourceResolver( &SourceResolver ); check( SUCCEEDED( HResult ) ); /* Determine codec type */ FString Ext = ".null"; switch( CodecType ) { case ECodecMovie::CodecMovie_Wmv: Ext = ".wmv"; break; /* Currently not working */ case ECodecMovie::CodecMovie_Mp4: Ext = ".mp4"; break; case ECodecMovie::CodecMovie_Avi: Ext = ".avi"; break; } /* Create custom ByteStream */ FImfByteStream* NewByteStream = new FImfByteStream( FileSource, Size ); /* Create Media Source */ MF_OBJECT_TYPE ObjectType = MF_OBJECT_INVALID; HResult = SourceResolver->CreateObjectFromByteStream( NewByteStream, *Ext, MF_RESOLUTION_MEDIASOURCE, NULL, &ObjectType, &Source ); SourceResolver->Release( ); if( SUCCEEDED( HResult ) ) { HResult = Source->QueryInterface( IID_PPV_ARGS( &MediaSource ) ); Source->Release( ); /* Set Playback Topology */ OutDimensions = SetPlaybackTopology( SampleGrabberCallback ); } else { /* Failed */ MovieIsFinished.Set( 1 ); } return OutDimensions; }