#include #include #include #include #include #include #include void Die(char *mess) { perror(mess); exit(1); } typedef struct { long ChunkID; long ChunkSize; long Format; } WaveRiffChunk; typedef struct { long chunkID; long chunkSize; short wFormatTag; unsigned short wChannels; unsigned long dwSamplesPerSec; unsigned long dwAvgBytesPerSec; unsigned short wBlockAlign; unsigned short wBitsPerSample; } WavFormatChunk; typedef struct { long chunkID; long chunkSize; unsigned char waveformData[]; } WavDataChunk; //this is a client that establishes a TCP session with another client int main(int argc, char *argv[]) { GetAudio(1); //seconds GetAudio(1); //seconds GetAudio(1); //seconds } //main int GetAudio(int Seconds) { #define SampleRate 8000 //8000Htz #define SampleSize 8 //8 bit sample #define Channels 1 //Mono #define BUFFSIZE 2048 #define SendStr "GET /cgi-bin/getaudio.cgi HTTP/1.1\r\nHost: 192.168.0.106\r\nUser-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.7.10)\r\nGecko/20050716 Firefox/1.0.6\r\nAccept: image/png,*/*;q=0.5\r\nAccept-Language: en-us,en;q=0.5\r\nAccept-Encoding: gzip,deflate\r\nAccept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7\r\nKeep-Alive: 300\r\nConnection:\r\nkeep-alive\r\nIf-Modified-Since: Sat, 01 Jan 2000 18:09:09 GMT\r\nCache-Control: max-age=0\r\n\r\n" #define Detect 1 //Only record changes #define Diff 3 //Difference between last sample int sock; struct sockaddr_in echoserver; unsigned char buffer[BUFFSIZE]; unsigned int echolen; int status=0,bytes=0,numbytes,pasthtml; char ip[20],port[10]; FILE *fptr; int wend,wend2,tcnt,skip2; WaveRiffChunk wrc; WavFormatChunk wfc; WavDataChunk wdc; wrc.ChunkID=0x46464952; //RIFF; wrc.ChunkSize=4+sizeof(WavFormatChunk)+sizeof(WavDataChunk)+SampleRate*(SampleSize>>3)*Seconds; //filesize-8 wrc.Format=0x45564157; //WAVE //make .wav file header wfc.chunkID=0x20746d66; //fmt wfc.chunkSize=sizeof(WavFormatChunk)-8; wfc.wFormatTag=1; //PCM=1, others=compression wfc.wChannels=Channels; wfc.dwSamplesPerSec=SampleRate; wfc.dwAvgBytesPerSec=Channels*SampleRate*(SampleSize>>3); wfc.wBlockAlign=Channels*(SampleSize>>3); //bytes per sample=Channels*SampleSize wfc.wBitsPerSample=8; wdc.chunkID=0x61746164; //data wdc.chunkSize=Channels*SampleRate*(SampleSize>>3)*Seconds; //open .wav file and write header fptr=fopen("out.wav","wb"); fwrite(&wrc,sizeof(wrc),1,fptr); fwrite(&wfc,sizeof(wfc),1,fptr); fwrite(&wdc,8,1,fptr); //by default this computer uses a port such as 49342 to send strcpy(ip,"192.168.0.106"); strcpy(port,"80"); //1047 80 /* Create the TCP socket */ if ((sock = socket(PF_INET,SOCK_STREAM,IPPROTO_TCP)) < 0) { Die("Failed to create socket\n"); } /* Construct the server sockaddr_in structure */ memset(&echoserver, 0, sizeof(echoserver)); /* Clear struct */ echoserver.sin_family = AF_INET; /* Internet/IP */ echoserver.sin_addr.s_addr = inet_addr(ip); /* IP address */ echoserver.sin_port = htons(atoi(port)); /* server port */ /* Establish connection */ if (connect(sock,(struct sockaddr *)&echoserver,sizeof(echoserver))<0) { Die("Failed to connect with server\n"); } //Send the HTTP request for a audio data if (write(sock,SendStr,sizeof(SendStr))!= sizeof(SendStr)) { Die("Failed to send bytes to client"); } // printf("%d: %s",bytes,buffer); numbytes=0; pasthtml=0; wend=0; wend2=0; skip2=0; //audio will continue until stopped //before and after every 2000 bytes are 2 byte markers //ffa8=before //ffa9=after while ((status=read(sock,buffer,sizeof(buffer)-1))>0 && !wend) { //printf("%d: %s", status, buffer); // printf("%d\t%d\n",status,numbytes); // fwrite(buffer,status,1,fptr); if (pasthtml<3) { //remove HTML header //basically 0d0a0d0a is the end of the header bytes=0; while(bytes3 /* //brute force method tcnt=0; while(status>0) { if (bytes>2000 && bytes<2004) { fwrite(buffer+tcnt,1,1,fptr); bytes++; } else { bytes=0; } tcnt++; status--; } */ if (skip2) { //skip the first 2 bytes 0xff 0xa8 //printf("skip2 %2x %2x\n",buffer[0],buffer[1]); if (bytes>0) bytes-=2; memcpy(buffer,buffer+2,status-2); status-=2; skip2=0; } //This are complicated because the data is not just sent raw, //but is sent with a 2 byte header and 2 byte tailer //that come in variable size packets (but mostly in 2 packets). //The data goes 0xff 0xa8 <2000 bytes of data (.25s)> 0xff 0xa9 //So I simply add up the bytes read, and at 2000 subtract ffa9ffa8 //and start the byte count at 0 again //but added complexity occurs because sometimes //and extra fff9 will be read at 4006 bytes 2000+4+2000+2 // // printf("b=%d\ts=%d\tb+s=%d\n",bytes,status,bytes+status); //does this data contain 2001,2002,2003 or 2004? //if yes process, if no write all //process=write before 2001 and after 2004 if ((bytes+status)>2000) { //contains 2001-2004, process //write all before 2001 and all after 2004 //if bytes before 2000 write //if (bytes==2002) skip2=1; if (bytes<2000) { fwrite(buffer,2000-bytes,1,fptr); // printf("buffer=ffa9? %2x %2x\n",buffer[2000-bytes],buffer[2000-bytes+1]); } //if bytes after 2004 write if ((bytes+status)>2004) { // printf("buffer2=ffa8? %2x %2x\n",buffer[2000-bytes+2],buffer[2000-bytes+3]); if (bytes+status==4006) {//special case of 2001,2002 at end fwrite(buffer+2004-bytes,bytes+status-2006,1,fptr); // printf("spec=ffa9? %2x %2x\n",buffer[4006-bytes-2],buffer[4006-bytes-1]); bytes=0; skip2=1; } else { fwrite(buffer+2004-bytes,bytes+status-2004,1,fptr); bytes=bytes+status-2004; } } else bytes+=status; } else { //not >2000 doesn't contain 2001-2004 simply write fwrite(buffer,status,1,fptr); bytes+=status; } /* //depending on where bytes is relative to 2001,2002,2003,2004 (or possibly 1,2 if ((bytes+status)<2002) { fwrite(buffer+drop2,status-drop2,1,fptr); bytes+=status; drop2=0; } else { //<2002 if (bytes+status)==2002) { fwrite(buffer,status-2,1,fptr); //drop last 2 bytes bytes=0; drop2=2; } //==2002 else { if (bytes+status)>2002) { //2004+ drop 4 bytes fwrite(buffer,status-2,1,fptr); //drop last 2 bytes bytes=0; drop2=0; } //>2002 } //==2002 } //<2002 */ /* if (bytes==0) { //in theory it could land on 1, but doesn't in practice fwrite(buffer+2,status-2,1,fptr); } else { if ((bytes+status)>2001) { printf("1:b=%d s=%d bytes+status=%d\n",bytes,status,bytes+status); fwrite(buffer,status-(bytes-2000),1,fptr); if (bytes+status!=2002) { fwrite(buffer+status-(bytes-2002),bytes+status-2002,1,fptr); } bytes-=2002; } //bytes>2000 else { fwrite(buffer,status,1,fptr); //somewhere between 0 and 2000 } } //bytes==0 */ /* //remove marker every 2000 bytes ( if (buffer[0]==0xff && buffer[1]==0xa8) fwrite(buffer+2,status-2,1,fptr); else //is second half that ends in 0xa9 fwrite(buffer,status-2,1,fptr); */ /* bytes+=status; printf("2:b=%d s=%d bytes+status=%d\n",bytes,status,bytes+status); */ } //else pasthtml>3 numbytes+=status; if (numbytes>SampleRate*Seconds) wend=1; //64kb = 8khz 8-bit //if (numbytes>1000) wend=1; } //end while fclose(fptr); // bytes=recv(sock,buffer,BUFFSIZE-1,0); // bytes = recv(sock,buffer,BUFFSIZE,0); //bytes = recv(sock,buffer,1,0); //bytes=read(sock,buffer,1); // printf("%d: %s",bytes,buffer); // fprintf(stdout,"done\n"); close(sock); exit(0); } //end GetAudio