File: ifusionsoundstream.c

package info (click to toggle)
fusionsound 0.9.25-2
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 3,924 kB
  • ctags: 1,275
  • sloc: ansic: 13,230; sh: 8,306; perl: 668; makefile: 285
file content (488 lines) | stat: -rw-r--r-- 13,765 bytes parent folder | download
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
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
/*
   (c) Copyright 2000-2002  convergence integrated media GmbH.
   (c) Copyright 2002       convergence GmbH.

   All rights reserved.

   Written by Denis Oliver Kropp <dok@directfb.org>,
              Andreas Hundt <andi@fischlustig.de> and
              Sven Neumann <sven@convergence.de>.

   This library 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 of the License, or (at your option) any later version.

   This library 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 library; if not, write to the
   Free Software Foundation, Inc., 59 Temple Place - Suite 330,
   Boston, MA 02111-1307, USA.
*/

#include <pthread.h>

#include <fusionsound.h>

#include <direct/debug.h>
#include <direct/interface.h>
#include <direct/mem.h>
#include <direct/memcpy.h>
#include <direct/util.h>

#include <core/core_sound.h>
#include <core/playback.h>
#include <core/sound_buffer.h>

#include "ifusionsoundplayback.h"
#include "ifusionsoundstream.h"

/******/

static DFBResult      IFusionSoundStream_FillBuffer( IFusionSoundStream_data *data,
                                                     const void              *sample_data,
                                                     int                      length,
                                                     int                     *ret_bytes );

static ReactionResult IFusionSoundStream_React     ( const void              *msg_data,
                                                     void                    *ctx );

/******/

static void
IFusionSoundStream_Destruct( IFusionSoundStream *thiz )
{
     IFusionSoundStream_data *data = (IFusionSoundStream_data*)thiz->priv;

     D_ASSERT( data->buffer != NULL );
     D_ASSERT( data->playback != NULL );

     fs_playback_detach( data->playback, &data->reaction );

     fs_playback_stop( data->playback, true );

     fs_playback_unref( data->playback );

     fs_buffer_unref( data->buffer );

     pthread_cond_destroy( &data->wait );
     pthread_mutex_destroy( &data->lock );

     DIRECT_DEALLOCATE_INTERFACE( thiz );
}

static DFBResult
IFusionSoundStream_AddRef( IFusionSoundStream *thiz )
{
     DIRECT_INTERFACE_GET_DATA(IFusionSoundStream)

     data->ref++;

     return DFB_OK;
}

static DFBResult
IFusionSoundStream_Release( IFusionSoundStream *thiz )
{
     DIRECT_INTERFACE_GET_DATA(IFusionSoundStream)

     if (--data->ref == 0)
          IFusionSoundStream_Destruct( thiz );

     return DFB_OK;
}


static DFBResult
IFusionSoundStream_GetDescription( IFusionSoundStream  *thiz,
                                   FSStreamDescription *desc )
{
     DIRECT_INTERFACE_GET_DATA(IFusionSoundStream)

     if (!desc)
          return DFB_INVARG;

     desc->flags = FSSDF_BUFFERSIZE | FSSDF_CHANNELS |
                   FSSDF_SAMPLEFORMAT | FSSDF_SAMPLERATE | FSSDF_PREBUFFER;
     desc->buffersize   = data->size;
     desc->channels     = data->channels;
     desc->sampleformat = data->format;
     desc->samplerate   = data->rate;
     desc->prebuffer    = data->prebuffer;

     return DFB_OK;
}

static DFBResult
IFusionSoundStream_Write( IFusionSoundStream *thiz,
                          const void         *sample_data,
                          int                 length )
{
     DIRECT_INTERFACE_GET_DATA(IFusionSoundStream)

     if (!sample_data || length < 1)
          return DFB_INVARG;

     pthread_mutex_lock( &data->lock );

     while (length) {
          DFBResult ret;
          int       num;
          int       bytes;

          D_DEBUG( "%s: length %d, read pos %d, write pos %d, filled %d/%d (%splaying)\n",
                   __FUNCTION__, length, data->pos_read, data->pos_write, data->filled,
                   data->size, data->playing ? "" : "not " );

          D_ASSERT( data->filled <= data->size );

          /* Wait for at least one free sample. */
          while (data->filled == data->size)
               pthread_cond_wait( &data->wait, &data->lock );

          /* Calculate number of free samples in the buffer. */
          num = data->size - data->filled;

          /* Do not write more than requested. */
          if (num > length)
               num = length;

          /* Fill free space with automatic wrap around. */
          ret = IFusionSoundStream_FillBuffer( data, sample_data, num, &bytes);
          if (ret) {
               pthread_mutex_unlock( &data->lock );
               return ret;
          }

          /* (Re)start if playback had stopped (buffer underrun). */
          if (!data->playing && data->prebuffer >= 0 && data->filled >= data->prebuffer) {
               D_DEBUG( "%s: starting playback now!\n", __FUNCTION__ );

               fs_playback_start( data->playback, true );
          }

          /* Update input parameters. */
          length      -= num;
          sample_data += bytes;
     }

     pthread_mutex_unlock( &data->lock );

     return DFB_OK;
}

static DFBResult
IFusionSoundStream_Wait( IFusionSoundStream *thiz,
                         int                 length )
{
     DIRECT_INTERFACE_GET_DATA(IFusionSoundStream)

     if (length < 0 || length > data->size)
          return DFB_INVARG;

     pthread_mutex_lock( &data->lock );

     while (true) {
          if (length) {
               int num;

               /* Calculate number of free samples in the buffer. */
               num = data->size - data->filled;

               if (num >= length)
                    break;
          }
          else if (!data->playing)
               break;

          pthread_cond_wait( &data->wait, &data->lock );
     }

     pthread_mutex_unlock( &data->lock );

     return DFB_OK;
}

static DFBResult
IFusionSoundStream_GetStatus( IFusionSoundStream *thiz,
                              int                *filled,
                              int                *total,
                              int                *read_position,
                              int                *write_position,
                              DFBBoolean         *playing )
{
     DIRECT_INTERFACE_GET_DATA(IFusionSoundStream)

     pthread_mutex_lock( &data->lock );

     if (filled)
          *filled = data->filled;

     if (total)
          *total = data->size;

     if (read_position)
          *read_position = data->pos_read;

     if (write_position)
          *write_position = data->pos_write;

     if (playing)
          *playing = data->playing;

     pthread_mutex_unlock( &data->lock );

     return DFB_OK;
}

static DFBResult
IFusionSoundStream_Flush( IFusionSoundStream *thiz )
{
     DIRECT_INTERFACE_GET_DATA(IFusionSoundStream)

     /* Stop the playback. */
     fs_playback_stop( data->playback, true );

     pthread_mutex_lock( &data->lock );

     while (data->playing) {
          pthread_cond_wait( &data->wait, &data->lock );
     }

     /* Reset the buffer. */
     data->pos_write = data->pos_read;
     data->filled    = 0;

     pthread_mutex_unlock( &data->lock );

     return DFB_OK;
}


static DFBResult
IFusionSoundStream_GetPresentationDelay( IFusionSoundStream *thiz,
                                         int                *delay )
{
     DIRECT_INTERFACE_GET_DATA(IFusionSoundStream)

     if (!delay)
          return DFB_INVARG;

     pthread_mutex_lock( &data->lock );

     *delay = fs_core_output_delay( data->core )  +  data->filled * 1000 / data->rate;

     pthread_mutex_unlock( &data->lock );

     return DFB_OK;
}

static DFBResult
IFusionSoundStream_GetPlayback( IFusionSoundStream    *thiz,
                                IFusionSoundPlayback **ret_interface )
{
     DFBResult             ret;
     IFusionSoundPlayback *interface;

     DIRECT_INTERFACE_GET_DATA(IFusionSoundStream)

     if (!ret_interface)
          return DFB_INVARG;

     DIRECT_ALLOCATE_INTERFACE( interface, IFusionSoundPlayback );

     ret = IFusionSoundPlayback_Construct( interface, data->playback, -1 );
     if (ret)
          *ret_interface = NULL;
     else
          *ret_interface = interface;

     return ret;
}

/******/

DFBResult
IFusionSoundStream_Construct( IFusionSoundStream *thiz,
                              CoreSound          *core,
                              CoreSoundBuffer    *buffer,
                              int                 size,
                              int                 channels,
                              FSSampleFormat      format,
                              int                 rate,
                              int                 prebuffer )
{
     DFBResult     ret;
     CorePlayback *playback;

     DIRECT_ALLOCATE_INTERFACE_DATA(thiz, IFusionSoundStream)

     /* Increase reference counter of the buffer. */
     if (fs_buffer_ref( buffer )) {
          ret = DFB_FUSION;
          goto error_ref;
     }

     /* Create a playback object for the buffer. */
     ret = fs_playback_create( core, buffer, true, &playback );
     if (ret)
          goto error_create;

     /* Attach our listener to the playback object. */
     if (fs_playback_attach( playback, IFusionSoundStream_React, data, &data->reaction )) {
          ret = DFB_FUSION;
          goto error_attach;
     }

     /* Disable the playback. */
     fs_playback_stop( playback, true );

     /* Initialize private data. */
     data->ref       = 1;
     data->core      = core;
     data->buffer    = buffer;
     data->playback  = playback;
     data->size      = size;
     data->channels  = channels;
     data->format    = format;
     data->rate      = rate;
     data->prebuffer = prebuffer;

     /* Initialize lock and condition. */
     direct_util_recursive_pthread_mutex_init( &data->lock );
     pthread_cond_init( &data->wait, NULL );

     /* Initialize method table. */
     thiz->AddRef               = IFusionSoundStream_AddRef;
     thiz->Release              = IFusionSoundStream_Release;

     thiz->GetDescription       = IFusionSoundStream_GetDescription;

     thiz->Write                = IFusionSoundStream_Write;
     thiz->Wait                 = IFusionSoundStream_Wait;
     thiz->GetStatus            = IFusionSoundStream_GetStatus;
     thiz->Flush                = IFusionSoundStream_Flush;

     thiz->GetPresentationDelay = IFusionSoundStream_GetPresentationDelay;

     thiz->GetPlayback          = IFusionSoundStream_GetPlayback;

     return DFB_OK;

error_attach:
     fs_playback_unref( playback );

error_create:
     fs_buffer_unref( buffer );

error_ref:
     DIRECT_DEALLOCATE_INTERFACE( thiz );

     return ret;
}

/******/

static DFBResult
IFusionSoundStream_FillBuffer( IFusionSoundStream_data *data,
                               const void              *sample_data,
                               int                      length,
                               int                     *ret_bytes )
{
     DFBResult        ret;
     void            *lock_data;
     int              lock_bytes;
     int              offset = 0;

     D_DEBUG( "%s: length %d\n", __FUNCTION__, length );

     D_ASSERT( length <= data->size - data->filled );

     while (length) {
          int num = MIN( length, data->size - data->pos_write );

          /* Write data. */
          ret = fs_buffer_lock( data->buffer, data->pos_write, num, &lock_data, &lock_bytes );
          if (ret)
               return ret;

          direct_memcpy( lock_data, sample_data + offset, lock_bytes );

          fs_buffer_unlock( data->buffer );

          /* Update input parameters. */
          length -= num;
          offset += lock_bytes;

          /* Update write position. */
          data->pos_write += num;

          /* Handle wrap around. */
          if (data->pos_write == data->size)
               data->pos_write = 0;

          /* Set new stop position. */
          ret = fs_playback_set_stop( data->playback, data->pos_write );
          if (ret)
               return ret;

          /* (Re)enable playback if buffer has been empty. */
          fs_playback_enable( data->playback );

          /* Update the fill level. */
          data->filled += num;
     }

     if (ret_bytes)
          *ret_bytes = offset;

     return DFB_OK;
}

static ReactionResult
IFusionSoundStream_React( const void *msg_data,
                          void       *ctx )
{
     const CorePlaybackNotification *notification = msg_data;
     CorePlaybackNotificationFlags   flags        = notification->flags;
     IFusionSoundStream_data        *data         = ctx;

     if (flags & CPNF_START) {
          D_DEBUG( "%s: playback started at %d\n", __FUNCTION__, notification->pos );

          /* No locking here to avoid dead possible dead lock with IFusionSoundStream_Write(). */
          data->playing = true;

          return RS_OK;
     }

     pthread_mutex_lock( &data->lock );

     if (notification->flags & CPNF_ADVANCE) {
          D_DEBUG( "%s: playback advanced by %d from %d to %d\n",
                   __FUNCTION__, notification->num, data->pos_read, notification->pos );

          D_ASSERT( data->filled >= notification->num );

          data->filled -= notification->num;
     }

     data->pos_read = notification->pos;

     if (flags & CPNF_STOP) {
          D_DEBUG( "%s: playback stopped at %d!\n", __FUNCTION__, notification->pos );

          data->playing = false;
     }

     pthread_cond_broadcast( &data->wait );

     pthread_mutex_unlock( &data->lock );

     return RS_OK;
}