forked from DeaDBeeF-Player/deadbeef
-
Notifications
You must be signed in to change notification settings - Fork 0
/
streamreader.c
235 lines (196 loc) · 5.64 KB
/
streamreader.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
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
/*
DeaDBeeF -- the music player
Copyright (C) 2009-2017 Alexey Yakovenko and other contributors
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any damages
arising from the use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it
freely, subject to the following restrictions:
1. The origin of this software must not be misrepresented; you must not
claim that you wrote the original software. If you use this software
in a product, an acknowledgment in the product documentation would be
appreciated but is not required.
2. Altered source versions must be plainly marked as such, and must not be
misrepresented as being the original software.
3. This notice may not be removed or altered from any source distribution.
*/
#include <string.h>
#include <assert.h>
#include <stdlib.h>
#include "streamreader.h"
#include "replaygain.h"
#include "threading.h"
// read ahead about 5 sec at 44100/16/2
#define BLOCK_SIZE 16384
#define BLOCK_COUNT 48
static streamblock_t *blocks; // list of all blocks
static streamblock_t *block_data; // first available block with data (can be NULL)
static streamblock_t *block_next; // next block available to be read into / queued
static int numblocks_ready;
static int curr_block_bitrate;
static playItem_t *_prev_rg_track;
static int _rg_settingschanged = 1;
static int _firstblock = 0;
void
streamreader_init (void) {
_prev_rg_track = NULL;
_rg_settingschanged = 1;
for (int i = 0; i < BLOCK_COUNT; i++) {
streamblock_t *b = calloc (1, sizeof (streamblock_t));
b->pos = -1;
b->buf = malloc (BLOCK_SIZE);
b->next = blocks;
blocks = b;
}
block_next = blocks;
numblocks_ready = 0;
_firstblock = 0;
}
void
streamreader_free (void) {
streamreader_reset ();
while (blocks) {
streamblock_t *next = blocks->next;
free (blocks->buf);
free (blocks);
blocks = next;
}
block_next = block_data = NULL;
numblocks_ready = 0;
_prev_rg_track = NULL;
_rg_settingschanged = 1;
_firstblock = 0;
}
streamblock_t *
streamreader_get_next_block (void) {
if (block_next->pos >= 0) {
return NULL; // all buffers full
}
return block_next;
}
void
streamreader_configchanged (void) {
_rg_settingschanged = 1;
}
int
streamreader_read_block (streamblock_t *block, playItem_t *track, DB_fileinfo_t *fileinfo, uint64_t mutex) {
if (!fileinfo->plugin) {
// return dummy block for a failed track
mutex_lock (mutex);
block->size = 0;
block->first = 1;
block->last = 1;
block->track = track;
_firstblock = 1;
return 0;
}
// clip size to max possible, with current sample format
int size = BLOCK_SIZE;
int samplesize = fileinfo->fmt.channels * (fileinfo->fmt.bps>>3);
int mod = size % samplesize;
if (mod) {
size -= mod;
}
// replaygain settings
if (_rg_settingschanged || _prev_rg_track != track) {
_prev_rg_track = track;
_rg_settingschanged = 0;
ddb_replaygain_settings_t rg_settings;
rg_settings._size = sizeof (rg_settings);
replaygain_init_settings (&rg_settings, track);
replaygain_set_current (&rg_settings);
}
// NOTE: streamer_set_bitrate may be called during decoder->read, and set immediated bitrate of the block
curr_block_bitrate = -1;
int rb = fileinfo->plugin->read (fileinfo, block->buf, size);
if (rb < 0) {
return -1;
}
mutex_lock (mutex);
block->bitrate = curr_block_bitrate;
block->pos = 0;
block->size = rb;
memcpy (&block->fmt, &fileinfo->fmt, sizeof (ddb_waveformat_t));
block->track = track;
int input_does_rg = fileinfo->plugin->plugin.flags & DDB_PLUGIN_FLAG_REPLAYGAIN;
if (!input_does_rg) {
replaygain_apply (&fileinfo->fmt, block->buf, block->size);
}
if (_firstblock) {
block->first = 1;
_firstblock = 0;
}
else {
block->first = 0;
}
if (rb != size) {
block->last = 1;
_firstblock = 1;
}
else {
block->last = 0;
}
return 0;
}
void
streamreader_enqueue_block (streamblock_t *block) {
// block is passed just for sanity checking
assert (block->track);
if (!block_data) {
block_data = block_next;
}
block_next = block_next->next;
if (!block_next) {
block_next = blocks;
}
block->queued = 1;
numblocks_ready++;
}
void
streamer_set_bitrate (int bitrate) {
curr_block_bitrate = bitrate;
}
streamblock_t *
streamreader_get_curr_block (void) {
return block_data;
}
void
streamreader_next_block (void) {
if (block_data) {
block_data->pos = -1;
block_data->track = NULL;
block_data->queued = 0;
block_data = block_data->next;
if (!block_data) {
block_data = blocks;
}
numblocks_ready--;
if (numblocks_ready < 0) {
numblocks_ready = 0;
}
}
if (block_data && !block_data->queued) {
block_data = NULL; // no available blocks with data
}
}
void
streamreader_reset (void) {
streamblock_t *b = blocks;
while (b) {
b->pos = -1;
if (b->track) {
b->track = NULL;
}
b->queued = 0;
b = b->next;
}
block_next = blocks;
block_data = NULL;
numblocks_ready = 0;
_firstblock = 0;
}
int
streamreader_num_blocks_ready (void) {
return numblocks_ready;
}