NullEncoderPlugin.cxx 2.63 KB
Newer Older
1
/*
2
 * Copyright (C) 2003-2013 The Music Player Daemon Project
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
 * 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.
 *
 * 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.
 */

20
#include "config.h"
21
#include "NullEncoderPlugin.hxx"
22
#include "EncoderAPI.hxx"
23
#include "util/fifo_buffer.h"
24
extern "C" {
25
#include "util/growing_fifo.h"
26
}
27
#include "Compiler.h"
28

29 30 31
#include <assert.h>
#include <string.h>

32
struct NullEncoder final {
33
	Encoder encoder;
34

35
	struct fifo_buffer *buffer;
36

37
	NullEncoder():encoder(null_encoder_plugin) {}
38
};
39

40
static Encoder *
41
null_encoder_init(gcc_unused const config_param &param,
42
		  gcc_unused Error &error)
43
{
44
	NullEncoder *encoder = new NullEncoder();
45 46 47 48
	return &encoder->encoder;
}

static void
49
null_encoder_finish(Encoder *_encoder)
50
{
51
	NullEncoder *encoder = (NullEncoder *)_encoder;
52

53
	delete encoder;
54 55
}

56
static void
57
null_encoder_close(Encoder *_encoder)
58
{
59
	NullEncoder *encoder = (NullEncoder *)_encoder;
60

61
	fifo_buffer_free(encoder->buffer);
62 63 64
}


65
static bool
66
null_encoder_open(Encoder *_encoder,
67
		  gcc_unused AudioFormat &audio_format,
68
		  gcc_unused Error &error)
69
{
70
	NullEncoder *encoder = (NullEncoder *)_encoder;
71
	encoder->buffer = growing_fifo_new();
72 73 74 75
	return true;
}

static bool
76
null_encoder_write(Encoder *_encoder,
77
		   const void *data, size_t length,
78
		   gcc_unused Error &error)
79
{
80
	NullEncoder *encoder = (NullEncoder *)_encoder;
81

82 83
	growing_fifo_append(&encoder->buffer, data, length);
	return length;
84 85 86
}

static size_t
87
null_encoder_read(Encoder *_encoder, void *dest, size_t length)
88
{
89
	NullEncoder *encoder = (NullEncoder *)_encoder;
90

91 92
	size_t max_length;
	const void *src = fifo_buffer_read(encoder->buffer, &max_length);
93
	if (src == nullptr)
94
		return 0;
95

96 97
	if (length > max_length)
		length = max_length;
98

99 100
	memcpy(dest, src, length);
	fifo_buffer_consume(encoder->buffer, length);
101 102 103
	return length;
}

104
const EncoderPlugin null_encoder_plugin = {
105 106 107 108 109 110 111 112 113 114 115 116
	"null",
	null_encoder_init,
	null_encoder_finish,
	null_encoder_open,
	null_encoder_close,
	nullptr,
	nullptr,
	nullptr,
	nullptr,
	null_encoder_write,
	null_encoder_read,
	nullptr,
117
};