Skip to content

Commit

Permalink
test: add mock for audio codec
Browse files Browse the repository at this point in the history
  • Loading branch information
cspiel1 committed May 29, 2024
1 parent cfeb5e3 commit 285a05a
Show file tree
Hide file tree
Showing 3 changed files with 96 additions and 0 deletions.
1 change: 1 addition & 0 deletions test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ add_executable(${PROJECT_NAME}

mock/cert.c

mock/mock_aucodec.c
mock/mock_auplay.c
mock/mock_mnat.c
mock/mock_vidcodec.c
Expand Down
93 changes: 93 additions & 0 deletions test/mock/mock_aucodec.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
/**
* @file mock_aucodec.c mockup for audio codec with float support
*
* Copyright (C) 2024 commend.com - Christian Spielberger
*/

#include <re.h>
#include <rem.h>
#include <baresip.h>
#include "../test.h"



static int encode(struct auenc_state *aes, bool *marker, uint8_t *buf,
size_t *len, int fmt, const void *sampv, size_t sampc)
{
const uint8_t *p = sampv;
size_t sz = aufmt_sample_size(fmt);
size_t psize = sampc * sz;

(void)aes;
(void)marker;

if (!buf || !len || !sampv)
return EINVAL;

if (*len < psize)
return ENOMEM;

*len = psize;

while (sampc--) {
/* raw format */
memcpy(buf, p, sz);

buf += sz;
p += sz;
}

return 0;
}


static int decode(struct audec_state *ads, int fmt, void *sampv,
size_t *sampc, bool marker,
const uint8_t *buf, size_t len)
{
uint8_t *p = sampv;
size_t sz = aufmt_sample_size(fmt);

(void)ads;
(void)marker;

if (!sampv || !sampc || !buf || !len)
return EINVAL;

if (*sampc < len)
return ENOMEM;

*sampc = len/sz;

while (len) {
memcpy(p, buf, sz);
buf += sz;
p += sz;
len -= sz;
}

return 0;
}


static struct aucodec aucmock = {
.name = "aucmock",
.srate = 48000,
.crate = 48000,
.ch = 2,
.pch = 2,
.ench = encode,
.dech = decode,
};


void mock_aucodec_register(void)
{
aucodec_register(baresip_aucodecl(), &aucmock);
}


void mock_aucodec_unregister(void)
{
aucodec_unregister(&aucmock);
}
2 changes: 2 additions & 0 deletions test/test.h
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,8 @@ struct auplay;

typedef void (mock_sample_h)(struct auframe *af, const char *dev, void *arg);

void mock_aucodec_register(void);
void mock_aucodec_unregister(void);
int mock_auplay_register(struct auplay **auplayp, struct list *auplayl,
mock_sample_h *sampleh, void *arg);

Expand Down

0 comments on commit 285a05a

Please sign in to comment.