예제 #1
0
HRESULT FMPlayerDShow::BuildTheGraph()
{
	CComQIPtr<IGraphBuilder> pGraphBuilder = m_FilterGraph; 
	HRESULT hr = E_FAIL; 
	if (pGraphBuilder)
	{
		CComPtr<IPin> Pin1 = GetOutPin(m_AviSplitter, 0); 
		CComPtr<IPin> Pin2 = GetOutPin(m_AviSplitter, 1); 
		CComPtr<IPin> PinXV = GetInPin(m_xVid, 0); 
		if (Pin1 && Pin2 && PinXV)
		{
			hr = m_FilterGraph->ConnectDirect(Pin1, PinXV, NULL); 
			if (FAILED(hr))
			{
				hr = m_FilterGraph->ConnectDirect(Pin2, PinXV, NULL); 
			}
			if (SUCCEEDED(hr))
			{
				CComPtr<IPin> PinOutXV = GetOutPin(m_xVid, 0); 
				CComPtr<IPin> PinInRenderer = GetInPin(m_VideoRenderer, 0); 
				if (PinOutXV && PinInRenderer)
				{
					CComPtr<IPin> PinConnected; 
					if (PinOutXV->ConnectedTo(&PinConnected) & VFW_E_NOT_CONNECTED)
					{
						//Disconnect renderer from any filters;
						PinInRenderer->Disconnect(); 
						hr = m_FilterGraph->ConnectDirect(PinOutXV, PinInRenderer, NULL); 
					}
					if (SUCCEEDED(hr))
					{
						hr = RenderOutputPins(pGraphBuilder, m_AviSplitter); 
						if (FAILED(hr))
							_DBGAlertM("FMediaMgr: **RenderOutputPins: %s\n", GetErrorDescription(hr)); 
						m_GraphBuilt = TRUE; 
					}
					else
						_DBGAlertM("FMediaMgr: **Connect PinOutXV to PinInRenderer: %s\n", GetErrorDescription(hr)); 
				}
				else
					_DBGAlertM("FMediaMgr: **PinOutXV or PinInRenderer is NULL\n"); 
			}
			else
				_DBGAlertM("FMediaMgr: **Connect Splitter to xvid: %s\n", GetErrorDescription(hr)); 
		}
		else
			_DBGAlertM("FMediaMgr: **Pin1 or Pin2 or PinXV is NULL.\n"); 
	}

	return hr; 
}
예제 #2
0
HRESULT FMPlayerDShow::LoadMedia(const tchar* pFileName, IMediaOptions* pOptions)
{
	USES_CONVERSION;

	HRESULT hr = E_FAIL; 

	if (m_FourCC == "divx" && m_StreamFormat == "divx")
	{
		Init(m_hWndParent, m_pNotify); 
	}

	if (m_FilterGraph == NULL)
		return E_FAIL;

	m_MediaControl->Stop(); 


	if (m_FileSource)
	{
		m_FilterGraph->RemoveFilter(m_FileSource);
		m_FileSource.Release();
	}
	else
	{
	/*	CComQIPtr<IGraphBuilder> pGraphBuilder = m_FilterGraph; 

		if (SUCCEEDED(pGraphBuilder->RenderFile(T2OLE(pFileName), NULL)))
		{
			hr = Start();
			return hr; 
		}
	*/
		if (SUCCEEDED(DetermineVideoType(pFileName)))
		{
			InitFilters(pFileName); 
		}
		else
		{
			_DBGAlertM("FMPlayerDShow: PlayVideo: **Unable to determine file format.\n");
			return E_FAIL;
		}
	}

	_DBGAlertM("FMPlayerDShow: Starting playback: %s; offset = %I64d\n", pFileName, pOptions->rtOffset); 

	CComQIPtr<IGraphBuilder> pGraphBuilder = m_FilterGraph; 

	hr = pGraphBuilder->AddSourceFilter(T2OLE(pFileName), T2OLE(pFileName), &m_FileSource); 

	if (SUCCEEDED(hr))
	{
		CComPtr<IPin> PinSourceOut = GetOutPin(m_FileSource, 0); 
		CComPtr<IPin> PinSplitterIn = GetInPin(m_AviSplitter, 0); 

		if (PinSourceOut && PinSplitterIn)
		{
			PinSplitterIn->Disconnect(); 
			hr = m_FilterGraph->ConnectDirect(PinSourceOut, PinSplitterIn, NULL); 
			if (FAILED(hr))
				_DBGAlertM("FMediaMgr: **Connect SourceOut to SplitterIn: %s\n", GetErrorDescription(hr)); 
		}
		else
			_DBGAlertM("FMediaMgr: **PinSourceOut or PinSourceIn is NULL\n"); 

		hr = BuildTheGraph(); 

		if (SUCCEEDED(hr))
		{
			m_FileName = pFileName; 
			m_Offset = pOptions->rtOffset;

			hr = Start(); 
			if (FAILED(hr))
				_DBGAlertM("FMPlayerDShow: **Start() failed: %s\n", GetErrorDescription(hr));
			else
			{
				SetVideoRect(m_VideoRect); 
			}
		}
		else
			_DBGAlertM("FMPlayerDShow: **Cannot Start(): %s\n", GetErrorDescription(hr)); 
	}
	else
		_DBGAlertM("FMPlayerDShow: **AddSourceFilter() failed: %s\n", GetErrorDescription(hr));

	_DBGAlertM("FMPlayerDShow: StartPlayback() exiting: %s\n", GetErrorDescription(hr)); 
	return hr; 
}
예제 #3
0
bool VideoCapture::InitCamera(int iDevice, int iResolution)
{
	if (cameraInitialized) return false;

	HRESULT hr;
	bool ans;
	int iWidth = devices_resolutions[iDevice].x[iResolution];
	int iHeight = devices_resolutions[iDevice].y[iResolution];

    USES_CONVERSION;

	CoCreateInstance(CLSID_SampleGrabber, NULL, CLSCTX_INPROC_SERVER,
		  IID_ISampleGrabber, (void**)&pGrabber);
	if( !pGrabber ) return false;
    
	hr = pNull.CoCreateInstance(CLSID_NullRenderer);
	
    CComQIPtr< IBaseFilter, &IID_IBaseFilter > pGrabberBase( pGrabber );

	ans = BindFilter(iDevice, &pSource);
	if( !pSource ) return false;
	
	hr = pGraph.CoCreateInstance( CLSID_FilterGraph );
	if (hr!=S_OK) return false;

	hr = pGraph->AddFilter( pSource, L"Source" );
	if (hr!=S_OK) return false;
	hr = pGraph->AddFilter( pGrabberBase, L"Grabber" );
    if (hr!=S_OK) return false;
	hr = pGraph->AddFilter( pNull, L"NullRenderer" );
	if (hr!=S_OK) return false;
	
	// Tell the grabber to grab 24-bit video. Must do this
    // before connecting it
    CMediaType GrabType;

	GrabType.SetType( &MEDIATYPE_Video );
	GrabType.SetSubtype( &MEDIASUBTYPE_RGB24 );

	hr = pGrabber->SetMediaType( &GrabType );
	if (hr!=S_OK) return false;

    CComPtr< IPin > pGrabPinIn;
    CComPtr< IPin > pGrabPinOut;
    CComPtr< IPin > pNullPinIn;

    pSourcePin = GetOutPin( pSource, 0 );
    pGrabPinIn   = GetInPin( pGrabberBase, 0 );
    pGrabPinOut  = GetOutPin( pGrabberBase, 0 );
    pNullPinIn  = GetInPin( pNull, 0 );

	CComPtr <ICaptureGraphBuilder2> pCaptB;
	pCaptB.CoCreateInstance(CLSID_CaptureGraphBuilder2);
	pCaptB->SetFiltergraph(pGraph);

	hr = pCaptB->FindInterface(&PIN_CATEGORY_CAPTURE,
		&MEDIATYPE_Video,pSource, 
		IID_IAMStreamConfig, (void**)&pConfig);
	if (hr!=S_OK) return false;

	SetResolution(iDevice, iResolution);
    // ... and connect them
    //
    hr = pGraph->Connect( pSourcePin, pGrabPinIn);
    if (hr!=S_OK) return false;
	
	hr = pGraph->Connect( pGrabPinOut, pNullPinIn );
	if (hr!=S_OK) return false;
	
	// Ask for the connection media type so we know its size
    //
    AM_MEDIA_TYPE mt;
	hr = pGrabber->GetConnectedMediaType( &mt );
	if (hr!=S_OK) return false;

    VIDEOINFOHEADER * vih = (VIDEOINFOHEADER*) mt.pbFormat;
    callback.im_width  = iWidth;
    callback.im_height = iHeight;
    FreeMediaType( mt );

	// Write the bitmap format
    //   
    memset( &(callback.cbInfo.bih), 0, sizeof( callback.cbInfo.bih ) );
    callback.cbInfo.bih.biSize = iWidth*iHeight*3;
    callback.cbInfo.bih.biWidth = iWidth;
    callback.cbInfo.bih.biHeight = iHeight;
    callback.cbInfo.bih.biPlanes = 1;
    callback.cbInfo.bih.biBitCount = 24;

	callback.cbInfo.pBuffer = new byte[callback.cbInfo.bih.biSize];
	    
	hr = pGrabber->SetBufferSamples( FALSE );
	hr = pGrabber->SetOneShot( FALSE );
    hr = pGrabber->SetCallback( &callback, 1);

	hr = pGraph->QueryInterface(IID_IMediaControl,(LPVOID *) &pControl);

	hr = pControl->Run( );

	cameraInitialized = true;
	return true;	
}