File: input_esd.c

package info (click to toggle)
libvisual-plugins 1%3A0.4.0%2Bdfsg1-17
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 3,656 kB
  • sloc: ansic: 9,358; sh: 8,809; cpp: 871; makefile: 222; sed: 16
file content (171 lines) | stat: -rw-r--r-- 4,132 bytes parent folder | download | duplicates (7)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
/* Libvisual-plugins - Standard plugins for libvisual
 * 
 * Copyright (C) 2004, 2005, 2006 Dennis Smit <ds@nerds-incorporated.org>
 *
 * Authors: Dennis Smit <ds@nerds-incorporated.org>
 *
 * $Id: input_esd.c,v 1.17 2006/01/22 13:25:27 synap Exp $
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; either version 2.1
 * 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 Lesser General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 */

#include <config.h>

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <string.h>
#include <errno.h>

#include <gettext.h>

#include <esd.h>

#include <libvisual/libvisual.h>

#define PCM_BUF_SIZE	4096

typedef struct {
	int esdhandle;
} EsdPrivate;

int inp_esd_init (VisPluginData *plugin);
int inp_esd_cleanup (VisPluginData *plugin);
int inp_esd_upload (VisPluginData *plugin, VisAudio *audio);

VISUAL_PLUGIN_API_VERSION_VALIDATOR

const VisPluginInfo *get_plugin_info (int *count)
{
	static VisInputPlugin input[] = {{
		.upload = inp_esd_upload
	}};

	static VisPluginInfo info[] = {{
		.type = VISUAL_PLUGIN_TYPE_INPUT,

		.plugname = "esd",
		.name = "esd",
		.author = "Dennis Smit <ds@nerds-incorporated.org>",
		.version = "0.1",
		.about = N_("ESOUND capture plugin"),
		.help = N_("Use this plugin to capture PCM data from the ESD daemon"),
		.license = VISUAL_PLUGIN_LICENSE_LGPL,

		.init = inp_esd_init,
		.cleanup = inp_esd_cleanup,

		.plugin = VISUAL_OBJECT (&input[0])
	}};

	*count = sizeof (info) / sizeof (*info);

	return info;
}

int inp_esd_init (VisPluginData *plugin)
{
	EsdPrivate *priv;

	visual_log_return_val_if_fail (plugin != NULL, -1);

#if ENABLE_NLS
	bindtextdomain (GETTEXT_PACKAGE, LOCALEDIR);
#endif

	priv = visual_mem_new0 (EsdPrivate, 1);
	visual_log_return_val_if_fail (priv != NULL, -1);
	visual_object_set_private (VISUAL_OBJECT (plugin), priv);

	priv->esdhandle = esd_monitor_stream (ESD_BITS16 | ESD_STEREO | ESD_STREAM | ESD_MONITOR, 44100, NULL, "lv_esd_plugin");

	if (priv->esdhandle <= 0)
		return -1;

	return 0;
}

int inp_esd_cleanup (VisPluginData *plugin)
{
	EsdPrivate *priv = visual_object_get_private (VISUAL_OBJECT (plugin));

	visual_log_return_val_if_fail( plugin != NULL, -1 );
	priv = visual_object_get_private (VISUAL_OBJECT (plugin));
	visual_log_return_val_if_fail( priv != NULL, -1 );

	esd_close (priv->esdhandle);

	visual_mem_free (priv);

	return 0;
}

int inp_esd_upload (VisPluginData *plugin, VisAudio *audio)
{
	EsdPrivate *priv = NULL;
	short data[PCM_BUF_SIZE];
	struct timeval tv;
	int r;
	int i;

	visual_log_return_val_if_fail(audio != NULL, -1);
	visual_log_return_val_if_fail(plugin != NULL, -1);
	priv = visual_object_get_private (VISUAL_OBJECT (plugin));
	visual_log_return_val_if_fail(priv != NULL, -1);

	do {
		fd_set rdset;

		FD_ZERO (&rdset);
		FD_SET (priv->esdhandle, &rdset);

		tv.tv_sec = 0;
		tv.tv_usec = 10000;

		r = select (priv->esdhandle + 1, &rdset, NULL, NULL, &tv);

		/* Select time out */
		if (r == 0)
			return -1;

		if (r < 0) {
			visual_log (VISUAL_LOG_CRITICAL, _("ESD: Select error (%d, %s)"), errno, strerror (errno));

			return -1;
		}

		r = read (priv->esdhandle, data, PCM_BUF_SIZE);

		if (r > 0) {
			VisBuffer buffer;

			visual_buffer_init (&buffer, data, r, NULL);

			visual_audio_samplepool_input (audio->samplepool, &buffer, VISUAL_AUDIO_SAMPLE_RATE_44100,
					VISUAL_AUDIO_SAMPLE_FORMAT_S16, VISUAL_AUDIO_SAMPLE_CHANNEL_STEREO);
		}

		if (r < 0) {
			visual_log (VISUAL_LOG_CRITICAL, _("ESD: Error while reading data"));

			return -1;
		}

	} while (r > 0);

	return 0;
}