1
/* $Id: stereo_port.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
21
#include <pjmedia/stereo.h>
22
#include <pj/assert.h>
24
#include <pj/string.h>
27
#define SIGNATURE PJMEDIA_SIG_PORT_STEREO
33
pjmedia_port *dn_port;
41
static pj_status_t stereo_put_frame(pjmedia_port *this_port,
42
pjmedia_frame *frame);
43
static pj_status_t stereo_get_frame(pjmedia_port *this_port,
44
pjmedia_frame *frame);
45
static pj_status_t stereo_destroy(pjmedia_port *this_port);
49
PJ_DEF(pj_status_t) pjmedia_stereo_port_create( pj_pool_t *pool,
50
pjmedia_port *dn_port,
51
unsigned channel_count,
53
pjmedia_port **p_port )
55
const pj_str_t name = pj_str("stereo");
56
struct stereo_port *sport;
57
unsigned samples_per_frame;
59
/* Validate arguments. */
60
PJ_ASSERT_RETURN(pool && dn_port && channel_count && p_port, PJ_EINVAL);
62
/* Only supports 16bit samples per frame */
63
PJ_ASSERT_RETURN(PJMEDIA_PIA_BITS(&dn_port->info) == 16,
66
/* Validate channel counts */
67
PJ_ASSERT_RETURN(((PJMEDIA_PIA_CCNT(&dn_port->info)>1 &&
69
(PJMEDIA_PIA_CCNT(&dn_port->info)==1 &&
73
/* Create and initialize port. */
74
sport = PJ_POOL_ZALLOC_T(pool, struct stereo_port);
75
PJ_ASSERT_RETURN(sport != NULL, PJ_ENOMEM);
77
samples_per_frame = PJMEDIA_PIA_SPF(&dn_port->info) * channel_count /
78
PJMEDIA_PIA_CCNT(&dn_port->info);
80
pjmedia_port_info_init(&sport->base.info, &name, SIGNATURE,
81
PJMEDIA_PIA_SRATE(&dn_port->info),
83
PJMEDIA_PIA_BITS(&dn_port->info),
86
sport->dn_port = dn_port;
87
sport->options = options;
89
/* We always need buffer for put_frame */
90
sport->put_buf = (pj_int16_t*)
92
PJMEDIA_PIA_AVG_FSZ(&dn_port->info));
94
/* See if we need buffer for get_frame */
95
if (PJMEDIA_PIA_CCNT(&dn_port->info) > channel_count) {
96
sport->get_buf = (pj_int16_t*)
98
PJMEDIA_PIA_AVG_FSZ(&dn_port->info));
101
/* Media port interface */
102
sport->base.get_frame = &stereo_get_frame;
103
sport->base.put_frame = &stereo_put_frame;
104
sport->base.on_destroy = &stereo_destroy;
108
*p_port = &sport->base;
113
static pj_status_t stereo_put_frame(pjmedia_port *this_port,
114
pjmedia_frame *frame)
116
struct stereo_port *sport = (struct stereo_port*) this_port;
117
const pjmedia_audio_format_detail *s_afd, *dn_afd;
118
pjmedia_frame tmp_frame;
120
/* Return if we don't have downstream port. */
121
if (sport->dn_port == NULL) {
125
s_afd = pjmedia_format_get_audio_format_detail(&this_port->info.fmt, 1);
126
dn_afd = pjmedia_format_get_audio_format_detail(&sport->dn_port->info.fmt,
129
if (frame->type == PJMEDIA_FRAME_TYPE_AUDIO) {
130
tmp_frame.buf = sport->put_buf;
131
if (dn_afd->channel_count == 1) {
132
pjmedia_convert_channel_nto1((pj_int16_t*)tmp_frame.buf,
133
(const pj_int16_t*)frame->buf,
134
s_afd->channel_count,
135
PJMEDIA_AFD_SPF(s_afd),
136
(sport->options & PJMEDIA_STEREO_MIX),
139
pjmedia_convert_channel_1ton((pj_int16_t*)tmp_frame.buf,
140
(const pj_int16_t*)frame->buf,
141
dn_afd->channel_count,
142
PJMEDIA_AFD_SPF(s_afd),
145
tmp_frame.size = PJMEDIA_AFD_AVG_FSZ(dn_afd);
147
tmp_frame.buf = frame->buf;
148
tmp_frame.size = frame->size;
151
tmp_frame.type = frame->type;
152
tmp_frame.timestamp.u64 = frame->timestamp.u64;
154
return pjmedia_port_put_frame( sport->dn_port, &tmp_frame );
159
static pj_status_t stereo_get_frame(pjmedia_port *this_port,
160
pjmedia_frame *frame)
162
struct stereo_port *sport = (struct stereo_port*) this_port;
163
const pjmedia_audio_format_detail *s_afd, *dn_afd;
164
pjmedia_frame tmp_frame;
167
/* Return silence if we don't have downstream port */
168
if (sport->dn_port == NULL) {
169
pj_bzero(frame->buf, frame->size);
173
s_afd = pjmedia_format_get_audio_format_detail(&this_port->info.fmt, 1);
174
dn_afd = pjmedia_format_get_audio_format_detail(&sport->dn_port->info.fmt,
177
tmp_frame.buf = sport->get_buf? sport->get_buf : frame->buf;
178
tmp_frame.size = PJMEDIA_PIA_AVG_FSZ(&sport->dn_port->info);
179
tmp_frame.timestamp.u64 = frame->timestamp.u64;
180
tmp_frame.type = PJMEDIA_FRAME_TYPE_AUDIO;
182
status = pjmedia_port_get_frame( sport->dn_port, &tmp_frame);
183
if (status != PJ_SUCCESS)
186
if (tmp_frame.type != PJMEDIA_FRAME_TYPE_AUDIO) {
187
frame->type = tmp_frame.type;
188
frame->timestamp = tmp_frame.timestamp;
189
frame->size = tmp_frame.size;
190
if (tmp_frame.size && tmp_frame.buf == sport->get_buf)
191
pj_memcpy(frame->buf, tmp_frame.buf, tmp_frame.size);
195
if (s_afd->channel_count == 1) {
196
pjmedia_convert_channel_nto1((pj_int16_t*)frame->buf,
197
(const pj_int16_t*)tmp_frame.buf,
198
dn_afd->channel_count,
199
PJMEDIA_AFD_SPF(s_afd),
200
(sport->options & PJMEDIA_STEREO_MIX), 0);
202
pjmedia_convert_channel_1ton((pj_int16_t*)frame->buf,
203
(const pj_int16_t*)tmp_frame.buf,
204
s_afd->channel_count,
205
PJMEDIA_AFD_SPF(dn_afd),
209
frame->size = PJMEDIA_AFD_AVG_FSZ(s_afd);
210
frame->type = PJMEDIA_FRAME_TYPE_AUDIO;
216
static pj_status_t stereo_destroy(pjmedia_port *this_port)
218
struct stereo_port *sport = (struct stereo_port*) this_port;
220
if ((sport->options & PJMEDIA_STEREO_DONT_DESTROY_DN)==0) {
221
pjmedia_port_destroy(sport->dn_port);
222
sport->dn_port = NULL;