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

📄 voip_raw_data_file.c

📁 telcobridges voip develop
💻 C
📖 第 1 页 / 共 5 页
字号:
		rewind( pFile );

		/* The buffer size must be a multiple of the packet size to prevent */
		/* sending an incomplete packet (it would be dropped because it is invalid) */
		/* allocate buffer for pattern data */
		if( un32FileSize % VOIP_RAW_DATA_FILE_PACKET_SIZE )
		{
			TBX_UINT32	un32PacketCount;
			/* Align buffer */
			un32PacketCount = (un32FileSize / VOIP_RAW_DATA_FILE_PACKET_SIZE) + 1;
			in_pPlayContext->un32DataSize  = un32PacketCount * VOIP_RAW_DATA_FILE_PACKET_SIZE;
		}
		else
		{
			in_pPlayContext->un32DataSize = un32FileSize;
		}


		in_pPlayContext->pData = malloc( in_pPlayContext->un32DataSize );
		if( !in_pPlayContext->pData )
		{
			TBX_EXIT_ERROR( TBX_RESULT_FAIL, 0, "Not enough memory to allocate buffer for data!" );
		}
		memset( in_pPlayContext->pData, 0, in_pPlayContext->un32DataSize );

		/* Fill data buffer */
		un32ByteRead = fread( in_pPlayContext->pData, 1, in_pPlayContext->un32DataSize, pFile );
		if( un32ByteRead != un32FileSize )
		{
			TBX_EXIT_ERROR( TBX_RESULT_FAIL, 0, "Error while reading data file!" );
		}
		fclose( pFile );
		pFile = NULL;

		Result = VoipRawDataFileStartPlayInternal( in_pPlayContext );
		if( TBX_RESULT_FAILURE( Result ) == TBX_TRUE )
		{
			TBX_EXIT_ERROR( Result, 0, "Failed to start raw data file play!" );
		}

		in_pPlayContext->fStarting = TBX_TRUE;

		/* End of the code (skip to cleanup) */
		TBX_EXIT_SUCCESS (TBX_RESULT_OK);
	}

	/*---------------------------------------------------------------------------------------------------------------------------
	 |  Error handling section
	 *--------------------------------------------------------------------------------------------------------------------------*/
	ERROR_HANDLING
	{
		/* Print the error to the user */
		TbxCliToolsLogPrint(
			pCliContext->hCliTools,
			TRACE_LEVEL_ERROR,
			NULL,
			"VoipRawDataFileStartPlay: %s (Result 0x%08X, %s, line %d)\n",
			TBX_ERROR_DESCRIPTION,
			TBX_ERROR_RESULT,
			__FILE__,
			TBX_ERROR_LINE);
	}

	/*---------------------------------------------------------------------------------------------------------------------------
	 |  Cleanup section
	 *--------------------------------------------------------------------------------------------------------------------------*/
	CLEANUP
	{
	}

	RETURN;
}

static TBX_RESULT VoipRawDataFileStartPlayInternal(
	IN		PVOIP_RAW_DATA_FILE_PLAY_CONTEXT	in_pPlayContext)
{
	TBX_RESULT							Result = TBX_RESULT_OK;
	PVOIP_CLI_CONTEXT					pCliContext;
		TBX_STREAM_LIB_PLAY_PARAMETERS	PlayParameters;
		TBX_UINT16						un16Port;
		TBX_SOCKADDR					SockAddr0;
		TBX_SOCKADDR					SockAddr1;

	/*---------------------------------------------------------------------------------------------------------------------------
	 |  Code section
	 *--------------------------------------------------------------------------------------------------------------------------*/
	CODE
	{
		/* Initialize local variables */
		pCliContext		= &g_pContext->CliContext;

		/***************************************/
		/* Start playing data using stream lib */
		/***************************************/
		
		/* Initialize the play parameters */
		memset( &PlayParameters, 0, sizeof( PlayParameters ) );
		PlayParameters.un32StructVersion		= 1;
		PlayParameters.PacketSize				= VOIP_RAW_DATA_FILE_PACKET_SIZE;
		PlayParameters.PacketType				= TBX_STREAM_LIB_PACKET_TYPE_PCMA;
		PlayParameters.fUseDoubleBuffering		= TBX_TRUE;
		PlayParameters.fAllowIncompletePackets	= TBX_FALSE;
		PlayParameters.fExternalData			= TBX_TRUE;
		PlayParameters.RedundancyMode			= TBX_STREAM_NETWORK_REDUNDANCY_MODE_SWITCH;
		PlayParameters.un32StreamBitRate		= TBX_STREAM_LIB_BIT_RATE_64KBPS;

		un16Port					= in_pPlayContext->un16Port;

		SockAddr0.sin_family		= PF_INET;
		SockAddr0.sin_port			= htons( un16Port );
		SockAddr0.sin_addr.s_addr	= TBX_INET_ADDR( in_pPlayContext->szIpAddress0 );
		if( SockAddr0.sin_addr.s_addr == 0xFFFFFFFF )
			SockAddr0.sin_addr.s_addr = 0;

		SockAddr1.sin_family		= PF_INET;
		SockAddr1.sin_port			= htons( un16Port );
		SockAddr1.sin_addr.s_addr	= TBX_INET_ADDR( in_pPlayContext->szIpAddress1);
		if( SockAddr1.sin_addr.s_addr == 0xFFFFFFFF )
			SockAddr1.sin_addr.s_addr = 0;

		Result = TBXStreamPlay
		(
			g_pContext->hStreamLib,
			in_pPlayContext->un64Id,
			&PlayParameters,
			SockAddr0.sin_addr.s_addr ? &SockAddr0 : NULL,
			SockAddr1.sin_addr.s_addr ? &SockAddr1 : NULL,
			&( in_pPlayContext->hPlay )
		);
		if (TBX_RESULT_FAILURE (Result))
		{
			TBX_EXIT_ERROR( Result, 0, "Error while trying to play stream!" );
		}

		/* End of the code (skip to cleanup) */
		TBX_EXIT_SUCCESS (TBX_RESULT_OK);
	}

	/*---------------------------------------------------------------------------------------------------------------------------
	 |  Error handling section
	 *--------------------------------------------------------------------------------------------------------------------------*/
	ERROR_HANDLING
	{
		/* Print the error to the user */
		TbxCliToolsLogPrint(
			pCliContext->hCliTools,
			TRACE_LEVEL_ERROR,
			NULL,
			"VoipRawDataFileStartPlay: %s (Result 0x%08X, %s, line %d)\n",
			TBX_ERROR_DESCRIPTION,
			TBX_ERROR_RESULT,
			__FILE__,
			TBX_ERROR_LINE);
	}

	/*---------------------------------------------------------------------------------------------------------------------------
	 |  Cleanup section
	 *--------------------------------------------------------------------------------------------------------------------------*/
	CLEANUP
	{
	}

	RETURN;
}

/*--------------------------------------------------------------------------------------------------------------------------------
 |
 |  VoipRawDataFileStopPlay	:	Stop playing an audio file for the specified raw data file
 |
 |	in_un32PromptId				:	Identifier of the raw data file to stop playing.
 |
 |  Note						:	~
 |
 |  Return						:	TBX_RESULT_OK if the function succeeded
 |									Other error code if the function could not complete properly.
 |
 *------------------------------------------------------------------------------------------------------------------------------*/
TBX_RESULT VoipRawDataFileStopPlay(
	IN		PVOIP_RAW_DATA_FILE_PLAY_CONTEXT	in_pPlayContext)
{
	TBX_RESULT							Result = TBX_RESULT_OK;
	PVOIP_CLI_CONTEXT					pCliContext;

	/*---------------------------------------------------------------------------------------------------------------------------
	 |  Code section
	 *--------------------------------------------------------------------------------------------------------------------------*/
	CODE
	{
		/* Initialize local variables */
		pCliContext		= &g_pContext->CliContext;


		Result = VoipRawDataFileStopPlayInternal( in_pPlayContext );
		if( TBX_RESULT_FAILURE( Result ) == TBX_TRUE )
		{
			TBX_EXIT_ERROR( Result, 0, "Failed to stop raw data file play!" );
		}

		/* End of the code (skip to cleanup) */
		TBX_EXIT_SUCCESS (TBX_RESULT_OK);
	}

	/*---------------------------------------------------------------------------------------------------------------------------
	 |  Error handling section
	 *--------------------------------------------------------------------------------------------------------------------------*/
	ERROR_HANDLING
	{
		/* Print the error to the user */
		TbxCliToolsLogPrint(
			pCliContext->hCliTools,
			TRACE_LEVEL_ERROR,
			NULL,
			"VoipRawDataFileStopPlay: %s (Result 0x%08X, %s, line %d\n",
			TBX_ERROR_DESCRIPTION,
			TBX_ERROR_RESULT,
			__FILE__,
			TBX_ERROR_LINE);
	}

	/*---------------------------------------------------------------------------------------------------------------------------
	 |  Cleanup section
	 *--------------------------------------------------------------------------------------------------------------------------*/
	CLEANUP
	{
	}

	RETURN;
}

TBX_RESULT VoipRawDataFileStopPlayInternal(
	IN		PVOIP_RAW_DATA_FILE_PLAY_CONTEXT	in_pPlayContext )
{
	TBX_RESULT							Result = TBX_RESULT_OK;
	PVOIP_CLI_CONTEXT					pCliContext;

	/* Initialize local variables */
	pCliContext = &g_pContext->CliContext;

	/* Validate current state */
	if
	( 
		!(
			in_pPlayContext->fStarting	||
			in_pPlayContext->fPlaying
		) ||
		in_pPlayContext->fStopping
	)
	{
		TbxCliToolsLogPrint(
			pCliContext->hCliTools,
			TRACE_LEVEL_ERROR,
			NULL,
			"ERROR: Invalid state to stop play of raw data file 0x%08X\n",
			in_pPlayContext->szPathName );
		Result = TBX_RESULT_INVALID_STATE;
	}
	else
	{
		/* Stop streamlib  */
		if( in_pPlayContext->hPlay != (TBX_STREAM_LIB_PLAY_HANDLE)TBX_HANDLE_INVALID )
		{
			TBXStreamStop( g_pContext->hStreamLib, in_pPlayContext->hPlay );
		}

		in_pPlayContext->fStopping	= TBX_TRUE;
		in_pPlayContext->fPlaying	= TBX_FALSE;
		in_pPlayContext->fStarting	= TBX_FALSE;
	}

	return Result;
}

/*--------------------------------------------------------------------------------------------------------------------------------
 |
 |  VoipRawDataFileGetKey	:	Function called by the hash table to retrieve the Key (user context)
 |										from the element stored in the hash table (pointer to the stream server raw data file
 |										context structure)
 |
 |  Note						:	
 |								
 |  Return						:	
 |
 *------------------------------------------------------------------------------------------------------------------------------*/
static TBX_HASH_KEY	VoipRawDataFileGetKey(
	IN		PTBX_VOID	in_pUserContext,
	IN		PTBX_VOID	in_pElement )
{
	PVOIP_STREAM_SERVER_PROMPT_CONTEXT	pRawDataFile = (PVOIP_STREAM_SERVER_PROMPT_CONTEXT)in_pElement;

	(TBX_VOID)in_pUserContext;

	return (TBX_HASH_KEY)pRawDataFile->un32PromptId;
}

/*--------------------------------------------------------------------------------------------------------------------------------
 |
 |  VoipRawDataFilePlayClear	:	Clear the runtime play context for the specified raw data file
 |
 |	in_un32PromptId				:	Identifier of the raw data file to clear playing context.
 |
 |  Note						:	~
 |
 |  Return						:	TBX_RESULT_OK if the function succeeded
 |									Other error code if the function could not complete properly.
 |
 *------------------------------------------------------------------------------------------------------------------------------*/
TBX_RESULT VoipRawDataFilePlayClear(
	IN		PVOIP_RAW_DATA_FILE_PLAY_CONTEXT	in_pPlayContext)
{
	TBX_RESULT							Result = TBX_RESULT_OK;
	PVOIP_CLI_CONTEXT					pCliContext;

	/*---------------------------------------------------------------------------------------------------------------------------
	 |  Code section
	 *--------------------------------------------------------------------------------------------------------------------------*/
	CODE
	{
		/* Initialize local variables */
		pCliContext		= &g_pContext->CliContext;


		memset( in_pPlayContext, 0, sizeof( *in_pPlayContext ) );
		
		/* End of the code (skip to cleanup) */
		TBX_EXIT_SUCCESS (TBX_RESULT_OK);
	}

	/*---------------------------------------------------------------------------------------------------------------------------
	 |  Error handling section
	 *--------------------------------------------------------------------------------------------------------------------------*/
	ERROR_HANDLING
	{
		/* Print the error to the user */
		TbxCliToolsLogPrint(
			pCliContext->hCliTools,
			TRACE_LEVEL_ERROR,
			NULL,
			"VoipRawDataFilePlayClear: %s (Result 0x%08X, %s, line %d\n",
			TBX_ERROR_DESCRIPTION,
			TBX_ERROR_RESULT,
			__FILE__,
			TBX_ERROR_LINE);
	}

	/*---------------------------------------------------------------------------------------------------------------------------
	 |  Cleanup section
	 *--------------------------------------------------------------------------------------------------------------------------*/
	CLEANUP
	{
	}

	RETURN;
}

/*--------------------------------------------------------------------------------------------------------------------------------
 |
 |  VoipRawDataFileRecordAlloc	:	Function that allocates the runtime context to record a raw data file
 |
 |	in_un16Port					:	UDP port to use for stream
 |	in_pszIpAddress0			:
 |	in_pszPathName				:
 |	in_pun32PromptId			:
 |	in_un32NbRepeat				:	Number of time the raw data file will restart recording
 |
 |  Note						:	
 |
 |  Return						:	The allocated raw data file context, or NULL if out of resources
 |

⌨️ 快捷键说明

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