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); }
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; }
/** * 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; }