通过网络Ubuntu和C发送声音

时间:2014-03-24 14:53:38

标签: c linux ubuntu

我正试图通过C网络中的麦克风发送声音,作为一种联网的婴儿监视器。 我有服务器录制声音,但对于我的生活不能让它在客户端播放。 我的代码是一个可怕的混乱我告诫,但这是我到目前为止

服务器

int main (int argc, const char * argv[]) {
    printf("Server\n");

    int server_sock_fd;     //file descriptor for server socket
    int client_sock_fd;     //file descriptor for client socket
    socklen_t server_len, client_len;

    struct sockaddr_in server_address;
    struct sockaddr_in client_address;

    //Remove any old sockets and re-create
    unlink("server_socket");

    //Obtain the file descriptor for the socket
    server_sock_fd = socket(AF_INET, SOCK_STREAM, 0);

    //Name the socket and set properties
    server_address.sin_family       = AF_INET;                  //This is the Internet protocol
    server_address.sin_addr.s_addr  = htonl(INADDR_ANY);        //Accept from any address (note the conversion function)
    server_address.sin_port         = htons(9734);              //remember to use host-to-network-type conversion
    server_len = sizeof(server_address);
    bind(server_sock_fd, (struct sockaddr*)&server_address, server_len);    //bind socket to address

    //Create a connection queue and define max number of client connections
    listen(server_sock_fd, 1);

    //Main loop (ctrl-C will quit this application)
    while (1) {
        char ch;
        printf("Server waiting\n");
        client_len = sizeof(client_address);

        //Accept incoming client connection and create client socket
        //Returns a file descriptor for each connection in the queue (BLOCKS IF QUEUE IS EMPTY)
        client_sock_fd = accept(server_sock_fd, (struct sockaddr*)&client_address, &client_len);

        //Now we can read and write data using the standard read and write functions
        //Note that the socket is treated like a file with read/write permissions
        //We read the socket as if it were a file - note it is the client
        //read(client_sock_fd, &ch, 1); //Read a single byte
        //ch++;


    //Build the 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 as a child process for read (stdout will be directed to the pipe)
    inputStream = popen(strCommand,"r");
    if (inputStream == NULL) {
        perror("Cannot open child process for record");
        exit(1);
    }
//We get back a C Stream - I prefer to use a UNIX file descriptor
    fidIn = fileno(inputStream);

    //Read a finite block of data into a buffer
    char* pBuffer = buf;
    int iSamplesRead;
    iBytesRead = 0;
    while (iBytesRead < BUFFER_SIZE_IN_BYTES) {

        //Grab a block samples
        iSamplesRead = read(fidIn, pBuffer, BUFFER_SIZE_IN_BYTES);          //4000 is max for one hit

        //It is likely that the actual number of samples read is not as many as we wanted
        if (iSamplesRead > 0) {
            iBytesRead += iSamplesRead;
            pBuffer += iSamplesRead;
            printf("%d ", iSamplesRead);
        } else {
            break;
        }
    }

    //All done, then close the child process
    pclose(inputStream);
        //We can also send a response
        //write(client_sock_fd, &ch, 1);    //Write a single byte

        //Now close the client socket (we have finished)
        close(client_sock_fd);
    }
    return 0;
}

客户端

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 the socket - align with the 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 to server socket
    result = connect(socket_fd, (struct sockaddr*)&address, len);
    if (result == -1) {
        printf("Error - cannot connect socket\n");
        exit(0);
    }

    //ALL DONE - now we can use the socket as if it were a file
    //write(socket_fd, &ch, 1);
    //read(socket_fd, &ch, 1);
    //printf("Character back from server = %u\n", ch);
//Open process pipe for read only (pipe stream is 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 and unblock child process
    pclose(outputStream);
    printf("Address of buffer: %p\nAddress of pointer: %p\nDelta = %d\n", buf, pBuffer, (pBuffer-buf));

   return (EXIT_SUCCESS);
}

非常感谢任何帮助或提示!

1 个答案:

答案 0 :(得分:1)

我确信编写代码很有趣:)但是你可以使用gstreamer RTP来完成这个。 无论是以编程方式还是通过命令行。

一般文件: http://gstreamer.freedesktop.org/documentation/rtp.html

使用Gstreamer RTP传输音频的实际示例: http://delog.wordpress.com/2011/05/11/audio-streaming-over-rtp-using-the-rtpbin-plugin-of-gstreamer/