summaryrefslogtreecommitdiffstats
path: root/tinyDAV/src/tdav.c
blob: 0fa72e8bbfe0e6b39d77167de5482b439777b07d (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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
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
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
/*
* Copyright (C) 2010-2014 Mamadou DIOP.
*
* This file is part of Open Source Doubango Framework.
*
* DOUBANGO 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 3 of the License, or
* (at your option) any later version.
*
* DOUBANGO 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 DOUBANGO.
*
*/
#include "tinydav/tdav.h"

static tsk_bool_t __b_initialized = tsk_false;
static tsk_bool_t __b_ipsec_supported = tsk_false;
static const struct tmedia_codec_plugin_def_s* __codec_plugins_all[0xFF] = { tsk_null }; // list of all codecs BEFORE filtering
static const tsk_size_t __codec_plugins_all_count = sizeof(__codec_plugins_all)/sizeof(__codec_plugins_all[0]);

#if TDAV_UNDER_WINDOWS
#	include "tinydav/tdav_win32.h"
#elif TDAV_UNDER_APPLE
#	include "tinydav/tdav_apple.h"
#endif

// Shared libraries not allowed on WP8 and iOS
#if !TDAV_UNDER_WINDOWS_PHONE && !TDAV_UNDER_IPHONE
#include "tsk_plugin.h"
#	if TDAV_UNDER_WINDOWS
#		define TDAV_HAVE_PLUGIN_EXT_WIN32	1
static struct tsk_plugin_s* __dll_plugin_wasapi = tsk_null; /* Windows Audio Session API (WASAPI): Windows [Vista - 8] */
static struct tsk_plugin_s* __dll_plugin_dshow = tsk_null; /* DirectShow: Windows [XP - 8] */
static struct tsk_plugin_s* __dll_plugin_mf = tsk_null; /* Media Foundation and WASAPI : Windows [7 - 8] */
static struct tsk_plugin_s* __dll_plugin_dd = tsk_null; /* Microsoft Desktop Duplication API : Windows [8 - ] */
static struct tsk_plugin_s* __dll_plugin_cuda = tsk_null; /* Media Foundation and WASAPI : Windows [XP - 8] */
static struct tsk_plugin_s* __dll_plugin_audio_dsp = tsk_null; /* Audio DSP, Resampler, AEC, NS, AGC...: Windows [Vista - 8] */
static struct tsk_plugin_s* __dll_plugin_ipsec_wfp = tsk_null; /* IPSec implementation using WFP (Windows Filtering platform): Windows [Vista - 8] */
#	endif /* TDAV_UNDER_WINDOWS */
#endif

// Media Contents, plugins defintion...
#include "tinymedia.h"

// IPSec
#if !defined(HAVE_TINYIPSEC) || HAVE_TINYIPSEC
#include "tipsec.h"
#endif

// Converters
#include "tinymedia/tmedia_converter_video.h"
// Converters
#include "tinymedia/tmedia_converter_video.h"
#include "tinydav/video/tdav_converter_video.h"

// Sessions
#include "tinymedia/tmedia_session_ghost.h"
#include "tinydav/audio/tdav_session_audio.h"
#include "tinydav/video/tdav_session_video.h"
#include "tinydav/msrp/tdav_session_msrp.h"
#include "tinydav/bfcp/tdav_session_bfcp.h"
#include "tinydav/t140/tdav_session_t140.h"

// Codecs
#include "tinydav/codecs/dtmf/tdav_codec_dtmf.h"
#include "tinydav/codecs/t140/tdav_codec_t140.h"
#include "tinydav/codecs/fec/tdav_codec_ulpfec.h"
#include "tinydav/codecs/fec/tdav_codec_red.h"
#if !defined(HAVE_TINYMSRP) || HAVE_TINYMSRP
#include "tinydav/codecs/msrp/tdav_codec_msrp.h"
#endif
#if !defined(HAVE_TINYBFCP) || HAVE_TINYBFCP
#include "tinydav/codecs/bfcp/tdav_codec_bfcp.h"
#endif
#include "tinydav/codecs/amr/tdav_codec_amr.h"
#include "tinydav/codecs/bv/tdav_codec_bv16.h"
#include "tinydav/codecs/g711/tdav_codec_g711.h"
#include "tinydav/codecs/gsm/tdav_codec_gsm.h"
#include "tinydav/codecs/ilbc/tdav_codec_ilbc.h"
#include "tinydav/codecs/g729/tdav_codec_g729.h"
#include "tinydav/codecs/g722/tdav_codec_g722.h"
#include "tinydav/codecs/speex/tdav_codec_speex.h"
#include "tinydav/codecs/opus/tdav_codec_opus.h"
#include "tinydav/codecs/h261/tdav_codec_h261.h"
#include "tinydav/codecs/h263/tdav_codec_h263.h"
#include "tinydav/codecs/h264/tdav_codec_h264.h"
#include "tinydav/codecs/h264/tdav_codec_h264_cuda.h"
#include "tinydav/codecs/h264/tdav_codec_h264_cisco.h"
#include "tinydav/codecs/h264/tdav_codec_h264_intel.h"
#include "tinydav/codecs/theora/tdav_codec_theora.h"
#include "tinydav/codecs/mp4ves/tdav_codec_mp4ves.h"
#include "tinydav/codecs/vpx/tdav_codec_vp8.h"

// Consumers
#include "tinydav/audio/waveapi/tdav_consumer_waveapi.h"
#include "tinydav/audio/directsound/tdav_consumer_dsound.h"
#include "tinydav/audio/coreaudio/tdav_consumer_audioqueue.h"
#include "tinydav/audio/coreaudio/tdav_consumer_audiounit.h"
#include "tinydav/audio/wasapi/tdav_consumer_wasapi.h"
#include "tinydav/audio/alsa/tdav_consumer_alsa.h"
#include "tinydav/audio/oss/tdav_consumer_oss.h"
#include "tinydav/video/winm/tdav_consumer_winm.h"
#include "tinydav/video/mf/tdav_consumer_video_mf.h"
#include "tinydav/video/gdi/tdav_consumer_video_gdi.h"
#include "tinydav/t140/tdav_consumer_t140.h"

// Producers
#include "tinydav/audio/waveapi/tdav_producer_waveapi.h"
#include "tinydav/audio/directsound/tdav_producer_dsound.h"
#include "tinydav/video/gdi/tdav_producer_screencast_gdi.h"
#include "tinydav/video/directx/tdav_producer_screencast_ddraw.h"
#include "tinydav/video/v4linux/tdav_producer_video_v4l2.h"
#include "tinydav/audio/coreaudio/tdav_producer_audioqueue.h"
#include "tinydav/audio/coreaudio/tdav_producer_audiounit.h"
#include "tinydav/audio/wasapi/tdav_producer_wasapi.h"
#include "tinydav/audio/alsa/tdav_producer_alsa.h"
#include "tinydav/audio/oss/tdav_producer_oss.h"
#include "tinydav/video/winm/tdav_producer_winm.h"
#include "tinydav/video/mf/tdav_producer_video_mf.h"
#include "tinydav/t140/tdav_producer_t140.h"

// Audio Denoise (AGC, Noise Suppression, VAD and AEC)
#if HAVE_SPEEX_DSP && (!defined(HAVE_SPEEX_DENOISE) || HAVE_SPEEX_DENOISE)
#	include "tinydav/audio/tdav_speex_denoise.h"
#endif
#if HAVE_WEBRTC && (!defined(HAVE_WEBRTC_DENOISE) || HAVE_WEBRTC_DENOISE)
#	include "tinydav/audio/tdav_webrtc_denoise.h"
#endif

// Audio resampler
#if HAVE_SPEEX_DSP && (!defined(HAVE_SPEEX_RESAMPLER) || HAVE_SPEEX_RESAMPLER)
#	include "tinydav/audio/tdav_speex_resampler.h"
#endif

// Audio/Video JitterBuffer
#if HAVE_SPEEX_DSP && HAVE_SPEEX_JB
#	include "tinydav/audio/tdav_speex_jitterbuffer.h"
#else
#	include "tinydav/audio/tdav_speakup_jitterbuffer.h"
#endif

#if HAVE_FFMPEG
#	include <libavcodec/avcodec.h>
#endif

static inline int _tdav_codec_plugins_collect();
static inline int _tdav_codec_plugins_disperse();
static inline tsk_bool_t _tdav_codec_is_supported(tdav_codec_id_t codec, const tmedia_codec_plugin_def_t* plugin);

int tdav_init()
{
    int ret = 0;

    if(__b_initialized) {
        TSK_DEBUG_INFO("TINYDAV already initialized");
        return 0;
    }

    /* === OS specific === */
#if TDAV_UNDER_WINDOWS
    if ((ret = tdav_win32_init())) {
        return ret;
    }
#elif TDAV_UNDER_APPLE
    if ((ret = tdav_apple_init())) {
        return ret;
    }
#endif

    /* === Initialize ffmpeg === */
#if HAVE_FFMPEG
#   if LIBAVCODEC_VERSION_MAJOR <= 53
    avcodec_init();
#   endif
#endif

    /* === stand-alone plugins === */
#if TDAV_HAVE_PLUGIN_EXT_WIN32
    {
        tsk_size_t plugins_count = 0;
        char* full_path = tsk_null; // Loading plugins from ActiveX fails when using relative path.
        /* WASAPI (Audio consumer, Audio producer) */
#if 0 // disable WASAPI by default (AEC issue because of code#consumer rate mismatch)
        if(tdav_win32_is_winvista_or_later()) {
            tsk_sprintf(&full_path, "%s/pluginWASAPI.dll", tdav_get_current_directory_const());
            if(tsk_plugin_file_exist(full_path) && (__dll_plugin_wasapi = tsk_plugin_create(full_path))) {
                plugins_count += tmedia_plugin_register(__dll_plugin_wasapi, tsk_plugin_def_type_all, tsk_plugin_def_media_type_all);
            }
        }
#endif
        /* CUDA (H.264 codec) */
#if 1 // Enable CUDA by default
        tsk_sprintf(&full_path, "%s/pluginCUDA.dll", tdav_get_current_directory_const()); // CUDA works on all Windows versions
        if(tsk_plugin_file_exist(full_path) && (__dll_plugin_cuda = tsk_plugin_create(full_path))) {
            plugins_count += tmedia_plugin_register(__dll_plugin_cuda, tsk_plugin_def_type_all, tsk_plugin_def_media_type_all);
        }
#endif
        /* Microsoft Desktop Duplication API (Screen capture) */
        if (tdav_win32_is_win8_or_later()) {
            tsk_sprintf(&full_path, "%s/pluginWinDD.dll", tdav_get_current_directory_const());
            if (tsk_plugin_file_exist(full_path) && (__dll_plugin_dd = tsk_plugin_create(full_path))) {
                plugins_count += tmedia_plugin_register(__dll_plugin_dd, tsk_plugin_def_type_all, tsk_plugin_def_media_type_all);
            }
        }

        /* Media Foundation (Video converter, Video consumer, Video producer, Microsoft H.264 codec, Intel Quick Sync H.264 codec) */
        if(tdav_win32_is_win7_or_later()) {
            tsk_sprintf(&full_path, "%s/pluginWinMF.dll", tdav_get_current_directory_const());
            if(tsk_plugin_file_exist(full_path) && (__dll_plugin_mf = tsk_plugin_create(full_path))) {
                plugins_count += tmedia_plugin_register(__dll_plugin_mf, tsk_plugin_def_type_all, tsk_plugin_def_media_type_all);
            }
        }
        /* DirectShow (Video consumer, Video producer) */
        if (tdav_win32_is_winxp_or_later()) {
            tsk_sprintf(&full_path, "%s/pluginDirectShow.dll", tdav_get_current_directory_const());
            if (tsk_plugin_file_exist(full_path) && (__dll_plugin_dshow = tsk_plugin_create(full_path))) {
                plugins_count += tmedia_plugin_register(__dll_plugin_dshow, tsk_plugin_def_type_all, tsk_plugin_def_media_type_all);
            }
        }
        /* Audio DSP (Resampler, AEC, NS, AGC...) */
        if (tdav_win32_is_winvista_or_later()) {
            tsk_sprintf(&full_path, "%s/pluginWinAudioDSP.dll", tdav_get_current_directory_const());
            if(tsk_plugin_file_exist(full_path) && (__dll_plugin_audio_dsp = tsk_plugin_create(full_path))) {
                plugins_count += tmedia_plugin_register(__dll_plugin_audio_dsp, tsk_plugin_def_type_all, tsk_plugin_def_media_type_all);
            }
        }
        /* IPSec implementation using Windows Filtering Platform (WFP) */
#if !defined(HAVE_TINYIPSEC) || HAVE_TINYIPSEC
        if (tdav_win32_is_winvista_or_later()) {
            tsk_sprintf(&full_path, "%s/pluginWinIPSecVista.dll", tdav_get_current_directory_const());
            if (tsk_plugin_file_exist(full_path) && (tipsec_plugin_register_file(full_path, &__dll_plugin_ipsec_wfp) == 0)) {
                plugins_count += 1; // at least one
                __b_ipsec_supported = tsk_true;
            }
        }
#endif

        TSK_FREE(full_path);
        TSK_DEBUG_INFO("Windows stand-alone plugins loaded = %u", plugins_count);
    }
#endif

    /* === Register media contents === */
    tmedia_content_plugin_register("text/html", tmedia_content_dummy_plugin_def_t);
    tmedia_content_plugin_register("text/plain", tmedia_content_dummy_plugin_def_t);
    tmedia_content_plugin_register("application/octet-stream", tmedia_content_dummy_plugin_def_t);
    tmedia_content_plugin_register("message/CPIM", tmedia_content_cpim_plugin_def_t);
    /* To be implemented
    tmedia_content_plugin_register("message/sipfrag", tmedia_content_sipfrag_plugin_def_t);
    tmedia_content_plugin_register("multipart/digest", tmedia_content_multipart_plugin_def_t);
    tmedia_content_plugin_register("multipart/mixed", tmedia_content_multipart_plugin_def_t);
    tmedia_content_plugin_register("multipart/related", tmedia_content_multipart_plugin_def_t);
    tmedia_content_plugin_register("multipart/alternative", tmedia_content_multipart_plugin_def_t);
    tmedia_content_plugin_register("multipart/encrypted", tmedia_content_multipart_plugin_def_t);
    tmedia_content_plugin_register("multipart/parallel", tmedia_content_multipart_plugin_def_t);
    tmedia_content_plugin_register("multipart/signed", tmedia_content_multipart_plugin_def_t);
    */

    /* === Register sessions === */
    tmedia_session_plugin_register(tmedia_session_ghost_plugin_def_t);
    tmedia_session_plugin_register(tdav_session_audio_plugin_def_t);
    tmedia_session_plugin_register(tdav_session_video_plugin_def_t);
#if !defined(HAVE_TINYMSRP) || HAVE_TINYMSRP
    tmedia_session_plugin_register(tdav_session_msrp_plugin_def_t);
#endif
    tmedia_session_plugin_register(tdav_session_t140_plugin_def_t);
#if !defined(HAVE_TINYBFCP) || HAVE_TINYBFCP
    tmedia_session_plugin_register(tdav_session_bfcp_plugin_def_t);
#endif
    tmedia_session_plugin_register(tdav_session_bfcpaudio_plugin_def_t);
    tmedia_session_plugin_register(tdav_session_bfcpvideo_plugin_def_t);

    /* === Register codecs === */
#if HAVE_FFMPEG
    avcodec_register_all();
#endif
#if !defined(HAVE_TINYMSRP) || HAVE_TINYMSRP
    tmedia_codec_plugin_register(tdav_codec_msrp_plugin_def_t);
#endif
    tmedia_codec_plugin_register(tdav_codec_t140_plugin_def_t);
#if !defined(HAVE_TINYBFCP) || HAVE_TINYBFCP
    tmedia_codec_plugin_register(tdav_codec_bfcp_plugin_def_t);
#endif
    tmedia_codec_plugin_register(tdav_codec_red_plugin_def_t);
    tmedia_codec_plugin_register(tdav_codec_g711a_plugin_def_t);
    tmedia_codec_plugin_register(tdav_codec_g711u_plugin_def_t);
    tmedia_codec_plugin_register(tdav_codec_g722_plugin_def_t);
#if HAVE_OPENCORE_AMR
    tmedia_codec_plugin_register(tdav_codec_amrnb_oa_plugin_def_t);
    tmedia_codec_plugin_register(tdav_codec_amrnb_be_plugin_def_t);
#endif
#if HAVE_BV16
    tmedia_codec_plugin_register(tdav_codec_bv16_plugin_def_t);
#endif
#if HAVE_LIBGSM
    tmedia_codec_plugin_register(tdav_codec_gsm_plugin_def_t);
#endif
#if HAVE_ILBC
    tmedia_codec_plugin_register(tdav_codec_ilbc_plugin_def_t);
#endif
#if HAVE_LIB_SPEEX
    tmedia_codec_plugin_register(tdav_codec_speex_nb_plugin_def_t);
    tmedia_codec_plugin_register(tdav_codec_speex_wb_plugin_def_t);
    tmedia_codec_plugin_register(tdav_codec_speex_uwb_plugin_def_t);
#endif
#if HAVE_LIBOPUS
    tmedia_codec_plugin_register(tdav_codec_opus_plugin_def_t);
#endif
#if HAVE_G729
    tmedia_codec_plugin_register(tdav_codec_g729ab_plugin_def_t);
#endif
    // last: dtmf, ULPFEC and RED
    tmedia_codec_plugin_register(tdav_codec_dtmf_plugin_def_t);
    // tmedia_codec_plugin_register(tdav_codec_ulpfec_plugin_def_t);
    // tmedia_codec_plugin_register(tdav_codec_red_plugin_def_t);

#if HAVE_LIBVPX
    tmedia_codec_plugin_register(tdav_codec_vp8_plugin_def_t);
#endif
#if HAVE_CUDA
#error "Support for H.264 Cuda is deprecated"
    if(tdav_codec_h264_cuda_is_supported()) {
        tmedia_codec_plugin_register(tdav_codec_h264_cuda_bp10_plugin_def_t);
        tmedia_codec_plugin_register(tdav_codec_h264_cuda_bp20_plugin_def_t);
        tmedia_codec_plugin_register(tdav_codec_h264_cuda_bp30_plugin_def_t);
    }
#endif
#if HAVE_OPENH264
    tmedia_codec_plugin_register(tdav_codec_h264_cisco_base_plugin_def_t);
#endif
#if HAVE_FFMPEG
    if(tdav_codec_ffmpeg_mp4ves_is_supported()) {
        tmedia_codec_plugin_register(tdav_codec_mp4ves_plugin_def_t);
    }
    if(tdav_codec_ffmpeg_h264_is_supported()) {
        if(!tmedia_codec_plugin_is_registered_2(tmedia_codec_id_h264_bp)) { // could be already registered by stand alone plugins (e.g. pluginWinMF.DLL)
            tmedia_codec_plugin_register(tdav_codec_h264_base_plugin_def_t);
        }
        if(!tmedia_codec_plugin_is_registered_2(tmedia_codec_id_h264_mp)) { // could be already registered by stand alone plugins (e.g. pluginWinMF.DLL)
            tmedia_codec_plugin_register(tdav_codec_h264_main_plugin_def_t);
        }
    }
    tmedia_codec_plugin_register(tdav_codec_h263p_plugin_def_t);
    tmedia_codec_plugin_register(tdav_codec_h263pp_plugin_def_t);
    if(tdav_codec_ffmpeg_theora_is_supported()) {
        tmedia_codec_plugin_register(tdav_codec_theora_plugin_def_t);
    }
    tmedia_codec_plugin_register(tdav_codec_h263_plugin_def_t);
    tmedia_codec_plugin_register(tdav_codec_h261_plugin_def_t);
#elif HAVE_H264_PASSTHROUGH
    tmedia_codec_plugin_register(tdav_codec_h264_base_plugin_def_t);
    tmedia_codec_plugin_register(tdav_codec_h264_main_plugin_def_t);
#endif
#if HAVE_INTEL_MEDIA_SDK
    tmedia_codec_plugin_register(tdav_codec_h264_intel_base_plugin_def_t);
    tmedia_codec_plugin_register(tdav_codec_h264_intel_main_plugin_def_t);
#endif


    /* === Register converters === */
    // register several convertors and try them all (e.g. LIBYUV only support to/from I420)
#if HAVE_LIBYUV
    tmedia_converter_video_plugin_register(tdav_converter_video_libyuv_plugin_def_t);
#endif
#if HAVE_FFMPEG || HAVE_SWSSCALE
    tmedia_converter_video_plugin_register(tdav_converter_video_ffmpeg_plugin_def_t);
#endif

    /* === Register consumers === */
    tmedia_consumer_plugin_register(tdav_consumer_t140_plugin_def_t); /* T140 */
#if HAVE_DSOUND_H
    tmedia_consumer_plugin_register(tdav_consumer_dsound_plugin_def_t);
#elif HAVE_WAVE_API
    tmedia_consumer_plugin_register(tdav_consumer_waveapi_plugin_def_t);
#elif HAVE_WASAPI
    tmedia_consumer_plugin_register(tdav_consumer_wasapi_plugin_def_t);
#endif
#if TDAV_UNDER_WINDOWS && !TDAV_UNDER_WINDOWS_RT // Windows GDI
    tmedia_consumer_plugin_register(tdav_consumer_video_gdi_plugin_def_t);
#endif
#if HAVE_WINM // Windows Media (WP8)
    tmedia_consumer_plugin_register(tdav_consumer_winm_plugin_def_t);
#endif
#if HAVE_ALSA_ASOUNDLIB_H // Linux
    tmedia_consumer_plugin_register(tdav_consumer_alsa_plugin_def_t);
#endif
#if HAVE_LINUX_SOUNDCARD_H // Linux
    tmedia_consumer_plugin_register(tdav_consumer_oss_plugin_def_t);
#endif

#if HAVE_COREAUDIO_AUDIO_UNIT // CoreAudio based on AudioUnit
    tmedia_consumer_plugin_register(tdav_consumer_audiounit_plugin_def_t);
#elif HAVE_COREAUDIO_AUDIO_QUEUE // CoreAudio based on AudioQueue
    tmedia_consumer_plugin_register(tdav_consumer_audioqueue_plugin_def_t);
#endif

#if HAVE_OSS_H
    tmedia_consumer_plugin_register(tmedia_consumer_oss_plugin_def_t);
#endif

    /* === Register producers === */
    tmedia_producer_plugin_register(tdav_producer_t140_plugin_def_t); /* T140 */
#if HAVE_DSOUND_H // DirectSound
    tmedia_producer_plugin_register(tdav_producer_dsound_plugin_def_t);
#elif HAVE_WAVE_API // WaveAPI
    tmedia_producer_plugin_register(tdav_producer_waveapi_plugin_def_t);
#elif HAVE_WASAPI // WASAPI
    tmedia_producer_plugin_register(tdav_producer_wasapi_plugin_def_t);
#endif
#if TDAV_UNDER_WINDOWS && !TDAV_UNDER_WINDOWS_RT // Windows DirectDraw (DirectX)
    if (tdav_producer_screencast_ddraw_plugin_is_supported()) {
        tmedia_producer_plugin_register(tdav_producer_screencast_ddraw_plugin_def_t);
    }
#endif
#if TDAV_UNDER_WINDOWS && !TDAV_UNDER_WINDOWS_RT // Windows GDI
    tmedia_producer_plugin_register(tdav_producer_screencast_gdi_plugin_def_t);
#endif
#if HAVE_WINM // Windows Media (WP8)
    tmedia_producer_plugin_register(tdav_producer_winm_plugin_def_t);
#endif
#if HAVE_ALSA_ASOUNDLIB_H // Linux
    tmedia_producer_plugin_register(tdav_producer_alsa_plugin_def_t);
#endif
#if HAVE_LINUX_SOUNDCARD_H // Linux
    tmedia_producer_plugin_register(tdav_producer_oss_plugin_def_t);
#endif
#if HAVE_LINUX_VIDEODEV2_H // V4L2 (Linux)
    tmedia_producer_plugin_register(tdav_producer_video_v4l2_plugin_def_t);
    tmedia_producer_plugin_register(tdav_producer_screencast_v4l2_plugin_def_t);
#endif

#if HAVE_COREAUDIO_AUDIO_UNIT // CoreAudio based on AudioUnit
    tmedia_producer_plugin_register(tdav_producer_audiounit_plugin_def_t);
#elif HAVE_COREAUDIO_AUDIO_QUEUE // CoreAudio based on AudioQueue
    tmedia_producer_plugin_register(tdav_producer_audioqueue_plugin_def_t);
#endif

    /* === Register Audio Denoise (AGC, VAD, Noise Suppression and AEC) === */
#if HAVE_WEBRTC && (!defined(HAVE_WEBRTC_DENOISE) || HAVE_WEBRTC_DENOISE)
    tmedia_denoise_plugin_register(tdav_webrtc_denoise_plugin_def_t);
#endif
#if HAVE_SPEEX_DSP && (!defined(HAVE_SPEEX_DENOISE) || HAVE_SPEEX_DENOISE)
    tmedia_denoise_plugin_register(tdav_speex_denoise_plugin_def_t);
#endif

    /* === Register Audio Resampler === */
#if HAVE_SPEEX_DSP && (!defined(HAVE_SPEEX_RESAMPLER) || HAVE_SPEEX_RESAMPLER)
    tmedia_resampler_plugin_register(tdav_speex_resampler_plugin_def_t);
#endif

    /* === Register Audio/video JitterBuffer === */
#if HAVE_SPEEX_DSP && HAVE_SPEEX_JB
    tmedia_jitterbuffer_plugin_register(tdav_speex_jitterbuffer_plugin_def_t);
#else
    tmedia_jitterbuffer_plugin_register(tdav_speakup_jitterbuffer_plugin_def_t);
#endif

    // collect all codecs before filtering
    _tdav_codec_plugins_collect();

    __b_initialized = tsk_true;

    return ret;
}

int tdav_codec_set_priority(tdav_codec_id_t codec_id, int priority)
{
    tsk_size_t i;

    if(priority < 0) {
        TSK_DEBUG_ERROR("Invalid parameter");
        return -1;
    }
    for(i = 0; i < __codec_plugins_all_count && __codec_plugins_all[i]; ++i) {
        if(__codec_plugins_all[i]->codec_id == codec_id) {
            const struct tmedia_codec_plugin_def_s *codec_decl_1, *codec_decl_2;
            priority = TSK_MIN(priority, (int)__codec_plugins_all_count-1);
            codec_decl_1 = __codec_plugins_all[priority];
            codec_decl_2 = __codec_plugins_all[i];

            __codec_plugins_all[i] = codec_decl_1;
            __codec_plugins_all[priority] = codec_decl_2;

            // change priority if already registered and supported
            if(_tdav_codec_is_supported((tdav_codec_id_t)codec_decl_2->codec_id, codec_decl_2) && tmedia_codec_plugin_is_registered(codec_decl_2)) {
                return tmedia_codec_plugin_register_2(codec_decl_2, priority);
            }
            return 0;
        }
    }

    TSK_DEBUG_INFO("Cannot find codec with id=%d for priority setting", codec_id);
    return 0;
}

int tdav_set_codecs(tdav_codec_id_t codecs)
{
    tsk_size_t i, prio;

    // unregister all codecs
    tmedia_codec_plugin_unregister_all();
    // register "selected" and "fake" codecs. "fake" codecs have "none" as id (e.g. MSRP or DTMF)
    for(i=0,prio=0; i<__codec_plugins_all_count && __codec_plugins_all[i]; ++i) {
        if((codecs & __codec_plugins_all[i]->codec_id) || __codec_plugins_all[i]->codec_id == tmedia_codec_id_none) {
            if(_tdav_codec_is_supported((tdav_codec_id_t)__codec_plugins_all[i]->codec_id, __codec_plugins_all[i])) {
                tmedia_codec_plugin_register_2(__codec_plugins_all[i], (int)prio++);
            }
        }
    }
    return 0;
}

static inline int _tdav_codec_plugins_collect()
{
#if defined(_MSC_VER) // TODO: Why next code crash on CentOS64 when built with debug enabled ("-g -O0")
    const struct tmedia_codec_plugin_def_s* (* plugins)[TMED_CODEC_MAX_PLUGINS];
    tsk_size_t i, count;
    int ret;
    static const tsk_size_t __codec_plugins_all_count = sizeof(__codec_plugins_all)/sizeof(__codec_plugins_all[0]);

    ret = _tdav_codec_plugins_disperse();
    if((ret = tmedia_codec_plugin_registered_get_all(&plugins, &count)) == 0) {
        for(i = 0; i < count && i < __codec_plugins_all_count; ++i) {
            __codec_plugins_all[i] = (*plugins)[i];
        }
    }
    return 0;
#else
    extern const tmedia_codec_plugin_def_t* __tmedia_codec_plugins[TMED_CODEC_MAX_PLUGINS];

    static const tsk_size_t __codec_plugins_all_count = sizeof(__codec_plugins_all)/sizeof(__codec_plugins_all[0]);

    int ret = _tdav_codec_plugins_disperse();
    if (ret == 0) {
        tsk_size_t i, count_max = sizeof(__tmedia_codec_plugins)/sizeof(__tmedia_codec_plugins[0]);
        for(i = 0; i < count_max && i < __codec_plugins_all_count; ++i) {
            __codec_plugins_all[i] = __tmedia_codec_plugins[i];
        }
    }
    return ret;
#endif
}

static inline int _tdav_codec_plugins_disperse()
{
    memset((void*)__codec_plugins_all, 0, sizeof(__codec_plugins_all));
    return 0;
}


/*
 Must be called after tdav_init()
*/
static inline tsk_bool_t _tdav_codec_is_supported(tdav_codec_id_t codec, const tmedia_codec_plugin_def_t* plugin)
{
    tsk_size_t i;
    for(i = 0; i < __codec_plugins_all_count && __codec_plugins_all[i]; ++i) {
        if((plugin && __codec_plugins_all[i] == plugin) || __codec_plugins_all[i]->codec_id == codec) {
            return tsk_true;
        }
    }
    return tsk_false;
}

/**
* Checks whether a codec is supported. Being supported doesn't mean it's enabled and ready for use.
* @return @ref tsk_true if supported and @tsk_false otherwise.
* @sa @ref tdav_codec_is_enabled()
*/
tsk_bool_t tdav_codec_is_supported(tdav_codec_id_t codec)
{
    return _tdav_codec_is_supported(codec, tsk_null);
}

/**
* Checks whether a codec is enabled.
* @return @ref tsk_true if enabled and @tsk_false otherwise.
* @sa @ref tdav_codec_is_supported()
*/
tsk_bool_t tdav_codec_is_enabled(tdav_codec_id_t codec)
{
    return tmedia_codec_plugin_is_registered_2((tmedia_codec_id_t)codec);
}

/**
* Checks whether a IPSec is supported.
* @return @ref tsk_true if supported and @tsk_false otherwise.
*/
tsk_bool_t tdav_ipsec_is_supported()
{
    return __b_ipsec_supported;
}

int tdav_deinit()
{
    int ret = 0;

    if(!__b_initialized) {
        TSK_DEBUG_INFO("TINYDAV not initialized");
        return 0;
    }

    /* === OS specific === */
#if TDAV_UNDER_WINDOWS
    if ((ret = tdav_win32_deinit())) {
        return ret;
    }
#elif TDAV_UNDER_APPLE
    if ((ret = tdav_apple_deinit())) {
        return ret;
    }
#endif

    /* === UnRegister media contents === */
    tmedia_content_plugin_unregister_all();

    /* === UnRegister sessions === */
    tmedia_session_plugin_unregister(tmedia_session_ghost_plugin_def_t);
    tmedia_session_plugin_unregister(tdav_session_audio_plugin_def_t);
    tmedia_session_plugin_unregister(tdav_session_video_plugin_def_t);
#if !defined(HAVE_TINYMSRP) || HAVE_TINYMSRP
    tmedia_session_plugin_unregister(tdav_session_msrp_plugin_def_t);
#endif
    tmedia_session_plugin_unregister(tdav_session_t140_plugin_def_t);
#if !defined(HAVE_TINYBFCP) || HAVE_TINYBFCP
    tmedia_session_plugin_unregister(tdav_session_bfcp_plugin_def_t);
#endif
    tmedia_session_plugin_unregister(tdav_session_bfcpaudio_plugin_def_t);
    tmedia_session_plugin_unregister(tdav_session_bfcpvideo_plugin_def_t);

    /* === UnRegister codecs === */
    tmedia_codec_plugin_unregister_all();


    /* === unRegister converters === */
#if HAVE_LIBYUV
    tmedia_converter_video_plugin_unregister(tdav_converter_video_libyuv_plugin_def_t);
#endif
#if HAVE_FFMPEG || HAVE_SWSSCALE
    tmedia_converter_video_plugin_unregister(tdav_converter_video_ffmpeg_plugin_def_t);
#endif

    /* === unRegister consumers === */
    tmedia_consumer_plugin_unregister(tdav_consumer_t140_plugin_def_t); /* T140 */
#if HAVE_DSOUND_H
    tmedia_consumer_plugin_unregister(tdav_consumer_dsound_plugin_def_t);
#endif
#if HAVE_WAVE_API
    tmedia_consumer_plugin_unregister(tdav_consumer_waveapi_plugin_def_t);
#endif
#if HAVE_WASAPI
    tmedia_consumer_plugin_unregister(tdav_consumer_wasapi_plugin_def_t);
#endif
#if TDAV_UNDER_WINDOWS && !TDAV_UNDER_WINDOWS_RT // Windows GDI
    tmedia_consumer_plugin_unregister(tdav_consumer_video_gdi_plugin_def_t);
#endif
#if HAVE_WINM // Windows Media (WP8)
    tmedia_consumer_plugin_unregister(tdav_consumer_winm_plugin_def_t);
#endif
#if HAVE_COREAUDIO_AUDIO_UNIT // CoreAudio based on AudioUnit
    tmedia_consumer_plugin_unregister(tdav_consumer_audiounit_plugin_def_t);
#endif
#if HAVE_COREAUDIO_AUDIO_QUEUE // CoreAudio based on AudioQueue
    tmedia_consumer_plugin_unregister(tdav_consumer_audioqueue_plugin_def_t);
#endif
#if HAVE_ALSA_ASOUNDLIB_H // Linux
    tmedia_consumer_plugin_unregister(tdav_consumer_alsa_plugin_def_t);
#endif
#if HAVE_LINUX_SOUNDCARD_H // Linux
    tmedia_consumer_plugin_unregister(tdav_consumer_oss_plugin_def_t);
#endif

    /* === UnRegister producers === */
    tmedia_producer_plugin_unregister(tdav_producer_t140_plugin_def_t); /* T140 */
#if HAVE_DSOUND_H // DirectSound
    tmedia_producer_plugin_unregister(tdav_producer_dsound_plugin_def_t);
#endif
#if HAVE_WAVE_API // WaveAPI
    tmedia_producer_plugin_unregister(tdav_producer_waveapi_plugin_def_t);
#endif
#if HAVE_WASAPI // WASAPI
    tmedia_producer_plugin_unregister(tdav_producer_wasapi_plugin_def_t);
#endif
#if TDAV_UNDER_WINDOWS && !TDAV_UNDER_WINDOWS_RT // Windows DirectDraw (DirectX)
    tmedia_producer_plugin_unregister(tdav_producer_screencast_ddraw_plugin_def_t);
#endif
#if TDAV_UNDER_WINDOWS && !TDAV_UNDER_WINDOWS_RT // Windows GDI
    tmedia_producer_plugin_unregister(tdav_producer_screencast_gdi_plugin_def_t);
#endif
#if HAVE_WINM // Windows Media (WP8)
    tmedia_producer_plugin_unregister(tdav_producer_winm_plugin_def_t);
#endif
#if HAVE_ALSA_ASOUNDLIB_H // Linux
    tmedia_producer_plugin_unregister(tdav_producer_alsa_plugin_def_t);
#endif
#if HAVE_LINUX_SOUNDCARD_H // Linux
    tmedia_producer_plugin_unregister(tdav_producer_oss_plugin_def_t);
#endif
#if HAVE_LINUX_VIDEODEV2_H // V4L2 (Linux)
    tmedia_producer_plugin_unregister(tdav_producer_video_v4l2_plugin_def_t);
    tmedia_producer_plugin_unregister(tdav_producer_screencast_v4l2_plugin_def_t);
#endif

#if HAVE_COREAUDIO_AUDIO_UNIT // CoreAudio based on AudioUnit
    tmedia_producer_plugin_unregister(tdav_producer_audiounit_plugin_def_t);
#elif HAVE_COREAUDIO_AUDIO_QUEUE // CoreAudio based on AudioQueue
    tmedia_producer_plugin_unregister(tdav_producer_audioqueue_plugin_def_t);
#endif

#if HAVE_OSS_H
    tmedia_consumer_plugin_unregister(tmedia_consumer_oss_plugin_def_t);
#endif

    /* === UnRegister Audio Denoise (AGC, VAD, Noise Suppression and AEC) === */
#if HAVE_SPEEX_DSP && (!defined(HAVE_SPEEX_DENOISE) || HAVE_SPEEX_DENOISE)
    tmedia_denoise_plugin_unregister(tdav_speex_denoise_plugin_def_t);
#endif
#if HAVE_WEBRTC && (!defined(HAVE_WEBRTC_DENOISE) || HAVE_WEBRTC_DENOISE)
    tmedia_denoise_plugin_unregister(tdav_webrtc_denoise_plugin_def_t);
#endif

    /* === UnRegister Audio Resampler === */
#if HAVE_SPEEX_DSP && (!defined(HAVE_SPEEX_RESAMPLER) || HAVE_SPEEX_RESAMPLER)
    tmedia_resampler_plugin_unregister(tdav_speex_resampler_plugin_def_t);
#endif

    /* === UnRegister Audio/video JitterBuffer === */
#if HAVE_SPEEX_DSP && HAVE_SPEEX_JB
    tmedia_jitterbuffer_plugin_unregister(tdav_speex_jitterbuffer_plugin_def_t);
#else
    tmedia_jitterbuffer_plugin_unregister(tdav_speakup_jitterbuffer_plugin_def_t);
#endif

    /* === stand-alone plugins === */
#if TDAV_HAVE_PLUGIN_EXT_WIN32
    {
        TSK_OBJECT_SAFE_FREE(__dll_plugin_cuda);
        TSK_OBJECT_SAFE_FREE(__dll_plugin_wasapi);
        TSK_OBJECT_SAFE_FREE(__dll_plugin_mf);
        TSK_OBJECT_SAFE_FREE(__dll_plugin_dd);
        TSK_OBJECT_SAFE_FREE(__dll_plugin_dshow);
        TSK_OBJECT_SAFE_FREE(__dll_plugin_audio_dsp);
        TSK_OBJECT_SAFE_FREE(__dll_plugin_ipsec_wfp);
    }
#endif

    // disperse all collected codecs
    _tdav_codec_plugins_disperse();

    __b_initialized = tsk_false;

    return ret;
}
OpenPOWER on IntegriCloud