DecoderControl.cxx 3.09 KB
Newer Older
1
/*
2
 * Copyright (C) 2003-2013 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 "MusicPipe.hxx"
23
#include "Song.hxx"
24

25 26
#include <glib.h>

27
#include <assert.h>
28

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

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

41
	if (song != nullptr)
42
		song->Free();
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
bool
58
DecoderControl::IsCurrentSong(const Song &_song) const
59
{
60
	switch (state) {
61 62
	case DecoderState::STOP:
	case DecoderState::ERROR:
63 64
		return false;

65 66
	case DecoderState::START:
	case DecoderState::DECODE:
67
		return SongEquals(*song, _song);
68 69 70
	}

	assert(false);
71
	gcc_unreachable();
72 73
}

74
void
75 76 77
DecoderControl::Start(Song *_song,
		      unsigned _start_ms, unsigned _end_ms,
		      MusicBuffer &_buffer, MusicPipe &_pipe)
Max Kellermann's avatar
Max Kellermann committed
78
{
79
	assert(_song != nullptr);
80
	assert(_pipe.IsEmpty());
Max Kellermann's avatar
Max Kellermann committed
81

82
	if (song != nullptr)
83
		song->Free();
84 85 86 87

	song = _song;
	start_ms = _start_ms;
	end_ms = _end_ms;
88
	buffer = &_buffer;
89
	pipe = &_pipe;
90

91
	LockSynchronousCommand(DecoderCommand::START);
Max Kellermann's avatar
Max Kellermann committed
92 93
}

94
void
95
DecoderControl::Stop()
Max Kellermann's avatar
Max Kellermann committed
96
{
97
	Lock();
98

99
	if (command != DecoderCommand::NONE)
100 101 102 103
		/* 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). */
104
		SynchronousCommandLocked(DecoderCommand::STOP);
105

106
	if (state != DecoderState::STOP && state != DecoderState::ERROR)
107
		SynchronousCommandLocked(DecoderCommand::STOP);
108

109
	Unlock();
Max Kellermann's avatar
Max Kellermann committed
110 111
}

112
bool
113
DecoderControl::Seek(double where)
Max Kellermann's avatar
Max Kellermann committed
114
{
115
	assert(state != DecoderState::START);
Max Kellermann's avatar
Max Kellermann committed
116 117
	assert(where >= 0.0);

118 119
	if (state == DecoderState::STOP ||
	    state == DecoderState::ERROR || !seekable)
120
		return false;
Max Kellermann's avatar
Max Kellermann committed
121

122 123
	seek_where = where;
	seek_error = false;
124
	LockSynchronousCommand(DecoderCommand::SEEK);
Max Kellermann's avatar
Max Kellermann committed
125

126
	return !seek_error;
Max Kellermann's avatar
Max Kellermann committed
127
}
128 129

void
130
DecoderControl::Quit()
131
{
132
	assert(thread.IsDefined());
133

134
	quit = true;
135
	LockAsynchronousCommand(DecoderCommand::STOP);
136

137
	thread.Join();
138
}
139 140

void
141
DecoderControl::CycleMixRamp()
142
{
143 144
	previous_mix_ramp = std::move(mix_ramp);
	mix_ramp.Clear();
145
}