12024
|
1 /*
|
|
2 The mediastreamer library aims at providing modular media processing and I/O
|
|
3 for linphone, but also for any telephony application.
|
|
4 Copyright (C) 2001 Simon MORLAT simon.morlat@linphone.org
|
|
5
|
|
6 This library is free software; you can redistribute it and/or
|
|
7 modify it under the terms of the GNU Lesser General Public
|
|
8 License as published by the Free Software Foundation; either
|
|
9 version 2.1 of the License, or (at your option) any later version.
|
|
10
|
|
11 This library 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 GNU
|
|
14 Lesser General Public License for more details.
|
|
15
|
|
16 You should have received a copy of the GNU Lesser General Public
|
|
17 License along with this library; if not, write to the Free Software
|
|
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
19 */
|
|
20
|
|
21 #include "msrtpsend.h"
|
|
22 #include <ortp/telephonyevents.h>
|
|
23 #include "mssync.h"
|
|
24 #include "mscodec.h"
|
|
25
|
|
26
|
|
27
|
|
28 static MSRtpSendClass *ms_rtp_send_class=NULL;
|
|
29
|
|
30 MSFilter * ms_rtp_send_new(void)
|
|
31 {
|
|
32 MSRtpSend *r;
|
|
33
|
|
34 r=g_new(MSRtpSend,1);
|
|
35
|
|
36 if (ms_rtp_send_class==NULL)
|
|
37 {
|
|
38 ms_rtp_send_class=g_new(MSRtpSendClass,1);
|
|
39 ms_rtp_send_class_init(ms_rtp_send_class);
|
|
40 }
|
|
41 MS_FILTER(r)->klass=MS_FILTER_CLASS(ms_rtp_send_class);
|
|
42 ms_rtp_send_init(r);
|
|
43 return(MS_FILTER(r));
|
|
44 }
|
|
45
|
|
46
|
|
47 void ms_rtp_send_init(MSRtpSend *r)
|
|
48 {
|
|
49 ms_filter_init(MS_FILTER(r));
|
|
50 MS_FILTER(r)->infifos=r->f_inputs;
|
|
51 MS_FILTER(r)->inqueues=r->q_inputs;
|
|
52 MS_FILTER(r)->r_mingran=MSRTPSEND_DEF_GRAN;
|
|
53 memset(r->f_inputs,0,sizeof(MSFifo*)*MSRTPSEND_MAX_INPUTS);
|
|
54 memset(r->q_inputs,0,sizeof(MSFifo*)*MSRTPSEND_MAX_INPUTS);
|
|
55 r->rtpsession=NULL;
|
|
56 r->ts=0;
|
|
57 r->ts_inc=0;
|
|
58 r->flags=0;
|
|
59 r->delay=0;
|
|
60 }
|
|
61
|
|
62 void ms_rtp_send_class_init(MSRtpSendClass *klass)
|
|
63 {
|
|
64 ms_filter_class_init(MS_FILTER_CLASS(klass));
|
|
65 ms_filter_class_set_name(MS_FILTER_CLASS(klass),"RTPSend");
|
|
66 MS_FILTER_CLASS(klass)->max_qinputs=MSRTPSEND_MAX_INPUTS;
|
|
67 MS_FILTER_CLASS(klass)->max_finputs=MSRTPSEND_MAX_INPUTS;
|
|
68 MS_FILTER_CLASS(klass)->r_maxgran=MSRTPSEND_DEF_GRAN;
|
|
69 MS_FILTER_CLASS(klass)->destroy=(MSFilterDestroyFunc)ms_rtp_send_destroy;
|
|
70 MS_FILTER_CLASS(klass)->process=(MSFilterProcessFunc)ms_rtp_send_process;
|
|
71 MS_FILTER_CLASS(klass)->setup=(MSFilterSetupFunc)ms_rtp_send_setup;
|
|
72 }
|
|
73
|
|
74 void ms_rtp_send_set_timing(MSRtpSend *r, guint32 ts_inc, gint payload_size)
|
|
75 {
|
|
76 r->ts_inc=ts_inc;
|
|
77 r->packet_size=payload_size;
|
|
78 if (r->ts_inc!=0) r->flags|=RTPSEND_CONFIGURED;
|
|
79 else r->flags&=~RTPSEND_CONFIGURED;
|
|
80 MS_FILTER(r)->r_mingran=payload_size;
|
|
81 /*g_message("ms_rtp_send_set_timing: ts_inc=%i",ts_inc);*/
|
|
82 }
|
|
83
|
|
84 guint32 get_new_timestamp(MSRtpSend *r,guint32 synctime)
|
|
85 {
|
|
86 guint32 clockts;
|
|
87 /* use the sync system time to compute a timestamp */
|
|
88 PayloadType *pt=rtp_profile_get_payload(r->rtpsession->profile,r->rtpsession->payload_type);
|
|
89 g_return_val_if_fail(pt!=NULL,0);
|
|
90 clockts=(guint32)(((double)synctime * (double)pt->clock_rate)/1000.0);
|
|
91 ms_trace("ms_rtp_send_process: sync->time=%i clock=%i",synctime,clockts);
|
|
92 if (r->flags & RTPSEND_CONFIGURED){
|
|
93 if (RTP_TIMESTAMP_IS_STRICTLY_NEWER_THAN(clockts,r->ts+(2*r->ts_inc) )){
|
|
94 r->ts=clockts;
|
|
95 }
|
|
96 else r->ts+=r->ts_inc;
|
|
97 }else{
|
|
98 r->ts=clockts;
|
|
99 }
|
|
100 return r->ts;
|
|
101 }
|
|
102
|
|
103
|
|
104 void ms_rtp_send_process(MSRtpSend *r)
|
|
105 {
|
|
106 MSFifo *fi;
|
|
107 MSQueue *qi;
|
|
108 MSSync *sync= r->sync;
|
|
109 int gran=ms_sync_get_samples_per_tick(sync);
|
|
110 guint32 ts;
|
|
111 void *s;
|
|
112 guint skip;
|
|
113 guint32 synctime=sync->time;
|
|
114
|
|
115 g_return_if_fail(gran>0);
|
|
116 if (r->rtpsession==NULL) return;
|
|
117
|
|
118 ms_filter_lock(MS_FILTER(r));
|
|
119 skip=r->delay!=0;
|
|
120 if (skip) r->delay--;
|
|
121 /* process output fifo and output queue*/
|
|
122 fi=r->f_inputs[0];
|
|
123 if (fi!=NULL)
|
|
124 {
|
|
125 ts=get_new_timestamp(r,synctime);
|
|
126 /* try to read r->packet_size bytes and send them in a rtp packet*/
|
|
127 ms_fifo_get_read_ptr(fi,r->packet_size,&s);
|
|
128 if (!skip){
|
|
129 rtp_session_send_with_ts(r->rtpsession,s,r->packet_size,ts);
|
|
130 ms_trace("len=%i, ts=%i ",r->packet_size,ts);
|
|
131 }
|
|
132 }
|
|
133 qi=r->q_inputs[0];
|
|
134 if (qi!=NULL)
|
|
135 {
|
|
136 MSMessage *msg;
|
|
137 /* read a MSMessage and send it through the network*/
|
|
138 while ( (msg=ms_queue_get(qi))!=NULL){
|
|
139 ts=get_new_timestamp(r,synctime);
|
|
140 if (!skip) {
|
|
141 /*g_message("Sending packet with ts=%u",ts);*/
|
|
142 rtp_session_send_with_ts(r->rtpsession,msg->data,msg->size,ts);
|
|
143
|
|
144 }
|
|
145 ms_message_destroy(msg);
|
|
146 }
|
|
147 }
|
|
148 ms_filter_unlock(MS_FILTER(r));
|
|
149 }
|
|
150
|
|
151 void ms_rtp_send_destroy( MSRtpSend *obj)
|
|
152 {
|
|
153 g_free(obj);
|
|
154 }
|
|
155
|
|
156 RtpSession * ms_rtp_send_set_session(MSRtpSend *obj,RtpSession *session)
|
|
157 {
|
|
158 RtpSession *old=obj->rtpsession;
|
|
159 obj->rtpsession=session;
|
|
160 obj->ts=0;
|
|
161 obj->ts_inc=0;
|
|
162 return old;
|
|
163 }
|
|
164
|
|
165 void ms_rtp_send_setup(MSRtpSend *r, MSSync *sync)
|
|
166 {
|
|
167 MSFilter *codec;
|
|
168 MSCodecInfo *info;
|
|
169 r->sync=sync;
|
|
170 codec=ms_filter_search_upstream_by_type(MS_FILTER(r),MS_FILTER_AUDIO_CODEC);
|
|
171 if (codec==NULL) codec=ms_filter_search_upstream_by_type(MS_FILTER(r),MS_FILTER_VIDEO_CODEC);
|
|
172 if (codec==NULL){
|
|
173 g_warning("ms_rtp_send_setup: could not find upstream codec.");
|
|
174 return;
|
|
175 }
|
|
176 info=MS_CODEC_INFO(codec->klass->info);
|
|
177 if (info->info.type==MS_FILTER_AUDIO_CODEC){
|
|
178 int ts_inc=info->fr_size/2;
|
|
179 int psize=info->dt_size;
|
|
180 if (ts_inc==0){
|
|
181 /* dont'use the normal frame size: this is a variable frame size codec */
|
|
182 /* use the MS_FILTER(codec)->r_mingran */
|
|
183 ts_inc=MS_FILTER(codec)->r_mingran/2;
|
|
184 psize=0;
|
|
185 }
|
|
186 ms_rtp_send_set_timing(r,ts_inc,psize);
|
|
187 }
|
|
188 }
|
|
189
|
|
190 gint ms_rtp_send_dtmf(MSRtpSend *r, gchar dtmf)
|
|
191 {
|
|
192 gint res;
|
|
193
|
|
194 if (r->rtpsession==NULL) return -1;
|
|
195 if (rtp_session_telephone_events_supported(r->rtpsession)==-1){
|
|
196 g_warning("ERROR : telephone events not supported.\n");
|
|
197 return -1;
|
|
198 }
|
|
199
|
|
200 ms_filter_lock(MS_FILTER(r));
|
|
201 g_message("Sending DTMF.");
|
|
202 res=rtp_session_send_dtmf(r->rtpsession, dtmf, r->ts);
|
|
203 if (res==0){
|
|
204 //r->ts+=r->ts_inc;
|
|
205 r->delay+=2;
|
|
206 }else g_warning("Could not send dtmf.");
|
|
207
|
|
208 ms_filter_unlock(MS_FILTER(r));
|
|
209
|
|
210 return res;
|
|
211 }
|