DecoderControl.cxx 3.07 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
#include <assert.h>
26

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

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

39
	if (song != nullptr)
40
		song->Free();
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 Song &_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 SongEquals(*song, _song);
66 67 68
	}

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

72
void
73 74 75
DecoderControl::Start(Song *_song,
		      unsigned _start_ms, unsigned _end_ms,
		      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
	if (song != nullptr)
81
		song->Free();
82 83 84 85

	song = _song;
	start_ms = _start_ms;
	end_ms = _end_ms;
86
	buffer = &_buffer;
87
	pipe = &_pipe;
88

89
	LockSynchronousCommand(DecoderCommand::START);
Max Kellermann's avatar
Max Kellermann committed
90 91
}

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

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

104
	if (state != DecoderState::STOP && state != DecoderState::ERROR)
105
		SynchronousCommandLocked(DecoderCommand::STOP);
106

107
	Unlock();
Max Kellermann's avatar
Max Kellermann committed
108 109
}

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

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

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

124
	return !seek_error;
Max Kellermann's avatar
Max Kellermann committed
125
}
126 127

void
128
DecoderControl::Quit()
129
{
130
	assert(thread.IsDefined());
131

132
	quit = true;
133
	LockAsynchronousCommand(DecoderCommand::STOP);
134

135
	thread.Join();
136
}
137 138

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