DecoderControl.cxx 4.11 KB
Newer Older
1
/*
Max Kellermann's avatar
Max Kellermann committed
2
 * Copyright 2003-2017 The Music Player Daemon Project
3
 * http://www.musicpd.org
Max Kellermann's avatar
Max Kellermann committed
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.
Max Kellermann's avatar
Max Kellermann committed
18 19
 */

20
#include "config.h"
21
#include "DecoderControl.hxx"
22
#include "DecoderError.hxx"
23
#include "MusicPipe.hxx"
24
#include "DetachedSong.hxx"
25 26

#include <stdexcept>
27

28
#include <assert.h>
29

30
DecoderControl::DecoderControl(Mutex &_mutex, Cond &_client_cond,
31
			       const AudioFormat _configured_audio_format,
32
			       const ReplayGainConfig &_replay_gain_config)
33 34
	:thread(BIND_THIS_METHOD(RunThread)),
	 mutex(_mutex), client_cond(_client_cond),
35
	 configured_audio_format(_configured_audio_format),
36
	 replay_gain_config(_replay_gain_config) {}
37

38
DecoderControl::~DecoderControl()
39
{
40
	ClearError();
41

42
	delete song;
43 44
}

45 46 47 48 49 50 51 52 53 54 55 56
void
DecoderControl::WaitForDecoder()
{
	assert(!client_is_waiting);
	client_is_waiting = true;

	client_cond.wait(mutex);

	assert(client_is_waiting);
	client_is_waiting = false;
}

57 58 59 60 61 62 63 64 65 66 67
void
DecoderControl::SetReady(const AudioFormat audio_format,
			 bool _seekable, SignedSongTime _duration)
{
	assert(state == DecoderState::START);
	assert(pipe != nullptr);
	assert(pipe->IsEmpty());
	assert(audio_format.IsDefined());
	assert(audio_format.IsValid());

	in_audio_format = audio_format;
68
	out_audio_format = audio_format.WithMask(configured_audio_format);
69 70 71 72 73 74 75 76

	seekable = _seekable;
	total_time = _duration;

	state = DecoderState::DECODE;
	client_cond.signal();
}

77
bool
78
DecoderControl::IsCurrentSong(const DetachedSong &_song) const noexcept
79
{
80
	switch (state) {
81 82
	case DecoderState::STOP:
	case DecoderState::ERROR:
83 84
		return false;

85 86
	case DecoderState::START:
	case DecoderState::DECODE:
87
		return song->IsSame(_song);
88 89 90
	}

	assert(false);
91
	gcc_unreachable();
92 93
}

94
void
95
DecoderControl::Start(DetachedSong *_song,
96
		      SongTime _start_time, SongTime _end_time,
97
		      MusicBuffer &_buffer, MusicPipe &_pipe)
Max Kellermann's avatar
Max Kellermann committed
98
{
99 100
	const std::lock_guard<Mutex> protect(mutex);

101
	assert(_song != nullptr);
102
	assert(_pipe.IsEmpty());
Max Kellermann's avatar
Max Kellermann committed
103

104
	delete song;
105
	song = _song;
106 107
	start_time = _start_time;
	end_time = _end_time;
108
	buffer = &_buffer;
109
	pipe = &_pipe;
110

111 112
	ClearError();
	SynchronousCommandLocked(DecoderCommand::START);
Max Kellermann's avatar
Max Kellermann committed
113 114
}

115
void
116
DecoderControl::Stop()
Max Kellermann's avatar
Max Kellermann committed
117
{
118
	const std::lock_guard<Mutex> protect(mutex);
119

120
	if (command != DecoderCommand::NONE)
121 122 123 124
		/* Attempt to cancel the current command.  If it's too
		   late and the decoder thread is already executing
		   the old command, we'll call STOP again in this
		   function (see below). */
125
		SynchronousCommandLocked(DecoderCommand::STOP);
126

127
	if (state != DecoderState::STOP && state != DecoderState::ERROR)
128
		SynchronousCommandLocked(DecoderCommand::STOP);
Max Kellermann's avatar
Max Kellermann committed
129 130
}

131 132
void
DecoderControl::Seek(SongTime t)
Max Kellermann's avatar
Max Kellermann committed
133
{
134 135
	const std::lock_guard<Mutex> protect(mutex);

136
	assert(state != DecoderState::START);
137
	assert(state != DecoderState::ERROR);
Max Kellermann's avatar
Max Kellermann committed
138

139 140
	switch (state) {
	case DecoderState::START:
141
	case DecoderState::ERROR:
142 143 144
		gcc_unreachable();

	case DecoderState::STOP:
145 146
		/* TODO: if this happens, the caller should be given a
		   chance to restart the decoder */
147
		throw std::runtime_error("Decoder is dead");
148 149 150 151 152

	case DecoderState::DECODE:
		break;
	}

153 154
	if (!seekable)
		throw std::runtime_error("Not seekable");
Max Kellermann's avatar
Max Kellermann committed
155

156
	seek_time = t;
157
	seek_error = false;
158
	SynchronousCommandLocked(DecoderCommand::SEEK);
Max Kellermann's avatar
Max Kellermann committed
159

160 161
	if (seek_error)
		throw std::runtime_error("Decoder failed to seek");
Max Kellermann's avatar
Max Kellermann committed
162
}
163 164

void
165
DecoderControl::Quit()
166
{
167
	assert(thread.IsDefined());
168

169
	quit = true;
170
	LockAsynchronousCommand(DecoderCommand::STOP);
171

172
	thread.Join();
173
}
174 175

void
176
DecoderControl::CycleMixRamp()
177
{
178 179
	previous_mix_ramp = std::move(mix_ramp);
	mix_ramp.Clear();
180
}