通过网络Ubuntu和C发送声音



我正试图通过C中的网络从麦克风发送声音,作为一种网络婴儿监视器。我已经得到了服务器录制声音,但对于我的生命不能让它在客户端播放。我的代码非常混乱,我很抱歉,但这是我目前为止的代码

服务器

int main (int argc, const char * argv[]) {
    printf("Servern");
    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 waitingn");
        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 %sn", 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("Clientn");
    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 socketn");
        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 = %un", 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 %sn", 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: %pnAddress of pointer: %pnDelta = %dn", buf, pBuffer, (pBuffer-buf));
   return (EXIT_SUCCESS);
}

任何帮助或提示将非常感激!

我确信编写代码很有趣:)但是您可以使用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/

最新更新