-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathplayer_oggvorbis.c
94 lines (81 loc) · 2.18 KB
/
player_oggvorbis.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
/*
* Copyright (c) 2022 Omar Polo <op@omarpolo.com>
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#include <fcntl.h>
#include <math.h>
#include <inttypes.h>
#include <limits.h>
#include <stdio.h>
#include <stdint.h>
#include <unistd.h>
#include <vorbis/codec.h>
#include <vorbis/vorbisfile.h>
#include "log.h"
#include "player.h"
#ifndef nitems
#define nitems(x) (sizeof(x)/sizeof(x[0]))
#endif
int
play_oggvorbis(int fd, const char **errstr)
{
static char pcmout[4096];
FILE *f;
OggVorbis_File vf;
vorbis_info *vi;
int64_t seek = -1;
int current_section, ret = 0;
if ((f = fdopen(fd, "r")) == NULL) {
*errstr = "fdopen failed";
close(fd);
return -1;
}
if (ov_open_callbacks(f, &vf, NULL, 0, OV_CALLBACKS_NOCLOSE) < 0) {
*errstr = "input is not an Ogg bitstream";
fclose(f);
return -1;
}
/*
* we could extract some tags by looping over the NULL
* terminated array returned by ov_comment(&vf, -1), see
* previous revision of this file.
*/
vi = ov_info(&vf, -1);
if (player_setup(16, vi->rate, vi->channels) == -1)
fatal("player_setup");
player_setduration(ov_time_total(&vf, -1) * vi->rate);
for (;;) {
long r;
if (seek != -1) {
r = ov_pcm_seek(&vf, seek);
if (r != 0)
break;
player_setpos(seek);
}
r = ov_read(&vf, pcmout, sizeof(pcmout), 0, 2, 1,
¤t_section);
if (r == 0)
break;
else if (r > 0) {
/* TODO: deal with sample rate changes */
if (!play(pcmout, r, &seek)) {
ret = 1;
break;
}
}
}
ov_clear(&vf);
fclose(f);
return ret;
}