Asterisk - The Open Source Telephony Project  18.5.0
Data Structures | Macros | Functions | Variables
codec_dahdi.c File Reference

Translate between various formats natively through DAHDI transcoding. More...

#include "asterisk.h"
#include <stdbool.h>
#include <poll.h>
#include <fcntl.h>
#include <netinet/in.h>
#include <sys/ioctl.h>
#include <sys/mman.h>
#include <dahdi/user.h>
#include "asterisk/lock.h"
#include "asterisk/translate.h"
#include "asterisk/config.h"
#include "asterisk/module.h"
#include "asterisk/cli.h"
#include "asterisk/channel.h"
#include "asterisk/utils.h"
#include "asterisk/linkedlists.h"
#include "asterisk/ulaw.h"
#include "asterisk/format_compatibility.h"
Include dependency graph for codec_dahdi.c:

Go to the source code of this file.

Data Structures

struct  channel_usage
 
struct  codec_dahdi_pvt
 
struct  translator
 
struct  translators
 the list of translators More...
 

Macros

#define BUFFER_SIZE   8000
 
#define container_of(ptr, type, member)   ((type *)((char *)(ptr) - offsetof(type, member)))
 
#define DAHDI_FORMAT_ADPCM   (1 << 5)
 
#define DAHDI_FORMAT_ALAW   (1 << 3)
 
#define DAHDI_FORMAT_G723_1   (1 << 0)
 
#define DAHDI_FORMAT_G726   (1 << 4)
 
#define DAHDI_FORMAT_G729A   (1 << 8)
 
#define DAHDI_FORMAT_GSM   (1 << 1)
 
#define DAHDI_FORMAT_ILBC   (1 << 10)
 
#define DAHDI_FORMAT_LPC10   (1 << 7)
 
#define DAHDI_FORMAT_SLINEAR   (1 << 6)
 
#define DAHDI_FORMAT_SPEEX   (1 << 9)
 
#define DAHDI_FORMAT_ULAW   (1 << 2)
 
#define G723_SAMPLES   240
 
#define G729_SAMPLES   160
 
#define ULAW_SAMPLES   160
 

Functions

static void __reg_module (void)
 
static void __unreg_module (void)
 
struct ast_moduleAST_MODULE_SELF_SYM (void)
 
static void build_translators (uint32_t dstfmts, uint32_t srcfmts)
 
static int dahdi_decoder_framein (struct ast_trans_pvt *pvt, struct ast_frame *f)
 
static struct ast_framedahdi_decoder_frameout (struct ast_trans_pvt *pvt)
 
static void dahdi_destroy (struct ast_trans_pvt *pvt)
 
static int dahdi_encoder_framein (struct ast_trans_pvt *pvt, struct ast_frame *f)
 
static struct ast_framedahdi_encoder_frameout (struct ast_trans_pvt *pvt)
 
static struct ast_formatdahdi_format_to_cached (int format)
 
static int dahdi_new (struct ast_trans_pvt *pvt)
 
static int dahdi_translate (struct ast_trans_pvt *pvt, uint32_t dst_dahdi_fmt, uint32_t src_dahdi_fmt)
 
static void dahdi_wait_for_packet (int fd)
 
static void dahdi_write_frame (struct codec_dahdi_pvt *dahdip, const uint8_t *buffer, const ssize_t count)
 
static struct ast_framefakesrc_sample (void)
 
static int find_transcoders (void)
 
static const struct ast_codecget_dahdi_codec (uint32_t dahdi_fmt)
 
static char * handle_cli_transcoder_show (struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
 
static bool is_already_registered (uint32_t dstfmt, uint32_t srcfmt)
 
static bool is_encoder (uint32_t src_dahdi_fmt)
 
static int lintoulaw (struct ast_trans_pvt *pvt, struct ast_frame *f)
 
static int load_module (void)
 
static int register_translator (uint32_t dst_dahdi_fmt, uint32_t src_dahdi_fmt)
 
static int reload (void)
 
static int ulawtolin (struct ast_trans_pvt *pvt, int samples)
 
static int unload_module (void)
 
static void unregister_translators (void)
 

Variables

static struct ast_module_info __mod_info = { .name = AST_MODULE, .flags = AST_MODFLAG_DEFAULT , .description = "Generic DAHDI Transcoder Codec Translator" , .key = "This paragraph is copyright (c) 2006 by Digium, Inc. \In order for your module to load, it must return this \key via a function called \"key\". Any code which \includes this paragraph must be licensed under the GNU \General Public License version 2 or later (at your \option). In addition to Digium's general reservations \of rights, Digium expressly reserves the right to \allow other parties to license this paragraph under \different terms. Any use of Digium, Inc. trademarks or \logos (including \"Asterisk\" or \"Digium\") without \express written permission of Digium, Inc. is prohibited.\n" , .buildopt_sum = "30ef0c93b36035ec78c9cfd712d36d9b" , .support_level = AST_MODULE_SUPPORT_CORE, .load = load_module, .unload = unload_module, .reload = reload, }
 
static const struct ast_module_infoast_module_info = &__mod_info
 
static struct channel_usage channels
 
static struct ast_cli_entry cli []
 
static struct translators translators = { .first = NULL, .last = NULL, .lock = { PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP , NULL, {1, 0} } , }
 

Detailed Description

Translate between various formats natively through DAHDI transcoding.

Definition in file codec_dahdi.c.

Macro Definition Documentation

◆ BUFFER_SIZE

#define BUFFER_SIZE   8000

Definition at line 55 of file codec_dahdi.c.

Referenced by register_translator().

◆ container_of

#define container_of (   ptr,
  type,
  member 
)    ((type *)((char *)(ptr) - offsetof(type, member)))

Definition at line 277 of file codec_dahdi.c.

Referenced by dahdi_cc_callback(), and dahdi_new().

◆ DAHDI_FORMAT_ADPCM

#define DAHDI_FORMAT_ADPCM   (1 << 5)

ADPCM (IMA)

Definition at line 74 of file codec_dahdi.c.

Referenced by dahdi_format_to_cached(), and get_dahdi_codec().

◆ DAHDI_FORMAT_ALAW

#define DAHDI_FORMAT_ALAW   (1 << 3)

Raw A-law data (G.711)

Definition at line 70 of file codec_dahdi.c.

Referenced by dahdi_format_to_cached(), find_transcoders(), get_dahdi_codec(), and is_encoder().

◆ DAHDI_FORMAT_G723_1

#define DAHDI_FORMAT_G723_1   (1 << 0)

G.723.1 compression

Definition at line 64 of file codec_dahdi.c.

Referenced by dahdi_destroy(), dahdi_format_to_cached(), dahdi_translate(), and get_dahdi_codec().

◆ DAHDI_FORMAT_G726

#define DAHDI_FORMAT_G726   (1 << 4)

ADPCM (G.726, 32kbps)

Definition at line 72 of file codec_dahdi.c.

Referenced by dahdi_format_to_cached(), and get_dahdi_codec().

◆ DAHDI_FORMAT_G729A

#define DAHDI_FORMAT_G729A   (1 << 8)

G.729A audio

Definition at line 80 of file codec_dahdi.c.

Referenced by dahdi_destroy(), dahdi_format_to_cached(), dahdi_translate(), and get_dahdi_codec().

◆ DAHDI_FORMAT_GSM

#define DAHDI_FORMAT_GSM   (1 << 1)

GSM compression

Definition at line 66 of file codec_dahdi.c.

Referenced by dahdi_format_to_cached(), and get_dahdi_codec().

◆ DAHDI_FORMAT_ILBC

#define DAHDI_FORMAT_ILBC   (1 << 10)

iLBC Free Compression

Definition at line 84 of file codec_dahdi.c.

Referenced by dahdi_format_to_cached(), and get_dahdi_codec().

◆ DAHDI_FORMAT_LPC10

#define DAHDI_FORMAT_LPC10   (1 << 7)

LPC10, 180 samples/frame

Definition at line 78 of file codec_dahdi.c.

Referenced by dahdi_format_to_cached(), and get_dahdi_codec().

◆ DAHDI_FORMAT_SLINEAR

#define DAHDI_FORMAT_SLINEAR   (1 << 6)

Raw 16-bit Signed Linear (8000 Hz) PCM

Definition at line 76 of file codec_dahdi.c.

Referenced by dahdi_format_to_cached(), dahdi_translate(), find_transcoders(), get_dahdi_codec(), and is_encoder().

◆ DAHDI_FORMAT_SPEEX

#define DAHDI_FORMAT_SPEEX   (1 << 9)

SpeeX Free Compression

Definition at line 82 of file codec_dahdi.c.

Referenced by dahdi_format_to_cached(), and get_dahdi_codec().

◆ DAHDI_FORMAT_ULAW

#define DAHDI_FORMAT_ULAW   (1 << 2)

Raw mu-law data (G.711)

Definition at line 68 of file codec_dahdi.c.

Referenced by dahdi_format_to_cached(), dahdi_translate(), find_transcoders(), get_dahdi_codec(), and is_encoder().

◆ G723_SAMPLES

#define G723_SAMPLES   240

Definition at line 57 of file codec_dahdi.c.

Referenced by dahdi_translate().

◆ G729_SAMPLES

#define G729_SAMPLES   160

Definition at line 58 of file codec_dahdi.c.

Referenced by dahdi_translate().

◆ ULAW_SAMPLES

#define ULAW_SAMPLES   160

Definition at line 59 of file codec_dahdi.c.

Referenced by dahdi_decoder_frameout().

Function Documentation

◆ __reg_module()

static void __reg_module ( void  )
static

Definition at line 873 of file codec_dahdi.c.

◆ __unreg_module()

static void __unreg_module ( void  )
static

Definition at line 873 of file codec_dahdi.c.

◆ AST_MODULE_SELF_SYM()

struct ast_module* AST_MODULE_SELF_SYM ( void  )

Definition at line 873 of file codec_dahdi.c.

◆ build_translators()

static void build_translators ( uint32_t  dstfmts,
uint32_t  srcfmts 
)
static

Definition at line 785 of file codec_dahdi.c.

References AST_LIST_LOCK, AST_LIST_UNLOCK, is_already_registered(), and register_translator().

Referenced by find_transcoders().

786 {
787  uint32_t srcfmt;
788  uint32_t dstfmt;
789 
791 
792  for (srcfmt = 1; srcfmt != 0; srcfmt <<= 1) {
793  for (dstfmt = 1; dstfmt != 0; dstfmt <<= 1) {
794  if (!(dstfmts & dstfmt) || !(srcfmts & srcfmt)) {
795  continue;
796  }
797  if (is_already_registered(dstfmt, srcfmt)) {
798  continue;
799  }
800  register_translator(dstfmt, srcfmt);
801  }
802  }
803 
805 }
static int register_translator(uint32_t dst_dahdi_fmt, uint32_t src_dahdi_fmt)
Definition: codec_dahdi.c:707
#define AST_LIST_LOCK(head)
Locks a list.
Definition: linkedlists.h:39
#define AST_LIST_UNLOCK(head)
Attempts to unlock a list.
Definition: linkedlists.h:139
the list of translators
Definition: codec_dahdi.c:281
static bool is_already_registered(uint32_t dstfmt, uint32_t srcfmt)
Definition: codec_dahdi.c:770

◆ dahdi_decoder_framein()

static int dahdi_decoder_framein ( struct ast_trans_pvt pvt,
struct ast_frame f 
)
static

Definition at line 476 of file codec_dahdi.c.

References ast_log, dahdi_write_frame(), ast_frame::data, ast_frame::datalen, ast_trans_pvt::datalen, codec_dahdi_pvt::fake, ast_frame_subclass::format, LOG_ERROR, ast_frame::ptr, ast_trans_pvt::pvt, codec_dahdi_pvt::required_samples, ast_frame::samples, ast_trans_pvt::samples, codec_dahdi_pvt::samples_written_to_hardware, and ast_frame::subclass.

Referenced by register_translator().

477 {
478  struct codec_dahdi_pvt *dahdip = pvt->pvt;
479 
480  if (!f->subclass.format) {
481  /* We're just faking a return for calculation purposes. */
482  dahdip->fake = 2;
483  pvt->samples = f->samples;
484  return 0;
485  }
486 
487  if (!f->datalen) {
488  if (f->samples != dahdip->required_samples) {
489  ast_log(LOG_ERROR, "%d != %d %d\n", f->samples, dahdip->required_samples, f->datalen);
490  }
491  }
492  dahdi_write_frame(dahdip, f->data.ptr, f->datalen);
493  dahdip->samples_written_to_hardware += f->samples;
494  pvt->samples += f->samples;
495  pvt->datalen = 0;
496  return -1;
497 }
int datalen
actual space used in outbuf
Definition: translate.h:218
void * pvt
Definition: translate.h:219
struct ast_frame_subclass subclass
static void dahdi_write_frame(struct codec_dahdi_pvt *dahdip, const uint8_t *buffer, const ssize_t count)
Definition: codec_dahdi.c:359
uint16_t required_samples
Definition: codec_dahdi.c:288
#define ast_log
Definition: astobj2.c:42
#define LOG_ERROR
Definition: logger.h:285
union ast_frame::@263 data
struct ast_format * format
uint16_t samples_written_to_hardware
Definition: codec_dahdi.c:290
unsigned int fake
Definition: codec_dahdi.c:287

◆ dahdi_decoder_frameout()

static struct ast_frame* dahdi_decoder_frameout ( struct ast_trans_pvt pvt)
static

Definition at line 499 of file codec_dahdi.c.

References AST_FRAME_VOICE, ast_frisolate, ast_log, ast_translator::buf_size, ast_trans_pvt::c, dahdi_wait_for_packet(), ast_frame::datalen, ast_trans_pvt::datalen, errno, ast_trans_pvt::f, codec_dahdi_pvt::fake, codec_dahdi_pvt::fd, ast_frame::frametype, LOG_ERROR, ast_translator::name, NULL, ast_trans_pvt::outbuf, ast_trans_pvt::pvt, codec_dahdi_pvt::required_samples, ast_frame::samples, ast_trans_pvt::samples, codec_dahdi_pvt::samples_written_to_hardware, codec_dahdi_pvt::softslin, ast_trans_pvt::t, codec_dahdi_pvt::ulaw_buffer, ULAW_SAMPLES, and ulawtolin().

Referenced by register_translator().

500 {
501  int res;
502  struct codec_dahdi_pvt *dahdip = pvt->pvt;
503 
504  if (2 == dahdip->fake) {
505  struct ast_frame frm = {
507  .samples = dahdip->required_samples,
508  .src = pvt->t->name,
509  };
510 
511  dahdip->fake = 1;
512  pvt->samples = 0;
513 
514  return ast_frisolate(&frm);
515  } else if (1 == dahdip->fake) {
516  pvt->samples = 0;
517  dahdip->fake = 0;
518  return NULL;
519  }
520 
521  if (dahdip->samples_written_to_hardware >= ULAW_SAMPLES) {
522  dahdi_wait_for_packet(dahdip->fd);
523  }
524 
525  /* Let's check to see if there is a new frame for us.... */
526  if (dahdip->softslin) {
527  res = read(dahdip->fd, dahdip->ulaw_buffer, sizeof(dahdip->ulaw_buffer));
528  } else {
529  res = read(dahdip->fd, pvt->outbuf.c + pvt->datalen, pvt->t->buf_size - pvt->datalen);
530  }
531 
532  if (-1 == res) {
533  if (EWOULDBLOCK == errno) {
534  /* Nothing waiting... */
535  return NULL;
536  } else {
537  ast_log(LOG_ERROR, "Failed to read from transcoder: %s\n", strerror(errno));
538  return NULL;
539  }
540  } else {
541  if (dahdip->softslin) {
542  ulawtolin(pvt, res);
543  pvt->f.datalen = res * 2;
544  } else {
545  pvt->f.datalen = res;
546  }
547  pvt->datalen = 0;
548  pvt->f.samples = res;
549  pvt->samples = 0;
551  (dahdip->samples_written_to_hardware >= res) ?
552  dahdip->samples_written_to_hardware - res : 0;
553 
554  return ast_frisolate(&pvt->f);
555  }
556 
557  /* Shouldn't get here... */
558  return NULL;
559 }
int datalen
actual space used in outbuf
Definition: translate.h:218
uint8_t ulaw_buffer[1024]
Definition: codec_dahdi.c:291
struct ast_frame f
Definition: translate.h:215
#define NULL
Definition: resample.c:96
void * pvt
Definition: translate.h:219
unsigned int softslin
Definition: codec_dahdi.c:286
uint16_t required_samples
Definition: codec_dahdi.c:288
static void dahdi_wait_for_packet(int fd)
Definition: codec_dahdi.c:417
#define ast_log
Definition: astobj2.c:42
#define ULAW_SAMPLES
Definition: codec_dahdi.c:59
int buf_size
size of outbuf, in bytes. Mandatory. The wrapper code will also allocate an AST_FRIENDLY_OFFSET space...
Definition: translate.h:182
union ast_trans_pvt::@327 outbuf
#define LOG_ERROR
Definition: logger.h:285
int errno
#define ast_frisolate(fr)
Makes a frame independent of any static storage.
static int ulawtolin(struct ast_trans_pvt *pvt, int samples)
Definition: codec_dahdi.c:295
Data structure associated with a single frame of data.
struct ast_translator * t
Definition: translate.h:214
enum ast_frame_type frametype
uint16_t samples_written_to_hardware
Definition: codec_dahdi.c:290
unsigned int fake
Definition: codec_dahdi.c:287
char name[80]
Definition: translate.h:138

◆ dahdi_destroy()

static void dahdi_destroy ( struct ast_trans_pvt pvt)
static

Definition at line 562 of file codec_dahdi.c.

References ast_atomic_fetchadd_int(), channels, DAHDI_FORMAT_G723_1, DAHDI_FORMAT_G729A, channel_usage::decoders, channel_usage::encoders, codec_dahdi_pvt::fd, codec_dahdi_pvt::fmts, and ast_trans_pvt::pvt.

Referenced by register_translator().

563 {
564  struct codec_dahdi_pvt *dahdip = pvt->pvt;
565 
566  switch (dahdip->fmts.dstfmt) {
567  case DAHDI_FORMAT_G729A:
568  case DAHDI_FORMAT_G723_1:
570  break;
571  default:
573  break;
574  }
575 
576  close(dahdip->fd);
577 }
struct dahdi_transcoder_formats fmts
Definition: codec_dahdi.c:285
#define DAHDI_FORMAT_G723_1
Definition: codec_dahdi.c:64
void * pvt
Definition: translate.h:219
int ast_atomic_fetchadd_int(volatile int *p, int v)
Atomically add v to *p and return the previous value of *p.
Definition: lock.h:755
#define DAHDI_FORMAT_G729A
Definition: codec_dahdi.c:80
static struct channel_usage channels

◆ dahdi_encoder_framein()

static int dahdi_encoder_framein ( struct ast_trans_pvt pvt,
struct ast_frame f 
)
static

Definition at line 372 of file codec_dahdi.c.

References ast_log, dahdi_write_frame(), ast_frame::data, ast_trans_pvt::datalen, codec_dahdi_pvt::fake, ast_frame_subclass::format, lintoulaw(), LOG_ERROR, ast_frame::ptr, ast_trans_pvt::pvt, codec_dahdi_pvt::required_samples, ast_frame::samples, ast_trans_pvt::samples, codec_dahdi_pvt::samples_in_buffer, codec_dahdi_pvt::samples_written_to_hardware, codec_dahdi_pvt::softslin, ast_frame::subclass, and codec_dahdi_pvt::ulaw_buffer.

Referenced by register_translator().

373 {
374  struct codec_dahdi_pvt *dahdip = pvt->pvt;
375 
376  if (!f->subclass.format) {
377  /* We're just faking a return for calculation purposes. */
378  dahdip->fake = 2;
379  pvt->samples = f->samples;
380  return 0;
381  }
382 
383  /* Buffer up the packets and send them to the hardware if we
384  * have enough samples set up. */
385  if (dahdip->softslin) {
386  if (lintoulaw(pvt, f)) {
387  return -1;
388  }
389  } else {
390  /* NOTE: If softslin support is not needed, and the sample
391  * size is equal to the required sample size, we wouldn't
392  * need this copy operation. But at the time this was
393  * written, only softslin is supported. */
394  if (dahdip->samples_in_buffer + f->samples > sizeof(dahdip->ulaw_buffer)) {
395  ast_log(LOG_ERROR, "Out of buffer space.\n");
396  return -1;
397  }
398  memcpy(&dahdip->ulaw_buffer[dahdip->samples_in_buffer], f->data.ptr, f->samples);
399  dahdip->samples_in_buffer += f->samples;
400  }
401 
402  while (dahdip->samples_in_buffer >= dahdip->required_samples) {
403  dahdi_write_frame(dahdip, dahdip->ulaw_buffer, dahdip->required_samples);
405  dahdip->samples_in_buffer -= dahdip->required_samples;
406  if (dahdip->samples_in_buffer) {
407  /* Shift any remaining bytes down. */
408  memmove(dahdip->ulaw_buffer, &dahdip->ulaw_buffer[dahdip->required_samples],
409  dahdip->samples_in_buffer);
410  }
411  }
412  pvt->samples += f->samples;
413  pvt->datalen = 0;
414  return -1;
415 }
int datalen
actual space used in outbuf
Definition: translate.h:218
uint8_t ulaw_buffer[1024]
Definition: codec_dahdi.c:291
void * pvt
Definition: translate.h:219
struct ast_frame_subclass subclass
unsigned int softslin
Definition: codec_dahdi.c:286
static void dahdi_write_frame(struct codec_dahdi_pvt *dahdip, const uint8_t *buffer, const ssize_t count)
Definition: codec_dahdi.c:359
uint16_t required_samples
Definition: codec_dahdi.c:288
#define ast_log
Definition: astobj2.c:42
#define LOG_ERROR
Definition: logger.h:285
uint16_t samples_in_buffer
Definition: codec_dahdi.c:289
union ast_frame::@263 data
struct ast_format * format
static int lintoulaw(struct ast_trans_pvt *pvt, struct ast_frame *f)
Definition: codec_dahdi.c:311
uint16_t samples_written_to_hardware
Definition: codec_dahdi.c:290
unsigned int fake
Definition: codec_dahdi.c:287

◆ dahdi_encoder_frameout()

static struct ast_frame* dahdi_encoder_frameout ( struct ast_trans_pvt pvt)
static

Definition at line 425 of file codec_dahdi.c.

References ast_codec_samples_count(), AST_FRAME_VOICE, ast_frisolate, ast_log, ast_translator::buf_size, ast_trans_pvt::c, dahdi_wait_for_packet(), ast_frame::datalen, ast_trans_pvt::datalen, errno, ast_trans_pvt::f, codec_dahdi_pvt::fake, codec_dahdi_pvt::fd, ast_frame::frametype, LOG_ERROR, ast_translator::name, NULL, ast_trans_pvt::outbuf, ast_trans_pvt::pvt, codec_dahdi_pvt::required_samples, ast_frame::samples, ast_trans_pvt::samples, codec_dahdi_pvt::samples_written_to_hardware, and ast_trans_pvt::t.

Referenced by register_translator().

426 {
427  struct codec_dahdi_pvt *dahdip = pvt->pvt;
428  int res;
429 
430  if (2 == dahdip->fake) {
431  struct ast_frame frm = {
433  .samples = dahdip->required_samples,
434  .src = pvt->t->name,
435  };
436 
437  dahdip->fake = 1;
438  pvt->samples = 0;
439 
440  return ast_frisolate(&frm);
441  } else if (1 == dahdip->fake) {
442  dahdip->fake = 0;
443  return NULL;
444  }
445 
446  if (dahdip->samples_written_to_hardware >= dahdip->required_samples) {
447  dahdi_wait_for_packet(dahdip->fd);
448  }
449 
450  res = read(dahdip->fd, pvt->outbuf.c + pvt->datalen, pvt->t->buf_size - pvt->datalen);
451  if (-1 == res) {
452  if (EWOULDBLOCK == errno) {
453  /* Nothing waiting... */
454  return NULL;
455  } else {
456  ast_log(LOG_ERROR, "Failed to read from transcoder: %s\n", strerror(errno));
457  return NULL;
458  }
459  } else {
460  pvt->f.datalen = res;
461  pvt->f.samples = ast_codec_samples_count(&pvt->f);
462 
464  (dahdip->samples_written_to_hardware >= pvt->f.samples) ?
465  dahdip->samples_written_to_hardware - pvt->f.samples : 0;
466 
467  pvt->samples = 0;
468  pvt->datalen = 0;
469  return ast_frisolate(&pvt->f);
470  }
471 
472  /* Shouldn't get here... */
473  return NULL;
474 }
int datalen
actual space used in outbuf
Definition: translate.h:218
struct ast_frame f
Definition: translate.h:215
#define NULL
Definition: resample.c:96
void * pvt
Definition: translate.h:219
uint16_t required_samples
Definition: codec_dahdi.c:288
static void dahdi_wait_for_packet(int fd)
Definition: codec_dahdi.c:417
#define ast_log
Definition: astobj2.c:42
unsigned int ast_codec_samples_count(struct ast_frame *frame)
Get the number of samples contained within a frame.
Definition: codec.c:378
int buf_size
size of outbuf, in bytes. Mandatory. The wrapper code will also allocate an AST_FRIENDLY_OFFSET space...
Definition: translate.h:182
union ast_trans_pvt::@327 outbuf
#define LOG_ERROR
Definition: logger.h:285
int errno
#define ast_frisolate(fr)
Makes a frame independent of any static storage.
Data structure associated with a single frame of data.
struct ast_translator * t
Definition: translate.h:214
enum ast_frame_type frametype
uint16_t samples_written_to_hardware
Definition: codec_dahdi.c:290
unsigned int fake
Definition: codec_dahdi.c:287
char name[80]
Definition: translate.h:138

◆ dahdi_format_to_cached()

static struct ast_format* dahdi_format_to_cached ( int  format)
static

Definition at line 579 of file codec_dahdi.c.

References ast_assert, ast_format_adpcm, ast_format_alaw, ast_format_g723, ast_format_g726, ast_format_g729, ast_format_gsm, ast_format_ilbc, ast_format_lpc10, ast_format_slin, ast_format_speex, ast_format_ulaw, DAHDI_FORMAT_ADPCM, DAHDI_FORMAT_ALAW, DAHDI_FORMAT_G723_1, DAHDI_FORMAT_G726, DAHDI_FORMAT_G729A, DAHDI_FORMAT_GSM, DAHDI_FORMAT_ILBC, DAHDI_FORMAT_LPC10, DAHDI_FORMAT_SLINEAR, DAHDI_FORMAT_SPEEX, DAHDI_FORMAT_ULAW, and NULL.

Referenced by dahdi_translate().

580 {
581  switch (format) {
582  case DAHDI_FORMAT_G723_1:
583  return ast_format_g723;
584  case DAHDI_FORMAT_GSM:
585  return ast_format_gsm;
586  case DAHDI_FORMAT_ULAW:
587  return ast_format_ulaw;
588  case DAHDI_FORMAT_ALAW:
589  return ast_format_alaw;
590  case DAHDI_FORMAT_G726:
591  return ast_format_g726;
592  case DAHDI_FORMAT_ADPCM:
593  return ast_format_adpcm;
595  return ast_format_slin;
596  case DAHDI_FORMAT_LPC10:
597  return ast_format_lpc10;
598  case DAHDI_FORMAT_G729A:
599  return ast_format_g729;
600  case DAHDI_FORMAT_SPEEX:
601  return ast_format_speex;
602  case DAHDI_FORMAT_ILBC:
603  return ast_format_ilbc;
604  }
605 
606  /* This will never be reached */
607  ast_assert(0);
608  return NULL;
609 }
struct ast_format * ast_format_g726
Built-in cached g726 format.
Definition: format_cache.c:116
struct ast_format * ast_format_g723
Built-in cached g723.1 format.
Definition: format_cache.c:151
struct ast_format * ast_format_ulaw
Built-in cached ulaw format.
Definition: format_cache.c:86
#define DAHDI_FORMAT_G723_1
Definition: codec_dahdi.c:64
#define ast_assert(a)
Definition: utils.h:695
#define NULL
Definition: resample.c:96
#define DAHDI_FORMAT_LPC10
Definition: codec_dahdi.c:78
struct ast_format * ast_format_lpc10
Built-in cached ilbc format.
Definition: format_cache.c:131
#define DAHDI_FORMAT_SPEEX
Definition: codec_dahdi.c:82
#define DAHDI_FORMAT_G729A
Definition: codec_dahdi.c:80
#define DAHDI_FORMAT_ULAW
Definition: codec_dahdi.c:68
struct ast_format * ast_format_gsm
Built-in cached gsm format.
Definition: format_cache.c:101
#define DAHDI_FORMAT_SLINEAR
Definition: codec_dahdi.c:76
struct ast_format * ast_format_speex
Built-in cached speex format.
Definition: format_cache.c:136
#define DAHDI_FORMAT_G726
Definition: codec_dahdi.c:72
#define DAHDI_FORMAT_ALAW
Definition: codec_dahdi.c:70
#define DAHDI_FORMAT_ADPCM
Definition: codec_dahdi.c:74
struct ast_format * ast_format_alaw
Built-in cached alaw format.
Definition: format_cache.c:91
struct ast_format * ast_format_adpcm
Built-in cached adpcm format.
Definition: format_cache.c:106
#define DAHDI_FORMAT_ILBC
Definition: codec_dahdi.c:84
struct ast_format * ast_format_g729
Built-in cached g729 format.
Definition: format_cache.c:156
struct ast_format * ast_format_slin
Built-in cached signed linear 8kHz format.
Definition: format_cache.c:41
static snd_pcm_format_t format
Definition: chan_alsa.c:102
#define DAHDI_FORMAT_GSM
Definition: codec_dahdi.c:66
struct ast_format * ast_format_ilbc
Built-in cached ilbc format.
Definition: format_cache.c:126

◆ dahdi_new()

static int dahdi_new ( struct ast_trans_pvt pvt)
static

Definition at line 682 of file codec_dahdi.c.

References container_of, dahdi_translate(), translator::dst_dahdi_fmt, translator::src_dahdi_fmt, translator::t, and ast_trans_pvt::t.

Referenced by register_translator().

683 {
684  struct translator *zt = container_of(pvt->t, struct translator, t);
685 
686  return dahdi_translate(pvt, zt->dst_dahdi_fmt, zt->src_dahdi_fmt);
687 }
static int dahdi_translate(struct ast_trans_pvt *pvt, uint32_t dst_dahdi_fmt, uint32_t src_dahdi_fmt)
Definition: codec_dahdi.c:611
uint32_t dst_dahdi_fmt
Definition: codec_dahdi.c:272
struct ast_translator t
Definition: codec_dahdi.c:270
#define container_of(ptr, type, member)
Definition: codec_dahdi.c:277
struct ast_translator * t
Definition: translate.h:214
uint32_t src_dahdi_fmt
Definition: codec_dahdi.c:271

◆ dahdi_translate()

static int dahdi_translate ( struct ast_trans_pvt pvt,
uint32_t  dst_dahdi_fmt,
uint32_t  src_dahdi_fmt 
)
static

Definition at line 611 of file codec_dahdi.c.

References ao2_bump, ast_assert, ast_atomic_fetchadd_int(), ast_debug, ast_fd_set_flags, ast_log, channels, DAHDI_FORMAT_G723_1, DAHDI_FORMAT_G729A, DAHDI_FORMAT_SLINEAR, dahdi_format_to_cached(), DAHDI_FORMAT_ULAW, channel_usage::decoders, ast_translator::dst_codec, channel_usage::encoders, errno, ast_trans_pvt::f, codec_dahdi_pvt::fd, codec_dahdi_pvt::fmts, ast_frame_subclass::format, G723_SAMPLES, G729_SAMPLES, LOG_ERROR, ast_codec::name, NULL, ast_trans_pvt::pvt, codec_dahdi_pvt::required_samples, codec_dahdi_pvt::softslin, ast_translator::src_codec, ast_frame::subclass, and ast_trans_pvt::t.

Referenced by dahdi_new().

612 {
613  /* Request translation through zap if possible */
614  int fd;
615  struct codec_dahdi_pvt *dahdip = pvt->pvt;
616  int tried_once = 0;
617  const char *dev_filename = "/dev/dahdi/transcode";
618 
619  if ((fd = open(dev_filename, O_RDWR)) < 0) {
620  ast_log(LOG_ERROR, "Failed to open %s: %s\n", dev_filename, strerror(errno));
621  return -1;
622  }
623 
624  dahdip->fmts.srcfmt = src_dahdi_fmt;
625  dahdip->fmts.dstfmt = dst_dahdi_fmt;
626 
627  ast_assert(pvt->f.subclass.format == NULL);
628  pvt->f.subclass.format = ao2_bump(dahdi_format_to_cached(dahdip->fmts.dstfmt));
629 
630  ast_debug(1, "Opening transcoder channel from %s to %s.\n", pvt->t->src_codec.name, pvt->t->dst_codec.name);
631 
632 retry:
633  if (ioctl(fd, DAHDI_TC_ALLOCATE, &dahdip->fmts)) {
634  if ((ENODEV == errno) && !tried_once) {
635  /* We requested to translate to/from an unsupported
636  * format. Most likely this is because signed linear
637  * was not supported by any hardware devices even
638  * though this module always registers signed linear
639  * support. In this case we'll retry, requesting
640  * support for ULAW instead of signed linear and then
641  * we'll just convert from ulaw to signed linear in
642  * software. */
643  if (dahdip->fmts.srcfmt == DAHDI_FORMAT_SLINEAR) {
644  ast_debug(1, "Using soft_slin support on source\n");
645  dahdip->softslin = 1;
646  dahdip->fmts.srcfmt = DAHDI_FORMAT_ULAW;
647  } else if (dahdip->fmts.dstfmt == DAHDI_FORMAT_SLINEAR) {
648  ast_debug(1, "Using soft_slin support on destination\n");
649  dahdip->softslin = 1;
650  dahdip->fmts.dstfmt = DAHDI_FORMAT_ULAW;
651  }
652  tried_once = 1;
653  goto retry;
654  }
655  ast_log(LOG_ERROR, "Unable to attach to transcoder: %s\n", strerror(errno));
656  close(fd);
657 
658  return -1;
659  }
660 
661  ast_fd_set_flags(fd, O_NONBLOCK);
662 
663  dahdip->fd = fd;
664 
665  dahdip->required_samples = ((dahdip->fmts.dstfmt|dahdip->fmts.srcfmt) & (DAHDI_FORMAT_G723_1)) ? G723_SAMPLES : G729_SAMPLES;
666 
667  switch (dahdip->fmts.dstfmt) {
668  case DAHDI_FORMAT_G729A:
670  break;
671  case DAHDI_FORMAT_G723_1:
673  break;
674  default:
676  break;
677  }
678 
679  return 0;
680 }
struct dahdi_transcoder_formats fmts
Definition: codec_dahdi.c:285
const char * name
Name for this codec.
Definition: codec.h:46
struct ast_frame f
Definition: translate.h:215
#define DAHDI_FORMAT_G723_1
Definition: codec_dahdi.c:64
#define ast_assert(a)
Definition: utils.h:695
#define NULL
Definition: resample.c:96
void * pvt
Definition: translate.h:219
#define G729_SAMPLES
Definition: codec_dahdi.c:58
int ast_atomic_fetchadd_int(volatile int *p, int v)
Atomically add v to *p and return the previous value of *p.
Definition: lock.h:755
struct ast_frame_subclass subclass
unsigned int softslin
Definition: codec_dahdi.c:286
#define ao2_bump(obj)
Definition: astobj2.h:491
uint16_t required_samples
Definition: codec_dahdi.c:288
#define ast_fd_set_flags(fd, flags)
Set flags on the given file descriptor.
Definition: utils.h:1009
#define ast_debug(level,...)
Log a DEBUG message.
Definition: logger.h:452
#define ast_log
Definition: astobj2.c:42
#define DAHDI_FORMAT_G729A
Definition: codec_dahdi.c:80
#define DAHDI_FORMAT_ULAW
Definition: codec_dahdi.c:68
static struct ast_format * dahdi_format_to_cached(int format)
Definition: codec_dahdi.c:579
static struct channel_usage channels
#define DAHDI_FORMAT_SLINEAR
Definition: codec_dahdi.c:76
struct ast_codec dst_codec
Definition: translate.h:140
#define LOG_ERROR
Definition: logger.h:285
int errno
struct ast_codec src_codec
Definition: translate.h:139
#define G723_SAMPLES
Definition: codec_dahdi.c:57
struct ast_translator * t
Definition: translate.h:214
struct ast_format * format

◆ dahdi_wait_for_packet()

static void dahdi_wait_for_packet ( int  fd)
static

Definition at line 417 of file codec_dahdi.c.

Referenced by dahdi_decoder_frameout(), and dahdi_encoder_frameout().

418 {
419  struct pollfd p = {0};
420  p.fd = fd;
421  p.events = POLLIN;
422  poll(&p, 1, 10);
423 }

◆ dahdi_write_frame()

static void dahdi_write_frame ( struct codec_dahdi_pvt dahdip,
const uint8_t *  buffer,
const ssize_t  count 
)
static

Definition at line 359 of file codec_dahdi.c.

References ast_log, errno, codec_dahdi_pvt::fd, and LOG_ERROR.

Referenced by dahdi_decoder_framein(), and dahdi_encoder_framein().

360 {
361  int res;
362  if (!count) return;
363  res = write(dahdip->fd, buffer, count);
364  if (-1 == res) {
365  ast_log(LOG_ERROR, "Failed to write to transcoder: %s\n", strerror(errno));
366  }
367  if (count != res) {
368  ast_log(LOG_ERROR, "Requested write of %zd bytes, but only wrote %d bytes.\n", count, res);
369  }
370 }
#define ast_log
Definition: astobj2.c:42
#define LOG_ERROR
Definition: logger.h:285
int errno

◆ fakesrc_sample()

static struct ast_frame* fakesrc_sample ( void  )
static

Definition at line 689 of file codec_dahdi.c.

References AST_FRAME_VOICE, and ast_frame::frametype.

Referenced by register_translator().

690 {
691  /* Don't bother really trying to test hardware ones. */
692  static struct ast_frame f = {
694  .samples = 160,
695  .src = __PRETTY_FUNCTION__
696  };
697 
698  return &f;
699 }
Data structure associated with a single frame of data.
enum ast_frame_type frametype

◆ find_transcoders()

static int find_transcoders ( void  )
static

Definition at line 807 of file codec_dahdi.c.

References ast_atomic_fetchadd_int(), ast_log, ast_verb, build_translators(), channels, DAHDI_FORMAT_ALAW, DAHDI_FORMAT_SLINEAR, DAHDI_FORMAT_ULAW, errno, LOG_ERROR, and channel_usage::total.

Referenced by load_module().

808 {
809  struct dahdi_transcoder_info info = { 0, };
810  int fd;
811 
812  if ((fd = open("/dev/dahdi/transcode", O_RDWR)) < 0) {
813  ast_log(LOG_ERROR, "Failed to open /dev/dahdi/transcode: %s\n", strerror(errno));
814  return 0;
815  }
816 
817  for (info.tcnum = 0; !ioctl(fd, DAHDI_TC_GETINFO, &info); info.tcnum++) {
818  ast_verb(2, "Found transcoder '%s'.\n", info.name);
819 
820  /* Complex codecs need to support signed linear. If the
821  * hardware transcoder does not natively support signed linear
822  * format, we will emulate it in software directly in this
823  * module. Also, do not allow direct ulaw/alaw to complex
824  * codec translation, since that will prevent the generic PLC
825  * functions from working. */
826  if (info.dstfmts & (DAHDI_FORMAT_ULAW | DAHDI_FORMAT_ALAW)) {
827  info.dstfmts |= DAHDI_FORMAT_SLINEAR;
828  info.dstfmts &= ~(DAHDI_FORMAT_ULAW | DAHDI_FORMAT_ALAW);
829  }
830  if (info.srcfmts & (DAHDI_FORMAT_ULAW | DAHDI_FORMAT_ALAW)) {
831  info.srcfmts |= DAHDI_FORMAT_SLINEAR;
832  info.srcfmts &= ~(DAHDI_FORMAT_ULAW | DAHDI_FORMAT_ALAW);
833  }
834 
835  build_translators(info.dstfmts, info.srcfmts);
836  ast_atomic_fetchadd_int(&channels.total, info.numchannels / 2);
837  }
838 
839  close(fd);
840 
841  if (!info.tcnum) {
842  ast_verb(2, "No hardware transcoders found.\n");
843  }
844 
845  return 0;
846 }
static void build_translators(uint32_t dstfmts, uint32_t srcfmts)
Definition: codec_dahdi.c:785
#define ast_verb(level,...)
Definition: logger.h:463
int ast_atomic_fetchadd_int(volatile int *p, int v)
Atomically add v to *p and return the previous value of *p.
Definition: lock.h:755
#define ast_log
Definition: astobj2.c:42
#define DAHDI_FORMAT_ULAW
Definition: codec_dahdi.c:68
static struct channel_usage channels
#define DAHDI_FORMAT_SLINEAR
Definition: codec_dahdi.c:76
#define LOG_ERROR
Definition: logger.h:285
def info(msg)
int errno
#define DAHDI_FORMAT_ALAW
Definition: codec_dahdi.c:70

◆ get_dahdi_codec()

static const struct ast_codec* get_dahdi_codec ( uint32_t  dahdi_fmt)
static

Definition at line 161 of file codec_dahdi.c.

References a, AST_MEDIA_TYPE_AUDIO, DAHDI_FORMAT_ADPCM, DAHDI_FORMAT_ALAW, DAHDI_FORMAT_G723_1, DAHDI_FORMAT_G726, DAHDI_FORMAT_G729A, DAHDI_FORMAT_GSM, DAHDI_FORMAT_ILBC, DAHDI_FORMAT_LPC10, DAHDI_FORMAT_SLINEAR, DAHDI_FORMAT_SPEEX, DAHDI_FORMAT_ULAW, handle_cli_transcoder_show(), ast_codec::name, and NULL.

Referenced by register_translator().

162 {
163  const struct ast_codec *codec;
164 
165  static const struct ast_codec dahdi_g723_1 = {
166  .name = "g723",
167  .type = AST_MEDIA_TYPE_AUDIO,
168  .sample_rate = 8000,
169  };
170  static const struct ast_codec dahdi_gsm = {
171  .name = "gsm",
172  .type = AST_MEDIA_TYPE_AUDIO,
173  .sample_rate = 8000,
174  };
175  static const struct ast_codec dahdi_ulaw = {
176  .name = "ulaw",
177  .type = AST_MEDIA_TYPE_AUDIO,
178  .sample_rate = 8000,
179  };
180  static const struct ast_codec dahdi_alaw = {
181  .name = "alaw",
182  .type = AST_MEDIA_TYPE_AUDIO,
183  .sample_rate = 8000,
184  };
185  static const struct ast_codec dahdi_g726 = {
186  .name = "g726",
187  .type = AST_MEDIA_TYPE_AUDIO,
188  .sample_rate = 8000,
189  };
190  static const struct ast_codec dahdi_adpcm = {
191  .name = "adpcm",
192  .type = AST_MEDIA_TYPE_AUDIO,
193  .sample_rate = 8000,
194  };
195  static const struct ast_codec dahdi_slinear = {
196  .name = "slin",
197  .type = AST_MEDIA_TYPE_AUDIO,
198  .sample_rate = 8000,
199  };
200  static const struct ast_codec dahdi_lpc10 = {
201  .name = "lpc10",
202  .type = AST_MEDIA_TYPE_AUDIO,
203  .sample_rate = 8000,
204  };
205  static const struct ast_codec dahdi_g729a = {
206  .name = "g729",
207  .type = AST_MEDIA_TYPE_AUDIO,
208  .sample_rate = 8000,
209  };
210  static const struct ast_codec dahdi_speex = {
211  .name = "speex",
212  .type = AST_MEDIA_TYPE_AUDIO,
213  .sample_rate = 8000,
214  };
215  static const struct ast_codec dahdi_ilbc = {
216  .name = "ilbc",
217  .type = AST_MEDIA_TYPE_AUDIO,
218  .sample_rate = 8000,
219  };
220 
221  switch (dahdi_fmt) {
222  case DAHDI_FORMAT_G723_1:
223  codec = &dahdi_g723_1;
224  break;
225  case DAHDI_FORMAT_GSM:
226  codec = &dahdi_gsm;
227  break;
228  case DAHDI_FORMAT_ULAW:
229  codec = &dahdi_ulaw;
230  break;
231  case DAHDI_FORMAT_ALAW:
232  codec = &dahdi_alaw;
233  break;
234  case DAHDI_FORMAT_G726:
235  codec = &dahdi_g726;
236  break;
237  case DAHDI_FORMAT_ADPCM:
238  codec = &dahdi_adpcm;
239  break;
241  codec = &dahdi_slinear;
242  break;
243  case DAHDI_FORMAT_LPC10:
244  codec = &dahdi_lpc10;
245  break;
246  case DAHDI_FORMAT_G729A:
247  codec = &dahdi_g729a;
248  break;
249  case DAHDI_FORMAT_SPEEX:
250  codec = &dahdi_speex;
251  break;
252  case DAHDI_FORMAT_ILBC:
253  codec = &dahdi_ilbc;
254  break;
255  default:
256  codec = NULL;
257  break;
258  }
259 
260  return codec;
261 }
const char * name
Name for this codec.
Definition: codec.h:46
#define DAHDI_FORMAT_G723_1
Definition: codec_dahdi.c:64
#define NULL
Definition: resample.c:96
#define DAHDI_FORMAT_LPC10
Definition: codec_dahdi.c:78
#define DAHDI_FORMAT_SPEEX
Definition: codec_dahdi.c:82
#define DAHDI_FORMAT_G729A
Definition: codec_dahdi.c:80
#define DAHDI_FORMAT_ULAW
Definition: codec_dahdi.c:68
#define DAHDI_FORMAT_SLINEAR
Definition: codec_dahdi.c:76
#define DAHDI_FORMAT_G726
Definition: codec_dahdi.c:72
#define DAHDI_FORMAT_ALAW
Definition: codec_dahdi.c:70
#define DAHDI_FORMAT_ADPCM
Definition: codec_dahdi.c:74
#define DAHDI_FORMAT_ILBC
Definition: codec_dahdi.c:84
#define DAHDI_FORMAT_GSM
Definition: codec_dahdi.c:66
Represents a media codec within Asterisk.
Definition: codec.h:42

◆ handle_cli_transcoder_show()

static char * handle_cli_transcoder_show ( struct ast_cli_entry e,
int  cmd,
struct ast_cli_args a 
)
static

Definition at line 331 of file codec_dahdi.c.

References ast_cli_args::argc, ast_cli(), channels, CLI_GENERATE, CLI_INIT, CLI_SHOWUSAGE, CLI_SUCCESS, ast_cli_entry::command, channel_usage::decoders, channel_usage::encoders, ast_cli_args::fd, NULL, channel_usage::total, and ast_cli_entry::usage.

Referenced by get_dahdi_codec().

332 {
333  struct channel_usage copy;
334 
335  switch (cmd) {
336  case CLI_INIT:
337  e->command = "transcoder show";
338  e->usage =
339  "Usage: transcoder show\n"
340  " Displays channel utilization of DAHDI transcoder(s).\n";
341  return NULL;
342  case CLI_GENERATE:
343  return NULL;
344  }
345 
346  if (a->argc != 2)
347  return CLI_SHOWUSAGE;
348 
349  copy = channels;
350 
351  if (copy.total == 0)
352  ast_cli(a->fd, "No DAHDI transcoders found.\n");
353  else
354  ast_cli(a->fd, "%d/%d encoders/decoders of %d channels are in use.\n", copy.encoders, copy.decoders, copy.total);
355 
356  return CLI_SUCCESS;
357 }
const int argc
Definition: cli.h:160
Definition: cli.h:152
static int copy(char *infile, char *outfile)
Utility function to copy a file.
#define NULL
Definition: resample.c:96
void ast_cli(int fd, const char *fmt,...)
Definition: clicompat.c:6
const int fd
Definition: cli.h:159
static struct channel_usage channels
#define CLI_SHOWUSAGE
Definition: cli.h:45
char * command
Definition: cli.h:186
const char * usage
Definition: cli.h:177
#define CLI_SUCCESS
Definition: cli.h:44

◆ is_already_registered()

static bool is_already_registered ( uint32_t  dstfmt,
uint32_t  srcfmt 
)
static

Definition at line 770 of file codec_dahdi.c.

References AST_LIST_TRAVERSE, translator::dst_dahdi_fmt, and translator::src_dahdi_fmt.

Referenced by build_translators().

771 {
772  bool res = false;
773  const struct translator *zt;
774 
776  if ((zt->src_dahdi_fmt == srcfmt) && (zt->dst_dahdi_fmt == dstfmt)) {
777  res = true;
778  break;
779  }
780  }
781 
782  return res;
783 }
uint32_t dst_dahdi_fmt
Definition: codec_dahdi.c:272
the list of translators
Definition: codec_dahdi.c:281
#define AST_LIST_TRAVERSE(head, var, field)
Loops over (traverses) the entries in a list.
Definition: linkedlists.h:490
Definition: search.h:40
uint32_t src_dahdi_fmt
Definition: codec_dahdi.c:271

◆ is_encoder()

static bool is_encoder ( uint32_t  src_dahdi_fmt)
static

Definition at line 701 of file codec_dahdi.c.

References DAHDI_FORMAT_ALAW, DAHDI_FORMAT_SLINEAR, and DAHDI_FORMAT_ULAW.

Referenced by register_translator().

702 {
704 }
#define DAHDI_FORMAT_ULAW
Definition: codec_dahdi.c:68
#define DAHDI_FORMAT_SLINEAR
Definition: codec_dahdi.c:76
#define DAHDI_FORMAT_ALAW
Definition: codec_dahdi.c:70
uint32_t src_dahdi_fmt
Definition: codec_dahdi.c:271

◆ lintoulaw()

static int lintoulaw ( struct ast_trans_pvt pvt,
struct ast_frame f 
)
static

Definition at line 311 of file codec_dahdi.c.

References AST_LIN2MU, ast_log, ast_frame::data, LOG_ERROR, ast_frame::ptr, ast_trans_pvt::pvt, ast_frame::samples, codec_dahdi_pvt::samples_in_buffer, and codec_dahdi_pvt::ulaw_buffer.

Referenced by dahdi_encoder_framein().

312 {
313  struct codec_dahdi_pvt *dahdip = pvt->pvt;
314  int i = f->samples;
315  uint8_t *dst = &dahdip->ulaw_buffer[dahdip->samples_in_buffer];
316  int16_t *src = f->data.ptr;
317 
318  if (dahdip->samples_in_buffer + i > sizeof(dahdip->ulaw_buffer)) {
319  ast_log(LOG_ERROR, "Out of buffer space!\n");
320  return -i;
321  }
322 
323  while (i--) {
324  *dst++ = AST_LIN2MU(*src++);
325  }
326 
327  dahdip->samples_in_buffer += f->samples;
328  return 0;
329 }
short int16_t
Definition: db.h:59
uint8_t ulaw_buffer[1024]
Definition: codec_dahdi.c:291
void * pvt
Definition: translate.h:219
#define ast_log
Definition: astobj2.c:42
#define LOG_ERROR
Definition: logger.h:285
uint16_t samples_in_buffer
Definition: codec_dahdi.c:289
union ast_frame::@263 data
#define AST_LIN2MU(a)
Definition: ulaw.h:49

◆ load_module()

static int load_module ( void  )
static

Definition at line 861 of file codec_dahdi.c.

References ARRAY_LEN, ast_cli_register_multiple, AST_MODFLAG_DEFAULT, AST_MODULE_INFO(), AST_MODULE_LOAD_SUCCESS, AST_MODULE_SUPPORT_CORE, ASTERISK_GPL_KEY, find_transcoders(), reload(), and unload_module().

862 {
866 }
#define ARRAY_LEN(a)
Definition: isdn_lib.c:42
#define ast_cli_register_multiple(e, len)
Register multiple commands.
Definition: cli.h:265
static int find_transcoders(void)
Definition: codec_dahdi.c:807
static struct ast_cli_entry cli[]
Definition: codec_dahdi.c:265

◆ register_translator()

static int register_translator ( uint32_t  dst_dahdi_fmt,
uint32_t  src_dahdi_fmt 
)
static

Definition at line 707 of file codec_dahdi.c.

References ast_calloc, ast_free, AST_LIST_INSERT_HEAD, ast_register_translator, ast_translator::buf_size, ast_translator::buffer_samples, BUFFER_SIZE, dahdi_decoder_framein(), dahdi_decoder_frameout(), dahdi_destroy(), dahdi_encoder_framein(), dahdi_encoder_frameout(), dahdi_new(), ast_translator::desc_size, ast_translator::destroy, ast_translator::dst_codec, translator::dst_dahdi_fmt, fakesrc_sample(), ast_translator::framein, ast_translator::frameout, get_dahdi_codec(), is_encoder(), ast_codec::name, ast_translator::name, ast_translator::native_plc, ast_translator::newpvt, ast_translator::sample, ast_translator::src_codec, translator::src_dahdi_fmt, and translator::t.

Referenced by build_translators().

708 {
709  const struct ast_codec *dst_codec;
710  const struct ast_codec *src_codec;
711  struct translator *zt;
712  int res;
713 
714  dst_codec = get_dahdi_codec(dst_dahdi_fmt);
715  src_codec = get_dahdi_codec(src_dahdi_fmt);
716  if (!dst_codec || !src_codec) {
717  return -1;
718  }
719 
720  if (!(zt = ast_calloc(1, sizeof(*zt)))) {
721  return -1;
722  }
723 
726 
727  snprintf(zt->t.name, sizeof(zt->t.name), "dahdi_%s_to_%s",
728  src_codec->name, dst_codec->name);
729 
730  memcpy(&zt->t.src_codec, src_codec, sizeof(*src_codec));
731  memcpy(&zt->t.dst_codec, dst_codec, sizeof(*dst_codec));
732  zt->t.buf_size = BUFFER_SIZE;
733  if (is_encoder(src_dahdi_fmt)) {
736  } else {
739  }
740  zt->t.destroy = dahdi_destroy;
741  zt->t.buffer_samples = 0;
742  zt->t.newpvt = dahdi_new;
743  zt->t.sample = fakesrc_sample;
744  zt->t.native_plc = 0;
745 
746  zt->t.desc_size = sizeof(struct codec_dahdi_pvt);
747  if ((res = ast_register_translator(&zt->t))) {
748  ast_free(zt);
749  return -1;
750  }
751 
753 
754  return res;
755 }
static const struct ast_codec * get_dahdi_codec(uint32_t dahdi_fmt)
Definition: codec_dahdi.c:161
const char * name
Name for this codec.
Definition: codec.h:46
int(* newpvt)(struct ast_trans_pvt *)
Definition: translate.h:151
static struct ast_frame * dahdi_encoder_frameout(struct ast_trans_pvt *pvt)
Definition: codec_dahdi.c:425
static struct ast_frame * fakesrc_sample(void)
Definition: codec_dahdi.c:689
static int dahdi_new(struct ast_trans_pvt *pvt)
Definition: codec_dahdi.c:682
uint32_t dst_dahdi_fmt
Definition: codec_dahdi.c:272
struct ast_translator t
Definition: codec_dahdi.c:270
static int dahdi_decoder_framein(struct ast_trans_pvt *pvt, struct ast_frame *f)
Definition: codec_dahdi.c:476
#define BUFFER_SIZE
Definition: codec_dahdi.c:55
#define ast_register_translator(t)
See __ast_register_translator()
Definition: translate.h:257
int buf_size
size of outbuf, in bytes. Mandatory. The wrapper code will also allocate an AST_FRIENDLY_OFFSET space...
Definition: translate.h:182
void(* destroy)(struct ast_trans_pvt *pvt)
Definition: translate.h:166
the list of translators
Definition: codec_dahdi.c:281
struct ast_codec dst_codec
Definition: translate.h:140
static int dahdi_encoder_framein(struct ast_trans_pvt *pvt, struct ast_frame *f)
Definition: codec_dahdi.c:372
struct ast_codec src_codec
Definition: translate.h:139
#define AST_LIST_INSERT_HEAD(head, elm, field)
Inserts a list entry at the head of a list.
Definition: linkedlists.h:710
#define ast_free(a)
Definition: astmm.h:182
#define ast_calloc(num, len)
A wrapper for calloc()
Definition: astmm.h:204
struct ast_frame *(* sample)(void)
Definition: translate.h:170
static bool is_encoder(uint32_t src_dahdi_fmt)
Definition: codec_dahdi.c:701
static void dahdi_destroy(struct ast_trans_pvt *pvt)
Definition: codec_dahdi.c:562
struct ast_frame *(* frameout)(struct ast_trans_pvt *pvt)
Definition: translate.h:158
static struct ast_frame * dahdi_decoder_frameout(struct ast_trans_pvt *pvt)
Definition: codec_dahdi.c:499
Definition: search.h:40
Represents a media codec within Asterisk.
Definition: codec.h:42
char name[80]
Definition: translate.h:138
int(* framein)(struct ast_trans_pvt *pvt, struct ast_frame *in)
Definition: translate.h:154
uint32_t src_dahdi_fmt
Definition: codec_dahdi.c:271
int buffer_samples
size of outbuf, in samples. Leave it 0 if you want the framein callback deal with the frame...
Definition: translate.h:177

◆ reload()

static int reload ( void  )
static

Definition at line 848 of file codec_dahdi.c.

References AST_MODULE_LOAD_SUCCESS.

Referenced by load_module().

849 {
851 }

◆ ulawtolin()

static int ulawtolin ( struct ast_trans_pvt pvt,
int  samples 
)
static

Definition at line 295 of file codec_dahdi.c.

References AST_MULAW, ast_trans_pvt::datalen, ast_trans_pvt::i16, ast_trans_pvt::outbuf, ast_trans_pvt::pvt, and codec_dahdi_pvt::ulaw_buffer.

Referenced by dahdi_decoder_frameout().

296 {
297  struct codec_dahdi_pvt *dahdip = pvt->pvt;
298  int i = samples;
299  uint8_t *src = &dahdip->ulaw_buffer[0];
300  int16_t *dst = pvt->outbuf.i16 + pvt->datalen;
301 
302  /* convert and copy in outbuf */
303  while (i--) {
304  *dst++ = AST_MULAW(*src++);
305  }
306 
307  return 0;
308 }
int datalen
actual space used in outbuf
Definition: translate.h:218
short int16_t
Definition: db.h:59
uint8_t ulaw_buffer[1024]
Definition: codec_dahdi.c:291
void * pvt
Definition: translate.h:219
int16_t * i16
Definition: translate.h:223
#define AST_MULAW(a)
Definition: ulaw.h:85
union ast_trans_pvt::@327 outbuf

◆ unload_module()

static int unload_module ( void  )
static

Definition at line 853 of file codec_dahdi.c.

References ARRAY_LEN, ast_cli_unregister_multiple(), and unregister_translators().

Referenced by load_module().

854 {
857 
858  return 0;
859 }
#define ARRAY_LEN(a)
Definition: isdn_lib.c:42
int ast_cli_unregister_multiple(struct ast_cli_entry *e, int len)
Unregister multiple commands.
Definition: clicompat.c:30
static void unregister_translators(void)
Definition: codec_dahdi.c:757
static struct ast_cli_entry cli[]
Definition: codec_dahdi.c:265

◆ unregister_translators()

static void unregister_translators ( void  )
static

Definition at line 757 of file codec_dahdi.c.

References ast_free, AST_LIST_LOCK, AST_LIST_REMOVE_HEAD, AST_LIST_UNLOCK, ast_unregister_translator(), and translator::t.

Referenced by unload_module().

758 {
759  struct translator *cur;
760 
762  while ((cur = AST_LIST_REMOVE_HEAD(&translators, entry))) {
764  ast_free(cur);
765  }
767 }
#define AST_LIST_LOCK(head)
Locks a list.
Definition: linkedlists.h:39
#define AST_LIST_UNLOCK(head)
Attempts to unlock a list.
Definition: linkedlists.h:139
struct ast_translator t
Definition: codec_dahdi.c:270
int ast_unregister_translator(struct ast_translator *t)
Unregister a translator Unregisters the given tranlator.
Definition: translate.c:1333
#define AST_LIST_REMOVE_HEAD(head, field)
Removes and returns the head entry from a list.
Definition: linkedlists.h:832
the list of translators
Definition: codec_dahdi.c:281
#define ast_free(a)
Definition: astmm.h:182
Definition: search.h:40

Variable Documentation

◆ __mod_info

struct ast_module_info __mod_info = { .name = AST_MODULE, .flags = AST_MODFLAG_DEFAULT , .description = "Generic DAHDI Transcoder Codec Translator" , .key = "This paragraph is copyright (c) 2006 by Digium, Inc. \In order for your module to load, it must return this \key via a function called \"key\". Any code which \includes this paragraph must be licensed under the GNU \General Public License version 2 or later (at your \option). In addition to Digium's general reservations \of rights, Digium expressly reserves the right to \allow other parties to license this paragraph under \different terms. Any use of Digium, Inc. trademarks or \logos (including \"Asterisk\" or \"Digium\") without \express written permission of Digium, Inc. is prohibited.\n" , .buildopt_sum = "30ef0c93b36035ec78c9cfd712d36d9b" , .support_level = AST_MODULE_SUPPORT_CORE, .load = load_module, .unload = unload_module, .reload = reload, }
static

Definition at line 873 of file codec_dahdi.c.

◆ ast_module_info

const struct ast_module_info* ast_module_info = &__mod_info
static

Definition at line 873 of file codec_dahdi.c.

◆ channels

struct channel_usage channels
static

◆ cli

struct ast_cli_entry cli[]
static
Initial value:
= {
{ .handler = handle_cli_transcoder_show , .summary = "Display DAHDI transcoder utilization." ,}
}
static char * handle_cli_transcoder_show(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
Definition: codec_dahdi.c:331

Definition at line 265 of file codec_dahdi.c.

◆ translators

struct translators translators = { .first = NULL, .last = NULL, .lock = { PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP , NULL, {1, 0} } , }
static