Diff: ShoutcastConnector.cpp
- Revision:
- 5:36d14584e42f
- Parent:
- 4:a3b7032f0776
- Child:
- 6:e269a1b3bf4f
--- a/ShoutcastConnector.cpp Thu Jan 13 13:32:03 2011 +0000
+++ b/ShoutcastConnector.cpp Sat Jan 29 21:15:17 2011 +0000
@@ -4,9 +4,6 @@
enum wSTATE {NOP,PUT_HEADER} WriteState;
enum rSTATE {GET_RESPONSE,GET_STREAM} ReadState;
-
-char inbuf[BUFFER_Z]; // IP input buffer
-char sResponse[800]; // keeps Stream Response
bool play;
@@ -19,21 +16,24 @@
-ShoutcastConnector::ShoutcastConnector(VS1053* mp3, myCallBack_pfn callback) {
+ShoutcastConnector::ShoutcastConnector(VS1053* mp3, myCallBack_pfn callback, char *buffer, int buffer_size) {
_mp3 = mp3;
_callback = callback;
+ _buffer = buffer;
+ _buffer_size = buffer_size;
ReadState=GET_RESPONSE;
WriteState=PUT_HEADER;
}
-int ShoutcastConnector::Connect(IpAddr address, int port) {
+int ShoutcastConnector::Connect(IpAddr address, int port, char * path) {
if ((ReadState!=GET_RESPONSE)||(WriteState!=PUT_HEADER)) {
Disconnect();
//wait
wait_ms(500);
}
+ _path = path;
kb = 0;
readed = 0;
@@ -41,10 +41,8 @@
metaint=0; // metadate interval
play=false;
- for(int i=0; i< BUFFER_Z; i++)
- inbuf[i] = 0; // IP input buffer
- for(int i=0; i< 800; i++)
- sResponse[i] = 0; // IP input buffer
+ for(int i=0; i< _buffer_size; i++)
+ _buffer[i] = 0; // IP input buffer
Host server(address, port); // 128k mp3
IpAddr serverIp = server.getIp();
@@ -53,8 +51,8 @@
serverIp[0],serverIp[1],serverIp[2],serverIp[3],port);
- sock.setOnEvent(this, &ShoutcastConnector::onTCPSocketEvent);
- TCPSocketErr bindErr = sock.connect(server);
+ _sock.setOnEvent(this, &ShoutcastConnector::onTCPSocketEvent);
+ TCPSocketErr bindErr = _sock.connect(server);
if (TCPSOCKET_OK != bindErr) {
DEBUGOUT("ShoutcastConnector: Error %d in sock.connect().\r\n", bindErr);
@@ -64,7 +62,7 @@
}
int ShoutcastConnector::Disconnect() {
- TCPSocketErr err = sock.close();
+ TCPSocketErr err = _sock.close();
if (TCPSOCKET_OK != err) {
@@ -121,27 +119,23 @@
case TCPSOCKET_DISCONNECTED:
//Close socket...
DEBUGOUT("ShoutcastConnector: Disconnected\r\n");
- sock.close();
+ _sock.close();
break;
}// switch(e)
}
void ShoutcastConnector::Writeable() {
+
switch (WriteState) {
- case PUT_HEADER:
- const char* str =
-// "GET /live HTTP/1.0\r\n" // use this if solopiano
- "GET / HTTP/1.0\r\n"
- "Host: gw\r\n"
- "Accept: */*\r\n"
- "User-Agent: mbed\r\n"
-
- "Icy-MetaData: 1\r\n" // send this if you want Metadata
-// "Connection: close\r\n"
- "\r\n"
- ;
- sock.send(str, strlen(str));
- DEBUGOUT("\r\nShoutcastConnector: HEADER:\r\n%s\r\n", str); // display PUT_HEADER
+ case PUT_HEADER:
+ strcpy(_buffer,"GET ");
+ strcat(_buffer,_path);
+ strcat(_buffer, " HTTP/1.0\r\nHost: gw\r\nAccept: */*\r\nUser-Agent: mbed\r\nIcy-MetaData: 1\r\n\r\n");
+
+ DEBUGOUT("\r\nShoutcastConnector: HEADER:\r\n%s\r\n", _buffer); // display PUT_HEADER
+
+ _sock.send(_buffer, strlen(_buffer));
+
WriteState=NOP;
ReadState=GET_RESPONSE;
break;
@@ -157,20 +151,22 @@
switch (ReadState) {
case GET_RESPONSE:
- len=sock.recv(inbuf, sizeof inbuf);
- cp=strstr(inbuf,"\r\n\r\n");
+ for(int j=0; j<_buffer_size; j++)
+ _buffer[j] = 0x00;
+
+ len=_sock.recv(_buffer, _buffer_size);
+ cp=strstr(_buffer,"\r\n\r\n");
if (cp==NULL) {
- inbuf[len]=0;
- DEBUGOUT("%s",inbuf); // debug
- //sprintf(sResponse,"%s",inbuf);
+ _buffer[len]=0;
+ DEBUGOUT("%s", _buffer); // debug
return; // still read response again
}
//
*cp=0;
- sprintf(sResponse+strlen(sResponse),"%s\r\n\r\n",inbuf);
- DEBUGOUT("ShoutcastConnector: RESPONSE:\r\n%s",sResponse);
+
+ DEBUGOUT("ShoutcastConnector: RESPONSE:\r\n%s",_buffer);
// get metaint value
- cp=strstr(sResponse,"icy-metaint:");
+ cp=strstr(_buffer,"icy-metaint:");
if (cp==NULL) metaint=0;
else sscanf(cp+strlen("icy-metaint:"),"%d",&metaint);
DEBUGOUT("\r\nShoutcastConnector: metaint: %d\r\n\r\n",metaint); //debug
@@ -178,7 +174,7 @@
DEBUGOUT("ShoutcastConnector: HeaderLen:%d\r\n",len);
- i=strlen(inbuf)+4; // bump bitstream right after response
+ i=strlen(_buffer)+4; // bump bitstream right after response
ReadState=GET_STREAM;
//initDec();
//
@@ -192,7 +188,7 @@
//
case GET_STREAM:
// receive data ****repeatedly****
- while (len=sock.recv(inbuf, sizeof inbuf)) {
+ while ((len=_sock.recv(_buffer, _buffer_size)) != 0x00) {
kb += len;
@@ -202,11 +198,11 @@
if (readed + len > metaint) {
//XXX
int startindex = metaint-readed;
- size = inbuf[startindex];
+ size = _buffer[startindex];
//Send first part of data
while (_mp3->bufferFree() < startindex)
; // wait
- _mp3->bufferPutStream(inbuf, startindex);
+ _mp3->bufferPutStream(_buffer, startindex);
if (size>0) {
//1.Fall : (metaint-readed)+size <= len
@@ -218,11 +214,11 @@
for (int i = startindex + 1; i < startindex + 1 + size*16; i++) {
- if (inbuf[i] == '\'') {
+ if (_buffer[i] == '\'') {
if (start == -1)
start = i+1-(startindex+1);
else {
- if(inbuf[i+1] == ';')
+ if(_buffer[i+1] == ';')
{
length = (i - (startindex+1)) - start; //last indexs - first index
break;
@@ -232,12 +228,12 @@
}
}
- _callback(&inbuf[startindex + 1], size*16, start ,length);
+ _callback(&_buffer[startindex + 1], size*16, start ,length);
//Send last part of data
while (_mp3->bufferFree() < len-(startindex+1+size*16))
; // wait
readed = len-(startindex+1+size*16);
- _mp3->bufferPutStream(&inbuf[startindex+1+size*16], readed);
+ _mp3->bufferPutStream(&_buffer[startindex+1+size*16], readed);
} else {
DEBUGOUT("ShoutcastConnector: Muh")
}
@@ -248,19 +244,19 @@
; // wait
//XXX
readed = len-(startindex+1);
- _mp3->bufferPutStream(&inbuf[startindex+1], len-(startindex+1));
+ _mp3->bufferPutStream(&_buffer[startindex+1], len-(startindex+1));
}
} else {
readed += len;
while (_mp3->bufferFree() < len)
; // wait
- _mp3->bufferPutStream(inbuf, len);
+ _mp3->bufferPutStream(_buffer, len);
}
} else {
while (_mp3->bufferFree() < len)
; // wait
- _mp3->bufferPutStream(inbuf, len);
+ _mp3->bufferPutStream(_buffer, len);
}