-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpump.c
More file actions
218 lines (189 loc) · 5.64 KB
/
pump.c
File metadata and controls
218 lines (189 loc) · 5.64 KB
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
#define PY_SSIZE_T_CLEAN
#include <Python.h>
#include <structmember.h>
#include <stdint.h>
#include <stdlib.h>
#include <unistd.h>
#include <stdio.h>
#include <signal.h>
#include <time.h>
#include <string.h>
#include <stdbool.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include "sbc/sbc.h"
//#include "a2dp-codecs.h"
#include "rtp.h"
sbc_t sbct;
// 1024/44100 in nanosecs
#define PERIOD 23219955
int transportsock;
uint16_t seqnum = 0;
uint32_t timestamp = 0;
uint8_t inbuf[4096];
uint8_t outbuf[1024];
struct rtp_header* rtph = outbuf;
struct rtp_payload* rtpp = outbuf + sizeof(struct rtp_header);
int rtpsize = sizeof(struct rtp_header) + sizeof(struct rtp_payload);
FILE* wavfile;
int32_t nsamples;
volatile int ticks = 0;
volatile bool done = false;
// these routines assume write-mtu == 1000
// if packet size is less then fewer frames can be sent at a time
bool chunk_to_a2dp(void)
{
unsigned int written;
int n, k;
uint8_t *d, *p, *end;
uint8_t frame, nf;
rtph->sequence_number = htons(seqnum++);
rtph->timestamp = htonl(timestamp);
k = fread(inbuf, 2, 1024, wavfile);
if (k==0) return true;
p = inbuf;
d = outbuf + rtpsize;
end = outbuf+1000;
rtpp->frame_count = nf = k/128;
for (frame=0; frame<nf; frame++) {
sbc_encode(&sbct, p, 256, d, end-d, &written);
p+=256; d+=written;
}
n = d - outbuf;
send(transportsock, outbuf, n, 0);
timestamp += 1024;
nsamples -= k;
return (nsamples<=0);
}
bool stereo_chunk_to_a2dp(void)
{
unsigned int written;
int n, k;
uint8_t *d, *p, *end;
uint8_t frame, nf;
rtph->sequence_number = htons(seqnum++);
rtph->timestamp = htonl(timestamp);
k = fread(inbuf, 4, 1024, wavfile);
if (k==0) return true;
p = inbuf;
d = outbuf + rtpsize;
end = outbuf+1000;
rtpp->frame_count = nf = k/128;
for (frame=0; frame<nf; frame++) {
sbc_encode(&sbct, p, 512, d, end-d, &written);
p+=512; d+=written;
}
n = d - outbuf;
send(transportsock, outbuf, n, 0);
timestamp += 1024;
nsamples -= k;
return (nsamples<=0);
}
static void sighandler(int sig, siginfo_t *si, void *uc)
{
done = chunk_to_a2dp();
ticks++;
}
static void stereosighandler(int sig, siginfo_t *si, void *uc)
{
done = stereo_chunk_to_a2dp();
ticks++;
}
void init_sbc(bool mono)
{
// this should be done in accordance with the config of the transport
// 2nd byte of config ==21 -> 8 subbands 16 blocks loundness-alloc
sbc_init(&sbct, 0);
if (mono) sbct.mode = SBC_MODE_MONO;
else sbct.mode = SBC_MODE_STEREO;
sbct.frequency = SBC_FREQ_44100;
sbct.allocation = SBC_AM_LOUDNESS;
sbct.subbands = SBC_SB_8;
sbct.blocks = SBC_BLK_16;
sbct.bitpool = 53;
sbct.endian = SBC_LE;
}
static char pump_doc[] = "encode and stream file to a2dp sink";
static PyObject* _datapump(PyObject *self, PyObject *args)
{
char *filename;
timer_t timerid;
struct sigevent sev;
struct itimerspec its;
sigset_t mask;
struct sigaction sa;
int t;
uint32_t datasize;
uint16_t numchans;
bool mono;
if (!PyArg_ParseTuple(args, "isH", &transportsock, &filename, &numchans)) return NULL;
mono = (numchans<2);
printf("transportsock=%d. file=%s pumping...\n", transportsock, filename);
sa.sa_flags = SA_SIGINFO;
if (mono) sa.sa_sigaction = sighandler;
else sa.sa_sigaction = stereosighandler;
sigemptyset(&sa.sa_mask);
if (sigaction(SIGRTMIN, &sa, NULL) == -1) { printf("sigaction"); return NULL; }
sigemptyset(&mask);
sigaddset(&mask, SIGRTMIN);
if (sigprocmask(SIG_SETMASK, &mask, NULL) == -1) { printf("sigprocmask"); return NULL; }
sev.sigev_notify = SIGEV_SIGNAL;
sev.sigev_signo = SIGRTMIN;
sev.sigev_value.sival_ptr = &timerid;
if (timer_create(CLOCK_REALTIME, &sev, &timerid) == -1) { printf("timer_create"); return NULL; }
// first callback in 11.61ms then every 23.22ms
// so remote end never runs out of data
its.it_value.tv_sec = 0; its.it_value.tv_nsec = PERIOD>>1;
its.it_interval.tv_sec = 0;
its.it_interval.tv_nsec = PERIOD;
if (timer_settime(timerid, 0, &its, NULL) == -1) { printf("timer_settime"); return NULL; }
init_sbc(mono);
memset(rtph, 0, rtpsize);
rtph->v=2; rtph->pt=1; rtph->ssrc=htonl(1);
// unsigned int framelen, codesize, ms;
// framelen = sbc_get_frame_length(&sbct);
// codesize = sbc_get_codesize(&sbct);
// ms = sbc_get_frame_duration(&sbct);
// printf("framlen=%u codesize=%u duration=%u\n", framelen, codesize, ms);
wavfile = fopen(filename, "r"); //TODO check wavfile open
fseek(wavfile, 40, SEEK_SET);
fread(&datasize, 4, 1, wavfile);
if (mono) {
nsamples = datasize >> 1;
printf("nsamples=%u\n", nsamples);
chunk_to_a2dp();
}
else {
nsamples = datasize >> 2;
printf("nsamples=%u\n", nsamples);
stereo_chunk_to_a2dp();
}
printf("unbloking\n");
if (sigprocmask(SIG_UNBLOCK, &mask, NULL) == -1) { printf("sigprocmask unblock"); return NULL; }
t = ticks;
while (!done) {
if (t != ticks) {
if ((ticks & 0xf) == 0) printf("%d\n", ticks);
t=ticks;
}
}
timer_delete(timerid);
fclose(wavfile);
sbc_finish(&sbct);
Py_INCREF(Py_None);
return Py_None;
}
static PyMethodDef Pump_methods[] = {
{"datapump", _datapump, METH_VARARGS, pump_doc },
{ NULL, NULL, 0, NULL } };
static struct PyModuleDef _pumpmodule = {
PyModuleDef_HEAD_INIT,
"_Pump",
"NULL",
-1,
Pump_methods };
PyMODINIT_FUNC PyInit__Pump(void)
{
PyObject *mod = PyModule_Create(&_pumpmodule);
return mod;
}