-
Notifications
You must be signed in to change notification settings - Fork 85
/
asterisk_opus+vp8.diff
1209 lines (1187 loc) · 39.1 KB
/
asterisk_opus+vp8.diff
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
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
diff -urN asterisk-11.1.2/build_tools/menuselect-deps.in asterisk-11.1.2-opus/build_tools/menuselect-deps.in
--- asterisk-11.1.2/build_tools/menuselect-deps.in 2012-07-25 14:21:54.000000000 +0200
+++ asterisk-11.1.2-opus/build_tools/menuselect-deps.in 2013-05-24 16:22:58.320556018 +0200
@@ -40,6 +40,7 @@
NEON29=@PBX_NEON29@
OGG=@PBX_OGG@
OPENH323=@PBX_OPENH323@
+OPUS=@PBX_OPUS@
OSPTK=@PBX_OSPTK@
OSS=@PBX_OSS@
PGSQL=@PBX_PGSQL@
diff -urN asterisk-11.1.2/channels/chan_sip.c asterisk-11.1.2-opus/channels/chan_sip.c
--- asterisk-11.1.2/channels/chan_sip.c 2013-01-02 20:23:44.000000000 +0100
+++ asterisk-11.1.2-opus/channels/chan_sip.c 2013-05-27 12:16:44.907975566 +0200
@@ -7724,8 +7724,25 @@
break;
case AST_CONTROL_VIDUPDATE: /* Request a video frame update */
if (p->vrtp && !p->novideo) {
- transmit_info_with_vidupdate(p);
- /* ast_rtcp_send_h261fur(p->vrtp); */
+ /* Only use this for WebRTC users */
+ struct ast_format_cap *fcap = ast_channel_nativeformats(ast);
+ struct ast_format vp8;
+ ast_format_set(&vp8, AST_FORMAT_VP8, 0);
+ if(ast_format_cap_iscompatible(fcap, &vp8)) {
+ sip_pvt_lock(p);
+ if (p->vrtp) {
+ ast_log(LOG_WARNING, "chan_sip, sending RTCP FIR to WebRTC user\n");
+ /* FIXME Fake RTP write, this will be sent as an RTCP packet */
+ struct ast_frame fr;
+ fr.frametype = AST_FRAME_CONTROL;
+ fr.subclass.integer = AST_CONTROL_VIDUPDATE;
+ res = ast_rtp_instance_write(p->vrtp, &fr);
+ }
+ sip_pvt_unlock(p);
+ } else {
+ transmit_info_with_vidupdate(p);
+ /* ast_rtcp_send_h261fur(p->vrtp); */
+ }
} else
res = -1;
break;
@@ -10982,7 +10999,7 @@
struct ast_format *format;
if ((format = ast_rtp_codecs_get_payload_format(newaudiortp, codec))) {
- unsigned int bit_rate;
+ unsigned int bit_rate, value;
if (!ast_format_sdp_parse(format, fmtp_string)) {
found = TRUE;
@@ -11021,6 +11038,53 @@
}
}
break;
+ /* Opus SDP fmtp parameters (draft-ietf-payload-rtp-opus-00) */
+ case AST_FORMAT_OPUS:
+ if (sscanf(fmtp_string, "maxplaybackrate=%30u", &value) == 1) {
+ ast_log(LOG_WARNING, "Got Opus maxplaybackrate=%d\n", value);
+ /* TODO: actually handle this */
+ found = TRUE;
+ }
+ if (sscanf(fmtp_string, "sprop-maxcapturerate=%30u", &value) == 1) {
+ ast_log(LOG_WARNING, "Got Opus sprop-maxcapturerate=%d\n", value);
+ /* TODO: actually handle this */
+ found = TRUE;
+ }
+ if (sscanf(fmtp_string, "minptime=%30u", &value) == 1) {
+ ast_log(LOG_WARNING, "Got Opus minptime=%d\n", value);
+ /* TODO: actually handle this */
+ found = TRUE;
+ }
+ if (sscanf(fmtp_string, "maxaveragebitrate=%30u", &value) == 1) {
+ ast_log(LOG_WARNING, "Got Opus maxaveragebitrate=%d\n", value);
+ /* TODO: actually handle this */
+ found = TRUE;
+ }
+ if (sscanf(fmtp_string, "stereo=%30u", &value) == 1) {
+ ast_log(LOG_WARNING, "Got Opus stereo=%d\n", value);
+ /* TODO: actually handle this */
+ found = TRUE;
+ }
+ if (sscanf(fmtp_string, "sprop-stereo=%30u", &value) == 1) {
+ ast_log(LOG_WARNING, "Got Opus sprop-stereo=%d\n", value);
+ /* TODO: actually handle this */
+ found = TRUE;
+ }
+ if (sscanf(fmtp_string, "cbr=%30u", &value) == 1) {
+ ast_log(LOG_WARNING, "Got Opus cbr=%d\n", value);
+ /* TODO: actually handle this */
+ found = TRUE;
+ }
+ if (sscanf(fmtp_string, "useinbandfec=%30u", &value) == 1) {
+ ast_log(LOG_WARNING, "Got Opus useinbandfec=%d\n", value);
+ /* TODO: actually handle this */
+ found = TRUE;
+ }
+ if (sscanf(fmtp_string, "usedtx=%30u", &value) == 1) {
+ ast_log(LOG_WARNING, "Got Opus usedtx=%d\n", value);
+ /* TODO: actually handle this */
+ found = TRUE;
+ }
}
}
}
@@ -11041,7 +11105,9 @@
/* We have a rtpmap to handle */
if (*last_rtpmap_codec < SDP_MAX_RTPMAP_CODECS) {
/* Note: should really look at the '#chans' params too */
- if (!strncasecmp(mimeSubtype, "H26", 3) || !strncasecmp(mimeSubtype, "MP4", 3)) {
+ if (!strncasecmp(mimeSubtype, "H26", 3) || !strncasecmp(mimeSubtype, "MP4", 3)
+ /* VP8 */
+ || !strncasecmp(mimeSubtype, "VP8", 3)) {
if (!(ast_rtp_codecs_payloads_set_rtpmap_type_rate(newvideortp, NULL, codec, "video", mimeSubtype, 0, sample_rate))) {
if (debug)
ast_verbose("Found video description format %s for ID %d\n", mimeSubtype, codec);
@@ -12583,7 +12649,11 @@
} else /* I don't see how you couldn't have p->rtp, but good to check for and error out if not there like earlier code */
return;
ast_str_append(m_buf, 0, " %d", rtp_code);
- ast_str_append(a_buf, 0, "a=rtpmap:%d %s/%d\r\n", rtp_code, mime, rate);
+ /* Opus mandates 2 channels in rtpmap */
+ if((int) format->id == AST_FORMAT_OPUS)
+ ast_str_append(a_buf, 0, "a=rtpmap:%d %s/%d/2\r\n", rtp_code, mime, rate);
+ else
+ ast_str_append(a_buf, 0, "a=rtpmap:%d %s/%d\r\n", rtp_code, mime, rate);
ast_format_sdp_generate(format, rtp_code, a_buf);
@@ -12612,6 +12682,17 @@
/* Indicate that we only expect 64Kbps */
ast_str_append(a_buf, 0, "a=fmtp:%d bitrate=64000\r\n", rtp_code);
break;
+ /* Opus, pass parameters we care about (FIXME could this be 'fb' and not 'wb'?) */
+ case AST_FORMAT_OPUS:
+ ast_str_append(a_buf, 0, "a=maxptime:%d\r\n", 60); /* FIXME */
+ ast_str_append(a_buf, 0, "a=fmtp:%d maxplaybackrate=%d; stereo=%d; sprop-stereo=%d; useinbandfec=%d\r\n",
+ rtp_code,
+ 16000, /* maxplaybackrate */
+ 0, /* stereo */
+ 0, /* sprop-stereo */
+ 0 /* useinbandfec FIXME */
+ );
+ break;
}
if (fmt.cur_ms && (fmt.cur_ms < *min_packet_size))
diff -urN asterisk-11.1.2/codecs/codec_opus.c asterisk-11.1.2-opus/codecs/codec_opus.c
--- asterisk-11.1.2/codecs/codec_opus.c 1970-01-01 01:00:00.000000000 +0100
+++ asterisk-11.1.2-opus/codecs/codec_opus.c 2015-02-13 12:59:46.395779315 +0100
@@ -0,0 +1,539 @@
+/*
+ * Asterisk -- An open source telephony toolkit.
+ *
+ * Copyright (C) 1999 - 2005, Digium, Inc.
+ *
+ * Mark Spencer <markster@digium.com>
+ *
+ *
+ * See http://www.asterisk.org for more information about
+ * the Asterisk project. Please do not directly contact
+ * any of the maintainers of this project for assistance;
+ * the project provides a web site, mailing lists and IRC
+ * channels for your use.
+ *
+ * This program is free software, distributed under the terms of
+ * the GNU General Public License Version 2. See the LICENSE file
+ * at the top of the source tree.
+ */
+
+/*! \file
+ *
+ * \brief Translate between signed linear and Opus (Open Codec)
+ *
+ * \author Lorenzo Miniero <lorenzo@meetecho.com>
+ *
+ * \note This work was motivated by Mozilla
+ *
+ * \ingroup codecs
+ *
+ * \extref The Opus library - http://opus-codec.org
+ *
+ */
+
+/*** MODULEINFO
+ <depend>opus</depend>
+ <support_level>core</support_level>
+ ***/
+
+#include "asterisk.h"
+
+ASTERISK_FILE_VERSION(__FILE__, "$Revision: 328209 $")
+
+#include <opus/opus.h>
+
+#include "asterisk/translate.h"
+#include "asterisk/module.h"
+#include "asterisk/config.h"
+#include "asterisk/utils.h"
+#include "asterisk/cli.h"
+
+
+#define BUFFER_SAMPLES 8000
+#define OPUS_SAMPLES 160
+
+#define USE_FEC 0
+
+#define DEFAULT_COMPLEXITY 4
+
+
+/* Sample frame data */
+#include "asterisk/slin.h"
+#include "ex_opus.h"
+
+/* FIXME: Test */
+#include "asterisk/file.h"
+
+
+static int encid = 0;
+static int decid = 0;
+
+static int opusdebug = 0;
+
+
+/* Private structures */
+struct opus_coder_pvt {
+ void *opus; /* May be encoder or decoder */
+ int sampling_rate;
+ int multiplier;
+ int fec;
+
+ int id;
+
+ int16_t buf[BUFFER_SAMPLES]; /* FIXME */
+ int framesize;
+
+ FILE *file;
+};
+
+
+/* Helper methods */
+static int opus_encoder_construct(struct ast_trans_pvt *pvt, int sampling_rate) {
+ if(sampling_rate != 8000 && sampling_rate != 12000 && sampling_rate != 16000 && sampling_rate != 24000 && sampling_rate != 48000)
+ return -1;
+ struct opus_coder_pvt *opvt = pvt->pvt;
+ opvt->sampling_rate = sampling_rate;
+ opvt->multiplier = 48000/sampling_rate;
+ opvt->fec = USE_FEC;
+ int error = 0;
+ opvt->opus = opus_encoder_create(sampling_rate, 1, OPUS_APPLICATION_VOIP, &error);
+ if(error != OPUS_OK) {
+ if(opusdebug)
+ ast_verbose("[Opus] Ops! got an error creating the Opus encoder: %d (%s)\n", error, opus_strerror(error));
+ return -1;
+ }
+ if(sampling_rate == 8000)
+ opus_encoder_ctl(opvt->opus, OPUS_SET_MAX_BANDWIDTH(OPUS_BANDWIDTH_NARROWBAND));
+ else if(sampling_rate == 12000)
+ opus_encoder_ctl(opvt->opus, OPUS_SET_MAX_BANDWIDTH(OPUS_BANDWIDTH_MEDIUMBAND));
+ else if(sampling_rate == 16000)
+ opus_encoder_ctl(opvt->opus, OPUS_SET_MAX_BANDWIDTH(OPUS_BANDWIDTH_WIDEBAND));
+ else if(sampling_rate == 24000)
+ opus_encoder_ctl(opvt->opus, OPUS_SET_MAX_BANDWIDTH(OPUS_BANDWIDTH_SUPERWIDEBAND));
+ else if(sampling_rate == 48000)
+ opus_encoder_ctl(opvt->opus, OPUS_SET_MAX_BANDWIDTH(OPUS_BANDWIDTH_FULLBAND));
+ opus_encoder_ctl(opvt->opus, OPUS_SET_INBAND_FEC(opvt->fec));
+ opus_encoder_ctl(opvt->opus, OPUS_SET_COMPLEXITY(DEFAULT_COMPLEXITY));
+ opvt->framesize = sampling_rate/50;
+ opvt->id = ++encid;
+ if(opusdebug)
+ ast_verbose("[Opus] Created encoder #%d (%d->opus)\n", opvt->id, sampling_rate);
+
+ return 0;
+}
+
+static int opus_decoder_construct(struct ast_trans_pvt *pvt, int sampling_rate) {
+ if(sampling_rate != 8000 && sampling_rate != 12000 && sampling_rate != 16000 && sampling_rate != 24000 && sampling_rate != 48000)
+ return -1;
+ struct opus_coder_pvt *opvt = pvt->pvt;
+ opvt->sampling_rate = sampling_rate;
+ opvt->multiplier = 48000/sampling_rate;
+ opvt->fec = USE_FEC; /* FIXME: should be triggered by chan_sip */
+ int error = 0;
+ opvt->opus = opus_decoder_create(sampling_rate, 1, &error);
+ if(error != OPUS_OK) {
+ if(opusdebug)
+ ast_verbose("[Opus] Ops! got an error creating the Opus decoder: %d (%s)\n", error, opus_strerror(error));
+ return -1;
+ }
+ opvt->id = ++decid;
+ if(opusdebug)
+ ast_verbose("[Opus] Created decoder #%d (opus->%d)\n", opvt->id, sampling_rate);
+
+ if(opusdebug > 1) {
+ char filename[50];
+ sprintf(filename, "/home/lminiero/opusdec-%04d-%d.raw", opvt->id, opvt->sampling_rate);
+ opvt->file = fopen(filename, "wb");
+ }
+
+ return 0;
+}
+
+/* Translator callbacks */
+static int lintoopus_new(struct ast_trans_pvt *pvt) {
+ return opus_encoder_construct(pvt, 8000);
+}
+
+static int lin12toopus_new(struct ast_trans_pvt *pvt) {
+ return opus_encoder_construct(pvt, 12000);
+}
+
+static int lin16toopus_new(struct ast_trans_pvt *pvt) {
+ return opus_encoder_construct(pvt, 16000);
+}
+
+static int lin24toopus_new(struct ast_trans_pvt *pvt) {
+ return opus_encoder_construct(pvt, 24000);
+}
+
+static int lin48toopus_new(struct ast_trans_pvt *pvt) {
+ return opus_encoder_construct(pvt, 48000);
+}
+
+static int opustolin_new(struct ast_trans_pvt *pvt) {
+ return opus_decoder_construct(pvt, 8000);
+}
+
+static int opustolin12_new(struct ast_trans_pvt *pvt) {
+ return opus_decoder_construct(pvt, 12000);
+}
+
+static int opustolin16_new(struct ast_trans_pvt *pvt) {
+ return opus_decoder_construct(pvt, 16000);
+}
+
+static int opustolin24_new(struct ast_trans_pvt *pvt) {
+ return opus_decoder_construct(pvt, 24000);
+}
+
+static int opustolin48_new(struct ast_trans_pvt *pvt) {
+ return opus_decoder_construct(pvt, 48000);
+}
+
+static int lintoopus_framein(struct ast_trans_pvt *pvt, struct ast_frame *f) {
+ struct opus_coder_pvt *opvt = pvt->pvt;
+
+ /* XXX We should look at how old the rest of our stream is, and if it
+ is too old, then we should overwrite it entirely, otherwise we can
+ get artifacts of earlier talk that do not belong */
+ memcpy(opvt->buf + pvt->samples, f->data.ptr, f->datalen);
+ pvt->samples += f->samples;
+
+ return 0;
+}
+
+static struct ast_frame *lintoopus_frameout(struct ast_trans_pvt *pvt) {
+ struct opus_coder_pvt *opvt = pvt->pvt;
+
+ /* We can't work on anything less than a frame in size */
+ if (pvt->samples < opvt->framesize)
+ return NULL;
+
+ int datalen = 0; /* output bytes */
+ int samples = 0; /* output samples */
+
+ while(pvt->samples >= opvt->framesize) {
+ /* Encode 160 samples (or more if it's not narrowband) */
+ if(opusdebug > 1)
+ ast_verbose("[Opus] [Encoder #%d (%d)] %d samples, %d bytes\n", opvt->id, opvt->sampling_rate, opvt->framesize, opvt->framesize*2);
+ int len = opus_encode(opvt->opus, opvt->buf, opvt->framesize, pvt->outbuf.uc, BUFFER_SAMPLES);
+ if(len < 0) {
+ if(opusdebug)
+ ast_verbose("[Opus] Ops! got an error encoding the Opus frame: %d (%s)\n", len, opus_strerror(len));
+ return NULL;
+ }
+ datalen += len;
+ samples += opvt->framesize;
+ pvt->samples -= opvt->framesize;
+ /* Move the data at the end of the buffer to the front */
+ if (pvt->samples)
+ memmove(opvt->buf, opvt->buf + samples, pvt->samples * 2);
+
+ if(opusdebug > 1)
+ ast_verbose("[Opus] [Encoder #%d (%d)] >> Got %d samples, %d bytes\n", opvt->id, opvt->sampling_rate, opvt->multiplier*samples, datalen);
+
+ if(opvt->file)
+ fwrite(opvt->buf, sizeof(int16_t), opvt->multiplier*samples, opvt->file);
+ }
+
+ /* Move the data at the end of the buffer to the front */
+ if(pvt->samples)
+ memmove(opvt->buf, opvt->buf+samples, pvt->samples*2);
+
+ return ast_trans_frameout(pvt, datalen, opvt->multiplier*samples);
+}
+
+static int opustolin_framein(struct ast_trans_pvt *pvt, struct ast_frame *f) {
+ struct opus_coder_pvt *opvt = pvt->pvt;
+ /* Decode */
+ if(opusdebug > 1)
+ ast_verbose("[Opus] [Decoder #%d (%d)] %d samples, %d bytes\n", opvt->id, opvt->sampling_rate, f->samples, f->datalen);
+ int error = opus_decode(opvt->opus, f->data.ptr, f->datalen, pvt->outbuf.i16, BUFFER_SAMPLES, opvt->fec);
+ if(error < 0) {
+ if(opusdebug)
+ ast_verbose("[Opus] Ops! got an error decoding the Opus frame: %d (%s)\n", error, opus_strerror(error));
+ return -1;
+ }
+ pvt->samples += error;
+ pvt->datalen += error*2;
+
+ if(opusdebug > 1)
+ ast_verbose("[Opus] [Decoder #%d (%d)] >> Got %d samples, %d bytes\n", opvt->id, opvt->sampling_rate, pvt->samples, pvt->datalen);
+
+ if(opvt->file)
+ fwrite(pvt->outbuf.i16, sizeof(int16_t), pvt->samples, opvt->file);
+
+ return 0;
+}
+
+static void lintoopus_destroy(struct ast_trans_pvt *arg) {
+ struct opus_coder_pvt *opvt = arg->pvt;
+ if(opvt == NULL || opvt->opus == NULL)
+ return;
+ opus_encoder_destroy(opvt->opus);
+ if(opusdebug)
+ ast_verbose("[Opus] Destroyed encoder #%d (%d->opus)\n", opvt->id, opvt->sampling_rate);
+ opvt->opus = NULL;
+
+ if(opvt->file)
+ fclose(opvt->file);
+ opvt->file = NULL;
+}
+
+static void opustolin_destroy(struct ast_trans_pvt *arg) {
+ struct opus_coder_pvt *opvt = arg->pvt;
+ if(opvt == NULL || opvt->opus == NULL)
+ return;
+ opus_decoder_destroy(opvt->opus);
+ if(opusdebug)
+ ast_verbose("[Opus] Destroyed decoder #%d (opus->%d)\n", opvt->id, opvt->sampling_rate);
+ opvt->opus = NULL;
+
+ if(opvt->file)
+ fclose(opvt->file);
+ opvt->file = NULL;
+}
+
+
+/* Translators */
+static struct ast_translator lintoopus = {
+ .name = "lintoopus",
+ .newpvt = lintoopus_new,
+ .framein = lintoopus_framein,
+ .frameout = lintoopus_frameout,
+ .destroy = lintoopus_destroy,
+ .sample = slin8_sample,
+ .desc_size = sizeof(struct opus_coder_pvt),
+ .buffer_samples = BUFFER_SAMPLES,
+ .buf_size = BUFFER_SAMPLES * 2,
+};
+
+static struct ast_translator lin12toopus = {
+ .name = "lin12toopus",
+ .newpvt = lin12toopus_new,
+ .framein = lintoopus_framein,
+ .frameout = lintoopus_frameout,
+ .destroy = lintoopus_destroy,
+ .desc_size = sizeof(struct opus_coder_pvt),
+ .buffer_samples = BUFFER_SAMPLES,
+ .buf_size = BUFFER_SAMPLES * 2,
+};
+
+static struct ast_translator lin16toopus = {
+ .name = "lin16toopus",
+ .newpvt = lin16toopus_new,
+ .framein = lintoopus_framein,
+ .frameout = lintoopus_frameout,
+ .destroy = lintoopus_destroy,
+ .sample = slin16_sample,
+ .desc_size = sizeof(struct opus_coder_pvt),
+ .buffer_samples = BUFFER_SAMPLES,
+ .buf_size = BUFFER_SAMPLES * 2,
+};
+
+static struct ast_translator lin24toopus = {
+ .name = "lin24toopus",
+ .newpvt = lin24toopus_new,
+ .framein = lintoopus_framein,
+ .frameout = lintoopus_frameout,
+ .destroy = lintoopus_destroy,
+ .desc_size = sizeof(struct opus_coder_pvt),
+ .buffer_samples = BUFFER_SAMPLES,
+ .buf_size = BUFFER_SAMPLES * 2,
+};
+
+static struct ast_translator lin48toopus = {
+ .name = "lin48toopus",
+ .newpvt = lin48toopus_new,
+ .framein = lintoopus_framein,
+ .frameout = lintoopus_frameout,
+ .destroy = lintoopus_destroy,
+ .desc_size = sizeof(struct opus_coder_pvt),
+ .buffer_samples = BUFFER_SAMPLES,
+ .buf_size = BUFFER_SAMPLES * 2,
+};
+
+static struct ast_translator opustolin = {
+ .name = "opustolin",
+ .newpvt = opustolin_new,
+ .framein = opustolin_framein,
+ .destroy = opustolin_destroy,
+ .sample = opus_sample,
+ .desc_size = sizeof(struct opus_coder_pvt),
+ .buffer_samples = BUFFER_SAMPLES,
+ .buf_size = BUFFER_SAMPLES * 2,
+ .native_plc = 1, /* FIXME: needed? */
+};
+
+static struct ast_translator opustolin12 = {
+ .name = "opustolin12",
+ .newpvt = opustolin12_new,
+ .framein = opustolin_framein,
+ .destroy = opustolin_destroy,
+ .sample = opus_sample,
+ .desc_size = sizeof(struct opus_coder_pvt),
+ .buffer_samples = BUFFER_SAMPLES,
+ .buf_size = BUFFER_SAMPLES * 2,
+ .native_plc = 1, /* FIXME: needed? */
+};
+
+static struct ast_translator opustolin16 = {
+ .name = "opustolin16",
+ .newpvt = opustolin16_new,
+ .framein = opustolin_framein,
+ .destroy = opustolin_destroy,
+ .sample = opus_sample,
+ .desc_size = sizeof(struct opus_coder_pvt),
+ .buffer_samples = BUFFER_SAMPLES,
+ .buf_size = BUFFER_SAMPLES * 2,
+ .native_plc = 1, /* FIXME: needed? */
+};
+
+static struct ast_translator opustolin24 = {
+ .name = "opustolin24",
+ .newpvt = opustolin24_new,
+ .framein = opustolin_framein,
+ .destroy = opustolin_destroy,
+ .sample = opus_sample,
+ .desc_size = sizeof(struct opus_coder_pvt),
+ .buffer_samples = BUFFER_SAMPLES,
+ .buf_size = BUFFER_SAMPLES * 2,
+ .native_plc = 1, /* FIXME: needed? */
+};
+
+static struct ast_translator opustolin48 = {
+ .name = "opustolin48",
+ .newpvt = opustolin48_new,
+ .framein = opustolin_framein,
+ .destroy = opustolin_destroy,
+ .sample = opus_sample,
+ .desc_size = sizeof(struct opus_coder_pvt),
+ .buffer_samples = BUFFER_SAMPLES,
+ .buf_size = BUFFER_SAMPLES * 2,
+ .native_plc = 1, /* FIXME: needed? */
+};
+
+
+/* Simple CLI interface to enable/disable debugging */
+static char *handle_cli_opus_set_debug(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
+{
+ switch (cmd) {
+ case CLI_INIT:
+ e->command = "opus set debug";
+ e->usage =
+ "Usage: opus set debug {status|none|normal|huge}\n"
+ " Enable/Disable Opus debugging: normal only debugs setup and errors, huge debugs every single packet\n";
+ return NULL;
+ case CLI_GENERATE:
+ return NULL;
+ }
+
+ if (a->argc != 4)
+ return CLI_SHOWUSAGE;
+
+ if (!strncasecmp(a->argv[a->argc-1], "status", 6)) {
+ ast_cli(a->fd, "Opus debugging %s\n", opusdebug > 1 ? "huge" : opusdebug > 0 ? "normal" : "none");
+ return CLI_SUCCESS;
+ }
+ if (!strncasecmp(a->argv[a->argc-1], "huge", 4))
+ opusdebug = 2;
+ else if (!strncasecmp(a->argv[a->argc-1], "normal", 6))
+ opusdebug = 1;
+ else if (!strncasecmp(a->argv[a->argc-1], "none", 4))
+ opusdebug = 0;
+ else
+ return CLI_SHOWUSAGE;
+
+ ast_cli(a->fd, "Opus debugging %s\n", opusdebug > 1 ? "huge" : opusdebug > 0 ? "normal" : "none");
+ return CLI_SUCCESS;
+}
+
+static struct ast_cli_entry cli_opus[] = {
+ AST_CLI_DEFINE(handle_cli_opus_set_debug, "Enable/Disable Opus debugging"),
+};
+
+
+/* Configuration and module setup */
+static int parse_config(int reload) {
+ // TODO
+ return 0;
+}
+
+static int reload(void) {
+ if(parse_config(1))
+ return AST_MODULE_LOAD_DECLINE;
+ return AST_MODULE_LOAD_SUCCESS;
+}
+
+static int unload_module(void) {
+ int res = 0;
+
+ res |= ast_unregister_translator(&opustolin);
+ res |= ast_unregister_translator(&lintoopus);
+ res |= ast_unregister_translator(&opustolin12);
+ res |= ast_unregister_translator(&lin12toopus);
+ res |= ast_unregister_translator(&opustolin16);
+ res |= ast_unregister_translator(&lin16toopus);
+ res |= ast_unregister_translator(&opustolin24);
+ res |= ast_unregister_translator(&lin24toopus);
+ res |= ast_unregister_translator(&opustolin48);
+ res |= ast_unregister_translator(&lin48toopus);
+
+ ast_cli_unregister_multiple(cli_opus, ARRAY_LEN(cli_opus));
+
+ return res;
+}
+
+static int load_module(void) {
+ int res = 0;
+
+ if(parse_config(0))
+ return AST_MODULE_LOAD_DECLINE;
+
+ /* 8khz (nb) */
+ ast_format_set(&opustolin.src_format, AST_FORMAT_OPUS, 0);
+ ast_format_set(&opustolin.dst_format, AST_FORMAT_SLINEAR, 0);
+ ast_format_set(&lintoopus.src_format, AST_FORMAT_SLINEAR, 0);
+ ast_format_set(&lintoopus.dst_format, AST_FORMAT_OPUS, 0);
+ /* 12khz (mb) */
+ ast_format_set(&opustolin12.src_format, AST_FORMAT_OPUS, 0);
+ ast_format_set(&opustolin12.dst_format, AST_FORMAT_SLINEAR12, 0);
+ ast_format_set(&lin12toopus.src_format, AST_FORMAT_SLINEAR12, 0);
+ ast_format_set(&lin12toopus.dst_format, AST_FORMAT_OPUS, 0);
+ /* 16khz (wb) */
+ ast_format_set(&opustolin16.src_format, AST_FORMAT_OPUS, 0);
+ ast_format_set(&opustolin16.dst_format, AST_FORMAT_SLINEAR16, 0);
+ ast_format_set(&lin16toopus.src_format, AST_FORMAT_SLINEAR16, 0);
+ ast_format_set(&lin16toopus.dst_format, AST_FORMAT_OPUS, 0);
+ /* 24khz (swb) */
+ ast_format_set(&opustolin24.src_format, AST_FORMAT_OPUS, 0);
+ ast_format_set(&opustolin24.dst_format, AST_FORMAT_SLINEAR24, 0);
+ ast_format_set(&lin24toopus.src_format, AST_FORMAT_SLINEAR24, 0);
+ ast_format_set(&lin24toopus.dst_format, AST_FORMAT_OPUS, 0);
+ /* 48khz (fb) */
+ ast_format_set(&opustolin48.src_format, AST_FORMAT_OPUS, 0);
+ ast_format_set(&opustolin48.dst_format, AST_FORMAT_SLINEAR48, 0);
+ ast_format_set(&lin48toopus.src_format, AST_FORMAT_SLINEAR48, 0);
+ ast_format_set(&lin48toopus.dst_format, AST_FORMAT_OPUS, 0);
+
+ res |= ast_register_translator(&opustolin);
+ res |= ast_register_translator(&lintoopus);
+ res |= ast_register_translator(&opustolin12);
+ res |= ast_register_translator(&lin12toopus);
+ res |= ast_register_translator(&opustolin16);
+ res |= ast_register_translator(&lin16toopus);
+ res |= ast_register_translator(&opustolin24);
+ res |= ast_register_translator(&lin24toopus);
+ res |= ast_register_translator(&opustolin48);
+ res |= ast_register_translator(&lin48toopus);
+
+ ast_cli_register_multiple(cli_opus, sizeof(cli_opus) / sizeof(struct ast_cli_entry));
+
+ return res;
+}
+
+AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_DEFAULT, "Opus Coder/Decoder",
+ .load = load_module,
+ .unload = unload_module,
+ .reload = reload,
+ );
diff -urN asterisk-11.1.2/codecs/ex_opus.h asterisk-11.1.2-opus/codecs/ex_opus.h
--- asterisk-11.1.2/codecs/ex_opus.h 1970-01-01 01:00:00.000000000 +0100
+++ asterisk-11.1.2-opus/codecs/ex_opus.h 2013-05-24 17:06:09.976176036 +0200
@@ -0,0 +1,35 @@
+/*! \file
+ * \brief 8-bit data
+ *
+ * Copyright (C) 2008, Digium, Inc.
+ *
+ * Distributed under the terms of the GNU General Public License
+ *
+ */
+
+/* Opus, a 20ms sample */
+static uint8_t ex_opus[] = {
+ 0x4b, 0x41, 0x25, 0x0b, 0xe4, 0x55, 0xc6, 0x74,
+ 0xda, 0xbb, 0x40, 0x00, 0x00, 0x00, 0x00, 0x00,
+ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+};
+
+static struct ast_frame *opus_sample(void)
+{
+ static struct ast_frame f = {
+ .frametype = AST_FRAME_VOICE,
+ .datalen = sizeof(ex_opus),
+ .samples = 960, //ARRAY_LEN(ex_opus),
+ .mallocd = 0,
+ .offset = 0,
+ .src = __PRETTY_FUNCTION__,
+ .data.ptr = ex_opus,
+ };
+
+ ast_format_set(&f.subclass.format, AST_FORMAT_OPUS, 0);
+
+ return &f;
+}
diff -urN asterisk-11.1.2/configure.ac asterisk-11.1.2-opus/configure.ac
--- asterisk-11.1.2/configure.ac 2012-10-18 22:02:02.000000000 +0200
+++ asterisk-11.1.2-opus/configure.ac 2013-05-24 16:25:25.766936899 +0200
@@ -422,6 +422,7 @@
AST_EXT_LIB_SETUP([NEWT], [newt], [newt])
AST_EXT_LIB_SETUP([OGG], [OGG], [ogg])
AST_EXT_LIB_SETUP([OPENR2], [MFR2], [openr2])
+AST_EXT_LIB_SETUP([OPUS], [Opus], [opus])
AST_EXT_LIB_SETUP([OSPTK], [OSP Toolkit], [osptk])
AST_EXT_LIB_SETUP([OSS], [Open Sound System], [oss])
AST_EXT_LIB_SETUP([PGSQL], [PostgreSQL], [postgres])
@@ -2086,6 +2087,8 @@
AC_SUBST(PBX_SPEEX_PREPROCESS)
+AST_EXT_LIB_CHECK([OPUS], [opus], [opus_encoder_create], [opus/opus.h])
+
AST_EXT_LIB_CHECK([SQLITE], [sqlite], [sqlite_exec], [sqlite.h])
AST_EXT_LIB_CHECK([SQLITE3], [sqlite3], [sqlite3_open], [sqlite3.h], [${PTHREAD_LIBS}], [${PTHREAD_CFLAGS}])
diff -urN asterisk-11.1.2/formats/format_vp8.c asterisk-11.1.2-opus/formats/format_vp8.c
--- asterisk-11.1.2/formats/format_vp8.c 1970-01-01 01:00:00.000000000 +0100
+++ asterisk-11.1.2-opus/formats/format_vp8.c 2013-05-24 17:01:31.070470091 +0200
@@ -0,0 +1,195 @@
+/*
+ * Asterisk -- An open source telephony toolkit.
+ *
+ * Copyright (C) 1999 - 2005, Digium, Inc.
+ *
+ * Mark Spencer <markster@digium.com>
+ *
+ * See http://www.asterisk.org for more information about
+ * the Asterisk project. Please do not directly contact
+ * any of the maintainers of this project for assistance;
+ * the project provides a web site, mailing lists and IRC
+ * channels for your use.
+ *
+ * This program is free software, distributed under the terms of
+ * the GNU General Public License Version 2. See the LICENSE file
+ * at the top of the source tree.
+ */
+
+/*! \file
+ *
+ * \brief Save to raw, headerless VP8 data.
+ *
+ * \author Lorenzo Miniero <lorenzo@meetecho.com>
+ *
+ * \note Basically a "clone" of the H.264 passthrough format
+ *
+ * \arg File name extension: VP8
+ * \ingroup formats
+ * \arg See \ref AstVideo
+ */
+
+/*** MODULEINFO
+ <support_level>core</support_level>
+ ***/
+
+#include "asterisk.h"
+
+ASTERISK_FILE_VERSION(__FILE__, "$Revision: 328209 $")
+
+#include "asterisk/mod_format.h"
+#include "asterisk/module.h"
+#include "asterisk/endian.h"
+
+/* VP8 passthrough */
+
+#define BUF_SIZE 4096
+struct vp8_desc {
+ unsigned int lastts;
+};
+
+static int vp8_open(struct ast_filestream *s)
+{
+ unsigned int ts;
+ if (fread(&ts, 1, sizeof(ts), s->f) < sizeof(ts)) {
+ ast_log(LOG_WARNING, "Empty file!\n");
+ return -1;
+ }
+ return 0;
+}
+
+static struct ast_frame *vp8_read(struct ast_filestream *s, int *whennext)
+{
+ int res;
+ int mark = 0;
+ unsigned short len;
+ unsigned int ts;
+ struct vp8_desc *fs = (struct vp8_desc *)s->_private;
+
+ /* Send a frame from the file to the appropriate channel */
+ if ((res = fread(&len, 1, sizeof(len), s->f)) < 1)
+ return NULL;
+ len = ntohs(len);
+ mark = (len & 0x8000) ? 1 : 0;
+ len &= 0x7fff;
+ if (len > BUF_SIZE) {
+ ast_log(LOG_WARNING, "Length %d is too long\n", len);
+ len = BUF_SIZE; /* XXX truncate */
+ }
+ s->fr.frametype = AST_FRAME_VIDEO;
+ ast_format_set(&s->fr.subclass.format, AST_FORMAT_VP8, 0);
+ s->fr.mallocd = 0;
+ AST_FRAME_SET_BUFFER(&s->fr, s->buf, AST_FRIENDLY_OFFSET, len);
+ if ((res = fread(s->fr.data.ptr, 1, s->fr.datalen, s->f)) != s->fr.datalen) {
+ if (res)
+ ast_log(LOG_WARNING, "Short read (%d of %d) (%s)!\n", res, len, strerror(errno));
+ return NULL;
+ }
+ s->fr.samples = fs->lastts;
+ s->fr.datalen = len;
+ if (mark) {
+ ast_format_set_video_mark(&s->fr.subclass.format);
+ }
+ s->fr.delivery.tv_sec = 0;
+ s->fr.delivery.tv_usec = 0;
+ if ((res = fread(&ts, 1, sizeof(ts), s->f)) == sizeof(ts)) {
+ fs->lastts = ntohl(ts);
+ *whennext = fs->lastts * 4/45;
+ } else
+ *whennext = 0;
+ return &s->fr;
+}
+
+static int vp8_write(struct ast_filestream *s, struct ast_frame *f)
+{
+ int res;
+ unsigned int ts;
+ unsigned short len;
+ int mark;
+
+ if (f->frametype != AST_FRAME_VIDEO) {
+ ast_log(LOG_WARNING, "Asked to write non-video frame!\n");
+ return -1;
+ }
+ mark = ast_format_get_video_mark(&f->subclass.format) ? 0x8000 : 0;
+ if (f->subclass.format.id != AST_FORMAT_VP8) {
+ ast_log(LOG_WARNING, "Asked to write non-VP8 frame (%s)!\n", ast_getformatname(&f->subclass.format));
+ return -1;
+ }
+ ts = htonl(f->samples);
+ if ((res = fwrite(&ts, 1, sizeof(ts), s->f)) != sizeof(ts)) {
+ ast_log(LOG_WARNING, "Bad write (%d/4): %s\n", res, strerror(errno));
+ return -1;
+ }
+ len = htons(f->datalen | mark);
+ if ((res = fwrite(&len, 1, sizeof(len), s->f)) != sizeof(len)) {
+ ast_log(LOG_WARNING, "Bad write (%d/2): %s\n", res, strerror(errno));
+ return -1;
+ }
+ if ((res = fwrite(f->data.ptr, 1, f->datalen, s->f)) != f->datalen) {
+ ast_log(LOG_WARNING, "Bad write (%d/%d): %s\n", res, f->datalen, strerror(errno));
+ return -1;
+ }
+ return 0;
+}
+
+static int vp8_seek(struct ast_filestream *fs, off_t sample_offset, int whence)
+{
+ /* No way Jose */
+ return -1;
+}
+
+static int vp8_trunc(struct ast_filestream *fs)
+{
+ int fd;
+ off_t cur;
+
+ if ((fd = fileno(fs->f)) < 0) {
+ ast_log(AST_LOG_WARNING, "Unable to determine file descriptor for VP8 filestream %p: %s\n", fs, strerror(errno));
+ return -1;
+ }
+ if ((cur = ftello(fs->f)) < 0) {
+ ast_log(AST_LOG_WARNING, "Unable to determine current position in VP8 filestream %p: %s\n", fs, strerror(errno));
+ return -1;
+ }
+ /* Truncate file to current length */
+ return ftruncate(fd, cur);
+}
+
+static off_t vp8_tell(struct ast_filestream *fs)
+{
+ off_t offset = ftell(fs->f);
+ return offset; /* XXX totally bogus, needs fixing */
+}
+
+static struct ast_format_def vp8_f = {
+ .name = "VP8",
+ .exts = "vp8",
+ .open = vp8_open,
+ .write = vp8_write,
+ .seek = vp8_seek,
+ .trunc = vp8_trunc,
+ .tell = vp8_tell,
+ .read = vp8_read,
+ .buf_size = BUF_SIZE + AST_FRIENDLY_OFFSET,
+ .desc_size = sizeof(struct vp8_desc),
+};
+
+static int load_module(void)
+{
+ ast_format_set(&vp8_f.format, AST_FORMAT_VP8, 0);
+ if (ast_format_def_register(&vp8_f))
+ return AST_MODULE_LOAD_FAILURE;
+ return AST_MODULE_LOAD_SUCCESS;
+}
+
+static int unload_module(void)
+{
+ return ast_format_def_unregister(vp8_f.name);
+}
+
+AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_LOAD_ORDER, "Raw VP8 data",
+ .load = load_module,
+ .unload = unload_module,
+ .load_pri = AST_MODPRI_APP_DEPEND
+);
diff -urN asterisk-11.1.2/include/asterisk/format.h asterisk-11.1.2-opus/include/asterisk/format.h
--- asterisk-11.1.2/include/asterisk/format.h 2012-07-13 20:41:07.000000000 +0200
+++ asterisk-11.1.2-opus/include/asterisk/format.h 2013-05-24 16:42:59.833669803 +0200
@@ -101,6 +101,8 @@
AST_FORMAT_SLINEAR192 = 27 + AST_FORMAT_TYPE_AUDIO,
AST_FORMAT_SPEEX32 = 28 + AST_FORMAT_TYPE_AUDIO,
AST_FORMAT_CELT = 29 + AST_FORMAT_TYPE_AUDIO,
+ /*! Opus */
+ AST_FORMAT_OPUS = 30 + AST_FORMAT_TYPE_AUDIO,
/*! H.261 Video */
AST_FORMAT_H261 = 1 + AST_FORMAT_TYPE_VIDEO,
@@ -112,6 +114,8 @@
AST_FORMAT_H264 = 4 + AST_FORMAT_TYPE_VIDEO,
/*! MPEG4 Video */
AST_FORMAT_MP4_VIDEO = 5 + AST_FORMAT_TYPE_VIDEO,
+ /*! VP8 */
+ AST_FORMAT_VP8 = 6 + AST_FORMAT_TYPE_VIDEO,
/*! JPEG Images */
AST_FORMAT_JPEG = 1 + AST_FORMAT_TYPE_IMAGE,
diff -urN asterisk-11.1.2/main/channel.c asterisk-11.1.2-opus/main/channel.c
--- asterisk-11.1.2/main/channel.c 2013-01-02 20:23:44.000000000 +0100
+++ asterisk-11.1.2-opus/main/channel.c 2013-05-24 16:41:27.279429674 +0200
@@ -909,6 +909,8 @@
AST_FORMAT_SPEEX32,
AST_FORMAT_SPEEX16,
AST_FORMAT_SPEEX,
+ /*! Opus */
+ AST_FORMAT_OPUS,
/*! SILK is pretty awesome. */
AST_FORMAT_SILK,
/*! CELT supports crazy high sample rates */
diff -urN asterisk-11.1.2/main/format.c asterisk-11.1.2-opus/main/format.c
--- asterisk-11.1.2/main/format.c 2012-10-02 03:27:19.000000000 +0200
+++ asterisk-11.1.2-opus/main/format.c 2013-06-04 17:20:35.935378513 +0200
@@ -430,6 +430,9 @@
/*! SpeeX Wideband (16kHz) Free Compression */
case AST_FORMAT_SPEEX16:
return (1ULL << 33);
+ /*! Opus audio (8kHz, 16kHz, 24kHz, 48Khz) */
+ case AST_FORMAT_OPUS:
+ return (1ULL << 34);
/*! Raw mu-law data (G.711) */
case AST_FORMAT_TESTLAW:
return (1ULL << 47);
@@ -449,6 +452,9 @@
/*! MPEG4 Video */
case AST_FORMAT_MP4_VIDEO:
return (1ULL << 22);
+ /*! VP8 Video */
+ case AST_FORMAT_VP8: