DecoderInternal.cxx 2.18 KB
Newer Older
1
/*
Max Kellermann's avatar
Max Kellermann committed
2
 * Copyright (C) 2003-2014 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
MusicChunk *
60
Decoder::GetChunk()
61
{
62
	DecoderCommand cmd;
63

64 65
	if (chunk != nullptr)
		return chunk;
66

67
	do {
68 69 70 71 72 73 74
		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;
75
		}
76

77
		dc.Lock();
78
		cmd = need_chunks(dc);
79
		dc.Unlock();
80
	} while (cmd == DecoderCommand::NONE);
81

82
	return nullptr;
83 84
}

85
void
86
Decoder::FlushChunk()
87
{
Max Kellermann's avatar
Max Kellermann committed
88 89 90
	assert(!seeking);
	assert(!initial_seek_running);
	assert(!initial_seek_pending);
91
	assert(chunk != nullptr);
92

93 94
	if (chunk->IsEmpty())
		dc.buffer->Return(chunk);
95
	else
96
		dc.pipe->Push(chunk);
97

98
	chunk = nullptr;
99

100
	dc.Lock();
101 102
	if (dc.client_is_waiting)
		dc.client_cond.signal();
103
	dc.Unlock();
104
}