factory.c 14.3 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
/*
linphone
Copyright (C) 2016 Belledonne Communications <info@belledonne-communications.com>

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., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
*/

20
#include "linphone/factory.h"
21 22

#include "c-wrapper/c-wrapper.h"
23

24
#ifndef PACKAGE_SOUND_DIR
25
	#define PACKAGE_SOUND_DIR "."
26 27
#endif
#ifndef PACKAGE_RING_DIR
28
	#define PACKAGE_RING_DIR "."
29 30 31
#endif

#ifndef PACKAGE_DATA_DIR
32
	#define PACKAGE_DATA_DIR "."
33 34
#endif

35 36 37 38 39 40
extern LinphoneAddress *_linphone_address_new(const char *addr);

typedef belle_sip_object_t_vptr_t LinphoneFactory_vptr_t;

struct _LinphoneFactory {
	belle_sip_object_t base;
41 42 43

	bctbx_list_t *supported_video_definitions;

Simon Morlat's avatar
Simon Morlat committed
44
	/*these are the directories set by the application*/
45 46 47 48 49 50
	char *top_resources_dir;
	char *data_resources_dir;
	char *sound_resources_dir;
	char *ring_resources_dir;
	char *image_resources_dir;
	char *msplugins_dir;
51

Simon Morlat's avatar
Simon Morlat committed
52 53 54 55 56 57
	/*these are the cached result computed from directories set by the application*/
	char *cached_data_resources_dir;
	char *cached_sound_resources_dir;
	char *cached_ring_resources_dir;
	char *cached_image_resources_dir;
	char *cached_msplugins_dir;
58
	LinphoneErrorInfo* ei;
59 60

	void *user_data;
61 62
};

Simon Morlat's avatar
Simon Morlat committed
63
static void linphone_factory_uninit(LinphoneFactory *obj){
64 65
	bctbx_list_free_with_data(obj->supported_video_definitions, (bctbx_list_free_func)linphone_video_definition_unref);

Simon Morlat's avatar
Simon Morlat committed
66 67 68 69 70 71
	STRING_RESET(obj->top_resources_dir);
	STRING_RESET(obj->data_resources_dir);
	STRING_RESET(obj->sound_resources_dir);
	STRING_RESET(obj->ring_resources_dir);
	STRING_RESET(obj->image_resources_dir);
	STRING_RESET(obj->msplugins_dir);
72

Simon Morlat's avatar
Simon Morlat committed
73 74 75 76 77 78 79
	STRING_RESET(obj->cached_data_resources_dir);
	STRING_RESET(obj->cached_sound_resources_dir);
	STRING_RESET(obj->cached_ring_resources_dir);
	STRING_RESET(obj->cached_image_resources_dir);
	STRING_RESET(obj->cached_msplugins_dir);
}

80 81
BELLE_SIP_DECLARE_NO_IMPLEMENTED_INTERFACES(LinphoneFactory);
BELLE_SIP_INSTANCIATE_VPTR(LinphoneFactory, belle_sip_object_t,
Simon Morlat's avatar
Simon Morlat committed
82
	linphone_factory_uninit, // destroy
83 84 85 86 87 88 89 90 91 92 93 94 95 96
	NULL, // clone
	NULL, // Marshall
	FALSE
);

static LinphoneFactory *_factory = NULL;

static void _linphone_factory_destroying_cb(void) {
	if (_factory != NULL) {
		belle_sip_object_unref(_factory);
		_factory = NULL;
	}
}

97 98
#define ADD_SUPPORTED_VIDEO_DEFINITION(factory, width, height, name) \
	(factory)->supported_video_definitions = bctbx_list_append((factory)->supported_video_definitions, \
99
		linphone_video_definition_new(width, height, name))
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

static void initialize_supported_video_definitions(LinphoneFactory *factory) {
#if !defined(__ANDROID__) && !TARGET_OS_IPHONE
	ADD_SUPPORTED_VIDEO_DEFINITION(factory, MS_VIDEO_SIZE_1080P_W, MS_VIDEO_SIZE_1080P_H, "1080p");
#endif
#if !defined(__ANDROID__) && !TARGET_OS_MAC /*limit to most common sizes because mac video API cannot list supported resolutions*/
	ADD_SUPPORTED_VIDEO_DEFINITION(factory, MS_VIDEO_SIZE_UXGA_W, MS_VIDEO_SIZE_UXGA_H, "uxga");
	ADD_SUPPORTED_VIDEO_DEFINITION(factory, MS_VIDEO_SIZE_SXGA_MINUS_W, MS_VIDEO_SIZE_SXGA_MINUS_H, "sxga-");
#endif
	ADD_SUPPORTED_VIDEO_DEFINITION(factory, MS_VIDEO_SIZE_720P_W, MS_VIDEO_SIZE_720P_H, "720p");
#if !defined(__ANDROID__) && !TARGET_OS_MAC
	ADD_SUPPORTED_VIDEO_DEFINITION(factory, MS_VIDEO_SIZE_XGA_W, MS_VIDEO_SIZE_XGA_H, "xga");
#endif
#if !defined(__ANDROID__) && !TARGET_OS_IPHONE
	ADD_SUPPORTED_VIDEO_DEFINITION(factory, MS_VIDEO_SIZE_SVGA_W, MS_VIDEO_SIZE_SVGA_H, "svga");
	ADD_SUPPORTED_VIDEO_DEFINITION(factory, MS_VIDEO_SIZE_4CIF_W, MS_VIDEO_SIZE_4CIF_H, "4cif");
#endif
	ADD_SUPPORTED_VIDEO_DEFINITION(factory, MS_VIDEO_SIZE_VGA_W, MS_VIDEO_SIZE_VGA_H, "vga");
#if TARGET_OS_IPHONE
	ADD_SUPPORTED_VIDEO_DEFINITION(factory, MS_VIDEO_SIZE_IOS_MEDIUM_H, MS_VIDEO_SIZE_IOS_MEDIUM_W, "ios-medium");
#endif
	ADD_SUPPORTED_VIDEO_DEFINITION(factory, MS_VIDEO_SIZE_CIF_W, MS_VIDEO_SIZE_CIF_H, "cif");
#if !TARGET_OS_MAC || TARGET_OS_IPHONE /* OS_MAC is 1 for iPhone, but we need QVGA */
	ADD_SUPPORTED_VIDEO_DEFINITION(factory, MS_VIDEO_SIZE_QVGA_W, MS_VIDEO_SIZE_QVGA_H, "qvga");
#endif
	ADD_SUPPORTED_VIDEO_DEFINITION(factory, MS_VIDEO_SIZE_QCIF_W, MS_VIDEO_SIZE_QCIF_H, "qcif");
}

Simon Morlat's avatar
Simon Morlat committed
128 129 130
static LinphoneFactory *linphone_factory_new(void){
	LinphoneFactory *factory = belle_sip_object_new(LinphoneFactory);
	factory->top_resources_dir = bctbx_strdup(PACKAGE_DATA_DIR);
131
	initialize_supported_video_definitions(factory);
Simon Morlat's avatar
Simon Morlat committed
132 133 134 135
	return factory;
}


136 137
LinphoneFactory *linphone_factory_get(void) {
	if (_factory == NULL) {
Simon Morlat's avatar
Simon Morlat committed
138
		_factory = linphone_factory_new();
139 140 141 142 143
		atexit(_linphone_factory_destroying_cb);
	}
	return _factory;
}

Simon Morlat's avatar
Simon Morlat committed
144 145 146 147 148 149 150
void linphone_factory_clean(void){
	if (_factory){
		belle_sip_object_unref(_factory);
		_factory = NULL;
	}
}

151 152 153
LinphoneCore *linphone_factory_create_core_2(const LinphoneFactory *factory, LinphoneCoreCbs *cbs,
		const char *config_path, const char *factory_config_path, void *user_data, void *system_context) {
	bctbx_init_logger(FALSE);
154
	LpConfig *config = lp_config_new_with_factory(config_path, factory_config_path);
155
	LinphoneCore *lc = _linphone_core_new_with_config(cbs, config, user_data, system_context);
156 157 158 159
	lp_config_unref(config);
	return lc;
}

160 161 162 163 164
LinphoneCore *linphone_factory_create_core(const LinphoneFactory *factory, LinphoneCoreCbs *cbs,
		const char *config_path, const char *factory_config_path){
	return linphone_factory_create_core_2(factory, cbs, config_path, factory_config_path, NULL, NULL);
}

165
LinphoneCore *linphone_factory_create_core_with_config(const LinphoneFactory *factory, LinphoneCoreCbs *cbs, LinphoneConfig *config) {
166 167 168 169 170
	return _linphone_core_new_with_config(cbs, config, NULL, NULL);
}

LinphoneCore *linphone_factory_create_core_with_config_2(const LinphoneFactory *factory, LinphoneCoreCbs *cbs, LinphoneConfig *config, void *user_data, void *system_context) {
	return _linphone_core_new_with_config(cbs, config, user_data, system_context);
171 172 173 174 175 176 177
}

LinphoneCoreCbs *linphone_factory_create_core_cbs(const LinphoneFactory *factory) {
	return _linphone_core_cbs_new();
}

LinphoneAddress *linphone_factory_create_address(const LinphoneFactory *factory, const char *addr) {
178
	return linphone_address_new(addr);
179 180 181 182 183
}

LinphoneAuthInfo *linphone_factory_create_auth_info(const LinphoneFactory *factory, const char *username, const char *userid, const char *passwd, const char *ha1, const char *realm, const char *domain) {
	return linphone_auth_info_new(username, userid, passwd, ha1, realm, domain);
}
184

185 186 187 188
LinphoneCallCbs * linphone_factory_create_call_cbs(const LinphoneFactory *factory) {
	return _linphone_call_cbs_new();
}

189
LinphoneVcard *linphone_factory_create_vcard(LinphoneFactory *factory) {
Simon Morlat's avatar
Simon Morlat committed
190
	return _linphone_vcard_new();
191
}
192

193
LinphoneVideoDefinition * linphone_factory_create_video_definition(const LinphoneFactory *factory, unsigned int width, unsigned int height) {
194 195
	LinphoneVideoDefinition *supported = linphone_factory_find_supported_video_definition(factory, width, height);
	return supported ? linphone_video_definition_clone(supported) : linphone_video_definition_new(width, height, NULL);
196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216
}

LinphoneVideoDefinition * linphone_factory_create_video_definition_from_name(const LinphoneFactory *factory, const char *name) {
	unsigned int width = 0;
	unsigned int height = 0;
	LinphoneVideoDefinition *vdef = linphone_factory_find_supported_video_definition_by_name(factory, name);
	if (vdef != NULL) return vdef;
	if (sscanf(name, "%ux%u", &width, &height) == 2) {
		return linphone_video_definition_new(width, height, NULL);
	}
	return linphone_video_definition_new(0, 0, NULL);
}

const bctbx_list_t * linphone_factory_get_supported_video_definitions(const LinphoneFactory *factory) {
	return factory->supported_video_definitions;
}

LinphoneVideoDefinition * linphone_factory_find_supported_video_definition(const LinphoneFactory *factory, unsigned int width, unsigned int height) {
	const bctbx_list_t *item;
	const bctbx_list_t *supported = linphone_factory_get_supported_video_definitions(factory);
	LinphoneVideoDefinition *searched_vdef = linphone_video_definition_new(width, height, NULL);
217
	LinphoneVideoDefinition *found = NULL;
218 219 220 221

	for (item = supported; item != NULL; item = bctbx_list_next(item)) {
		LinphoneVideoDefinition *svdef = (LinphoneVideoDefinition *)bctbx_list_get_data(item);
		if (linphone_video_definition_equals(svdef, searched_vdef)) {
222 223
			found = svdef;
			break;
224 225
		}
	}
226 227
	linphone_video_definition_unref(searched_vdef);
	return found;
228 229 230 231 232 233 234 235 236
}

LinphoneVideoDefinition * linphone_factory_find_supported_video_definition_by_name(const LinphoneFactory *factory, const char *name) {
	const bctbx_list_t *item;
	const bctbx_list_t *supported = linphone_factory_get_supported_video_definitions(factory);

	for (item = supported; item != NULL; item = bctbx_list_next(item)) {
		LinphoneVideoDefinition *svdef = (LinphoneVideoDefinition *)bctbx_list_get_data(item);
		if (strcmp(linphone_video_definition_get_name(svdef), name) == 0) {
237
			return svdef;
238 239 240 241 242
		}
	}
	return NULL;
}

Simon Morlat's avatar
Simon Morlat committed
243 244
const char * linphone_factory_get_top_resources_dir(const LinphoneFactory *factory) {
	return factory->top_resources_dir;
245 246 247
}

void linphone_factory_set_top_resources_dir(LinphoneFactory *factory, const char *path) {
Simon Morlat's avatar
Simon Morlat committed
248
	STRING_SET(factory->top_resources_dir, path);
249 250
}

Simon Morlat's avatar
Simon Morlat committed
251 252 253 254 255 256 257 258
const char * linphone_factory_get_data_resources_dir(LinphoneFactory *factory) {
	if (factory->data_resources_dir) return factory->data_resources_dir;
	if (factory->top_resources_dir){
		STRING_TRANSFER(factory->cached_data_resources_dir, bctbx_strdup_printf("%s/linphone", factory->top_resources_dir));
	}else{
		STRING_TRANSFER(factory->cached_data_resources_dir, bctbx_strdup_printf("%s/linphone", PACKAGE_DATA_DIR));
	}
	return factory->cached_data_resources_dir;
259 260 261
}

void linphone_factory_set_data_resources_dir(LinphoneFactory *factory, const char *path) {
Simon Morlat's avatar
Simon Morlat committed
262
	STRING_SET(factory->data_resources_dir, path);
263 264
}

Simon Morlat's avatar
Simon Morlat committed
265 266 267 268 269 270 271
const char * linphone_factory_get_sound_resources_dir(LinphoneFactory *factory) {
	if (factory->sound_resources_dir) return factory->sound_resources_dir;
	if (factory->top_resources_dir){
		STRING_TRANSFER(factory->cached_sound_resources_dir, bctbx_strdup_printf("%s/sounds/linphone", factory->top_resources_dir));
		return factory->cached_sound_resources_dir;
	}
	return PACKAGE_SOUND_DIR;
272 273 274
}

void linphone_factory_set_sound_resources_dir(LinphoneFactory *factory, const char *path) {
Simon Morlat's avatar
Simon Morlat committed
275
	STRING_SET(factory->sound_resources_dir, path);
276 277
}

Simon Morlat's avatar
Simon Morlat committed
278 279 280
const char * linphone_factory_get_ring_resources_dir(LinphoneFactory *factory) {
	if (factory->ring_resources_dir) return factory->ring_resources_dir;
	if (factory->sound_resources_dir){
281 282 283 284 285 286
		STRING_TRANSFER(factory->cached_ring_resources_dir, bctbx_strdup_printf("%s/rings", factory->sound_resources_dir));
		return factory->cached_ring_resources_dir;
	}
	if (factory->top_resources_dir) {
		STRING_TRANSFER(factory->cached_ring_resources_dir, bctbx_strdup_printf("%s/sounds/linphone/rings", factory->top_resources_dir));
		return factory->cached_ring_resources_dir;
Simon Morlat's avatar
Simon Morlat committed
287 288
	}
	return PACKAGE_RING_DIR;
289 290 291
}

void linphone_factory_set_ring_resources_dir(LinphoneFactory *factory, const char *path) {
Simon Morlat's avatar
Simon Morlat committed
292
	STRING_SET(factory->ring_resources_dir, path);
293 294
}

Simon Morlat's avatar
Simon Morlat committed
295 296 297 298 299 300 301 302
const char * linphone_factory_get_image_resources_dir(LinphoneFactory *factory) {
	if (factory->image_resources_dir) return factory->image_resources_dir;
	if (factory->top_resources_dir) {
		STRING_TRANSFER(factory->cached_image_resources_dir, bctbx_strdup_printf("%s/images", factory->top_resources_dir));
	}else{
		STRING_TRANSFER(factory->cached_image_resources_dir, bctbx_strdup_printf("%s/images", PACKAGE_DATA_DIR));
	}
	return factory->cached_image_resources_dir;
303 304 305
}

void linphone_factory_set_image_resources_dir(LinphoneFactory *factory, const char *path) {
Simon Morlat's avatar
Simon Morlat committed
306
	STRING_SET(factory->image_resources_dir, path);
307 308
}

Simon Morlat's avatar
Simon Morlat committed
309 310
const char * linphone_factory_get_msplugins_dir(LinphoneFactory *factory) {
	return factory->msplugins_dir;
311 312 313
}

void linphone_factory_set_msplugins_dir(LinphoneFactory *factory, const char *path) {
Simon Morlat's avatar
Simon Morlat committed
314
	STRING_SET(factory->msplugins_dir, path);
315
}
316

317
LinphoneErrorInfo *linphone_factory_create_error_info(LinphoneFactory *factory){
318

319
	return linphone_error_info_new();
320

321
}
322 323 324

LinphoneRange *linphone_factory_create_range(LinphoneFactory *factory) {
	return linphone_range_new();
325 326 327 328 329 330 331 332
}

LinphoneTransports *linphone_factory_create_transports(LinphoneFactory *factory) {
	return linphone_transports_new();
}

LinphoneVideoActivationPolicy *linphone_factory_create_video_activation_policy(LinphoneFactory *factory) {
	return linphone_video_activation_policy_new();
333
}
334

335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362
LinphoneContent *linphone_factory_create_content(LinphoneFactory *factory) {
	return linphone_content_new();
}

LinphoneBuffer *linphone_factory_create_buffer(LinphoneFactory *factory) {
	return linphone_buffer_new();
}

LinphoneBuffer *linphone_factory_create_buffer_from_data(LinphoneFactory *factory, const uint8_t *data, size_t size) {
	return linphone_buffer_new_from_data(data, size);
}

LinphoneBuffer *linphone_factory_create_buffer_from_string(LinphoneFactory *factory, const char *data) {
	return linphone_buffer_new_from_string(data);
}

LinphoneConfig *linphone_factory_create_config(LinphoneFactory *factory, const char *path) {
	return linphone_config_new(path);
}

LinphoneConfig *linphone_factory_create_config_with_factory(LinphoneFactory *factory, const char *path, const char *factory_path) {
	return linphone_config_new_with_factory(path, factory_path);
}

LinphoneConfig *linphone_factory_create_config_from_string(LinphoneFactory *factory, const char *data) {
	return linphone_config_new_from_buffer(data);
}

Sylvain Berfini's avatar
Sylvain Berfini committed
363 364 365 366
const bctbx_list_t * linphone_factory_get_dial_plans(const LinphoneFactory *factory) {
	return linphone_dial_plan_get_all_list();
}

367 368 369 370 371 372
void *linphone_factory_get_user_data(const LinphoneFactory *factory) {
	return factory->user_data;
}

void linphone_factory_set_user_data(LinphoneFactory *factory, void *data) {
	factory->user_data = data;
373 374
}

375
void linphone_factory_set_log_collection_path(LinphoneFactory *factory, const char *path) {
376 377 378 379 380
	linphone_core_set_log_collection_path(path);
}

void linphone_factory_enable_log_collection(LinphoneFactory *factory, LinphoneLogCollectionState state) {
	linphone_core_enable_log_collection(state);
381
}