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
|
/*
* Copyright (C) 2003-2013 The Music Player Daemon Project
* http://www.musicpd.org
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#include "config.h"
#include "PlayerControl.hxx"
#include "Idle.hxx"
#include "song.h"
#include "DecoderControl.hxx"
#include "Main.hxx"
#include <cmath>
#include <assert.h>
#include <stdio.h>
static void
pc_enqueue_song_locked(struct player_control *pc, struct song *song);
player_control::player_control(unsigned _buffer_chunks,
unsigned _buffered_before_play)
:buffer_chunks(_buffer_chunks),
buffered_before_play(_buffered_before_play),
thread(nullptr),
command(PLAYER_COMMAND_NONE),
state(PLAYER_STATE_STOP),
error_type(PLAYER_ERROR_NONE),
error(nullptr),
next_song(nullptr),
cross_fade_seconds(0),
mixramp_db(0),
mixramp_delay_seconds(std::nanf("")),
total_play_time(0),
border_pause(false)
{
}
player_control::~player_control()
{
if (next_song != nullptr)
song_free(next_song);
}
static void
player_command_wait_locked(struct player_control *pc)
{
while (pc->command != PLAYER_COMMAND_NONE)
pc->cond.wait(pc->mutex);
}
static void
player_command_locked(struct player_control *pc, enum player_command cmd)
{
assert(pc->command == PLAYER_COMMAND_NONE);
pc->command = cmd;
pc->Signal();
player_command_wait_locked(pc);
}
static void
player_command(struct player_control *pc, enum player_command cmd)
{
pc->Lock();
player_command_locked(pc, cmd);
pc->Unlock();
}
void
player_control::Play(struct song *song)
{
assert(song != NULL);
Lock();
if (state != PLAYER_STATE_STOP)
player_command_locked(this, PLAYER_COMMAND_STOP);
assert(next_song == nullptr);
pc_enqueue_song_locked(this, song);
assert(next_song == nullptr);
Unlock();
idle_add(IDLE_PLAYER);
}
void
player_control::Cancel()
{
player_command(this, PLAYER_COMMAND_CANCEL);
assert(next_song == NULL);
}
void
player_control::Stop()
{
player_command(this, PLAYER_COMMAND_CLOSE_AUDIO);
assert(next_song == nullptr);
idle_add(IDLE_PLAYER);
}
void
player_control::UpdateAudio()
{
player_command(this, PLAYER_COMMAND_UPDATE_AUDIO);
}
void
player_control::Kill()
{
assert(thread != NULL);
player_command(this, PLAYER_COMMAND_EXIT);
g_thread_join(thread);
thread = NULL;
idle_add(IDLE_PLAYER);
}
void
player_control::Pause()
{
Lock();
if (state != PLAYER_STATE_STOP) {
player_command_locked(this, PLAYER_COMMAND_PAUSE);
idle_add(IDLE_PLAYER);
}
Unlock();
}
static void
pc_pause_locked(struct player_control *pc)
{
if (pc->state != PLAYER_STATE_STOP) {
player_command_locked(pc, PLAYER_COMMAND_PAUSE);
idle_add(IDLE_PLAYER);
}
}
void
player_control::SetPause(bool pause_flag)
{
Lock();
switch (state) {
case PLAYER_STATE_STOP:
break;
case PLAYER_STATE_PLAY:
if (pause_flag)
pc_pause_locked(this);
break;
case PLAYER_STATE_PAUSE:
if (!pause_flag)
pc_pause_locked(this);
break;
}
Unlock();
}
void
player_control::SetBorderPause(bool _border_pause)
{
Lock();
border_pause = _border_pause;
Unlock();
}
player_status
player_control::GetStatus()
{
player_status status;
Lock();
player_command_locked(this, PLAYER_COMMAND_REFRESH);
status.state = state;
if (state != PLAYER_STATE_STOP) {
status.bit_rate = bit_rate;
status.audio_format = audio_format;
status.total_time = total_time;
status.elapsed_time = elapsed_time;
}
Unlock();
return status;
}
void
player_control::SetError(player_error type, GError *_error)
{
assert(type != PLAYER_ERROR_NONE);
assert(_error != NULL);
if (error_type != PLAYER_ERROR_NONE)
g_error_free(error);
error_type = type;
error = _error;
}
void
player_control::ClearError()
{
Lock();
if (error_type != PLAYER_ERROR_NONE) {
error_type = PLAYER_ERROR_NONE;
g_error_free(error);
}
Unlock();
}
char *
player_control::GetErrorMessage() const
{
Lock();
char *message = error_type != PLAYER_ERROR_NONE
? g_strdup(error->message)
: NULL;
Unlock();
return message;
}
static void
pc_enqueue_song_locked(struct player_control *pc, struct song *song)
{
assert(song != NULL);
assert(pc->next_song == NULL);
pc->next_song = song;
player_command_locked(pc, PLAYER_COMMAND_QUEUE);
}
void
player_control::EnqueueSong(struct song *song)
{
assert(song != NULL);
Lock();
pc_enqueue_song_locked(this, song);
Unlock();
}
bool
player_control::Seek(struct song *song, float seek_time)
{
assert(song != NULL);
Lock();
if (next_song != nullptr)
song_free(next_song);
next_song = song;
seek_where = seek_time;
player_command_locked(this, PLAYER_COMMAND_SEEK);
Unlock();
assert(next_song == nullptr);
idle_add(IDLE_PLAYER);
return true;
}
void
player_control::SetCrossFade(float _cross_fade_seconds)
{
if (_cross_fade_seconds < 0)
_cross_fade_seconds = 0;
cross_fade_seconds = _cross_fade_seconds;
idle_add(IDLE_OPTIONS);
}
void
player_control::SetMixRampDb(float _mixramp_db)
{
mixramp_db = _mixramp_db;
idle_add(IDLE_OPTIONS);
}
void
player_control::SetMixRampDelay(float _mixramp_delay_seconds)
{
mixramp_delay_seconds = _mixramp_delay_seconds;
idle_add(IDLE_OPTIONS);
}
|