forked from videolan/vlc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtizen_audio.c
375 lines (317 loc) · 9.85 KB
/
tizen_audio.c
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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
/*****************************************************************************
* tizen_audio.c: Tizen audio output module
*****************************************************************************
* Copyright © 2015 VLC authors, VideoLAN and VideoLabs
*
* Authors: Thomas Guillem <thomas@gllm.fr>
*
* 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 Lesser 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., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
*****************************************************************************/
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif
#include <assert.h>
#include <dlfcn.h>
#include <stdbool.h>
#include <stdatomic.h>
#include <vlc_common.h>
#include <vlc_plugin.h>
#include <vlc_aout.h>
#include "audio_io.h"
#include "sound_manager.h"
static int Open( vlc_object_t * );
static void Close( vlc_object_t * );
struct aout_sys_t {
/* sw gain */
float soft_gain;
bool soft_mute;
audio_out_h out;
bool b_prepared;
bool b_error;
atomic_bool interrupted_completed;
unsigned int i_rate;
audio_sample_type_e i_sample_type;
audio_channel_e i_channel;
int (*pf_audio_out_drain)( audio_out_h output );
int (*pf_audio_out_flush)( audio_out_h output );
};
/* Soft volume helper */
#include "audio_output/volume.h"
vlc_module_begin ()
set_shortname( "Tizen audio" )
set_description( "Tizen audio output" )
set_capability( "audio output", 180 )
set_category( CAT_AUDIO )
set_subcategory( SUBCAT_AUDIO_AOUT )
add_sw_gain()
add_shortcut( "tizen" )
set_callbacks( Open, Close )
vlc_module_end ()
static const char *
AudioIO_Err2Str( audio_io_error_e e )
{
switch( e )
{
case AUDIO_IO_ERROR_NONE:
return "AUDIO_IO_ERROR_NONE";
case AUDIO_IO_ERROR_OUT_OF_MEMORY:
return "AUDIO_IO_ERROR_OUT_OF_MEMORY";
case AUDIO_IO_ERROR_INVALID_PARAMETER:
return "AUDIO_IO_ERROR_INVALID_PARAMETER";
case AUDIO_IO_ERROR_INVALID_OPERATION:
return "AUDIO_IO_ERROR_INVALID_OPERATION";
case AUDIO_IO_ERROR_PERMISSION_DENIED:
return "AUDIO_IO_ERROR_PERMISSION_DENIED";
case AUDIO_IO_ERROR_NOT_SUPPORTED:
return "AUDIO_IO_ERROR_NOT_SUPPORTED";
case AUDIO_IO_ERROR_DEVICE_NOT_OPENED:
return "AUDIO_IO_ERROR_DEVICE_NOT_OPENED";
case AUDIO_IO_ERROR_DEVICE_NOT_CLOSED:
return "AUDIO_IO_ERROR_DEVICE_NOT_CLOSED";
case AUDIO_IO_ERROR_INVALID_BUFFER:
return "AUDIO_IO_ERROR_INVALID_BUFFER";
case AUDIO_IO_ERROR_SOUND_POLICY:
return "AUDIO_IO_ERROR_SOUND_POLICY";
default:
return "UNKNOWN_ERROR";
}
}
static int
AudioIO_VlcRet( audio_output_t *p_aout, const char *p_func, int i_ret )
{
if( i_ret != AUDIO_IO_ERROR_NONE )
{
aout_sys_t *p_sys = p_aout->sys;
msg_Err( p_aout, "%s failed: 0x%X, %s", p_func,
i_ret, AudioIO_Err2Str( i_ret ) );
/* Error could be recoverable if audio_out was interrupted. */
p_sys->b_error = true;
return VLC_EGENERIC;
}
else
return VLC_SUCCESS;
}
#define VLCRET( func ) AudioIO_VlcRet( p_aout, #func, func )
static int
AudioIO_Prepare( audio_output_t *p_aout )
{
aout_sys_t *p_sys = p_aout->sys;
/* if no more interrupted, cancel error and try again */
if( atomic_exchange( &p_sys->interrupted_completed, false ) )
p_sys->b_error = false;
if( p_sys->b_error )
return VLC_EGENERIC;
if( !p_sys->b_prepared )
{
if( VLCRET( audio_out_prepare( p_sys->out ) ) )
return VLC_EGENERIC;
p_sys->b_prepared = true;
}
return VLC_SUCCESS;
}
static int
AudioIO_Unprepare( audio_output_t *p_aout )
{
aout_sys_t *p_sys = p_aout->sys;
if( p_sys->b_prepared )
{
p_sys->b_prepared = false;
/* Unlocked to avoid deadlock with AudioIO_StreamCb */
if( VLCRET( audio_out_unprepare( p_sys->out ) ) )
return VLC_EGENERIC;
}
return VLC_SUCCESS;
}
static void
AudioIO_InterruptedCb(audio_io_interrupted_code_e code, void *p_user_data)
{
audio_output_t *p_aout = p_user_data;
aout_sys_t *p_sys = p_aout->sys;
if( code == AUDIO_IO_INTERRUPTED_COMPLETED
|| code == AUDIO_IO_INTERRUPTED_BY_EARJACK_UNPLUG )
{
msg_Warn( p_aout, "audio_out interrupted completed by %d", code);
atomic_store( &p_sys->interrupted_completed, true );
}
else
{
msg_Warn( p_aout, "audio_out interrupted by %d", code);
atomic_store( &p_sys->interrupted_completed, false );
}
}
static int
AudioIO_Start( audio_output_t *p_aout )
{
aout_sys_t *p_sys = p_aout->sys;
/* Out create */
if( VLCRET( audio_out_create( p_sys->i_rate, p_sys->i_channel,
p_sys->i_sample_type, SOUND_TYPE_MEDIA,
&p_sys->out ) ) )
return VLC_EGENERIC;
return VLCRET( audio_out_set_interrupted_cb( p_sys->out,
AudioIO_InterruptedCb,
p_aout ) );
}
static int
Start( audio_output_t *p_aout, audio_sample_format_t *restrict p_fmt )
{
aout_sys_t *p_sys = p_aout->sys;
if( aout_FormatNbChannels( p_fmt ) == 0 )
return VLC_EGENERIC;
aout_FormatPrint( p_aout, "Tizen audio is looking for:", p_fmt );
/* Sample rate: tizen accept rate between 8000 and 48000 Hz */
p_sys->i_rate = p_fmt->i_rate = VLC_CLIP( p_fmt->i_rate, 8000, 48000 );
/* Channel */
switch( p_fmt->i_physical_channels )
{
case AOUT_CHAN_LEFT:
p_sys->i_channel = AUDIO_CHANNEL_MONO;
break;
default:
case AOUT_CHANS_STEREO:
p_fmt->i_physical_channels = AOUT_CHANS_STEREO;
p_sys->i_channel = AUDIO_CHANNEL_STEREO;
break;
}
/* Sample type */
switch( p_fmt->i_format )
{
case VLC_CODEC_U8:
p_sys->i_sample_type = AUDIO_SAMPLE_TYPE_U8;
break;
default:
case VLC_CODEC_S16N:
p_fmt->i_format = VLC_CODEC_S16N;
p_sys->i_sample_type = AUDIO_SAMPLE_TYPE_S16_LE;
break;
}
if( AudioIO_Start( p_aout ) != VLC_SUCCESS )
return VLC_EGENERIC;
p_fmt->channel_type = AUDIO_CHANNEL_TYPE_BITMAP;
aout_FormatPrepare( p_fmt );
aout_SoftVolumeStart( p_aout );
aout_FormatPrint( p_aout, "Tizen audio will output:", p_fmt );
return VLC_SUCCESS;
}
static void
Stop( audio_output_t *p_aout )
{
aout_sys_t *p_sys = p_aout->sys;
if( p_sys->out)
{
AudioIO_Unprepare( p_aout );
audio_out_unset_interrupted_cb( p_sys->out );
audio_out_destroy( p_sys->out );
p_sys->out = NULL;
}
p_sys->b_error = false;
atomic_store( &p_sys->interrupted_completed, false );
p_sys->i_rate = 0;
p_sys->i_channel = 0;
p_sys->i_sample_type = 0;
}
static void
Play( audio_output_t *p_aout, block_t *p_block )
{
aout_sys_t *p_sys = p_aout->sys;
if( !p_sys->out || AudioIO_Prepare( p_aout ) )
{
block_Release( p_block );
return;
}
while( p_block )
{
int i_ret = audio_out_write( p_sys->out, p_block->p_buffer, p_block->i_buffer );
if( i_ret < 0 )
{
AudioIO_VlcRet( p_aout, "audio_out_write", i_ret );
block_Release( p_block );
p_block = NULL;
}
else
{
p_block->i_buffer -= i_ret;
p_block->p_buffer += i_ret;
if( !p_block->i_buffer )
{
block_Release( p_block );
p_block = NULL;
}
}
}
}
static void
Pause( audio_output_t *p_aout, bool b_pause, mtime_t i_date )
{
aout_sys_t *p_sys = p_aout->sys;
(void) i_date;
if( !p_sys->out )
return;
if( b_pause )
AudioIO_Unprepare( p_aout );
}
static void
Flush( audio_output_t *p_aout, bool b_wait )
{
aout_sys_t *p_sys = p_aout->sys;
if( !p_sys->out )
return;
if( p_sys->pf_audio_out_drain || p_sys->pf_audio_out_flush )
{
if( b_wait )
VLCRET( p_sys->pf_audio_out_drain( p_sys->out ) );
else
VLCRET( p_sys->pf_audio_out_flush( p_sys->out ) );
}
else
{
(void) b_wait;
if( AudioIO_Unprepare( p_aout ) )
return;
audio_out_unset_interrupted_cb( p_sys->out );
audio_out_destroy( p_sys->out );
p_sys->out = NULL;
AudioIO_Start( p_aout );
}
}
static int
Open( vlc_object_t *obj )
{
audio_output_t *p_aout = (audio_output_t *) obj;
aout_sys_t *p_sys;
p_sys = calloc( 1, sizeof (aout_sys_t) );
if( unlikely( p_sys == NULL ) )
return VLC_ENOMEM;
p_aout->sys = p_sys;
p_aout->start = Start;
p_aout->stop = Stop;
p_aout->play = Play;
p_aout->pause = Pause;
p_aout->flush = Flush;
/* p_aout->time_get = TimeGet; FIXME */
/* Available only on 2.4 */
p_sys->pf_audio_out_drain = dlsym( RTLD_DEFAULT, "audio_out_drain" );
p_sys->pf_audio_out_flush = dlsym( RTLD_DEFAULT, "audio_out_flush" );
aout_SoftVolumeInit( p_aout );
atomic_init( &p_sys->interrupted_completed, false );
return VLC_SUCCESS;
}
static void
Close( vlc_object_t *obj )
{
audio_output_t *p_aout = (audio_output_t *) obj;
aout_sys_t *p_sys = p_aout->sys;
free( p_sys );
}