Control.cxx 4.25 KB
Newer Older
1
/*
2
 * Copyright 2003-2018 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 "Control.hxx"
21
#include "MusicPipe.hxx"
22
#include "song/DetachedSong.hxx"
23 24

#include <stdexcept>
25

26
#include <assert.h>
27

28
DecoderControl::DecoderControl(Mutex &_mutex, Cond &_client_cond,
29
			       InputCacheManager *_input_cache,
30
			       const AudioFormat _configured_audio_format,
31
			       const ReplayGainConfig &_replay_gain_config) noexcept
32
	:thread(BIND_THIS_METHOD(RunThread)),
33
	 input_cache(_input_cache),
34
	 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(std::unique_lock<Mutex> &lock) noexcept
45 46 47 48
{
	assert(!client_is_waiting);
	client_is_waiting = true;

49
	client_cond.wait(lock);
50 51 52 53 54

	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

	seekable = _seekable;
	total_time = _duration;

	state = DecoderState::DECODE;
72
	client_cond.notify_one();
73 74
}

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 94
DecoderControl::Start(std::unique_lock<Mutex> &lock,
		      std::unique_ptr<DetachedSong> _song,
95
		      SongTime _start_time, SongTime _end_time,
96 97
		      MusicBuffer &_buffer,
		      std::shared_ptr<MusicPipe> _pipe) noexcept
Max Kellermann's avatar
Max Kellermann committed
98
{
99
	assert(_song != nullptr);
100
	assert(_pipe->IsEmpty());
Max Kellermann's avatar
Max Kellermann committed
101

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

108
	ClearError();
109
	SynchronousCommandLocked(lock, DecoderCommand::START);
Max Kellermann's avatar
Max Kellermann committed
110 111
}

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

122
	if (state != DecoderState::STOP && state != DecoderState::ERROR)
123
		SynchronousCommandLocked(lock, DecoderCommand::STOP);
Max Kellermann's avatar
Max Kellermann committed
124 125
}

126
void
127
DecoderControl::Seek(std::unique_lock<Mutex> &lock, SongTime t)
Max Kellermann's avatar
Max Kellermann committed
128
{
129
	assert(state != DecoderState::START);
130
	assert(state != DecoderState::ERROR);
Max Kellermann's avatar
Max Kellermann committed
131

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

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

	case DecoderState::DECODE:
		break;
	}

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

149
	seek_time = t;
150
	seek_error = false;
151
	SynchronousCommandLocked(lock, DecoderCommand::SEEK);
Max Kellermann's avatar
Max Kellermann committed
152

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

void
158
DecoderControl::Quit() noexcept
159
{
160
	assert(thread.IsDefined());
161

162
	quit = true;
163
	LockAsynchronousCommand(DecoderCommand::STOP);
164

165
	thread.Join();
166
}
167 168

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