Skip to content

Instantly share code, notes, and snippets.

@makotoshimazu
Last active August 29, 2015 14:23
Show Gist options
  • Save makotoshimazu/b1f6bbbc4d82d7dcecf4 to your computer and use it in GitHub Desktop.
Save makotoshimazu/b1f6bbbc4d82d7dcecf4 to your computer and use it in GitHub Desktop.
#include <netinet/in.h>
#include <netinet/ip.h>
#include <netinet/tcp.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <termios.h>
#include <fcntl.h>
#include <time.h>
#include <unistd.h>
#define N 8800
int subVoiceStream() {
fprintf(stderr, "hoge1\n");
// printf("kopurostart");
int ss = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
struct sockaddr_in addr;
addr.sin_family = AF_INET;
addr.sin_addr.s_addr = INADDR_ANY;
addr.sin_port = 50000;
bind(ss, (struct sockaddr *)&addr, sizeof(addr));
listen(ss, 10);
struct sockaddr_in client_addr;
socklen_t len = sizeof(struct sockaddr_in);
int s = accept(ss, (struct sockaddr *)&client_addr, &len);
close(ss);
/* main loop */
if(s > 0){
printf("kopuro\n");
FILE *fp3 = fopen("wakanapo.raw","rb");
int c = getchar();
switch (c) {
case 'a':
printf("a");
while(n > 0){
n=fread(data, 1, N, fp3);
send(s, data, n, 0);
}
break;
default:
break;
}
return 1;
}
return 0;
}
int mainStream() {
// printf("oyapuro");
fprintf(stderr, "hoge2\n");
int ss = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
struct sockaddr_in addr;
addr.sin_family = AF_INET;
addr.sin_addr.s_addr = INADDR_ANY;
addr.sin_port = 50001;
bind(ss, (struct sockaddr *)&addr, sizeof(addr));
listen(ss, 10);
struct sockaddr_in client_addr;
socklen_t len = sizeof(struct sockaddr_in);
int s = accept(ss, (struct sockaddr *)&client_addr, &len);
close(ss);
if(s > 0){
FILE *fp = popen("rec -q -t raw -b 16 -c 1 -e s -r 44100 -", "r");
FILE *fp2 = popen("play -q -t raw -b 16 -c 1 -e s -r 44100 -", "w");
while(1){
n=fread(data, 1, N, fp);
send(s, data, n, 0);
n2=recv(s, data2, N, 0);
fwrite(data2, 1, n2, fp2);
}
return 2;
close(s);
pclose(fp);
pclose(fp2);
}
}
void setupNonBlocking() {
// non blocking
struct termios save_settings;
struct termios settings;
char c;
tcgetattr(0,&save_settings);
settings = save_settings;
settings.c_lflag &= ~(ECHO|ICANON); /* 入力をエコーバックしない、バッファリングしない */
settings.c_cc[VTIME] = 0;
settings.c_cc[VMIN] = 1;
tcsetattr(0,TCSANOW,&settings);
fcntl(0,F_SETFL,O_NONBLOCK); /* 標準入力からの読み込むときブロックしないようにする */
}
int main(int argc, char** argv){
setupNonBlocking();
unsigned char data[N], data2[N];
int n,n2;
pid_t pid = fork();
if (pid < 0) {
perror("fork");
exit(-1);
} else if (pid == 0) {//コプロせす
return subVoiceStream();
} else { // 親プロセス
return mainStream();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment