DecoderControl.cxx 4.07 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 33
			       const ReplayGainConfig &_replay_gain_config)
	:mutex(_mutex), client_cond(_client_cond),
34
	 configured_audio_format(_configured_audio_format),
35
	 replay_gain_config(_replay_gain_config) {}
36

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

41
	delete song;
42 43
}

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

	client_cond.wait(mutex);

	assert(client_is_waiting);
	client_is_waiting = false;
}

56 57 58 59 60 61 62 63 64 65 66
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;
67
	out_audio_format = audio_format.WithMask(configured_audio_format);
68 69 70 71 72 73 74 75

	seekable = _seekable;
	total_time = _duration;

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

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

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

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

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

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

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

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

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

119
	if (command != DecoderCommand::NONE)
120 121 122 123
		/* 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). */
124
		SynchronousCommandLocked(DecoderCommand::STOP);
125

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

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

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

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

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

	case DecoderState::DECODE:
		break;
	}

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

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

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

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

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

171
	thread.Join();
172
}
173 174

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