~ubuntu-branches/ubuntu/trusty/sflphone/trusty

« back to all changes in this revision

Viewing changes to daemon/libs/pjproject-2.1.0/pjmedia/src/pjmedia/wav_playlist.c

  • Committer: Package Import Robot
  • Author(s): Mark Purcell
  • Date: 2014-01-28 18:23:36 UTC
  • mfrom: (4.3.4 sid)
  • Revision ID: package-import@ubuntu.com-20140128182336-jrsv0k9u6cawc068
Tags: 1.3.0-1
* New upstream release 
  - Fixes "New Upstream Release" (Closes: #735846)
  - Fixes "Ringtone does not stop" (Closes: #727164)
  - Fixes "[sflphone-kde] crash on startup" (Closes: #718178)
  - Fixes "sflphone GUI crashes when call is hung up" (Closes: #736583)
* Build-Depends: ensure GnuTLS 2.6
  - libucommon-dev (>= 6.0.7-1.1), libccrtp-dev (>= 2.0.6-3)
  - Fixes "FTBFS Build-Depends libgnutls{26,28}-dev" (Closes: #722040)
* Fix "boost 1.49 is going away" unversioned Build-Depends: (Closes: #736746)
* Add Build-Depends: libsndfile-dev, nepomuk-core-dev

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* $Id: wav_playlist.c 3917 2011-12-20 10:01:35Z nanang $ */
 
2
/* 
 
3
 * Copyright (C) 2008-2011 Teluu Inc. (http://www.teluu.com)
 
4
 * Copyright (C) 2003-2008 Benny Prijono <benny@prijono.org>
 
5
 *
 
6
 * Original author:
 
7
 *  David Clark <vdc1048 @ tx.rr.com>
 
8
 *
 
9
 * This program is free software; you can redistribute it and/or modify
 
10
 * it under the terms of the GNU General Public License as published by
 
11
 * the Free Software Foundation; either version 2 of the License, or
 
12
 * (at your option) any later version.
 
13
 *
 
14
 * This program is distributed in the hope that it will be useful,
 
15
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
16
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
17
 * GNU General Public License for more details.
 
18
 *
 
19
 * You should have received a copy of the GNU General Public License
 
20
 * along with this program; if not, write to the Free Software
 
21
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA 
 
22
 */
 
23
#include <pjmedia/wav_playlist.h>
 
24
#include <pjmedia/errno.h>
 
25
#include <pjmedia/wave.h>
 
26
#include <pj/assert.h>
 
27
#include <pj/file_access.h>
 
28
#include <pj/file_io.h>
 
29
#include <pj/log.h>
 
30
#include <pj/pool.h>
 
31
#include <pj/string.h>
 
32
 
 
33
#define THIS_FILE           "wav_playlist.c"
 
34
 
 
35
#define SIGNATURE           PJMEDIA_SIG_PORT_WAV_PLAYLIST
 
36
#define BYTES_PER_SAMPLE    2
 
37
 
 
38
 
 
39
#if 1
 
40
#   define TRACE_(x)    PJ_LOG(4,x)
 
41
#else
 
42
#   define TRACE_(x)
 
43
#endif
 
44
 
 
45
#if defined(PJ_IS_BIG_ENDIAN) && PJ_IS_BIG_ENDIAN!=0
 
46
    static void samples_to_host(pj_int16_t *samples, unsigned count)
 
47
    {
 
48
        unsigned i;
 
49
        for (i=0; i<count; ++i) {
 
50
            samples[i] = pj_swap16(samples[i]);
 
51
        }
 
52
    }
 
53
#else
 
54
#   define samples_to_host(samples,count)
 
55
#endif
 
56
 
 
57
 
 
58
struct playlist_port
 
59
{
 
60
    pjmedia_port     base;
 
61
    unsigned         options;
 
62
    pj_bool_t        eof;
 
63
    pj_size_t        bufsize;
 
64
    char            *buf;
 
65
    char            *readpos;
 
66
 
 
67
    pj_off_t        *fsize_list;
 
68
    unsigned        *start_data_list;
 
69
    pj_off_t        *fpos_list;
 
70
    pj_oshandle_t   *fd_list;       /* list of file descriptors */
 
71
    int              current_file;  /* index of current file.   */
 
72
    int              max_file;      /* how many files.          */
 
73
 
 
74
    pj_status_t    (*cb)(pjmedia_port*, void*);
 
75
};
 
76
 
 
77
 
 
78
static pj_status_t file_list_get_frame(pjmedia_port *this_port,
 
79
                                       pjmedia_frame *frame);
 
80
static pj_status_t file_list_on_destroy(pjmedia_port *this_port);
 
81
 
 
82
 
 
83
static struct playlist_port *create_file_list_port(pj_pool_t *pool,
 
84
                                                   const pj_str_t *name)
 
85
{
 
86
    struct playlist_port *port;
 
87
 
 
88
    port = PJ_POOL_ZALLOC_T(pool, struct playlist_port);
 
89
    if (!port)
 
90
        return NULL;
 
91
 
 
92
    /* Put in default values.
 
93
     * These will be overriden once the file is read.
 
94
     */
 
95
    pjmedia_port_info_init(&port->base.info, name, SIGNATURE,
 
96
                           8000, 1, 16, 80);
 
97
 
 
98
    port->base.get_frame = &file_list_get_frame;
 
99
    port->base.on_destroy = &file_list_on_destroy;
 
100
 
 
101
    return port;
 
102
}
 
103
 
 
104
 
 
105
/*
 
106
 * Fill buffer for file_list operations.
 
107
 */
 
108
static pj_status_t file_fill_buffer(struct playlist_port *fport)
 
109
{
 
110
    pj_ssize_t size_left = fport->bufsize;
 
111
    unsigned size_to_read;
 
112
    pj_ssize_t size;
 
113
    pj_status_t status;
 
114
    int current_file = fport->current_file;
 
115
 
 
116
    /* Can't read file if EOF and loop flag is disabled */
 
117
    if (fport->eof)
 
118
        return PJ_EEOF;
 
119
 
 
120
    while (size_left > 0)
 
121
    {
 
122
        /* Calculate how many bytes to read in this run. */
 
123
        size = size_to_read = size_left;
 
124
        status = pj_file_read(fport->fd_list[current_file],
 
125
                              &fport->buf[fport->bufsize-size_left],
 
126
                              &size);
 
127
        if (status != PJ_SUCCESS)
 
128
            return status;
 
129
        
 
130
        if (size < 0)
 
131
        {
 
132
            /* Should return more appropriate error code here.. */
 
133
            return PJ_ECANCELLED;
 
134
        }
 
135
        
 
136
        size_left -= size;
 
137
        fport->fpos_list[current_file] += size;
 
138
        
 
139
        /* If size is less than size_to_read, it indicates that we've
 
140
         * encountered EOF. Rewind the file.
 
141
         */
 
142
        if (size < (pj_ssize_t)size_to_read)
 
143
        {
 
144
            /* Rewind the file for the next iteration */
 
145
            fport->fpos_list[current_file] = 
 
146
                fport->start_data_list[current_file];
 
147
            pj_file_setpos(fport->fd_list[current_file], 
 
148
                           fport->fpos_list[current_file], PJ_SEEK_SET);
 
149
 
 
150
            /* Move to next file */
 
151
            current_file++;
 
152
            fport->current_file = current_file;
 
153
 
 
154
            if (fport->current_file == fport->max_file)
 
155
            {
 
156
                /* Clear the remaining part of the buffer first, to prevent
 
157
                 * old samples from being played. If the playback restarts,
 
158
                 * this will be overwritten by new reading.
 
159
                 */
 
160
                if (size_left > 0) {
 
161
                    pj_bzero(&fport->buf[fport->bufsize-size_left], 
 
162
                             size_left);
 
163
                }
 
164
 
 
165
                /* All files have been played. Call callback, if any. */
 
166
                if (fport->cb)
 
167
                {
 
168
                    PJ_LOG(5,(THIS_FILE,
 
169
                              "File port %.*s EOF, calling callback",
 
170
                              (int)fport->base.info.name.slen,
 
171
                              fport->base.info.name.ptr));
 
172
                    
 
173
                    fport->eof = PJ_TRUE;
 
174
 
 
175
                    status = (*fport->cb)(&fport->base,
 
176
                                          fport->base.port_data.pdata);
 
177
 
 
178
                    if (status != PJ_SUCCESS)
 
179
                    {
 
180
                        /* This will crash if file port is destroyed in the
 
181
                         * callback, that's why we set the eof flag before
 
182
                         * calling the callback:
 
183
                         fport->eof = PJ_TRUE;
 
184
                         */
 
185
                        return status;
 
186
                    }
 
187
 
 
188
                    fport->eof = PJ_FALSE;
 
189
                }
 
190
 
 
191
 
 
192
                if (fport->options & PJMEDIA_FILE_NO_LOOP)
 
193
                {
 
194
                    PJ_LOG(5,(THIS_FILE, "File port %.*s EOF, stopping..",
 
195
                              (int)fport->base.info.name.slen,
 
196
                              fport->base.info.name.ptr));
 
197
                    fport->eof = PJ_TRUE;
 
198
                    return PJ_EEOF;
 
199
                }
 
200
                else
 
201
                {
 
202
                    PJ_LOG(5,(THIS_FILE, "File port %.*s EOF, rewinding..",
 
203
                              (int)fport->base.info.name.slen,
 
204
                              fport->base.info.name.ptr));
 
205
                    
 
206
                    /* start with first file again. */
 
207
                    fport->current_file = current_file = 0;
 
208
                    fport->fpos_list[0] = fport->start_data_list[0];
 
209
                    pj_file_setpos(fport->fd_list[0], fport->fpos_list[0],
 
210
                                   PJ_SEEK_SET);
 
211
                }               
 
212
                
 
213
            } /* if current_file == max_file */
 
214
 
 
215
        } /* size < size_to_read */
 
216
 
 
217
    } /* while () */
 
218
    
 
219
    /* Convert samples to host rep */
 
220
    samples_to_host((pj_int16_t*)fport->buf, fport->bufsize/BYTES_PER_SAMPLE);
 
221
    
 
222
    return PJ_SUCCESS;
 
223
}
 
224
 
 
225
 
 
226
/*
 
227
 * Create wave list player.
 
228
 */
 
229
PJ_DEF(pj_status_t) pjmedia_wav_playlist_create(pj_pool_t *pool,
 
230
                                                const pj_str_t *port_label,
 
231
                                                const pj_str_t file_list[],
 
232
                                                int file_count,
 
233
                                                unsigned ptime,
 
234
                                                unsigned options,
 
235
                                                pj_ssize_t buff_size,
 
236
                                                pjmedia_port **p_port)
 
237
{
 
238
    struct playlist_port *fport;
 
239
    pjmedia_audio_format_detail *afd;
 
240
    pj_off_t pos;
 
241
    pj_status_t status;
 
242
    int index;
 
243
    pj_bool_t has_wave_info = PJ_FALSE;
 
244
    pj_str_t tmp_port_label;
 
245
    char filename[PJ_MAXPATH];  /* filename for open operations.    */
 
246
 
 
247
 
 
248
    /* Check arguments. */
 
249
    PJ_ASSERT_RETURN(pool && file_list && file_count && p_port, PJ_EINVAL);
 
250
 
 
251
    /* Normalize port_label */
 
252
    if (port_label == NULL || port_label->slen == 0) {
 
253
        tmp_port_label = pj_str("WAV playlist");
 
254
        port_label = &tmp_port_label;
 
255
    }
 
256
 
 
257
    /* Be sure all files exist  */
 
258
    for (index=0; index<file_count; index++) {
 
259
 
 
260
        PJ_ASSERT_RETURN(file_list[index].slen < PJ_MAXPATH, PJ_ENAMETOOLONG);
 
261
 
 
262
        pj_memcpy(filename, file_list[index].ptr, file_list[index].slen);
 
263
        filename[file_list[index].slen] = '\0';
 
264
 
 
265
        /* Check the file really exists. */
 
266
        if (!pj_file_exists(filename)) {
 
267
            PJ_LOG(4,(THIS_FILE,
 
268
                      "WAV playlist error: file '%s' not found",
 
269
                      filename));
 
270
            return PJ_ENOTFOUND;
 
271
        }
 
272
    }
 
273
 
 
274
    /* Normalize ptime */
 
275
    if (ptime == 0)
 
276
        ptime = 20;
 
277
 
 
278
    /* Create fport instance. */
 
279
    fport = create_file_list_port(pool, port_label);
 
280
    if (!fport) {
 
281
        return PJ_ENOMEM;
 
282
    }
 
283
 
 
284
    afd = pjmedia_format_get_audio_format_detail(&fport->base.info.fmt, 1);
 
285
 
 
286
    /* start with the first file. */
 
287
    fport->current_file = 0;
 
288
    fport->max_file = file_count;
 
289
 
 
290
    /* Create file descriptor list */
 
291
    fport->fd_list = (pj_oshandle_t*)
 
292
                     pj_pool_zalloc(pool, sizeof(pj_oshandle_t)*file_count);
 
293
    if (!fport->fd_list) {
 
294
        return PJ_ENOMEM;
 
295
    }
 
296
 
 
297
    /* Create file size list */
 
298
    fport->fsize_list = (pj_off_t*)
 
299
                        pj_pool_alloc(pool, sizeof(pj_off_t)*file_count);
 
300
    if (!fport->fsize_list) {
 
301
        return PJ_ENOMEM;
 
302
    }
 
303
 
 
304
    /* Create start of WAVE data list */
 
305
    fport->start_data_list = (unsigned*)
 
306
                             pj_pool_alloc(pool, sizeof(unsigned)*file_count);
 
307
    if (!fport->start_data_list) {
 
308
        return PJ_ENOMEM;
 
309
    }
 
310
 
 
311
    /* Create file position list */
 
312
    fport->fpos_list = (pj_off_t*)
 
313
                       pj_pool_alloc(pool, sizeof(pj_off_t)*file_count);
 
314
    if (!fport->fpos_list) {
 
315
        return PJ_ENOMEM;
 
316
    }
 
317
 
 
318
    /* Create file buffer once for this operation.
 
319
     */
 
320
    if (buff_size < 1) buff_size = PJMEDIA_FILE_PORT_BUFSIZE;
 
321
    fport->bufsize = buff_size;
 
322
 
 
323
 
 
324
    /* Create buffer. */
 
325
    fport->buf = (char*) pj_pool_alloc(pool, fport->bufsize);
 
326
    if (!fport->buf) {
 
327
        return PJ_ENOMEM;
 
328
    }
 
329
 
 
330
    /* Initialize port */
 
331
    fport->options = options;
 
332
    fport->readpos = fport->buf;
 
333
 
 
334
 
 
335
    /* ok run this for all files to be sure all are good for playback. */
 
336
    for (index=file_count-1; index>=0; index--) {
 
337
 
 
338
        pjmedia_wave_hdr wavehdr;
 
339
        pj_ssize_t size_to_read, size_read;
 
340
 
 
341
        /* we end with the last one so we are good to go if still in function*/
 
342
        pj_memcpy(filename, file_list[index].ptr, file_list[index].slen);
 
343
        filename[file_list[index].slen] = '\0';
 
344
 
 
345
        /* Get the file size. */
 
346
        fport->current_file = index;
 
347
        fport->fsize_list[index] = pj_file_size(filename);
 
348
        
 
349
        /* Size must be more than WAVE header size */
 
350
        if (fport->fsize_list[index] <= sizeof(pjmedia_wave_hdr)) {
 
351
            status = PJMEDIA_ENOTVALIDWAVE;
 
352
            goto on_error;
 
353
        }
 
354
        
 
355
        /* Open file. */
 
356
        status = pj_file_open( pool, filename, PJ_O_RDONLY, 
 
357
                               &fport->fd_list[index]);
 
358
        if (status != PJ_SUCCESS)
 
359
            goto on_error;
 
360
        
 
361
        /* Read the file header plus fmt header only. */
 
362
        size_read = size_to_read = sizeof(wavehdr) - 8;
 
363
        status = pj_file_read( fport->fd_list[index], &wavehdr, &size_read);
 
364
        if (status != PJ_SUCCESS) {
 
365
            goto on_error;
 
366
        }
 
367
 
 
368
        if (size_read != size_to_read) {
 
369
            status = PJMEDIA_ENOTVALIDWAVE;
 
370
            goto on_error;
 
371
        }
 
372
        
 
373
        /* Normalize WAVE header fields values from little-endian to host
 
374
         * byte order.
 
375
         */
 
376
        pjmedia_wave_hdr_file_to_host(&wavehdr);
 
377
        
 
378
        /* Validate WAVE file. */
 
379
        if (wavehdr.riff_hdr.riff != PJMEDIA_RIFF_TAG ||
 
380
            wavehdr.riff_hdr.wave != PJMEDIA_WAVE_TAG ||
 
381
            wavehdr.fmt_hdr.fmt != PJMEDIA_FMT_TAG)
 
382
        {
 
383
            TRACE_((THIS_FILE,
 
384
                "actual value|expected riff=%x|%x, wave=%x|%x fmt=%x|%x",
 
385
                wavehdr.riff_hdr.riff, PJMEDIA_RIFF_TAG,
 
386
                wavehdr.riff_hdr.wave, PJMEDIA_WAVE_TAG,
 
387
                wavehdr.fmt_hdr.fmt, PJMEDIA_FMT_TAG));
 
388
            status = PJMEDIA_ENOTVALIDWAVE;
 
389
            goto on_error;
 
390
        }
 
391
        
 
392
        /* Must be PCM with 16bits per sample */
 
393
        if (wavehdr.fmt_hdr.fmt_tag != 1 ||
 
394
            wavehdr.fmt_hdr.bits_per_sample != 16)
 
395
        {
 
396
            status = PJMEDIA_EWAVEUNSUPP;
 
397
            goto on_error;
 
398
        }
 
399
        
 
400
        /* Block align must be 2*nchannels */
 
401
        if (wavehdr.fmt_hdr.block_align != 
 
402
                wavehdr.fmt_hdr.nchan * BYTES_PER_SAMPLE)
 
403
        {
 
404
            status = PJMEDIA_EWAVEUNSUPP;
 
405
            goto on_error;
 
406
        }
 
407
        
 
408
        /* If length of fmt_header is greater than 16, skip the remaining
 
409
         * fmt header data.
 
410
         */
 
411
        if (wavehdr.fmt_hdr.len > 16) {
 
412
            size_to_read = wavehdr.fmt_hdr.len - 16;
 
413
            status = pj_file_setpos(fport->fd_list[index], size_to_read, 
 
414
                                    PJ_SEEK_CUR);
 
415
            if (status != PJ_SUCCESS) {
 
416
                goto on_error;
 
417
            }
 
418
        }
 
419
        
 
420
        /* Repeat reading the WAVE file until we have 'data' chunk */
 
421
        for (;;) {
 
422
            pjmedia_wave_subchunk subchunk;
 
423
            size_read = 8;
 
424
            status = pj_file_read(fport->fd_list[index], &subchunk, 
 
425
                                  &size_read);
 
426
            if (status != PJ_SUCCESS || size_read != 8) {
 
427
                status = PJMEDIA_EWAVETOOSHORT;
 
428
                goto on_error;
 
429
            }
 
430
            
 
431
            /* Normalize endianness */
 
432
            PJMEDIA_WAVE_NORMALIZE_SUBCHUNK(&subchunk);
 
433
            
 
434
            /* Break if this is "data" chunk */
 
435
            if (subchunk.id == PJMEDIA_DATA_TAG) {
 
436
                wavehdr.data_hdr.data = PJMEDIA_DATA_TAG;
 
437
                wavehdr.data_hdr.len = subchunk.len;
 
438
                break;
 
439
            }
 
440
            
 
441
            /* Otherwise skip the chunk contents */
 
442
            size_to_read = subchunk.len;
 
443
            status = pj_file_setpos(fport->fd_list[index], size_to_read, 
 
444
                                    PJ_SEEK_CUR);
 
445
            if (status != PJ_SUCCESS) {
 
446
                goto on_error;
 
447
            }
 
448
        }
 
449
        
 
450
        /* Current file position now points to start of data */
 
451
        status = pj_file_getpos(fport->fd_list[index], &pos);
 
452
        fport->start_data_list[index] = (unsigned)pos;
 
453
        
 
454
        /* Validate length. */
 
455
        if (wavehdr.data_hdr.len != fport->fsize_list[index] - 
 
456
                                       fport->start_data_list[index]) 
 
457
        {
 
458
            status = PJMEDIA_EWAVEUNSUPP;
 
459
            goto on_error;
 
460
        }
 
461
        if (wavehdr.data_hdr.len < 400) {
 
462
            status = PJMEDIA_EWAVETOOSHORT;
 
463
            goto on_error;
 
464
        }
 
465
        
 
466
        /* It seems like we have a valid WAVE file. */
 
467
        
 
468
        /* Update port info if we don't have one, otherwise check
 
469
         * that the WAV file has the same attributes as previous files. 
 
470
         */
 
471
        if (!has_wave_info) {
 
472
            afd->channel_count = wavehdr.fmt_hdr.nchan;
 
473
            afd->clock_rate = wavehdr.fmt_hdr.sample_rate;
 
474
            afd->bits_per_sample = wavehdr.fmt_hdr.bits_per_sample;
 
475
            afd->frame_time_usec = ptime * 1000;
 
476
            afd->avg_bps = afd->max_bps = afd->clock_rate *
 
477
                                          afd->channel_count *
 
478
                                          afd->bits_per_sample;
 
479
 
 
480
            has_wave_info = PJ_TRUE;
 
481
 
 
482
        } else {
 
483
 
 
484
            /* Check that this file has the same characteristics as the other
 
485
             * files.
 
486
             */
 
487
            if (wavehdr.fmt_hdr.nchan != afd->channel_count ||
 
488
                wavehdr.fmt_hdr.sample_rate != afd->clock_rate ||
 
489
                wavehdr.fmt_hdr.bits_per_sample != afd->bits_per_sample)
 
490
            {
 
491
                /* This file has different characteristics than the other 
 
492
                 * files. 
 
493
                 */
 
494
                PJ_LOG(4,(THIS_FILE,
 
495
                          "WAV playlist error: file '%s' has differrent number"
 
496
                          " of channels, sample rate, or bits per sample",
 
497
                          filename));
 
498
                status = PJMEDIA_EWAVEUNSUPP;
 
499
                goto on_error;
 
500
            }
 
501
 
 
502
        }
 
503
        
 
504
        
 
505
        /* Set initial position of the file. */
 
506
        fport->fpos_list[index] = fport->start_data_list[index];
 
507
    }
 
508
 
 
509
    /* Fill up the buffer. */
 
510
    status = file_fill_buffer(fport);
 
511
    if (status != PJ_SUCCESS) {
 
512
        goto on_error;
 
513
    }
 
514
    
 
515
    /* Done. */
 
516
    
 
517
    *p_port = &fport->base;
 
518
    
 
519
    PJ_LOG(4,(THIS_FILE,
 
520
             "WAV playlist '%.*s' created: samp.rate=%d, ch=%d, bufsize=%uKB",
 
521
             (int)port_label->slen,
 
522
             port_label->ptr,
 
523
             afd->clock_rate,
 
524
             afd->channel_count,
 
525
             fport->bufsize / 1000));
 
526
    
 
527
    return PJ_SUCCESS;
 
528
 
 
529
on_error:
 
530
    for (index=0; index<file_count; ++index) {
 
531
        if (fport->fd_list[index] != 0)
 
532
            pj_file_close(fport->fd_list[index]);
 
533
    }
 
534
 
 
535
    return status;
 
536
}
 
537
 
 
538
 
 
539
/*
 
540
 * Register a callback to be called when the file reading has reached the
 
541
 * end of the last file.
 
542
 */
 
543
PJ_DEF(pj_status_t) pjmedia_wav_playlist_set_eof_cb(pjmedia_port *port,
 
544
                                void *user_data,
 
545
                                pj_status_t (*cb)(pjmedia_port *port,
 
546
                                                  void *usr_data))
 
547
{
 
548
    struct playlist_port *fport;
 
549
 
 
550
    /* Sanity check */
 
551
    PJ_ASSERT_RETURN(port, PJ_EINVAL);
 
552
 
 
553
    /* Check that this is really a playlist port */
 
554
    PJ_ASSERT_RETURN(port->info.signature == SIGNATURE, PJ_EINVALIDOP);
 
555
 
 
556
    fport = (struct playlist_port*) port;
 
557
 
 
558
    fport->base.port_data.pdata = user_data;
 
559
    fport->cb = cb;
 
560
 
 
561
    return PJ_SUCCESS;
 
562
}
 
563
 
 
564
 
 
565
/*
 
566
 * Get frame from file for file_list operation
 
567
 */
 
568
static pj_status_t file_list_get_frame(pjmedia_port *this_port,
 
569
                                       pjmedia_frame *frame)
 
570
{
 
571
    struct playlist_port *fport = (struct playlist_port*)this_port;
 
572
    unsigned frame_size;
 
573
    pj_status_t status;
 
574
 
 
575
    pj_assert(fport->base.info.signature == SIGNATURE);
 
576
 
 
577
    //frame_size = fport->base.info.bytes_per_frame;
 
578
    //pj_assert(frame->size == frame_size);
 
579
    frame_size = frame->size;
 
580
 
 
581
    /* Copy frame from buffer. */
 
582
    frame->type = PJMEDIA_FRAME_TYPE_AUDIO;
 
583
    frame->size = frame_size;
 
584
    frame->timestamp.u64 = 0;
 
585
 
 
586
    if (fport->readpos + frame_size <= fport->buf + fport->bufsize) {
 
587
 
 
588
        /* Read contiguous buffer. */
 
589
        pj_memcpy(frame->buf, fport->readpos, frame_size);
 
590
 
 
591
        /* Fill up the buffer if all has been read. */
 
592
        fport->readpos += frame_size;
 
593
        if (fport->readpos == fport->buf + fport->bufsize) {
 
594
            fport->readpos = fport->buf;
 
595
 
 
596
            status = file_fill_buffer(fport);
 
597
            if (status != PJ_SUCCESS) {
 
598
                frame->type = PJMEDIA_FRAME_TYPE_NONE;
 
599
                frame->size = 0;
 
600
                return status;
 
601
            }
 
602
        }
 
603
    } else {
 
604
        unsigned endread;
 
605
 
 
606
        /* Split read.
 
607
         * First stage: read until end of buffer.
 
608
         */
 
609
        endread = (fport->buf+fport->bufsize) - fport->readpos;
 
610
        pj_memcpy(frame->buf, fport->readpos, endread);
 
611
 
 
612
        /* Second stage: fill up buffer, and read from the start of buffer. */
 
613
        status = file_fill_buffer(fport);
 
614
        if (status != PJ_SUCCESS) {
 
615
            pj_bzero(((char*)frame->buf)+endread, frame_size-endread);
 
616
            return status;
 
617
        }
 
618
 
 
619
        pj_memcpy(((char*)frame->buf)+endread, fport->buf, frame_size-endread);
 
620
        fport->readpos = fport->buf + (frame_size - endread);
 
621
    }
 
622
 
 
623
    return PJ_SUCCESS;
 
624
}
 
625
 
 
626
 
 
627
/*
 
628
 * Destroy port.
 
629
 */
 
630
static pj_status_t file_list_on_destroy(pjmedia_port *this_port)
 
631
{
 
632
    struct playlist_port *fport = (struct playlist_port*) this_port;
 
633
    int index;
 
634
 
 
635
    pj_assert(this_port->info.signature == SIGNATURE);
 
636
 
 
637
    for (index=0; index<fport->max_file; index++)
 
638
        pj_file_close(fport->fd_list[index]);
 
639
 
 
640
    return PJ_SUCCESS;
 
641
}
 
642