1 /**
2  * @file mock/mock_aucodec.c Mock audio codec
3  *
4  * Copyright (C) 2010 - 2016 Creytiv.com
5  */
6 
7 #include <string.h>
8 #include <re.h>
9 #include <rem.h>
10 #include <baresip.h>
11 #include "../test.h"
12 
13 
14 /* A dummy protocol header */
15 #define L16_HEADER 0x1616
16 
17 
mock_l16_encode(struct auenc_state * st,uint8_t * buf,size_t * len,int fmt,const void * sampv_void,size_t sampc)18 static int mock_l16_encode(struct auenc_state *st, uint8_t *buf, size_t *len,
19 			   int fmt, const void *sampv_void, size_t sampc)
20 {
21 	int16_t *p = (void *)buf;
22 	const int16_t *sampv = sampv_void;
23 	(void)st;
24 
25 	if (!buf || !len || !sampv)
26 		return EINVAL;
27 
28 	if (*len < sampc*2)
29 		return ENOMEM;
30 
31 	if (fmt != AUFMT_S16LE)
32 		return ENOTSUP;
33 
34 	*len = 2 + sampc*2;
35 
36 	*p++ = L16_HEADER;
37 
38 	while (sampc--)
39 		*p++ = htons(*sampv++);
40 
41 	return 0;
42 }
43 
44 
mock_l16_decode(struct audec_state * st,int fmt,void * sampv_void,size_t * sampc,const uint8_t * buf,size_t len)45 static int mock_l16_decode(struct audec_state *st,
46 			   int fmt, void *sampv_void, size_t *sampc,
47 			   const uint8_t *buf, size_t len)
48 {
49 	int16_t *p = (void *)buf;
50 	int16_t *sampv = sampv_void;
51 	uint16_t hdr;
52 	(void)st;
53 
54 	if (!buf || !len || !sampv)
55 		return EINVAL;
56 
57 	if (len < 2)
58 		return EINVAL;
59 
60 	if (*sampc < len/2)
61 		return ENOMEM;
62 
63 	if (fmt != AUFMT_S16LE)
64 		return ENOTSUP;
65 
66 	*sampc = (len - 2)/2;
67 
68 	hdr = *p++;
69 	if (L16_HEADER != hdr) {
70 		warning("mock_aucodec: invalid L16 header"
71 			" 0x%04x (len=%zu)\n", hdr, len);
72 		return EPROTO;
73 	}
74 
75 	len = len/2 - 2;
76 	while (len--)
77 		*sampv++ = ntohs(*p++);
78 
79 	return 0;
80 }
81 
82 
83 static struct aucodec ac_dummy = {
84 	.name = "FOO16",
85 	.srate = 8000,
86 	.crate = 8000,
87 	.ch = 1,
88 	.ench = mock_l16_encode,
89 	.dech = mock_l16_decode,
90 };
91 
92 
mock_aucodec_register(void)93 void mock_aucodec_register(void)
94 {
95 	aucodec_register(baresip_aucodecl(), &ac_dummy);
96 }
97 
98 
mock_aucodec_unregister(void)99 void mock_aucodec_unregister(void)
100 {
101 	aucodec_unregister(&ac_dummy);
102 }
103