forked from signalwire/freeswitch
-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathmod_amrwb.c
More file actions
978 lines (843 loc) · 30.2 KB
/
Copy pathmod_amrwb.c
File metadata and controls
978 lines (843 loc) · 30.2 KB
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
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
/*
* FreeSWITCH Modular Media Switching Software Library / Soft-Switch Application
* Copyright (C) 2005-2014, Anthony Minessale II <anthm@freeswitch.org>
*
* Version: MPL 1.1
*
* The contents of this file are subject to the Mozilla Public License Version
* 1.1 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
* http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the
* License.
*
* The Original Code is FreeSWITCH Modular Media Switching Software Library / Soft-Switch Application
*
* The Initial Developer of the Original Code is
* Anthony Minessale II <anthm@freeswitch.org>
* Portions created by the Initial Developer are Copyright (C)
* the Initial Developer. All Rights Reserved.
*
* Contributor(s):
*
* Anthony Minessale II <anthm@freeswitch.org>
* Brian K. West <brian@freeswitch.org>
* Dragos Oancea <dragos.oancea@athonet.com>
* Federico Favaro <federico.favaro@athonet.com>
* Marco Sinibaldi <marco.sinibaldi@athonet.com>
*
* The amrwb codec itself is not distributed with this module.
*
* mod_amrwb.c -- GSM-AMRWB Codec Module
*
*
* XML Parameters
*
* default-bitrate
* Bitrate mode that will be used if mode-set-overwrite and mode-set-overwrite-with-default-bitrate are set).
* volte
* If set, configures codec for use on cellular networks.
* adjust-bitrate
* Vary bitrate according to feedback from RTCP.
* force-oa
* Configure codec in octet aligned mode.
* force-be
* Configure codec in bandwidth efficient mode.
* mode-set-overwrite
* When answering a call, use codec bitrate modes from mode-set param, instead of mirroring the OFFER.
* mode-set-overwrite-with-default-bitrate
* If mode-set-overwrite is on, then use default-bitrate mode instead of mode-set.
* invite-prefer-oa
* When answering a call, if AMR-WB is offered in 2 modes (octet aligned and bandwidth efficient), select octet aligned.
* invite-prefer-be
* When answering a call, if AMR-WB is offered in 2 modes (octet aligned and bandwidth efficient), select bandwidth efficient.
* mode-set
* Provides bitrate modes to be used with mode-set-overwrite (if mode-set-overwrite-with-default-bitrate is off).
* debug
* If on, print extra codec info (CMR, ToC, last frame flag) at the FS's DEBUG level.
* silence-supp-off
* If true, then SDP has 'silenceSupp:off - - - -' to turn silence suppression off (no CNG).
* fmtp-extra
* Append any extra info to fmtp entry for AMR-WB.
*
*/
#include "switch.h"
SWITCH_MODULE_LOAD_FUNCTION(mod_amrwb_load);
SWITCH_MODULE_SHUTDOWN_FUNCTION(mod_amrwb_unload);
SWITCH_MODULE_DEFINITION(mod_amrwb, mod_amrwb_load, mod_amrwb_unload, NULL);
switch_mutex_t *global_lock;
int global_debug;
char AMRWB_CONFIGURATION[2000];
#ifndef AMRWB_PASSTHROUGH
#include "opencore-amrwb/dec_if.h" /*AMR-WB decoder API*/
#include "vo-amrwbenc/enc_if.h" /*AMR-WB encoder API*/
#include "bitshift.h"
#include "amrwb_be.h"
typedef enum {
AMRWB_OPT_OCTET_ALIGN = (1 << 0),
AMRWB_OPT_CRC = (1 << 1),
AMRWB_OPT_MODE_CHANGE_NEIGHBOR = (1 << 2),
AMRWB_OPT_ROBUST_SORTING = (1 << 3),
AMRWB_OPT_INTERLEAVING = (1 << 4)
} amrwb_flag_t;
typedef enum {
AMRWB_BITRATE_7K = 0,
AMRWB_BITRATE_8K,
AMRWB_BITRATE_12K,
AMRWB_BITRATE_14K,
AMRWB_BITRATE_16K,
AMRWB_BITRATE_18K,
AMRWB_BITRATE_20K,
AMRWB_BITRATE_23K,
AMRWB_BITRATE_24K
} amrwb_bitrate_t;
struct amrwb_context {
void *encoder_state;
void *decoder_state;
switch_byte_t enc_modes;
switch_byte_t enc_mode;
uint32_t change_period;
switch_byte_t max_ptime;
switch_byte_t ptime;
switch_byte_t channels;
switch_byte_t flags;
int max_red;
int debug;
};
#define SWITCH_AMRWB_DEFAULT_BITRATE AMRWB_BITRATE_24K
static struct {
switch_byte_t default_bitrate;
switch_byte_t volte;
switch_byte_t adjust_bitrate;
switch_byte_t force_oa; /*force OA when originating*/
switch_byte_t force_be;
switch_byte_t mode_set_overwrite;
switch_byte_t mode_set_overwrite_with_default_bitrate;
switch_byte_t invite_prefer_oa;
switch_byte_t invite_prefer_be;
struct amrwb_context context;
char *fmtp_extra;
switch_byte_t silence_supp_off;
} globals;
const int switch_amrwb_frame_sizes[] = {17, 23, 32, 36, 40, 46, 50, 58, 60, 5, 0, 0, 0, 0, 1, 1};
#define SWITCH_AMRWB_OUT_MAX_SIZE 62
#define SWITCH_AMRWB_MODES 10 /* Silence Indicator (SID) included */
#define invalid_frame_type (index > SWITCH_AMRWB_MODES && index != 0xe && index != 0xf) /* include SPEECH_LOST and NO_DATA*/
static switch_bool_t switch_amrwb_unpack_oa(unsigned char *buf, uint8_t *tmp, int encoded_data_len)
{
uint8_t *tocs;
int index;
int framesz;
buf++;/* CMR skip */
tocs = buf;
index = ((tocs[0]>>3) & 0xf);
buf++; /* point to voice payload */
if (invalid_frame_type) {
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "AMRWB decoder (OA): Invalid TOC: 0x%x", index);
return SWITCH_FALSE;
}
framesz = switch_amrwb_frame_sizes[index];
if (framesz > encoded_data_len - 1) {
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "AMRWB decoder (OA): Invalid frame size: %d\n", framesz);
return SWITCH_FALSE;
}
tmp[0] = tocs[0];
memcpy(&tmp[1], buf, framesz);
return SWITCH_TRUE;
}
static switch_bool_t switch_amrwb_pack_oa(unsigned char *shift_buf, int n)
{
/* Interleaving code here */
return SWITCH_TRUE;
}
static switch_bool_t switch_amrwb_info(switch_codec_t *codec, unsigned char *encoded_buf, int encoded_data_len, int payload_format, char *print_text)
{
uint8_t *tocs;
int framesz, index, not_last_frame, q, ft;
uint8_t shift_tocs[2] = {0x00, 0x00};
if (!encoded_buf) {
return SWITCH_FALSE;
}
/* payload format can be OA (octed-aligned) or BE (bandwidth efficient)*/
if (payload_format) {
/* OA */
encoded_buf++; /* CMR skip */
tocs = encoded_buf;
index = (tocs[0] >> 3) & 0x0f;
if (invalid_frame_type) {
switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(codec->session), SWITCH_LOG_ERROR, "AMRWB decoder (OA): Invalid TOC 0x%x\n", index);
return SWITCH_FALSE;
}
framesz = switch_amrwb_frame_sizes[index];
not_last_frame = (tocs[0] >> 7) & 1;
q = (tocs[0] >> 2) & 1;
ft = tocs[0] >> 3;
ft &= ~(1 << 5); /* Frame Type */
} else {
/* BE */
memcpy(shift_tocs, encoded_buf, 2);
/* shift for BE */
switch_amr_array_lshift(4, shift_tocs, 2);
not_last_frame = (shift_tocs[0] >> 7) & 1;
q = (shift_tocs[0] >> 2) & 1;
ft = shift_tocs[0] >> 3;
ft &= ~(1 << 5); /* Frame Type */
index = (shift_tocs[0] >> 3) & 0x0f;
if (invalid_frame_type) {
switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(codec->session), SWITCH_LOG_ERROR, "AMRWB decoder (BE): Invalid TOC 0x%x\n", index);
return SWITCH_FALSE;
}
framesz = switch_amrwb_frame_sizes[index];
}
switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(codec->session), SWITCH_LOG_DEBUG,
"%s (%s): FT: [0x%x] Q: [0x%x] Frame flag: [%d]\n",
print_text, payload_format ? "OA":"BE", ft, q, not_last_frame);
switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(codec->session), SWITCH_LOG_DEBUG,
"%s (%s): AMRWB encoded voice payload sz: [%d] : | encoded_data_len: [%d]\n",
print_text, payload_format ? "OA":"BE", framesz, encoded_data_len);
return SWITCH_TRUE;
}
#endif
static switch_status_t amrwb_parse_fmtp_cb(const char *fmtp, switch_codec_fmtp_t *codec_fmtp)
{
/* Must return IGNORE for FS to skip this codec */
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "Considering fmtp %s\n", fmtp);
if (!zstr(fmtp)) {
int x, argc;
char *argv[10];
char *fmtp_dup = strdup(fmtp);
/* If there is no octet-align param on fmtp then default is 0 (bandwidth efficient). */
int oa = 0;
if (!fmtp_dup) {
return SWITCH_STATUS_FALSE;
}
argc = switch_separate_string(fmtp_dup, ';', argv, (sizeof(argv) / sizeof(argv[0])));
for (x = 0; x < argc; x++) {
char *data = argv[x];
char *arg;
while (*data == ' ') {
data++;
}
if ((arg = strchr(data, '='))) {
*arg++ = '\0';
if (!strcasecmp(data, "octet-align")) {
oa = switch_true(arg);
}
}
}
free(fmtp_dup);
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "AMR-WB fmtp mode: %s\n", oa ? "octet aligned" : "bandwidth efficient");
if ((oa == 0 && globals.invite_prefer_oa) || (oa == 1 && globals.invite_prefer_be)) {
return SWITCH_STATUS_IGNORE;
}
}
/* Must return FALSE for FS to continue as if callback was not called */
return SWITCH_STATUS_FALSE;
}
static switch_status_t switch_amrwb_init(switch_codec_t *codec, switch_codec_flag_t flags, const switch_codec_settings_t *codec_settings)
{
#ifdef AMRWB_PASSTHROUGH
codec->flags |= SWITCH_CODEC_FLAG_PASSTHROUGH;
if (codec->fmtp_in) {
codec->fmtp_out = switch_core_strdup(codec->memory_pool, codec->fmtp_in);
}
return SWITCH_STATUS_SUCCESS;
#else
struct amrwb_context *context = NULL;
int encoding, decoding;
int x, i, argc, fmtptmp_pos;
char *argv[10];
char fmtptmp[128];
char *fmtp_dup = NULL;
switch_core_session_t *session = codec->session;
encoding = (flags & SWITCH_CODEC_FLAG_ENCODE);
decoding = (flags & SWITCH_CODEC_FLAG_DECODE);
if (!(encoding || decoding) || (!(context = switch_core_alloc(codec->memory_pool, sizeof(struct amrwb_context))))) {
return SWITCH_STATUS_FALSE;
} else {
/* "mode" may mean two different things:
* "Octed Aligned" or "Bandwidth Efficient" encoding mode ,
* or the actual bitrate which is set with FMTP param "mode-set". */
/* https://tools.ietf.org/html/rfc4867 */
/* set the default mode just in case there's no "mode-set" FMTP param */
context->enc_mode = globals.default_bitrate;
/* octet-align = 0 - per RFC - if there's no `octet-align` FMTP value then BE is employed */
switch_clear_flag(context, AMRWB_OPT_OCTET_ALIGN);
if (codec->fmtp_in) {
fmtp_dup = strdup(codec->fmtp_in);
switch_assert(fmtp_dup);
argc = switch_separate_string(fmtp_dup, ';', argv, (sizeof(argv) / sizeof(argv[0])));
for (x = 0; x < argc; x++) {
char *data = argv[x];
char *arg;
while (*data && *data == ' ') {
data++;
}
if ((arg = strchr(data, '='))) {
*arg++ = '\0';
if (!strcasecmp(data, "octet-align")) {
if (atoi(arg)) {
switch_set_flag(context, AMRWB_OPT_OCTET_ALIGN);
}
} else if (!strcasecmp(data, "mode-change-neighbor")) {
if (atoi(arg)) {
switch_set_flag(context, AMRWB_OPT_MODE_CHANGE_NEIGHBOR);
}
} else if (!strcasecmp(data, "crc")) {
if (atoi(arg)) {
switch_set_flag(context, AMRWB_OPT_CRC);
}
} else if (!strcasecmp(data, "robust-sorting")) {
if (atoi(arg)) {
switch_set_flag(context, AMRWB_OPT_ROBUST_SORTING);
}
} else if (!strcasecmp(data, "interleaving")) {
if (atoi(arg)) {
switch_set_flag(context, AMRWB_OPT_INTERLEAVING);
}
} else if (!strcasecmp(data, "mode-change-period")) {
context->change_period = atoi(arg);
} else if (!strcasecmp(data, "ptime")) {
context->ptime = (switch_byte_t) atoi(arg);
} else if (!strcasecmp(data, "channels")) {
context->channels = (switch_byte_t) atoi(arg);
} else if (!strcasecmp(data, "maxptime")) {
context->max_ptime = (switch_byte_t) atoi(arg);
} else if (!strcasecmp(data, "max-red")) {
context->max_red = atoi(arg);
} else if (!strcasecmp(data, "mode-set")) {
int y, m_argc;
char *m_argv[SWITCH_AMRWB_MODES-1]; /* AMRWB has 9 modes */
m_argc = switch_separate_string(arg, ',', m_argv, (sizeof(m_argv) / sizeof(m_argv[0])));
for (y = 0; y < m_argc; y++) {
context->enc_modes |= (1 << atoi(m_argv[y]));
context->enc_mode = atoi(m_argv[y]);
}
}
}
}
free(fmtp_dup);
}
if (globals.force_oa) {
switch_set_flag(context, AMRWB_OPT_OCTET_ALIGN);
}
if (globals.force_be) {
switch_clear_flag(context, AMRWB_OPT_OCTET_ALIGN);
}
if (context->enc_modes && !globals.mode_set_overwrite) {
/* If inbound fmtp has mode-set and XML overwrite is not set, then mirror these mode-set */
/* choose the highest mode (bitrate) for high audio quality. */
for (i = SWITCH_AMRWB_MODES-2; i > -1; i--) {
if (context->enc_modes & (1 << i)) {
context->enc_mode = (switch_byte_t) i;
break;
}
}
/* re-create mode-set */
fmtptmp_pos = switch_snprintf(fmtptmp, sizeof(fmtptmp), "mode-set=");
for (i = 0; SWITCH_AMRWB_MODES-1 > i; ++i) {
if (context->enc_modes & (1 << i)) {
fmtptmp_pos += switch_snprintf(fmtptmp + fmtptmp_pos, sizeof(fmtptmp) - fmtptmp_pos, fmtptmp_pos > strlen("mode-set=") ? ",%d" : "%d", i);
}
}
} else {
/* It is inbound fmtp with no mode-set or outbound */
if (globals.mode_set_overwrite_with_default_bitrate) {
fmtptmp_pos = switch_snprintf(fmtptmp, sizeof(fmtptmp), "mode-set=%d", globals.default_bitrate);
} else {
char modes[100] = { 0 };
int i = 0, j = 0;
for (i = 0; SWITCH_AMRWB_MODES-1 > i; ++i) {
if (globals.context.enc_modes & (1 << i)) {
j++;
snprintf(modes + strlen(modes), sizeof(modes) - strlen(modes), j > 1 ? ",%d" : "%d", i);
}
}
fmtptmp_pos = switch_snprintf(fmtptmp, sizeof(fmtptmp), "mode-set=%s", modes);
}
/* When carrier omits mode-set, sync enc_mode with configured mode-set
* so the encoder does not exceed the modes advertised in our answer SDP. */
if (globals.context.enc_modes) {
for (i = SWITCH_AMRWB_MODES-2; i > -1; i--) {
if (globals.context.enc_modes & (1 << i)) {
context->enc_mode = (switch_byte_t) i;
break;
}
}
}
}
if (globals.adjust_bitrate) {
switch_set_flag(codec, SWITCH_CODEC_FLAG_HAS_ADJ_BITRATE);
}
if (!globals.volte) {
switch_snprintf(fmtptmp + fmtptmp_pos, sizeof(fmtptmp) - fmtptmp_pos, ";octet-align=%d",
switch_test_flag(context, AMRWB_OPT_OCTET_ALIGN) ? 1 : 0);
} else {
switch_snprintf(fmtptmp + fmtptmp_pos, sizeof(fmtptmp) - fmtptmp_pos, ";octet-align=%d;max-red=0;mode-change-capability=2",
switch_test_flag(context, AMRWB_OPT_OCTET_ALIGN) ? 1 : 0);
}
if (!zstr(globals.fmtp_extra)) {
fmtptmp_pos += switch_snprintf(fmtptmp + fmtptmp_pos, sizeof(fmtptmp) - fmtptmp_pos, "; %s", globals.fmtp_extra);
}
if (globals.silence_supp_off) {
switch_channel_t *channel = NULL;
if (session) {
channel = switch_core_session_get_channel(session);
switch_channel_set_variable(channel, "suppress_cng", "true");
switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_DEBUG, "Turning CNG off (silence suppression off, suppress_cng=true) due to silence-supp-off=true\n");
} else {
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Cannot turn silence suppression off - session missing\n");
}
}
codec->fmtp_out = switch_core_strdup(codec->memory_pool, fmtptmp);
context->encoder_state = NULL;
context->decoder_state = NULL;
if (encoding) {
context->encoder_state = E_IF_init();
}
if (decoding) {
context->decoder_state = D_IF_init();
}
codec->private_info = context;
return SWITCH_STATUS_SUCCESS;
}
#endif
}
static switch_status_t switch_amrwb_destroy(switch_codec_t *codec)
{
#ifndef AMRWB_PASSTHROUGH
struct amrwb_context *context = codec->private_info;
if (context->encoder_state) {
E_IF_exit(context->encoder_state);
}
if (context->decoder_state) {
D_IF_exit(context->decoder_state);
}
codec->private_info = NULL;
#endif
return SWITCH_STATUS_SUCCESS;
}
static switch_status_t switch_amrwb_encode(switch_codec_t *codec,
switch_codec_t *other_codec,
void *decoded_data,
uint32_t decoded_data_len,
uint32_t decoded_rate, void *encoded_data, uint32_t *encoded_data_len, uint32_t *encoded_rate,
unsigned int *flag)
{
#ifdef AMRWB_PASSTHROUGH
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "This codec is only usable in passthrough mode!\n");
return SWITCH_STATUS_FALSE;
#else
struct amrwb_context *context = codec->private_info;
int n;
unsigned char *shift_buf = encoded_data;
if (!context) {
return SWITCH_STATUS_FALSE;
}
n = E_IF_encode(context->encoder_state, context->enc_mode, (int16_t *) decoded_data, (switch_byte_t *) encoded_data + 1, 0);
if (n < 0) {
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "AMRWB encoder: E_IF_encode() ERROR!\n");
return SWITCH_STATUS_FALSE;
}
/* set CMR + TOC (F + 3 bits of FT), 1111 = CMR: No mode request */
*(switch_byte_t *) encoded_data = 0xf0;
*encoded_data_len = n;
if (switch_test_flag(context, AMRWB_OPT_OCTET_ALIGN)) {
switch_amrwb_pack_oa(shift_buf, n); /* the payload is OA as it
comes out of the encoding function */
*encoded_data_len = n + 1;
} else {
switch_amrwb_pack_be(shift_buf, n);
}
switch_mutex_lock(global_lock);
if (global_debug) {
switch_amrwb_info(codec, shift_buf, *encoded_data_len, switch_test_flag(context, AMRWB_OPT_OCTET_ALIGN) ? 1 : 0, "AMRWB encoder");
}
switch_mutex_unlock(global_lock);
return SWITCH_STATUS_SUCCESS;
#endif
}
static switch_status_t switch_amrwb_decode(switch_codec_t *codec,
switch_codec_t *other_codec,
void *encoded_data,
uint32_t encoded_data_len,
uint32_t encoded_rate, void *decoded_data, uint32_t *decoded_data_len, uint32_t *decoded_rate,
unsigned int *flag)
{
#ifdef AMRWB_PASSTHROUGH
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "This codec is only usable in passthrough mode!\n");
return SWITCH_STATUS_FALSE;
#else
struct amrwb_context *context = codec->private_info;
unsigned char buf[SWITCH_AMRWB_OUT_MAX_SIZE];
uint8_t tmp[SWITCH_AMRWB_OUT_MAX_SIZE];
if (!context || encoded_data_len > SWITCH_AMRWB_OUT_MAX_SIZE) {
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "AMRWB decoder: Invalid context or encoded data length %d\n", encoded_data_len);
goto decode_error;
}
memcpy(buf, encoded_data, encoded_data_len);
switch_mutex_lock(global_lock);
if (global_debug) {
switch_amrwb_info(codec, buf, encoded_data_len, switch_test_flag(context, AMRWB_OPT_OCTET_ALIGN) ? 1 : 0, "AMRWB decoder");
}
switch_mutex_unlock(global_lock);
if (switch_test_flag(context, AMRWB_OPT_OCTET_ALIGN)) {
/* Octed Aligned */
if (!switch_amrwb_unpack_oa(buf, tmp, encoded_data_len)) {
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "AMRWB decoder (OA): Invalid frame size: %d\n", encoded_data_len);
goto decode_error;
}
} else {
/* Bandwidth Efficient */
if (!switch_amrwb_unpack_be(buf, tmp, encoded_data_len)) {
goto decode_error;
}
}
D_IF_decode(context->decoder_state, tmp, (int16_t *) decoded_data, 0);
*decoded_data_len = codec->implementation->decoded_bytes_per_packet;
return SWITCH_STATUS_SUCCESS;
decode_error:
/* Check if codec reset is in progress to prevent call termination */
if (switch_test_flag(codec, SWITCH_CODEC_FLAG_RESET_PENDING)) {
return SWITCH_STATUS_NOOP;
}
return SWITCH_STATUS_FALSE;
#endif
}
#ifndef AMRWB_PASSTHROUGH
static switch_status_t switch_amrwb_control(switch_codec_t *codec,
switch_codec_control_command_t cmd,
switch_codec_control_type_t ctype,
void *cmd_data,
switch_codec_control_type_t atype,
void *cmd_arg,
switch_codec_control_type_t *rtype,
void **ret_data)
{
struct amrwb_context *context = codec->private_info;
int debug = 0;
switch_mutex_lock(global_lock);
debug = global_debug;
switch_mutex_unlock(global_lock);
switch(cmd) {
case SCC_DEBUG:
{
int32_t level = *((uint32_t *) cmd_data);
context->debug = level;
}
break;
case SCC_AUDIO_ADJUST_BITRATE:
{
const char *cmd = (const char *)cmd_data;
if (!strcasecmp(cmd, "increase")) {
if (context->enc_mode < SWITCH_AMRWB_MODES - 1) {
int mode_step = 2; /*this is the mode, not the actual bitrate*/
context->enc_mode = context->enc_mode + mode_step;
if (debug || context->debug) {
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG,
"AMRWB encoder: Adjusting mode to %d (increase)\n", context->enc_mode);
}
}
} else if (!strcasecmp(cmd, "decrease")) {
if (context->enc_mode > 0) {
int mode_step = 2; /*this is the mode, not the actual bitrate*/
context->enc_mode = context->enc_mode - mode_step;
if (debug || context->debug) {
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG,
"AMRWB encoder: Adjusting mode to %d (decrease)\n", context->enc_mode);
}
}
} else if (!strcasecmp(cmd, "default")) {
context->enc_mode = globals.default_bitrate;
if (debug || context->debug) {
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG,
"AMRWB encoder: Adjusting mode to %d (default)\n", context->enc_mode);
}
} else {
/*minimum bitrate (AMRWB mode)*/
context->enc_mode = 0;
if (debug || context->debug) {
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG,
"AMRWB encoder: Adjusting mode to %d (minimum)\n", context->enc_mode);
}
}
}
break;
default:
break;
}
return SWITCH_STATUS_SUCCESS;
}
#endif
#ifndef AMRWB_PASSTHROUGH
static int extract_octet_align(const char *fmtp)
{
int oa = 0;
int argc;
char *argv[10];
char *fmtp_dup;
if (zstr(fmtp)) return oa;
fmtp_dup = strdup(fmtp);
if (!fmtp_dup) return oa;
argc = switch_separate_string(fmtp_dup, ';', argv, (int)(sizeof(argv) / sizeof(argv[0])));
for (int i = 0; i < argc; ++i) {
char *data = argv[i];
char *arg;
while (*data == ' ') data++;
arg = strchr(data, '=');
if (arg) {
*arg++ = '\0';
while (*arg == ' ') arg++;
if (!strcasecmp(data, "octet-align")) {
oa = switch_true(arg);
break;
}
}
}
switch_safe_free(fmtp_dup);
return oa;
}
static switch_status_t matches_fmtp(const char *fmtp, const char *codec_fmtp)
{
int oa1 = extract_octet_align(fmtp);
int oa2 = extract_octet_align(codec_fmtp);
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "AMRWB fmtp: %s, codec_fmtp: %s\n", switch_str_nil(fmtp), switch_str_nil(codec_fmtp));
return (oa1 == oa2) ? SWITCH_STATUS_SUCCESS : SWITCH_STATUS_FALSE;
}
#endif
static char *generate_fmtp(switch_memory_pool_t *pool , int octet_align)
{
char buf[256] = { 0 };
#ifndef AMRWB_PASSTHROUGH
int i = 0, j =0;
#endif
snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf), "octet-align=%d; ", octet_align);
#ifndef AMRWB_PASSTHROUGH
// ENGDESK-15706
if (globals.context.enc_modes && !globals.mode_set_overwrite) {
for (i = 0; SWITCH_AMRWB_MODES-1 > i; ++i) {
if (globals.context.enc_modes & (1 << i)) {
j++;
snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf), j > 1 ? ",%d" : "mode-set=%d", i);
}
}
} else {
snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf), "mode-set=%d", globals.default_bitrate);
}
snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf), "; ");
if (globals.volte) {
snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf), "max-red=0; mode-change-capability=2; ");
}
#endif
if (!zstr(globals.fmtp_extra)) {
snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf), "%s", globals.fmtp_extra);
}
if (end_of(buf) == ' ') {
*(end_of_p(buf) - 1) = '\0';
}
return switch_core_strdup(pool, buf);
}
#ifndef AMRWB_PASSTHROUGH
#define AMRWB_DEBUG_SYNTAX "<on|off>"
SWITCH_STANDARD_API(mod_amrwb_debug)
{
if (zstr(cmd)) {
stream->write_function(stream, "-USAGE: %s\n", AMRWB_DEBUG_SYNTAX);
} else {
if (!strcasecmp(cmd, "on")) {
switch_mutex_lock(global_lock);
global_debug = 1;
switch_mutex_unlock(global_lock);
stream->write_function(stream, "AMRWB Debug: on\n");
} else if (!strcasecmp(cmd, "off")) {
switch_mutex_lock(global_lock);
global_debug = 0;
switch_mutex_unlock(global_lock);
stream->write_function(stream, "AMRWB Debug: off\n");
} else {
stream->write_function(stream, "-USAGE: %s\n", AMRWB_DEBUG_SYNTAX);
}
}
return SWITCH_STATUS_SUCCESS;
}
#endif
static void mod_amrwb_configuration_snprintf(void) {
char modes[100] = { 0 };
int i = 0, j = 0;
int debug = 0;
snprintf(modes + strlen(modes), sizeof(modes) - strlen(modes), "[");
for (i = 0; SWITCH_AMRWB_MODES-1 > i; ++i) {
if (globals.context.enc_modes & (1 << i)) {
j++;
snprintf(modes + strlen(modes), sizeof(modes) - strlen(modes), j > 1 ? ",%d" : "%d", i);
}
}
snprintf(modes + strlen(modes), sizeof(modes) - strlen(modes), "]");
switch_mutex_lock(global_lock);
debug = global_debug;
switch_mutex_unlock(global_lock);
snprintf(AMRWB_CONFIGURATION, sizeof(AMRWB_CONFIGURATION),
"modes: %s, "
"mode-set-overwrite: %d, "
"mode-set-overwrite-with-default-bitrate: %d, "
"default-bitrate: %d, "
"volte: %d, "
"adjust-bitrate: %d, "
"force-oa: %d, "
"force-be: %d, "
"invite-prefer-oa: %d, "
"invite-prefer-be: %d, "
"fmtp-extra: [%s], "
"debug: %d, "
"silence-supp-off: %d\n",
modes,
globals.mode_set_overwrite,
globals.mode_set_overwrite_with_default_bitrate,
globals.default_bitrate,
globals.volte,
globals.adjust_bitrate,
globals.force_oa,
globals.force_be,
globals.invite_prefer_oa,
globals.invite_prefer_be,
!zstr(globals.fmtp_extra) ? globals.fmtp_extra : "",
debug,
globals.silence_supp_off
);
}
#define AMRWB_SHOW_SYNTAX ""
SWITCH_STANDARD_API(mod_amrwb_show)
{
if (stream && stream->write_function) {
mod_amrwb_configuration_snprintf();
stream->write_function(stream, AMRWB_CONFIGURATION);
}
return SWITCH_STATUS_SUCCESS;
}
/* Registration */
SWITCH_MODULE_LOAD_FUNCTION(mod_amrwb_load)
{
switch_codec_interface_t *codec_interface;
char *default_fmtp_oa = NULL;
char *default_fmtp_be = NULL;
#ifndef AMRWB_PASSTHROUGH
switch_api_interface_t *commands_api_interface;
char *cf = "amrwb.conf";
switch_xml_t cfg, xml, settings, param;
memset(&globals, 0, sizeof(globals));
globals.default_bitrate = SWITCH_AMRWB_DEFAULT_BITRATE;
globals.mode_set_overwrite_with_default_bitrate = 1;
if ((xml = switch_xml_open_cfg(cf, &cfg, NULL))) {
if ((settings = switch_xml_child(cfg, "settings"))) {
for (param = switch_xml_child(settings, "param"); param; param = param->next) {
char *var = (char *) switch_xml_attr_soft(param, "name");
char *val = (char *) switch_xml_attr_soft(param, "value");
if (!strcasecmp(var, "default-bitrate")) {
globals.default_bitrate = (switch_byte_t) atoi(val);
}
if (!strcasecmp(var, "volte")) {
/* ETSI TS 126 236 compatibility: http://www.etsi.org/deliver/etsi_ts/126200_126299/126236/10.00.00_60/ts_126236v100000p.pdf */
globals.volte = (switch_byte_t) atoi(val);
}
if (!strcasecmp(var, "adjust-bitrate")) {
globals.adjust_bitrate = (switch_byte_t) atoi(val);
}
if (!strcasecmp(var, "force-oa")) {
globals.force_oa = (switch_byte_t) atoi(val);
}
if (!strcasecmp(var, "force-be")) {
globals.force_be = (switch_byte_t) atoi(val);
}
if (!strcasecmp(var, "mode-set-overwrite")) {
globals.mode_set_overwrite = (switch_byte_t) atoi(val);
}
if (!strcasecmp(var, "mode-set-overwrite-with-default-bitrate")) {
globals.mode_set_overwrite_with_default_bitrate = (switch_byte_t) atoi(val);
}
if (!strcasecmp(var, "invite-prefer-oa")) {
globals.invite_prefer_oa = (switch_byte_t) atoi(val);
}
if (!strcasecmp(var, "invite-prefer-be")) {
globals.invite_prefer_be = (switch_byte_t) atoi(val);
}
if (!strcasecmp(var, "mode-set")) {
int y, m_argc;
char *m_argv[SWITCH_AMRWB_MODES-1]; /* AMRWB has 9 modes */
m_argc = switch_separate_string(val, ',', m_argv, (sizeof(m_argv) / sizeof(m_argv[0])));
for (y = 0; y < m_argc; y++) {
globals.context.enc_modes |= (1 << atoi(m_argv[y]));
globals.context.enc_mode = atoi(m_argv[y]);
}
}
if (!strcasecmp(var, "debug")) {
global_debug = (switch_byte_t) atoi(val);
}
if (!strcasecmp(var, "fmtp-extra")) {
globals.fmtp_extra = switch_core_strdup(pool, val);
switch_assert(globals.fmtp_extra);
}
if (!strcasecmp(var, "silence-supp-off")) {
globals.silence_supp_off = (switch_byte_t) atoi(val);
}
}
}
}
if (xml) {
switch_xml_free(xml);
}
#endif
/* connect my internal structure to the blank pointer passed to me */
*module_interface = switch_loadable_module_create_module_interface(pool, modname);
#ifndef AMRWB_PASSTHROUGH
SWITCH_ADD_API(commands_api_interface, "amrwb_debug", "Set AMR-WB Debug", mod_amrwb_debug, AMRWB_DEBUG_SYNTAX);
switch_console_set_complete("add amrwb_debug on");
switch_console_set_complete("add amrwb_debug off");
#endif
SWITCH_ADD_API(commands_api_interface, "amrwb_show", "Show AMR-WB configuration", mod_amrwb_show, AMRWB_SHOW_SYNTAX);
SWITCH_ADD_CODEC(codec_interface, "AMR-WB / Octet Aligned");
codec_interface->parse_fmtp = amrwb_parse_fmtp_cb;
default_fmtp_oa = generate_fmtp(pool, 1);
switch_core_codec_add_implementation(pool, codec_interface,
SWITCH_CODEC_TYPE_AUDIO, 100, "AMR-WB", default_fmtp_oa,
16000, 16000, 23850, 20000, 320, 640, 0, 1, 1,
switch_amrwb_init, switch_amrwb_encode, switch_amrwb_decode, switch_amrwb_destroy);
codec_interface->implementations->always_emit_channels_in_rtpmap = SWITCH_TRUE;
#ifndef AMRWB_PASSTHROUGH
codec_interface->implementations->codec_control = switch_amrwb_control;
codec_interface->implementations->matches_fmtp = matches_fmtp;
#endif
// SWITCH_ADD_CODEC(codec_interface, "AMR-WB / Bandwidth Efficient");
codec_interface->parse_fmtp = amrwb_parse_fmtp_cb;
default_fmtp_be = generate_fmtp(pool, 0);
switch_core_codec_add_implementation(pool, codec_interface,
SWITCH_CODEC_TYPE_AUDIO, 110, "AMR-WB", default_fmtp_be,
16000, 16000, 23850, 20000, 320, 640, 0, 1, 1,
switch_amrwb_init, switch_amrwb_encode, switch_amrwb_decode, switch_amrwb_destroy);
codec_interface->implementations->always_emit_channels_in_rtpmap = SWITCH_TRUE;
#ifndef AMRWB_PASSTHROUGH
codec_interface->implementations->codec_control = switch_amrwb_control;
codec_interface->implementations->matches_fmtp = matches_fmtp;
#endif
switch_mutex_init(&global_lock, SWITCH_MUTEX_NESTED, pool);
mod_amrwb_configuration_snprintf();
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_NOTICE, "AMRWB config: %s", AMRWB_CONFIGURATION);
/* indicate that the module should continue to be loaded */
return SWITCH_STATUS_SUCCESS;
}
SWITCH_MODULE_SHUTDOWN_FUNCTION(mod_amrwb_unload) {
switch_mutex_destroy(global_lock);
return SWITCH_STATUS_SUCCESS;
}
/* For Emacs:
* Local Variables:
* mode:c
* indent-tabs-mode:t
* tab-width:4
* c-basic-offset:4
* End:
* For VIM:
* vim:set softtabstop=4 shiftwidth=4 tabstop=4 noet:
*/