blob: a6f9a99ea31ee66b3017c75239674d8a181a49cd [file] [log] [blame]
Tristan Matthews0a329cc2013-07-17 13:20:14 -04001/* $Id$ */
2/*
3 * Copyright (C) 2008-2011 Teluu Inc. (http://www.teluu.com)
4 * Copyright (C) 2003-2008 Benny Prijono <benny@prijono.org>
5 *
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.
10 *
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.
15 *
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
19 */
20#include <pjmedia/mem_port.h>
21#include <pj/assert.h>
22#include <pj/errno.h>
23#include <pj/pool.h>
24
25
26#define THIS_FILE "mem_capture.c"
27
28#define SIGNATURE PJMEDIA_SIG_PORT_MEM_CAPTURE
29#define BYTES_PER_SAMPLE 2
30
31struct mem_rec
32{
33 pjmedia_port base;
34
35 unsigned options;
36
37 char *buffer;
38 pj_size_t buf_size;
39 char *write_pos;
40
41 pj_bool_t eof;
42 void *user_data;
43 pj_status_t (*cb)(pjmedia_port *port,
44 void *user_data);
45};
46
47
48static pj_status_t rec_put_frame(pjmedia_port *this_port,
49 pjmedia_frame *frame);
50static pj_status_t rec_get_frame(pjmedia_port *this_port,
51 pjmedia_frame *frame);
52static pj_status_t rec_on_destroy(pjmedia_port *this_port);
53
54
55PJ_DEF(pj_status_t) pjmedia_mem_capture_create( pj_pool_t *pool,
56 void *buffer,
57 pj_size_t size,
58 unsigned clock_rate,
59 unsigned channel_count,
60 unsigned samples_per_frame,
61 unsigned bits_per_sample,
62 unsigned options,
63 pjmedia_port **p_port)
64{
65 struct mem_rec *rec;
66 const pj_str_t name = { "memrec", 6 };
67
68 /* Sanity check */
69 PJ_ASSERT_RETURN(pool && buffer && size && clock_rate && channel_count &&
70 samples_per_frame && bits_per_sample && p_port,
71 PJ_EINVAL);
72
73 /* Can only support 16bit PCM */
74 PJ_ASSERT_RETURN(bits_per_sample == 16, PJ_EINVAL);
75
76
77 rec = PJ_POOL_ZALLOC_T(pool, struct mem_rec);
78 PJ_ASSERT_RETURN(rec != NULL, PJ_ENOMEM);
79
80 /* Create the rec */
81 pjmedia_port_info_init(&rec->base.info, &name, SIGNATURE,
82 clock_rate, channel_count, bits_per_sample,
83 samples_per_frame);
84
85
86 rec->base.put_frame = &rec_put_frame;
87 rec->base.get_frame = &rec_get_frame;
88 rec->base.on_destroy = &rec_on_destroy;
89
90
91 /* Save the buffer */
92 rec->buffer = rec->write_pos = (char*)buffer;
93 rec->buf_size = size;
94
95 /* Options */
96 rec->options = options;
97
98 *p_port = &rec->base;
99
100 return PJ_SUCCESS;
101}
102
103
104/*
105 * Register a callback to be called when the file reading has reached the
106 * end of buffer.
107 */
108PJ_DEF(pj_status_t) pjmedia_mem_capture_set_eof_cb( pjmedia_port *port,
109 void *user_data,
110 pj_status_t (*cb)(pjmedia_port *port,
111 void *usr_data))
112{
113 struct mem_rec *rec;
114
115 PJ_ASSERT_RETURN(port->info.signature == SIGNATURE,
116 PJ_EINVALIDOP);
117
118 rec = (struct mem_rec*) port;
119 rec->user_data = user_data;
120 rec->cb = cb;
121
122 return PJ_SUCCESS;
123}
124
125
126/* Get current buffer size */
127PJ_DEF(pj_size_t) pjmedia_mem_capture_get_size(pjmedia_port *port)
128{
129 struct mem_rec *rec;
130
131 PJ_ASSERT_RETURN(port->info.signature == SIGNATURE,
132 0);
133
134 rec = (struct mem_rec*) port;
135 if (rec->eof){
136 return rec->buf_size;
137 }
138 return rec->write_pos - rec->buffer;
139}
140
141
142static pj_status_t rec_put_frame( pjmedia_port *this_port,
143 pjmedia_frame *frame)
144{
145 struct mem_rec *rec;
146 char *endpos;
147 pj_size_t size_written;
148
149 PJ_ASSERT_RETURN(this_port->info.signature == SIGNATURE,
150 PJ_EINVALIDOP);
151
152 rec = (struct mem_rec*) this_port;
153
154 if (rec->eof) {
155 return PJ_EEOF;
156 }
157
158 size_written = 0;
159 endpos = rec->buffer + rec->buf_size;
160
161 while (size_written < frame->size) {
162 pj_size_t max;
163
164 max = frame->size - size_written;
165 if ((endpos - rec->write_pos) < (int)max)
166 max = endpos - rec->write_pos;
167
168 pj_memcpy(rec->write_pos, ((char*)frame->buf)+size_written, max);
169 size_written += max;
170 rec->write_pos += max;
171
172 pj_assert(rec->write_pos <= endpos);
173
174 if (rec->write_pos == endpos) {
175
176 /* Rewind */
177 rec->write_pos = rec->buffer;
178
179 /* Call callback, if any */
180 if (rec->cb) {
181 pj_status_t status;
182
183 rec->eof = PJ_TRUE;
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.
188 */
189 return status;
190 }
191 rec->eof = PJ_FALSE;
192 }
193 }
194 }
195
196 return PJ_SUCCESS;
197}
198
199
200static pj_status_t rec_get_frame( pjmedia_port *this_port,
201 pjmedia_frame *frame)
202{
203 PJ_ASSERT_RETURN(this_port->info.signature == SIGNATURE,
204 PJ_EINVALIDOP);
205
206 PJ_UNUSED_ARG(this_port);
207
208 frame->size = 0;
209 frame->type = PJMEDIA_FRAME_TYPE_NONE;
210
211 return PJ_SUCCESS;
212}
213
214
215static pj_status_t rec_on_destroy(pjmedia_port *this_port)
216{
217 /* Call callback if data was captured
218 * and we're not in the callback already.
219 */
220 struct mem_rec *rec;
221
222 PJ_ASSERT_RETURN(this_port->info.signature == SIGNATURE,
223 PJ_EINVALIDOP);
224
225 rec = (struct mem_rec*) this_port;
226
227 if(rec->cb && PJ_FALSE == rec->eof) {
228 rec->eof = PJ_TRUE;
229 (*rec->cb)(this_port, rec->user_data);
230 }
231
232 return PJ_SUCCESS;
233}
234
235