DecoderInternal.cxx 2.28 KB
Newer Older
1
/*
2
 * Copyright 2003-2016 The Music Player Daemon Project
3 4 5 6 7 8 9 10 11 12 13
 * http://www.musicpd.org
 *
 * 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.
18 19
 */

20
#include "config.h"
21 22
#include "DecoderInternal.hxx"
#include "DecoderControl.hxx"
23
#include "pcm/PcmConvert.hxx"
24 25 26
#include "MusicPipe.hxx"
#include "MusicBuffer.hxx"
#include "MusicChunk.hxx"
27
#include "tag/Tag.hxx"
28 29 30

#include <assert.h>

31
Decoder::~Decoder()
32 33 34 35
{
	/* caller must flush the chunk */
	assert(chunk == nullptr);

36 37 38 39 40
	if (convert != nullptr) {
		convert->Close();
		delete convert;
	}

Max Kellermann's avatar
Max Kellermann committed
41 42 43
	delete song_tag;
	delete stream_tag;
	delete decoder_tag;
44 45
}

46 47 48 49
/**
 * All chunks are full of decoded data; wait for the player to free
 * one.
 */
50
static DecoderCommand
51
need_chunks(DecoderControl &dc)
52
{
53
	if (dc.command == DecoderCommand::NONE)
54
		dc.Wait();
55

56
	return dc.command;
57 58
}

59 60 61 62 63 64 65
static DecoderCommand
LockNeedChunks(DecoderControl &dc)
{
	const ScopeLock protect(dc.mutex);
	return need_chunks(dc);
}

66
MusicChunk *
67
Decoder::GetChunk()
68
{
69
	DecoderCommand cmd;
70

71 72
	if (chunk != nullptr)
		return chunk;
73

74
	do {
75 76 77 78 79 80 81
		chunk = dc.buffer->Allocate();
		if (chunk != nullptr) {
			chunk->replay_gain_serial = replay_gain_serial;
			if (replay_gain_serial != 0)
				chunk->replay_gain_info = replay_gain_info;

			return chunk;
82
		}
83

84
		cmd = LockNeedChunks(dc);
85
	} while (cmd == DecoderCommand::NONE);
86

87
	return nullptr;
88 89
}

90
void
91
Decoder::FlushChunk()
92
{
Max Kellermann's avatar
Max Kellermann committed
93 94 95
	assert(!seeking);
	assert(!initial_seek_running);
	assert(!initial_seek_pending);
96
	assert(chunk != nullptr);
97

98 99
	if (chunk->IsEmpty())
		dc.buffer->Return(chunk);
100
	else
101
		dc.pipe->Push(chunk);
102

103
	chunk = nullptr;
104

105
	const ScopeLock protect(dc.mutex);
106 107
	if (dc.client_is_waiting)
		dc.client_cond.signal();
108
}