File: input_mplayer.c

package info (click to toggle)
libvisual-plugins 1%3A0.4.0%2Bdfsg1-16
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 3,656 kB
  • sloc: ansic: 9,358; sh: 8,809; cpp: 871; makefile: 222; sed: 16
file content (281 lines) | stat: -rw-r--r-- 7,587 bytes parent folder | download | duplicates (4)
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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
/* Libvisual-plugins - Standard plugins for libvisual
 * 
 * Copyright (C) 2004, 2005, 2006 Gustavo Sverzut Barbieri <gsbarbieri@yahoo.com.br>
 *
 * Authors: Gustavo Sverzut Barbieri <gsbarbieri@yahoo.com.br>
 *	    Dennis Smit <ds@nerds-incorporated.org>
 *
 * $Id: input_mplayer.c,v 1.19 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 <gettext.h>

#include <sys/mman.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <errno.h>
#include <assert.h>

#include <libvisual/libvisual.h>

#ifndef SHARED_FILE
#define SHARED_FILE ".mplayer/mplayer-af_export" /**< default file name,
						   relative to $HOME */
#endif /* SHARED_FILE */


/* Data structures ***********************************************************/
typedef struct {
	int nch;                  /**< number of channels */
	int bs;                   /**< buffer size */
	unsigned long long count; /**< sample counter */
} mplayer_data_t;


typedef struct {
	int fd;                    /**< file descriptor to mmaped area */
	char *sharedfile;          /**< shared file name */
	mplayer_data_t *mmap_area; /**< mmap()'ed area */

	int loaded;                /**< plugin state */
} mplayer_priv_t;




/* Functions *****************************************************************/
int inp_mplayer_init( VisPluginData *plugin );
int inp_mplayer_cleanup( VisPluginData *plugin );
int inp_mplayer_upload( VisPluginData *plugin, VisAudio *audio );

VISUAL_PLUGIN_API_VERSION_VALIDATOR

/**
 * set up plugin
 *
 * @param ref plugin reference
 *
 * @return plugin ready for use
 */
const VisPluginInfo *get_plugin_info( int *count )
{
	static VisInputPlugin input[] = {{
		.upload = inp_mplayer_upload
	}};

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

		.plugname = "mplayer",
		.name = "mplayer",
		.author = "Gustavo Sverzut Barbieri <gsbarbieri@users.sourceforge.net>",
		.version = "$Revision: 1.19 $",
		.about = N_("Use data exported from MPlayer"),
		.help = N_("This plugin uses data exported from 'mplayer -af export'"),
		.license = VISUAL_PLUGIN_LICENSE_LGPL,

		.init = inp_mplayer_init,
		.cleanup = inp_mplayer_cleanup,

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

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

	return info;
}

/**
 * Initialize plugin
 *
 * @param plugin plugin to be initialized.
 *
 * @return 0 on success.
 */
int inp_mplayer_init( VisPluginData *plugin )
{
	mplayer_priv_t *priv = NULL;

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

	priv = visual_mem_new0(mplayer_priv_t, 1);

	priv->sharedfile = visual_mem_malloc0( sizeof( char ) * 
			( strlen( SHARED_FILE ) + 
			  strlen( getenv( "HOME" ) ) + 2 ) );

	strcpy( priv->sharedfile, getenv( "HOME" ) );
	strcat( priv->sharedfile, "/" );
	strcat( priv->sharedfile, SHARED_FILE );

	visual_object_set_private (VISUAL_OBJECT (plugin), priv);

	visual_log_return_val_if_fail( plugin != NULL, -1 );

	visual_log_return_val_if_fail( priv != NULL, -1 );
	visual_log_return_val_if_fail( priv->sharedfile != NULL, -1 );

	priv->fd = open( priv->sharedfile, O_RDONLY );

	if ( priv->fd < 0 )
	{
		/* 
		 * FIXME this will cause the application to abort,
		 * may be we must print a warning and clean all
		 * before to return the error value.
		 */
		visual_log( VISUAL_LOG_ERROR,
				_("Could not open file '%s': %s"),
				priv->sharedfile, strerror( errno ) );
		return -3;
	}

	priv->mmap_area = mmap( 0, sizeof( mplayer_data_t ),
			PROT_READ, MAP_SHARED, priv->fd, 0 );
// [FIX] input_mplayer.c:158:33: warning: cast from pointer to integer of different size [-Wpointer-to-int-cast]
	visual_log_return_val_if_fail( (long)priv->mmap_area != -1, -1 );

	if ( priv->mmap_area->nch == 0 )
	{
		visual_log( VISUAL_LOG_ERROR, _("No audio channel available") );
		return -5;
	}

	if ( ( priv->mmap_area->nch != 2 ) ||
			( priv->mmap_area->bs  != 2048 ) )
	{
		visual_log( VISUAL_LOG_ERROR,
				_("Data in wrong format. It should be 2 channels" \
					" with 512 16bit samples. There are %d channels %d 16bit " \
					"samples in it (buffer is %d bytes)"),
				priv->mmap_area->nch,
				priv->mmap_area->bs / 2 / priv->mmap_area->nch,
				priv->mmap_area->bs );
		return -6;
	}

	priv->mmap_area = mremap( priv->mmap_area, sizeof( mplayer_data_t ),
			sizeof( mplayer_data_t ) + priv->mmap_area->bs,
			0 );
// [FIX] input_mplayer.c:183:7: warning: cast from pointer to integer of different size [-Wpointer-to-int-cast]
	if ( (long)priv->mmap_area == -1 )
	{
		visual_log( VISUAL_LOG_CRITICAL, 
// [FIX] input_mplayer.c:187:7: warning: format '%d' expects argument of type 'int', but argument 8 has type 'long unsigned int' [-Wformat=]
				_("Could not mremap() area from file '%s' " \
					" (%p from %ld to %ld bytes): %s"),
				priv->sharedfile, 
				priv->mmap_area, sizeof( mplayer_data_t ),
				sizeof( mplayer_data_t ) + priv->mmap_area->bs,
				strerror( errno ) );
		return -7;
	}  

	priv->loaded = 1;
	return 0;
}

/**
 * cleanup plugin (release resources)
 *
 * @param plugin plugin to be cleaned up.
 *
 * @return 0 on success.
 */
int inp_mplayer_cleanup( VisPluginData *plugin )
{
	int unclean = 0;
	mplayer_priv_t *priv = NULL;

	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 );

	if ( priv->loaded == 1 )
	{
		void *mmap_area  = (void*)priv->mmap_area;
		int   mmap_count = priv->mmap_area->bs + sizeof( mplayer_data_t );

		if ( priv->fd > 0 )
		{
			if ( close( priv->fd ) != 0 )
			{
				visual_log( VISUAL_LOG_ERROR,
						_("Could not close file descriptor %d: %s"),
						priv->fd, strerror( errno ) );
				unclean |= 1;
			}
			priv->fd = -1;
		}
		else
		{
			visual_log( VISUAL_LOG_ERROR, _("Wrong file descriptor %d"), 
					priv->fd );
			unclean |= 2;
		}

		if ( munmap( mmap_area, mmap_count ) != 0 )
		{
			visual_log( VISUAL_LOG_ERROR,
					_("Could not munmap() area %p+%d. %s"),
					mmap_area, mmap_count,
					strerror( errno ) );
			unclean |= 4;
		}      
	}

	visual_mem_free( priv->sharedfile );
	visual_mem_free( priv );

	return - unclean;
}


/**
 * upload data to libvisual
 *
 * @param plugin plugin that should upload data.
 * @param where to upload data.
 * 
 * @return 0 on success.
 */
int inp_mplayer_upload( VisPluginData *plugin, VisAudio *audio )
{
	mplayer_priv_t *priv = NULL;

	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 );
	visual_log_return_val_if_fail( priv->mmap_area != NULL, -1 );

	visual_mem_copy( audio->plugpcm, 
			((void *)priv->mmap_area) + sizeof( mplayer_data_t ),
			2048 );

	return 0;
}