| 1 | /* |
|---|
| 2 | mediastreamer2 library - modular sound and video processing and streaming |
|---|
| 3 | Copyright (C) 2006 Simon MORLAT (simon.morlat@linphone.org) |
|---|
| 4 | |
|---|
| 5 | This program is free software; you can redistribute it and/or |
|---|
| 6 | modify it under the terms of the GNU General Public License |
|---|
| 7 | as published by the Free Software Foundation; either version 2 |
|---|
| 8 | of the License, or (at your option) any later version. |
|---|
| 9 | |
|---|
| 10 | This program is distributed in the hope that it will be useful, |
|---|
| 11 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
|---|
| 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|---|
| 13 | GNU General Public License for more details. |
|---|
| 14 | |
|---|
| 15 | You should have received a copy of the GNU General Public License |
|---|
| 16 | along with this program; if not, write to the Free Software |
|---|
| 17 | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
|---|
| 18 | */ |
|---|
| 19 | |
|---|
| 20 | |
|---|
| 21 | #ifdef HAVE_CONFIG_H |
|---|
| 22 | #include "mediastreamer-config.h" |
|---|
| 23 | #endif |
|---|
| 24 | |
|---|
| 25 | #include "mediastreamer2/mediastream.h" |
|---|
| 26 | |
|---|
| 27 | #include "mediastreamer2/dtmfgen.h" |
|---|
| 28 | #include "mediastreamer2/mssndcard.h" |
|---|
| 29 | #include "mediastreamer2/msrtp.h" |
|---|
| 30 | #include "mediastreamer2/msfileplayer.h" |
|---|
| 31 | #include "mediastreamer2/msfilerec.h" |
|---|
| 32 | #include "mediastreamer2/msvolume.h" |
|---|
| 33 | #include "mediastreamer2/msequalizer.h" |
|---|
| 34 | |
|---|
| 35 | #ifdef INET6 |
|---|
| 36 | #include <sys/types.h> |
|---|
| 37 | #ifndef WIN32 |
|---|
| 38 | #include <sys/socket.h> |
|---|
| 39 | #include <netdb.h> |
|---|
| 40 | #endif |
|---|
| 41 | #endif |
|---|
| 42 | |
|---|
| 43 | |
|---|
| 44 | #define MAX_RTP_SIZE 1500 |
|---|
| 45 | |
|---|
| 46 | |
|---|
| 47 | /* this code is not part of the library itself, it is part of the mediastream program */ |
|---|
| 48 | void audio_stream_free(AudioStream *stream) |
|---|
| 49 | { |
|---|
| 50 | if (stream->session!=NULL) { |
|---|
| 51 | rtp_session_unregister_event_queue(stream->session,stream->evq); |
|---|
| 52 | rtp_session_destroy(stream->session); |
|---|
| 53 | } |
|---|
| 54 | if (stream->evq) ortp_ev_queue_destroy(stream->evq); |
|---|
| 55 | if (stream->rtpsend!=NULL) ms_filter_destroy(stream->rtpsend); |
|---|
| 56 | if (stream->rtprecv!=NULL) ms_filter_destroy(stream->rtprecv); |
|---|
| 57 | if (stream->soundread!=NULL) ms_filter_destroy(stream->soundread); |
|---|
| 58 | if (stream->soundwrite!=NULL) ms_filter_destroy(stream->soundwrite); |
|---|
| 59 | if (stream->encoder!=NULL) ms_filter_destroy(stream->encoder); |
|---|
| 60 | if (stream->decoder!=NULL) ms_filter_destroy(stream->decoder); |
|---|
| 61 | if (stream->dtmfgen!=NULL) ms_filter_destroy(stream->dtmfgen); |
|---|
| 62 | if (stream->ec!=NULL) ms_filter_destroy(stream->ec); |
|---|
| 63 | if (stream->volrecv!=NULL) ms_filter_destroy(stream->volrecv); |
|---|
| 64 | if (stream->volsend!=NULL) ms_filter_destroy(stream->volsend); |
|---|
| 65 | if (stream->equalizer!=NULL) ms_filter_destroy(stream->equalizer); |
|---|
| 66 | if (stream->ticker!=NULL) ms_ticker_destroy(stream->ticker); |
|---|
| 67 | if (stream->read_resampler!=NULL) ms_filter_destroy(stream->read_resampler); |
|---|
| 68 | if (stream->write_resampler!=NULL) ms_filter_destroy(stream->write_resampler); |
|---|
| 69 | if (stream->dtmfgen_rtp!=NULL) ms_filter_destroy(stream->dtmfgen_rtp); |
|---|
| 70 | ms_free(stream); |
|---|
| 71 | } |
|---|
| 72 | |
|---|
| 73 | static int dtmf_tab[16]={'0','1','2','3','4','5','6','7','8','9','*','#','A','B','C','D'}; |
|---|
| 74 | |
|---|
| 75 | static void on_dtmf_received(RtpSession *s, int dtmf, void * user_data) |
|---|
| 76 | { |
|---|
| 77 | AudioStream *stream=(AudioStream*)user_data; |
|---|
| 78 | if (dtmf>15){ |
|---|
| 79 | ms_warning("Unsupported telephone-event type."); |
|---|
| 80 | return; |
|---|
| 81 | } |
|---|
| 82 | ms_message("Receiving dtmf %c.",dtmf_tab[dtmf]); |
|---|
| 83 | if (stream->dtmfgen!=NULL && stream->play_dtmfs){ |
|---|
| 84 | ms_filter_call_method(stream->dtmfgen,MS_DTMF_GEN_PUT,&dtmf_tab[dtmf]); |
|---|
| 85 | } |
|---|
| 86 | } |
|---|
| 87 | |
|---|
| 88 | bool_t ms_is_ipv6(const char *remote){ |
|---|
| 89 | bool_t ret=FALSE; |
|---|
| 90 | #ifdef INET6 |
|---|
| 91 | struct addrinfo hints, *res0; |
|---|
| 92 | |
|---|
| 93 | int err; |
|---|
| 94 | memset(&hints, 0, sizeof(hints)); |
|---|
| 95 | hints.ai_family = PF_UNSPEC; |
|---|
| 96 | hints.ai_socktype = SOCK_DGRAM; |
|---|
| 97 | err = getaddrinfo(remote,"8000", &hints, &res0); |
|---|
| 98 | if (err!=0) { |
|---|
| 99 | ms_warning ("get_local_addr_for: %s", gai_strerror(err)); |
|---|
| 100 | return FALSE; |
|---|
| 101 | } |
|---|
| 102 | ret=(res0->ai_addr->sa_family==AF_INET6); |
|---|
| 103 | freeaddrinfo(res0); |
|---|
| 104 | #endif |
|---|
| 105 | return ret; |
|---|
| 106 | } |
|---|
| 107 | |
|---|
| 108 | static void audio_stream_configure_resampler(MSFilter *resampler,MSFilter *from,MSFilter *to) { |
|---|
| 109 | int from_rate=0, to_rate=0; |
|---|
| 110 | ms_filter_call_method(from,MS_FILTER_GET_SAMPLE_RATE,&from_rate); |
|---|
| 111 | ms_filter_call_method(to,MS_FILTER_GET_SAMPLE_RATE,&to_rate); |
|---|
| 112 | ms_filter_call_method(resampler,MS_FILTER_SET_SAMPLE_RATE,&from_rate); |
|---|
| 113 | ms_filter_call_method(resampler,MS_FILTER_SET_OUTPUT_SAMPLE_RATE,&to_rate); |
|---|
| 114 | ms_message("configuring %s-->%s from rate[%i] to rate [%i]", |
|---|
| 115 | from->desc->name, to->desc->name, from_rate,to_rate); |
|---|
| 116 | } |
|---|
| 117 | |
|---|
| 118 | RtpSession * create_duplex_rtpsession( int locport, bool_t ipv6){ |
|---|
| 119 | RtpSession *rtpr; |
|---|
| 120 | rtpr=rtp_session_new(RTP_SESSION_SENDRECV); |
|---|
| 121 | rtp_session_set_recv_buf_size(rtpr,MAX_RTP_SIZE); |
|---|
| 122 | rtp_session_set_scheduling_mode(rtpr,0); |
|---|
| 123 | rtp_session_set_blocking_mode(rtpr,0); |
|---|
| 124 | rtp_session_enable_adaptive_jitter_compensation(rtpr,TRUE); |
|---|
| 125 | rtp_session_set_symmetric_rtp(rtpr,TRUE); |
|---|
| 126 | #ifndef HAVE_CSL |
|---|
| 127 | rtp_session_set_local_addr(rtpr,ipv6 ? "::" : "0.0.0.0",locport); |
|---|
| 128 | #endif |
|---|
| 129 | rtp_session_signal_connect(rtpr,"timestamp_jump",(RtpCallback)rtp_session_resync,(long)NULL); |
|---|
| 130 | rtp_session_signal_connect(rtpr,"ssrc_changed",(RtpCallback)rtp_session_resync,(long)NULL); |
|---|
| 131 | rtp_session_set_ssrc_changed_threshold(rtpr,0); |
|---|
| 132 | return rtpr; |
|---|
| 133 | } |
|---|
| 134 | |
|---|
| 135 | #if defined(_WIN32_WCE) |
|---|
| 136 | time_t |
|---|
| 137 | ms_time (time_t *t) |
|---|
| 138 | { |
|---|
| 139 | DWORD timemillis = GetTickCount(); |
|---|
| 140 | if (timemillis>0) |
|---|
| 141 | { |
|---|
| 142 | if (t!=NULL) |
|---|
| 143 | *t = timemillis/1000; |
|---|
| 144 | } |
|---|
| 145 | return timemillis/1000; |
|---|
| 146 | } |
|---|
| 147 | #endif |
|---|
| 148 | |
|---|
| 149 | bool_t audio_stream_alive(AudioStream * stream, int timeout){ |
|---|
| 150 | RtpSession *session=stream->session; |
|---|
| 151 | const rtp_stats_t *stats=rtp_session_get_stats(session); |
|---|
| 152 | if (stats->recv!=0){ |
|---|
| 153 | if (stream->evq){ |
|---|
| 154 | OrtpEvent *ev=ortp_ev_queue_get(stream->evq); |
|---|
| 155 | if (ev!=NULL){ |
|---|
| 156 | if (ortp_event_get_type(ev)==ORTP_EVENT_RTCP_PACKET_RECEIVED){ |
|---|
| 157 | stream->last_packet_time=ms_time(NULL); |
|---|
| 158 | } |
|---|
| 159 | ortp_event_destroy(ev); |
|---|
| 160 | } |
|---|
| 161 | } |
|---|
| 162 | if (stats->recv!=stream->last_packet_count){ |
|---|
| 163 | stream->last_packet_count=stats->recv; |
|---|
| 164 | stream->last_packet_time=ms_time(NULL); |
|---|
| 165 | }else{ |
|---|
| 166 | if (ms_time(NULL)-stream->last_packet_time>timeout){ |
|---|
| 167 | /* more than timeout seconds of inactivity*/ |
|---|
| 168 | return FALSE; |
|---|
| 169 | } |
|---|
| 170 | } |
|---|
| 171 | } |
|---|
| 172 | return TRUE; |
|---|
| 173 | } |
|---|
| 174 | |
|---|
| 175 | /*this function must be called from the MSTicker thread: |
|---|
| 176 | it replaces one filter by another one. |
|---|
| 177 | This is a dirty hack that works anyway. |
|---|
| 178 | It would be interesting to have something that does the job |
|---|
| 179 | simplier within the MSTicker api |
|---|
| 180 | */ |
|---|
| 181 | void audio_stream_change_decoder(AudioStream *stream, int payload){ |
|---|
| 182 | RtpSession *session=stream->session; |
|---|
| 183 | RtpProfile *prof=rtp_session_get_profile(session); |
|---|
| 184 | PayloadType *pt=rtp_profile_get_payload(prof,payload); |
|---|
| 185 | if (pt!=NULL){ |
|---|
| 186 | MSFilter *dec=ms_filter_create_decoder(pt->mime_type); |
|---|
| 187 | if (dec!=NULL){ |
|---|
| 188 | ms_filter_unlink(stream->rtprecv, 0, stream->decoder, 0); |
|---|
| 189 | ms_filter_unlink(stream->decoder,0,stream->dtmfgen,0); |
|---|
| 190 | ms_filter_postprocess(stream->decoder); |
|---|
| 191 | ms_filter_destroy(stream->decoder); |
|---|
| 192 | stream->decoder=dec; |
|---|
| 193 | if (pt->recv_fmtp!=NULL) |
|---|
| 194 | ms_filter_call_method(stream->decoder,MS_FILTER_ADD_FMTP,(void*)pt->recv_fmtp); |
|---|
| 195 | ms_filter_link (stream->rtprecv, 0, stream->decoder, 0); |
|---|
| 196 | ms_filter_link (stream->decoder,0 , stream->dtmfgen, 0); |
|---|
| 197 | ms_filter_preprocess(stream->decoder,stream->ticker); |
|---|
| 198 | |
|---|
| 199 | }else{ |
|---|
| 200 | ms_warning("No decoder found for %s",pt->mime_type); |
|---|
| 201 | } |
|---|
| 202 | }else{ |
|---|
| 203 | ms_warning("No payload defined with number %i",payload); |
|---|
| 204 | } |
|---|
| 205 | } |
|---|
| 206 | |
|---|
| 207 | static void payload_type_changed(RtpSession *session, unsigned long data){ |
|---|
| 208 | AudioStream *stream=(AudioStream*)data; |
|---|
| 209 | int pt=rtp_session_get_recv_payload_type(stream->session); |
|---|
| 210 | audio_stream_change_decoder(stream,pt); |
|---|
| 211 | } |
|---|
| 212 | |
|---|
| 213 | int audio_stream_start_full(AudioStream *stream, RtpProfile *profile, const char *remip,int remport, |
|---|
| 214 | int rem_rtcp_port, int payload,int jitt_comp, const char *infile, const char *outfile, |
|---|
| 215 | MSSndCard *playcard, MSSndCard *captcard, bool_t use_ec) |
|---|
| 216 | { |
|---|
| 217 | RtpSession *rtps=stream->session; |
|---|
| 218 | PayloadType *pt; |
|---|
| 219 | int tmp; |
|---|
| 220 | MSConnectionHelper h; |
|---|
| 221 | int sample_rate; |
|---|
| 222 | |
|---|
| 223 | rtp_session_set_profile(rtps,profile); |
|---|
| 224 | if (remport>0) rtp_session_set_remote_addr_full(rtps,remip,remport,rem_rtcp_port); |
|---|
| 225 | rtp_session_set_payload_type(rtps,payload); |
|---|
| 226 | rtp_session_set_jitter_compensation(rtps,jitt_comp); |
|---|
| 227 | |
|---|
| 228 | if (remport>0) |
|---|
| 229 | ms_filter_call_method(stream->rtpsend,MS_RTP_SEND_SET_SESSION,rtps); |
|---|
| 230 | stream->rtprecv=ms_filter_new(MS_RTP_RECV_ID); |
|---|
| 231 | ms_filter_call_method(stream->rtprecv,MS_RTP_RECV_SET_SESSION,rtps); |
|---|
| 232 | stream->session=rtps; |
|---|
| 233 | |
|---|
| 234 | stream->dtmfgen=ms_filter_new(MS_DTMF_GEN_ID); |
|---|
| 235 | rtp_session_signal_connect(rtps,"telephone-event",(RtpCallback)on_dtmf_received,(unsigned long)stream); |
|---|
| 236 | rtp_session_signal_connect(rtps,"payload_type_changed",(RtpCallback)payload_type_changed,(unsigned long)stream); |
|---|
| 237 | /* creates the local part */ |
|---|
| 238 | if (captcard!=NULL) stream->soundread=ms_snd_card_create_reader(captcard); |
|---|
| 239 | else { |
|---|
| 240 | stream->soundread=ms_filter_new(MS_FILE_PLAYER_ID); |
|---|
| 241 | stream->read_resampler=ms_filter_new(MS_RESAMPLE_ID); |
|---|
| 242 | if (infile!=NULL) audio_stream_play(stream,infile); |
|---|
| 243 | } |
|---|
| 244 | if (playcard!=NULL) stream->soundwrite=ms_snd_card_create_writer(playcard); |
|---|
| 245 | else { |
|---|
| 246 | stream->soundwrite=ms_filter_new(MS_FILE_REC_ID); |
|---|
| 247 | if (outfile!=NULL) audio_stream_record(stream,outfile); |
|---|
| 248 | } |
|---|
| 249 | |
|---|
| 250 | /* creates the couple of encoder/decoder */ |
|---|
| 251 | pt=rtp_profile_get_payload(profile,payload); |
|---|
| 252 | if (pt==NULL){ |
|---|
| 253 | ms_error("audiostream.c: undefined payload type."); |
|---|
| 254 | return -1; |
|---|
| 255 | } |
|---|
| 256 | if (rtp_profile_get_payload_from_mime (profile,"telephone-event")==NULL |
|---|
| 257 | && ( strcasecmp(pt->mime_type,"pcmu")==0 || strcasecmp(pt->mime_type,"pcma")==0)){ |
|---|
| 258 | /*if no telephone-event payload is usable and pcma or pcmu is used, we will generate |
|---|
| 259 | inband dtmf*/ |
|---|
| 260 | stream->dtmfgen_rtp=ms_filter_new (MS_DTMF_GEN_ID); |
|---|
| 261 | } |
|---|
| 262 | |
|---|
| 263 | if (ms_filter_call_method(stream->rtpsend,MS_FILTER_GET_SAMPLE_RATE,&sample_rate)!=0){ |
|---|
| 264 | ms_error("Sample rate is unknown for RTP side !"); |
|---|
| 265 | return -1; |
|---|
| 266 | } |
|---|
| 267 | |
|---|
| 268 | stream->encoder=ms_filter_create_encoder(pt->mime_type); |
|---|
| 269 | stream->decoder=ms_filter_create_decoder(pt->mime_type); |
|---|
| 270 | if ((stream->encoder==NULL) || (stream->decoder==NULL)){ |
|---|
| 271 | /* big problem: we have not a registered codec for this payload...*/ |
|---|
| 272 | ms_error("mediastream.c: No decoder available for payload %i.",payload); |
|---|
| 273 | return -1; |
|---|
| 274 | } |
|---|
| 275 | |
|---|
| 276 | stream->volsend=ms_filter_new(MS_VOLUME_ID); |
|---|
| 277 | stream->volrecv=ms_filter_new(MS_VOLUME_ID); |
|---|
| 278 | audio_stream_enable_echo_limiter(stream,stream->el_type); |
|---|
| 279 | audio_stream_enable_noise_gate(stream,stream->use_ng); |
|---|
| 280 | |
|---|
| 281 | if (stream->use_agc){ |
|---|
| 282 | int tmp=1; |
|---|
| 283 | if (stream->volsend==NULL) |
|---|
| 284 | stream->volsend=ms_filter_new(MS_VOLUME_ID); |
|---|
| 285 | ms_filter_call_method(stream->volsend,MS_VOLUME_ENABLE_AGC,&tmp); |
|---|
| 286 | } |
|---|
| 287 | |
|---|
| 288 | /* give the sound filters some properties */ |
|---|
| 289 | if (ms_filter_call_method(stream->soundread,MS_FILTER_SET_SAMPLE_RATE,&sample_rate) != 0) { |
|---|
| 290 | /* need to add resampler*/ |
|---|
| 291 | if (stream->read_resampler == NULL) stream->read_resampler=ms_filter_new(MS_RESAMPLE_ID); |
|---|
| 292 | } |
|---|
| 293 | |
|---|
| 294 | if (ms_filter_call_method(stream->soundwrite,MS_FILTER_SET_SAMPLE_RATE,&sample_rate) != 0) { |
|---|
| 295 | /* need to add resampler*/ |
|---|
| 296 | if (stream->write_resampler == NULL) stream->write_resampler=ms_filter_new(MS_RESAMPLE_ID); |
|---|
| 297 | } |
|---|
| 298 | |
|---|
| 299 | tmp=1; |
|---|
| 300 | ms_filter_call_method(stream->soundwrite,MS_FILTER_SET_NCHANNELS, &tmp); |
|---|
| 301 | |
|---|
| 302 | /*configure the echo canceller if required */ |
|---|
| 303 | if (!use_ec) { |
|---|
| 304 | ms_filter_destroy(stream->ec); |
|---|
| 305 | stream->ec=NULL; |
|---|
| 306 | } |
|---|
| 307 | if (stream->ec){ |
|---|
| 308 | ms_filter_call_method(stream->ec,MS_FILTER_SET_SAMPLE_RATE,&sample_rate); |
|---|
| 309 | } |
|---|
| 310 | |
|---|
| 311 | /* give the encoder/decoder some parameters*/ |
|---|
| 312 | ms_filter_call_method(stream->encoder,MS_FILTER_SET_SAMPLE_RATE,&pt->clock_rate); |
|---|
| 313 | ms_message("Payload's bitrate is %i",pt->normal_bitrate); |
|---|
| 314 | if (pt->normal_bitrate>0){ |
|---|
| 315 | ms_message("Setting audio encoder network bitrate to %i",pt->normal_bitrate); |
|---|
| 316 | ms_filter_call_method(stream->encoder,MS_FILTER_SET_BITRATE,&pt->normal_bitrate); |
|---|
| 317 | } |
|---|
| 318 | ms_filter_call_method(stream->decoder,MS_FILTER_SET_SAMPLE_RATE,&pt->clock_rate); |
|---|
| 319 | |
|---|
| 320 | if (pt->send_fmtp!=NULL) ms_filter_call_method(stream->encoder,MS_FILTER_ADD_FMTP, (void*)pt->send_fmtp); |
|---|
| 321 | if (pt->recv_fmtp!=NULL) ms_filter_call_method(stream->decoder,MS_FILTER_ADD_FMTP,(void*)pt->recv_fmtp); |
|---|
| 322 | |
|---|
| 323 | /*create the equalizer*/ |
|---|
| 324 | stream->equalizer=ms_filter_new(MS_EQUALIZER_ID); |
|---|
| 325 | tmp=stream->eq_active; |
|---|
| 326 | ms_filter_call_method(stream->equalizer,MS_EQUALIZER_SET_ACTIVE,&tmp); |
|---|
| 327 | /*configure resampler if needed*/ |
|---|
| 328 | if (stream->read_resampler){ |
|---|
| 329 | audio_stream_configure_resampler(stream->read_resampler,stream->soundread,stream->rtpsend); |
|---|
| 330 | } |
|---|
| 331 | |
|---|
| 332 | if (stream->write_resampler){ |
|---|
| 333 | audio_stream_configure_resampler(stream->write_resampler,stream->rtprecv,stream->soundwrite); |
|---|
| 334 | } |
|---|
| 335 | /* and then connect all */ |
|---|
| 336 | /* tip: draw yourself the picture if you don't understand */ |
|---|
| 337 | |
|---|
| 338 | /*sending graph*/ |
|---|
| 339 | ms_connection_helper_start(&h); |
|---|
| 340 | ms_connection_helper_link(&h,stream->soundread,-1,0); |
|---|
| 341 | if (stream->read_resampler) |
|---|
| 342 | ms_connection_helper_link(&h,stream->read_resampler,0,0); |
|---|
| 343 | if (stream->ec) |
|---|
| 344 | ms_connection_helper_link(&h,stream->ec,1,1); |
|---|
| 345 | if (stream->volsend) |
|---|
| 346 | ms_connection_helper_link(&h,stream->volsend,0,0); |
|---|
| 347 | if (stream->dtmfgen_rtp) |
|---|
| 348 | ms_connection_helper_link(&h,stream->dtmfgen_rtp,0,0); |
|---|
| 349 | ms_connection_helper_link(&h,stream->encoder,0,0); |
|---|
| 350 | ms_connection_helper_link(&h,stream->rtpsend,0,-1); |
|---|
| 351 | |
|---|
| 352 | /*receiving graph*/ |
|---|
| 353 | ms_connection_helper_start(&h); |
|---|
| 354 | ms_connection_helper_link(&h,stream->rtprecv,-1,0); |
|---|
| 355 | ms_connection_helper_link(&h,stream->decoder,0,0); |
|---|
| 356 | ms_connection_helper_link(&h,stream->dtmfgen,0,0); |
|---|
| 357 | if (stream->equalizer) |
|---|
| 358 | ms_connection_helper_link(&h,stream->equalizer,0,0); |
|---|
| 359 | if (stream->volrecv) |
|---|
| 360 | ms_connection_helper_link(&h,stream->volrecv,0,0); |
|---|
| 361 | if (stream->ec) |
|---|
| 362 | ms_connection_helper_link(&h,stream->ec,0,0); |
|---|
| 363 | if (stream->write_resampler) |
|---|
| 364 | ms_connection_helper_link(&h,stream->write_resampler,0,0); |
|---|
| 365 | ms_connection_helper_link(&h,stream->soundwrite,0,-1); |
|---|
| 366 | |
|---|
| 367 | /* create ticker */ |
|---|
| 368 | stream->ticker=ms_ticker_new(); |
|---|
| 369 | ms_ticker_set_name(stream->ticker,"Audio MSTicker"); |
|---|
| 370 | ms_ticker_attach(stream->ticker,stream->soundread); |
|---|
| 371 | ms_ticker_attach(stream->ticker,stream->rtprecv); |
|---|
| 372 | |
|---|
| 373 | return 0; |
|---|
| 374 | } |
|---|
| 375 | |
|---|
| 376 | |
|---|
| 377 | int audio_stream_start_with_files(AudioStream *stream, RtpProfile *prof,const char *remip, int remport, |
|---|
| 378 | int rem_rtcp_port, int pt,int jitt_comp, const char *infile, const char * outfile) |
|---|
| 379 | { |
|---|
| 380 | return audio_stream_start_full(stream,prof,remip,remport,rem_rtcp_port,pt,jitt_comp,infile,outfile,NULL,NULL,FALSE); |
|---|
| 381 | } |
|---|
| 382 | |
|---|
| 383 | AudioStream * audio_stream_start(RtpProfile *prof,int locport,const char *remip,int remport,int profile,int jitt_comp,bool_t use_ec) |
|---|
| 384 | { |
|---|
| 385 | MSSndCard *sndcard_playback; |
|---|
| 386 | MSSndCard *sndcard_capture; |
|---|
| 387 | AudioStream *stream; |
|---|
| 388 | sndcard_capture=ms_snd_card_manager_get_default_capture_card(ms_snd_card_manager_get()); |
|---|
| 389 | sndcard_playback=ms_snd_card_manager_get_default_playback_card(ms_snd_card_manager_get()); |
|---|
| 390 | if (sndcard_capture==NULL || sndcard_playback==NULL) |
|---|
| 391 | return NULL; |
|---|
| 392 | stream=audio_stream_new(locport, ms_is_ipv6(remip)); |
|---|
| 393 | if (audio_stream_start_full(stream,prof,remip,remport,remport+1,profile,jitt_comp,NULL,NULL,sndcard_playback,sndcard_capture,use_ec)==0) return stream; |
|---|
| 394 | audio_stream_free(stream); |
|---|
| 395 | return NULL; |
|---|
| 396 | } |
|---|
| 397 | |
|---|
| 398 | AudioStream *audio_stream_start_with_sndcards(RtpProfile *prof,int locport,const char *remip,int remport,int profile,int jitt_comp,MSSndCard *playcard, MSSndCard *captcard, bool_t use_ec) |
|---|
| 399 | { |
|---|
| 400 | AudioStream *stream; |
|---|
| 401 | if (playcard==NULL) { |
|---|
| 402 | ms_error("No playback card."); |
|---|
| 403 | return NULL; |
|---|
| 404 | } |
|---|
| 405 | if (captcard==NULL) { |
|---|
| 406 | ms_error("No capture card."); |
|---|
| 407 | return NULL; |
|---|
| 408 | } |
|---|
| 409 | stream=audio_stream_new(locport, ms_is_ipv6(remip)); |
|---|
| 410 | if (audio_stream_start_full(stream,prof,remip,remport,remport+1,profile,jitt_comp,NULL,NULL,playcard,captcard,use_ec)==0) return stream; |
|---|
| 411 | audio_stream_free(stream); |
|---|
| 412 | return NULL; |
|---|
| 413 | } |
|---|
| 414 | |
|---|
| 415 | void audio_stream_set_rtcp_information(AudioStream *st, const char *cname, const char *tool){ |
|---|
| 416 | if (st->session!=NULL){ |
|---|
| 417 | rtp_session_set_source_description(st->session,cname,NULL,NULL,NULL,NULL,tool , "This is free software (GPL) !"); |
|---|
| 418 | } |
|---|
| 419 | } |
|---|
| 420 | |
|---|
| 421 | void audio_stream_play(AudioStream *st, const char *name){ |
|---|
| 422 | if (ms_filter_get_id(st->soundread)==MS_FILE_PLAYER_ID){ |
|---|
| 423 | ms_filter_call_method_noarg(st->soundread,MS_FILE_PLAYER_CLOSE); |
|---|
| 424 | ms_filter_call_method(st->soundread,MS_FILE_PLAYER_OPEN,(void*)name); |
|---|
| 425 | if (st->read_resampler){ |
|---|
| 426 | audio_stream_configure_resampler(st->read_resampler,st->soundread,st->rtpsend); |
|---|
| 427 | } |
|---|
| 428 | ms_filter_call_method_noarg(st->soundread,MS_FILE_PLAYER_START); |
|---|
| 429 | }else{ |
|---|
| 430 | ms_error("Cannot play file: the stream hasn't been started with" |
|---|
| 431 | " audio_stream_start_with_files"); |
|---|
| 432 | } |
|---|
| 433 | } |
|---|
| 434 | |
|---|
| 435 | void audio_stream_record(AudioStream *st, const char *name){ |
|---|
| 436 | if (ms_filter_get_id(st->soundwrite)==MS_FILE_REC_ID){ |
|---|
| 437 | ms_filter_call_method_noarg(st->soundwrite,MS_FILE_REC_CLOSE); |
|---|
| 438 | ms_filter_call_method(st->soundwrite,MS_FILE_REC_OPEN,(void*)name); |
|---|
| 439 | ms_filter_call_method_noarg(st->soundwrite,MS_FILE_REC_START); |
|---|
| 440 | }else{ |
|---|
| 441 | ms_error("Cannot record file: the stream hasn't been started with" |
|---|
| 442 | " audio_stream_start_with_files"); |
|---|
| 443 | } |
|---|
| 444 | } |
|---|
| 445 | |
|---|
| 446 | |
|---|
| 447 | AudioStream *audio_stream_new(int locport, bool_t ipv6){ |
|---|
| 448 | AudioStream *stream=(AudioStream *)ms_new0(AudioStream,1); |
|---|
| 449 | ms_filter_enable_statistics(TRUE); |
|---|
| 450 | ms_filter_reset_statistics(); |
|---|
| 451 | MSFilterDesc *ec_desc=ms_filter_lookup_by_name("MSOslec"); |
|---|
| 452 | |
|---|
| 453 | stream->session=create_duplex_rtpsession(locport,ipv6); |
|---|
| 454 | /*some filters are created right now to allow configuration by the application before start() */ |
|---|
| 455 | stream->rtpsend=ms_filter_new(MS_RTP_SEND_ID); |
|---|
| 456 | |
|---|
| 457 | if (ec_desc!=NULL) |
|---|
| 458 | stream->ec=ms_filter_new_from_desc(ec_desc); |
|---|
| 459 | else |
|---|
| 460 | stream->ec=ms_filter_new(MS_SPEEX_EC_ID); |
|---|
| 461 | |
|---|
| 462 | stream->evq=ortp_ev_queue_new(); |
|---|
| 463 | rtp_session_register_event_queue(stream->session,stream->evq); |
|---|
| 464 | stream->play_dtmfs=TRUE; |
|---|
| 465 | stream->use_gc=FALSE; |
|---|
| 466 | stream->use_agc=FALSE; |
|---|
| 467 | stream->use_ng=FALSE; |
|---|
| 468 | return stream; |
|---|
| 469 | } |
|---|
| 470 | |
|---|
| 471 | void audio_stream_play_received_dtmfs(AudioStream *st, bool_t yesno){ |
|---|
| 472 | st->play_dtmfs=yesno; |
|---|
| 473 | } |
|---|
| 474 | |
|---|
| 475 | int audio_stream_start_now(AudioStream *stream, RtpProfile * prof, const char *remip, int remport, int rem_rtcp_port, int payload_type, int jitt_comp, MSSndCard *playcard, MSSndCard *captcard, bool_t use_ec){ |
|---|
| 476 | return audio_stream_start_full(stream,prof,remip,remport,rem_rtcp_port, |
|---|
| 477 | payload_type,jitt_comp,NULL,NULL,playcard,captcard,use_ec); |
|---|
| 478 | } |
|---|
| 479 | |
|---|
| 480 | void audio_stream_set_relay_session_id(AudioStream *stream, const char *id){ |
|---|
| 481 | ms_filter_call_method(stream->rtpsend, MS_RTP_SEND_SET_RELAY_SESSION_ID,(void*)id); |
|---|
| 482 | } |
|---|
| 483 | |
|---|
| 484 | void audio_stream_set_echo_canceller_params(AudioStream *stream, int tail_len_ms, int delay_ms, int framesize){ |
|---|
| 485 | if (tail_len_ms!=0) |
|---|
| 486 | ms_filter_call_method(stream->ec,MS_ECHO_CANCELLER_SET_TAIL_LENGTH,&tail_len_ms); |
|---|
| 487 | if (delay_ms!=0){ |
|---|
| 488 | ms_filter_call_method(stream->ec,MS_ECHO_CANCELLER_SET_DELAY,&delay_ms); |
|---|
| 489 | } |
|---|
| 490 | if (framesize!=0) |
|---|
| 491 | ms_filter_call_method(stream->ec,MS_ECHO_CANCELLER_SET_FRAMESIZE,&framesize); |
|---|
| 492 | } |
|---|
| 493 | |
|---|
| 494 | void audio_stream_enable_echo_limiter(AudioStream *stream, EchoLimiterType type){ |
|---|
| 495 | stream->el_type=type; |
|---|
| 496 | if (stream->volsend){ |
|---|
| 497 | bool_t enable_noise_gate = stream->el_type==ELControlFull; |
|---|
| 498 | ms_filter_call_method(stream->volrecv,MS_VOLUME_ENABLE_NOISE_GATE,&enable_noise_gate); |
|---|
| 499 | ms_filter_call_method(stream->volsend,MS_VOLUME_SET_PEER,type!=ELInactive?stream->volrecv:NULL); |
|---|
| 500 | } else { |
|---|
| 501 | ms_warning("cannot set echo limiter to mode [%i] because no volume send",type); |
|---|
| 502 | } |
|---|
| 503 | } |
|---|
| 504 | |
|---|
| 505 | void audio_stream_enable_gain_control(AudioStream *stream, bool_t val){ |
|---|
| 506 | stream->use_gc=val; |
|---|
| 507 | } |
|---|
| 508 | |
|---|
| 509 | void audio_stream_enable_automatic_gain_control(AudioStream *stream, bool_t val){ |
|---|
| 510 | stream->use_agc=val; |
|---|
| 511 | } |
|---|
| 512 | |
|---|
| 513 | void audio_stream_enable_noise_gate(AudioStream *stream, bool_t val){ |
|---|
| 514 | stream->use_ng=val; |
|---|
| 515 | if (stream->volsend){ |
|---|
| 516 | ms_filter_call_method(stream->volsend,MS_VOLUME_ENABLE_NOISE_GATE,&val); |
|---|
| 517 | } else { |
|---|
| 518 | ms_warning("cannot set noise gate mode to [%i] because no volume send",val); |
|---|
| 519 | } |
|---|
| 520 | |
|---|
| 521 | |
|---|
| 522 | } |
|---|
| 523 | |
|---|
| 524 | void audio_stream_set_mic_gain(AudioStream *stream, float gain){ |
|---|
| 525 | if (stream->volsend){ |
|---|
| 526 | ms_filter_call_method(stream->volsend,MS_VOLUME_SET_GAIN,&gain); |
|---|
| 527 | }else ms_warning("Could not apply gain: gain control wasn't activated. " |
|---|
| 528 | "Use audio_stream_enable_gain_control() before starting the stream."); |
|---|
| 529 | } |
|---|
| 530 | |
|---|
| 531 | void audio_stream_enable_equalizer(AudioStream *stream, bool_t enabled){ |
|---|
| 532 | stream->eq_active=enabled; |
|---|
| 533 | if (stream->equalizer){ |
|---|
| 534 | int tmp=enabled; |
|---|
| 535 | ms_filter_call_method(stream->equalizer,MS_EQUALIZER_SET_ACTIVE,&tmp); |
|---|
| 536 | } |
|---|
| 537 | } |
|---|
| 538 | |
|---|
| 539 | void audio_stream_equalizer_set_gain(AudioStream *stream, int frequency, float gain, int freq_width){ |
|---|
| 540 | if (stream->equalizer){ |
|---|
| 541 | MSEqualizerGain d; |
|---|
| 542 | d.frequency=frequency; |
|---|
| 543 | d.gain=gain; |
|---|
| 544 | d.width=freq_width; |
|---|
| 545 | ms_filter_call_method(stream->equalizer,MS_EQUALIZER_SET_GAIN,&d); |
|---|
| 546 | } |
|---|
| 547 | } |
|---|
| 548 | |
|---|
| 549 | void audio_stream_stop(AudioStream * stream) |
|---|
| 550 | { |
|---|
| 551 | if (stream->ticker){ |
|---|
| 552 | MSConnectionHelper h; |
|---|
| 553 | ms_ticker_detach(stream->ticker,stream->soundread); |
|---|
| 554 | ms_ticker_detach(stream->ticker,stream->rtprecv); |
|---|
| 555 | |
|---|
| 556 | rtp_stats_display(rtp_session_get_stats(stream->session),"Audio session's RTP statistics"); |
|---|
| 557 | |
|---|
| 558 | /*dismantle the outgoing graph*/ |
|---|
| 559 | ms_connection_helper_start(&h); |
|---|
| 560 | ms_connection_helper_unlink(&h,stream->soundread,-1,0); |
|---|
| 561 | if (stream->read_resampler!=NULL) |
|---|
| 562 | ms_connection_helper_unlink(&h,stream->read_resampler,0,0); |
|---|
| 563 | if (stream->ec!=NULL) |
|---|
| 564 | ms_connection_helper_unlink(&h,stream->ec,1,1); |
|---|
| 565 | if (stream->volsend!=NULL) |
|---|
| 566 | ms_connection_helper_unlink(&h,stream->volsend,0,0); |
|---|
| 567 | if (stream->dtmfgen_rtp) |
|---|
| 568 | ms_connection_helper_unlink(&h,stream->dtmfgen_rtp,0,0); |
|---|
| 569 | ms_connection_helper_unlink(&h,stream->encoder,0,0); |
|---|
| 570 | ms_connection_helper_unlink(&h,stream->rtpsend,0,-1); |
|---|
| 571 | |
|---|
| 572 | /*dismantle the receiving graph*/ |
|---|
| 573 | ms_connection_helper_start(&h); |
|---|
| 574 | ms_connection_helper_unlink(&h,stream->rtprecv,-1,0); |
|---|
| 575 | ms_connection_helper_unlink(&h,stream->decoder,0,0); |
|---|
| 576 | ms_connection_helper_unlink(&h,stream->dtmfgen,0,0); |
|---|
| 577 | if (stream->equalizer) |
|---|
| 578 | ms_connection_helper_unlink(&h,stream->equalizer,0,0); |
|---|
| 579 | if (stream->volrecv!=NULL) |
|---|
| 580 | ms_connection_helper_unlink(&h,stream->volrecv,0,0); |
|---|
| 581 | if (stream->ec!=NULL) |
|---|
| 582 | ms_connection_helper_unlink(&h,stream->ec,0,0); |
|---|
| 583 | if (stream->write_resampler!=NULL) |
|---|
| 584 | ms_connection_helper_unlink(&h,stream->write_resampler,0,0); |
|---|
| 585 | ms_connection_helper_unlink(&h,stream->soundwrite,0,-1); |
|---|
| 586 | |
|---|
| 587 | } |
|---|
| 588 | audio_stream_free(stream); |
|---|
| 589 | ms_filter_log_statistics(); |
|---|
| 590 | } |
|---|
| 591 | |
|---|
| 592 | RingStream * ring_start(const char *file, int interval, MSSndCard *sndcard){ |
|---|
| 593 | return ring_start_with_cb(file,interval,sndcard,NULL,NULL); |
|---|
| 594 | } |
|---|
| 595 | |
|---|
| 596 | RingStream * ring_start_with_cb(const char *file,int interval,MSSndCard *sndcard, MSFilterNotifyFunc func,void * user_data) |
|---|
| 597 | { |
|---|
| 598 | RingStream *stream; |
|---|
| 599 | int tmp; |
|---|
| 600 | int srcrate,dstrate; |
|---|
| 601 | MSConnectionHelper h; |
|---|
| 602 | |
|---|
| 603 | stream=(RingStream *)ms_new0(RingStream,1); |
|---|
| 604 | stream->source=ms_filter_new(MS_FILE_PLAYER_ID); |
|---|
| 605 | if (file) |
|---|
| 606 | ms_filter_call_method(stream->source,MS_FILE_PLAYER_OPEN,(void*)file); |
|---|
| 607 | |
|---|
| 608 | ms_filter_call_method(stream->source,MS_FILE_PLAYER_LOOP,&interval); |
|---|
| 609 | ms_filter_call_method_noarg(stream->source,MS_FILE_PLAYER_START); |
|---|
| 610 | if (func!=NULL) |
|---|
| 611 | ms_filter_set_notify_callback(stream->source,func,user_data); |
|---|
| 612 | stream->gendtmf=ms_filter_new(MS_DTMF_GEN_ID); |
|---|
| 613 | |
|---|
| 614 | |
|---|
| 615 | stream->sndwrite=ms_snd_card_create_writer(sndcard); |
|---|
| 616 | ms_filter_call_method(stream->source,MS_FILTER_GET_SAMPLE_RATE,&srcrate); |
|---|
| 617 | ms_filter_call_method(stream->gendtmf,MS_FILTER_SET_SAMPLE_RATE,&srcrate); |
|---|
| 618 | ms_filter_call_method(stream->sndwrite,MS_FILTER_SET_SAMPLE_RATE,&srcrate); |
|---|
| 619 | ms_filter_call_method(stream->sndwrite,MS_FILTER_GET_SAMPLE_RATE,&dstrate); |
|---|
| 620 | if (srcrate!=dstrate){ |
|---|
| 621 | stream->write_resampler=ms_filter_new(MS_RESAMPLE_ID); |
|---|
| 622 | ms_filter_call_method(stream->write_resampler,MS_FILTER_SET_SAMPLE_RATE,&srcrate); |
|---|
| 623 | ms_filter_call_method(stream->write_resampler,MS_FILTER_SET_OUTPUT_SAMPLE_RATE,&dstrate); |
|---|
| 624 | ms_message("configuring resampler from rate[%i] to rate [%i]", srcrate,dstrate); |
|---|
| 625 | } |
|---|
| 626 | ms_filter_call_method(stream->source,MS_FILTER_GET_NCHANNELS,&tmp); |
|---|
| 627 | ms_filter_call_method(stream->gendtmf,MS_FILTER_SET_NCHANNELS,&tmp); |
|---|
| 628 | ms_filter_call_method(stream->sndwrite,MS_FILTER_SET_NCHANNELS,&tmp); |
|---|
| 629 | |
|---|
| 630 | stream->volume = ms_filter_new(MS_VOLUME_ID); |
|---|
| 631 | |
|---|
| 632 | stream->ticker=ms_ticker_new(); |
|---|
| 633 | |
|---|
| 634 | ms_ticker_set_name(stream->ticker,"Audio (ring) MSTicker"); |
|---|
| 635 | |
|---|
| 636 | ms_connection_helper_start(&h); |
|---|
| 637 | ms_connection_helper_link(&h,stream->source,-1,0); |
|---|
| 638 | ms_connection_helper_link(&h,stream->volume,0,0); |
|---|
| 639 | ms_connection_helper_link(&h,stream->gendtmf,0,0); |
|---|
| 640 | if (stream->write_resampler) |
|---|
| 641 | ms_connection_helper_link(&h,stream->write_resampler,0,0); |
|---|
| 642 | ms_connection_helper_link(&h,stream->sndwrite,0,-1); |
|---|
| 643 | ms_ticker_attach(stream->ticker,stream->source); |
|---|
| 644 | |
|---|
| 645 | return stream; |
|---|
| 646 | } |
|---|
| 647 | |
|---|
| 648 | void ring_play_dtmf(RingStream *stream, char dtmf, int duration_ms){ |
|---|
| 649 | if (duration_ms>0) |
|---|
| 650 | ms_filter_call_method(stream->gendtmf, MS_DTMF_GEN_PLAY, &dtmf); |
|---|
| 651 | else ms_filter_call_method(stream->gendtmf, MS_DTMF_GEN_START, &dtmf); |
|---|
| 652 | } |
|---|
| 653 | |
|---|
| 654 | void ring_stop_dtmf(RingStream *stream){ |
|---|
| 655 | ms_filter_call_method_noarg(stream->gendtmf, MS_DTMF_GEN_STOP); |
|---|
| 656 | } |
|---|
| 657 | |
|---|
| 658 | void ring_stop(RingStream *stream){ |
|---|
| 659 | MSConnectionHelper h; |
|---|
| 660 | ms_ticker_detach(stream->ticker,stream->source); |
|---|
| 661 | |
|---|
| 662 | ms_connection_helper_start(&h); |
|---|
| 663 | ms_connection_helper_unlink(&h,stream->source,-1,0); |
|---|
| 664 | ms_connection_helper_unlink(&h,stream->gendtmf,0,0); |
|---|
| 665 | if (stream->write_resampler) |
|---|
| 666 | ms_connection_helper_unlink(&h,stream->write_resampler,0,0); |
|---|
| 667 | ms_connection_helper_unlink(&h,stream->sndwrite,0,-1); |
|---|
| 668 | |
|---|
| 669 | ms_ticker_destroy(stream->ticker); |
|---|
| 670 | ms_filter_destroy(stream->source); |
|---|
| 671 | ms_filter_destroy(stream->volume); |
|---|
| 672 | ms_filter_destroy(stream->gendtmf); |
|---|
| 673 | ms_filter_destroy(stream->sndwrite); |
|---|
| 674 | ms_free(stream); |
|---|
| 675 | #ifdef _WIN32_WCE |
|---|
| 676 | ms_warning("Sleeping a bit after closing the audio device..."); |
|---|
| 677 | ms_sleep(1); |
|---|
| 678 | #endif |
|---|
| 679 | } |
|---|
| 680 | |
|---|
| 681 | |
|---|
| 682 | int audio_stream_send_dtmf(AudioStream *stream, char dtmf) |
|---|
| 683 | { |
|---|
| 684 | if (stream->dtmfgen_rtp) |
|---|
| 685 | ms_filter_call_method(stream->dtmfgen_rtp,MS_DTMF_GEN_PLAY,&dtmf); |
|---|
| 686 | else if (stream->rtpsend) |
|---|
| 687 | ms_filter_call_method(stream->rtpsend,MS_RTP_SEND_SEND_DTMF,&dtmf); |
|---|
| 688 | return 0; |
|---|
| 689 | } |
|---|
| 690 | |
|---|
| 691 | void audio_stream_get_local_rtp_stats(AudioStream *stream, rtp_stats_t *lstats){ |
|---|
| 692 | if (stream->session){ |
|---|
| 693 | const rtp_stats_t *stats=rtp_session_get_stats(stream->session); |
|---|
| 694 | memcpy(lstats,stats,sizeof(*stats)); |
|---|
| 695 | }else memset(lstats,0,sizeof(rtp_stats_t)); |
|---|
| 696 | } |
|---|
| 697 | |
|---|
| 698 | |
|---|
| 699 | void audio_stream_mute_rtp(AudioStream *stream, bool_t val) |
|---|
| 700 | { |
|---|
| 701 | if (stream->rtpsend){ |
|---|
| 702 | if (val) |
|---|
| 703 | ms_filter_call_method(stream->rtpsend,MS_RTP_SEND_MUTE_MIC,&val); |
|---|
| 704 | else |
|---|
| 705 | ms_filter_call_method(stream->rtpsend,MS_RTP_SEND_UNMUTE_MIC,&val); |
|---|
| 706 | } |
|---|
| 707 | } |
|---|