null_encoder.c 3.12 KB
Newer Older
1
/*
Max Kellermann's avatar
Max Kellermann committed
2
 * Copyright (C) 2003-2011 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 22
#include "encoder_api.h"
#include "encoder_plugin.h"
23
#include "pcm_buffer.h"
24 25 26 27

#include <assert.h>
#include <string.h>

28
struct null_encoder {
29 30
	struct encoder encoder;

31
	struct pcm_buffer buffer;
32 33 34
	size_t buffer_length;
};

35
extern const struct encoder_plugin null_encoder_plugin;
36 37

static inline GQuark
38
null_encoder_quark(void)
39
{
40
	return g_quark_from_static_string("null_encoder");
41 42 43
}

static struct encoder *
44
null_encoder_init(G_GNUC_UNUSED const struct config_param *param,
45 46
		  G_GNUC_UNUSED GError **error)
{
47
	struct null_encoder *encoder;
48

49 50
	encoder = g_new(struct null_encoder, 1);
	encoder_struct_init(&encoder->encoder, &null_encoder_plugin);
51 52 53 54 55

	return &encoder->encoder;
}

static void
56
null_encoder_finish(struct encoder *_encoder)
57
{
58
	struct null_encoder *encoder = (struct null_encoder *)_encoder;
59 60 61 62

	g_free(encoder);
}

63 64 65 66 67 68 69 70 71
static void
null_encoder_close(struct encoder *_encoder)
{
	struct null_encoder *encoder = (struct null_encoder *)_encoder;

	pcm_buffer_deinit(&encoder->buffer);
}


72
static bool
73 74
null_encoder_open(struct encoder *_encoder,
		  G_GNUC_UNUSED struct audio_format *audio_format,
75 76
		  G_GNUC_UNUSED GError **error)
{
77
	struct null_encoder *encoder = (struct null_encoder *)_encoder;
78 79

	encoder->buffer_length = 0;
80
	pcm_buffer_init(&encoder->buffer);
81 82 83 84 85

	return true;
}

static bool
86
null_encoder_write(struct encoder *_encoder,
87 88 89
		   const void *data, size_t length,
		   G_GNUC_UNUSED GError **error)
{
90
	struct null_encoder *encoder = (struct null_encoder *)_encoder;
91
	char *buffer = pcm_buffer_get(&encoder->buffer, encoder->buffer_length + length);
92

93
	memcpy(buffer+encoder->buffer_length, data, length);
94 95 96 97 98 99

	encoder->buffer_length += length;
	return true;
}

static size_t
100
null_encoder_read(struct encoder *_encoder, void *dest, size_t length)
101
{
102
	struct null_encoder *encoder = (struct null_encoder *)_encoder;
103
	char *buffer = pcm_buffer_get(&encoder->buffer, encoder->buffer_length);
104 105 106 107

	if (length > encoder->buffer_length)
		length = encoder->buffer_length;

108
	memcpy(dest, buffer, length);
109 110

	encoder->buffer_length -= length;
111
	memmove(buffer, buffer + length, encoder->buffer_length);
112 113 114 115

	return length;
}

116 117 118 119 120
const struct encoder_plugin null_encoder_plugin = {
	.name = "null",
	.init = null_encoder_init,
	.finish = null_encoder_finish,
	.open = null_encoder_open,
121
	.close = null_encoder_close,
122 123
	.write = null_encoder_write,
	.read = null_encoder_read,
124
};