-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathminiogg.c
187 lines (158 loc) · 4.23 KB
/
miniogg.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
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
/*
* This program functions as a client that captures audio from a UNIX
* socket, encodes it using the Opus codec, and then streams the
* encoded audio to standard output.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <signal.h>
#include <unistd.h>
#include <opusenc.h>
#define SAMPLE_RATE 48000
#define CHANNELS 2
static OggOpusEnc *enc = NULL;
static int sock = -1;
void
handle_sigint (int sig) {
if (enc != NULL)
{
ope_encoder_drain (enc);
ope_encoder_destroy (enc);
enc = NULL;
}
if (sock != -1)
{
close (sock);
sock = -1;
}
exit(0);
}
static int
write_callback (void *data __attribute__((unused)),
const unsigned char *ptr,
opus_int32 len)
{
if (fwrite (ptr, 1, len, stdout) == (unsigned) len)
{
fflush (stdout);
return 0;
}
return -1;
}
static int
close_callback (void *data __attribute__((unused))) {
return 0;
}
int
main (int argc, char **argv)
{
const char *socket_path = "minirec.sock";
if (argc == 2)
socket_path = argv[1];
else if (argc > 2)
{
fprintf (stderr, "usage: %s [minirec socket]\n", argv[0]);
exit (EXIT_FAILURE);
}
sock = socket (AF_UNIX, SOCK_STREAM, 0);
if (sock < 0) {
perror ("socket");
exit (EXIT_FAILURE);
}
struct sockaddr_un server_addr;
memset (&server_addr, 0, sizeof (struct sockaddr_un));
server_addr.sun_family = AF_UNIX;
strncpy (server_addr.sun_path,
socket_path,
sizeof server_addr.sun_path - 1);
if (connect (sock,
(struct sockaddr *) &server_addr,
sizeof server_addr) < 0) {
perror ("connect");
close (sock);
exit (EXIT_FAILURE);
}
OpusEncCallbacks callbacks = { write_callback, close_callback };
OggOpusComments *comments = ope_comments_create();
int error;
enc = ope_encoder_create_callbacks (&callbacks, NULL,
comments,
SAMPLE_RATE,
CHANNELS, 0, &error);
if (!enc)
{
fprintf (stderr, "failed to create encoder: %s\n",
ope_strerror(error));
ope_comments_destroy (comments);
close (sock);
exit (EXIT_FAILURE);
}
int mux_delay = 0;
if (ope_encoder_ctl (enc, OPE_GET_MUXING_DELAY (&mux_delay)) != OPE_OK)
{
fprintf (stderr, "failed to get muxing delay\n");
ope_encoder_destroy (enc);
ope_comments_destroy (comments);
close (sock);
exit (EXIT_FAILURE);
}
/* fprintf (stderr, "initial muxing delay: %d samples, %2.2f s\n", */
/* mux_delay, mux_delay / 48000.0); */
// set the muxing delay to 0.1s, i.e. 4800 samples
if (ope_encoder_ctl (enc, OPE_SET_MUXING_DELAY (4800)) != OPE_OK)
{
fprintf (stderr, "failed to set muxing delay\n");
ope_encoder_destroy (enc);
ope_comments_destroy (comments);
close (sock);
exit (EXIT_FAILURE);
}
/* fprintf (stderr, "set muxing delay to 0.1s\n"); */
// now do the decision delay
int decision_delay = 0;
if (ope_encoder_ctl (enc, OPE_GET_DECISION_DELAY (&decision_delay))
!= OPE_OK)
{
fprintf (stderr, "failed to get decision delay\n");
ope_encoder_destroy (enc);
ope_comments_destroy (comments);
close (sock);
exit (EXIT_FAILURE);
}
/* fprintf (stderr, "initial decision delay: %d samples, %2.2f s\n", */
/* decision_delay, decision_delay / 48000.0); */
// set the decision delay to 0.1s, i.e. 4800 samples
if (ope_encoder_ctl (enc, OPE_SET_DECISION_DELAY (4800)) != OPE_OK)
{
fprintf (stderr, "failed to set decision delay\n");
ope_encoder_destroy (enc);
ope_comments_destroy (comments);
close (sock);
exit (EXIT_FAILURE);
}
struct sigaction act;
memset (&act, 0, sizeof act);
act.sa_handler = handle_sigint;
sigaction (SIGINT, &act, NULL);
opus_int16 in[960 * 2];
ssize_t read_size;
while ((read_size = read (sock, in, sizeof in)) > 0)
{
if (ope_encoder_write (enc, in,
read_size / sizeof (opus_int16) / 2)
!= OPE_OK)
{
fprintf (stderr, "failed to encode frame\n");
break;
}
}
ope_encoder_drain (enc);
ope_encoder_destroy (enc);
ope_comments_destroy (comments);
close (sock);
/* fprintf(stderr, "done\n"); */
return 0;
}