2006-06-23 16:33:17 +00:00
|
|
|
/*
|
|
|
|
* Asterisk -- An open source telephony toolkit.
|
|
|
|
*
|
2008-06-12 17:27:55 +00:00
|
|
|
* DAHDI native transcoding support
|
2006-06-23 16:33:17 +00:00
|
|
|
*
|
|
|
|
* Copyright (C) 1999 - 2006, Digium, Inc.
|
|
|
|
*
|
|
|
|
* Mark Spencer <markster@digium.com>
|
|
|
|
* Kevin P. Fleming <kpfleming@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
|
|
|
|
*
|
2008-06-12 17:27:55 +00:00
|
|
|
* \brief Translate between various formats natively through DAHDI transcoding
|
2006-06-23 16:33:17 +00:00
|
|
|
*
|
|
|
|
* \ingroup codecs
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*** MODULEINFO
|
2008-06-12 17:27:55 +00:00
|
|
|
<depend>dahdi</depend>
|
2006-06-23 16:33:17 +00:00
|
|
|
***/
|
|
|
|
|
|
|
|
#include "asterisk.h"
|
|
|
|
|
|
|
|
ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
|
|
|
|
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <netinet/in.h>
|
|
|
|
#include <sys/ioctl.h>
|
|
|
|
#include <sys/mman.h>
|
|
|
|
|
2008-06-25 23:05:28 +00:00
|
|
|
#include <dahdi/user.h>
|
|
|
|
|
2006-06-23 16:33:17 +00:00
|
|
|
#include "asterisk/lock.h"
|
|
|
|
#include "asterisk/translate.h"
|
|
|
|
#include "asterisk/config.h"
|
|
|
|
#include "asterisk/module.h"
|
2007-08-31 15:58:31 +00:00
|
|
|
#include "asterisk/cli.h"
|
2006-06-23 16:33:17 +00:00
|
|
|
#include "asterisk/channel.h"
|
|
|
|
#include "asterisk/utils.h"
|
|
|
|
#include "asterisk/linkedlists.h"
|
|
|
|
|
|
|
|
#define BUFFER_SAMPLES 8000
|
|
|
|
|
|
|
|
static unsigned int global_useplc = 0;
|
2006-12-11 21:04:41 +00:00
|
|
|
|
2007-08-31 15:58:31 +00:00
|
|
|
static struct channel_usage {
|
|
|
|
int total;
|
|
|
|
int encoders;
|
|
|
|
int decoders;
|
|
|
|
} channels;
|
|
|
|
|
2007-10-19 18:01:00 +00:00
|
|
|
static char *handle_cli_transcoder_show(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a);
|
2007-08-31 15:58:31 +00:00
|
|
|
|
|
|
|
static struct ast_cli_entry cli[] = {
|
2008-06-12 17:27:55 +00:00
|
|
|
AST_CLI_DEFINE(handle_cli_transcoder_show, "Display DAHDI transcoder utilization.")
|
2007-08-31 15:58:31 +00:00
|
|
|
};
|
|
|
|
|
2006-06-23 16:33:17 +00:00
|
|
|
struct format_map {
|
|
|
|
unsigned int map[32][32];
|
|
|
|
};
|
|
|
|
|
|
|
|
static struct format_map global_format_map = { { { 0 } } };
|
|
|
|
|
|
|
|
struct translator {
|
|
|
|
struct ast_translator t;
|
|
|
|
AST_LIST_ENTRY(translator) entry;
|
|
|
|
};
|
|
|
|
|
2007-03-06 22:15:02 +00:00
|
|
|
static AST_LIST_HEAD_STATIC(translators, translator);
|
2006-06-23 16:33:17 +00:00
|
|
|
|
|
|
|
struct pvt {
|
|
|
|
int fd;
|
|
|
|
int fake;
|
2006-08-30 03:20:54 +00:00
|
|
|
#ifdef DEBUG_TRANSCODE
|
|
|
|
int totalms;
|
|
|
|
int lasttotalms;
|
|
|
|
#endif
|
2008-06-12 17:27:55 +00:00
|
|
|
struct dahdi_transcode_header *hdr;
|
2006-06-23 16:33:17 +00:00
|
|
|
};
|
|
|
|
|
2007-10-19 18:01:00 +00:00
|
|
|
static char *handle_cli_transcoder_show(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
|
2007-08-31 15:58:31 +00:00
|
|
|
{
|
|
|
|
struct channel_usage copy;
|
|
|
|
|
2007-10-19 18:01:00 +00:00
|
|
|
switch (cmd) {
|
|
|
|
case CLI_INIT:
|
|
|
|
e->command = "transcoder show";
|
|
|
|
e->usage =
|
|
|
|
"Usage: transcoder show\n"
|
2008-06-12 17:27:55 +00:00
|
|
|
" Displays channel utilization of DAHDI transcoder(s).\n";
|
2007-10-19 18:01:00 +00:00
|
|
|
return NULL;
|
|
|
|
case CLI_GENERATE:
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (a->argc != 2)
|
|
|
|
return CLI_SHOWUSAGE;
|
|
|
|
|
2007-08-31 15:58:31 +00:00
|
|
|
copy = channels;
|
|
|
|
|
|
|
|
if (copy.total == 0)
|
2008-06-12 17:27:55 +00:00
|
|
|
ast_cli(a->fd, "No DAHDI transcoders found.\n");
|
2007-08-31 15:58:31 +00:00
|
|
|
else
|
2007-10-19 18:01:00 +00:00
|
|
|
ast_cli(a->fd, "%d/%d encoders/decoders of %d channels are in use.\n", copy.encoders, copy.decoders, copy.total);
|
2007-08-31 15:58:31 +00:00
|
|
|
|
2007-10-19 18:01:00 +00:00
|
|
|
return CLI_SUCCESS;
|
2007-08-31 15:58:31 +00:00
|
|
|
}
|
|
|
|
|
2008-06-12 17:27:55 +00:00
|
|
|
static int dahdi_framein(struct ast_trans_pvt *pvt, struct ast_frame *f)
|
2006-06-23 16:33:17 +00:00
|
|
|
{
|
2008-06-12 17:27:55 +00:00
|
|
|
struct pvt *dahdip = pvt->pvt;
|
|
|
|
struct dahdi_transcode_header *hdr = dahdip->hdr;
|
2006-06-23 16:33:17 +00:00
|
|
|
|
|
|
|
if (!f->subclass) {
|
|
|
|
/* Fake a return frame for calculation purposes */
|
2008-06-12 17:27:55 +00:00
|
|
|
dahdip->fake = 2;
|
2006-06-23 16:33:17 +00:00
|
|
|
pvt->samples = f->samples;
|
|
|
|
return 0;
|
|
|
|
}
|
2007-01-08 21:40:45 +00:00
|
|
|
|
2006-06-23 16:33:17 +00:00
|
|
|
if (!hdr->srclen)
|
|
|
|
/* Copy at front of buffer */
|
|
|
|
hdr->srcoffset = 0;
|
|
|
|
|
2007-03-06 22:15:02 +00:00
|
|
|
if (hdr->srclen + f->datalen > sizeof(hdr->srcdata)) {
|
2006-06-23 16:33:17 +00:00
|
|
|
ast_log(LOG_WARNING, "Out of space for codec translation!\n");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2007-03-06 22:15:02 +00:00
|
|
|
if (hdr->srclen + f->datalen + hdr->srcoffset > sizeof(hdr->srcdata)) {
|
2006-06-23 16:33:17 +00:00
|
|
|
/* Very unlikely */
|
|
|
|
memmove(hdr->srcdata, hdr->srcdata + hdr->srcoffset, hdr->srclen);
|
|
|
|
hdr->srcoffset = 0;
|
|
|
|
}
|
|
|
|
|
2008-05-22 16:29:54 +00:00
|
|
|
memcpy(hdr->srcdata + hdr->srcoffset + hdr->srclen, f->data.ptr, f->datalen);
|
2006-06-23 16:33:17 +00:00
|
|
|
hdr->srclen += f->datalen;
|
|
|
|
pvt->samples += f->samples;
|
|
|
|
|
2007-03-06 22:15:02 +00:00
|
|
|
return -1;
|
2006-06-23 16:33:17 +00:00
|
|
|
}
|
|
|
|
|
2008-06-12 17:27:55 +00:00
|
|
|
static struct ast_frame *dahdi_frameout(struct ast_trans_pvt *pvt)
|
2006-06-23 16:33:17 +00:00
|
|
|
{
|
2008-06-12 17:27:55 +00:00
|
|
|
struct pvt *dahdip = pvt->pvt;
|
|
|
|
struct dahdi_transcode_header *hdr = dahdip->hdr;
|
2006-06-23 16:33:17 +00:00
|
|
|
unsigned int x;
|
|
|
|
|
2008-06-12 17:27:55 +00:00
|
|
|
if (dahdip->fake == 2) {
|
|
|
|
dahdip->fake = 1;
|
Merged revisions 98943 via svnmerge from
https://origsvn.digium.com/svn/asterisk/branches/1.4
........
r98943 | russell | 2008-01-15 17:26:52 -0600 (Tue, 15 Jan 2008) | 25 lines
Commit a fix for some memory access errors pointed out by the valgrind2.txt
output on issue #11698.
The issue here is that it is possible for an instance of a translator to get
destroyed while the frame allocated as a part of the translator is still being
processed. Specifically, this is possible anywhere between a call to ast_read()
and ast_frame_free(), which is _a lot_ of places in the code. The reason this
happens is that the channel might get masqueraded during this time. During a
masquerade, existing translation paths get destroyed.
So, this patch fixes the issue in an API and ABI compatible way. (This one is
for you, paravoid!)
It changes an int in ast_frame to be used as flag bits. The 1 bit is still used
to indicate that the frame contains timing information. Also, a second flag has
been added to indicate that the frame came from a translator. When a frame with
this flag gets released and has this flag, a function is called in translate.c to
let it know that this frame is doing being processed. At this point, the flag gets
cleared. Also, if the translator was requested to be destroyed while its internal
frame still had this flag set, its destruction has been deffered until it finds out
that the frame is no longer being processed.
Admittedly, this feels like a hack. But, it does fix the issue, and I was not able
to think of a better solution ...
........
git-svn-id: http://svn.digium.com/svn/asterisk/trunk@98944 f38db490-d61c-443f-a65b-d21fe96a405b
2008-01-15 23:31:53 +00:00
|
|
|
pvt->f.frametype = AST_FRAME_VOICE;
|
|
|
|
pvt->f.subclass = 0;
|
|
|
|
pvt->f.samples = 160;
|
2008-05-22 16:29:54 +00:00
|
|
|
pvt->f.data.ptr = NULL;
|
Merged revisions 98943 via svnmerge from
https://origsvn.digium.com/svn/asterisk/branches/1.4
........
r98943 | russell | 2008-01-15 17:26:52 -0600 (Tue, 15 Jan 2008) | 25 lines
Commit a fix for some memory access errors pointed out by the valgrind2.txt
output on issue #11698.
The issue here is that it is possible for an instance of a translator to get
destroyed while the frame allocated as a part of the translator is still being
processed. Specifically, this is possible anywhere between a call to ast_read()
and ast_frame_free(), which is _a lot_ of places in the code. The reason this
happens is that the channel might get masqueraded during this time. During a
masquerade, existing translation paths get destroyed.
So, this patch fixes the issue in an API and ABI compatible way. (This one is
for you, paravoid!)
It changes an int in ast_frame to be used as flag bits. The 1 bit is still used
to indicate that the frame contains timing information. Also, a second flag has
been added to indicate that the frame came from a translator. When a frame with
this flag gets released and has this flag, a function is called in translate.c to
let it know that this frame is doing being processed. At this point, the flag gets
cleared. Also, if the translator was requested to be destroyed while its internal
frame still had this flag set, its destruction has been deffered until it finds out
that the frame is no longer being processed.
Admittedly, this feels like a hack. But, it does fix the issue, and I was not able
to think of a better solution ...
........
git-svn-id: http://svn.digium.com/svn/asterisk/trunk@98944 f38db490-d61c-443f-a65b-d21fe96a405b
2008-01-15 23:31:53 +00:00
|
|
|
pvt->f.offset = 0;
|
|
|
|
pvt->f.datalen = 0;
|
|
|
|
pvt->f.mallocd = 0;
|
|
|
|
ast_set_flag(&pvt->f, AST_FRFLAG_FROM_TRANSLATOR);
|
2006-06-23 16:33:17 +00:00
|
|
|
pvt->samples = 0;
|
2008-06-12 17:27:55 +00:00
|
|
|
} else if (dahdip->fake == 1) {
|
2006-06-23 16:33:17 +00:00
|
|
|
return NULL;
|
|
|
|
} else {
|
2006-08-30 03:20:54 +00:00
|
|
|
if (hdr->dstlen) {
|
|
|
|
#ifdef DEBUG_TRANSCODE
|
2008-06-12 17:27:55 +00:00
|
|
|
dahdip->totalms += hdr->dstsamples;
|
|
|
|
if ((dahdip->totalms - dahdip->lasttotalms) > 8000) {
|
|
|
|
printf("Whee %p, %d (%d to %d)\n", dahdip, hdr->dstlen, dahdip->lasttotalms, dahdip->totalms);
|
|
|
|
dahdip->lasttotalms = dahdip->totalms;
|
2006-08-30 03:20:54 +00:00
|
|
|
}
|
|
|
|
#endif
|
Merged revisions 98943 via svnmerge from
https://origsvn.digium.com/svn/asterisk/branches/1.4
........
r98943 | russell | 2008-01-15 17:26:52 -0600 (Tue, 15 Jan 2008) | 25 lines
Commit a fix for some memory access errors pointed out by the valgrind2.txt
output on issue #11698.
The issue here is that it is possible for an instance of a translator to get
destroyed while the frame allocated as a part of the translator is still being
processed. Specifically, this is possible anywhere between a call to ast_read()
and ast_frame_free(), which is _a lot_ of places in the code. The reason this
happens is that the channel might get masqueraded during this time. During a
masquerade, existing translation paths get destroyed.
So, this patch fixes the issue in an API and ABI compatible way. (This one is
for you, paravoid!)
It changes an int in ast_frame to be used as flag bits. The 1 bit is still used
to indicate that the frame contains timing information. Also, a second flag has
been added to indicate that the frame came from a translator. When a frame with
this flag gets released and has this flag, a function is called in translate.c to
let it know that this frame is doing being processed. At this point, the flag gets
cleared. Also, if the translator was requested to be destroyed while its internal
frame still had this flag set, its destruction has been deffered until it finds out
that the frame is no longer being processed.
Admittedly, this feels like a hack. But, it does fix the issue, and I was not able
to think of a better solution ...
........
git-svn-id: http://svn.digium.com/svn/asterisk/trunk@98944 f38db490-d61c-443f-a65b-d21fe96a405b
2008-01-15 23:31:53 +00:00
|
|
|
pvt->f.frametype = AST_FRAME_VOICE;
|
|
|
|
pvt->f.subclass = hdr->dstfmt;
|
|
|
|
pvt->f.samples = hdr->dstsamples;
|
2008-05-22 16:29:54 +00:00
|
|
|
pvt->f.data.ptr = hdr->dstdata + hdr->dstoffset;
|
Merged revisions 98943 via svnmerge from
https://origsvn.digium.com/svn/asterisk/branches/1.4
........
r98943 | russell | 2008-01-15 17:26:52 -0600 (Tue, 15 Jan 2008) | 25 lines
Commit a fix for some memory access errors pointed out by the valgrind2.txt
output on issue #11698.
The issue here is that it is possible for an instance of a translator to get
destroyed while the frame allocated as a part of the translator is still being
processed. Specifically, this is possible anywhere between a call to ast_read()
and ast_frame_free(), which is _a lot_ of places in the code. The reason this
happens is that the channel might get masqueraded during this time. During a
masquerade, existing translation paths get destroyed.
So, this patch fixes the issue in an API and ABI compatible way. (This one is
for you, paravoid!)
It changes an int in ast_frame to be used as flag bits. The 1 bit is still used
to indicate that the frame contains timing information. Also, a second flag has
been added to indicate that the frame came from a translator. When a frame with
this flag gets released and has this flag, a function is called in translate.c to
let it know that this frame is doing being processed. At this point, the flag gets
cleared. Also, if the translator was requested to be destroyed while its internal
frame still had this flag set, its destruction has been deffered until it finds out
that the frame is no longer being processed.
Admittedly, this feels like a hack. But, it does fix the issue, and I was not able
to think of a better solution ...
........
git-svn-id: http://svn.digium.com/svn/asterisk/trunk@98944 f38db490-d61c-443f-a65b-d21fe96a405b
2008-01-15 23:31:53 +00:00
|
|
|
pvt->f.offset = hdr->dstoffset;
|
|
|
|
pvt->f.datalen = hdr->dstlen;
|
|
|
|
pvt->f.mallocd = 0;
|
|
|
|
ast_set_flag(&pvt->f, AST_FRFLAG_FROM_TRANSLATOR);
|
|
|
|
pvt->samples -= pvt->f.samples;
|
2006-08-30 03:20:54 +00:00
|
|
|
hdr->dstlen = 0;
|
|
|
|
|
|
|
|
} else {
|
|
|
|
if (hdr->srclen) {
|
|
|
|
hdr->dstoffset = AST_FRIENDLY_OFFSET;
|
2008-06-12 17:27:55 +00:00
|
|
|
x = DAHDI_TCOP_TRANSCODE;
|
|
|
|
if (ioctl(dahdip->fd, DAHDI_TRANSCODE_OP, &x))
|
2006-08-30 03:20:54 +00:00
|
|
|
ast_log(LOG_WARNING, "Failed to transcode: %s\n", strerror(errno));
|
|
|
|
}
|
2006-06-23 16:33:17 +00:00
|
|
|
return NULL;
|
2006-08-30 03:20:54 +00:00
|
|
|
}
|
2006-06-23 16:33:17 +00:00
|
|
|
}
|
|
|
|
|
Merged revisions 98943 via svnmerge from
https://origsvn.digium.com/svn/asterisk/branches/1.4
........
r98943 | russell | 2008-01-15 17:26:52 -0600 (Tue, 15 Jan 2008) | 25 lines
Commit a fix for some memory access errors pointed out by the valgrind2.txt
output on issue #11698.
The issue here is that it is possible for an instance of a translator to get
destroyed while the frame allocated as a part of the translator is still being
processed. Specifically, this is possible anywhere between a call to ast_read()
and ast_frame_free(), which is _a lot_ of places in the code. The reason this
happens is that the channel might get masqueraded during this time. During a
masquerade, existing translation paths get destroyed.
So, this patch fixes the issue in an API and ABI compatible way. (This one is
for you, paravoid!)
It changes an int in ast_frame to be used as flag bits. The 1 bit is still used
to indicate that the frame contains timing information. Also, a second flag has
been added to indicate that the frame came from a translator. When a frame with
this flag gets released and has this flag, a function is called in translate.c to
let it know that this frame is doing being processed. At this point, the flag gets
cleared. Also, if the translator was requested to be destroyed while its internal
frame still had this flag set, its destruction has been deffered until it finds out
that the frame is no longer being processed.
Admittedly, this feels like a hack. But, it does fix the issue, and I was not able
to think of a better solution ...
........
git-svn-id: http://svn.digium.com/svn/asterisk/trunk@98944 f38db490-d61c-443f-a65b-d21fe96a405b
2008-01-15 23:31:53 +00:00
|
|
|
return &pvt->f;
|
2006-06-23 16:33:17 +00:00
|
|
|
}
|
|
|
|
|
2008-06-12 17:27:55 +00:00
|
|
|
static void dahdi_destroy(struct ast_trans_pvt *pvt)
|
2006-06-23 16:33:17 +00:00
|
|
|
{
|
2008-06-12 17:27:55 +00:00
|
|
|
struct pvt *dahdip = pvt->pvt;
|
2007-10-18 15:57:30 +00:00
|
|
|
unsigned int x;
|
|
|
|
|
2008-06-12 17:27:55 +00:00
|
|
|
x = DAHDI_TCOP_RELEASE;
|
|
|
|
if (ioctl(dahdip->fd, DAHDI_TRANSCODE_OP, &x))
|
2007-10-18 15:57:30 +00:00
|
|
|
ast_log(LOG_WARNING, "Failed to release transcoder channel: %s\n", strerror(errno));
|
2006-06-23 16:33:17 +00:00
|
|
|
|
2008-06-12 17:27:55 +00:00
|
|
|
switch (dahdip->hdr->dstfmt) {
|
2007-08-31 15:58:31 +00:00
|
|
|
case AST_FORMAT_G729A:
|
|
|
|
case AST_FORMAT_G723_1:
|
|
|
|
ast_atomic_fetchadd_int(&channels.encoders, -1);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
ast_atomic_fetchadd_int(&channels.decoders, -1);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2008-06-12 17:27:55 +00:00
|
|
|
munmap(dahdip->hdr, sizeof(*dahdip->hdr));
|
|
|
|
close(dahdip->fd);
|
2006-06-23 16:33:17 +00:00
|
|
|
}
|
|
|
|
|
2008-06-12 17:27:55 +00:00
|
|
|
static int dahdi_translate(struct ast_trans_pvt *pvt, int dest, int source)
|
2006-06-23 16:33:17 +00:00
|
|
|
{
|
2008-06-12 17:27:55 +00:00
|
|
|
/* Request translation through dahdi if possible */
|
2006-06-23 16:33:17 +00:00
|
|
|
int fd;
|
2008-06-12 17:27:55 +00:00
|
|
|
unsigned int x = DAHDI_TCOP_ALLOCATE;
|
|
|
|
struct pvt *dahdip = pvt->pvt;
|
|
|
|
struct dahdi_transcode_header *hdr;
|
2006-08-30 03:20:54 +00:00
|
|
|
int flags;
|
2006-06-23 16:33:17 +00:00
|
|
|
|
2008-06-12 17:27:55 +00:00
|
|
|
if ((fd = open("/dev/dahdi/transcode", O_RDWR)) < 0)
|
2006-06-24 19:43:31 +00:00
|
|
|
return -1;
|
2006-08-30 03:20:54 +00:00
|
|
|
flags = fcntl(fd, F_GETFL);
|
|
|
|
if (flags > - 1) {
|
|
|
|
if (fcntl(fd, F_SETFL, flags | O_NONBLOCK))
|
|
|
|
ast_log(LOG_WARNING, "Could not set non-block mode!\n");
|
|
|
|
}
|
2007-03-06 22:15:02 +00:00
|
|
|
|
2006-06-23 16:33:17 +00:00
|
|
|
|
|
|
|
if ((hdr = mmap(NULL, sizeof(*hdr), PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0)) == MAP_FAILED) {
|
|
|
|
ast_log(LOG_ERROR, "Memory Map failed for transcoding (%s)\n", strerror(errno));
|
|
|
|
close(fd);
|
|
|
|
|
2006-06-24 19:43:31 +00:00
|
|
|
return -1;
|
2006-06-23 16:33:17 +00:00
|
|
|
}
|
|
|
|
|
2008-06-12 17:27:55 +00:00
|
|
|
if (hdr->magic != DAHDI_TRANSCODE_MAGIC) {
|
2006-06-23 16:33:17 +00:00
|
|
|
ast_log(LOG_ERROR, "Transcoder header (%08x) wasn't magic. Abandoning\n", hdr->magic);
|
|
|
|
munmap(hdr, sizeof(*hdr));
|
|
|
|
close(fd);
|
|
|
|
|
2006-06-24 19:43:31 +00:00
|
|
|
return -1;
|
2006-06-23 16:33:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
hdr->srcfmt = (1 << source);
|
|
|
|
hdr->dstfmt = (1 << dest);
|
2008-06-12 17:27:55 +00:00
|
|
|
if (ioctl(fd, DAHDI_TRANSCODE_OP, &x)) {
|
2006-06-23 16:33:17 +00:00
|
|
|
ast_log(LOG_ERROR, "Unable to attach transcoder: %s\n", strerror(errno));
|
|
|
|
munmap(hdr, sizeof(*hdr));
|
|
|
|
close(fd);
|
|
|
|
|
2006-06-24 19:43:31 +00:00
|
|
|
return -1;
|
2006-06-23 16:33:17 +00:00
|
|
|
}
|
|
|
|
|
2008-06-12 17:27:55 +00:00
|
|
|
dahdip = pvt->pvt;
|
|
|
|
dahdip->fd = fd;
|
|
|
|
dahdip->hdr = hdr;
|
2006-06-23 16:33:17 +00:00
|
|
|
|
2007-08-31 15:58:31 +00:00
|
|
|
switch (hdr->dstfmt) {
|
|
|
|
case AST_FORMAT_G729A:
|
|
|
|
case AST_FORMAT_G723_1:
|
|
|
|
ast_atomic_fetchadd_int(&channels.encoders, +1);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
ast_atomic_fetchadd_int(&channels.decoders, +1);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2006-06-24 19:43:31 +00:00
|
|
|
return 0;
|
2006-06-23 16:33:17 +00:00
|
|
|
}
|
|
|
|
|
2008-06-12 17:27:55 +00:00
|
|
|
static int dahdi_new(struct ast_trans_pvt *pvt)
|
2006-06-23 16:33:17 +00:00
|
|
|
{
|
2008-06-12 17:27:55 +00:00
|
|
|
return dahdi_translate(pvt, pvt->t->dstfmt, pvt->t->srcfmt);
|
2006-06-23 16:33:17 +00:00
|
|
|
}
|
|
|
|
|
2006-12-12 19:55:39 +00:00
|
|
|
static struct ast_frame *fakesrc_sample(void)
|
2006-11-13 22:01:43 +00:00
|
|
|
{
|
|
|
|
/* Don't bother really trying to test hardware ones. */
|
|
|
|
static struct ast_frame f = {
|
|
|
|
.frametype = AST_FRAME_VOICE,
|
2007-03-06 22:15:02 +00:00
|
|
|
.samples = 160,
|
2006-11-13 22:01:43 +00:00
|
|
|
.src = __PRETTY_FUNCTION__
|
|
|
|
};
|
|
|
|
|
|
|
|
return &f;
|
|
|
|
}
|
|
|
|
|
2006-08-21 02:11:39 +00:00
|
|
|
static int register_translator(int dst, int src)
|
2006-06-23 16:33:17 +00:00
|
|
|
{
|
2008-06-12 17:27:55 +00:00
|
|
|
struct translator *dahdi;
|
2006-06-23 16:33:17 +00:00
|
|
|
int res;
|
2006-12-12 19:55:39 +00:00
|
|
|
|
2008-06-12 17:27:55 +00:00
|
|
|
if (!(dahdi = ast_calloc(1, sizeof(*dahdi))))
|
2006-12-12 19:55:39 +00:00
|
|
|
return -1;
|
2007-03-06 22:15:02 +00:00
|
|
|
|
2008-06-12 17:27:55 +00:00
|
|
|
snprintf((char *) (dahdi->t.name), sizeof(dahdi->t.name), "DAHDI%sto%s",
|
2006-12-12 19:55:39 +00:00
|
|
|
ast_getformatname((1 << src)), ast_getformatname((1 << dst)));
|
2008-06-12 17:27:55 +00:00
|
|
|
dahdi->t.srcfmt = (1 << src);
|
|
|
|
dahdi->t.dstfmt = (1 << dst);
|
|
|
|
dahdi->t.newpvt = dahdi_new;
|
|
|
|
dahdi->t.framein = dahdi_framein;
|
|
|
|
dahdi->t.frameout = dahdi_frameout;
|
|
|
|
dahdi->t.destroy = dahdi_destroy;
|
|
|
|
dahdi->t.sample = fakesrc_sample;
|
|
|
|
dahdi->t.useplc = global_useplc;
|
|
|
|
dahdi->t.buf_size = BUFFER_SAMPLES * 2;
|
|
|
|
dahdi->t.desc_size = sizeof(struct pvt);
|
|
|
|
if ((res = ast_register_translator(&dahdi->t))) {
|
|
|
|
ast_free(dahdi);
|
2006-06-23 16:33:17 +00:00
|
|
|
return -1;
|
|
|
|
}
|
2006-12-12 19:55:39 +00:00
|
|
|
|
2007-03-06 22:15:02 +00:00
|
|
|
AST_LIST_LOCK(&translators);
|
2008-06-12 17:27:55 +00:00
|
|
|
AST_LIST_INSERT_HEAD(&translators, dahdi, entry);
|
2007-03-06 22:15:02 +00:00
|
|
|
AST_LIST_UNLOCK(&translators);
|
2006-12-12 19:55:39 +00:00
|
|
|
|
|
|
|
global_format_map.map[dst][src] = 1;
|
|
|
|
|
|
|
|
return res;
|
2006-06-23 16:33:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void drop_translator(int dst, int src)
|
|
|
|
{
|
|
|
|
struct translator *cur;
|
|
|
|
|
2007-03-06 22:15:02 +00:00
|
|
|
AST_LIST_LOCK(&translators);
|
|
|
|
AST_LIST_TRAVERSE_SAFE_BEGIN(&translators, cur, entry) {
|
2006-06-23 16:33:17 +00:00
|
|
|
if (cur->t.srcfmt != src)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (cur->t.dstfmt != dst)
|
|
|
|
continue;
|
|
|
|
|
2007-11-08 05:28:47 +00:00
|
|
|
AST_LIST_REMOVE_CURRENT(entry);
|
2006-06-23 16:33:17 +00:00
|
|
|
ast_unregister_translator(&cur->t);
|
2007-06-06 21:20:11 +00:00
|
|
|
ast_free(cur);
|
2006-06-23 16:33:17 +00:00
|
|
|
global_format_map.map[dst][src] = 0;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
AST_LIST_TRAVERSE_SAFE_END;
|
2007-03-06 22:15:02 +00:00
|
|
|
AST_LIST_UNLOCK(&translators);
|
2006-06-23 16:33:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void unregister_translators(void)
|
|
|
|
{
|
|
|
|
struct translator *cur;
|
|
|
|
|
2007-03-06 22:15:02 +00:00
|
|
|
AST_LIST_LOCK(&translators);
|
|
|
|
while ((cur = AST_LIST_REMOVE_HEAD(&translators, entry))) {
|
2006-06-23 16:33:17 +00:00
|
|
|
ast_unregister_translator(&cur->t);
|
2007-06-06 21:20:11 +00:00
|
|
|
ast_free(cur);
|
2006-06-23 16:33:17 +00:00
|
|
|
}
|
2007-03-06 22:15:02 +00:00
|
|
|
AST_LIST_UNLOCK(&translators);
|
2006-06-23 16:33:17 +00:00
|
|
|
}
|
|
|
|
|
2007-10-31 19:24:29 +00:00
|
|
|
static int parse_config(int reload)
|
2006-06-23 16:33:17 +00:00
|
|
|
{
|
|
|
|
struct ast_variable *var;
|
2007-08-16 21:09:46 +00:00
|
|
|
struct ast_flags config_flags = { reload ? CONFIG_FLAG_FILEUNCHANGED : 0 };
|
|
|
|
struct ast_config *cfg = ast_config_load("codecs.conf", config_flags);
|
2006-06-23 16:33:17 +00:00
|
|
|
|
2007-12-21 20:12:50 +00:00
|
|
|
if (cfg == NULL)
|
|
|
|
return 0;
|
2007-10-31 19:24:29 +00:00
|
|
|
if (cfg == CONFIG_STATUS_FILEUNCHANGED)
|
|
|
|
return 0;
|
2006-06-23 16:33:17 +00:00
|
|
|
|
|
|
|
for (var = ast_variable_browse(cfg, "plc"); var; var = var->next) {
|
|
|
|
if (!strcasecmp(var->name, "genericplc")) {
|
|
|
|
global_useplc = ast_true(var->value);
|
2008-06-12 17:27:55 +00:00
|
|
|
ast_verb(3, "codec_dahdi: %susing generic PLC\n",
|
2006-06-23 16:33:17 +00:00
|
|
|
global_useplc ? "" : "not ");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
ast_config_destroy(cfg);
|
2007-10-31 19:24:29 +00:00
|
|
|
return 0;
|
2006-06-23 16:33:17 +00:00
|
|
|
}
|
|
|
|
|
2007-03-06 22:15:02 +00:00
|
|
|
static void build_translators(struct format_map *map, unsigned int dstfmts, unsigned int srcfmts)
|
2006-06-23 16:33:17 +00:00
|
|
|
{
|
|
|
|
unsigned int src, dst;
|
|
|
|
|
|
|
|
for (src = 0; src < 32; src++) {
|
|
|
|
for (dst = 0; dst < 32; dst++) {
|
|
|
|
if (!(srcfmts & (1 << src)))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (!(dstfmts & (1 << dst)))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (global_format_map.map[dst][src])
|
|
|
|
continue;
|
|
|
|
|
2006-08-21 02:11:39 +00:00
|
|
|
if (!register_translator(dst, src))
|
2006-06-23 16:33:17 +00:00
|
|
|
map->map[dst][src] = 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-08-21 02:11:39 +00:00
|
|
|
static int find_transcoders(void)
|
2006-06-23 16:33:17 +00:00
|
|
|
{
|
2008-06-12 17:27:55 +00:00
|
|
|
struct dahdi_transcode_info info = { 0, };
|
2006-06-23 16:33:17 +00:00
|
|
|
struct format_map map = { { { 0 } } };
|
|
|
|
int fd, res;
|
|
|
|
unsigned int x, y;
|
|
|
|
|
2008-06-12 17:27:55 +00:00
|
|
|
if ((fd = open("/dev/dahdi/transcode", O_RDWR)) < 0) {
|
2008-01-09 17:30:13 +00:00
|
|
|
ast_verbose(VERBOSE_PREFIX_2 "No hardware transcoders found.\n");
|
2006-06-23 16:33:17 +00:00
|
|
|
return 0;
|
|
|
|
}
|
2008-01-09 17:30:13 +00:00
|
|
|
|
2008-06-12 17:27:55 +00:00
|
|
|
info.op = DAHDI_TCOP_GETINFO;
|
|
|
|
for (info.tcnum = 0; !(res = ioctl(fd, DAHDI_TRANSCODE_OP, &info)); info.tcnum++) {
|
2007-07-26 15:49:18 +00:00
|
|
|
ast_verb(2, "Found transcoder '%s'.\n", info.name);
|
2007-03-06 22:15:02 +00:00
|
|
|
build_translators(&map, info.dstfmts, info.srcfmts);
|
2007-11-06 19:10:18 +00:00
|
|
|
ast_atomic_fetchadd_int(&channels.total, info.numchannels / 2);
|
2006-06-23 16:33:17 +00:00
|
|
|
}
|
2008-01-09 17:30:13 +00:00
|
|
|
|
2006-06-23 16:33:17 +00:00
|
|
|
close(fd);
|
|
|
|
|
2007-07-26 15:49:18 +00:00
|
|
|
if (!info.tcnum)
|
|
|
|
ast_verb(2, "No hardware transcoders found.\n");
|
2006-06-23 16:33:17 +00:00
|
|
|
|
|
|
|
for (x = 0; x < 32; x++) {
|
|
|
|
for (y = 0; y < 32; y++) {
|
|
|
|
if (!map.map[x][y] && global_format_map.map[x][y])
|
|
|
|
drop_translator(x, y);
|
|
|
|
}
|
|
|
|
}
|
2007-03-06 22:15:02 +00:00
|
|
|
|
2006-06-23 16:33:17 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2006-08-21 02:11:39 +00:00
|
|
|
static int reload(void)
|
2006-06-23 16:33:17 +00:00
|
|
|
{
|
|
|
|
struct translator *cur;
|
|
|
|
|
2007-10-31 19:24:29 +00:00
|
|
|
if (parse_config(1))
|
|
|
|
return AST_MODULE_LOAD_DECLINE;
|
2006-06-23 16:33:17 +00:00
|
|
|
|
2007-03-06 22:15:02 +00:00
|
|
|
AST_LIST_LOCK(&translators);
|
|
|
|
AST_LIST_TRAVERSE(&translators, cur, entry)
|
2006-06-23 16:33:17 +00:00
|
|
|
cur->t.useplc = global_useplc;
|
2007-03-06 22:15:02 +00:00
|
|
|
AST_LIST_UNLOCK(&translators);
|
2006-06-23 16:33:17 +00:00
|
|
|
|
2007-10-31 19:24:29 +00:00
|
|
|
return AST_MODULE_LOAD_SUCCESS;
|
2006-06-23 16:33:17 +00:00
|
|
|
}
|
|
|
|
|
2006-08-21 02:11:39 +00:00
|
|
|
static int unload_module(void)
|
2006-06-23 16:33:17 +00:00
|
|
|
{
|
2007-08-31 15:58:31 +00:00
|
|
|
ast_cli_unregister_multiple(cli, sizeof(cli) / sizeof(cli[0]));
|
2006-06-23 16:33:17 +00:00
|
|
|
unregister_translators();
|
2007-03-06 22:15:02 +00:00
|
|
|
|
2006-06-23 16:33:17 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2006-08-21 02:11:39 +00:00
|
|
|
static int load_module(void)
|
2006-06-23 16:33:17 +00:00
|
|
|
{
|
2007-10-31 19:24:29 +00:00
|
|
|
if (parse_config(0))
|
|
|
|
return AST_MODULE_LOAD_DECLINE;
|
2006-12-11 21:04:41 +00:00
|
|
|
find_transcoders();
|
2007-08-31 15:58:31 +00:00
|
|
|
ast_cli_register_multiple(cli, sizeof(cli) / sizeof(cli[0]));
|
2007-10-31 19:24:29 +00:00
|
|
|
return AST_MODULE_LOAD_SUCCESS;
|
2006-06-23 16:33:17 +00:00
|
|
|
}
|
|
|
|
|
2008-06-12 17:27:55 +00:00
|
|
|
AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_DEFAULT, "Generic DAHDI Transcoder Codec Translator",
|
2006-08-21 02:11:39 +00:00
|
|
|
.load = load_module,
|
|
|
|
.unload = unload_module,
|
|
|
|
.reload = reload,
|
2007-10-19 18:01:00 +00:00
|
|
|
);
|