HRESULT CreateMediaSource(const WCHAR *sURL, IMFMediaSource **ppSource) { if (sURL == NULL) { return E_POINTER; } if (ppSource == NULL) { return E_POINTER; } HRESULT hr = S_OK; CComPtr<IMFSourceResolver> pSourceResolver; CComPtr<IUnknown> pSourceUnk; // Create the source resolver. if (SUCCEEDED(hr)) { hr = MFCreateSourceResolver(&pSourceResolver); } // Use the source resolver to create the media source. if (SUCCEEDED(hr)) { MF_OBJECT_TYPE ObjectType = MF_OBJECT_INVALID; hr = pSourceResolver->CreateObjectFromURL( sURL, // URL of the source. MF_RESOLUTION_MEDIASOURCE, // Create a source object. NULL, // Optional property store. &ObjectType, // Receives the created object type. &pSourceUnk // Receives a pointer to the media source. ); } // Get the IMFMediaSource interface from the media source. if (SUCCEEDED(hr)) { hr = pSourceUnk->QueryInterface(ppSource); } return hr; }
MediaSource* MediaSource::Open(PCWSTR url) { MF_OBJECT_TYPE ObjectType = MF_OBJECT_INVALID; IMFSourceResolver* mfSourceResolver = nullptr; IMFMediaSource* mfMediaSource = nullptr; IUnknown* pSource = nullptr; // Create the source resolver. HRESULT hr = MFCreateSourceResolver(&mfSourceResolver); // Use the source resolver to create the media source. hr = mfSourceResolver->CreateObjectFromURL( url, // URL of the source. MF_RESOLUTION_MEDIASOURCE, // Create a source object. nullptr, // Optional property store. &ObjectType, // Receives the created object type. &pSource // Receives a pointer to the media source. ); // Get the IMFMediaSource interface from the media source. hr = pSource->QueryInterface(IID_PPV_ARGS(&mfMediaSource)); if (pSource) pSource->Release(); if (mfSourceResolver) mfSourceResolver->Release(); return new MediaSource(mfMediaSource); }
// Create a media source from a URL. IMFMediaSourcePtr CreateMediaSource(const std::wstring& sURL) { MF_OBJECT_TYPE ObjectType = MF_OBJECT_INVALID; IMFSourceResolverPtr pSourceResolver; Microsoft::WRL::ComPtr<IUnknown> pSource; IMFMediaSourcePtr pSourceReturn; // Create the source resolver. THROW_IF_ERR(MFCreateSourceResolver(pSourceResolver.GetAddressOf())); // Use the source resolver to create the media source. // Note: For simplicity this sample uses the synchronous method to create // the media source. However, creating a media source can take a noticeable // amount of time, especially for a network source. For a more responsive // UI, use the asynchronous BeginCreateObjectFromURL method. THROW_IF_ERR(pSourceResolver->CreateObjectFromURL( sURL.c_str(), // URL of the source. MF_RESOLUTION_MEDIASOURCE, // Create a source object. NULL, // Optional property store. &ObjectType, // Receives the created object type. &pSource // Receives a pointer to the media source. )); // Get the IMFMediaSource interface from the media source. THROW_IF_ERR(pSource.As<IMFMediaSource>(&pSourceReturn)); return pSourceReturn; }
HRESULT CreateMediaSource( const WCHAR *sURL, // The URL of the file to open. IMFMediaSource** ppMediaSource // Receives a pointer to the media source. ) { if (!sURL) { return E_INVALIDARG; } if (!ppMediaSource) { return E_POINTER; } HRESULT hr = S_OK; MF_OBJECT_TYPE ObjectType = MF_OBJECT_INVALID; IMFSourceResolver* pSourceResolver = NULL; IUnknown* pUnkSource = NULL; // Create the source resolver. hr = MFCreateSourceResolver(&pSourceResolver); // Use the source resolver to create the media source. if (SUCCEEDED(hr)) { hr = pSourceResolver->CreateObjectFromURL( sURL, // URL of the source. MF_RESOLUTION_MEDIASOURCE, // Create a source object. NULL, // Optional property store. &ObjectType, // Receives the created object type. &pUnkSource // Receives a pointer to the media source. ); } // Get the IMFMediaSource from the IUnknown pointer. if (SUCCEEDED(hr)) { hr = pUnkSource->QueryInterface(IID_PPV_ARGS(ppMediaSource)); } SafeRelease(&pSourceResolver); SafeRelease(&pUnkSource); return hr; }
HRESULT MediaInfo::Open(LPCWSTR uri) { ComPtr<IMFSourceResolver> resolver; HRESULT hr = MFCreateSourceResolver(&resolver); if (FAILED(hr)) return hr; MF_OBJECT_TYPE objType = MF_OBJECT_INVALID; ComPtr<IUnknown> punk; hr = resolver->CreateObjectFromURL(uri, MF_RESOLUTION_MEDIASOURCE, NULL, &objType, &punk); if (FAILED(hr)) return hr; if (objType != MF_OBJECT_MEDIASOURCE) return E_FAIL; return InternalOpen(punk.Get()); }
HRESULT MediaInfo::Open(IMFByteStream* stream) { ComPtr<IMFSourceResolver> resolver; HRESULT hr = MFCreateSourceResolver(&resolver); if (FAILED(hr)) return hr; MF_OBJECT_TYPE objType = MF_OBJECT_INVALID; ComPtr<IUnknown> punk; hr = resolver->CreateObjectFromByteStream(stream, NULL, MF_RESOLUTION_MEDIASOURCE|MF_RESOLUTION_CONTENT_DOES_NOT_HAVE_TO_MATCH_EXTENSION_OR_MIME_TYPE, NULL, &objType, &punk); if (FAILED(hr)) return hr; if (objType != MF_OBJECT_MEDIASOURCE) return E_FAIL; return InternalOpen(punk.Get()); }
/** * Creates a Media Source. * * @param env JNI env * @param path path * @param ppMediaSrc media source * @return HRESULT */ HRESULT mf_createMediaSource(JNIEnv *env, jstring path, IMFMediaSource **ppMediaSrc) { HRESULT res = S_OK; const LPWSTR pwszFilePath = (LPWSTR)env->GetStringChars(path, NULL); IUnknown *pUnk = NULL; IMFSourceResolver *pResolver = NULL; MF_OBJECT_TYPE ObjectType = MF_OBJECT_INVALID; *ppMediaSrc = NULL; res = MFCreateSourceResolver(&pResolver); if (res != S_OK || pResolver == NULL) { throwUnsupportedAudioFileExceptionIfError(env, res, "Failed to create source resolver"); goto bail; } // File format may not match its extension so we ignore the extension res = pResolver->CreateObjectFromURL( pwszFilePath, MF_RESOLUTION_MEDIASOURCE | MF_RESOLUTION_READ | MF_RESOLUTION_CONTENT_DOES_NOT_HAVE_TO_MATCH_EXTENSION_OR_MIME_TYPE, NULL, &ObjectType, &pUnk); if (res != S_OK || pUnk == NULL) { throwUnsupportedAudioFileExceptionIfError(env, res, "Failed to create object from url"); goto bail; } res = pUnk->QueryInterface( IID_IMFMediaSource, (void**)(ppMediaSrc)); if (res != S_OK) { throwUnsupportedAudioFileExceptionIfError(env, res, "Failed get media source interface"); goto bail; } bail: SAFE_RELEASE(pResolver) SAFE_RELEASE(pUnk) env->ReleaseStringChars(path, (jchar *)pwszFilePath); return res; }
HRESULT CPlayer::CreateMediaSource(const WCHAR *sURL) { TRACE((L"CPlayer::CreateMediaSource\n")); HRESULT hr = S_OK; MF_OBJECT_TYPE ObjectType = MF_OBJECT_INVALID; IMFSourceResolver* pSourceResolver = NULL; IUnknown* pSource = NULL; SAFE_RELEASE(m_pSource); // Create the source resolver. CHECK_HR(hr = MFCreateSourceResolver(&pSourceResolver)); // Use the source resolver to create the media source. // Note: For simplicity this sample uses the synchronous method on // IMFSourceResolver to create the media source. However, creating a // media source can take a noticeable amount of time, especially for // a network source. For a more responsive UI, use the asynchronous // BeginCreateObjectFromURL method. CHECK_HR(hr = pSourceResolver->CreateObjectFromURL( sURL, // URL of the source. MF_RESOLUTION_MEDIASOURCE, // Create a source object. NULL, // Optional property store. &ObjectType, // Receives the created object type. &pSource // Receives a pointer to the media source. )); // Get the IMFMediaSource interface from the media source. CHECK_HR(hr = pSource->QueryInterface(__uuidof(IMFMediaSource), (void**)&m_pSource)); done: SAFE_RELEASE(pSourceResolver); SAFE_RELEASE(pSource); return hr; }
HRESULT CTedMediaFileRenderer::CreateSource(IMFMediaSource** ppSource) { HRESULT hr; CComPtr<IMFSourceResolver> spSourceResolver; CComPtr<IUnknown> spSourceUnk; IMFMediaSource* pSource; IFC( MFCreateSourceResolver(&spSourceResolver) ); MF_OBJECT_TYPE ObjectType; IFC( spSourceResolver->CreateObjectFromURL(m_szFileName, MF_RESOLUTION_MEDIASOURCE, NULL, &ObjectType, &spSourceUnk) ); hr = spSourceUnk->QueryInterface(IID_IMFMediaSource, (void**) &pSource); if(E_NOINTERFACE == hr) { hr = MF_E_UNSUPPORTED_BYTESTREAM_TYPE; } IFC( hr ); *ppSource = pSource; Cleanup: return hr; }
void SourceResolver::load(QMediaResourceList& resources, QIODevice* stream) { QMutexLocker locker(&m_mutex); HRESULT hr = S_OK; if (!m_sourceResolver) hr = MFCreateSourceResolver(&m_sourceResolver); if (m_stream) { m_stream->Release(); m_stream = NULL; } if (FAILED(hr)) { qWarning() << "Failed to create Source Resolver!"; emit error(hr); } else if (stream) { if (resources.count() > 0) { QMediaResource resource = resources.takeFirst(); QUrl url = resource.url(); m_stream = new MFStream(stream, false); hr = m_sourceResolver->BeginCreateObjectFromByteStream(m_stream, reinterpret_cast<const OLECHAR *>(url.toString().utf16()), MF_RESOLUTION_MEDIASOURCE, NULL, &m_cancelCookie, this, new State(m_sourceResolver, true)); if (FAILED(hr)) { qWarning() << "Unsupported stream!"; emit error(hr); } } else { hr = MF_E_UNSUPPORTED_BYTESTREAM_TYPE; qWarning() << "Can't load stream without a hint of MIME type in a url"; emit error(hr); } } else { QMediaResource resource = resources.takeFirst(); QUrl url = resource.url(); #ifdef DEBUG_MEDIAFOUNDATION qDebug() << "loading :" << url; qDebug() << "url path =" << url.path().mid(1); #endif #ifdef TEST_STREAMING //Testing stream function if (url.scheme() == QLatin1String("file")) { stream = new QFile(url.path().mid(1), this); if (stream->open(QIODevice::ReadOnly)) { m_stream = new MFStream(stream, true); hr = m_sourceResolver->BeginCreateObjectFromByteStream(m_stream, reinterpret_cast<const OLECHAR *>(url.toString().utf16()), MF_RESOLUTION_MEDIASOURCE, NULL, &m_cancelCookie, this, new State(m_sourceResolver, true)); if (FAILED(hr)) { qWarning() << "Unsupported stream!"; emit error(hr); } } else { delete stream; emit error(QMediaPlayer::FormatError); } } else #endif if (url.scheme() == QLatin1String("qrc")) { // If the canonical URL refers to a Qt resource, open with QFile and use // the stream playback capability to play. stream = new QFile(QLatin1Char(':') + url.path(), this); if (stream->open(QIODevice::ReadOnly)) { m_stream = new MFStream(stream, true); hr = m_sourceResolver->BeginCreateObjectFromByteStream(m_stream, reinterpret_cast<const OLECHAR *>(url.toString().utf16()), MF_RESOLUTION_MEDIASOURCE, NULL, &m_cancelCookie, this, new State(m_sourceResolver, true)); if (FAILED(hr)) { qWarning() << "Unsupported stream!"; emit error(hr); } } else { delete stream; emit error(QMediaPlayer::FormatError); } } else { hr = m_sourceResolver->BeginCreateObjectFromURL(reinterpret_cast<const OLECHAR *>(url.toString().utf16()), MF_RESOLUTION_MEDIASOURCE, NULL, &m_cancelCookie, this, new State(m_sourceResolver, false)); if (FAILED(hr)) { qWarning() << "Unsupported url scheme!"; emit error(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; }
/** 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; }