/**
 * audio_decoder_mp3_http.c: The mp3 decoder via http
 * 
 * Copyright: See COPYING file that comes with this distribution
 *
 * Author: Matthias Feistel <hitman_47@users.sourceforge.net>, (C) 2007
 */

#include "audio_decoder_mp3_http.h"
#include <vdr/tools.h> // DELETENULL
#include <sstream>

cHttpMp3Decoder::cHttpMp3Decoder( ISocketHandler& h ) :
	HttpGetSocket(h )
{
	mo_NETBuffer = new cSimpleRingBuffer( NET_BUFFER_SIZE );
}

cHttpMp3Decoder::~cHttpMp3Decoder( )
{
	if( mo_NETBuffer )
		DELETENULL( mo_NETBuffer );
}

void cHttpMp3Decoder::OnRead( void )
{
	int n = 0;
	unsigned char readBuffer[READBUFFSIZE];
	unsigned char *readBuffer_Ptr;
	
	readBuffer_Ptr = readBuffer;
	
	if( mo_NETBuffer && mo_NETBuffer->Free( ) > mo_NETBuffer->Size( )/8 )
	{
		n = recv( GetSocket( ), readBuffer_Ptr, READBUFFSIZE, MSG_NOSIGNAL );
		
		if( n == 0 )
		{
			SetConnected( false );
			return;
		}

		mo_NETBuffer->Put( (unsigned char*)readBuffer_Ptr, n );
	}

}

unsigned long cHttpMp3Decoder::GetDataFromSource( uint8_t *data,
		const unsigned long count )
{
	return mo_NETBuffer->Get( data, count );
}

std::string cHttpMp3Decoder::MyUseragent( void )
{
	std::stringstream buff_stream;
	buff_stream << PLUGIN_NAME << "/"
			<< PLUGIN_VERSION;
	
	return buff_stream.str( );
}

void cHttpMp3Decoder::OnDelete( void )
{
	SetConnected( false );
	OnContent();
}
