-
Notifications
You must be signed in to change notification settings - Fork 0
/
phone.c
128 lines (102 loc) · 3.15 KB
/
phone.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <netinet/ip.h>
#include <netinet/tcp.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <pthread.h>
#define BUFFER_SIZE 32768
int bandpass(long n, unsigned char *buffer);
char use_bandpass;
void *send_audio(void *arg) {
int sock = *((int *)arg);
unsigned char* buffer = malloc(sizeof(unsigned char) * BUFFER_SIZE);
while (1) {
ssize_t num_read = read(STDIN_FILENO, buffer, BUFFER_SIZE);
if (num_read <= 0) break;
if(use_bandpass) bandpass(BUFFER_SIZE, buffer);
send(sock, buffer, num_read, 0);
}
free(buffer);
return NULL;
}
void *receive_audio(void *arg) {
int sock = *((int *)arg);
unsigned char* buffer = malloc(sizeof(unsigned char) * BUFFER_SIZE);
while (1) {
ssize_t num_received = recv(sock, buffer, BUFFER_SIZE, 0);
if (num_received <= 0) break;
if(use_bandpass) bandpass(BUFFER_SIZE, buffer);
write(STDOUT_FILENO, buffer, num_received);
}
free(buffer);
return NULL;
}
int client_setup(char *server_ip, int port){
int s = socket(AF_INET, SOCK_STREAM, 0);
if (s == -1) {perror("socket"); exit(1);}
struct sockaddr_in addr;
memset(&addr, 0, sizeof(addr));
addr.sin_family = AF_INET;
addr.sin_port = htons(port);
addr.sin_addr.s_addr = inet_addr(server_ip);
if (connect(s, (struct sockaddr *)&addr, sizeof(addr)) == -1) {perror("connect"); exit(1);}
return s;
}
int server_setup(int port){
int ss = socket(AF_INET, SOCK_STREAM, 0);
if (ss == -1) {perror("socket"); exit(1);}
struct sockaddr_in addr;
memset(&addr, 0, sizeof(addr));
addr.sin_family = AF_INET;
addr.sin_port = htons(port);
addr.sin_addr.s_addr = INADDR_ANY;
if (bind(ss, (struct sockaddr *)&addr, sizeof(addr)) == -1) {perror("bind"); exit(1);}
if (listen(ss, 10) == -1) {perror("listen"); exit(1);}
struct sockaddr_in client_addr;
socklen_t len = sizeof(struct sockaddr_in);
int s = accept(ss, (struct sockaddr *)&client_addr, &len);
close(ss);
return s;
}
int main(int argc, char *argv[]) {
if ((argc < 2) || (argc > 3)) {
fprintf(stderr, "Usage Error\n");
exit(EXIT_FAILURE);
}
int sock;
if (argc == 3){
char* server_ip = argv[1];
int port = atoi(argv[2]);
sock = client_setup(server_ip, port);
}
if (argc == 2){
int port = atoi(argv[1]);
sock = server_setup(port);
}
FILE *fp1 = NULL;
char *cmdline = "rec -t raw -b 16 -c 1 -e s -r 44100 - & play -t raw -b 16 -c 1 -e s -r 44100 -";
if ((fp1 = popen(cmdline, "r")) == NULL) {
perror("popen");
exit(EXIT_FAILURE);
}
pthread_t send_thread, receive_thread;
use_bandpass = 1; // フィルタをかける場合1、かけない場合0で分ける
if (pthread_create(&send_thread, NULL, send_audio, &sock) != 0) {
perror("pthread_create send_audio");
close(sock);
exit(EXIT_FAILURE);
}
if (pthread_create(&receive_thread, NULL, receive_audio, &sock) != 0) {
perror("pthread_create receive_audio");
close(sock);
exit(EXIT_FAILURE);
}
pthread_join(send_thread, NULL);
pthread_join(receive_thread, NULL);
close(sock);
return 0;
}