Click here to Skip to main content
15,891,372 members
Home / Discussions / Graphics
   

Graphics

 
GeneralRe: Read from text file ..! Pin
mr jets17-May-07 13:30
mr jets17-May-07 13:30 
GeneralRe: Read from text file ..! Pin
Mark Salsbery18-May-07 5:47
Mark Salsbery18-May-07 5:47 
AnswerRe: Read from text file ..! Pin
Christian Graus17-May-07 13:53
protectorChristian Graus17-May-07 13:53 
QuestionI need to re-broadcast IP camera Pin
xlthim15-May-07 9:45
xlthim15-May-07 9:45 
AnswerRe: I need to re-broadcast IP camera Pin
Jain Mohit24-May-07 22:10
Jain Mohit24-May-07 22:10 
AnswerRe: I need to re-broadcast IP camera Pin
Rilhas25-May-07 9:36
Rilhas25-May-07 9:36 
QuestionCorel CMX plugin? Pin
Chals15-May-07 5:43
Chals15-May-07 5:43 
QuestionDirectShow - Analyze audio stream from AVI - as fast as possible Pin
Claudiu Bucur13-May-07 6:30
Claudiu Bucur13-May-07 6:30 
I need to create audio peaks for the audio stream from an AVI. For this I intend to use DirectShow by using SampleGrabber. I adapted some of the samples from the SDK.

There is only one problem here: I receive the audio samples in real-time (i.e. the same as I would be playing the file), but I need this process to be much faster. I was advised to set the graph SyncSource to NULL and by this the samples will be processed as quickly as possible. Unfortunatelly I get no effect by doing this.

I'm attaching here the code I'm using, perhaps I'm doing something wrong and somebody can help me out

Thanks alot !

#include <windows.h>
#include <streams.h>
#include <stdio.h>
#include <atlbase.h>
#include <qedit.h>

int GrabBitmaps(TCHAR * szFile);
HRESULT GetPin(IBaseFilter * pFilter, PIN_DIRECTION dirrequired,  int iNum, IPin **ppPin);
IPin *  GetInPin ( IBaseFilter *pFilter, int Num );
IPin *  GetOutPin( IBaseFilter *pFilter, int Num );

class CSampleGrabberCB : public ISampleGrabberCB {

public:

	STDMETHODIMP_(ULONG) AddRef() { return 2; }
	STDMETHODIMP_(ULONG) Release() { return 1; }
	STDMETHODIMP QueryInterface(REFIID riid, void ** ppv) {
		CheckPointer(ppv,E_POINTER);
		if( riid == IID_ISampleGrabberCB || riid == IID_IUnknown )  {
			*ppv = (void *) static_cast<ISampleGrabberCB*> ( this );
			return NOERROR;
		}
		return E_NOINTERFACE;
	}

	STDMETHODIMP SampleCB( double SampleTime, IMediaSample * pSample ) {return 0;}

	STDMETHODIMP BufferCB( double SampleTime, BYTE * pBuffer, long BufferSize ) {
		_tprintf(TEXT("Found a sample at %f s\t[%ld]\r\n"), SampleTime , BufferSize );
		return 0;
	}
}

int _tmain(int argc, TCHAR* argv[]) {
	if( argc != 2 || !argv || !argv[1] ) {
		_tprintf( TEXT("GrabBitmaps: You must specify a media filename!\r\n") );
		_tprintf( TEXT("Usage: GrabBitmaps Filename\r\n"));
		return 0;
	}

	CoInitializeEx(NULL, COINIT_APARTMENTTHREADED);

	// Read the filename from the command line      
	TCHAR szFile[MAX_PATH];
	_tcsncpy(szFile, argv[1], MAX_PATH-1);
	szFile[MAX_PATH-1] = 0;      // Null-terminate

	int nSuccess = GrabBitmaps(szFile);
	CoUninitialize();
	return nSuccess;
}

int GrabBitmaps(TCHAR * szFile ) {
	USES_CONVERSION;
	CComPtr< ISampleGrabber > pGrabber;
	CComPtr< IBaseFilter >    pSource;
	CComPtr< IGraphBuilder >  pGraph;
	CComPtr< IVideoWindow >   pVideoWindow;
	HRESULT hr;

	if (!szFile) return -1;
	_tprintf(TEXT("Grabbing bitmaps from %s.\r\n"), szFile);
	
	// Create the sample grabber
	pGrabber.CoCreateInstance( CLSID_SampleGrabber );
	if( !pGrabber ) {
		_tprintf( TEXT("Could not create CLSID_SampleGrabber\r\n") );
		return -1;
	}
	CComQIPtr<IBaseFilter, &IID_IBaseFilter> pGrabberBase(pGrabber);
	
	// Create the file reader
	pSource.CoCreateInstance(CLSID_AsyncReader);
	if (!pSource) {
		_tprintf( TEXT("Could not create source filter\r\n") );
		return -1;
	}
	
	// Create the graph
	pGraph.CoCreateInstance(CLSID_FilterGraph);
	if( !pGraph ) {
		_tprintf( TEXT("Could not not create the graph\r\n") );
		return -1;
	}
	
	// Put them in the graph
	hr = pGraph->AddFilter( pSource, L"Source" );
	hr = pGraph->AddFilter( pGrabberBase, L"Grabber" );

	// Load the source
	CComQIPtr<IFileSourceFilter, &IID_IFileSourceFilter> pLoad(pSource);
	hr = pLoad->Load( T2W( szFile ), NULL );
	if (FAILED(hr)) {
		_tprintf( TEXT("Could not load the media file\r\n") );
		return -1;
	}

	CMediaType GrabType;
	GrabType.SetType(&MEDIATYPE_Audio);
	GrabType.SetSubtype(&MEDIASUBTYPE_PCM);
	GrabType.SetFormatType(&FORMAT_WaveFormatEx);
	hr = pGrabber->SetMediaType(&GrabType);

	// Get the output pin and the input pin
	CComPtr< IPin > pSourcePin;
	CComPtr< IPin > pGrabPin;

	pSourcePin = GetOutPin( pSource, 0 );
	pGrabPin   = GetInPin( pGrabberBase, 0 );

	// ... and connect them
	hr = pGraph->Connect( pSourcePin, pGrabPin );
	if( FAILED( hr ) ) {
		_tprintf( TEXT("Could not connect source filter to grabber\r\n") );
		return -1;
	}

	CComPtr <IPin> pGrabOutPin = GetOutPin( pGrabberBase, 0 );
	hr = pGraph->Render( pGrabOutPin );
	if( FAILED( hr ) ) {
		_tprintf( TEXT("Could not render grabber output pin\r\n") );
		return -1;
	}
	
	// Don't buffer the samples as they pass through
	hr = pGrabber->SetBufferSamples( FALSE );

	hr = pGrabber->SetOneShot(FALSE);

	// Set the callback, so we can grab the one sample
	CSampleGrabberCB CB;
	hr = pGrabber->SetCallback(&CB, 1);

	// Keep a useless clock from being instantiated....
	CComQIPtr<IMediaFilter, &IID_IMediaFilter> pFilter(pGraph);
	hr = pFilter->SetSyncSource(NULL);
	if (FAILED(hr)) printf("SetNoClock: Failed to set sync source!  hr=0x%x\n", hr);

	CComQIPtr<IVideoWindow, &IID_IVideoWindow> pWindow(pGraph);
	if (pWindow) hr = pWindow->put_AutoShow(OAFALSE);

	// activate the threads		
	CComQIPtr< IMediaControl, &IID_IMediaControl > pControl( pGraph );
	hr = pControl->Run( );

	// wait for the graph to settle
	CComQIPtr< IMediaEvent, &IID_IMediaEvent > pEvent( pGraph );
	long EvCode = 0;
	hr = pEvent->WaitForCompletion( INFINITE, &EvCode );
	
	_tprintf(TEXT("Sample grabbing complete.\r\n"));
	return 0;
}


HRESULT GetPin( IBaseFilter * pFilter, PIN_DIRECTION dirrequired, int iNum, IPin **ppPin) {
	CComPtr< IEnumPins > pEnum;
	*ppPin = NULL;

	HRESULT hr = pFilter->EnumPins(&pEnum);
	if(FAILED(hr))  return hr;

	ULONG ulFound;
	IPin *pPin;
	hr = E_FAIL;

	while (S_OK == pEnum->Next(1, &pPin, &ulFound)) {
		PIN_DIRECTION pindir = (PIN_DIRECTION)3;
		pPin->QueryDirection(&pindir);
		if (pindir == dirrequired) {
			if (iNum == 0) {
				*ppPin = pPin;  // Return the pin's interface
				hr = S_OK;      // Found requested pin, so clear error
				break;
			}
			iNum--;
		} 
		pPin->Release();
	}

	return hr;
}

IPin * GetInPin( IBaseFilter * pFilter, int nPin ) {
	CComPtr<IPin> pComPin=0;
	GetPin(pFilter, PINDIR_INPUT, nPin, &pComPin);
	return pComPin;
}

IPin * GetOutPin( IBaseFilter * pFilter, int nPin ) {
	CComPtr<IPin> pComPin=0;
	GetPin(pFilter, PINDIR_OUTPUT, nPin, &pComPin);
	return pComPin;
}

QuestionRe: DirectShow - Analyze audio stream from AVI - as fast as possible Pin
Mark Salsbery14-May-07 11:50
Mark Salsbery14-May-07 11:50 
AnswerRe: DirectShow - Analyze audio stream from AVI - as fast as possible Pin
Claudiu Bucur15-May-07 4:58
Claudiu Bucur15-May-07 4:58 
QuestionRe: DirectShow - Analyze audio stream from AVI - as fast as possible Pin
Mark Salsbery15-May-07 5:12
Mark Salsbery15-May-07 5:12 
AnswerRe: DirectShow - Analyze audio stream from AVI - as fast as possible Pin
Claudiu Bucur15-May-07 5:20
Claudiu Bucur15-May-07 5:20 
GeneralRe: DirectShow - Analyze audio stream from AVI - as fast as possible Pin
Mark Salsbery15-May-07 7:33
Mark Salsbery15-May-07 7:33 
AnswerRe: DirectShow - Analyze audio stream from AVI - as fast as possible Pin
Claudiu Bucur15-May-07 7:43
Claudiu Bucur15-May-07 7:43 
GeneralRe: DirectShow - Analyze audio stream from AVI - as fast as possible Pin
Mark Salsbery15-May-07 7:59
Mark Salsbery15-May-07 7:59 
Questionurgent..pzzz!! Pin
mr jets10-May-07 17:38
mr jets10-May-07 17:38 
AnswerRe: urgent..pzzz!! Pin
Mark Salsbery11-May-07 13:16
Mark Salsbery11-May-07 13:16 
GeneralRe: urgent..pzzz!! Pin
mr jets11-May-07 14:42
mr jets11-May-07 14:42 
GeneralRe: urgent..pzzz!! Pin
Mark Salsbery11-May-07 15:21
Mark Salsbery11-May-07 15:21 
AnswerRe: urgent..pzzz!! Pin
Christian Graus13-May-07 12:31
protectorChristian Graus13-May-07 12:31 
AnswerRe: urgent..pzzz!! Pin
El Corazon14-May-07 6:38
El Corazon14-May-07 6:38 
QuestionMore than one 'SwapBuffers(HDC)' Pin
XTAL25610-May-07 15:23
XTAL25610-May-07 15:23 
AnswerRe: More than one 'SwapBuffers(HDC)' Pin
El Corazon10-May-07 16:12
El Corazon10-May-07 16:12 
GeneralRe: More than one 'SwapBuffers(HDC)' Pin
XTAL25610-May-07 17:01
XTAL25610-May-07 17:01 
GeneralRe: More than one 'SwapBuffers(HDC)' Pin
El Corazon13-May-07 18:55
El Corazon13-May-07 18:55 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.