Be safer and patch codec_builtin.c
Sean Bright
9 years ago
0 | diff -urN a/main/Makefile b/main/Makefile | |
1 | --- a/main/Makefile | |
2 | +++ b/main/Makefile | |
3 | @@ -40,6 +40,7 @@ | |
4 | AST_LIBS+=$(URIPARSER_LIB) | |
5 | AST_LIBS+=$(UUID_LIB) | |
6 | AST_LIBS+=$(CRYPT_LIB) | |
7 | +AST_LIBS+=$(OPUS_LIB) | |
8 | ||
9 | ifneq ($(findstring $(OSARCH), linux-gnu uclinux linux-uclibc kfreebsd-gnu),) | |
10 | ifneq ($(findstring LOADABLE_MODULES,$(MENUSELECT_CFLAGS)),) | |
11 | @@ -159,6 +160,7 @@ | |
12 | bucket.o: _ASTCFLAGS+=$(URIPARSER_INCLUDE) | |
13 | crypt.o: _ASTCFLAGS+=$(CRYPT_INCLUDE) | |
14 | uuid.o: _ASTCFLAGS+=$(UUID_INCLUDE) | |
15 | +codec_builtin.o: _ASTCFLAGS+=$(OPUS_INCLUDE) | |
16 | ||
17 | ifneq ($(findstring ENABLE_UPLOADS,$(MENUSELECT_CFLAGS)),) | |
18 | http.o: _ASTCFLAGS+=$(GMIME_INCLUDE) | |
19 | diff -urN a/main/codec_builtin.c b/main/codec_builtin.c | |
20 | --- a/main/codec_builtin.c | |
21 | +++ b/main/codec_builtin.c | |
22 | @@ -38,6 +38,8 @@ | |
23 | #include "asterisk/format_cache.h" | |
24 | #include "asterisk/frame.h" | |
25 | ||
26 | +#include <opus/opus.h> | |
27 | + | |
28 | enum frame_type { | |
29 | TYPE_HIGH, /* 0x0 */ | |
30 | TYPE_LOW, /* 0x1 */ | |
31 | @@ -698,6 +700,11 @@ | |
32 | .get_length = g719_length, | |
33 | }; | |
34 | ||
35 | +static int opus_samples(struct ast_frame *frame) | |
36 | +{ | |
37 | + return opus_packet_get_nb_samples(frame->data.ptr, frame->datalen, 48000); | |
38 | +} | |
39 | + | |
40 | static struct ast_codec opus = { | |
41 | .name = "opus", | |
42 | .description = "Opus Codec", | |
43 | @@ -707,6 +714,7 @@ | |
44 | .maximum_ms = 60, | |
45 | .default_ms = 20, | |
46 | .minimum_bytes = 10, | |
47 | + .samples_count = opus_samples, | |
48 | }; | |
49 | ||
50 | static struct ast_codec jpeg = { | |
0 | 51 | diff -urN a/res/res_format_attr_opus.c b/res/res_format_attr_opus.c |
1 | 52 | --- a/res/res_format_attr_opus.c |
2 | 53 | +++ b/res/res_format_attr_opus.c |
513 | 513 | return AST_MODULE_LOAD_SUCCESS; |
514 | 514 | } |
515 | 515 | |
516 | static int opus_samples_count(struct ast_frame *frame) | |
517 | { | |
518 | return opus_packet_get_nb_samples(frame->data.ptr, frame->datalen, 48000); | |
519 | } | |
520 | ||
521 | static int (*save_samples_count)(struct ast_frame *); | |
522 | ||
523 | static int override_opus_samples_count(void) | |
524 | { | |
525 | struct ast_codec *codec; | |
526 | ||
527 | if (!(codec = ast_codec_get("opus", AST_MEDIA_TYPE_AUDIO, 48000))) { | |
528 | return 1; | |
529 | } | |
530 | ||
531 | save_samples_count = codec->samples_count; | |
532 | codec->samples_count = opus_samples_count; | |
533 | ||
534 | ao2_ref(codec, -1); | |
535 | ||
536 | return 0; | |
537 | } | |
538 | ||
539 | static int restore_opus_samples_count(void) | |
540 | { | |
541 | struct ast_codec *codec; | |
542 | ||
543 | if (!(codec = ast_codec_get("opus", AST_MEDIA_TYPE_AUDIO, 48000))) { | |
544 | return 1; | |
545 | } | |
546 | ||
547 | codec->samples_count = save_samples_count; | |
548 | ||
549 | ao2_ref(codec, -1); | |
550 | ||
551 | return 0; | |
552 | } | |
553 | ||
554 | 516 | static int unload_module(void) |
555 | 517 | { |
556 | 518 | int res = 0; |
557 | ||
558 | res |= restore_opus_samples_count(); | |
559 | 519 | |
560 | 520 | res |= ast_unregister_translator(&opustolin); |
561 | 521 | res |= ast_unregister_translator(&lintoopus); |
579 | 539 | return AST_MODULE_LOAD_DECLINE; |
580 | 540 | } |
581 | 541 | |
582 | if (override_opus_samples_count()) { | |
583 | return AST_MODULE_LOAD_DECLINE; | |
584 | } | |
585 | ||
586 | 542 | res |= ast_register_translator(&opustolin); |
587 | 543 | res |= ast_register_translator(&lintoopus); |
588 | 544 | res |= ast_register_translator(&opustolin12); |