Creating ASF Files in DirectShow


<br/>HRESULT hr;
	IBaseFilter *pSrcFilter=NULL;
	IBaseFilter *pASFWriter = NULL; // WM ASF File filter
	IFileSinkFilter2 *pSink = NULL; // File sink object
	IWMWriterAdvanced2 *pWMWA2 = NULL;
	IServiceProvider *pProvider;
	IConfigAsfWriter *mpWriterConfig = NULL;

	// Get DirectShow interfaces
	hr = GetInterfaces();
	if (FAILED(hr))
	{
		Msg(TEXT("Failed to get video interfaces!  hr=0x%x"), hr);
		return hr;
	}
	hr = CoCreateInstance (CLSID_WMAsfWriter, NULL, CLSCTX_INPROC_SERVER, IID_IBaseFilter, (void **) &pASFWriter);   

	hr =  g_pGraph->AddFilter (pASFWriter, L"ASF Writer");   
	if (FAILED(hr))
	{
		Msg(TEXT("Failed to CLSID_WMAsfWriter AddFilter!  hr=0x%x"), hr);
		return hr;
	}

	if (FAILED(hr))
	{
		Msg(TEXT("Failed to CLSID_WMAsfWriter!  hr=0x%x"), hr);
		return hr;
	}

	hr = pASFWriter->QueryInterface(IID_IConfigAsfWriter, (void**)&mpWriterConfig);  
	if (FAILED(hr))
	{
		Msg(TEXT("Failed to ConfigureFilterUsingProfileGuid !  hr=0x%x"), hr);
		return hr;
	}	 
	IWMProfileManager *profileManager;  
	IWMStreamConfig * pWMStreamConfig; 

	hr = WMCreateProfileManager(&profileManager);  
	if (FAILED(hr))
	{
		Msg(TEXT("Failed to ConfigureFilterUsingProfileGuid !  hr=0x%x"), hr);
		return hr;
	}

	IWMProfile *pProfile;  
	
	hr = profileManager->CreateEmptyProfile(WMT_VER_9_0,  &pProfile);
	if (FAILED(hr))
	{
		Msg(TEXT("Failed to ConfigureFilterUsingProfileGuid !  hr=0x%x"), hr);
		return hr;
	}
	hr = pProfile->CreateNewStream(WMMEDIATYPE_Video, &pWMStreamConfig);
	if (FAILED(hr))
	{
		Msg(TEXT("Failed to ConfigureFilterUsingProfileGuid !  hr=0x%x"), hr);
		return hr;
	}
	IWMVideoMediaProps *pProps;
	hr = pWMStreamConfig->QueryInterface(IID_IWMVideoMediaProps,(void**)&pProps);
	if (FAILED(hr))
	{
		Msg(TEXT("Failed to ConfigureFilterUsingProfileGuid !  hr=0x%x"), hr);
		return hr;
	}
	pProps->SetQuality(100);

	 VIDEOINFOHEADER VideoInfoHeader;
        VideoInfoHeader.dwBitRate = 1000000000000000;
        VideoInfoHeader.dwBitErrorRate =1;
        VideoInfoHeader.AvgTimePerFrame = (REFERENCE_TIME) (1 * 1000 * 10000 /10000000000000); 
            RECT rcDst2;
            SetRect(&rcDst2, 0, 0, 240, 200);
			SetRect(&VideoInfoHeader.rcSource,  0, 0,640,480);
			VideoInfoHeader.rcTarget = rcDst2;
        VideoInfoHeader.bmiHeader.biWidth = rcDst2.right; 
        VideoInfoHeader.bmiHeader.biHeight = rcDst2.bottom; 
        VideoInfoHeader.bmiHeader.biSizeImage = 0; 
		VideoInfoHeader.bmiHeader.biSize = sizeof(VideoInfoHeader.bmiHeader);
	VideoInfoHeader.bmiHeader.biXPelsPerMeter = 0;
	VideoInfoHeader.bmiHeader.biYPelsPerMeter = 0;
	VideoInfoHeader.bmiHeader.biClrImportant = 0;
	VideoInfoHeader.bmiHeader.biClrUsed = 0;
		VideoInfoHeader.bmiHeader.biPlanes = 1;
		VideoInfoHeader.bmiHeader.biCompression =  0x56595559;

    WM_MEDIA_TYPE *pType = new WM_MEDIA_TYPE();
            
        pType->majortype =WMMEDIATYPE_Video;
        pType->subtype  = WMMEDIASUBTYPE_WMV3;
        pType->bFixedSizeSamples = FALSE;
        pType->bTemporalCompression = TRUE;
        pType->lSampleSize = 0;
        pType->formattype = WMFORMAT_VideoInfo;
        pType->pUnk = NULL;
        pType->cbFormat = sizeof(VIDEOINFOHEADER);
        pType->pbFormat = (BYTE*) &VideoInfoHeader; 

	
	pProps->SetMediaType(pType);
	if (FAILED(hr))
	{
		Msg(TEXT("Failed to ConfigureFilterUsingProfileGuid !  hr=0x%x"), hr);
		return hr;
	}
	
	hr = mpWriterConfig->ConfigureFilterUsingProfile(pProfile);  
	if (FAILED(hr))
	{
		Msg(TEXT("Failed to ConfigureFilterUsingProfileGuid !  hr=0x%x"), hr);
		return hr;
	}
	//hr = mpWriterConfig->ConfigureFilterUsingProfileGuid(WMProfile_V40_250Video);

	if (FAILED(hr))
	{
		Msg(TEXT("Failed to ConfigureFilterUsingProfileGuid !  hr=0x%x"), hr);
		return hr;
	}

	

	hr = pASFWriter->QueryInterface(IID_IServiceProvider, (void**)&pProvider);
	if (SUCCEEDED(hr))
	{
		hr = pProvider->QueryService(IID_IWMWriterAdvanced2,
			IID_IWMWriterAdvanced2, (void**)&pWMWA2);
	}

	if (FAILED(hr = pWMWA2->SetLiveSource(TRUE)))
	{
		printf ("mpWriter2->SetLiveSource FAILED/n");
		return hr;
	} 

	IWMWriterFileSink*      m_pFileSink;

	hr = WMCreateWriterFileSink( &m_pFileSink );
	if ( FAILED( hr ) )
	{
		_tprintf( _T( "Could not create Writer File Sink (hr=0x%08x)./n" ), hr );
		return hr;
	}

	m_pFileSink->Open (L"test.asf");
	if ( FAILED( hr ) )
	{
		_tprintf( _T( "Could not create Writer File Sink (hr=0x%08x)./n" ), hr );
		return hr;
	}

	pWMWA2->AddSink(m_pFileSink);
	if ( FAILED( hr ) )
	{
		_tprintf( _T( "Could not create Writer File Sink (hr=0x%08x)./n" ), hr );
		return hr;
	}


	// remove default sink... added back later dynamically
	if (FAILED(hr = pWMWA2->RemoveSink(0)))
	{
		printf (" mpWriter2->RemoveSink FAILED/n");
		return hr;
	} 

	pProvider->Release();

	IWMWriterNetworkSink *mpNetSink = NULL;

	if (FAILED(hr = WMCreateWriterNetworkSink(&mpNetSink)))
	{
		printf ("WMCreateWriterNetworkSink FAILED/n");
		return hr;
	} 

	DWORD dwPort = 1234;




	if (FAILED(hr = mpNetSink->Open(&dwPort)))
	{
		printf ("Port open FAILED/n");
		return hr;
	} 

	WCHAR url[256];
	DWORD url_len = 256;

	hr = mpNetSink->GetHostURL(url, &url_len);


	if (FAILED(pWMWA2->AddSink(mpNetSink)))
	{
		printf ("mpWriter2->AddSink FAILED/n");
		return hr;
	}



	// Attach the filter graph to the capture graph
	hr = g_pCapture->SetFiltergraph(g_pGraph);
	if (FAILED(hr))
	{
		Msg(TEXT("Failed to set capture filter graph!  hr=0x%x"), hr);
		return hr;
	}

	// Use the system device enumerator and class enumerator to find
	// a video capture/preview device, such as a desktop USB video camera.
	hr = FindCaptureDevice(&pSrcFilter);
	if (FAILED(hr))
	{
		// Don't display a message because FindCaptureDevice will handle it
		return hr;
	}

	// Add Capture filter to our graph.
	hr = g_pGraph->AddFilter(pSrcFilter, L"Video Capture");
	if (FAILED(hr))
	{
		Msg(TEXT("Couldn't add the capture filter to the graph!  hr=0x%x/r/n/r/n") 
			TEXT("If you have a working video capture device, please make sure/r/n")
			TEXT("that it is connected and is not being used by another application./r/n/r/n")
			TEXT("The sample will now close."), hr);
		pSrcFilter->Release();
		return hr;
	}

	// Render the preview pin on the video capture filter
	// Use this instead of g_pGraph->RenderFile
	hr = g_pCapture->RenderStream (&PIN_CATEGORY_PREVIEW, &MEDIATYPE_Video,
		pSrcFilter, NULL, NULL);

	hr = g_pCapture->RenderStream (&PIN_CATEGORY_CAPTURE, &MEDIATYPE_Video,
		pSrcFilter, NULL, pASFWriter);

	if (FAILED(hr))
	{
		Msg(TEXT("Couldn't render the video capture stream.  hr=0x%x/r/n")
			TEXT("The capture device may already be in use by another application./r/n/r/n")
			TEXT("The sample will now close."), hr);
		pSrcFilter->Release();
		return hr;
	}

	// Now that the filter has been added to the graph and we have
	// rendered its stream, we can release this reference to the filter.
	pSrcFilter->Release();


	// Set video window style and position
	hr = SetupVideoWindow();
	if (FAILED(hr))
	{
		Msg(TEXT("Couldn't initialize video window!  hr=0x%x"), hr);
		return hr;
	}

#ifdef REGISTER_FILTERGRAPH
	// Add our graph to the running object table, which will allow
	// the GraphEdit application to "spy" on our graph
	hr = AddGraphToRot(g_pGraph, &g_dwGraphRegister);
	if (FAILED(hr))
	{
		Msg(TEXT("Failed to register filter graph with ROT!  hr=0x%x"), hr);
		g_dwGraphRegister = 0;
	}
#endif

	// Start previewing video data
	hr = g_pMC->Run();


	if (FAILED(hr))
	{
		Msg(TEXT("Couldn't run the graph!  hr=0x%x"), hr);
		return hr;
	}

	// Remember current state
	g_psCurrent = Running;

	return S_OK;
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值