Thu Apr 28 2011 17:14:03

Asterisk developer's documentation


slinfactory.h File Reference

A machine to gather up arbitrary frames and convert them to raw slinear on demand. More...

This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Data Structures

struct  ast_slinfactory

Defines

#define AST_SLINFACTORY_MAX_HOLD   1280

Functions

unsigned int ast_slinfactory_available (const struct ast_slinfactory *sf)
 Retrieve number of samples currently in a slinfactory.
void ast_slinfactory_destroy (struct ast_slinfactory *sf)
 Destroy the contents of a slinfactory.
int ast_slinfactory_feed (struct ast_slinfactory *sf, struct ast_frame *f)
 Feed audio into a slinfactory.
void ast_slinfactory_flush (struct ast_slinfactory *sf)
 Flush the contents of a slinfactory.
void ast_slinfactory_init (struct ast_slinfactory *sf)
 Initialize a slinfactory.
int ast_slinfactory_init_rate (struct ast_slinfactory *sf, unsigned int sample_rate)
 Initialize a slinfactory.
int ast_slinfactory_read (struct ast_slinfactory *sf, short *buf, size_t samples)
 Read samples from a slinfactory.

Detailed Description

A machine to gather up arbitrary frames and convert them to raw slinear on demand.

Definition in file slinfactory.h.


Define Documentation

#define AST_SLINFACTORY_MAX_HOLD   1280

Definition at line 31 of file slinfactory.h.

Referenced by ast_slinfactory_read().


Function Documentation

unsigned int ast_slinfactory_available ( const struct ast_slinfactory sf)

Retrieve number of samples currently in a slinfactory.

Parameters:
sfThe slinfactory to peek into
Returns:
Number of samples in slinfactory

Definition at line 192 of file slinfactory.c.

References ast_slinfactory::size.

Referenced by ast_audiohook_write_frame(), audio_audiohook_write_list(), audiohook_read_frame_both(), audiohook_read_frame_single(), and softmix_bridge_thread().

{
   return sf->size;
}
void ast_slinfactory_destroy ( struct ast_slinfactory sf)

Destroy the contents of a slinfactory.

Parameters:
sfThe slinfactory that is no longer needed

This function will free any memory allocated for the contents of the slinfactory. It does not free the slinfactory itself. If the sf is malloc'd, then it must be explicitly free'd after calling this function.

Returns:
Nothing

Definition at line 60 of file slinfactory.c.

References ast_frfree, AST_LIST_REMOVE_HEAD, ast_translator_free_path(), f, ast_slinfactory::queue, and ast_slinfactory::trans.

Referenced by ast_audiohook_destroy(), and softmix_bridge_leave().

{
   struct ast_frame *f;

   if (sf->trans) {
      ast_translator_free_path(sf->trans);
      sf->trans = NULL;
   }

   while ((f = AST_LIST_REMOVE_HEAD(&sf->queue, frame_list)))
      ast_frfree(f);
}
int ast_slinfactory_feed ( struct ast_slinfactory sf,
struct ast_frame f 
)

Feed audio into a slinfactory.

Parameters:
sfThe slinfactory to feed into
fFrame containing audio to feed in
Returns:
Number of frames currently in factory

Definition at line 73 of file slinfactory.c.

References ast_frdup(), ast_frfree, ast_frisolate(), ast_getformatname(), AST_LIST_INSERT_TAIL, AST_LIST_NEXT, AST_LIST_TRAVERSE, ast_log(), ast_translate(), ast_translator_build_path(), ast_translator_free_path(), ast_frame::data, f, ast_slinfactory::format, LOG_WARNING, ast_slinfactory::output_format, ast_frame::ptr, ast_slinfactory::queue, ast_frame::samples, ast_slinfactory::size, ast_frame::subclass, and ast_slinfactory::trans.

Referenced by ast_audiohook_write_frame(), and softmix_bridge_write().

{
   struct ast_frame *begin_frame = f, *duped_frame = NULL, *frame_ptr;
   unsigned int x = 0;

   /* In some cases, we can be passed a frame which has no data in it, but
    * which has a positive number of samples defined. Once such situation is
    * when a jitter buffer is in use and the jitter buffer interpolates a frame.
    * The frame it produces has data set to NULL, datalen set to 0, and samples
    * set to either 160 or 240.
    */
   if (!f->data.ptr) {
      return 0;
   }

   if (f->subclass != sf->output_format) {
      if (sf->trans && f->subclass != sf->format) {
         ast_translator_free_path(sf->trans);
         sf->trans = NULL;
      }

      if (!sf->trans) {
         if (!(sf->trans = ast_translator_build_path(sf->output_format, f->subclass))) {
            ast_log(LOG_WARNING, "Cannot build a path from %s to %s\n", ast_getformatname(f->subclass),
               ast_getformatname(sf->output_format));
            return 0;
         }
         sf->format = f->subclass;
      }

      if (!(begin_frame = ast_translate(sf->trans, f, 0))) {
         return 0;
      }
      
      if (!(duped_frame = ast_frisolate(begin_frame))) {
         return 0;
      }

      if (duped_frame != begin_frame) {
         ast_frfree(begin_frame);
      }
   } else {
      if (sf->trans) {
         ast_translator_free_path(sf->trans);
         sf->trans = NULL;
      }
      if (!(duped_frame = ast_frdup(f)))
         return 0;
   }

   AST_LIST_TRAVERSE(&sf->queue, frame_ptr, frame_list) {
      x++;
   }

   /* if the frame was translated, the translator may have returned multiple
      frames, so process each of them
   */
   for (begin_frame = duped_frame; begin_frame; begin_frame = AST_LIST_NEXT(begin_frame, frame_list)) {
      AST_LIST_INSERT_TAIL(&sf->queue, begin_frame, frame_list);
      sf->size += begin_frame->samples;
   }

   return x;
}
void ast_slinfactory_flush ( struct ast_slinfactory sf)

Flush the contents of a slinfactory.

Parameters:
sfThe slinfactory to flush
Returns:
Nothing

Definition at line 197 of file slinfactory.c.

References ast_frfree, AST_LIST_REMOVE_HEAD, ast_translator_free_path(), ast_slinfactory::hold, ast_slinfactory::holdlen, ast_slinfactory::offset, ast_slinfactory::queue, ast_slinfactory::size, and ast_slinfactory::trans.

Referenced by ast_audiohook_write_frame().

{
   struct ast_frame *fr = NULL;

   if (sf->trans) {
      ast_translator_free_path(sf->trans);
      sf->trans = NULL;
   }

   while ((fr = AST_LIST_REMOVE_HEAD(&sf->queue, frame_list)))
      ast_frfree(fr);

   sf->size = sf->holdlen = 0;
   sf->offset = sf->hold;

   return;
}
void ast_slinfactory_init ( struct ast_slinfactory sf)

Initialize a slinfactory.

Parameters:
sfThe slinfactory to initialize
Returns:
Nothing

Definition at line 35 of file slinfactory.c.

References AST_FORMAT_SLINEAR, ast_slinfactory::hold, ast_slinfactory::offset, and ast_slinfactory::output_format.

Referenced by ast_audiohook_init(), and softmix_bridge_join().

{
   memset(sf, 0, sizeof(*sf));
   sf->offset = sf->hold;
   sf->output_format = AST_FORMAT_SLINEAR;
}
int ast_slinfactory_init_rate ( struct ast_slinfactory sf,
unsigned int  sample_rate 
)

Initialize a slinfactory.

Parameters:
sfThe slinfactory to initialize
sample_rateThe output sample rate desired
Returns:
0 on success, non-zero on failure

Definition at line 42 of file slinfactory.c.

References AST_FORMAT_SLINEAR, AST_FORMAT_SLINEAR16, ast_slinfactory::hold, ast_slinfactory::offset, and ast_slinfactory::output_format.

{
   memset(sf, 0, sizeof(*sf));
   sf->offset = sf->hold;
   switch (sample_rate) {
   case 8000:
      sf->output_format = AST_FORMAT_SLINEAR;
      break;
   case 16000:
      sf->output_format = AST_FORMAT_SLINEAR16;
      break;
   default:
      return -1;
   }

   return 0;
}
int ast_slinfactory_read ( struct ast_slinfactory sf,
short *  buf,
size_t  samples 
)

Read samples from a slinfactory.

Parameters:
sfThe slinfactory to read from
bufBuffer to put samples into
samplesNumber of samples wanted
Returns:
Number of samples read

Definition at line 138 of file slinfactory.c.

References ast_frfree, AST_LIST_REMOVE_HEAD, AST_SLINFACTORY_MAX_HOLD, buf, ast_frame::data, ast_slinfactory::hold, ast_slinfactory::holdlen, ast_slinfactory::offset, ast_frame::offset, ast_frame::ptr, ast_slinfactory::queue, ast_frame::samples, and ast_slinfactory::size.

Referenced by audio_audiohook_write_list(), audiohook_read_frame_both(), audiohook_read_frame_single(), and softmix_bridge_thread().

{
   struct ast_frame *frame_ptr;
   unsigned int sofar = 0, ineed, remain;
   short *frame_data, *offset = buf;

   while (sofar < samples) {
      ineed = samples - sofar;

      if (sf->holdlen) {
         if (sf->holdlen <= ineed) {
            memcpy(offset, sf->hold, sf->holdlen * sizeof(*offset));
            sofar += sf->holdlen;
            offset += sf->holdlen;
            sf->holdlen = 0;
            sf->offset = sf->hold;
         } else {
            remain = sf->holdlen - ineed;
            memcpy(offset, sf->offset, ineed * sizeof(*offset));
            sofar += ineed;
            sf->offset += ineed;
            sf->holdlen = remain;
         }
         continue;
      }
      
      if ((frame_ptr = AST_LIST_REMOVE_HEAD(&sf->queue, frame_list))) {
         frame_data = frame_ptr->data.ptr;
         
         if (frame_ptr->samples <= ineed) {
            memcpy(offset, frame_data, frame_ptr->samples * sizeof(*offset));
            sofar += frame_ptr->samples;
            offset += frame_ptr->samples;
         } else {
            remain = frame_ptr->samples - ineed;
            memcpy(offset, frame_data, ineed * sizeof(*offset));
            sofar += ineed;
            frame_data += ineed;
            if (remain > (AST_SLINFACTORY_MAX_HOLD - sf->holdlen)) {
               remain = AST_SLINFACTORY_MAX_HOLD - sf->holdlen;
            }
            memcpy(sf->hold, frame_data, remain * sizeof(*offset));
            sf->holdlen = remain;
         }
         ast_frfree(frame_ptr);
      } else {
         break;
      }
   }

   sf->size -= sofar;
   return sofar;
}