linux - Sending sound over Network Ubuntu and C -
i trying send sound microphone on network in c kind of networked baby monitor. have got server recording sound life of me cannot play on client side. code horrible mess , appologise have far
server
int main (int argc, const char * argv[]) { printf("server\n"); int server_sock_fd; //file descriptor server socket int client_sock_fd; //file descriptor client socket socklen_t server_len, client_len; struct sockaddr_in server_address; struct sockaddr_in client_address; //remove old sockets , re-create unlink("server_socket"); //obtain file descriptor socket server_sock_fd = socket(af_inet, sock_stream, 0); //name socket , set properties server_address.sin_family = af_inet; //this internet protocol server_address.sin_addr.s_addr = htonl(inaddr_any); //accept address (note conversion function) server_address.sin_port = htons(9734); //remember use host-to-network-type conversion server_len = sizeof(server_address); bind(server_sock_fd, (struct sockaddr*)&server_address, server_len); //bind socket address //create connection queue , define max number of client connections listen(server_sock_fd, 1); //main loop (ctrl-c quit application) while (1) { char ch; printf("server waiting\n"); client_len = sizeof(client_address); //accept incoming client connection , create client socket //returns file descriptor each connection in queue (blocks if queue empty) client_sock_fd = accept(server_sock_fd, (struct sockaddr*)&client_address, &client_len); //now can read , write data using standard read , write functions //note socket treated file read/write permissions //we read socket if file - note client //read(client_sock_fd, &ch, 1); //read single byte //ch++; //build command string char strcommand[64]; sprintf(strcommand, "arecord -q -c 1 -t raw -f s16_le -r %u -d plughw:0,0", sampling_rate); printf("issuing command %s\n", strcommand); //open arecord child process read (stdout directed pipe) inputstream = popen(strcommand,"r"); if (inputstream == null) { perror("cannot open child process record"); exit(1); } //we c stream - prefer use unix file descriptor fidin = fileno(inputstream); //read finite block of data buffer char* pbuffer = buf; int isamplesread; ibytesread = 0; while (ibytesread < buffer_size_in_bytes) { //grab block samples isamplesread = read(fidin, pbuffer, buffer_size_in_bytes); //4000 max 1 hit //it actual number of samples read not many wanted if (isamplesread > 0) { ibytesread += isamplesread; pbuffer += isamplesread; printf("%d ", isamplesread); } else { break; } } //all done, close child process pclose(inputstream); //we can send response //write(client_sock_fd, &ch, 1); //write single byte //now close client socket (we have finished) close(client_sock_fd); } return 0; }
client
int main (int argc, const char * argv[]) { printf("client\n"); int socket_fd; //file descriptor int len; struct sockaddr_in address; int result; char ch = 'a'; //create client socket socket_fd = socket(af_inet, sock_stream, 0); //name socket - align server address.sin_family = af_inet; address.sin_addr.s_addr = inet_addr("127.0.0.1"); address.sin_port = htons(9734); len = sizeof(address); //connect client socket server socket result = connect(socket_fd, (struct sockaddr*)&address, len); if (result == -1) { printf("error - cannot connect socket\n"); exit(0); } //all done - can use socket if file //write(socket_fd, &ch, 1); //read(socket_fd, &ch, 1); //printf("character server = %u\n", ch); //open process pipe read (pipe stream uni-direction on linux) char strcommand[64]; char* pbuffer = buf; sprintf(strcommand, "aplay -q -c 1 -t raw -f s16_le -r %u", sampling_rate); printf("issuing command %s\n", strcommand); outputstream = popen(strcommand,"w"); if (outputstream == null) { perror("cannot open child process"); exit(1); } fidout = fileno(outputstream); //play audio ibyteswritten = 0; pbuffer = buf; int isampleswritten; fflush(outputstream); while (ibyteswritten < buffer_size_in_bytes) { isampleswritten = write(fidout, pbuffer, buffer_size_in_bytes); if (isampleswritten > 0) { ibyteswritten += isampleswritten; pbuffer += isampleswritten; printf("%d ", isampleswritten); } else { break; } } //close pipes , unblock child process pclose(outputstream); printf("address of buffer: %p\naddress of pointer: %p\ndelta = %d\n", buf, pbuffer, (pbuffer-buf)); return (exit_success); }
any or hints hugely appreciated!
i sure writing code fun :) can use gstreamer rtp accomplish this. either programmatically or through command line.
general documentation: http://gstreamer.freedesktop.org/documentation/rtp.html
actual example streaming audio using gstreamer rtp: http://delog.wordpress.com/2011/05/11/audio-streaming-over-rtp-using-the-rtpbin-plugin-of-gstreamer/
Comments
Post a Comment