DecoderControl.cxx 4.04 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) noexcept
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() noexcept
39
{
40
	ClearError();
41 42
}

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

	client_cond.wait(mutex);

	assert(client_is_waiting);
	client_is_waiting = false;
}

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

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

	seekable = _seekable;
	total_time = _duration;

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

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

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

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

92
void
93
DecoderControl::Start(std::unique_ptr<DetachedSong> _song,
94
		      SongTime _start_time, SongTime _end_time,
95
		      MusicBuffer &_buffer, MusicPipe &_pipe) noexcept
Max Kellermann's avatar
Max Kellermann committed
96
{
97
	assert(_song != nullptr);
98
	assert(_pipe.IsEmpty());
Max Kellermann's avatar
Max Kellermann committed
99

100
	song = std::move(_song);
101 102
	start_time = _start_time;
	end_time = _end_time;
103
	buffer = &_buffer;
104
	pipe = &_pipe;
105

106 107
	ClearError();
	SynchronousCommandLocked(DecoderCommand::START);
Max Kellermann's avatar
Max Kellermann committed
108 109
}

110
void
111
DecoderControl::Stop() noexcept
Max Kellermann's avatar
Max Kellermann committed
112
{
113
	if (command != DecoderCommand::NONE)
114 115 116 117
		/* 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). */
118
		SynchronousCommandLocked(DecoderCommand::STOP);
119

120
	if (state != DecoderState::STOP && state != DecoderState::ERROR)
121
		SynchronousCommandLocked(DecoderCommand::STOP);
Max Kellermann's avatar
Max Kellermann committed
122 123
}

124 125
void
DecoderControl::Seek(SongTime t)
Max Kellermann's avatar
Max Kellermann committed
126
{
127
	assert(state != DecoderState::START);
128
	assert(state != DecoderState::ERROR);
Max Kellermann's avatar
Max Kellermann committed
129

130 131
	switch (state) {
	case DecoderState::START:
132
	case DecoderState::ERROR:
133 134 135
		gcc_unreachable();

	case DecoderState::STOP:
136 137
		/* TODO: if this happens, the caller should be given a
		   chance to restart the decoder */
138
		throw std::runtime_error("Decoder is dead");
139 140 141 142 143

	case DecoderState::DECODE:
		break;
	}

144 145
	if (!seekable)
		throw std::runtime_error("Not seekable");
Max Kellermann's avatar
Max Kellermann committed
146

147
	seek_time = t;
148
	seek_error = false;
149
	SynchronousCommandLocked(DecoderCommand::SEEK);
Max Kellermann's avatar
Max Kellermann committed
150

151 152
	if (seek_error)
		throw std::runtime_error("Decoder failed to seek");
Max Kellermann's avatar
Max Kellermann committed
153
}
154 155

void
156
DecoderControl::Quit() noexcept
157
{
158
	assert(thread.IsDefined());
159

160
	quit = true;
161
	LockAsynchronousCommand(DecoderCommand::STOP);
162

163
	thread.Join();
164
}
165 166

void
167
DecoderControl::CycleMixRamp() noexcept
168
{
169 170
	previous_mix_ramp = std::move(mix_ramp);
	mix_ramp.Clear();
171
}