DecoderControl.cxx 3.5 KB
Newer Older
1
/*
2
 * Copyright 2003-2016 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 <assert.h>
27

28 29 30
DecoderControl::DecoderControl(Mutex &_mutex, Cond &_client_cond)
	:mutex(_mutex), client_cond(_client_cond),
	 state(DecoderState::STOP),
31
	 command(DecoderCommand::NONE),
32
	 client_is_waiting(false),
33
	 song(nullptr),
34
	 replay_gain_db(0), replay_gain_prev_db(0) {}
35

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

40
	delete song;
41 42
}

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

	client_cond.wait(mutex);

	assert(client_is_waiting);
	client_is_waiting = false;
}

55
bool
56
DecoderControl::IsCurrentSong(const DetachedSong &_song) const
57
{
58
	switch (state) {
59 60
	case DecoderState::STOP:
	case DecoderState::ERROR:
61 62
		return false;

63 64
	case DecoderState::START:
	case DecoderState::DECODE:
65
		return song->IsSame(_song);
66 67 68
	}

	assert(false);
69
	gcc_unreachable();
70 71
}

72
void
73
DecoderControl::Start(DetachedSong *_song,
74
		      SongTime _start_time, SongTime _end_time,
75
		      MusicBuffer &_buffer, MusicPipe &_pipe)
Max Kellermann's avatar
Max Kellermann committed
76
{
77
	assert(_song != nullptr);
78
	assert(_pipe.IsEmpty());
Max Kellermann's avatar
Max Kellermann committed
79

80
	delete song;
81
	song = _song;
82 83
	start_time = _start_time;
	end_time = _end_time;
84
	buffer = &_buffer;
85
	pipe = &_pipe;
86

87
	LockSynchronousCommand(DecoderCommand::START);
Max Kellermann's avatar
Max Kellermann committed
88 89
}

90
void
91
DecoderControl::Stop()
Max Kellermann's avatar
Max Kellermann committed
92
{
93
	const ScopeLock protect(mutex);
94

95
	if (command != DecoderCommand::NONE)
96 97 98 99
		/* 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). */
100
		SynchronousCommandLocked(DecoderCommand::STOP);
101

102
	if (state != DecoderState::STOP && state != DecoderState::ERROR)
103
		SynchronousCommandLocked(DecoderCommand::STOP);
Max Kellermann's avatar
Max Kellermann committed
104 105
}

106
bool
107
DecoderControl::Seek(SongTime t, Error &error_r)
Max Kellermann's avatar
Max Kellermann committed
108
{
109
	assert(state != DecoderState::START);
110
	assert(state != DecoderState::ERROR);
Max Kellermann's avatar
Max Kellermann committed
111

112 113
	switch (state) {
	case DecoderState::START:
114
	case DecoderState::ERROR:
115 116 117
		gcc_unreachable();

	case DecoderState::STOP:
118 119 120
		/* TODO: if this happens, the caller should be given a
		   chance to restart the decoder */
		error_r.Set(decoder_domain, "Decoder is dead");
121 122 123 124 125 126
		return false;

	case DecoderState::DECODE:
		break;
	}

127 128
	if (!seekable) {
		error_r.Set(decoder_domain, "Not seekable");
129
		return false;
130
	}
Max Kellermann's avatar
Max Kellermann committed
131

132
	seek_time = t;
133
	seek_error = false;
134
	LockSynchronousCommand(DecoderCommand::SEEK);
Max Kellermann's avatar
Max Kellermann committed
135

136 137 138 139 140 141
	if (seek_error) {
		error_r.Set(decoder_domain, "Decoder failed to seek");
		return false;
	}

	return true;
Max Kellermann's avatar
Max Kellermann committed
142
}
143 144

void
145
DecoderControl::Quit()
146
{
147
	assert(thread.IsDefined());
148

149
	quit = true;
150
	LockAsynchronousCommand(DecoderCommand::STOP);
151

152
	thread.Join();
153
}
154 155

void
156
DecoderControl::CycleMixRamp()
157
{
158 159
	previous_mix_ramp = std::move(mix_ramp);
	mix_ramp.Clear();
160
}