more slinfactory structure definition to inside implementation module

make read/write/hold work on samples, not bytes
add an API call to find out how many samples are available in a slinfactory


git-svn-id: https://origsvn.digium.com/svn/asterisk/trunk@38418 65c4cc65-6c06-0410-ace0-fbb531ad65f3
1.4
Kevin P. Fleming 19 years ago
parent a8b85fda84
commit d079b1c49f

@ -27,27 +27,17 @@
#include <unistd.h> #include <unistd.h>
#include <string.h> #include <string.h>
#if defined(__cplusplus) || defined(c_plusplus) #if defined(__cplusplus) || defined(c_plusplus)
extern "C" { extern "C" {
#endif #endif
struct ast_slinfactory { struct ast_slinfactory;
struct ast_frame *queue;
struct ast_trans_pvt *trans;
short hold[1280];
short *offset;
size_t holdlen;
int size;
int format;
};
void ast_slinfactory_init(struct ast_slinfactory *sf); void ast_slinfactory_init(struct ast_slinfactory *sf);
void ast_slinfactory_destroy(struct ast_slinfactory *sf); void ast_slinfactory_destroy(struct ast_slinfactory *sf);
int ast_slinfactory_feed(struct ast_slinfactory *sf, struct ast_frame *f); int ast_slinfactory_feed(struct ast_slinfactory *sf, struct ast_frame *f);
int ast_slinfactory_read(struct ast_slinfactory *sf, short *buf, size_t bytes); int ast_slinfactory_read(struct ast_slinfactory *sf, short *buf, size_t samples);
unsigned int ast_slinfactory_available(const struct ast_slinfactory *sf);
#if defined(__cplusplus) || defined(c_plusplus) #if defined(__cplusplus) || defined(c_plusplus)
} }

@ -30,14 +30,24 @@ ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
#include <string.h> #include <string.h>
#include "asterisk/frame.h"
#include "asterisk/slinfactory.h" #include "asterisk/slinfactory.h"
#include "asterisk/logger.h" #include "asterisk/logger.h"
#include "asterisk/translate.h" #include "asterisk/translate.h"
struct ast_slinfactory {
struct ast_frame *queue;
struct ast_trans_pvt *trans;
short hold[1280];
short *offset;
size_t holdlen; /*! in samples */
unsigned int size; /*! in samples */
unsigned int format;
};
void ast_slinfactory_init(struct ast_slinfactory *sf) void ast_slinfactory_init(struct ast_slinfactory *sf)
{ {
memset(sf, 0, sizeof(struct ast_slinfactory)); memset(sf, 0, sizeof(*sf));
sf->offset = sf->hold; sf->offset = sf->hold;
sf->queue = NULL; sf->queue = NULL;
} }
@ -51,7 +61,7 @@ void ast_slinfactory_destroy(struct ast_slinfactory *sf)
sf->trans = NULL; sf->trans = NULL;
} }
while((f = sf->queue)) { while ((f = sf->queue)) {
sf->queue = f->next; sf->queue = f->next;
ast_frfree(f); ast_frfree(f);
} }
@ -60,10 +70,7 @@ void ast_slinfactory_destroy(struct ast_slinfactory *sf)
int ast_slinfactory_feed(struct ast_slinfactory *sf, struct ast_frame *f) int ast_slinfactory_feed(struct ast_slinfactory *sf, struct ast_frame *f)
{ {
struct ast_frame *frame, *frame_ptr; struct ast_frame *frame, *frame_ptr;
unsigned int x;
if (!f) {
return 0;
}
if (f->subclass != AST_FORMAT_SLINEAR) { if (f->subclass != AST_FORMAT_SLINEAR) {
if (sf->trans && f->subclass != sf->format) { if (sf->trans && f->subclass != sf->format) {
@ -80,52 +87,49 @@ int ast_slinfactory_feed(struct ast_slinfactory *sf, struct ast_frame *f)
} }
} }
if (sf->trans) { if (sf->trans)
frame = ast_translate(sf->trans, f, 0); frame = ast_translate(sf->trans, f, 0);
} else { else
frame = ast_frdup(f); frame = ast_frdup(f);
}
if (frame) { if (!frame)
int x = 0; return 0;
for (frame_ptr = sf->queue; frame_ptr && frame_ptr->next; frame_ptr = frame_ptr->next) {
x++; for (x = 0, frame_ptr = sf->queue; frame_ptr && frame_ptr->next; frame_ptr = frame_ptr->next)
} x++;
if (frame_ptr) {
frame_ptr->next = frame; if (frame_ptr)
} else { frame_ptr->next = frame;
sf->queue = frame; else
} sf->queue = frame;
frame->next = NULL;
sf->size += frame->datalen;
return x;
}
return 0; frame->next = NULL;
sf->size += frame->samples;
return x;
} }
int ast_slinfactory_read(struct ast_slinfactory *sf, short *buf, size_t bytes) int ast_slinfactory_read(struct ast_slinfactory *sf, short *buf, size_t samples)
{ {
struct ast_frame *frame_ptr; struct ast_frame *frame_ptr;
int sofar = 0, ineed, remain; unsigned int sofar = 0, ineed, remain;
short *frame_data, *offset = buf; short *frame_data, *offset = buf;
while (sofar < bytes) { while (sofar < samples) {
ineed = bytes - sofar; ineed = samples - sofar;
if (sf->holdlen) { if (sf->holdlen) {
if ((sofar + sf->holdlen) <= ineed) { if ((sofar + sf->holdlen) <= ineed) {
memcpy(offset, sf->hold, sf->holdlen); memcpy(offset, sf->hold, sf->holdlen * sizeof(*offset));
sofar += sf->holdlen; sofar += sf->holdlen;
offset += (sf->holdlen / sizeof(short)); offset += sf->holdlen;
sf->holdlen = 0; sf->holdlen = 0;
sf->offset = sf->hold; sf->offset = sf->hold;
} else { } else {
remain = sf->holdlen - ineed; remain = sf->holdlen - ineed;
memcpy(offset, sf->offset, ineed); memcpy(offset, sf->offset, ineed * sizeof(*offset));
sofar += ineed; sofar += ineed;
sf->offset += (ineed / sizeof(short)); sf->offset += ineed;
sf->holdlen = remain; sf->holdlen = remain;
} }
continue; continue;
@ -135,16 +139,16 @@ int ast_slinfactory_read(struct ast_slinfactory *sf, short *buf, size_t bytes)
sf->queue = frame_ptr->next; sf->queue = frame_ptr->next;
frame_data = frame_ptr->data; frame_data = frame_ptr->data;
if ((sofar + frame_ptr->datalen) <= ineed) { if ((sofar + frame_ptr->samples) <= ineed) {
memcpy(offset, frame_data, frame_ptr->datalen); memcpy(offset, frame_data, frame_ptr->samples * sizeof(*offset));
sofar += frame_ptr->datalen; sofar += frame_ptr->samples;
offset += (frame_ptr->datalen / sizeof(short)); offset += frame_ptr->samples;
} else { } else {
remain = frame_ptr->datalen - ineed; remain = frame_ptr->samples - ineed;
memcpy(offset, frame_data, ineed); memcpy(offset, frame_data, ineed * sizeof(*offset));
sofar += ineed; sofar += ineed;
frame_data += (ineed / sizeof(short)); frame_data += ineed;
memcpy(sf->hold, frame_data, remain); memcpy(sf->hold, frame_data, remain * sizeof(*offset));
sf->holdlen = remain; sf->holdlen = remain;
} }
ast_frfree(frame_ptr); ast_frfree(frame_ptr);
@ -156,3 +160,8 @@ int ast_slinfactory_read(struct ast_slinfactory *sf, short *buf, size_t bytes)
sf->size -= sofar; sf->size -= sofar;
return sofar; return sofar;
} }
unsigned int ast_slinfactory_available(const struct ast_slinfactory *sf)
{
return sf->size;
}

Loading…
Cancel
Save