Пример #1
1
void playNormalVideo()
{
	IGraphBuilder *pGraphBuilder;
	IMediaControl *pMediaControl;
	IMediaEvent *pMediaEvent;
	long eventCode;

	CoInitialize(NULL);

	CoCreateInstance(CLSID_FilterGraph,
		NULL,
		CLSCTX_INPROC,
		IID_IGraphBuilder,
		(LPVOID *)&pGraphBuilder);

	pGraphBuilder->QueryInterface(IID_IMediaControl,
		(LPVOID *)&pMediaControl);

	pGraphBuilder->QueryInterface(IID_IMediaEvent,
		(LPVOID *)&pMediaEvent);

	pMediaControl->RenderFile(L"Rscreen_shortFinger.avi");

	pMediaControl->Run();

	FILTER_STATE fs;
	HRESULT hr = pMediaControl->GetState(100, (OAFilterState*)&fs);

	// The first argument is timeout value.
	// If you change the "-1" part into "2",
	// WaitForCompletion will timeout after 2 seconds.
	pMediaEvent->WaitForCompletion(-1, &eventCode);
	switch (eventCode) {
	 case 0:
		 printf("timeout\n");
		 break;
	 case EC_COMPLETE:
		 printf("complete\n");
		 break;
	 case EC_ERRORABORT:
		 printf("errorabort\n");
		 break;
	 case EC_USERABORT:
		 printf("userabort\n");
		 break;
	}

	hr = pMediaControl->GetState(100, (OAFilterState*)&fs);
	pMediaControl->Run();
	hr = pMediaControl->GetState(100, (OAFilterState*)&fs);
	pMediaEvent->WaitForCompletion(-1, &eventCode);

	pMediaControl->Release();
	pGraphBuilder->Release();
	CoUninitialize();
}
Пример #2
0
void Video::abort()
{
	HRESULT hr;
	IMediaControl *pMC;

	if(!init_success)
		return;


	// Obtain the interface to our filter graph
	if( (hr = pGraph->QueryInterface(IID_IMediaControl, (void **) &pMC)) == 0)
	{
		// Ask the filter graph to stop and release the interface
		hr = pMC->Stop();
		pMC->Release();

		// rewind to the beginning
		IMediaPosition *pMP;
		if( (hr=pGraph->QueryInterface( IID_IMediaPosition, (void **) &pMP))==0)
		{
			pMP->put_CurrentPosition( 0);
			pMP->Release();
		}
	}
	state = STOPPED;

	if( hr && !skip_on_fail_flag)
		err.run("video.abort error %d", hr);
}
Пример #3
0
//
//  Toggle the pause state for the media player.  Returns true if the media player pauses, false if it runs.
//
bool CMediaPlayer::TogglePauseState()
{
    bool isPaused = false;
    IMediaControl *mediaControl;

    HRESULT hr = _GraphBuilder->QueryInterface(IID_PPV_ARGS(&mediaControl));
    if (SUCCEEDED(hr))
    {
        OAFilterState filterState;

        hr = mediaControl->GetState(INFINITE, &filterState);
        if (SUCCEEDED(hr))
        {
            if (filterState == State_Running)
            {
                mediaControl->Pause();
                isPaused = true;
            }
            else if (filterState == State_Paused)
            {
                mediaControl->Run();
                isPaused = false;
            }
        }
        mediaControl->Release();
    }
    return isPaused;
}
Пример #4
0
void video_start(int id, bool loop) {
	get_video(videoStruct, id);
	IMediaControl *pControl = NULL;
    IMediaEvent   *pEvent = NULL;
    HRESULT hr = videoStruct->pGraph->QueryInterface(IID_IMediaControl, (void **)&pControl);
    hr = videoStruct->pGraph->QueryInterface(IID_IMediaEvent, (void **)&pEvent);
	
	// Run the graph.
    hr = pControl->Run();
    while (SUCCEEDED(hr)) {
        if (SUCCEEDED(hr)) {
            // Wait for completion.
            long evCode;
            pEvent->WaitForCompletion(INFINITE, &evCode);
        } else {
			MessageBox(NULL, "Failed to run the COM control.", "ERROR", MB_ICONERROR | MB_OK);
			// Don't return so pointers can be released.
		}
		
		if (loop) {
			video_set_seek(id, 0);
		} else {
			break;
		}
    } 
	if (FAILED(hr)) {
		MessageBox(NULL, "Failed to render the Filter Graph Manager.", "ERROR", MB_ICONERROR | MB_OK);
		// Don't return so pointers can be released.
    }

    pControl->Release();
	pEvent->Release();
}
Пример #5
0
void main()
{
	int option;
    IGraphBuilder *pGraph = NULL;  //Creem l'apuntador al graf de filtres
	IMediaControl *pControl = NULL; //creem l'apuntador a un controlador per ayurar i iniciar el graf 
	IMediaEvent *pEvent = NULL; // apunta a l'objecte necessari per obtenir events del filter graph manager
	//IBaseFilter *pGrabberF = NULL;
    //ISampleGrabber *pGrabber = NULL;

	HRESULT hr = CoInitialize(NULL); // Inicialitzem la llibreria COM
	if ( FAILED(hr) ){
		printf("ERROR - Could not initialize COM library");
        return;
	}
	// Create the filter graph manager and query for interfaces.
    hr = CoCreateInstance(CLSID_FilterGraph, NULL, CLSCTX_INPROC_SERVER, IID_IGraphBuilder, (void **)&pGraph);
    if (FAILED(hr))
    {
        printf("ERROR - Could not create the Filter Graph Manager.");
        return;
    }
	
    hr = pGraph->QueryInterface(IID_IMediaControl, (void **)&pControl);
    hr = pGraph->QueryInterface(IID_IMediaEvent, (void **)&pEvent);

    // Build the graph. IMPORTANT: Change this string to a file on your system.
	cout<<"introduce 1:chicken 2:futbol 3: video futbol audio chicken: \n";
	cin>>option;
	switch(option)
	{
		case 1: hr = pGraph->RenderFile(L"C:\\Users\\Victor\\Downloads\\chicken.wmv", NULL);
			
			break;
		case 2: hr = pGraph->RenderFile(L"C:\\Users\\Victor\\Downloads\\futbol.mpg", NULL);
			break;
		case 3:  // Create the Sample Grabber filter.
  			break;
	}

    if (SUCCEEDED(hr))
    {
        // Run the graph.
        hr = pControl->Run();
        if (SUCCEEDED(hr))
        {
            // Wait for completion.
            long evCode;
            pEvent->WaitForCompletion(INFINITE, &evCode);

            // Note: Do not use INFINITE in a real application, because it
            // can block indefinitely.
        }
    }
    pControl->Release();
    pEvent->Release();
    pGraph->Release();
    CoUninitialize();

}
Пример #6
0
void play_movie( HWND hwnd )
{
    IGraphBuilder *pGraph;
    IMediaControl *pMediaControl;
    IMediaEvent   *pEvent;
    IBasicVideo   *pBasic;
    IVideoWindow    *pVidWin = NULL;
    RECT grc;
    long width, height;

    CoInitialize(NULL);
    
    // Create the filter graph manager and query for interfaces.
    CoCreateInstance( 
	CLSID_FilterGraph, 
	NULL, 
	CLSCTX_INPROC_SERVER, 
	IID_IGraphBuilder, 
	(void **)&pGraph);
    
    pGraph->QueryInterface(IID_IVideoWindow, (void **)&pVidWin);

    pGraph->QueryInterface(IID_IMediaControl, (void **)&pMediaControl);
    pGraph->QueryInterface(IID_IMediaEvent, (void **)&pEvent);
    pGraph->QueryInterface(IID_IBasicVideo, (void**)&pBasic );

    // Build the graph. IMPORTANT: Change string to a file on your system.
    pGraph->RenderFile(L"e:\\alpha\\running.avi", NULL);

    pBasic->GetVideoSize( &width, &height );  
    printf( "video frames are %d x %d\n", width, height );

    pVidWin->put_Owner((OAHWND)hwnd);
    pVidWin->put_WindowStyle(WS_CHILD | WS_CLIPSIBLINGS);

    GetClientRect( hwnd, &grc );
    pVidWin->SetWindowPosition(10, 10, width, height);
    printf( "window is %d x %d\n", grc.right, grc.bottom );

    // Run the graph.
    pMediaControl->Run();

    // Wait for completion. 
    long evCode;
    pEvent->WaitForCompletion(INFINITE, &evCode);

    pVidWin->put_Visible(OAFALSE);
    pVidWin->put_Owner(NULL);
    
    // Clean up.
    pBasic->Release();
    pVidWin->Release();
    pMediaControl->Release();
    pEvent->Release();
    pGraph->Release();
    CoUninitialize();
}
Пример #7
0
void PlaybackControls::Play()
{
	IMediaControl *pControl = NULL;
		
	if (SUCCEEDED(LoadFile()))
	{
		mpGraph->QueryInterface(IID_IMediaControl, (void **)&pControl);
		pControl->Run();
		pControl->Release();
	}
}
Пример #8
0
bool CHogVideo::UnHog()
{
    if (!m_pMediaControl)
        return false;

    IMediaControl *pMediaControl = (IMediaControl*)m_pMediaControl;
    // Stop the graph.
    pMediaControl->Stop();

    return true;
}
Пример #9
0
//
//  Stops media playback.
//
void CMediaPlayer::Stop()
{
    IMediaControl *mediaControl;

    HRESULT hr = _GraphBuilder->QueryInterface(IID_PPV_ARGS(&mediaControl));
    if (SUCCEEDED(hr))
    {
        mediaControl->Stop();
        mediaControl->Release();
    }
	_Playing = false;
}
Пример #10
0
bool DirectShowUtil::FilterGrapph_Pause(IGraphBuilder *pFilterGraph)
{
	bool bRet=false;

	if(pFilterGraph){
		IMediaControl *pControl = GetMediaControl(pFilterGraph);
		if(pControl){
			HRESULT hr = pControl->Pause();
			SAFE_RELEASE(pControl);
			bRet = true;
		}
	}
	return bRet;
}
Пример #11
0
//
//  Continues media playback if it is currently paused.
//
bool CMediaPlayer::Continue()
{
    bool isContinued = false;
    IMediaControl *mediaControl;

    HRESULT hr = _GraphBuilder->QueryInterface(IID_PPV_ARGS(&mediaControl));
    if (SUCCEEDED(hr))
    {
        mediaControl->Run();
        mediaControl->Release();
        isContinued = true;
    }
    return isContinued;
}
Пример #12
0
void CaptureDShow::uninit()
{
    IMediaControl *control = NULL;

    if (SUCCEEDED(this->m_graph->QueryInterface(IID_IMediaControl,
                                                reinterpret_cast<void **>(&control)))) {
        control->Stop();
        control->Release();
    }

    this->m_grabber.clear();
    this->m_graph->Release();
    this->m_graph = NULL;
}
Пример #13
0
bool CaptureAndSend::start()
{
	if (m_CurrentState == Running)
	{
		return true;
	}

	if (!m_NetSenderFilter->start())
	{
		return false;
	}

	HRESULT hr = S_OK;

	IGraphBuilder* graphBuilder;
	hr = m_FilterGraph->QueryInterface(IID_IGraphBuilder, (void**)&graphBuilder);
	if (FAILED(hr))
	{
		ErrorPrint("Get graph builder interface error",hr);
		return false;
	}
	ComReleaser graphBuilderReleaser(graphBuilder);

	IMediaControl* mediaControl;
	hr = m_FilterGraph->QueryInterface(IID_IMediaControl, (void**)&mediaControl);
	if(FAILED(hr))
	{
		ErrorPrint("Get media control error", hr);
		return false;
	}
	ComReleaser mediaControlReleaser(mediaControl);

	hr = mediaControl->Run();
	if(FAILED(hr))
	{
		ErrorPrint("Run error",hr);
		return false;
	}

	if (m_CurrentState == Stopped)
	{
		g_StartCount++;
	}
	m_CurrentState = Running;

	return true;
}
Пример #14
0
void Video::PlayMovie(string path)
{
	IGraphBuilder *pGraph = NULL;
    IMediaControl *pControl = NULL;
    IMediaEvent   *pEvent = NULL;
	IVideoWindow  *pVideo = NULL;
	
    // Initialize the COM library.
    CoInitialize(NULL);

    // Create the filter graph manager and query for interfaces.
    CoCreateInstance(CLSID_FilterGraph, NULL, CLSCTX_INPROC_SERVER, IID_IGraphBuilder, (void **)&pGraph);

	// Build the graph
	int len;
	int slength = (int)path.length() + 1;
	len = MultiByteToWideChar(CP_ACP, 0, path.c_str(), slength, 0, 0);
	 wchar_t* buf = new wchar_t[len];
	MultiByteToWideChar(CP_ACP, 0, path.c_str(), slength, buf, len);
	 std::wstring r(buf);
	delete[] buf;

	pGraph->RenderFile(LPCWSTR(r.c_str()), NULL);

	// set the owner window
	pGraph->QueryInterface(IID_IVideoWindow, (void **) &pVideo);	
	pVideo->put_Owner((OAHWND)window);
	pVideo->put_WindowStyle( WS_CHILD );
	pVideo->put_Left(0);
	pVideo->put_Top(0);
	

    pGraph->QueryInterface(IID_IMediaControl, (void **)&pControl);
    pGraph->QueryInterface(IID_IMediaEvent, (void **)&pEvent);

	pControl->Run();
	long evCode;
	pEvent->WaitForCompletion(-1, &evCode);
	
	// release controls
    pControl->Release();
    pEvent->Release();
    pGraph->Release();
	pVideo->Release();
    CoUninitialize();
}
Пример #15
0
//
//  Starts the media player.
//
void CMediaPlayer::Play()
{
	if(_Playing)
		return;
	_Playing = true;

    IMediaControl *mediaControl;
    _GraphBuilder->QueryInterface(IID_PPV_ARGS(&mediaControl));

    if (mediaControl != NULL)
    {
        REFERENCE_TIME timeBegin = 0;
        _MediaSeeking->SetPositions(&timeBegin, AM_SEEKING_AbsolutePositioning, NULL, AM_SEEKING_NoPositioning);

        mediaControl->Run();
        mediaControl->Release();
    }
}
Пример #16
0
void CTsReaderFilter::SetMediaPosition(REFERENCE_TIME MediaPos)
{
  {
    CAutoLock cObjectLock(&m_GetTimeLock);
    m_MediaPos = MediaPos ;
    m_BaseTime = (REFERENCE_TIME)GetTickCount() * 10000 ; // m_pClock->GetTime(&m_BaseTime) ;
    m_LastTime=m_BaseTime ;
  }
//  LogDebug("SetMediaPos : %f %f",(float)MediaPos/10000,(float)m_LastTime/10000) ; 

// This is not really the right place, but this is the only method called by "MPmain" that could allow
// TsReader to "Pause" itself without deadlock issue.
// This is also here to allow compatibility with previous releases, avoiding a new callback from MP.

  if (m_bRenderingClockTooFast)
  {
    if (m_bPauseOnClockTooFast)
      return ;                  // Do not re-enter !
    if (GetCurrentThreadId()!=m_MPmainThreadID) 
      return ;                  // Only MPmain can do that !
    if (((m_MediaPos/10000)-m_absSeekTime.Millisecs()) < 30*1000)
    {
      return ;                  
    }

    m_bPauseOnClockTooFast=true ;
    if (State() == State_Running)
    {
      IMediaControl * ptrMediaCtrl;
      if (SUCCEEDED(GetFilterGraph()->QueryInterface(IID_IMediaControl, (void**)&ptrMediaCtrl)))
      {
        LogDebug("Pause 200mS renderer clock to match provider/RTSP clock...") ; 
        ptrMediaCtrl->Pause() ;
        Sleep(200) ;
//        m_TestTime = GetTickCount() ;
        ptrMediaCtrl->Run() ;
        m_bRenderingClockTooFast=false ;
      }
      else
        LogDebug("Pause failed...") ; 
    }
    m_bPauseOnClockTooFast=false ;
  }
}
Пример #17
0
void PlaybackControls::PlayPause()
{
	IMediaControl *pControl = NULL;
	FILTER_STATE fs;
	HRESULT hr;
	
	if (mpGraph)
	{
		hr = mpGraph->QueryInterface(IID_IMediaControl, (void **)&pControl);
		pControl->GetState(50, (OAFilterState*)&fs);

		if (fs == State_Running)
			pControl->Pause();
		else
			pControl->Run();

		pControl->Release();
	}
}
bool DirectShowVideoWrapper::isPaused(void) const
{
    if(isInitialized())
    {
		HRESULT hr;

		IMediaControl* mediaControl;
		hr = _pGraphBuilder->QueryInterface(IID_IMediaControl,(void**)&mediaControl);

		OAFilterState GraphState;
		if (SUCCEEDED(mediaControl->GetState(0.1f, &GraphState)))
        {
			return GraphState == State_Paused;
		}
        else
        {
			return false;
		}
    }
    return false;
}
Пример #19
0
void PlaybackControls::Stop()
{
	mpParentWindow->id3Art->clear();
	mpParentWindow->artistLabel->clear();
	mpParentWindow->albumLabel->clear();
	mpParentWindow->titleLabel->clear();
		
	IMediaControl *pControl = NULL;
	HRESULT hr;
	
	if (mpGraph)
	{
		hr = mpGraph->QueryInterface(IID_IMediaControl, (void **)&pControl);
		pControl->Stop();
		EmptyGraph();
		pControl->Release();
		mpGraph->Release();
		mpGraph = NULL;
	}
	mGraphValid = false;
	needsReset = true;
}
Пример #20
0
bool CHogVideo::Hog()
{
    if (!m_pGraph && !m_pMediaControl && !m_pVideoWindow)
        return false;

    this->UnHog();

    HRESULT hr = NULL;
    IGraphBuilder *pGraph           = (IGraphBuilder*)  m_pGraph;
    IMediaControl *pMediaControl    = (IMediaControl*)  m_pMediaControl;
    IVideoWindow  *pVideoWindow     = (IVideoWindow*)   m_pVideoWindow;

    // Build the graph. IMPORTANT: Change string to a file on your system.
    hr = pGraph->RenderFile(m_wszFilename, NULL);
    if (SUCCEEDED(hr))
    {
        // Run the graph.
        hr = pMediaControl->Run();
        // Hide the window
        pVideoWindow->put_WindowState(SW_HIDE);
        pVideoWindow->put_AutoShow(OAFALSE);
        pVideoWindow->put_Visible(OAFALSE);
        pVideoWindow->put_Top(-100);
        pVideoWindow->put_Left(-100);
        pVideoWindow->put_Width(0);
        pVideoWindow->put_Height(0);

        if (SUCCEEDED(hr))
        {
            // Hog the resource.
            pMediaControl->Pause();
            return true;
        }
        else
            return false;
    }
    else
        return false;
}
Пример #21
0
void CHogVideo::Cleanup()
{
    this->UnHog();

    if (m_pEvent)
    {
        IMediaEvent *pEvent = (IMediaEvent*)m_pEvent;
        pEvent->Release();
        m_pEvent = NULL;
    }

    if (m_pVideoWindow)
    {
        IVideoWindow *pVideoWindow = (IVideoWindow*)m_pVideoWindow;
        pVideoWindow->Release();
        m_pVideoWindow = NULL;
    }

    if (m_pMediaControl)
    {
        IMediaControl *pMediaControl = (IMediaControl*)m_pMediaControl;
        pMediaControl->Release();
        m_pMediaControl = NULL;
    }

    if (m_pGraph)
    {
        IGraphBuilder *pGraph = (IGraphBuilder*)m_pGraph;
        pGraph->Release();
        m_pGraph = NULL;
    }

    if (m_bCOMInitialized)
    {
        CoUninitialize();
        m_bCOMInitialized = false;
    }
}
Пример #22
0
HRESULT CDShowControl::CreateWmvPlayGraph(HWND hWnd, CString fileName)
{
	IMediaControl *pControl = NULL;
	IMediaEvent   *pEvent = NULL;

	HRESULT hr = S_OK;
	hr = CoCreateInstance(CLSID_FilterGraph, NULL, CLSCTX_INPROC_SERVER,
		IID_IGraphBuilder, (void **)&m_moviePlayGraph);

	hr = m_moviePlayGraph->QueryInterface(IID_IMediaControl, (void **)&pControl);
	hr = m_moviePlayGraph->QueryInterface(IID_IMediaEvent, (void **)&pEvent);

	hr = pControl->RenderFile(fileName.AllocSysString());
	//    hr = pControl->Run();

	CComQIPtr<IVideoWindow>pVW(m_moviePlayGraph);
	if (pVW != NULL)
	{
		CComQIPtr<IBasicVideo> pBV(m_moviePlayGraph);
		CRect rect,videoRect;
		RETURNIF(pVW->put_Owner((OAHWND)hWnd));
		RETURNIF(pVW->put_WindowStyle(WS_CHILD | WS_VISIBLE | WS_CLIPCHILDREN | WS_CLIPSIBLINGS));
		RETURNIF(pVW->put_MessageDrain((long)hWnd));

		CRect ClientRect;
		::GetClientRect(hWnd, &ClientRect);
		pVW->SetWindowPosition(0, 0, ClientRect.Width(), ClientRect.Height());
		pVW->put_AutoShow(OATRUE);
		pVW->put_Visible(OATRUE);
	}

	hr = pControl->Run();

	pControl->Release();
	pEvent->Release();

	return S_OK;
}
Пример #23
0
bool CaptureAndSend::stop()
{
	if (m_CurrentState == Stopped)
	{
		return true;
	}

	if(!m_NetSenderFilter->stop())
	{
		return false;
	}

	g_StartCount--;
	if ( g_StartCount == 0)
	{
		HRESULT hr;
		IMediaControl* mediaControl;
		hr = m_FilterGraph->QueryInterface(IID_IMediaControl, (void**)&mediaControl);
		if(FAILED(hr))
		{
			ErrorPrint("Get media control error", hr);
			return false;
		}
		ComReleaser mediaControlReleaser(mediaControl);
		hr = mediaControl->Stop();
		if(FAILED(hr))
		{
			ErrorPrint("Stop error",hr);
			return false;
		}
	}

	m_CurrentState = Stopped;

	return true;
}
Пример #24
0
//
//  Handles DirectShow graph events.
//
//  Returns true if the player should be stopped.
//
bool CMediaPlayer::HandleGraphEvent()
{
    bool stopped = false;
    // Disregard if we don't have an IMediaEventEx pointer.
    if (_MediaEvent== NULL)
    {
        return stopped;
    }

    // Get all the events
    long evCode;
    LONG_PTR param1, param2;
    while (SUCCEEDED(_MediaEvent->GetEvent(&evCode, &param1, &param2, 0)))
    {
        _MediaEvent->FreeEventParams(evCode, param1, param2);
        switch (evCode)
        {
        case EC_COMPLETE:
        {
			if(!_Repeat){
				_Playing = false;
				// Stop playback, we're done.
				{
					IMediaControl *mediaControl;
					_GraphBuilder->QueryInterface(IID_PPV_ARGS(&mediaControl));

					mediaControl->Stop();
					mediaControl->Release();
				}

				REFERENCE_TIME timeBegin = 0;
				_MediaSeeking->SetPositions(&timeBegin, AM_SEEKING_AbsolutePositioning, NULL, AM_SEEKING_NoPositioning);
				stopped = true;
			}else{
				// Repeat
				REFERENCE_TIME timeBegin = 0;
				_MediaSeeking->SetPositions(&timeBegin, AM_SEEKING_AbsolutePositioning, NULL, AM_SEEKING_NoPositioning);

				IMediaControl *mediaControl;
				_GraphBuilder->QueryInterface(IID_PPV_ARGS(&mediaControl));
				mediaControl->Run();
				mediaControl->Release();
			}
        }
        break;
        case EC_USERABORT: // Fall through.
        case EC_ERRORABORT:
            stopped = false;
        }
    }
    return stopped;
}
void DirectShowVideoWrapper::uninitVideo()
{
    _VideoInitialized = false;


    //Stop the video if need be
    HRESULT hr;
    if (_VideoInitialized &&
        _pGraphBuilder)
    {
        //_pSampleGrabberFilter->Release();
        IMediaControl* mediaControl;
        hr = _pGraphBuilder->QueryInterface(IID_IMediaControl,
                                            (void**)&mediaControl);
        mediaControl->Stop();
    }

    //Clear all reference counted pointers
    if(_pGraphBuilder)
    {
        _pGraphBuilder.Release();
        _pGraphBuilder.Detach();
    }
    if(_pGraphCaptureBuilder)
    {
        _pGraphCaptureBuilder.Release();
        _pGraphCaptureBuilder.Detach();
    }
    if(_pMediaControl)
    {
        _pMediaControl.Release();
        _pMediaControl.Detach();
    }
    if(_pMediaPosition)
    {
        _pMediaPosition.Release();
        _pMediaPosition.Detach();
    }
    if(_pMediaEvent)
    {
        _pMediaEvent.Release();
        _pMediaEvent.Detach();
    }
    if(g_IFileSource)
    {
        g_IFileSource.Release();
        g_IFileSource.Detach();
    }
    if(_pSourceFilter)
    {
        _pSourceFilter.Release();
        _pSourceFilter.Detach();
    }
    if(_pSourceAudioPin)
    {
        _pSourceAudioPin.Release();
        _pSourceAudioPin.Detach();
    }
    if(_pSourceVideoPin)
    {
        _pSourceVideoPin.Release();
        _pSourceVideoPin.Detach();
    }
    if(_pDecoderFilter)
    {
        _pDecoderFilter.Release();
        _pDecoderFilter.Detach();
    }
    if(_pVideoRenderer)
    {
        _pVideoRenderer.Release();
        _pVideoRenderer.Detach();
    }
    if(_DecoderOutputPin)
    {
        _DecoderOutputPin.Release();
        _DecoderOutputPin.Detach();
    }
    if(_pNullAudioFilter)
    {
        _pNullAudioFilter.Release();
        _pNullAudioFilter.Detach();
    }
    if(_pAudioRenderer)
    {
        _pAudioRenderer.Release();
        _pAudioRenderer.Detach();
    }
    if(_pSampleGrabberFilter)
    {
        _pSampleGrabberFilter.Release();
        _pSampleGrabberFilter.Detach();
    }
    if(_pCSCFilter)
    {
        _pCSCFilter.Release();
        _pCSCFilter.Detach();
    }
    if(_SampleGrabberIntputPin)
    {
        _SampleGrabberIntputPin.Release();
        _SampleGrabberIntputPin.Detach();
    }
    if(_SampleGrabberOutputPin)
    {
        _SampleGrabberOutputPin.Release();
        _SampleGrabberOutputPin.Detach();
    }

    //deallocate framebuffer
    if(_FrameBuffer != NULL)
    {
        delete[] _FrameBuffer;
        _FrameBuffer = NULL;
    }

    //Set image to NULL
    setImage(NULL);
}
int _tmain(int argc, _TCHAR* argv[])
{
	IGraphBuilder *pGraph = NULL;
    IMediaControl *pControl = NULL;
    IMediaEvent   *pEvent = NULL; 
	//Get some param--------------
	HRESULT hr1;
	IBasicVideo *pVideo=NULL;
	IBasicAudio *pAudio=NULL;
	IVideoWindow *pWindow=NULL;
	IMediaSeeking *pSeeking=NULL;
	
	
    // Init COM
    HRESULT hr = CoInitialize(NULL);
    if (FAILED(hr)){
        printf("Error - Can't init COM.");
        return -1;
    }

	// Create FilterGraph
   hr=CoCreateInstance(CLSID_FilterGraph, NULL,CLSCTX_INPROC_SERVER,IID_IGraphBuilder, (void **)&pGraph);
    if (FAILED(hr)){
        printf("Error - Can't create Filter Graph.");
        return -1;
    }
   //  Query Interface
    hr = pGraph->QueryInterface(IID_IMediaControl, (void **)&pControl);
    hr = pGraph->QueryInterface(IID_IMediaEvent, (void **)&pEvent);
	// RenderFile
    hr = pGraph->RenderFile(L"cuc_ieschool.mov", NULL);
	if (FAILED(hr)){
		printf("Error - Can't Render File.");
		return -1;
	}
#if OUTPUT_INFO
	//Get some information----------
	long video_w=0,video_h=0,video_bitrate=0,audio_volume=0;
	long long duration_1=0,position_1=0;
	REFTIME avgtimeperframe=0;
	float framerate=0,duration_sec=0,progress=0,position_sec=0;
	//Video
	hr1=pGraph->QueryInterface(IID_IBasicVideo, (void **)&pVideo);
	pVideo->get_VideoWidth(&video_w);
	pVideo->get_VideoHeight(&video_h);
	pVideo->get_AvgTimePerFrame(&avgtimeperframe);
	framerate=1/avgtimeperframe;
	//pVideo->get_BitRate(&video_bitrate);
	//Audio
	hr1=pGraph->QueryInterface(IID_IBasicAudio, (void **)&pAudio);
	//Mute
	//pAudio->put_Volume(-10000);
	printf("Some Information:\n");
	printf("Video Resolution:\t%dx%d\n",video_w,video_h);
	printf("Video Framerate:\t%.3f\n",framerate);
	//Window
	hr1=pGraph->QueryInterface(IID_IVideoWindow, (void **)&pWindow);
	pWindow->put_Caption(L"Simplest DirectShow Player");
	//pWindow->put_Width(480);
	//pWindow->put_Height(272);
	//Seek
	hr1=pGraph->QueryInterface(IID_IMediaSeeking, (void **)&pSeeking);
	pSeeking->GetDuration(&duration_1);
	//time unit:100ns=0.0000001s
	duration_sec=(float)duration_1/10000000.0;
	printf("Duration:\t%.2f s\n",duration_sec);
	//pSeeking->SetPositions();
	//PlayBack Rate
	//pSeeking->SetRate(2.0);

	//Show Filter in FilterGpagh
	show_filters_in_filtergraph(pGraph);
	//----------------------
#endif

	printf("Progress Info\n");
	printf("Position\tProgress\n");
    if (SUCCEEDED(hr)){
        // Run
        hr = pControl->Run();
        if (SUCCEEDED(hr)){
			long evCode=0;
			//pEvent->WaitForCompletion(INFINITE, &evCode);
			while(evCode!=EC_COMPLETE){
				//Info
#if OUTPUT_INFO
				pSeeking->GetCurrentPosition(&position_1);
				position_sec=(float)position_1/10000000.0;
				progress=position_sec*100/duration_sec;
				printf("%7.2fs\t%5.2f%%\n",position_sec,progress);
#endif
				//1000ms
				pEvent->WaitForCompletion(1000, &evCode);
			}
        }
    }
	// Release resource
    pControl->Release();
    pEvent->Release();
    pGraph->Release();
    CoUninitialize();
	return 0;
}
Пример #27
0
int main(int argc, char* argv[])
{
    ICaptureGraphBuilder2   *pCaptureGraphBuilder = NULL;
    IGraphBuilder           *pGraphBuilder = NULL;
    IBaseFilter             *pSource = NULL;
    IBaseFilter             *pMux = NULL;
    IBaseFilter             *pVideoCompressor = NULL;
    IBaseFilter             *pAudioCompressor = NULL;

    IAMStreamConfig         *pAMStreamConfig = NULL;
    IAMVideoCompression     *pAMVideoCompression = NULL;

    IMediaControl           *pControl = NULL;
    IMediaSeeking           *pSeek = NULL;
    IMediaEvent             *pEvent = NULL;

    HRESULT hr;

    DWORD pdwRegister=0;
    CoInitialize(NULL);

    // Create the capture graph builder.
    CoCreateInstance(CLSID_CaptureGraphBuilder2, NULL, CLSCTX_INPROC,
                     IID_ICaptureGraphBuilder2, (void **)&pCaptureGraphBuilder);

    // Make the rendering section of the graph.
    pCaptureGraphBuilder->SetOutputFileName(
        &MEDIASUBTYPE_Avi,  // File type.
        L"C:\\STDIUE1.avi",  // File name.
        &pMux,              // pointer to the multiplexer.
        NULL);              // pointer to the file writer.

    // Load the source file.
    pCaptureGraphBuilder->GetFiltergraph(&pGraphBuilder);
    pGraphBuilder->AddSourceFilter(L"C:\\Program Files\\Microsoft Money\\Media\\STDIUE1.avi", L"Source Filter", &pSource);

    // Add the compressor filter.
    CoCreateInstance(CLSID_AVICo, NULL, CLSCTX_INPROC,
                     IID_IBaseFilter, (void **)&pVideoCompressor);
    pGraphBuilder->AddFilter(pVideoCompressor, L"Video Compressor");

    // Render the video stream, through the compressor.
    pCaptureGraphBuilder->RenderStream(
        NULL,       // Output pin category
        NULL,       // Media type
        pSource,       // Source filter
        pVideoCompressor,     // Compressor filter
        pMux);      // Sink filter (the AVI Mux)

    /* CoCreateInstance(CLSID_GSM, NULL, CLSCTX_INPROC,
             IID_IBaseFilter, (void **)&pAudioCompressor);
     pGraphBuilder->AddFilter(pAudioCompressor, L"Audio Compressor");*/

    // Render the audio stream.
    pCaptureGraphBuilder->RenderStream(
        NULL,
        NULL,
        pSource,
        pAudioCompressor,
        pMux);

    // Compress at 100k/second data rate.
    AM_MEDIA_TYPE *pmt;
    pCaptureGraphBuilder->FindInterface(NULL, NULL, pVideoCompressor, IID_IAMStreamConfig, (void **)&pAMStreamConfig);

    pAMStreamConfig->GetFormat(&pmt);

    if (pmt->formattype == FORMAT_VideoInfo)
    {

        ((VIDEOINFOHEADER *)(pmt->pbFormat))->dwBitRate = 100000;

        pAMStreamConfig->SetFormat(pmt);
    }


    // Request key frames every four frames.
    pAMStreamConfig->QueryInterface(IID_IAMVideoCompression, (void **)&pAMVideoCompression);
    pAMVideoCompression->put_KeyFrameRate(4);
    pAMVideoCompression->Release();
    pAMStreamConfig->Release();

    // Run the graph.

    pGraphBuilder->QueryInterface(IID_IMediaControl, (void **)&pControl);
    pGraphBuilder->QueryInterface(IID_IMediaEvent, (void **)&pEvent);

    hr = pMux->QueryInterface(IID_IMediaSeeking, (void**)&pSeek);


    pControl->Run();
    printf("Recompressing... \n");

    long evCode;
    if (SUCCEEDED(hr))
    {
        REFERENCE_TIME rtTotal, rtNow = 0;
        pSeek->GetDuration(&rtTotal);
        while ((pEvent->WaitForCompletion(1000, &evCode)) == E_ABORT)
        {
            pSeek->GetCurrentPosition(&rtNow);
            printf("%d%%\n", (rtNow * 100)/rtTotal);
        }
        pSeek->Release();
    }
    else  // Cannot update the progress.
    {
        pEvent->WaitForCompletion(INFINITE, &evCode);
    }
    pControl->Stop();
    printf("All done\n");

    pSource->Release();
    pMux->Release();
    pVideoCompressor->Release();
    pAudioCompressor->Release ();
    pControl->Release();
    pEvent->Release();
    pCaptureGraphBuilder->Release();
    pGraphBuilder->Release();
    CoUninitialize();

    return 0;
}
Пример #28
0
bool CaptureDShow::init()
{
    // Create the pipeline.
    if (FAILED(CoCreateInstance(CLSID_FilterGraph,
                                NULL,
                                CLSCTX_INPROC_SERVER,
                                IID_IGraphBuilder,
                                reinterpret_cast<void **>(&this->m_graph))))
        return false;

    // Create the webcam filter.
    IBaseFilter *webcamFilter = this->findFilterP(this->m_device);

    if (!webcamFilter) {
        this->m_graph->Release();
        this->m_graph = NULL;

        return false;
    }

    if (FAILED(this->m_graph->AddFilter(webcamFilter, SOURCE_FILTER_NAME))) {
        this->m_graph->Release();
        this->m_graph = NULL;

        return false;
    }

    // Create the Sample Grabber filter.
    IBaseFilter *grabberFilter = NULL;

    if (FAILED(CoCreateInstance(CLSID_SampleGrabber,
                                NULL,
                                CLSCTX_INPROC_SERVER,
                                IID_IBaseFilter,
                                reinterpret_cast<void **>(&grabberFilter)))) {
        this->m_graph->Release();
        this->m_graph = NULL;

        return false;
    }

    if (FAILED(this->m_graph->AddFilter(grabberFilter, L"Grabber"))) {
        this->m_graph->Release();
        this->m_graph = NULL;

        return false;
    }

    ISampleGrabber *grabberPtr = NULL;

    if (FAILED(grabberFilter->QueryInterface(IID_ISampleGrabber,
                                             reinterpret_cast<void **>(&grabberPtr)))) {
        this->m_graph->Release();
        this->m_graph = NULL;

        return false;
    }

    if (FAILED(grabberPtr->SetOneShot(FALSE))) {
        this->m_graph->Release();
        this->m_graph = NULL;

        return false;
    }

    HRESULT hr = grabberPtr->SetBufferSamples(TRUE);

    if (FAILED(hr)) {
        this->m_graph->Release();
        this->m_graph = NULL;

        return false;
    }

    if (this->m_ioMethod != IoMethodDirectRead) {
        int type = this->m_ioMethod == IoMethodGrabSample? 0: 1;
        hr = grabberPtr->SetCallback(&this->m_frameGrabber, type);
    }

    this->m_grabber = SampleGrabberPtr(grabberPtr, this->deleteUnknown);

    if (!this->connectFilters(this->m_graph, webcamFilter, grabberFilter)) {
        this->m_graph->Release();
        this->m_graph = NULL;

        return false;
    }

    // Create null filter.
    IBaseFilter *nullFilter = NULL;

    if (FAILED(CoCreateInstance(CLSID_NullRenderer,
                                NULL,
                                CLSCTX_INPROC_SERVER,
                                IID_IBaseFilter,
                                reinterpret_cast<void **>(&nullFilter)))) {
        this->m_graph->Release();
        this->m_graph = NULL;

        return false;
    }

    if (FAILED(this->m_graph->AddFilter(nullFilter, L"NullFilter"))) {
        this->m_graph->Release();
        this->m_graph = NULL;

        return false;
    }

    if (!this->connectFilters(this->m_graph, grabberFilter, nullFilter)) {
        this->m_graph->Release();
        this->m_graph = NULL;

        return false;
    }

    // Set capture format
    QList<int> streams = this->streams();

    if (streams.isEmpty()) {
        this->m_graph->Release();
        this->m_graph = NULL;

        return false;
    }

    MediaTypesList mediaTypes = this->listMediaTypes(webcamFilter);

    if (mediaTypes.isEmpty()) {
        this->m_graph->Release();
        this->m_graph = NULL;

        return false;
    }

    MediaTypePtr mediaType = streams[0] < mediaTypes.size()?
                                mediaTypes[streams[0]]:
                                mediaTypes.first();

    if (FAILED(grabberPtr->SetMediaType(mediaType.data()))) {
        this->m_graph->Release();
        this->m_graph = NULL;

        return false;
    }

    PinList pins = this->enumPins(webcamFilter, PINDIR_OUTPUT);

    for (const PinPtr &pin: pins) {
        IAMStreamConfig *pStreamConfig = NULL;
        HRESULT hr =
                pin->QueryInterface(IID_IAMStreamConfig,
                                    reinterpret_cast<void **>(&pStreamConfig));

        if (SUCCEEDED(hr))
            pStreamConfig->SetFormat(mediaType.data());

        if (pStreamConfig)
            pStreamConfig->Release();
    }

    // Run the pipeline
    IMediaControl *control = NULL;

    if (FAILED(this->m_graph->QueryInterface(IID_IMediaControl,
                                             reinterpret_cast<void **>(&control)))) {
        this->m_graph->Release();
        this->m_graph = NULL;

        return false;
    }

    this->m_id = Ak::id();
    AkCaps caps = this->capsFromMediaType(mediaType);
    this->m_timeBase = AkFrac(caps.property("fps").toString()).invert();

    if (FAILED(control->Run())) {
        control->Release();
        this->m_graph->Release();
        this->m_graph = NULL;

        return false;
    }

    control->Release();

    this->m_localImageControls.clear();
    this->m_localImageControls.clear();

    return true;
}
Пример #29
0
int JukeboxDS::player()
{
    if( FAILED(CoInitialize(NULL)) )
		return -1;

	IGraphBuilder *	pGB = NULL;
	IMediaControl *	pMC = NULL;
	IBasicAudio *	pBA = NULL;
	IMediaEvent *	pME = NULL;

	while( m_Active )
	{
		// check the state of the music, if stopped move onto the next file
		if ( pME != NULL )
		{
			long eventCode;
			if ( pME->WaitForCompletion( 0, &eventCode ) == S_OK )
				nextTrack();		// current song has ended, next track
		}

		// check for a volume change
		if ( m_UpdateVolume.signaled() )
		{
			m_UpdateVolume.clear();
			if ( pBA != NULL )
			{
				// set the volume
				long pv = (100 - m_Volume) * -100;
				pBA->put_Volume( pv );
			}
		}

		if (! m_TrackEvent.wait( 250 ) )
		{
			AutoLock lock( &m_Lock );

			if ( m_PlayLists.isValid( m_ActiveList ) && m_PlayLists[ m_ActiveList ].files.isValid( m_CurrentTrack ) )
			{
				// get the filename of the current track
				CharString file = m_PlayLists[ m_ActiveList ].files[ m_CurrentTrack ];

				// release the previous interfaces
				RELEASEQI( pGB );
				RELEASEQI( pMC );
				RELEASEQI( pBA );
				RELEASEQI( pME );

				// Create DirectShow Graph
				if ( FAILED( CoCreateInstance(CLSID_FilterGraph, NULL, CLSCTX_INPROC, IID_IGraphBuilder, (void **)&pGB) ) )
					return -1;
				try {
					// load the first file
					if ( FAILED( pGB->RenderFile( String( file ), NULL ) ) )
						return -1;
				}
				catch( ... )
				{
					// driver failure, don't crash the game due to a bad codec..
					return -1;
				}
				// Get the IMediaControl Interface
				if ( FAILED( pGB->QueryInterface(IID_IMediaControl, (void **)&pMC ) ) )
					return -1;
				// Get the IBasicAudio interface
				if ( FAILED( pGB->QueryInterface(IID_IBasicAudio, (void **)&pBA) ) )
					return -1;
				// Get the IBasicAudio interface
				if ( FAILED( pGB->QueryInterface(IID_IMediaEvent, (void **)&pME) ) )
					return -1;
				// set the volume
				long pv = (100 - m_Volume) * -100;
				pBA->put_Volume( pv );
				// play the music
				pMC->Run();
			}
		}
	}

	RELEASEQI( pGB );
	RELEASEQI( pMC );
	RELEASEQI( pBA );
	RELEASEQI( pME );

	CoUninitialize();

	return 0;
}
Пример #30
0
void Video::play( char *fileName, DWORD )
{
	WCHAR wPath[100];
	HRESULT hr;
	IMediaControl *pMC;

	if(!init_success)
		return;

	MultiByteToWideChar( CP_ACP, 0, fileName, -1, wPath, 100 );

	if( (hr = pGraph->RenderFile(wPath, NULL)) == 0)
	{

		// use full screen video interface
		// try to change display mode
		IVideoWindow *iVideoWindow = NULL;
		if( (hr = pGraph->QueryInterface(IID_IVideoWindow, (void **) &iVideoWindow)) == 0)
		{
#ifdef CREATE_DUMMY_WINDOW
			if(hwnd)
			{
				HRESULT hr2 = iVideoWindow->put_MessageDrain((OAHWND) hwnd);
				hr2 = 0;
			}
#endif

#ifdef FULL_SCREEN_VIDEO
			IFilter *iFilter;
			if( pGraph->FindFilterByName(L"Video Renderer", &iFilter) == 0)
			{
				IBasicVideo *iBasicVideo;
				if( iFilter->QueryInterface(IID_IBasicVideo, (void **)&iBasicVideo) == 0)
				{
					IFullScreenVideo *iFullScreenVideo;
					IDirectDrawVideo *iDirectDrawVideo;
					if( iFilter->QueryInterface(IID_IFullScreenVideo, (void **)&iFullScreenVideo) == 0)
					{
						iFullScreenVideo->Release();
					}
					else if( iFilter->QueryInterface(IID_IDirectDrawVideo, (void **)&iDirectDrawVideo) == 0)
					{
						HRESULT hr2;
						hr2 = iDirectDrawVideo->UseWhenFullScreen(OATRUE);
						iDirectDrawVideo->Release();
					}

					iBasicVideo->Release();
				}
				iFilter->Release();
			}
			hr=iVideoWindow->put_FullScreenMode(OATRUE);
#endif

			/* // code to find all filter in the filter graph
			{
				IEnumFilters *iEnumFilters;
				pGraph->EnumFilters(&iEnumFilters);

				ULONG filterCount = 16;
				IFilter *iFilters[16];
				iEnumFilters->Next(filterCount, iFilters, &filterCount);

				for( ULONG j = 0; j < filterCount; ++j )
				{
					FILTER_INFO filterInfo;
					iFilters[j]->QueryFilterInfo(&filterInfo);
					filterInfo.pGraph->Release();
					iFilters[j]->Release();
				}

				iEnumFilters->Release();
			}*/

			iVideoWindow->HideCursor(OATRUE);
			iVideoWindow->put_Visible( OAFALSE );
			iVideoWindow->put_AutoShow( OAFALSE );
			LONG windowStyle;
			iVideoWindow->get_WindowStyle( &windowStyle);
			windowStyle &= ~WS_BORDER & ~WS_CAPTION & ~WS_SIZEBOX & ~WS_THICKFRAME &
				~WS_HSCROLL & ~WS_VSCROLL & ~WS_VISIBLE;
			iVideoWindow->put_WindowStyle( windowStyle);
		}
		else
			iVideoWindow = NULL;
		
		if( (hr = pGraph->QueryInterface(IID_IMediaControl, (void **) &pMC)) == 0)
		{
			pMC->Run();					// sometimes it returns 1, but still ok
			state = PLAYING;
			pMC->Release();
		}

		if( iVideoWindow )
		{
			iVideoWindow->put_Visible( OAFALSE );
			LONG windowStyle;
			iVideoWindow->get_WindowStyle( &windowStyle);
			windowStyle &= ~WS_BORDER & ~WS_CAPTION & ~WS_SIZEBOX & ~WS_THICKFRAME &
				~WS_HSCROLL & ~WS_VSCROLL & ~WS_VISIBLE;
			iVideoWindow->put_WindowStyle( windowStyle);

			LONG maxWidth;
			LONG maxHeight;
			hr=iVideoWindow->GetMaxIdealImageSize( &maxWidth, &maxHeight);
#ifdef FULL_SCREEN_VIDEO
#else
			iVideoWindow->put_BorderColor( RGB(0,0,0) );
			iVideoWindow->put_WindowState(SW_MAXIMIZE);

			IBaseFilter *iFilter;
			if( pGraph->FindFilterByName((const WCHAR *)L"Video Renderer", &iFilter) == 0)
			{
				IBasicVideo *iBasicVideo;
				if( iFilter->QueryInterface(IID_IBasicVideo, (void **)&iBasicVideo) == 0)
				{
					LONG screenWidth;
					LONG screenHeight;
					LONG videoWidth;
					LONG videoHeight;
					if( iVideoWindow->get_Width(&screenWidth) == 0 &&
						iVideoWindow->get_Height(&screenHeight) == 0 &&
						iBasicVideo->GetVideoSize(&videoWidth, &videoHeight) == 0)
					{
						// zoom in by 2 if possible
						if( screenWidth >= videoWidth * 2 &&
							screenHeight >= videoHeight * 2)
						{
							videoWidth *= 2;
							videoHeight *= 2;
						}

						// center the video client area
						iBasicVideo->SetDestinationPosition(
							(screenWidth-videoWidth)/2, (screenHeight-videoHeight)/2,
							videoWidth, videoHeight);
					}

					iBasicVideo->Release();
				}
				iFilter->Release();
			}
#endif
			iVideoWindow->HideCursor(OATRUE);
			iVideoWindow->SetWindowForeground(OATRUE);
		}

		if(iVideoWindow)
		{
			iVideoWindow->Release();
			iVideoWindow = NULL;
		}
	}

	if( hr && !skip_on_fail_flag)
		err.run("video.play error %d", hr );
}