msvoip.c 7.28 KB
Newer Older
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
/*
mediastreamer2 library - modular sound and video processing and streaming
Copyright (C) 2006  Simon MORLAT (simon.morlat@linphone.org)

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program 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 General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
*/

#ifdef HAVE_CONFIG_H
#include "mediastreamer-config.h"
#include "gitversion.h"
#else
#   ifndef MEDIASTREAMER_VERSION
#   define MEDIASTREAMER_VERSION "unknown"
#   endif
#	ifndef GIT_VERSION
#	define GIT_VERSION "unknown"
#	endif
#endif

#include "mediastreamer2/mscommon.h"
#include "mediastreamer2/mscodecutils.h"
#include "mediastreamer2/msfilter.h"
johan's avatar
johan committed
35 36
#include "mediastreamer2/ms_srtp.h"
#include "private.h"
37

38 39 40 41
#ifdef __cplusplus
extern "C"{
#endif

Simon Morlat's avatar
Simon Morlat committed
42
extern void __register_ffmpeg_encoders_if_possible(MSFactory *factory);
43
extern void __register_ffmpeg_h264_decoder_if_possible(MSFactory *factory);
44
extern void ms_ffmpeg_check_init();
Simon Morlat's avatar
Simon Morlat committed
45 46 47
extern bool_t libmsandroiddisplay_init(MSFactory *factory);
extern void libmsandroiddisplaybad_init(MSFactory *factory);
extern void libmsandroidopengldisplay_init(MSFactory *factory);
48 49 50

#include "voipdescs.h"
#include "mediastreamer2/mssndcard.h"
51
#include "mediastreamer2/msvideopresets.h"
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68
#include "mediastreamer2/mswebcam.h"

#ifdef __APPLE__
   #include "TargetConditionals.h"
#endif

#ifdef ANDROID
#include <android/log.h>
#endif


#ifdef MS2_FILTERS

#ifdef __ALSA_ENABLED__
extern MSSndCardDesc alsa_card_desc;
#endif

69 70 71 72
#ifdef __QSA_ENABLED__
extern MSSndCardDesc ms_qsa_card_desc;
#endif

73 74 75 76 77 78 79 80
#ifdef HAVE_SYS_SOUNDCARD_H
extern MSSndCardDesc oss_card_desc;
#endif

#ifdef __ARTS_ENABLED__
extern MSSndCardDesc arts_card_desc;
#endif

81
#ifdef MS2_WINDOWS_DESKTOP
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
extern MSSndCardDesc winsnd_card_desc;
#endif

#ifdef __DIRECTSOUND_ENABLED__
extern MSSndCardDesc winsndds_card_desc;
#endif

#ifdef __MACSND_ENABLED__
extern MSSndCardDesc ca_card_desc;
#endif

#ifdef __PORTAUDIO_ENABLED__
extern MSSndCardDesc pasnd_card_desc;
#endif

#ifdef __MAC_AQ_ENABLED__
extern MSSndCardDesc aq_card_desc;
#endif

#ifdef __PULSEAUDIO_ENABLED__
extern MSSndCardDesc pulse_card_desc;
#endif

#if TARGET_OS_IPHONE
extern MSSndCardDesc au_card_desc;
#endif

#ifdef ANDROID
extern MSSndCardDesc msandroid_sound_card_desc;
111
extern MSSndCardDesc android_native_snd_card_desc;
112
extern MSSndCardDesc android_native_snd_opensles_card_desc;
113 114 115 116 117 118
#endif

#endif /* MS2_FILTERS */

static MSSndCardDesc * ms_snd_card_descs[]={
#ifdef MS2_FILTERS
François Grisez's avatar
François Grisez committed
119 120 121 122 123

#ifdef __PULSEAUDIO_ENABLED__
        &pulse_card_desc,
#endif

124 125
#ifdef __ALSA_ENABLED__
	&alsa_card_desc,
126
#endif
François Grisez's avatar
François Grisez committed
127 128

#ifdef __QSA_ENABLED__
129
	&ms_qsa_card_desc,
130
#endif
François Grisez's avatar
François Grisez committed
131

132 133 134
#ifdef HAVE_SYS_SOUNDCARD_H
	&oss_card_desc,
#endif
François Grisez's avatar
François Grisez committed
135

136 137 138
#ifdef __ARTS_ENABLED__
	&arts_card_desc,
#endif
François Grisez's avatar
François Grisez committed
139

140
#ifdef MS2_WINDOWS_DESKTOP
141 142
	&winsnd_card_desc,
#endif
François Grisez's avatar
François Grisez committed
143

144 145 146
#ifdef __DIRECTSOUND_ENABLED__
	&winsndds_card_desc,
#endif
François Grisez's avatar
François Grisez committed
147

148 149 150
#ifdef __PORTAUDIO_ENABLED__
	&pasnd_card_desc,
#endif
François Grisez's avatar
François Grisez committed
151

152 153 154 155 156 157 158
#ifdef __MACSND_ENABLED__
	&ca_card_desc,
#endif

#if TARGET_OS_IPHONE
	&au_card_desc,
#endif
François Grisez's avatar
François Grisez committed
159

160 161 162
#ifdef __MAC_AQ_ENABLED__
	&aq_card_desc,
#endif
François Grisez's avatar
François Grisez committed
163

164
#ifdef ANDROID
165
	&android_native_snd_card_desc,
166
	&android_native_snd_opensles_card_desc,
167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184
	&msandroid_sound_card_desc,
#endif
#endif /* MS2_FILTERS */
NULL
};

#ifdef VIDEO_ENABLED

#ifdef MS2_FILTERS

#ifdef HAVE_LINUX_VIDEODEV_H
extern MSWebCamDesc v4l_desc;
#endif

#ifdef HAVE_LINUX_VIDEODEV2_H
extern MSWebCamDesc v4l2_card_desc;
#endif

185
#ifdef _WIN32
186 187 188
extern MSWebCamDesc ms_vfw_cam_desc;
#endif

189
#if defined(_WIN32) && defined(HAVE_DIRECTSHOW)
190 191 192
extern MSWebCamDesc ms_directx_cam_desc;
#endif

193
#if defined(__MINGW32__) || defined(HAVE_DIRECTSHOW)
194 195 196 197 198 199
extern MSWebCamDesc ms_dshow_cam_desc;
#endif

#if TARGET_OS_MAC && !TARGET_OS_IPHONE
extern MSWebCamDesc ms_v4m_cam_desc;
#endif
200

201 202
extern MSWebCamDesc static_image_desc;

203
extern MSWebCamDesc ms_mire_webcam_desc;
204 205 206 207 208 209 210 211
#ifdef ANDROID
extern MSWebCamDesc ms_android_video_capture_desc;
#endif

#if TARGET_OS_IPHONE && !TARGET_IPHONE_SIMULATOR
extern MSWebCamDesc ms_v4ios_cam_desc;
#endif

212 213 214 215
#ifdef __QNX__
extern MSWebCamDesc ms_bb10_camera_desc;
#endif

216 217 218 219
#endif /* MS2_FILTERS */

static MSWebCamDesc * ms_web_cam_descs[]={
#ifdef MS2_FILTERS
220 221 222
#if defined (ANDROID)
	&ms_android_video_capture_desc,
#endif
223 224 225 226 227 228
#ifdef HAVE_LINUX_VIDEODEV2_H
	&v4l2_card_desc,
#endif
#ifdef HAVE_LINUX_VIDEODEV_H
	&v4l_desc,
#endif
229
#if defined(_WIN32) && defined(HAVE_VFW)
230 231 232 233 234 235 236 237
	&ms_vfw_cam_desc,
#endif
#if defined(__MINGW32__) || defined (HAVE_DIRECTSHOW)
	&ms_dshow_cam_desc,
#endif
#if TARGET_OS_MAC && !TARGET_OS_IPHONE
	&ms_v4m_cam_desc,
#endif
238 239 240
#ifdef __QNX__
	&ms_bb10_camera_desc,
#endif
241 242 243
#if TARGET_OS_IPHONE &&  !TARGET_IPHONE_SIMULATOR
	&ms_v4ios_cam_desc,
#endif
244
	&ms_mire_webcam_desc,
245 246 247 248 249 250
	&static_image_desc,
#endif /*MS2_FILTERS */
	NULL
};

#endif
251
static int ms_voip_ref=0;
252
void ms_voip_init(){
253 254 255 256
	if (ms_voip_ref++ >0 ) {
		ms_message ("Skiping ms_voip_init, because [%i] ref",ms_voip_ref);
		return;
	}
johan's avatar
johan committed
257
	ms_srtp_init();
Simon Morlat's avatar
Simon Morlat committed
258 259 260 261 262 263 264 265
	ms_factory_init_voip(ms_factory_get_fallback());
}

static int managers_ref=0;

void ms_factory_init_voip(MSFactory *obj){
	MSSndCardManager *cm;
	int i;
266

267 268
	/* register builtin VoIP MSFilter's */
	for (i=0;ms_voip_filter_descs[i]!=NULL;i++){
Simon Morlat's avatar
Simon Morlat committed
269
		ms_factory_register_filter(obj,ms_voip_filter_descs[i]);
270
	}
271

Simon Morlat's avatar
Simon Morlat committed
272 273 274 275 276 277 278 279 280
	if (managers_ref==0){
		managers_ref++;
		cm=ms_snd_card_manager_get();
		if (cm->descs==NULL){
			ms_message("Registering all soundcard handlers");
			for (i=0;ms_snd_card_descs[i]!=NULL;i++){
				ms_snd_card_manager_register_desc(cm,ms_snd_card_descs[i]);
			}
		}
281 282

#ifdef VIDEO_ENABLED
Simon Morlat's avatar
Simon Morlat committed
283 284 285 286 287 288 289 290 291
		{
			MSWebCamManager *wm;
			wm=ms_web_cam_manager_get();
			if (wm->descs==NULL){
				ms_message("Registering all webcam handlers");
				for (i=0;ms_web_cam_descs[i]!=NULL;i++){
					ms_web_cam_manager_register_desc(wm,ms_web_cam_descs[i]);
				}
			}
292
		}
Simon Morlat's avatar
Simon Morlat committed
293
#endif
294
	}
295

296
#ifdef VIDEO_ENABLED
Ghislain MARY's avatar
Ghislain MARY committed
297 298 299 300
	{
		MSVideoPresetsManager *vpm = ms_video_presets_manager_new(obj);
		register_video_preset_high_fps(vpm);
	}
301 302
#endif

Simon Morlat's avatar
Simon Morlat committed
303
#if defined(VIDEO_ENABLED) && defined(MS2_FILTERS) && !defined(NO_FFMPEG) && defined(HAVE_LIBAVCODEC_AVCODEC_H)
304
	ms_ffmpeg_check_init();
Simon Morlat's avatar
Simon Morlat committed
305
	__register_ffmpeg_encoders_if_possible(obj);
306
	__register_ffmpeg_h264_decoder_if_possible(obj);
307 308 309 310
#endif

#if defined(ANDROID) && defined (VIDEO_ENABLED)
	if (1) {
Simon Morlat's avatar
Simon Morlat committed
311
		libmsandroidopengldisplay_init(obj);
312
	} else {
Simon Morlat's avatar
Simon Morlat committed
313 314
		if (!libmsandroiddisplay_init(obj)) {
			libmsandroiddisplaybad_init(obj);
315 316 317
		}
	}
#endif
Simon Morlat's avatar
Simon Morlat committed
318 319 320
	obj->voip_initd=TRUE;
	ms_message("ms_factory_init_voip() done");
}
321

Simon Morlat's avatar
Simon Morlat committed
322 323
void ms_factory_uninit_voip(MSFactory *obj){
	if (obj->voip_initd){
324 325 326
#ifdef VIDEO_ENABLED
		ms_video_presets_manager_destroy(obj->video_presets_manager);
#endif
Simon Morlat's avatar
Simon Morlat committed
327 328 329 330 331 332 333 334
		managers_ref--;
		if (managers_ref==0){
			ms_snd_card_manager_destroy();
#ifdef VIDEO_ENABLED
			ms_web_cam_manager_destroy();
#endif
		}
	}
335 336 337
}

void ms_voip_exit(){
338 339 340 341
	if (--ms_voip_ref >0 ) {
		ms_message ("Skiping ms_voip_exit, still [%i] ref",ms_voip_ref);
		return;
	}
johan's avatar
johan committed
342
	ms_srtp_shutdown();
Simon Morlat's avatar
Simon Morlat committed
343
	ms_factory_uninit_voip(ms_factory_get_fallback());
344
}
345 346 347 348

#ifdef __cplusplus
}
#endif