Tristan Matthews | 0a329cc | 2013-07-17 13:20:14 -0400 | [diff] [blame] | 1 | /* $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 | |
| 21 | #include <pjmedia/stereo.h> |
| 22 | #include <pj/assert.h> |
| 23 | #include <pj/pool.h> |
| 24 | #include <pj/string.h> |
| 25 | |
| 26 | |
| 27 | #define SIGNATURE PJMEDIA_SIG_PORT_STEREO |
| 28 | |
| 29 | |
| 30 | struct stereo_port |
| 31 | { |
| 32 | pjmedia_port base; |
| 33 | pjmedia_port *dn_port; |
| 34 | unsigned options; |
| 35 | pj_int16_t *put_buf; |
| 36 | pj_int16_t *get_buf; |
| 37 | }; |
| 38 | |
| 39 | |
| 40 | |
| 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); |
| 46 | |
| 47 | |
| 48 | |
| 49 | PJ_DEF(pj_status_t) pjmedia_stereo_port_create( pj_pool_t *pool, |
| 50 | pjmedia_port *dn_port, |
| 51 | unsigned channel_count, |
| 52 | unsigned options, |
| 53 | pjmedia_port **p_port ) |
| 54 | { |
| 55 | const pj_str_t name = pj_str("stereo"); |
| 56 | struct stereo_port *sport; |
| 57 | unsigned samples_per_frame; |
| 58 | |
| 59 | /* Validate arguments. */ |
| 60 | PJ_ASSERT_RETURN(pool && dn_port && channel_count && p_port, PJ_EINVAL); |
| 61 | |
| 62 | /* Only supports 16bit samples per frame */ |
| 63 | PJ_ASSERT_RETURN(PJMEDIA_PIA_BITS(&dn_port->info) == 16, |
| 64 | PJMEDIA_ENCBITS); |
| 65 | |
| 66 | /* Validate channel counts */ |
| 67 | PJ_ASSERT_RETURN(((PJMEDIA_PIA_CCNT(&dn_port->info)>1 && |
| 68 | channel_count==1) || |
| 69 | (PJMEDIA_PIA_CCNT(&dn_port->info)==1 && |
| 70 | channel_count>1)), |
| 71 | PJ_EINVAL); |
| 72 | |
| 73 | /* Create and initialize port. */ |
| 74 | sport = PJ_POOL_ZALLOC_T(pool, struct stereo_port); |
| 75 | PJ_ASSERT_RETURN(sport != NULL, PJ_ENOMEM); |
| 76 | |
| 77 | samples_per_frame = PJMEDIA_PIA_SPF(&dn_port->info) * channel_count / |
| 78 | PJMEDIA_PIA_CCNT(&dn_port->info); |
| 79 | |
| 80 | pjmedia_port_info_init(&sport->base.info, &name, SIGNATURE, |
| 81 | PJMEDIA_PIA_SRATE(&dn_port->info), |
| 82 | channel_count, |
| 83 | PJMEDIA_PIA_BITS(&dn_port->info), |
| 84 | samples_per_frame); |
| 85 | |
| 86 | sport->dn_port = dn_port; |
| 87 | sport->options = options; |
| 88 | |
| 89 | /* We always need buffer for put_frame */ |
| 90 | sport->put_buf = (pj_int16_t*) |
| 91 | pj_pool_alloc(pool, |
| 92 | PJMEDIA_PIA_AVG_FSZ(&dn_port->info)); |
| 93 | |
| 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*) |
| 97 | pj_pool_alloc(pool, |
| 98 | PJMEDIA_PIA_AVG_FSZ(&dn_port->info)); |
| 99 | } |
| 100 | |
| 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; |
| 105 | |
| 106 | |
| 107 | /* Done */ |
| 108 | *p_port = &sport->base; |
| 109 | |
| 110 | return PJ_SUCCESS; |
| 111 | } |
| 112 | |
| 113 | static pj_status_t stereo_put_frame(pjmedia_port *this_port, |
| 114 | pjmedia_frame *frame) |
| 115 | { |
| 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; |
| 119 | |
| 120 | /* Return if we don't have downstream port. */ |
| 121 | if (sport->dn_port == NULL) { |
| 122 | return PJ_SUCCESS; |
| 123 | } |
| 124 | |
| 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, |
| 127 | 1); |
| 128 | |
| 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), |
| 137 | 0); |
| 138 | } else { |
| 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), |
| 143 | sport->options); |
| 144 | } |
| 145 | tmp_frame.size = PJMEDIA_AFD_AVG_FSZ(dn_afd); |
| 146 | } else { |
| 147 | tmp_frame.buf = frame->buf; |
| 148 | tmp_frame.size = frame->size; |
| 149 | } |
| 150 | |
| 151 | tmp_frame.type = frame->type; |
| 152 | tmp_frame.timestamp.u64 = frame->timestamp.u64; |
| 153 | |
| 154 | return pjmedia_port_put_frame( sport->dn_port, &tmp_frame ); |
| 155 | } |
| 156 | |
| 157 | |
| 158 | |
| 159 | static pj_status_t stereo_get_frame(pjmedia_port *this_port, |
| 160 | pjmedia_frame *frame) |
| 161 | { |
| 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; |
| 165 | pj_status_t status; |
| 166 | |
| 167 | /* Return silence if we don't have downstream port */ |
| 168 | if (sport->dn_port == NULL) { |
| 169 | pj_bzero(frame->buf, frame->size); |
| 170 | return PJ_SUCCESS; |
| 171 | } |
| 172 | |
| 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, |
| 175 | 1); |
| 176 | |
| 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; |
| 181 | |
| 182 | status = pjmedia_port_get_frame( sport->dn_port, &tmp_frame); |
| 183 | if (status != PJ_SUCCESS) |
| 184 | return status; |
| 185 | |
| 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); |
| 192 | return PJ_SUCCESS; |
| 193 | } |
| 194 | |
| 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); |
| 201 | } else { |
| 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), |
| 206 | sport->options); |
| 207 | } |
| 208 | |
| 209 | frame->size = PJMEDIA_AFD_AVG_FSZ(s_afd); |
| 210 | frame->type = PJMEDIA_FRAME_TYPE_AUDIO; |
| 211 | |
| 212 | return PJ_SUCCESS; |
| 213 | } |
| 214 | |
| 215 | |
| 216 | static pj_status_t stereo_destroy(pjmedia_port *this_port) |
| 217 | { |
| 218 | struct stereo_port *sport = (struct stereo_port*) this_port; |
| 219 | |
| 220 | if ((sport->options & PJMEDIA_STEREO_DONT_DESTROY_DN)==0) { |
| 221 | pjmedia_port_destroy(sport->dn_port); |
| 222 | sport->dn_port = NULL; |
| 223 | } |
| 224 | |
| 225 | return PJ_SUCCESS; |
| 226 | } |
| 227 | |