1
/* $Id: mem_capture.c 3664 2011-07-19 03:42:28Z nanang $ */
3
* Copyright (C) 2008-2011 Teluu Inc. (http://www.teluu.com)
4
* Copyright (C) 2003-2008 Benny Prijono <benny@prijono.org>
6
* This program is free software; you can redistribute it and/or modify
7
* it under the terms of the GNU General Public License as published by
8
* the Free Software Foundation; either version 2 of the License, or
9
* (at your option) any later version.
11
* This program is distributed in the hope that it will be useful,
12
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
* GNU General Public License for more details.
16
* You should have received a copy of the GNU General Public License
17
* along with this program; if not, write to the Free Software
18
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20
#include <pjmedia/mem_port.h>
21
#include <pj/assert.h>
26
#define THIS_FILE "mem_capture.c"
28
#define SIGNATURE PJMEDIA_SIG_PORT_MEM_CAPTURE
29
#define BYTES_PER_SAMPLE 2
43
pj_status_t (*cb)(pjmedia_port *port,
48
static pj_status_t rec_put_frame(pjmedia_port *this_port,
49
pjmedia_frame *frame);
50
static pj_status_t rec_get_frame(pjmedia_port *this_port,
51
pjmedia_frame *frame);
52
static pj_status_t rec_on_destroy(pjmedia_port *this_port);
55
PJ_DEF(pj_status_t) pjmedia_mem_capture_create( pj_pool_t *pool,
59
unsigned channel_count,
60
unsigned samples_per_frame,
61
unsigned bits_per_sample,
63
pjmedia_port **p_port)
66
const pj_str_t name = { "memrec", 6 };
69
PJ_ASSERT_RETURN(pool && buffer && size && clock_rate && channel_count &&
70
samples_per_frame && bits_per_sample && p_port,
73
/* Can only support 16bit PCM */
74
PJ_ASSERT_RETURN(bits_per_sample == 16, PJ_EINVAL);
77
rec = PJ_POOL_ZALLOC_T(pool, struct mem_rec);
78
PJ_ASSERT_RETURN(rec != NULL, PJ_ENOMEM);
81
pjmedia_port_info_init(&rec->base.info, &name, SIGNATURE,
82
clock_rate, channel_count, bits_per_sample,
86
rec->base.put_frame = &rec_put_frame;
87
rec->base.get_frame = &rec_get_frame;
88
rec->base.on_destroy = &rec_on_destroy;
92
rec->buffer = rec->write_pos = (char*)buffer;
96
rec->options = options;
105
* Register a callback to be called when the file reading has reached the
108
PJ_DEF(pj_status_t) pjmedia_mem_capture_set_eof_cb( pjmedia_port *port,
110
pj_status_t (*cb)(pjmedia_port *port,
115
PJ_ASSERT_RETURN(port->info.signature == SIGNATURE,
118
rec = (struct mem_rec*) port;
119
rec->user_data = user_data;
126
/* Get current buffer size */
127
PJ_DEF(pj_size_t) pjmedia_mem_capture_get_size(pjmedia_port *port)
131
PJ_ASSERT_RETURN(port->info.signature == SIGNATURE,
134
rec = (struct mem_rec*) port;
136
return rec->buf_size;
138
return rec->write_pos - rec->buffer;
142
static pj_status_t rec_put_frame( pjmedia_port *this_port,
143
pjmedia_frame *frame)
147
pj_size_t size_written;
149
PJ_ASSERT_RETURN(this_port->info.signature == SIGNATURE,
152
rec = (struct mem_rec*) this_port;
159
endpos = rec->buffer + rec->buf_size;
161
while (size_written < frame->size) {
164
max = frame->size - size_written;
165
if ((endpos - rec->write_pos) < (int)max)
166
max = endpos - rec->write_pos;
168
pj_memcpy(rec->write_pos, ((char*)frame->buf)+size_written, max);
170
rec->write_pos += max;
172
pj_assert(rec->write_pos <= endpos);
174
if (rec->write_pos == endpos) {
177
rec->write_pos = rec->buffer;
179
/* Call callback, if any */
184
status = (*rec->cb)(this_port, rec->user_data);
185
if (status != PJ_SUCCESS) {
186
/* Must not access recorder from here on. It may be
187
* destroyed by application.
200
static pj_status_t rec_get_frame( pjmedia_port *this_port,
201
pjmedia_frame *frame)
203
PJ_ASSERT_RETURN(this_port->info.signature == SIGNATURE,
206
PJ_UNUSED_ARG(this_port);
209
frame->type = PJMEDIA_FRAME_TYPE_NONE;
215
static pj_status_t rec_on_destroy(pjmedia_port *this_port)
217
/* Call callback if data was captured
218
* and we're not in the callback already.
222
PJ_ASSERT_RETURN(this_port->info.signature == SIGNATURE,
225
rec = (struct mem_rec*) this_port;
227
if(rec->cb && PJ_FALSE == rec->eof) {
229
(*rec->cb)(this_port, rec->user_data);