Click here to Skip to main content
14,690,515 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello~~
Is there anyone watch the World Cup 2010??
I'm Korean so really really happy that Korean Team has gone to the Round of 16!

Wow~!!

Anyway...

There is some Questions about DirectShow!

I've made the program which could extract part of AVI from AVI file using timeline~!

BUT!!
When I run this program at [Start Debug(F5)] mode ,
The program stopped and finish debugging at below Code with out any alert.


WaitForCompletion(INFINITE, EventCode);

On the other hand, [Start with out debugging(CTRL + F5)](I use VS2008 Korean Version, so I don't know exact word) mode do very well~!

I was wondering if you could explain what the problem is.

In Addtion, the result avi file is strange.

When I play the result avi file, there's a lot of noise which color is green!

Thank you for reading and have a nice century!


-- CODE --

CoInitializeEx(NULL, COINIT_APARTMENTTHREADED);
 	WCHAR * wszVideo1Name = L"D:\\test1.avi\0";
 	WCHAR * wszVideo2Name = L"D:\\test2.avi\0";
	
	WCHAR* wszTitle = L"Timeline Test Sample\0";
	HRESULT hr;

	CComPtr< IRenderEngine >  pRenderEngine;
	CComPtr< IGraphBuilder >  pGraph;
	CComPtr< IVideoWindow >   pVidWindow;
	CComPtr< IMediaEvent >    pEvent;
	CComPtr< IAMTimeline >    pTimeline;
	CComPtr< IAMTimelineObj > pVideoGroupObj;
	//CComPtr< IAMTimelineObj > pAudioGroupObj;
	CComPtr< IMediaControl >  pControl;
	CComPtr< IMediaSeeking >  pSeeking; 


	hr = CoCreateInstance(
		CLSID_AMTimeline, 
		NULL, 
		CLSCTX_INPROC_SERVER, 
		IID_IAMTimeline, 
		(void**) &pTimeline 
		);
	if(FAILED( hr )) {
		Err(_T("Error"));
		return hr;
	}

	hr = pTimeline->CreateEmptyNode( &pVideoGroupObj, TIMELINE_MAJOR_TYPE_GROUP );
	if(FAILED( hr )) 
	{
		Err(_T("Error"));
		return hr;
	}

	CComQIPtr< IAMTimelineGroup, &IID_IAMTimelineGroup > pVideoGroup  
		( pVideoGroupObj );
	CMediaType VideoGroupType;

	// all we set is the major type. The group will automatically use other defaults
	VideoGroupType.SetType( &MEDIATYPE_Video );
	hr = pVideoGroup->SetMediaType( &VideoGroupType );
	if(FAILED( hr )) 
	{
		Err(_T("Error"));
		return hr;
	}

	hr = pTimeline->AddGroup( pVideoGroupObj );
	if(FAILED( hr )) 
	{
		Err(_T("Error"));
		return hr;
	}



	CComPtr< IAMTimelineObj > pTrack1Obj;
	hr = pTimeline->CreateEmptyNode( &pTrack1Obj, TIMELINE_MAJOR_TYPE_TRACK );
	if(FAILED( hr )) 
	{
		Err(_T("Error"));
		return hr;
	} 


	CComQIPtr< IAMTimelineComp, &IID_IAMTimelineComp > pRootComp( pVideoGroupObj );
	hr = pRootComp->VTrackInsBefore( pTrack1Obj, -1 );
	if(FAILED( hr )) 
	{
		Err(_T("Could not insert track"));
		return hr;
	}


	REFERENCE_TIME TLStart = 0 * UNITS;
	REFERENCE_TIME TLStop = 8 * UNITS;

	REFERENCE_TIME MediaStart = 0 * UNITS;
	REFERENCE_TIME MediaStop = 8 * UNITS;

	WCHAR pClipname[_MAX_PATH+_MAX_PATH+2];
	wcscpy( pClipname, wszVideo1Name );


	CComPtr<IAMTimelineObj> pSource1Obj;
	hr = pTimeline->CreateEmptyNode( &pSource1Obj, TIMELINE_MAJOR_TYPE_SOURCE );
	if(FAILED( hr )) 
	{
		Err(_T("Error"));
		return hr;
	}


	hr = pSource1Obj->SetStartStop( TLStart, TLStop );
	CComQIPtr< IAMTimelineSrc, &IID_IAMTimelineSrc > pSource1Src( pSource1Obj );
	hr |= pSource1Src->SetMediaTimes( MediaStart, MediaStop );
	hr |= pSource1Src->SetMediaName( pClipname );
	if(FAILED( hr )) 
	{
		Err(_T("Could not configure media source"));
		return E_FAIL;
	}

	CComQIPtr< IAMTimelineTrack, &IID_IAMTimelineTrack > pTrack1( pTrack1Obj );
	hr = pTrack1->SrcAdd( pSource1Obj );
	if(FAILED( hr )) 
	{
		Err(_T("Error"));
		return hr;
	} 
	
	CComPtr< IAMTimelineObj > pTrack2Obj;
	hr = pTimeline->CreateEmptyNode( &pTrack2Obj, TIMELINE_MAJOR_TYPE_TRACK );
	if(FAILED( hr )) 
	{
		Err(_T("Error"));
		return hr;
	}


	hr = pRootComp->VTrackInsBefore( pTrack2Obj, -1 );
	if(FAILED( hr )) 
	{
		Err(_T("Error"));
		return hr;
	}


	TLStart = 8 * UNITS;
	TLStop = 16 * UNITS;
	MediaStart = 8 * UNITS;
	MediaStop = 16 * UNITS;
	wcscpy( pClipname, wszVideo2Name );

	CComPtr<IAMTimelineObj> pSource2Obj;
	hr = pTimeline->CreateEmptyNode( &pSource2Obj, TIMELINE_MAJOR_TYPE_SOURCE );
	if(FAILED( hr )) 
	{
		Err(_T("Error"));
		return hr;
	} 


	hr = pSource2Obj->SetStartStop( TLStart, TLStop );
	CComQIPtr< IAMTimelineSrc, &IID_IAMTimelineSrc > pSource2Src( pSource2Obj );

	hr |= pSource2Src->SetMediaTimes( MediaStart, MediaStop );
	hr |= pSource2Src->SetMediaName( pClipname );
	if(FAILED( hr )) 
	{
		Err(_T("Error"));
		return E_FAIL;
	}


	CComQIPtr< IAMTimelineTrack, &IID_IAMTimelineTrack > pTrack2( pTrack2Obj );
	hr = pTrack2->SrcAdd( pSource2Obj );
	if(FAILED( hr )) 
	{
		Err(_T("Could not add second track"));
		return hr;
	}


	hr = pTimeline->ValidateSourceNames( 
		SFN_VALIDATEF_CHECK | SFN_VALIDATEF_POPUP | SFN_VALIDATEF_REPLACE, 
		NULL, 
		0 );
	ASSERT( !FAILED( hr ) );


	hr = CoCreateInstance(
		CLSID_RenderEngine,
		NULL,
		CLSCTX_INPROC_SERVER,
		IID_IRenderEngine,
		(void**) &pRenderEngine );
	ASSERT( !FAILED( hr ) );

	hr = pRenderEngine->SetTimelineObject( pTimeline );
	ASSERT( !FAILED( hr ) );


	hr = pRenderEngine->ConnectFrontEnd( ); 
	//hr = pRenderEngine->RenderOutputPins();
	ASSERT( !FAILED( hr ) );

	ICaptureGraphBuilder2 *pBuilder = NULL;
	hr = CoCreateInstance(CLSID_CaptureGraphBuilder2, NULL, CLSCTX_INPROC_SERVER,  
		IID_ICaptureGraphBuilder2, (void**)&pBuilder);
	if (FAILED(hr)) {
		Err(_T("Error"));
	}

	hr = pRenderEngine->GetFilterGraph(&pGraph);
	hr = pGraph->QueryInterface(IID_IMediaControl, (void**) &pControl);
	hr = pGraph->QueryInterface( IID_IVideoWindow, (void**) &pVidWindow );
	hr = pGraph->QueryInterface( IID_IMediaEvent, (void**) &pEvent );
	hr = pBuilder->SetFiltergraph(pGraph);
	
	//hr = pRenderEngine->RenderOutputPins();

	IBaseFilter *pMux;
	hr = pBuilder->SetOutputFileName(&MEDIASUBTYPE_Avi, OLESTR("c:\\out.avi"), 
		&pMux, NULL);
	if (FAILED(hr)) {
		Err(_T("Error!"));
	}

	long NumGroups;
	pTimeline->GetGroupCount(&NumGroups);


	int i;
	for (i = 0; i < NumGroups; i++) {
		IPin *pPin;
		if (pRenderEngine->GetGroupOutputPin(i, &pPin) == S_OK) {
			//IBaseFilter *pCompressor = NULL;


			pBuilder->RenderStream(NULL, NULL, pPin, NULL, pMux);
			//pCompressor->Release();
			pPin->Release();
		}
	}

	hr = pControl->Run();

	// Wait for Complete
	long EventCode = 0;



// HERE!!!!!!
	hr = pEvent->WaitForCompletion(-1, &EventCode);



	hr = pControl->Stop();

	Err(_T("Complete"));

	CoUninitialize();
	return S_OK;
Posted
Updated 23-Jun-10 22:16pm
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900