summaryrefslogtreecommitdiffstats
path: root/kopete/protocols/jabber/jingle/libjingle/talk/third_party/mediastreamer/mscodec.c
blob: 7fd3fac3f0cba393a5d7b79851cd2211b0bac651 (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
/*
  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 "mscodec.h"
#include "msMUlawdec.h"

#ifdef TRUESPEECH
extern MSCodecInfo TrueSpeechinfo;
#endif

#ifdef VIDEO_ENABLED
extern void ms_AVCodec_init();
#endif

#define UDP_HDR_SZ 8
#define RTP_HDR_SZ 12
#define IP4_HDR_SZ 20   /*20 is the minimum, but there may be some options*/




/* register all statically linked codecs */
void ms_codec_register_all()
{
/*//	ms_filter_register(MS_FILTER_INFO(&GSMinfo));
	//	ms_filter_register(MS_FILTER_INFO(&LPC10info));*/
	ms_filter_register(MS_FILTER_INFO(&MULAWinfo));
#ifdef TRUESPEECH
        ms_filter_register(MS_FILTER_INFO(&TrueSpeechinfo));
#endif
#ifdef VIDEO_ENABLED
	ms_AVCodec_init();
#endif
	
}

/* returns a list of MSCodecInfo */
GList * ms_codec_get_all_audio()
{
	GList *audio_codecs=NULL;
	GList *elem=filter_list;
	MSFilterInfo *info;
	while (elem!=NULL)
	{
		info=(MSFilterInfo *)elem->data;
		if (info->type==MS_FILTER_AUDIO_CODEC){
			audio_codecs=g_list_append(audio_codecs,info);
		}
		elem=g_list_next(elem);
	}
	return audio_codecs;
}


MSCodecInfo * ms_audio_codec_info_get(gchar *name)
{
	GList *elem=filter_list;
	MSFilterInfo *info;
	while (elem!=NULL)
	{
		info=(MSFilterInfo *)elem->data;
		if ( (info->type==MS_FILTER_AUDIO_CODEC) ){
			MSCodecInfo *codinfo=(MSCodecInfo *)info;
			if (strcmp(codinfo->description,name)==0){
				return MS_CODEC_INFO(info);
			}
		}
		elem=g_list_next(elem);
	}
	return NULL;
}

MSCodecInfo * ms_video_codec_info_get(gchar *name)
{
	GList *elem=filter_list;
	MSFilterInfo *info;
	while (elem!=NULL)
	{
		info=(MSFilterInfo *)elem->data;
		if ( (info->type==MS_FILTER_VIDEO_CODEC) ){
			MSCodecInfo *codinfo=(MSCodecInfo *)info;
			if (strcmp(codinfo->description,name)==0){
				return MS_CODEC_INFO(info);
			}
		}
		elem=g_list_next(elem);
	}
	return NULL;
}

/* returns a list of MSCodecInfo */
GList * ms_codec_get_all_video()
{
	GList *video_codecs=NULL;
	GList *elem=filter_list;
	MSFilterInfo *info;
	while (elem!=NULL)
	{
		info=(MSFilterInfo *)elem->data;
		if (info->type==MS_FILTER_VIDEO_CODEC){
			video_codecs=g_list_append(video_codecs,info);
		}
		elem=g_list_next(elem);
	}
	return video_codecs;
}

MSFilter * ms_encoder_new(gchar *name)
{
	GList *elem=filter_list;
	MSFilterInfo *info;
	while (elem!=NULL)
	{
		info=(MSFilterInfo *)elem->data;
		if ((info->type==MS_FILTER_AUDIO_CODEC) || (info->type==MS_FILTER_VIDEO_CODEC)){
			MSCodecInfo *codinfo=(MSCodecInfo *)elem->data;
			if (strcmp(info->name,name)==0){
				return codinfo->encoder();
			}
		}
		elem=g_list_next(elem);
	}
	return NULL;
}

MSFilter * ms_decoder_new(gchar *name)
{
	GList *elem=filter_list;
	MSFilterInfo *info;
	while (elem!=NULL)
	{
		info=(MSFilterInfo *)elem->data;
		if ((info->type==MS_FILTER_AUDIO_CODEC) || (info->type==MS_FILTER_VIDEO_CODEC)){
			MSCodecInfo *codinfo=(MSCodecInfo *)elem->data;
			if (strcmp(info->name,name)==0){
				return codinfo->decoder();
			}
		}
		elem=g_list_next(elem);
	}
	return NULL;
}

MSFilter * ms_encoder_new_with_pt(gint pt)
{
	GList *elem=filter_list;
	MSFilterInfo *info;
	while (elem!=NULL)
	{
		info=(MSFilterInfo *)elem->data;
		if ((info->type==MS_FILTER_AUDIO_CODEC) || (info->type==MS_FILTER_VIDEO_CODEC)){
			MSCodecInfo *codinfo=(MSCodecInfo *)elem->data;
			if (codinfo->pt==pt){
				return codinfo->encoder();
			}
		}
		elem=g_list_next(elem);
	}
	return NULL;
}

MSFilter * ms_decoder_new_with_pt(gint pt)
{
	GList *elem=filter_list;
	MSFilterInfo *info;
	while (elem!=NULL)
	{
		info=(MSFilterInfo *)elem->data;
		if ((info->type==MS_FILTER_AUDIO_CODEC) || (info->type==MS_FILTER_VIDEO_CODEC)){
			MSCodecInfo *codinfo=(MSCodecInfo *)elem->data;
			if (codinfo->pt==pt){
				return codinfo->decoder();
			}
		}
		elem=g_list_next(elem);
	}
	return NULL;
}

MSFilter * ms_decoder_new_with_string_id(gchar *id)
{
	GList *elem=filter_list;
	MSFilterInfo *info;
	while (elem!=NULL)
	{
		info=(MSFilterInfo *)elem->data;
		if ((info->type==MS_FILTER_AUDIO_CODEC) || (info->type==MS_FILTER_VIDEO_CODEC)){
			MSCodecInfo *codinfo=(MSCodecInfo *)elem->data;
			if (strcasecmp(codinfo->description,id)==0){
				return codinfo->decoder();
			}
		}
		elem=g_list_next(elem);
	}
	return NULL;
}

MSFilter * ms_encoder_new_with_string_id(gchar *id)
{
	GList *elem=filter_list;
	MSFilterInfo *info;
	while (elem!=NULL)
	{
		info=(MSFilterInfo *)elem->data;
		if ((info->type==MS_FILTER_AUDIO_CODEC) || (info->type==MS_FILTER_VIDEO_CODEC)){
			MSCodecInfo *codinfo=(MSCodecInfo *)elem->data;
			if (strcasecmp(codinfo->description,id)==0){
				return codinfo->encoder();
			}
		}
		elem=g_list_next(elem);
	}
	return NULL;
}
/* return 0 if codec can be used with bandwidth, -1 else*/
int ms_codec_is_usable(MSCodecInfo *codec,double bandwidth)
{
	double codec_band;
	double npacket;
	double packet_size;
	
	if (((MSFilterInfo*)codec)->type==MS_FILTER_AUDIO_CODEC)
	{
		/* calculate the total bandwdith needed by codec (including headers for rtp, udp, ip)*/		
		/* number of packet per second*/
		npacket=2.0*(double)(codec->rate)/(double)(codec->fr_size);
		packet_size=(double)(codec->dt_size)+UDP_HDR_SZ+RTP_HDR_SZ+IP4_HDR_SZ;
		codec_band=packet_size*8.0*npacket;
	}
	else return -1;
	return(codec_band<bandwidth);
}