⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 aaci_dmabak.c

📁 此压缩包为杰得开发得z228的BSP的源代码,可以实现很多功能,尤其是视频解码有很好的效果.
💻 C
📖 第 1 页 / 共 2 页
字号:
#include <windows.h>
#include <pkfuncs.h>
#include <ceddk.h>
#include <oalfuncs.h>
#include <dma.h>
#include "oalintr.h"
#include "wavemdd.h"
#include "pdd_audio.h"
#define DebugMsg_Normal 0x01
#define DebugMsg_Important 0x02
#define DebugMsg_Trace	0x04
#define DebugMsgMask (DebugMsg_Important|DebugMsg_Normal)

int ReleasePlayDMA(void);
int InitPlayDMA();
int AllocPlayDMA(void);
void GetPlayBuffer( unsigned int *buf );
int InitAll();
int StartPlayDMA( unsigned int bufphy, int size ,  int* actualbuffered );
int ReleaseAll();
int AllocRecordDMA(void);
int InitRecordDMA(void);
int StartRecordDMA( LPSTR buffer , DWORD size , int* actualbuffered);
DWORD AudioDMAThread(LPVOID p);

static HANDLE DmaPlayEvent = 0;
static HANDLE PlayCmdEvent = 0;
static volatile int WaitPlayBuffer = 0;
static HANDLE DmaRecordEvent = 0;
static HANDLE RecordCmdEvent = 0;
static volatile int WaitRecordBuffer = 0;
static volatile int DMAPlayCmd;
static volatile int DMARecordCmd;
static int TotalPlayBufferCnt = 0;
static volatile int PlayBufferPlayIndex = 0;
static volatile int PlayBufferStoreIndex = 0;

static int PlayDMAChannelNumber = -1;
static int PlayDmaIntID = 0;
static int RecordDMAChannelNumber = -1;
static int RecordDmaIntID = 0;
static HANDLE DmaThread = 0;
static HANDLE DmaRecordThread = 0;

WAVEFORMATEX	RecordFormat;

static int playcount = 0;
static int playedcount = 0;
int InRemove = 0;

static int recordcount = 0;
static int recordedcount = 0;
#define DMA_PLAY_STOP  0
#define DMA_PLAY_START 1
#define DMA_PLAY_EXIT  2

#define DMA_RECORD_STOP  0
#define DMA_RECORD_START 1
#define DMA_RECORD_EXIT  2

static char RecordTemp[4][0x1000];
static int RecordIndex = 0;
static int RecordOver = 0;

static char DebugMsg[1024*512];
static int MsgLength = 0;
CRITICAL_SECTION Critical;

static void StartDebugMsg()
{
	MsgLength = 0;
	memset( DebugMsg , 0 , sizeof(DebugMsg) );
}

static void EndDebugMsg()
{
	return;
	EnterCriticalSection( &Critical );
	if( MsgLength )
	{
		char temp[256];
		FILE* fp;
		sprintf( temp , "save %d" , MsgLength );
		fp = fopen( temp , "wb" );
		if( fp )
		{
			fwrite( DebugMsg + MsgLength , 1 , sizeof(DebugMsg)-MsgLength , fp );
			fwrite( DebugMsg , 1 , MsgLength , fp );
			fclose( fp );
		}
		MsgLength = 0;
	}
	LeaveCriticalSection( &Critical );
}
static void AddDebugMsg( char* fmt, ... )
{
	char temp[256];
	int length;
	va_list args;
	return;
	EnterCriticalSection( &Critical );	
	va_start(args, fmt);
	vsprintf( temp , fmt , args);
	length = strlen( temp );
	if( MsgLength + length >= sizeof(DebugMsg) )
		MsgLength = 0;
	{
		memcpy( DebugMsg+MsgLength , temp , length );
		MsgLength += length;
		DebugMsg[MsgLength++] = '\0';
	}
	LeaveCriticalSection( &Critical );
}

int OnInit()
{
	int error = 0;
	if( InitAll() != 0 )
		error = 1;
	if( error )
	{
		DMAPlayCmd = DMA_PLAY_EXIT;
		DMARecordCmd = DMA_RECORD_EXIT;
	}
	else
	{
		DMAPlayCmd = DMA_PLAY_STOP;
		DMARecordCmd = DMA_RECORD_STOP;
	}
	InitializeCriticalSection( &Critical );
	return error;
}

int OnDeinit()
{
	int error = 0;	
	ReleaseAll();
	return error;
}

int OnRecordOpen()
{
	int error = 0;
	DMARecordCmd = DMA_RECORD_STOP;	
	recordcount = recordedcount = 0;
	RecordIndex = RecordOver = 0;
	StartDebugMsg();
	RETAILMSG( (DebugMsgMask&DebugMsg_Normal) , (TEXT( "OnRecordOpen\r\n" ) ));
	return error;
}
int OnRecordStart()
{
	int error = 0;
	DMARecordCmd = DMA_RECORD_START;
	RETAILMSG( (DebugMsgMask&DebugMsg_Normal), (TEXT( "OnRecordStart\r\n" ) ));
	SetEvent( RecordCmdEvent );
	return error;
}
int OnRecordStop()
{
	int error = 0;
	DMARecordCmd = DMA_RECORD_STOP;
	return error;
}

int OnRecordClose()
{
	int error = 0;
	EndDebugMsg();
	DMARecordCmd = DMA_RECORD_STOP;
	SetEvent( RecordCmdEvent );
	RETAILMSG( (DebugMsgMask&DebugMsg_Normal), (TEXT( "OnRecordClose\r\n" ) ));
	return error;
}

int OnPlayOpen()
{
	int error = 0;
	PlayBufferPlayIndex = PlayBufferStoreIndex = 0;
	DMAPlayCmd = DMA_PLAY_START;	
	playcount = playedcount = 0;
	StartDebugMsg();
	RETAILMSG( (DebugMsgMask&DebugMsg_Normal), (TEXT( "OnPlayOpen\r\n" ) ));
	return error;
}

int OnPlayStart()
{
	int error = 0;
	DMAPlayCmd = DMA_PLAY_START;
	RETAILMSG( (DebugMsgMask&DebugMsg_Normal), (TEXT( "OnPlayStart\r\n" ) ));
	return error;
}
int OnPlayStop()
{
	int error = 0;
	DMAPlayCmd = DMA_PLAY_STOP;
	MsgLength = 0;
	SetEvent( PlayCmdEvent );
	return error;
}
int OnPlayClose()
{
	int error = 0;
	DMAPlayCmd = DMA_PLAY_STOP;

	RETAILMSG( (DebugMsgMask&DebugMsg_Normal), (TEXT( "OnPlayClose\r\n" ) ));
	return error;
}

int RemoveRecordBuffer( int toremove )
{
	PWAVEHDR g_pwh;
	int error = 0;
	EnterCriticalSection( &Critical );
	LOCK_GSI(WAPI_IN);
	if( gsi[WAPI_IN].bStarted == TRUE )
	{				
		g_pwh = gsi[WAPI_IN].pwh;				
		while( g_pwh && g_pwh->dwBytesRecorded != 0 )
			g_pwh = g_pwh->lpNext;
		while( toremove-- && g_pwh )
		{		
			memcpy( g_pwh->lpData , RecordTemp[RecordOver] , g_pwh->dwBufferLength );
			RecordOver = (RecordOver+1)%4;
			g_pwh->dwBytesRecorded = g_pwh->dwBufferLength;
			MARK_BUFFER_DONE(g_pwh);
			g_pwh = g_pwh->lpNext;
			recordedcount ++;
		}
		RemoveCompleteBlocks(WAPI_IN);
	}
	else
	{
		AddDebugMsg( "record not started\n" );
		error = 1;
	}
	UNLOCK_GSI(WAPI_IN);
	LeaveCriticalSection( &Critical );
	return error;
}


int RemovePlayBuffer( int toremove )
{
	PWAVEHDR g_pwh;
	int error = 0;
	EnterCriticalSection( &Critical );
	LOCK_GSI(WAPI_OUT);
	if( gsi[WAPI_OUT].bStarted == TRUE )
	{				
		g_pwh = gsi[WAPI_OUT].pwh;				
		while( g_pwh && g_pwh->dwBytesRecorded == g_pwh->dwBufferLength)
			g_pwh = g_pwh->lpNext;
		while( toremove-- && g_pwh )
		{				
			g_pwh->dwBytesRecorded = g_pwh->dwBufferLength;
			g_pwh = g_pwh->lpNext;
			playedcount ++;
		}
		MarkFullAsDone(WAPI_OUT);
		InRemove = 1;
		RemoveCompleteBlocks(WAPI_OUT);						
		InRemove = 0;
	}
	else
	{
		AddDebugMsg( "play not started\n" );
		error = 1;
	}
	UNLOCK_GSI(WAPI_OUT);
	LeaveCriticalSection( &Critical );
	return error;
}

int AddRecordBuffer( int* left , int* morebuffer)
{
	PWAVEHDR g_pwh;
	int error = 0;
	LOCK_GSI(WAPI_IN);
	EnterCriticalSection( &Critical );
	if( gsi[WAPI_IN].bStarted == TRUE )
	{
		g_pwh = gsi[WAPI_IN].pwh;				
		while( g_pwh && g_pwh->dwBytesRecorded != 0 )
			g_pwh = g_pwh->lpNext;
		if( g_pwh )
		{
			StartRecordDMA( RecordTemp[RecordIndex] , g_pwh->dwBufferLength , left );
			RecordIndex = (RecordIndex+1)%4;
			if( g_pwh->lpNext )
				*morebuffer = 1;
			else
				*morebuffer = 0;
			RETAILMSG( (DebugMsgMask&DebugMsg_Trace) , (TEXT("aaci : addrecord : %x , %x\r\n")  ,g_pwh->lpData , g_pwh->dwBufferLength));
		}
		else
		{
			error = 2;
			*morebuffer = 0;
		}
	}
	else
	{
		//DMARecordCmd = DMA_RECORD_STOP;
		error = 1;
	}
	UNLOCK_GSI(WAPI_IN);
	LeaveCriticalSection( &Critical );
	return error;
}

int AddPlayBuffer( int* left )
{
	PWAVEHDR g_pwh;
	int error = 0;
	EnterCriticalSection( &Critical );
	LOCK_GSI(WAPI_OUT);
	if( gsi[WAPI_OUT].bStarted == TRUE )
	{
		g_pwh = gsi[WAPI_OUT].pwh;				
		while( g_pwh && g_pwh->dwBytesRecorded != 0)
			g_pwh = g_pwh->lpNext;
		if( g_pwh )
		{
			DWORD bufphy;
			int size = g_pwh->dwBufferLength;
			g_pwh->dwBytesRecorded = size/2;					
			GetPlayBuffer( &bufphy );
			StartPlayDMA( bufphy , size , left );
		}
		else
			error = 2;
	}
	else
	{
	//	DMAPlayCmd = DMA_PLAY_STOP;
		error = 1;
	}
	UNLOCK_GSI(WAPI_OUT);
	LeaveCriticalSection( &Critical );
	return error;
}

int OnPlayDmaFail()
{
	{
		GET_ERROR_PARAMS p;
		GET_ERROR_RESULT r;
		KernelIoControl( IOCTL_HAL_GET_ERROR_STATUS , &p , sizeof(GET_ERROR_PARAMS) , &r , sizeof(GET_ERROR_RESULT) , 0);
		RETAILMSG( 1 , (TEXT("OnPlayDmaFail %x\r\n" ) , r.ucErrorStatus ));	
	}
	{
		CLEAR_ERROR_PARAMS p;
		CLEAR_ERROR_RESULT r;
		KernelIoControl( IOCTL_HAL_CLEAR_ERROR_STATUS, &p , sizeof(CLEAR_ERROR_PARAMS) , &r , sizeof(CLEAR_ERROR_RESULT) , 0);
	}
	{
		int param1 = 1 , param2 = 0;
		KernelIoControl( HAL_IOCTL_AACI_CLEARSTATUS , &param1 , sizeof(int) , &param2 , sizeof(int) , 0);
	}
	return 0;
}

#define RecordBufferCount 1
DWORD AudioRecordThread( LPVOID p )
{
	int leftrecordblock = 0 , bufferedrecordblock = 0 , moreinputblock = 0 , error = 0;
	int loopcount = 0  ,temp;
	SetProcPermissions( 0xFFFFFFFF );
	RETAILMSG( (DebugMsgMask&DebugMsg_Important ), (TEXT( "AACI : DMA : Enter record Thread\r\n" ) ));

	if(0)
	{
		int time = GetTickCount() ;
		if( (loopcount++%1000) == 0 )
			RETAILMSG( 1 , (TEXT("AudioRecordThread loop %d\r\n" ) , bufferedrecordblock ));		
		while( !error && DMARecordCmd != DMA_RECORD_EXIT )
		{
			if( (loopcount++%1000) == 0 )
				RETAILMSG( 1 , (TEXT("AudioRecordThread loop %d\r\n" ) , bufferedrecordblock ));
			if( DMARecordCmd == DMA_RECORD_STOP || moreinputblock == 0 )
				WaitForSingleObject( DmaRecordEvent , INFINITE );
			if( (DMARecordCmd != DMA_RECORD_START) )
				continue;
			if( AddRecordBuffer( &leftrecordblock , &moreinputblock ) == 0 )
			{
				while( GetTickCount() - time < 20 )
				{
					Sleep( 5 );
				}
				time = GetTickCount();
				RemoveRecordBuffer(1);
			}
		}
	}

	while( !error && DMARecordCmd != DMA_RECORD_EXIT )
	{
		if( (loopcount++%1000) == 0 )
			RETAILMSG( 1 , (TEXT("AudioRecordThread loop %d\r\n" ) , bufferedrecordblock ));		
		
		if( DMARecordCmd == DMA_RECORD_STOP )
		{
			int param1 = 0 , param2 = 1;
			KernelIoControl( HAL_IOCTL_AACI_CLEARSTATUS , &param1 , sizeof(int) , &param2 , sizeof(int) , 0);
			bufferedrecordblock = 0;
			RETAILMSG( (DebugMsgMask&DebugMsg_Trace) , (TEXT( "AACI : record : stoped \r\n" ) ));
		}
		
		if(  DMARecordCmd == DMA_RECORD_STOP || moreinputblock == 0  )
		{
			WaitRecordBuffer = 1;
			WaitForSingleObject( RecordCmdEvent , INFINITE );
			WaitRecordBuffer = 0;
		}

		if( bufferedrecordblock && ( bufferedrecordblock == RecordBufferCount||moreinputblock==0) )
		{
			RETAILMSG( (DebugMsgMask&DebugMsg_Trace) , (TEXT( "AACI : record : wait %d %d\r\n" ), bufferedrecordblock , moreinputblock ));
			if( WaitForSingleObject( DmaRecordEvent , INFINITE ) == 0 )
				InterruptDone( RecordDmaIntID );
			AddDebugMsg( "wait record dma over\n" );
		}
		if( (DMARecordCmd != DMA_RECORD_START) )
			continue;
		RETAILMSG( (DebugMsgMask&DebugMsg_Trace) , (TEXT( "AACI : DMA : start work\r\n" ) ));
		AddDebugMsg( "to get record status \n" );
		EnterCriticalSection( &Critical );
		KernelIoControl( HAL_IOCTL_AACI_GETBUFFER , &temp , sizeof(int) , &leftrecordblock , sizeof(int) , 0);
		LeaveCriticalSection( &Critical );
		AddDebugMsg("get record status %d %d %d\n" , leftrecordblock, bufferedrecordblock , temp);
		if( DMARecordCmd == DMA_RECORD_START )
			if( (leftrecordblock!=bufferedrecordblock) || bufferedrecordblock < RecordBufferCount) 
			{
				RETAILMSG( (DebugMsgMask&DebugMsg_Trace) , (TEXT("aaci : to add buffer %d %d\r\n") , bufferedrecordblock, leftrecordblock ));				
				AddDebugMsg( "to add record\n" );
				if( AddRecordBuffer( &leftrecordblock , &moreinputblock ) == 0 )
				{
					bufferedrecordblock ++;
					AddDebugMsg( "add record over\n" );
				}
				if( moreinputblock == 0 )
					RETAILMSG( (DebugMsgMask&DebugMsg_Normal), (TEXT("aaci : record : no more input block %d\r\n" ) , moreinputblock));
			}
				
		if( leftrecordblock < bufferedrecordblock )
		{
			int toremove = 1;//bufferedrecordblock - leftrecordblock;
			AddDebugMsg( "to remove record\n" );
			if( RemoveRecordBuffer( 1 ) == 0 )
			{
				bufferedrecordblock --;
				AddDebugMsg( "remove record over\n" );
			}
			
		}	
	}
	RETAILMSG( (DebugMsgMask&DebugMsg_Important ), (TEXT( "AACI : DMA : leave record Thread\r\n" ) ));
	return 0;

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -