-
Notifications
You must be signed in to change notification settings - Fork 289
/
Copy pathhls.cpp
executable file
·363 lines (311 loc) · 12.4 KB
/
hls.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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sched.h>
#include <errno.h>
#include <getopt.h>
#include <alsa/asoundlib.h>
#include <sys/time.h>
#include <math.h>
#include "Superpowered.h"
#include "SuperpoweredAdvancedAudioPlayer.h"
typedef struct ALSAContext {
snd_pcm_t *handle;
float *buffer;
struct pollfd *ufds;
unsigned int samplerate, numChannels, periodSize;
int pollDescriptorsCount;
void destroy() {
if (this->buffer) {
snd_pcm_drain(this->handle);
snd_pcm_close(this->handle);
free(this->buffer);
free(this->ufds);
}
}
bool setup() {
this->handle = NULL;
this->buffer = NULL;
this->ufds = NULL;
this->periodSize = 0;
unsigned int bufferTimeUs = 500000, periodTimeUs = 100000;
snd_pcm_uframes_t bufferSize = 0;
int dir;
int error = snd_pcm_open(&this->handle, "default", SND_PCM_STREAM_PLAYBACK, 0);
if (error < 0) {
printf("snd_pcm_open error %s\n", snd_strerror(error));
return false;
}
snd_pcm_hw_params_t *hwParams;
snd_pcm_hw_params_alloca(&hwParams);
error = snd_pcm_hw_params_any(this->handle, hwParams);
if (error < 0) {
printf("snd_pcm_hw_params_any error %s\n", snd_strerror(error));
snd_pcm_close(this->handle);
return false;
}
error = snd_pcm_hw_params_set_rate_resample(this->handle, hwParams, 0);
if (error < 0) {
printf("snd_pcm_hw_params_set_rate_resample error %s\n", snd_strerror(error));
snd_pcm_close(this->handle);
return false;
}
error = snd_pcm_hw_params_set_access(this->handle, hwParams, SND_PCM_ACCESS_RW_INTERLEAVED);
if (error < 0) {
printf("snd_pcm_hw_params_set_access error %s\n", snd_strerror(error));
snd_pcm_close(this->handle);
return false;
}
error = snd_pcm_hw_params_set_format(this->handle, hwParams, SND_PCM_FORMAT_FLOAT_LE);
if (error < 0) {
printf("snd_pcm_hw_params_set_format error %s\n", snd_strerror(error));
snd_pcm_close(this->handle);
return false;
}
error = snd_pcm_hw_params_set_channels(this->handle, hwParams, this->numChannels);
if (error < 0) {
printf("snd_pcm_hw_params_set_channels error %s\n", snd_strerror(error));
snd_pcm_close(this->handle);
return false;
}
error = snd_pcm_hw_params_set_rate_near(this->handle, hwParams, &this->samplerate, 0);
if (error < 0) {
printf("snd_pcm_hw_params_set_rate_near error %s\n", snd_strerror(error));
snd_pcm_close(this->handle);
return false;
}
error = snd_pcm_hw_params_set_buffer_time_near(this->handle, hwParams, &bufferTimeUs, &dir);
if (error < 0) {
printf("snd_pcm_hw_params_set_buffer_time error %s\n", snd_strerror(error));
snd_pcm_close(this->handle);
return false;
}
error = snd_pcm_hw_params_get_buffer_size(hwParams, &bufferSize);
if (error < 0) {
printf("snd_pcm_hw_params_get_buffer_size error %s\n", snd_strerror(error));
snd_pcm_close(this->handle);
return false;
}
error = snd_pcm_hw_params_set_period_time_near(this->handle, hwParams, &periodTimeUs, &dir);
if (error < 0) {
printf("snd_pcm_hw_params_set_period_time_near error %s\n", snd_strerror(error));
snd_pcm_close(this->handle);
return false;
}
snd_pcm_uframes_t frames = 0;
error = snd_pcm_hw_params_get_period_size(hwParams, &frames, &dir);
if (error < 0) {
printf("snd_pcm_hw_params_get_period_size error %s\n", snd_strerror(error));
snd_pcm_close(this->handle);
return false;
}
this->periodSize = (unsigned int)frames;
error = snd_pcm_hw_params(this->handle, hwParams);
if (error < 0) {
printf("snd_pcm_hw_params error %s\n", snd_strerror(error));
snd_pcm_close(this->handle);
return false;
}
snd_pcm_sw_params_t *swParams;
snd_pcm_sw_params_alloca(&swParams);
error = snd_pcm_sw_params_current(this->handle, swParams);
if (error < 0) {
printf("snd_pcm_sw_params_current error %s\n", snd_strerror(error));
snd_pcm_close(this->handle);
return false;
}
error = snd_pcm_sw_params_set_start_threshold(this->handle, swParams, (bufferSize / this->periodSize) * this->periodSize);
if (error < 0) {
printf("snd_pcm_sw_params_set_start_threshold error %s\n", snd_strerror(error));
snd_pcm_close(this->handle);
return false;
}
error = snd_pcm_sw_params_set_avail_min(this->handle, swParams, this->periodSize);
if (error < 0) {
printf("snd_pcm_sw_params_set_avail_min error %s\n", snd_strerror(error));
snd_pcm_close(this->handle);
return false;
}
error = snd_pcm_sw_params(this->handle, swParams);
if (error < 0) {
printf("snd_pcm_sw_params error %s\n", snd_strerror(error));
snd_pcm_close(this->handle);
return false;
}
this->pollDescriptorsCount = snd_pcm_poll_descriptors_count(this->handle);
if (this->pollDescriptorsCount <= 0) {
printf("invalid poll descriptors count %s\n", snd_strerror(error));
snd_pcm_close(this->handle);
return false;
}
this->ufds = (pollfd *)malloc(sizeof(struct pollfd) * this->pollDescriptorsCount);
if (!this->ufds) {
printf("out of memory\n");
snd_pcm_close(this->handle);
return false;
}
error = snd_pcm_poll_descriptors(this->handle, this->ufds, this->pollDescriptorsCount);
if (error < 0) {
printf("snd_pcm_poll_descriptors error %s\n", snd_strerror(error));
snd_pcm_close(this->handle);
free(this->ufds);
return false;
}
this->buffer = (float *)malloc(this->periodSize * this->numChannels * 8);
if (!this->buffer) {
printf("out of memory\n");
snd_pcm_close(this->handle);
free(this->ufds);
return false;
}
printf("buffer size: %li, period size: %i, sample rate: %i\n", bufferSize, this->periodSize, this->samplerate);
return true;
}
bool underrunRecovery(int error) {
if (error == -EPIPE) {
error = snd_pcm_prepare(this->handle);
if (error < 0) printf("underrun recovery 1, snd_pcm_prepare error %s\n", snd_strerror(error));
return true;
} else if (error == -ESTRPIPE) {
while ((error = snd_pcm_resume(this->handle)) == -EAGAIN) sleep(1);
if (error < 0) {
error = snd_pcm_prepare(this->handle);
if (error < 0) printf("underrun recovery 2, snd_pcm_prepare error %s\n", snd_strerror(error));
}
return true;
}
return (error >= 0);
}
bool waitForPoll(bool *init) {
unsigned short revents;
while (1) {
poll(this->ufds, this->pollDescriptorsCount, -1);
snd_pcm_poll_descriptors_revents(this->handle, this->ufds, this->pollDescriptorsCount, &revents);
if (revents & POLLOUT) return true;
if (revents & POLLERR) {
snd_pcm_state_t state = snd_pcm_state(this->handle);
if ((state == SND_PCM_STATE_XRUN) || (state == SND_PCM_STATE_SUSPENDED)) {
int error = (state == SND_PCM_STATE_XRUN) ? -EPIPE : -ESTRPIPE;
if (!underrunRecovery(error)) {
printf("wait for poll write error: %s\n", snd_strerror(error));
return false;
}
*init = true;
} else {
printf("wait for poll failed\n");
return false;
}
}
}
return true;
}
bool printAudioDevices() {
char **hints;
int error = snd_device_name_hint(-1, "pcm", (void ***)&hints);
if (error != 0) {
printf("snd_device_name_hint error %i\n", error);
return false;
}
if (*hints == NULL) {
printf("\nNo audio devices found.\n");
snd_device_name_free_hint((void **)hints);
return false;
}
printf("\nAudio Devices:\n");
char **hint = hints;
while (*hint != NULL) {
char *deviceName = snd_device_name_get_hint(*hint, "NAME"), *deviceIO = snd_device_name_get_hint(*hint, "IOID");
if ((deviceName != NULL) && (strcmp("null", deviceName) != 0)) {
printf("%s", deviceName);
if ((deviceIO != NULL) && (strcmp("null", deviceIO) != 0)) printf(" %s", deviceIO); else printf(" Input/Output");
printf("\n");
}
hint++;
}
printf("\n");
snd_device_name_free_hint((void **)hints);
return true;
}
} ALSAContext;
int main(int argc, char *argv[]) {
ALSAContext context;
if (!context.printAudioDevices()) return 0;
context.samplerate = 48000;
context.numChannels = 2;
if (!context.setup()) return 0;
Superpowered::Initialize("ExampleLicenseKey-WillExpire-OnNextUpdate");
Superpowered::AdvancedAudioPlayer::setTempFolder("/tmp/");
Superpowered::AdvancedAudioPlayer *player = new Superpowered::AdvancedAudioPlayer(context.samplerate, 0);
player->openHLS("http://qthttp.apple.com.edgesuite.net/1010qwoeiuryfg/sl.m3u8");
bool init = true;
int numTurns = 1, numFrames = context.periodSize;
if (context.periodSize > 1024) {
div_t d = div(context.periodSize, 1024);
numTurns = d.quot;
if (d.rem > 0) numTurns++;
numFrames = context.periodSize / numTurns;
}
printf("Superpowered turns: %i, frames per turn: %i\n\nPress ENTER to quit.\n", numTurns, numFrames);
bool run = true;
while (run) {
if (!init && !context.waitForPoll(&init)) break;
switch (player->getLatestEvent()) {
case Superpowered::AdvancedAudioPlayer::PlayerEvent_Opened:
printf("\rLoad success.\n");
player->play();
break;
case Superpowered::AdvancedAudioPlayer::PlayerEvent_ConnectionLost:
printf("\rConnection Lost\n");
run = false;
break;
case Superpowered::AdvancedAudioPlayer::PlayerEvent_OpenFailed:
{
int openError = player->getOpenErrorCode();
printf("\rOpen error %i: %s\n", openError, Superpowered::AdvancedAudioPlayer::statusCodeToString(openError));
run = false;
}
break;
default:;
}
if (player->eofRecently()) {
printf("\rEnd of file.\n");
break;
}
float *buf = context.buffer;
for (int n = 0; n < numTurns; n++) {
if (!player->processStereo(buf, false, numFrames)) memset(buf, 0, numFrames * 8);
buf += numFrames * 2;
}
float *ptr = context.buffer;
int cptr = context.periodSize;
while (cptr > 0) {
int error = snd_pcm_writei(context.handle, ptr, cptr);
if (error < 0) {
if (!context.underrunRecovery(error)) {
printf("underrun recovery write error: %s\n", snd_strerror(error));
run = false;
break;
}
init = true;
printf("skip one period\n");
break;
}
if (snd_pcm_state(context.handle) == SND_PCM_STATE_RUNNING) init = false;
ptr += error * context.numChannels;
cptr -= error;
if (cptr == 0) break;
if (!context.waitForPoll(&init)) {
run = false;
break;
}
}
struct timeval tv = { 0, 0 };
fd_set fds;
FD_ZERO(&fds);
FD_SET(0, &fds);
if (select(1, &fds, NULL, NULL, &tv)) break;
}
context.destroy();
if (player) delete(player);
return 0;
}