#include "pch.hpp"
//===========================================================================//
// File:	 DirectShow.cpp													 //
// Project:  GameOS					                                         //
// Contents: Interface to DirectShow										 //
//---------------------------------------------------------------------------//
// Date      Who    Modification                                             //
// 01/23/98  AG     Created													 //
//---------------------------------------------------------------------------//
// Copyright (C) 1999 Microsoft Corp.						                 //
// All Rights reserved worldwide                                             //
// This unpublished sourcecode is PROPRIETARY and CONFIDENTIAL               //
//===========================================================================//
#include "ErrorHandler.hpp"
#include "DirectXDebugging.hpp"



HRESULT wSetState( IMultiMediaStream* imms, STREAM_STATE NewState )
{
	SPEW(( GROUP_DIRECTSHOW, "SetState(0x%x: %s)",(int)imms, (NewState ? "STREAMSTATE_RUN" : "STREAMSTATE_STOP") ));

	HRESULT result=imms->SetState( NewState );

	if( FAILED(result) )
		PAUSE(( "FAILED (0x%x - %s) - SetState(0x%x: %s)",result,ErrorNumberToMessage(result),(int)imms, (NewState ? "Run" : "Stop") ));

	return result;
}

HRESULT wGetState( IMultiMediaStream* imms, STREAM_STATE* NewState )
{
	SPEW(( GROUP_DIRECTSHOW, "GetState(0x%x)+", (int)imms ));

	HRESULT result=imms->GetState( NewState );

	SPEW(( GROUP_DIRECTSHOW, " - ", (*NewState ? "STREAMSTATE_RUN" : "STREAMSTATE_STOP")));

	if( FAILED(result) )
		PAUSE(( "FAILED (0x%x - %s) - GetState(0x%x)",result,ErrorNumberToMessage(result), (int)imms ));

	return result;
}

HRESULT wSeek( IMultiMediaStream* imms, STREAM_TIME SeekTime )
{
	SPEW(( GROUP_DIRECTSHOW, "Seek(0x%x: %.2fms)", (int)imms, (double)(SeekTime)/10000 ));

	HRESULT result=imms->Seek( SeekTime );

	if( FAILED(result) )
		PAUSE(( "FAILED (0x%x - %s) - Seek(0x%x: %.2fms)",result,ErrorNumberToMessage(result),(int)imms,(double)(SeekTime)/10000 ));

	return result;
}


HRESULT wGetMediaStream( IMultiMediaStream* imms, REFMSPID idPurpose, IMediaStream** pMediaStream )
{
	SPEW(( GROUP_DIRECTSHOW, "GetMediaStream(0x%x: 0x%x)", (int)imms, (int)pMediaStream ));

	HRESULT result=imms->GetMediaStream( idPurpose, pMediaStream  );

	if( FAILED(result) )
		PAUSE(( "FAILED (0x%x - %s) - GetMediaStream(0x%x: 0x%x)",result,ErrorNumberToMessage(result), (int)imms, (int)pMediaStream ));

	return result;
}


HRESULT wInitialize( IAMMultiMediaStream* iammms, STREAM_TYPE StreamType, DWORD dwFlags, IGraphBuilder* pFilterGraph )
{
	SPEW(( GROUP_DIRECTSHOW, "Initialize(0x%x: %s,%s,0x%x)", (int)iammms, DSGetStreamType(StreamType),DSGetInitFlags(dwFlags),(int)pFilterGraph ));

	HRESULT result=iammms->Initialize( StreamType, dwFlags, pFilterGraph  );

	if( FAILED(result) )
		PAUSE(( "FAILED (0x%x - %s) - Initialize(0x%x: %s,%s,0x%x)",result,ErrorNumberToMessage(result), (int)iammms, DSGetStreamType(StreamType),DSGetInitFlags(dwFlags),(int)pFilterGraph ));

	return result;
}


HRESULT wAddMediaStream( IAMMultiMediaStream* iammms, IUnknown* pStreamObject, const MSPID *pPurposeID, DWORD dwFlags, IMediaStream** ppNewStream )
{
	SPEW(( GROUP_DIRECTSHOW, "AddMediaStream(0x%x: 0x%x,0x%x,0x%x,0x%x)",(int)iammms,(int)pStreamObject,(int)pPurposeID,(int)dwFlags,(int)ppNewStream ));

	HRESULT result=iammms->AddMediaStream( pStreamObject, pPurposeID, dwFlags, ppNewStream );

	if( FAILED(result) )
		PAUSE(( "FAILED (0x%x - %s) - AddMediaStream(0x%x: 0x%x,0x%x,0x%x,0x%x)",result,ErrorNumberToMessage(result),(int)iammms,(int)pStreamObject,(int)pPurposeID,(int)dwFlags,(int)ppNewStream ));

	return result;
}

HRESULT wOpenFile( IAMMultiMediaStream* iammms, LPCWSTR pszFileName, DWORD dwFlags )
{
	SPEW(( GROUP_DIRECTSHOW, "OpenFile(0x%x: \"%s\",0x%x)",(int)iammms,GetANSIfromUNICODE(pszFileName),(int)dwFlags ));

	HRESULT result=iammms->OpenFile( pszFileName, dwFlags );

	if( FAILED(result) )
		PAUSE(( "FAILED (0x%x - %s) - OpenFile(0x%x: \"%s\",0x%x)",result,ErrorNumberToMessage(result),(int)iammms,GetANSIfromUNICODE(pszFileName),(int)dwFlags ));

	return result;
}

HRESULT wCreateSample( IDirectDrawMediaStream* iddms, IDirectDrawSurface* pSurface, const RECT *pRect, DWORD dwFlags, IDirectDrawStreamSample** ppSample )
{
	char tmp[32];
	SPEW(( GROUP_DIRECTSHOW, "CreateSample(0x%x: 0x%x,%s,0x%x,0x%x)",(int)iddms,(int)pSurface,GetRectangle(tmp,(RECT*)pRect),(int)dwFlags,(int)ppSample ));

	HRESULT result=iddms->CreateSample( pSurface,pRect,dwFlags,ppSample );

	if( FAILED(result) )
		PAUSE(( "FAILED (0x%x - %s) - CreateSample(0x%x: 0x%x,%s,0x%x,0x%x)",result,ErrorNumberToMessage(result),(int)iddms,(int)pSurface,GetRectangle(tmp,(RECT*)pRect),(int)dwFlags,(int)ppSample ));

	return result;
}


HRESULT wSetFormat( IDirectDrawMediaStream* iddss, DDSURFACEDESC * ddsd )
{
	SPEW(( GROUP_DIRECTSHOW, "SetFormat(0x%x: %s)",(int)iddss, GetSurfaceDescriptionOld(ddsd) ));

	HRESULT result=iddss->SetFormat( ddsd, NULL); // NULL because we'll never support palettes

	if( FAILED(result) )
		PAUSE(( "FAILED (0x%x - %s) - SetFormat(0x%x %s)",result,ErrorNumberToMessage(result),(int)iddss,GetSurfaceDescriptionOld(ddsd) ));

	return result;
}

HRESULT wGetFormat( IDirectDrawMediaStream* iddss, DDSURFACEDESC* ddsd_current, DDSURFACEDESC* ddsd_desired, DWORD * flags )
{
	char tmp[64];
	strcpy( tmp, GetSurfaceDescriptionOld(ddsd_current) );

	SPEW(( GROUP_DIRECTSHOW, "GetFormat(0x%x: %s, %s, 0x%x)",(int)iddss, tmp, GetSurfaceDescriptionOld(ddsd_desired) , (int) flags ));

	HRESULT result=iddss->GetFormat( ddsd_current, NULL, ddsd_desired, flags); // NULL because we'll never support palettes

	if( FAILED(result) )
		PAUSE(( "FAILED (0x%x - %s) - GetFormat(0x%x: %s, %s, 0x%x)",result,ErrorNumberToMessage(result),(int)iddss, tmp, GetSurfaceDescriptionOld(ddsd_desired) , (int) flags ));

	return result;
}

HRESULT wGetSurface( IDirectDrawStreamSample* iddss, IDirectDrawSurface ** ppDirectDrawSurface, RECT* pRect )
{
	char tmp[32];
	SPEW(( GROUP_DIRECTSHOW, "GetSurface(0x%x: 0x%x,%s)",(int)iddss,(int)ppDirectDrawSurface,GetRectangle(tmp,(RECT*)pRect) ));

	HRESULT result=iddss->GetSurface( ppDirectDrawSurface, pRect );

	if( FAILED(result) )
		PAUSE(( "FAILED (0x%x - %s) - GetSurface(0x%x: 0x%x,%s)",result,ErrorNumberToMessage(result),(int)iddss,(int)ppDirectDrawSurface,GetRectangle(tmp,(RECT*)pRect) ));

	return result;
}

HRESULT wSetSampleTimes( IStreamSample* iddss, STREAM_TIME* st1, STREAM_TIME* st2 )
{
	SPEW(( GROUP_DIRECTSHOW, "SetSampleTimes(0x%x: %.2fms, %.2fms)",(int)iddss, (double)(*st1)/10000, (double)(*st2)/10000 ));

	HRESULT result=iddss->SetSampleTimes( st1, st2);

	if( FAILED(result) )
		PAUSE(( "FAILED (0x%x - %s) - SetSampleTimes(0x%x: %.2fms, %.2fms)",result,ErrorNumberToMessage(result),(int)iddss,(double)(*st1)/10000, (double)(*st2)/10000 ));

	return result;
}

HRESULT wUpdate( IStreamSample* iss, DWORD flags, HANDLE event, PAPCFUNC papcfunc, DWORD apc )
{
	SPEW(( GROUP_DIRECTSHOW, "Update(0x%x)",(int)iss ));

	HRESULT result=iss->Update( flags, event, papcfunc, apc );

	if ( FAILED(result) )
		SPEW(( GROUP_DIRECTSHOW, "Failed - %s",ErrorNumberToMessage(result) ));

	return result;
}

HRESULT wPut_Volume( IBasicAudio* iba, LONG value )
{
	SPEW(( GROUP_DIRECTSHOW, "put_Volume(0x%x, %d)",(int)iba, value ));

	HRESULT result=iba->put_Volume( value );

	if ( FAILED(result) )
		PAUSE(( "FAILED (0x%x - %s) - put_Volume(0x%x, %d)",result,ErrorNumberToMessage(result), iba, value ));

	return result;
}

HRESULT wPut_Balance( IBasicAudio* iba, LONG value )
{
	SPEW(( GROUP_DIRECTSHOW, "Put_Balance(0x%x, %d)",(int)iba, value ));

	HRESULT result=iba->put_Balance( value );

	if ( FAILED(result) )
		PAUSE(( "FAILED (0x%x - %s) - Put_Balance(0x%x, %d)",result,ErrorNumberToMessage(result), iba, value ));

	return result;
}

HRESULT wGetFilterGraph( IAMMultiMediaStream* iamms, IGraphBuilder ** igb )
{
	SPEW(( GROUP_DIRECTSHOW, "GetFilterGraph(0x%x: 0x%x)",(int)iamms, igb ));

	HRESULT result=iamms->GetFilterGraph( igb );

	if ( FAILED(result) )
		PAUSE(( "FAILED (0x%x - %s) - GetFilterGraph(0x%x: 0x%x)",result,ErrorNumberToMessage(result),(int)iamms, igb ));

	return result;
}

HRESULT wGetStopPosition( IMediaSeeking* ims, STREAM_TIME* st )
{
	HRESULT result=ims->GetStopPosition( st );

	if ( FAILED(result) )
		PAUSE(( "FAILED (0x%x - %s) - GetStopPosition(0x%x, 0x%x)",result,ErrorNumberToMessage(result), ims, st ));
	else
		if (st)
			SPEW(( GROUP_DIRECTSHOW, "GetStopPosition(0x%x) -  %I64d",(int)ims, *st ));
	return result;
}

HRESULT wSetPositions( IMediaSeeking* ims, STREAM_TIME* st, DWORD stflags, STREAM_TIME* end, DWORD eflags )
{
	char tmp1[256];
	char tmp2[256];
	strcpy(tmp1, GetDShowSeekType(stflags));
	strcpy(tmp2, GetDShowSeekType(stflags));

	SPEW(( GROUP_DIRECTSHOW, "SetPositions(0x%x: 0x%x, %s, 0x%x, %s)",(int)ims, st, tmp1, end, tmp2 ));

	HRESULT result=ims->SetPositions(st, stflags, end, eflags );

	if ( FAILED(result) )
		PAUSE(( "FAILED (0x%x - %s) - SetPositions(0x%x: 0x%x, %s, 0x%x, %s)",result,ErrorNumberToMessage(result),(int)ims,  st, tmp1, end, tmp2 ));

	return result;
}

HRESULT wGetSampleTimes( IDirectDrawStreamSample* iddss, STREAM_TIME* st, STREAM_TIME* end, STREAM_TIME* cur )
{
	SPEW(( GROUP_DIRECTSHOW, "GetSampleTimes(0x%x) +",(int)iddss ));

	HRESULT result=iddss->GetSampleTimes( st,end, cur);

	SPEW(( GROUP_DIRECTSHOW, "- Start=%.2fms, End=%.2fms, Current=%.2fms",(st ? (double)(*st)/10000 : 0), (end ? (double)(*end)/10000 : 0),(cur ? (double)(*cur)/10000 : 0) ));

	if ( FAILED(result) )
		PAUSE(( "FAILED (0x%x - %s) - GetSampleTimes(0x%x)",result,ErrorNumberToMessage(result), iddss ));

	return result;
}

HRESULT wGetTime( IMultiMediaStream* imms, STREAM_TIME* theTime )
{
	SPEW(( GROUP_DIRECTSHOW, "GetTime(0x%x) +",(int)imms ));

	HRESULT result=imms->GetTime( theTime );

	SPEW(( GROUP_DIRECTSHOW, "- %.2fms",(double)(*theTime)/10000 ));

	if ( FAILED(result) )
		PAUSE(( "FAILED (0x%x - %s) - GetSampleTimes(0x%x)",result,ErrorNumberToMessage(result),(int)imms ));

	return result;
}

HRESULT wGetDuration( IMultiMediaStream* imms, STREAM_TIME* theTime )
{
	SPEW(( GROUP_DIRECTSHOW, "GetDuration(0x%x) +",(int)imms ));

	HRESULT result=imms->GetDuration( theTime );

	SPEW(( GROUP_DIRECTSHOW, "- %.2fms",(double)(*theTime)/10000 ));

	if ( FAILED(result) )
		PAUSE(( "FAILED (0x%x - %s) - GetSampleTimes(0x%x)",result,ErrorNumberToMessage(result),(int)imms ));

	return result;
}

HRESULT wPut_CurrentPosition( IMediaPosition* iba, REFTIME value )
{
	SPEW(( GROUP_DIRECTSHOW, "put_CurrentPosition(0x%x: %f)",(int)iba, value ));

	HRESULT result=iba->put_CurrentPosition( value );

	if ( FAILED(result) )
		PAUSE(( "FAILED (0x%x - %s) - put_CurrentPosition(0x%x: %f)",result,ErrorNumberToMessage(result),(int)iba, value ));

	return result;
}

HRESULT wGet_CurrentPosition( IMediaPosition* iba, REFTIME* value )
{
	SPEW(( GROUP_DIRECTSHOW, "get_CurrentPosition(0x%x: 0x%x)",(int)iba, value ));

	HRESULT result=iba->get_CurrentPosition( value );

	if ( FAILED(result) )
		PAUSE(( "FAILED (0x%x - %s) - get_CurrentPosition(0x%x: 0x%x)",result,ErrorNumberToMessage(result),(int)iba, value ));

	return result;
}

HRESULT wCanSeekForward( IMediaPosition* iba, LONG* canDo )
{
	SPEW(( GROUP_DIRECTSHOW, "CanSeekForward(0x%x: 0x%x)",(int)iba, canDo ));

	HRESULT result=iba->CanSeekForward(canDo);

	if ( FAILED(result) )
		PAUSE(( "FAILED (0x%x - %s) - CanSeekForward(0x%x; 0x%x)",result,ErrorNumberToMessage(result),(int)iba, canDo ));

	return result;
}

HRESULT wGet_Duration( IMediaPosition* iba, REFTIME* value )
{
	SPEW(( GROUP_DIRECTSHOW, "get_Duration(0x%x: 0x%x)",(int)iba, value ));

	HRESULT result=iba->get_Duration( value );

	if ( FAILED(result) )
		PAUSE(( "FAILED (0x%x - %s) - get_Duration(0x%x: 0x%x)",result,ErrorNumberToMessage(result),(int)iba, value ));

	return result;
}

HRESULT wStop( IMediaControl* iba )
{
	SPEW(( GROUP_DIRECTSHOW, "Stop(0x%x)",(int)iba ));

	HRESULT result=iba->Stop();

	if ( FAILED(result) )
		PAUSE(( "FAILED (0x%x - %s) - Stop(0x%x)",result,ErrorNumberToMessage(result),(int)iba ));

	return result;
}

HRESULT wPause( IMediaControl* iba )
{
	SPEW(( GROUP_DIRECTSHOW, "Pause(0x%x)",(int)iba ));

	HRESULT result=iba->Pause();

	if ( FAILED(result) )
		PAUSE(( "FAILED (0x%x - %s) - Pause(0x%x)",result,ErrorNumberToMessage(result),(int)iba ));

	return result;
}

HRESULT wRun( IMediaControl* iba )
{
	SPEW(( GROUP_DIRECTSHOW, "Run(0x%x)",(int)iba ));

	HRESULT result=iba->Run();

	if ( FAILED(result) )
		PAUSE(( "FAILED (0x%x - %s) - Run(0x%x)",result,ErrorNumberToMessage(result),(int)iba ));

	return result;
}

HRESULT wGetEndOfStreamEventHandle(IMultiMediaStream* imms, HANDLE * ph)
{
	SPEW(( GROUP_DIRECTSHOW, "GetEndOfStreamEventHandle(0x%x: 0x%x)",(int)imms, (int) ph ));

	HRESULT result=imms->GetEndOfStreamEventHandle(ph);

	if ( FAILED(result) )
		PAUSE(( "FAILED (0x%x - %s) - GetEndOfStreamEventHandle(0x%x: 0x%x)",result,ErrorNumberToMessage(result), (int)imms, (int) ph ));

	return result;
}

HRESULT wGetInformation( IMultiMediaStream* imms, DWORD * flags, STREAM_TYPE *theType )
{
	SPEW(( GROUP_DIRECTSHOW, "GetInformation(0x%x: 0x%x, 0x%x)",(int)imms, flags, theType ));

	HRESULT result=imms->GetInformation( flags, theType );

	if ( FAILED(result) )
		PAUSE(( "FAILED (0x%x - %s) - GetInformation(0x%x: 0x%x, 0x%x)",result,ErrorNumberToMessage(result),(int)imms, flags, theType ));

	return result;
}















