PlayerControl.cxx 4.6 KB
Newer Older
1
/*
2
 * Copyright (C) 2003-2013 The Music Player Daemon Project
3
 * http://www.musicpd.org
4 5 6 7 8 9 10 11 12 13
 *
 * 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.
14 15 16 17
 *
 * 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.
18 19
 */

20
#include "config.h"
21
#include "PlayerControl.hxx"
Max Kellermann's avatar
Max Kellermann committed
22
#include "Idle.hxx"
23
#include "Song.hxx"
24
#include "DecoderControl.hxx"
25

26 27
#include <cmath>

Max Kellermann's avatar
Max Kellermann committed
28
#include <assert.h>
29

30 31
PlayerControl::PlayerControl(unsigned _buffer_chunks,
			     unsigned _buffered_before_play)
32 33
	:buffer_chunks(_buffer_chunks),
	 buffered_before_play(_buffered_before_play),
34 35 36
	 command(PlayerCommand::NONE),
	 state(PlayerState::STOP),
	 error_type(PlayerError::NONE),
37
	 tagged_song(nullptr),
38
	 next_song(nullptr),
39 40
	 total_play_time(0),
	 border_pause(false)
41 42 43
{
}

44
PlayerControl::~PlayerControl()
45
{
46
	if (next_song != nullptr)
47
		next_song->Free();
48 49 50

	if (tagged_song != nullptr)
		tagged_song->Free();
51 52
}

53
void
54
PlayerControl::Play(Song *song)
55
{
56
	assert(song != nullptr);
57

58
	Lock();
59

60 61
	if (state != PlayerState::STOP)
		SynchronousCommand(PlayerCommand::STOP);
62

63
	assert(next_song == nullptr);
64

65
	EnqueueSongLocked(song);
66

67
	assert(next_song == nullptr);
68

69
	Unlock();
70 71
}

72
void
73
PlayerControl::Cancel()
74
{
75
	LockSynchronousCommand(PlayerCommand::CANCEL);
76
	assert(next_song == nullptr);
77
}
78

79
void
80
PlayerControl::Stop()
81
{
82
	LockSynchronousCommand(PlayerCommand::CLOSE_AUDIO);
83
	assert(next_song == nullptr);
84 85

	idle_add(IDLE_PLAYER);
86 87
}

88
void
89
PlayerControl::UpdateAudio()
90
{
91
	LockSynchronousCommand(PlayerCommand::UPDATE_AUDIO);
92 93
}

94
void
95
PlayerControl::Kill()
96
{
97
	assert(thread.IsDefined());
98

99
	LockSynchronousCommand(PlayerCommand::EXIT);
100
	thread.Join();
101 102

	idle_add(IDLE_PLAYER);
103 104
}

105
void
106
PlayerControl::PauseLocked()
107
{
108 109
	if (state != PlayerState::STOP) {
		SynchronousCommand(PlayerCommand::PAUSE);
110 111 112 113
		idle_add(IDLE_PLAYER);
	}
}

114
void
115
PlayerControl::Pause()
116
{
117 118 119
	Lock();
	PauseLocked();
	Unlock();
120 121
}

122
void
123
PlayerControl::SetPause(bool pause_flag)
124
{
125
	Lock();
126

127
	switch (state) {
128
	case PlayerState::STOP:
129 130
		break;

131
	case PlayerState::PLAY:
132
		if (pause_flag)
133
			PauseLocked();
134
		break;
135

136
	case PlayerState::PAUSE:
137
		if (!pause_flag)
138
			PauseLocked();
139 140
		break;
	}
141

142
	Unlock();
143 144
}

145
void
146
PlayerControl::SetBorderPause(bool _border_pause)
147
{
148 149 150
	Lock();
	border_pause = _border_pause;
	Unlock();
151 152
}

153
player_status
154
PlayerControl::GetStatus()
155
{
156
	player_status status;
157

158
	Lock();
159
	SynchronousCommand(PlayerCommand::REFRESH);
160

161 162
	status.state = state;

163
	if (state != PlayerState::STOP) {
164 165 166 167
		status.bit_rate = bit_rate;
		status.audio_format = audio_format;
		status.total_time = total_time;
		status.elapsed_time = elapsed_time;
168
	}
169

170 171 172
	Unlock();

	return status;
173 174
}

175
void
176
PlayerControl::SetError(PlayerError type, Error &&_error)
177
{
178
	assert(type != PlayerError::NONE);
179
	assert(_error.IsDefined());
180

181
	error_type = type;
182
	error = std::move(_error);
183 184
}

185
void
186
PlayerControl::ClearError()
187
{
188
	Lock();
189

190 191
	if (error_type != PlayerError::NONE) {
	    error_type = PlayerError::NONE;
192
	    error.Clear();
193 194
	}

195
	Unlock();
196 197
}

198
void
199
PlayerControl::LockSetTaggedSong(const Song &song)
200 201 202 203 204 205 206 207 208
{
	Lock();
	if (tagged_song != nullptr)
		tagged_song->Free();
	tagged_song = song.DupDetached();
	Unlock();
}

void
209
PlayerControl::ClearTaggedSong()
210 211 212 213 214 215 216
{
	if (tagged_song != nullptr) {
		tagged_song->Free();
		tagged_song = nullptr;
	}
}

217
void
218
PlayerControl::EnqueueSong(Song *song)
219
{
220
	assert(song != nullptr);
221

222
	Lock();
223
	EnqueueSongLocked(song);
224
	Unlock();
225 226
}

227
bool
228
PlayerControl::Seek(Song *song, float seek_time)
229
{
230
	assert(song != nullptr);
231

232
	Lock();
233

234
	if (next_song != nullptr)
235
		next_song->Free();
236

237 238
	next_song = song;
	seek_where = seek_time;
239
	SynchronousCommand(PlayerCommand::SEEK);
240
	Unlock();
241

242
	assert(next_song == nullptr);
243

244
	idle_add(IDLE_PLAYER);
245

246
	return true;
247 248
}

249
void
250
PlayerControl::SetCrossFade(float _cross_fade_seconds)
251
{
252 253
	if (_cross_fade_seconds < 0)
		_cross_fade_seconds = 0;
254
	cross_fade.duration = _cross_fade_seconds;
255 256

	idle_add(IDLE_OPTIONS);
257 258
}

259
void
260
PlayerControl::SetMixRampDb(float _mixramp_db)
261
{
262
	cross_fade.mixramp_db = _mixramp_db;
263 264 265 266 267

	idle_add(IDLE_OPTIONS);
}

void
268
PlayerControl::SetMixRampDelay(float _mixramp_delay_seconds)
269
{
270
	cross_fade.mixramp_delay = _mixramp_delay_seconds;
271 272 273

	idle_add(IDLE_OPTIONS);
}