gpt4 book ai didi

c - 通过网络 Ubuntu 和 C 发送声音

转载 作者:太空狗 更新时间:2023-10-29 12:32:32 25 4
gpt4 key购买 nike

我正在尝试通过 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);
}

如有任何帮助或提示,我们将不胜感激!

最佳答案

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

关于c - 通过网络 Ubuntu 和 C 发送声音,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22612814/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com