Debian Codegraph @

plugins/input/mplayer/input_mplayer.c

raw
1 /* Libvisual-plugins - Standard plugins for libvisual
2 *
3 * Copyright (C) 2004, 2005, 2006 Gustavo Sverzut Barbieri <[email protected]>
4 *
5 * Authors: Gustavo Sverzut Barbieri <[email protected]>
6 * Dennis Smit <[email protected]>
7 *
8 * $Id: input_mplayer.c,v 1.19 2006/01/22 13:25:27 synap Exp $
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU Lesser General Public License as
12 * published by the Free Software Foundation; either version 2.1
13 * of the License, or (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU Lesser General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 */
24
25 #include <config.h>
26
27 #include <limits.h>
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <unistd.h>
31 #include <fcntl.h>
32 #include <string.h>
33 #include <gettext.h>
34
35 #include <sys/mman.h>
36 #include <sys/types.h>
37 #include <sys/stat.h>
38 #include <errno.h>
39 #include <assert.h>
40
41 #include <libvisual/libvisual.h>
42
43 #ifndef SHARED_FILE
44 #define SHARED_FILE ".mplayer/mplayer-af_export" /**< default file name,
45 relative to $HOME */
46 #endif /* SHARED_FILE */
47
48
49 /* Data structures ***********************************************************/
50 typedef struct {
51 int nch; /**< number of channels */
52 int bs; /**< buffer size */
53 unsigned long long count; /**< sample counter */
54 } mplayer_data_t;
55
56
57 typedef struct {
58 int fd; /**< file descriptor to mmaped area */
59 char *sharedfile; /**< shared file name */
60 mplayer_data_t *mmap_area; /**< mmap()'ed area */
61 unsigned long long prev_count; /**< to detect duplicates */
62
63 int loaded; /**< plugin state */
64 } mplayer_priv_t;
65
66
67
68
69 /* Functions *****************************************************************/
70 int inp_mplayer_init( VisPluginData *plugin );
71 int inp_mplayer_cleanup( VisPluginData *plugin );
72 int inp_mplayer_upload( VisPluginData *plugin, VisAudio *audio );
73
74 VISUAL_PLUGIN_API_VERSION_VALIDATOR
75
76 /**
77 * set up plugin
78 *
79 * @param ref plugin reference
80 *
81 * @return plugin ready for use
82 */
83 const VisPluginInfo *get_plugin_info( int *count )
84 {
85 static VisInputPlugin input[] = {{
86 .upload = inp_mplayer_upload
87 }};
88
89 static VisPluginInfo info[] = {{
90 .type = VISUAL_PLUGIN_TYPE_INPUT,
91
92 .plugname = "mplayer",
93 .name = "mplayer",
94 .author = "Gustavo Sverzut Barbieri <[email protected]>",
95 .version = "$Revision: 1.19 $",
96 .about = N_("Use data exported from MPlayer"),
97 .help = N_("This plugin uses data exported from 'mplayer -af export'"),
98 .license = VISUAL_PLUGIN_LICENSE_LGPL,
99
100 .init = inp_mplayer_init,
101 .cleanup = inp_mplayer_cleanup,
102
103 .plugin = VISUAL_OBJECT (&input[0])
104 }};
105
106 *count = sizeof( info ) / sizeof( *info );
107
108 return info;
109 }
110
111 /**
112 * Initialize plugin
113 *
114 * @param plugin plugin to be initialized.
115 *
116 * @return 0 on success.
117 */
118 int inp_mplayer_init( VisPluginData *plugin )
119 {
120 mplayer_priv_t *priv = NULL;
121
122 #if ENABLE_NLS
123 bindtextdomain (GETTEXT_PACKAGE, LOCALEDIR);
124 #endif
125
126 priv = visual_mem_new0(mplayer_priv_t, 1);
127
128 priv->sharedfile = visual_mem_malloc0( sizeof( char ) *
129 ( strlen( SHARED_FILE ) +
130 strlen( getenv( "HOME" ) ) + 2 ) );
131
132 strcpy( priv->sharedfile, getenv( "HOME" ) );
133 strcat( priv->sharedfile, "/" );
134 strcat( priv->sharedfile, SHARED_FILE );
135
136 visual_object_set_private (VISUAL_OBJECT (plugin), priv);
137
138 visual_log_return_val_if_fail( plugin != NULL, -1 );
139
140 visual_log_return_val_if_fail( priv != NULL, -1 );
141 visual_log_return_val_if_fail( priv->sharedfile != NULL, -1 );
142
143 priv->fd = open( priv->sharedfile, O_RDONLY );
144
145 if ( priv->fd < 0 )
146 {
147 /*
148 * FIXME this will cause the application to abort,
149 * may be we must print a warning and clean all
150 * before to return the error value.
151 */
152 visual_log( VISUAL_LOG_ERROR,
153 _("Could not open file '%s': %s"),
154 priv->sharedfile, strerror( errno ) );
155 return -3;
156 }
157
158 priv->mmap_area = mmap( 0, sizeof( mplayer_data_t ),
159 PROT_READ, MAP_SHARED, priv->fd, 0 );
160 visual_log_return_val_if_fail( priv->mmap_area != (void *)-1, -1 );
161
162 if ( priv->mmap_area->nch == 0 )
163 {
164 visual_log( VISUAL_LOG_ERROR, _("No audio channel available") );
165 return -5;
166 }
167
168 if ( ( priv->mmap_area->nch != 2 ) ||
169 ( priv->mmap_area->bs != 2048 ) )
170 {
171 visual_log( VISUAL_LOG_ERROR,
172 _("Data in wrong format. It should be 2 channels" \
173 " with 512 16bit samples. There are %d channels %d 16bit " \
174 "samples in it (buffer is %d bytes)"),
175 priv->mmap_area->nch,
176 priv->mmap_area->bs / 2 / priv->mmap_area->nch,
177 priv->mmap_area->bs );
178 return -6;
179 }
180
181 const int buffer_size = priv->mmap_area->bs;
182 munmap( priv->mmap_area, sizeof( mplayer_data_t ) );
183
184 priv->mmap_area = mmap( 0, sizeof( mplayer_data_t ) + buffer_size,
185 PROT_READ, MAP_SHARED, priv->fd, 0 );
186 if ( priv->mmap_area == (void *)-1 )
187 {
188 visual_log( VISUAL_LOG_CRITICAL,
189 _("Could not mremap() area from file '%s' " \
190 " (%p from %zu to %zu bytes): %s"),
191 priv->sharedfile,
192 priv->mmap_area, sizeof( mplayer_data_t ),
193 sizeof( mplayer_data_t ) + priv->mmap_area->bs,
194 strerror( errno ) );
195 return -7;
196 }
197 priv->prev_count = ULLONG_MAX;
198
199 priv->loaded = 1;
200 return 0;
201 }
202
203 /**
204 * cleanup plugin (release resources)
205 *
206 * @param plugin plugin to be cleaned up.
207 *
208 * @return 0 on success.
209 */
210 int inp_mplayer_cleanup( VisPluginData *plugin )
211 {
212 int unclean = 0;
213 mplayer_priv_t *priv = NULL;
214
215 visual_log_return_val_if_fail( plugin != NULL, -1 );
216 priv = visual_object_get_private (VISUAL_OBJECT (plugin));
217 visual_log_return_val_if_fail( priv != NULL, -1 );
218
219 if ( priv->loaded == 1 )
220 {
221 void *mmap_area = (void*)priv->mmap_area;
222 int mmap_count = priv->mmap_area->bs + sizeof( mplayer_data_t );
223
224 if ( priv->fd > 0 )
225 {
226 if ( close( priv->fd ) != 0 )
227 {
228 visual_log( VISUAL_LOG_ERROR,
229 _("Could not close file descriptor %d: %s"),
230 priv->fd, strerror( errno ) );
231 unclean |= 1;
232 }
233 priv->fd = -1;
234 }
235 else
236 {
237 visual_log( VISUAL_LOG_ERROR, _("Wrong file descriptor %d"),
238 priv->fd );
239 unclean |= 2;
240 }
241
242 if ( munmap( mmap_area, mmap_count ) != 0 )
243 {
244 visual_log( VISUAL_LOG_ERROR,
245 _("Could not munmap() area %p+%d. %s"),
246 mmap_area, mmap_count,
247 strerror( errno ) );
248 unclean |= 4;
249 }
250 }
251
252 visual_mem_free( priv->sharedfile );
253 visual_mem_free( priv );
254
255 return - unclean;
256 }
257
258
259 /**
260 * upload data to libvisual
261 *
262 * @param plugin plugin that should upload data.
263 * @param where to upload data.
264 *
265 * @return 0 on success.
266 */
267 int inp_mplayer_upload( VisPluginData *plugin, VisAudio *audio )
268 {
269 mplayer_priv_t *priv = NULL;
270
271 visual_log_return_val_if_fail( audio != NULL, -1 );
272 visual_log_return_val_if_fail( plugin != NULL, -1 );
273 priv = visual_object_get_private (VISUAL_OBJECT (plugin));
274 visual_log_return_val_if_fail( priv != NULL, -1 );
275 visual_log_return_val_if_fail( priv->mmap_area != NULL, -1 );
276
277 // NOTE: This avoids accounting for the same samples multiple times
278 if (priv->mmap_area->count == priv->prev_count) {
279 return 0;
280 }
281 priv->prev_count = priv->mmap_area->count;
282
283 void * const data = ((void *)priv->mmap_area) + sizeof( mplayer_data_t );
284
285 VisBuffer buffer;
286 visual_buffer_init (&buffer, data, 2048, NULL);
287
288 visual_audio_samplepool_input (audio->samplepool, &buffer, VISUAL_AUDIO_SAMPLE_RATE_44100,
289 VISUAL_AUDIO_SAMPLE_FORMAT_S16, VISUAL_AUDIO_SAMPLE_CHANNEL_STEREO);
290
291 return 0;
292 }
293