summaryrefslogtreecommitdiffstats
path: root/kopete/protocols/jabber/jingle/libjingle/talk/third_party/mediastreamer/audiostream.c
blob: 42198d6f38a9cb352d8fcf751407ef7fed8de5d5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
/*
  The mediastreamer library aims at providing modular media processing and I/O
	for linphone, but also for any telephony application.
  Copyright (C) 2001  Simon MORLAT simon.morlat@linphone.org
  										
  This library is free software; you can redistribute it and/or
  modify it under the terms of the GNU Lesser General Public
  License as published by the Free Software Foundation; either
  version 2.1 of the License, or (at your option) any later version.

  This library is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  Lesser General Public License for more details.

  You should have received a copy of the GNU Lesser General Public
  License along with this library; if not, write to the Free Software
  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
*/


#include "mediastream.h"
#ifdef INET6
	#include <sys/types.h>
	#include <sys/socket.h>
	#include <netdb.h>
#endif


#define MAX_RTP_SIZE	1500

/* this code is not part of the library itself, it is part of the mediastream program */
void audio_stream_free(AudioStream *stream)
{
	RtpSession *s;
	RtpSession *destroyed=NULL;
	if (stream->rtprecv!=NULL) {
		s=ms_rtp_recv_get_session(MS_RTP_RECV(stream->rtprecv));
		if (s!=NULL){
			destroyed=s;
			rtp_session_destroy(s);
		}
		ms_filter_destroy(stream->rtprecv);
	}
	if (stream->rtpsend!=NULL) {
		s=ms_rtp_send_get_session(MS_RTP_SEND(stream->rtpsend));
		if (s!=NULL){
			if (s!=destroyed)
				rtp_session_destroy(s);
		}
		ms_filter_destroy(stream->rtpsend);
	}
	if (stream->soundread!=NULL) ms_filter_destroy(stream->soundread);
	if (stream->soundwrite!=NULL) ms_filter_destroy(stream->soundwrite);
	if (stream->encoder!=NULL) ms_filter_destroy(stream->encoder);
	if (stream->decoder!=NULL) ms_filter_destroy(stream->decoder);
	if (stream->timer!=NULL) ms_sync_destroy(stream->timer);
	g_free(stream);
}

static int dtmf_tab[16]={'0','1','2','3','4','5','6','7','8','9','*','#','A','B','C','D'};

static void on_dtmf_received(RtpSession *s,gint dtmf,gpointer user_data)
{
	AudioStream *stream=(AudioStream*)user_data;
	if (dtmf>15){
		g_warning("Unsupported telephone-event type.");
		return;
	}
	g_message("Receiving dtmf %c.",dtmf_tab[dtmf]);
	if (stream!=NULL){
		if (strcmp(stream->soundwrite->klass->name,"OssWrite")==0)
			ms_oss_write_play_dtmf(MS_OSS_WRITE(stream->soundwrite),dtmf_tab[dtmf]);
	}
}

static void on_timestamp_jump(RtpSession *s,guint32* ts, gpointer user_data)
{
	g_warning("The remote sip-phone has send data with a future timestamp: %u,"
			"resynchronising session.",*ts);
	rtp_session_reset(s);
}

static const char *ip4local="0.0.0.0";
static const char *ip6local="::";

const char *get_local_addr_for(const char *remote)
{
	const char *ret;
#ifdef INET6
	char num[8];
	struct addrinfo hints, *res0;
	int err;
	memset(&hints, 0, sizeof(hints));
	hints.ai_family = PF_UNSPEC;
	hints.ai_socktype = SOCK_DGRAM;
	err = getaddrinfo(remote,"8000", &hints, &res0);
	if (err!=0) {
		g_warning ("get_local_addr_for: %s", gai_strerror(err));
		return ip4local;
	}
	ret=(res0->ai_addr->sa_family==AF_INET6) ? ip6local : ip4local; 
	freeaddrinfo(res0);
#else
	ret=ip4local;
#endif
	return ret;
}

void create_duplex_rtpsession(RtpProfile *profile, int locport,char *remip,int remport,
				int payload,int jitt_comp,
			RtpSession **recvsend){
	RtpSession *rtpr;
	rtpr=rtp_session_new(RTP_SESSION_SENDRECV);
	rtp_session_max_buf_size_set(rtpr,MAX_RTP_SIZE);
	rtp_session_set_profile(rtpr,profile);
	rtp_session_set_local_addr(rtpr,get_local_addr_for(remip),locport);
	if (remport>0) rtp_session_set_remote_addr(rtpr,remip,remport);
	rtp_session_set_scheduling_mode(rtpr,0);
	rtp_session_set_blocking_mode(rtpr,0);
	rtp_session_set_payload_type(rtpr,payload);
	rtp_session_set_jitter_compensation(rtpr,jitt_comp);
	rtp_session_enable_adaptive_jitter_compensation(rtpr,TRUE);
	/*rtp_session_signal_connect(rtpr,"timestamp_jump",(RtpCallback)on_timestamp_jump,NULL);*/
	*recvsend=rtpr;
}

void create_rtp_sessions(RtpProfile *profile, int locport,char *remip,int remport,
				int payload,int jitt_comp,
			RtpSession **recv, RtpSession **send){
	RtpSession *rtps,*rtpr;
	PayloadType *pt;
	/* creates two rtp filters to recv send streams (remote part)*/
	
	rtps=rtp_session_new(RTP_SESSION_SENDONLY);
	rtp_session_max_buf_size_set(rtps,MAX_RTP_SIZE);
	rtp_session_set_profile(rtps,profile);
#ifdef INET6
	rtp_session_set_local_addr(rtps,"::",locport+2);
#else
	rtp_session_set_local_addr(rtps,"0.0.0.0",locport+2);
#endif
	rtp_session_set_remote_addr(rtps,remip,remport);
	rtp_session_set_scheduling_mode(rtps,0);
	rtp_session_set_blocking_mode(rtps,0);
	rtp_session_set_payload_type(rtps,payload);
	rtp_session_set_jitter_compensation(rtps,jitt_comp);
	
	rtpr=rtp_session_new(RTP_SESSION_RECVONLY);
	rtp_session_max_buf_size_set(rtpr,MAX_RTP_SIZE);
	rtp_session_set_profile(rtpr,profile);
#ifdef INET6
	rtp_session_set_local_addr(rtpr,"::",locport);
#else
	rtp_session_set_local_addr(rtpr,"0.0.0.0",locport);
#endif
	rtp_session_set_scheduling_mode(rtpr,0);
	rtp_session_set_blocking_mode(rtpr,0);
	rtp_session_set_payload_type(rtpr,payload);
	rtp_session_set_jitter_compensation(rtpr,jitt_comp);
	rtp_session_signal_connect(rtpr,"telephone-event",(RtpCallback)on_dtmf_received,NULL);
	rtp_session_signal_connect(rtpr,"timestamp_jump",(RtpCallback)on_timestamp_jump,NULL);
	*recv=rtpr;
	*send=rtps;
	
}


AudioStream * audio_stream_start_full(RtpProfile *profile, int locport,char *remip,int remport,
				int payload,int jitt_comp, gchar *infile, gchar *outfile, SndCard *playcard, SndCard *captcard)
{
	AudioStream *stream=g_new0(AudioStream,1);
	RtpSession *rtps,*rtpr;
	PayloadType *pt;
	
	/* //create_rtp_sessions(profile,locport,remip,remport,payload,jitt_comp,&rtpr,&rtps); */
	
	create_duplex_rtpsession(profile,locport,remip,remport,payload,jitt_comp,&rtpr);
	rtp_session_signal_connect(rtpr,"telephone-event",(RtpCallback)on_dtmf_received,(gpointer)stream);
	rtps=rtpr;
	
	stream->recv_session = rtpr;
	stream->send_session = rtps;
	stream->rtpsend=ms_rtp_send_new();
	ms_rtp_send_set_session(MS_RTP_SEND(stream->rtpsend),rtps);
	stream->rtprecv=ms_rtp_recv_new();
	ms_rtp_recv_set_session(MS_RTP_RECV(stream->rtprecv),rtpr);
	
	
	/* creates the local part */
	if (infile==NULL) stream->soundread=snd_card_create_read_filter(captcard);
	else stream->soundread=ms_read_new(infile);
	if (outfile==NULL) stream->soundwrite=snd_card_create_write_filter(playcard);
	else stream->soundwrite=ms_write_new(outfile);
	
	/* creates the couple of encoder/decoder */
	pt=rtp_profile_get_payload(profile,payload);
	if (pt==NULL){
		g_error("audiostream.c: undefined payload type.");
		return NULL;
	}
	stream->encoder=ms_encoder_new_with_string_id(pt->mime_type);
	stream->decoder=ms_decoder_new_with_string_id(pt->mime_type);
	if ((stream->encoder==NULL) || (stream->decoder==NULL)){
		/* big problem: we have not a registered codec for this payload...*/
		audio_stream_free(stream);
		g_error("mediastream.c: No decoder available for payload %i.",payload);
		return NULL;
	}
	/* give the sound filters some properties */
	ms_filter_set_property(stream->soundread,MS_FILTER_PROPERTY_FREQ,&pt->clock_rate);
	ms_filter_set_property(stream->soundwrite,MS_FILTER_PROPERTY_FREQ,&pt->clock_rate);
	
	/* give the encoder/decoder some parameters*/
	ms_filter_set_property(stream->encoder,MS_FILTER_PROPERTY_FREQ,&pt->clock_rate);
	ms_filter_set_property(stream->encoder,MS_FILTER_PROPERTY_BITRATE,&pt->normal_bitrate);
	ms_filter_set_property(stream->decoder,MS_FILTER_PROPERTY_FREQ,&pt->clock_rate);
	ms_filter_set_property(stream->decoder,MS_FILTER_PROPERTY_BITRATE,&pt->normal_bitrate);
	
	ms_filter_set_property(stream->encoder,MS_FILTER_PROPERTY_FMTP, (void*)pt->fmtp);
	ms_filter_set_property(stream->decoder,MS_FILTER_PROPERTY_FMTP,(void*)pt->fmtp);
	/* create the synchronisation source */
	stream->timer=ms_timer_new();
	
	/* and then connect all */
	ms_filter_add_link(stream->soundread,stream->encoder);
	ms_filter_add_link(stream->encoder,stream->rtpsend);
	ms_filter_add_link(stream->rtprecv,stream->decoder);
	ms_filter_add_link(stream->decoder,stream->soundwrite);
	
	ms_sync_attach(stream->timer,stream->soundread);
	ms_sync_attach(stream->timer,stream->rtprecv);
	
	/* and start */
	ms_start(stream->timer);
	
	return stream;
}

static int defcard=0;

void audio_stream_set_default_card(int cardindex){
	defcard=cardindex;
}

AudioStream * audio_stream_start_with_files(RtpProfile *prof,int locport,char *remip,
		int remport,int profile,int jitt_comp,gchar *infile, gchar*outfile)
{
	return audio_stream_start_full(prof,locport,remip,remport,profile,jitt_comp,infile,outfile,NULL,NULL);
}

AudioStream * audio_stream_start(RtpProfile *prof,int locport,char *remip,int remport,int profile,int jitt_comp)
{
	SndCard *sndcard;
	sndcard=snd_card_manager_get_card(snd_card_manager,defcard);
	return audio_stream_start_full(prof,locport,remip,remport,profile,jitt_comp,NULL,NULL,sndcard,sndcard);
}

AudioStream *audio_stream_start_with_sndcards(RtpProfile *prof,int locport,char *remip,int remport,int profile,int jitt_comp,SndCard *playcard, SndCard *captcard)
{
	g_return_val_if_fail(playcard!=NULL,NULL);
	g_return_val_if_fail(captcard!=NULL,NULL);
	return audio_stream_start_full(prof,locport,remip,remport,profile,jitt_comp,NULL,NULL,playcard,captcard);
}

void audio_stream_set_rtcp_information(AudioStream *st, const char *cname){
	if (st->send_session!=NULL){
		rtp_session_set_source_description(st->send_session,cname,NULL,NULL,NULL, NULL,"linphone",
											"This is free software (GPL) !");
	}
}

void audio_stream_stop(AudioStream * stream)
{
	
	ms_stop(stream->timer);
	ortp_global_stats_display();
	ms_sync_detach(stream->timer,stream->soundread);
	ms_sync_detach(stream->timer,stream->rtprecv);
	
	ms_filter_remove_links(stream->soundread,stream->encoder);
	ms_filter_remove_links(stream->encoder,stream->rtpsend);
	ms_filter_remove_links(stream->rtprecv,stream->decoder);
	ms_filter_remove_links(stream->decoder,stream->soundwrite);
	
	audio_stream_free(stream);
}

RingStream * ring_start(gchar *file,gint interval,SndCard *sndcard)
{
   return ring_start_with_cb(file,interval,sndcard,NULL,NULL);
}

RingStream * ring_start_with_cb(gchar *file,gint interval,SndCard *sndcard, MSFilterNotifyFunc func,gpointer user_data)
{
	RingStream *stream;
	int tmp;
	g_return_val_if_fail(sndcard!=NULL,NULL);
	stream=g_new0(RingStream,1);
	stream->source=ms_ring_player_new(file,interval);
	if (stream->source==NULL) {
		g_warning("Could not create ring player. Probably the ring file (%s) does not exist.",file);
		return NULL;
	}
  if (func!=NULL) ms_filter_set_notify_func(MS_FILTER(stream->source),func,user_data);
	stream->sndwrite=snd_card_create_write_filter(sndcard);
	ms_filter_get_property(stream->source,MS_FILTER_PROPERTY_FREQ,&tmp);
	ms_filter_set_property(stream->sndwrite,MS_FILTER_PROPERTY_FREQ,&tmp);
	ms_filter_get_property(stream->source,MS_FILTER_PROPERTY_CHANNELS,&tmp);
	ms_filter_set_property(stream->sndwrite,MS_FILTER_PROPERTY_CHANNELS,&tmp);
	stream->timer=ms_timer_new();
	ms_filter_add_link(stream->source,stream->sndwrite);
	ms_sync_attach(stream->timer,stream->source);
	ms_start(stream->timer);
	return stream;
}

void ring_stop(RingStream *stream)
{
	ms_stop(stream->timer);
	ms_sync_detach(stream->timer,stream->source);
	ms_sync_destroy(stream->timer);
	ms_filter_remove_links(stream->source,stream->sndwrite);
	ms_filter_destroy(stream->source);
	ms_filter_destroy(stream->sndwrite);
	g_free(stream);
}

/* returns the latency in samples if the audio device with id dev_id is openable in full duplex mode, else 0 */
gint test_audio_dev(int dev_id)
{
	gint err;
	SndCard *sndcard=snd_card_manager_get_card(snd_card_manager,dev_id);
	if (sndcard==NULL) return -1;
	err=snd_card_probe(sndcard,16,0,8000);
	return err;  /* return latency in number of sample */
}

gint audio_stream_send_dtmf(AudioStream *stream, gchar dtmf)
{
	ms_rtp_send_dtmf(MS_RTP_SEND(stream->rtpsend), dtmf);
	ms_oss_write_play_dtmf(MS_OSS_WRITE(stream->soundwrite),dtmf);
	return 0;
}