Blame
Date:
Fri Jun 10 08:03:22 2022 UTC
Message:
free the FILE on op_open_callbacks failure the API is a bit of a shitshow. op_fdopen is just a wrapper around FILE*, but returns void* for don't know what Windows limitation. if op_open_callbacks fails we have to free the stream by ourselves. the documentation implies that this void* pointer is, in fact, a FILE*.
01
2022-02-16
op
/*
02
2022-02-16
op
* Copyright (c) 2022 Omar Polo <op@openbsd.org>
03
2022-02-16
op
*
04
2022-02-16
op
* Permission to use, copy, modify, and distribute this software for any
05
2022-02-16
op
* purpose with or without fee is hereby granted, provided that the above
06
2022-02-16
op
* copyright notice and this permission notice appear in all copies.
07
2022-02-16
op
*
08
2022-02-16
op
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
09
2022-02-16
op
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10
2022-02-16
op
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11
2022-02-16
op
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12
2022-02-16
op
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13
2022-02-16
op
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14
2022-02-16
op
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15
2022-02-16
op
*/
16
2022-02-16
op
17
2022-02-16
op
#include <sys/types.h>
18
2022-02-16
op
#include <sys/queue.h>
19
2022-02-16
op
#include <sys/uio.h>
20
2022-02-16
op
21
2022-02-16
op
#include <err.h>
22
2022-02-16
op
#include <event.h>
23
2022-02-16
op
#include <fcntl.h>
24
2022-02-16
op
#include <math.h>
25
2022-02-16
op
#include <inttypes.h>
26
2022-02-16
op
#include <limits.h>
27
2022-02-16
op
#include <stdio.h>
28
2022-02-16
op
#include <stdint.h>
29
2022-02-16
op
#include <imsg.h>
30
2022-02-16
op
#include <unistd.h>
31
2022-02-16
op
32
2022-02-16
op
#include <opusfile.h>
33
2022-02-16
op
34
2022-02-16
op
#include "amused.h"
35
2022-02-18
op
#include "log.h"
36
2022-02-16
op
37
2022-02-16
op
#ifndef nitems
38
2022-02-16
op
#define nitems(x) (sizeof(x)/sizeof(x[0]))
39
2022-02-16
op
#endif
40
2022-02-16
op
41
2022-03-09
op
int
42
2022-02-16
op
play_opus(int fd)
43
2022-02-16
op
{
44
2022-02-16
op
static uint16_t pcm[BUFSIZ];
45
2022-02-16
op
static uint8_t out[BUFSIZ * 2];
46
2022-02-16
op
OggOpusFile *of;
47
2022-02-16
op
void *f;
48
2022-03-09
op
int r, ret = 0;
49
2022-02-16
op
OpusFileCallbacks cb = {NULL, NULL, NULL, NULL};
50
2022-02-16
op
int i, li, prev_li = -1;
51
2022-02-16
op
52
2022-02-16
op
if ((f = op_fdopen(&cb, fd, "r")) == NULL)
53
2022-02-16
op
err(1, "fdopen");
54
2022-02-16
op
55
2022-03-09
op
of = op_open_callbacks(f, &cb, NULL, 0, &r);
56
2022-02-18
op
if (of == NULL) {
57
2022-06-10
op
fclose(f);
58
2022-03-09
op
return -1;
59
2022-02-18
op
}
60
2022-02-16
op
61
2022-02-16
op
for (;;) {
62
2022-02-18
op
/* NB: will downmix multichannels files into two channels */
63
2022-03-09
op
r = op_read_stereo(of, pcm, nitems(pcm));
64
2022-03-09
op
if (r == OP_HOLE) /* corrupt file segment? */
65
2022-02-16
op
continue;
66
2022-03-09
op
if (r < 0) {
67
2022-02-18
op
log_warnx("error %d decoding file", ret);
68
2022-03-09
op
ret = -1;
69
2022-02-18
op
break;
70
2022-02-18
op
}
71
2022-03-09
op
if (r == 0)
72
2022-02-16
op
break; /* eof */
73
2022-02-16
op
74
2022-02-16
op
li = op_current_link(of);
75
2022-02-16
op
if (li != prev_li) {
76
2022-02-16
op
const OpusHead *head;
77
2022-02-16
op
78
2022-02-16
op
prev_li = li;
79
2022-02-16
op
head = op_head(of, li);
80
2022-02-18
op
if (head->input_sample_rate &&
81
2022-02-21
op
player_setup(16, head->input_sample_rate, 2) == -1)
82
2022-06-10
op
err(1, "player_setup");
83
2022-02-16
op
}
84
2022-02-16
op
85
2022-03-23
op
for (i = 0; i < 2*r; ++i) {
86
2022-02-16
op
out[2*i+0] = pcm[i] & 0xFF;
87
2022-02-16
op
out[2*i+1] = (pcm[i] >> 8) & 0xFF;
88
2022-02-16
op
}
89
2022-03-09
op
90
2022-03-23
op
if (!play(out, 4*r)) {
91
2022-03-09
op
ret = 1;
92
2022-03-09
op
break;
93
2022-03-09
op
}
94
2022-02-16
op
}
95
2022-02-16
op
96
2022-02-16
op
op_free(of);
97
2022-03-09
op
return ret;
98
2022-02-16
op
}
Omar Polo