summaryrefslogtreecommitdiffstats
path: root/kopete/protocols/jabber/jingle/libjingle/talk/third_party/mediastreamer/msrtpsend.c
blob: 39ed1461a5a0e43e3d74fc47c9bf517282507b39 (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
/*
  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  02111-1307  USA
*/

#include "msrtpsend.h"
#include <ortp/telephonyevents.h>
#include "mssync.h"
#include "mscodec.h"



static MSRtpSendClass *ms_rtp_send_class=NULL;

MSFilter * ms_rtp_send_new(void)
{
	MSRtpSend *r;
	
	r=g_new(MSRtpSend,1);
	
	if (ms_rtp_send_class==NULL)
	{
		ms_rtp_send_class=g_new(MSRtpSendClass,1);
		ms_rtp_send_class_init(ms_rtp_send_class);
	}
	MS_FILTER(r)->klass=MS_FILTER_CLASS(ms_rtp_send_class);
	ms_rtp_send_init(r);
	return(MS_FILTER(r));
}
	

void ms_rtp_send_init(MSRtpSend *r)
{
	ms_filter_init(MS_FILTER(r));
	MS_FILTER(r)->infifos=r->f_inputs;
	MS_FILTER(r)->inqueues=r->q_inputs;
	MS_FILTER(r)->r_mingran=MSRTPSEND_DEF_GRAN;
	memset(r->f_inputs,0,sizeof(MSFifo*)*MSRTPSEND_MAX_INPUTS);
	memset(r->q_inputs,0,sizeof(MSFifo*)*MSRTPSEND_MAX_INPUTS);
	r->rtpsession=NULL;
	r->ts=0;
	r->ts_inc=0;
	r->flags=0;
	r->delay=0;
}

void ms_rtp_send_class_init(MSRtpSendClass *klass)
{
	ms_filter_class_init(MS_FILTER_CLASS(klass));
	ms_filter_class_set_name(MS_FILTER_CLASS(klass),"RTPSend");
	MS_FILTER_CLASS(klass)->max_qinputs=MSRTPSEND_MAX_INPUTS;
	MS_FILTER_CLASS(klass)->max_finputs=MSRTPSEND_MAX_INPUTS;
	MS_FILTER_CLASS(klass)->r_maxgran=MSRTPSEND_DEF_GRAN;
	MS_FILTER_CLASS(klass)->destroy=(MSFilterDestroyFunc)ms_rtp_send_destroy;
	MS_FILTER_CLASS(klass)->process=(MSFilterProcessFunc)ms_rtp_send_process;
	MS_FILTER_CLASS(klass)->setup=(MSFilterSetupFunc)ms_rtp_send_setup;
}

void ms_rtp_send_set_timing(MSRtpSend *r, guint32 ts_inc, gint payload_size)
{
	r->ts_inc=ts_inc;
	r->packet_size=payload_size;
	if (r->ts_inc!=0) r->flags|=RTPSEND_CONFIGURED;
	else r->flags&=~RTPSEND_CONFIGURED;
	MS_FILTER(r)->r_mingran=payload_size;	
	/*g_message("ms_rtp_send_set_timing: ts_inc=%i",ts_inc);*/
}

guint32 get_new_timestamp(MSRtpSend *r,guint32 synctime)
{
	guint32 clockts;
	/* use the sync system time to compute a timestamp */
	PayloadType *pt=rtp_profile_get_payload(r->rtpsession->profile,r->rtpsession->payload_type);
	g_return_val_if_fail(pt!=NULL,0);
	clockts=(guint32)(((double)synctime * (double)pt->clock_rate)/1000.0);
	ms_trace("ms_rtp_send_process: sync->time=%i clock=%i",synctime,clockts);
	if (r->flags & RTPSEND_CONFIGURED){
		if (RTP_TIMESTAMP_IS_STRICTLY_NEWER_THAN(clockts,r->ts+(2*r->ts_inc) )){
			r->ts=clockts;
		}
		else r->ts+=r->ts_inc;
	}else{
		r->ts=clockts;
	}
	return r->ts;
}


void ms_rtp_send_process(MSRtpSend *r)
{
	MSFifo *fi;
	MSQueue *qi;
	MSSync *sync= r->sync;
	int gran=ms_sync_get_samples_per_tick(sync);
	guint32 ts;
	void *s;
	guint skip;
	guint32 synctime=sync->time;
	
	g_return_if_fail(gran>0);
	if (r->rtpsession==NULL) return;

	ms_filter_lock(MS_FILTER(r));
	skip=r->delay!=0;
	if (skip) r->delay--;
	/* process output fifo and output queue*/
	fi=r->f_inputs[0];
	if (fi!=NULL)
	{
		ts=get_new_timestamp(r,synctime);
		/* try to read r->packet_size bytes and send them in a rtp packet*/
		ms_fifo_get_read_ptr(fi,r->packet_size,&s);
		if (!skip){
			rtp_session_send_with_ts(r->rtpsession,s,r->packet_size,ts);
			ms_trace("len=%i, ts=%i ",r->packet_size,ts);
		}
	}
	qi=r->q_inputs[0];
	if (qi!=NULL)
	{
		MSMessage *msg;
		/* read a MSMessage and send it through the network*/
		while ( (msg=ms_queue_get(qi))!=NULL){
			ts=get_new_timestamp(r,synctime);
			if (!skip) {
				/*g_message("Sending packet with ts=%u",ts);*/
				rtp_session_send_with_ts(r->rtpsession,msg->data,msg->size,ts);
				
			}
			ms_message_destroy(msg);
		}
	}
	ms_filter_unlock(MS_FILTER(r));
}

void ms_rtp_send_destroy( MSRtpSend *obj)
{
	g_free(obj);
}

RtpSession * ms_rtp_send_set_session(MSRtpSend *obj,RtpSession *session)
{
	RtpSession *old=obj->rtpsession;
	obj->rtpsession=session;
	obj->ts=0;
	obj->ts_inc=0;
	return old;
}

void ms_rtp_send_setup(MSRtpSend *r, MSSync *sync)
{
	MSFilter *codec;
	MSCodecInfo *info;
	r->sync=sync;
	codec=ms_filter_search_upstream_by_type(MS_FILTER(r),MS_FILTER_AUDIO_CODEC);
	if (codec==NULL) codec=ms_filter_search_upstream_by_type(MS_FILTER(r),MS_FILTER_VIDEO_CODEC);
	if (codec==NULL){
		g_warning("ms_rtp_send_setup: could not find upstream codec.");
		return;
	}
	info=MS_CODEC_INFO(codec->klass->info);
	if (info->info.type==MS_FILTER_AUDIO_CODEC){
		int ts_inc=info->fr_size/2;
		int psize=info->dt_size;
		if (ts_inc==0){
			/* dont'use the normal frame size: this is a variable frame size codec */
			/* use the MS_FILTER(codec)->r_mingran */
			ts_inc=MS_FILTER(codec)->r_mingran/2;
			psize=0;
		}
		ms_rtp_send_set_timing(r,ts_inc,psize);
	}
}

gint ms_rtp_send_dtmf(MSRtpSend *r, gchar dtmf)
{
	gint res;

	if (r->rtpsession==NULL) return -1;
	if (rtp_session_telephone_events_supported(r->rtpsession)==-1){
		g_warning("ERROR : telephone events not supported.\n");
 		return -1;
	}

	ms_filter_lock(MS_FILTER(r));
	g_message("Sending DTMF.");
	res=rtp_session_send_dtmf(r->rtpsession, dtmf, r->ts);
	if (res==0){
		/* //r->ts+=r->ts_inc; */
		r->delay+=2;
	}else g_warning("Could not send dtmf.");

	ms_filter_unlock(MS_FILTER(r));

	return res;
}