/
cast.cpp
220 lines (174 loc) · 5.31 KB
/
cast.cpp
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
extern "C"{
#include <errno.h>
#include <unistd.h>
#ifdef _WIN64
#include <winsock2.h>
#else
#include <netinet/in.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#endif
#include <SDL2/SDL.h>
#include <lame/lame.h>
#include "http.h"
}
#ifdef _WIN64
//none
#else
typedef int SOCKET;
#endif
SOCKET net_sock;
int net_init(sockaddr_in* pto, const char* uri){
// fprintf(stdout, "[%s]", __func__);fflush(stdout);
int e;
net_sock = socket(PF_INET, SOCK_STREAM, 0);
e = connect(net_sock, (sockaddr*)pto, sizeof(*pto));
if(e==-1){ fprintf(stderr,"!connect:%d\n",e); return 0; }
// request
{
char aBuf[256];
aBuf[sizeof(aBuf)-1] = '\0'; // For safety.
int n = 0;
n += snprintf(aBuf+n, sizeof(aBuf)-n,
"PUT %s HTTP/1.0\r\n"
"content-type: audio/mpeg\r\n"
"\r\n",
uri
);
send(net_sock, aBuf, n, 0);
}
//response
struct http hdr;
http_reset(&hdr);
int n = 0;
while(
(recv(net_sock, hdr.m_aBuf+(n++), 1, 0) == 1) &&
!(
hdr.m_aBuf[n-4]=='\r' &&
hdr.m_aBuf[n-3]=='\n' &&
hdr.m_aBuf[n-2]=='\r' &&
hdr.m_aBuf[n-1]=='\n' &&
1) &&
(n < HTTP_BUFSIZE) &&
1){}
if(0 < http_parse(&hdr, n)){
if(strcmp(hdr.m_pCode, "100") != 0){
fprintf(stderr, "!http code:%s\n", hdr.m_pCode);
return 0;
}
}else{
fprintf(stderr, "!http_parse\n");
return 0;
}
return 1;
}
void net_quit(){
close(net_sock);
}
lame_global_flags* mp3_lib;
int mp3_init(){
// fprintf(stdout, "[%s]", __func__);fflush(stdout);
mp3_lib = lame_init();
lame_set_num_channels(mp3_lib, 1);
lame_set_mode(mp3_lib, MONO);
lame_set_in_samplerate(mp3_lib, 44100);
lame_set_out_samplerate(mp3_lib, 8000);
lame_set_brate(mp3_lib, 8);
lame_set_quality(mp3_lib, 9);
if( lame_init_params(mp3_lib) < 0 ){
fprintf(stderr, "!lame_init_params\n");
return 0;
}
return 1;
}
void mp3_quit(){
lame_close(mp3_lib);
}
SDL_AudioDeviceID mic_dev;
void mic_cb(void *userdata, Uint8 *stream, int len) {
// fprintf(stdout, "[%s]", __func__);fflush(stdout);
unsigned char buf[(int)(1.25*2205) + 7200];
int n = lame_encode_buffer(mp3_lib, (const short*)stream, NULL, len/2, buf, sizeof(buf));
if(0 < n){
fprintf(stdout, "*");fflush(stdout);
send(net_sock, (const char*)buf, n, 0);
}
}
int mic_init() {
// fprintf(stdout, "[%s]", __func__);fflush(stdout);
SDL_Init(SDL_INIT_AUDIO);
SDL_AudioSpec want, have;
SDL_zero(want);
want.freq = 44100;
want.format = AUDIO_S16SYS;
want.channels = 1;
want.samples = 576;
want.callback = mic_cb;
mic_dev = SDL_OpenAudioDevice(SDL_GetAudioDeviceName(0, 1), 1, &want, &have, 0);
if(!mic_dev){
fprintf(stderr, "Failed to open audio: %s\n", SDL_GetError());
#ifdef _WIN64
fprintf(stderr, "Check 'Setting'->'Privacy'->'Microphone'.\n");
#endif
return 0;
}else
if (have.format != want.format) {
fprintf(stderr, "We didn't get the wanted format.\n");
return 0;
}
SDL_PauseAudioDevice(mic_dev, 0);
return 1;
}
void mic_quit(){
SDL_CloseAudioDevice(mic_dev);
}
int main(int argc, char *argv[]){
#ifdef _WIN64
WSADATA wsaData;
WSAStartup(MAKEWORD(2, 2), &wsaData);
#endif
struct sockaddr_in cnctto = {};
{
cnctto.sin_family = PF_INET;
cnctto.sin_port = htons(2000);
cnctto.sin_addr.s_addr = (127<<0) | (0<<8) | (0<<16) | (1<<24);
}
char uri[0x100] = "/";
if(2 <= argc){
cnctto.sin_addr.s_addr = inet_addr(argv[1]);
}
if(3 <= argc){
cnctto.sin_port = htons( atoi(argv[2]) );
}
if(4 <= argc){
strncpy(uri, argv[3], sizeof(uri))[sizeof(uri)-1] = '\0';
}
if(
!net_init(&cnctto, uri) ||
!mp3_init() ||
!mic_init() ||
0){
fprintf(stdout, "!error\n");fflush(stdout);
return 1;
}
// show help
{
fprintf(stdout, "connectto: %s:%d %s.\n", inet_ntoa(cnctto.sin_addr), (int)ntohs(cnctto.sin_port), uri);
fprintf(stdout, "'Enter' to stop.\n");
fflush(stdout);
}
char cmd[0x100];
while( fgets(cmd, sizeof(cmd), stdin) ){
cmd[strlen(cmd)-1] = '\0';
if( strcmp(cmd, "") == 0 ){
break;
}
}
mic_quit();
mp3_quit();
net_quit();
#ifdef _WIN64
WSACleanup();
#endif
return 0;
}