mscommon.h 9.6 KB
Newer Older
aymeric's avatar
aymeric committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
/*
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.
*/
#ifndef mscommon_h
#define mscommon_h

22 23 24
#include <ortp/logging.h>
#include <ortp/port.h>
#include <ortp/str_utils.h>
Ghislain MARY's avatar
Ghislain MARY committed
25
#include <ortp/payloadtype.h>
26
#include <time.h>
27
#if defined(__APPLE__)
jehan's avatar
jehan committed
28 29
#include "TargetConditionals.h"
#endif
aymeric's avatar
aymeric committed
30

31
#define MS_UNUSED(x) ((void)(x))
Ghislain MARY's avatar
Ghislain MARY committed
32

aymeric's avatar
aymeric committed
33 34 35 36 37 38 39
#define ms_malloc	ortp_malloc
#define ms_malloc0	ortp_malloc0
#define ms_realloc	ortp_realloc
#define ms_new		ortp_new
#define ms_new0		ortp_new0
#define ms_free		ortp_free
#define ms_strdup	ortp_strdup
jehan's avatar
jehan committed
40
#define ms_strndup	ortp_strndup
aymeric's avatar
aymeric committed
41
#define ms_strdup_printf	ortp_strdup_printf
42
#define ms_strcat_printf	ortp_strcat_printf
aymeric's avatar
aymeric committed
43 44 45 46 47 48 49 50 51 52 53 54 55 56

#define ms_mutex_t		ortp_mutex_t
#define ms_mutex_init		ortp_mutex_init
#define ms_mutex_destroy	ortp_mutex_destroy
#define ms_mutex_lock		ortp_mutex_lock
#define ms_mutex_unlock		ortp_mutex_unlock

#define ms_cond_t		ortp_cond_t
#define ms_cond_init		ortp_cond_init
#define ms_cond_wait		ortp_cond_wait
#define ms_cond_signal		ortp_cond_signal
#define ms_cond_broadcast	ortp_cond_broadcast
#define ms_cond_destroy		ortp_cond_destroy

57 58
#define MS2_INLINE ORTP_INLINE

Simon Morlat's avatar
Simon Morlat committed
59 60 61 62 63 64
#if defined(_MSC_VER)
#define MS2_PUBLIC	__declspec(dllexport)
#else
#define MS2_PUBLIC
#endif

65 66 67 68 69 70
#if defined(_WIN32_WCE)
time_t ms_time (time_t *t);
#else
#define ms_time time
#endif

aymeric's avatar
aymeric committed
71
#ifdef DEBUG
72
static MS2_INLINE void ms_debug(const char *fmt,...)
aymeric's avatar
aymeric committed
73 74 75 76 77 78 79
{
  va_list args;
  va_start (args, fmt);
  ortp_logv(ORTP_DEBUG, fmt, args);
  va_end (args);
}
#else
80
#define ms_debug(fmt, ...)
aymeric's avatar
aymeric committed
81 82 83 84 85 86 87 88
#endif

#define ms_message	ortp_message
#define ms_warning	ortp_warning
#define ms_error	ortp_error
#define ms_fatal	ortp_fatal

#define ms_return_val_if_fail(_expr_,_ret_)\
89
	if (!(_expr_)) { ms_fatal("assert "#_expr_ "failed"); return (_ret_);}
aymeric's avatar
aymeric committed
90 91

#define ms_return_if_fail(_expr_) \
92
	if (!(_expr_)){ ms_fatal("assert "#_expr_ "failed"); return ;}
aymeric's avatar
aymeric committed
93 94 95 96

#define ms_thread_t		ortp_thread_t
#define ms_thread_create 	ortp_thread_create
#define ms_thread_join		ortp_thread_join
97

98 99 100
typedef ortpTimeSpec MSTimeSpec;

#define ms_get_cur_time ortp_get_cur_time
Ghislain MARY's avatar
Ghislain MARY committed
101
#define ms_get_cur_time_ms ortp_get_cur_time_ms
aymeric's avatar
aymeric committed
102 103 104 105 106 107 108 109 110 111 112 113 114

struct _MSList {
	struct _MSList *next;
	struct _MSList *prev;
	void *data;
};

typedef struct _MSList MSList;


#define ms_list_next(elem) ((elem)->next)


115
typedef int (*MSCompareFunc)(const void *a, const void *b);
116 117
typedef void (*MSIterateFunc)(void *a);
typedef void (*MSIterate2Func)(void *a, void *b);
118

aymeric's avatar
aymeric committed
119 120 121
#ifdef __cplusplus
extern "C"{
#endif
122 123
/*for stun*/
typedef struct { unsigned char octet[12]; }  UInt96;
aymeric's avatar
aymeric committed
124

Sylvain Berfini's avatar
Sylvain Berfini committed
125
MS2_PUBLIC void ms_thread_exit(void* ret_val);
Simon Morlat's avatar
Simon Morlat committed
126
MS2_PUBLIC MSList * ms_list_append(MSList *elem, void * data);
Simon Morlat's avatar
Simon Morlat committed
127
MS2_PUBLIC MSList *ms_list_append_link(MSList *elem, MSList *new_elem);
Simon Morlat's avatar
Simon Morlat committed
128 129
MS2_PUBLIC MSList * ms_list_prepend(MSList *elem, void * data);
MS2_PUBLIC MSList * ms_list_free(MSList *elem);
130
MS2_PUBLIC MSList * ms_list_free_with_data(MSList *elem, void (*freefunc)(void*));
Simon Morlat's avatar
Simon Morlat committed
131 132
MS2_PUBLIC MSList * ms_list_concat(MSList *first, MSList *second);
MS2_PUBLIC MSList * ms_list_remove(MSList *first, void *data);
133
MS2_PUBLIC MSList * ms_list_remove_custom(MSList *first, MSCompareFunc compare_func, const void *user_data);
Simon Morlat's avatar
Simon Morlat committed
134
MS2_PUBLIC int ms_list_size(const MSList *first);
135 136
MS2_PUBLIC void ms_list_for_each(const MSList *list, MSIterateFunc iterate_func);
MS2_PUBLIC void ms_list_for_each2(const MSList *list, MSIterate2Func iterate_func, void *user_data);
Simon Morlat's avatar
Simon Morlat committed
137 138 139 140 141 142 143 144 145
MS2_PUBLIC MSList *ms_list_remove_link(MSList *list, MSList *elem);
MS2_PUBLIC MSList *ms_list_find(MSList *list, void *data);
MS2_PUBLIC MSList *ms_list_find_custom(MSList *list, MSCompareFunc compare_func, const void *user_data);
MS2_PUBLIC void * ms_list_nth_data(const MSList *list, int index);
MS2_PUBLIC int ms_list_position(const MSList *list, MSList *elem);
MS2_PUBLIC int ms_list_index(const MSList *list, void *data);
MS2_PUBLIC MSList *ms_list_insert_sorted(MSList *list, void *data, MSCompareFunc compare_func);
MS2_PUBLIC MSList *ms_list_insert(MSList *list, MSList *before, void *data);
MS2_PUBLIC MSList *ms_list_copy(const MSList *list);
Simon Morlat's avatar
Simon Morlat committed
146
MS2_PUBLIC MSList *ms_list_copy_with_data(const MSList *list, void *(*copyfunc)(void *));
aymeric's avatar
aymeric committed
147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162

#undef MIN
#define MIN(a,b)	((a)>(b) ? (b) : (a))
#undef MAX
#define MAX(a,b)	((a)>(b) ? (a) : (b))

/**
 * @file mscommon.h
 * @brief mediastreamer2 mscommon.h include file
 *
 * This file provide the API needed to initialize
 * and reset the mediastreamer2 library.
 *
 */

/**
163
 * @addtogroup mediastreamer2_init
aymeric's avatar
aymeric committed
164 165 166 167 168
 * @{
 */


/**
169 170 171
 * Helper macro for backward compatibility.
 * Use ms_base_init() and ms_voip_init() instead.
 */
172
#define ms_init()	ms_base_init(), ms_voip_init(), ms_plugins_init()
173 174 175 176 177

/**
 * Helper macro for backward compatibility.
 * Use ms_base_exit() and ms_voip_exit() instead.
 */
Sylvain Berfini's avatar
Sylvain Berfini committed
178
#define ms_exit()	ms_voip_exit(), ms_plugins_exit(), ms_base_exit()
179 180 181

/**
 * Initialize the mediastreamer2 base library.
aymeric's avatar
aymeric committed
182 183 184
 *
 * This must be called once before calling any other API.
 */
185 186 187 188 189 190 191 192
MS2_PUBLIC void ms_base_init(void);

/**
 * Initialize the mediastreamer2 VoIP library.
 *
 * This must be called one before calling any other API.
 */
MS2_PUBLIC void ms_voip_init(void);
aymeric's avatar
aymeric committed
193

194 195 196 197 198 199 200 201
/**
 * Load the plugins from the default plugin directory.
 *
 * This is just a wrapper around ms_load_plugins().
 * This must be called after ms_base_init() and after ms_voip_init().
 */
MS2_PUBLIC void ms_plugins_init(void);

202 203 204 205 206 207
/**
 * Set the directory from where the plugins are to be loaded when calling ms_plugins_init().
 * @param[in] path The path to the plugins directory.
 */
MS2_PUBLIC void ms_set_plugins_dir(const char *path);

aymeric's avatar
aymeric committed
208 209
/**
 * Load plugins from a specific directory.
smorlat's avatar
smorlat committed
210 211 212 213
 * This method basically loads all libraries in the specified directory and attempts to call a C function called
 * \<libraryname\>_init. For example if a library 'libdummy.so' or 'libdummy.dll' is found, then the loader tries to locate
 * a C function called 'libdummy_init()' and calls it if it exists.
 * ms_load_plugins() can be used to load non-mediastreamer2 plugins as it does not expect mediastreamer2 specific entry points.
aymeric's avatar
aymeric committed
214 215 216 217 218
 *
 * @param directory   A directory where plugins library are available.
 *
 * Returns: >0 if successfull, 0 if not plugins loaded, -1 otherwise.
 */
Simon Morlat's avatar
Simon Morlat committed
219
MS2_PUBLIC int ms_load_plugins(const char *directory);
aymeric's avatar
aymeric committed
220 221

/**
222 223 224 225 226 227 228 229
 * Release resource allocated in the mediastreamer2 base library.
 *
 * This must be called once before closing program.
 */
MS2_PUBLIC void ms_base_exit(void);

/**
 * Release resource allocated in the mediastreamer2 VoIP library.
aymeric's avatar
aymeric committed
230 231 232
 *
 * This must be called once before closing program.
 */
233
MS2_PUBLIC void ms_voip_exit(void);
aymeric's avatar
aymeric committed
234

235 236 237 238 239
/**
 * Unload the plugins loaded by ms_plugins_init().
 */
MS2_PUBLIC void ms_plugins_exit(void);

aymeric's avatar
aymeric committed
240 241
struct _MSSndCardDesc;

Simon Morlat's avatar
Simon Morlat committed
242
MS2_PUBLIC void ms_sleep(int seconds);
aymeric's avatar
aymeric committed
243

Simon Morlat's avatar
Simon Morlat committed
244
MS2_PUBLIC void ms_usleep(uint64_t usec);
245

aymeric's avatar
aymeric committed
246 247 248 249
/**
 * The max payload size allowed.
 * Filters that generate data that can be sent through RTP should make packets
 * whose size is below ms_get_payload_max_size().
jehan's avatar
jehan committed
250
 * The default value is 1440 computed as the standard internet MTU minus IPv6 header,
251
 * UDP header and RTP header. As IPV4 header is smaller than IPv6 header, this
aymeric's avatar
aymeric committed
252
 * value works for both.
253
 *
aymeric's avatar
aymeric committed
254
**/
255
MS2_PUBLIC int ms_get_payload_max_size(void);
aymeric's avatar
aymeric committed
256

Simon Morlat's avatar
Simon Morlat committed
257
MS2_PUBLIC void ms_set_payload_max_size(int size);
aymeric's avatar
aymeric committed
258 259 260 261 262 263

/**
 * Returns the network Max Transmission Unit to reach destination_host.
 * This will attempt to send one or more big packets to destination_host, to a random port.
 * Those packets are filled with zeroes.
**/
Simon Morlat's avatar
Simon Morlat committed
264
MS2_PUBLIC int ms_discover_mtu(const char *destination_host);
aymeric's avatar
aymeric committed
265 266 267 268 269

/**
 * Set mediastreamer default mtu, used to compute the default RTP max payload size.
 * This function will call ms_set_payload_max_size(mtu-[ipv6 header size]).
**/
Simon Morlat's avatar
Simon Morlat committed
270
MS2_PUBLIC void ms_set_mtu(int mtu);
aymeric's avatar
aymeric committed
271

272 273 274 275 276 277

/**
 * Get mediastreamer default mtu, used to compute the default RTP max payload size.
**/
MS2_PUBLIC int ms_get_mtu(void);

278 279 280 281
/**
 * Declare how many cpu (cores) are available on the platform
 */
MS2_PUBLIC void ms_set_cpu_count(unsigned int c);
282

283
MS2_PUBLIC unsigned int ms_get_cpu_count(void);
284

285 286 287 288 289
/**
 * Adds a new entry in the SoundDeviceDescription table
 */
MS2_PUBLIC void ms_sound_device_description_add(const char *manufacturer, const char *model, const char *platform, unsigned int flags, int delay, int recommended_rate);

jehan's avatar
jehan committed
290 291 292 293 294 295 296 297 298 299 300 301 302
/**
 * @return TRUE if address is ipv6
 */
MS2_PUBLIC bool_t ms_is_ipv6(const char *address);

/**
 * @return TRUE if address is multicast
 */
bool_t ms_is_multicast_addr(const struct sockaddr *address);
/**
 * @return TRUE if address is multicast
 */
MS2_PUBLIC bool_t ms_is_multicast(const char *address);
aymeric's avatar
aymeric committed
303 304 305 306 307 308
/** @} */

#ifdef __cplusplus
}
#endif

Simon Morlat's avatar
Simon Morlat committed
309 310 311 312
#ifdef MS2_INTERNAL
#  ifdef HAVE_CONFIG_H
#  include "mediastreamer-config.h" /*necessary to know if ENABLE_NLS is there*/
#  endif
313 314 315 316 317 318 319 320

#ifdef WIN32
#include <malloc.h> //for alloca
#ifdef _MSC_VER
#define alloca _alloca
#endif
#endif

Simon Morlat's avatar
Simon Morlat committed
321 322 323 324 325 326 327 328
#  if defined(ENABLE_NLS)
#    include <libintl.h>
#    define _(String) dgettext (GETTEXT_PACKAGE, String)
#  else
#    define _(String) (String)
#  endif // ENABLE_NLS
#define N_(String) (String)
#endif // MS2_INTERNAL
329 330 331 332

#ifdef ANDROID
#include "mediastreamer2/msjava.h"
#endif
aymeric's avatar
aymeric committed
333
#endif
Simon Morlat's avatar
Simon Morlat committed
334