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

Media Format API. More...

#include "asterisk.h"
#include "asterisk/logger.h"
#include "asterisk/codec.h"
#include "asterisk/format.h"
#include "asterisk/astobj2.h"
#include "asterisk/strings.h"
#include "asterisk/module.h"

Go to the source code of this file.

Data Structures

struct  ast_format
 Definition of a media format. More...
 
struct  format_interface
 Structure used when registering a format interface. More...
 

Macros

#define FORMAT_INTERFACE_BUCKETS   53
 Number of buckets to use for format interfaces (should be prime for performance reasons)
 

Functions

int __ast_format_interface_register (const char *codec, const struct ast_format_interface *interface, struct ast_module *mod)
 Register a format interface for use with the provided codec. More...
 
const void * ast_format_attribute_get (const struct ast_format *format, const char *name)
 
struct ast_formatast_format_attribute_set (const struct ast_format *format, const char *name, const char *value)
 Set an attribute on a format to a specific value. More...
 
int ast_format_can_be_smoothed (const struct ast_format *format)
 Get whether or not the format can be smoothed. More...
 
struct ast_formatast_format_clone (const struct ast_format *format)
 Clone an existing media format so it can be modified. More...
 
enum ast_format_cmp_res ast_format_cmp (const struct ast_format *format1, const struct ast_format *format2)
 Compare two formats. More...
 
struct ast_formatast_format_create (struct ast_codec *codec)
 Create a new media format. More...
 
struct ast_formatast_format_create_named (const char *format_name, struct ast_codec *codec)
 Create a new media format with a specific name. More...
 
unsigned int ast_format_determine_length (const struct ast_format *format, unsigned int samples)
 Get the length (in milliseconds) for the format with a given number of samples. More...
 
void ast_format_generate_sdp_fmtp (const struct ast_format *format, unsigned int payload, struct ast_str **str)
 This function is used to produce an fmtp SDP line for an Asterisk format. The attributes present on the Asterisk format are translated into the SDP equivalent. More...
 
void * ast_format_get_attribute_data (const struct ast_format *format)
 Get the attribute data on a format. More...
 
unsigned int ast_format_get_channel_count (const struct ast_format *format)
 Get the channel count on a format. More...
 
struct ast_codecast_format_get_codec (const struct ast_format *format)
 Get the codec associated with a format. More...
 
unsigned int ast_format_get_codec_id (const struct ast_format *format)
 Get the codec identifier associated with a format. More...
 
const char * ast_format_get_codec_name (const struct ast_format *format)
 Get the codec name associated with a format. More...
 
unsigned int ast_format_get_default_ms (const struct ast_format *format)
 Get the default framing size (in milliseconds) for a format. More...
 
unsigned int ast_format_get_maximum_ms (const struct ast_format *format)
 Get the maximum amount of media carried in this format. More...
 
unsigned int ast_format_get_minimum_bytes (const struct ast_format *format)
 Get the minimum number of bytes expected in a frame for this format. More...
 
unsigned int ast_format_get_minimum_ms (const struct ast_format *format)
 Get the minimum amount of media carried in this format. More...
 
const char * ast_format_get_name (const struct ast_format *format)
 Get the name associated with a format. More...
 
unsigned int ast_format_get_sample_rate (const struct ast_format *format)
 Get the sample rate of a media format. More...
 
int ast_format_get_smoother_flags (const struct ast_format *format)
 Get smoother flags for this format. More...
 
enum ast_media_type ast_format_get_type (const struct ast_format *format)
 Get the media type of a format. More...
 
int ast_format_init (void)
 Initialize media format support. More...
 
struct ast_formatast_format_joint (const struct ast_format *format1, const struct ast_format *format2)
 Get a common joint capability between two formats. More...
 
struct ast_formatast_format_parse_sdp_fmtp (const struct ast_format *format, const char *attributes)
 This function is used to have a media format aware module parse and interpret SDP attribute information. Once interpreted this information is stored on the format itself using Asterisk format attributes. More...
 
void ast_format_set_attribute_data (struct ast_format *format, void *attribute_data)
 Set the attribute data on a format. More...
 
void ast_format_set_channel_count (struct ast_format *format, unsigned int channel_count)
 Set the channel count on a format. More...
 
static void format_destroy (void *obj)
 Destructor for media formats.
 
static void format_shutdown (void)
 Function called when the process is shutting down.
 

Variables

static struct ao2_containerinterfaces
 Container for registered format interfaces.
 

Detailed Description

Media Format API.

Author
Joshua Colp jcolp.nosp@m.@dig.nosp@m.ium.c.nosp@m.om

Definition in file format.c.

Function Documentation

int __ast_format_interface_register ( const char *  codec,
const struct ast_format_interface interface,
struct ast_module mod 
)

Register a format interface for use with the provided codec.

Parameters
codecThe name of codec the interface is applicable to
interfaceA pointer to the interface implementation
modThe module this format interface is provided by
Return values
0success
-1failure

Definition at line 90 of file format.c.

References AO2_ALLOC_OPT_LOCK_NOLOCK, ao2_link_flags, ao2_ref, ast_module_shutdown_ref, format_interface::codec, ast_format_interface::format_clone, ast_format_interface::format_destroy, format_interface::interface, lock, OBJ_NOLOCK, OBJ_SEARCH_KEY, and SCOPED_AO2WRLOCK.

91 {
94 
95  if (!interface->format_clone || !interface->format_destroy) {
96  ast_log(LOG_ERROR, "Format interface for codec '%s' does not implement required callbacks\n", codec);
97  return -1;
98  }
99 
100  format_interface = ao2_find(interfaces, codec, OBJ_SEARCH_KEY | OBJ_NOLOCK);
101  if (format_interface) {
102  ast_log(LOG_ERROR, "A format interface is already present for codec '%s'\n", codec);
103  ao2_ref(format_interface, -1);
104  return -1;
105  }
106 
107  format_interface = ao2_alloc_options(sizeof(*format_interface) + strlen(codec) + 1,
109  if (!format_interface) {
110  return -1;
111  }
112  format_interface->interface = interface;
113  strcpy(format_interface->codec, codec); /* Safe */
114 
115  /* Once registered a format interface cannot be unregistered. */
117  ao2_link_flags(interfaces, format_interface, OBJ_NOLOCK);
118  ao2_ref(format_interface, -1);
119 
120  ast_verb(5, "Registered format interface for codec '%s'\n", codec);
121 
122  return 0;
123 }
The arg parameter is a search key, but is not an object.
Definition: astobj2.h:1101
const struct ast_format_interface * interface
Pointer to the format interface itself.
Definition: format.c:59
int(*const format_clone)(const struct ast_format *src, struct ast_format *dst)
Callback for when the format is cloned, used to clone attributes.
Definition: format.h:61
Assume that the ao2_container is already locked.
Definition: astobj2.h:1063
#define ao2_link_flags(container, obj, flags)
Add an object to a container.
Definition: astobj2.h:1554
char codec[0]
Name of the codec the interface is for.
Definition: format.c:61
#define SCOPED_AO2WRLOCK(varname, obj)
scoped lock specialization for ao2 write locks.
Definition: lock.h:614
static struct ao2_container * interfaces
Container for registered format interfaces.
Definition: format.c:65
ast_mutex_t lock
void(*const format_destroy)(struct ast_format *format)
Callback for when the format is destroyed, used to release attribute resources.
Definition: format.h:50
#define ao2_ref(o, delta)
Reference/unreference an object and return the old refcount.
Definition: astobj2.h:459
#define ast_module_shutdown_ref(mod)
Prevent unload of the module before shutdown.
Definition: module.h:478
Structure used when registering a format interface.
Definition: format.c:57
const void* ast_format_attribute_get ( const struct ast_format format,
const char *  name 
)
Since
13.6.0
Parameters
formatThe format to retrieve the attribute from
nameAttribute name
Return values
non-NULLthe attribute value
NULLthe attribute does not exist or is unset

Definition at line 267 of file format.c.

References ao2_ref, ast_format::codec, ast_codec::name, and OBJ_SEARCH_KEY.

268 {
269  const struct ast_format_interface *interface = format->interface;
270 
271  if (!interface) {
272  struct format_interface *format_interface = ao2_find(interfaces, format->codec->name, OBJ_SEARCH_KEY);
273  if (format_interface) {
274  interface = format_interface->interface;
275  ao2_ref(format_interface, -1);
276  }
277  }
278 
280  return NULL;
281  }
282 
283  return interface->format_attribute_get(format, name);
284 }
const char * name
Name for this codec.
Definition: codec.h:46
Optional format interface to extend format operations.
Definition: format.h:44
The arg parameter is a search key, but is not an object.
Definition: astobj2.h:1101
const struct ast_format_interface * interface
Pointer to the format interface itself.
Definition: format.c:59
static struct ao2_container * interfaces
Container for registered format interfaces.
Definition: format.c:65
#define ao2_ref(o, delta)
Reference/unreference an object and return the old refcount.
Definition: astobj2.h:459
struct ast_codec * codec
Pointer to the codec in use for this format.
Definition: format.c:47
const void *(*const format_attribute_get)(const struct ast_format *format, const char *name)
Retrieve a particular format attribute setting.
Definition: format.h:134
Structure used when registering a format interface.
Definition: format.c:57
struct ast_format* ast_format_attribute_set ( const struct ast_format format,
const char *  name,
const char *  value 
)

Set an attribute on a format to a specific value.

Parameters
formatThe format to set the attribute on
nameAttribute name
valueAttribute value
Return values
non-NULLsuccess
NULLfailure

Definition at line 248 of file format.c.

References ao2_bump, ao2_ref, and OBJ_SEARCH_KEY.

249 {
250  const struct ast_format_interface *interface = format->interface;
251 
252  if (!interface) {
253  struct format_interface *format_interface = ao2_find(interfaces, format->codec->name, OBJ_SEARCH_KEY);
254  if (format_interface) {
255  interface = format_interface->interface;
256  ao2_ref(format_interface, -1);
257  }
258  }
259 
261  return ao2_bump((struct ast_format*)format);
262  }
263 
264  return interface->format_attribute_set(format, name, value);
265 }
const char * name
Name for this codec.
Definition: codec.h:46
Optional format interface to extend format operations.
Definition: format.h:44
The arg parameter is a search key, but is not an object.
Definition: astobj2.h:1101
const struct ast_format_interface * interface
Pointer to the format interface itself.
Definition: format.c:59
struct ast_format *(*const format_attribute_set)(const struct ast_format *format, const char *name, const char *value)
Set an attribute on a format.
Definition: format.h:98
Definition of a media format.
Definition: format.c:43
static struct ao2_container * interfaces
Container for registered format interfaces.
Definition: format.c:65
#define ao2_bump(obj)
Bump refcount on an AO2 object by one, returning the object.
Definition: astobj2.h:480
#define ao2_ref(o, delta)
Reference/unreference an object and return the old refcount.
Definition: astobj2.h:459
struct ast_codec * codec
Pointer to the codec in use for this format.
Definition: format.c:47
Structure used when registering a format interface.
Definition: format.c:57
int ast_format_can_be_smoothed ( const struct ast_format format)

Get whether or not the format can be smoothed.

Parameters
formatThe media format
Return values
0the format cannot be smoothed
1the format can be smoothed

Definition at line 344 of file format.c.

References ast_format::codec, and ast_codec::smooth.

Referenced by ast_rtp_write(), and multicast_rtp_write().

345 {
346  return format->codec->smooth;
347 }
struct ast_codec * codec
Pointer to the codec in use for this format.
Definition: format.c:47
unsigned int smooth
Whether the media can be smoothed or not.
Definition: codec.h:78
struct ast_format* ast_format_clone ( const struct ast_format format)

Clone an existing media format so it can be modified.

Parameters
formatThe existing media format
Note
The returned format is a new ao2 object. It must be released using ao2_cleanup.
Return values
non-NULLsuccess
NULLfailure

Definition at line 180 of file format.c.

References ao2_ref, ast_format_create_named(), ast_format::codec, ast_format_interface::format_clone, ast_format::interface, and ast_format::name.

Referenced by test_core_format_attribute_set(), test_core_format_get_joint(), and test_core_format_parse_sdp_fmtp().

181 {
182  struct ast_format *cloned = ast_format_create_named(format->name, format->codec);
183 
184  if (!cloned) {
185  return NULL;
186  }
187 
188  if (cloned->interface && cloned->interface->format_clone(format, cloned)) {
189  ao2_ref(cloned, -1);
190  return NULL;
191  }
192 
193  return cloned;
194 }
const char * name
Definition: format.c:45
int(*const format_clone)(const struct ast_format *src, struct ast_format *dst)
Callback for when the format is cloned, used to clone attributes.
Definition: format.h:61
Definition of a media format.
Definition: format.c:43
#define ao2_ref(o, delta)
Reference/unreference an object and return the old refcount.
Definition: astobj2.h:459
struct ast_codec * codec
Pointer to the codec in use for this format.
Definition: format.c:47
const struct ast_format_interface * interface
Pointer to the optional format interface.
Definition: format.c:51
struct ast_format * ast_format_create_named(const char *format_name, struct ast_codec *codec)
Create a new media format with a specific name.
Definition: format.c:157
enum ast_format_cmp_res ast_format_cmp ( const struct ast_format format1,
const struct ast_format format2 
)

Compare two formats.

Returns
ast_format_cmp_res representing the result of comparing format1 and format2.

Definition at line 201 of file format.c.

References AST_FORMAT_CMP_EQUAL, AST_FORMAT_CMP_NOT_EQUAL, and ast_format::codec.

Referenced by __ast_read(), alarmreceiver_exec(), ast_bridge_channel_restore_formats(), ast_channel_make_compatible_helper(), ast_dsp_process(), ast_format_cache_is_slinear(), ast_format_cap_get_compatible_format(), ast_format_cap_get_format_framing(), ast_format_cap_iscompatible_format(), ast_format_compatibility_format2bitfield(), ast_format_joint(), ast_frame_slinear_sum(), ast_read_image(), ast_rtp_codecs_payload_code_sample_rate(), ast_rtp_codecs_payload_code_tx_sample_rate(), ast_rtp_codecs_payloads_set_rtpmap_type_rate(), ast_rtp_codecs_payloads_unset(), ast_rtp_engine_unload_format(), ast_rtp_get_rate(), ast_rtp_lookup_mime_subtype2(), ast_rtp_lookup_sample_rate2(), ast_rtp_write(), ast_set_read_format_path(), ast_set_write_format_path(), ast_slinfactory_feed(), ast_write_stream(), ast_writestream(), bridge_p2p_rtp_write(), chan_pjsip_read_stream(), fax_detect_framehook(), fax_gateway_framehook(), generic_fax_exec(), jingle_add_payloads_to_description(), make_silence(), rtp_raw_write(), and spandsp_fax_gateway_process().

202 {
203  const struct ast_format_interface *interface;
204 
205  if (format1 == NULL || format2 == NULL) {
207  }
208 
209  if (format1 == format2) {
210  return AST_FORMAT_CMP_EQUAL;
211  }
212 
213  if (format1->codec != format2->codec) {
215  }
216 
217  interface = format1->interface ? format1->interface : format2->interface;
218 
219  if (interface && interface->format_cmp) {
220  return interface->format_cmp(format1, format2);
221  }
222 
223  return AST_FORMAT_CMP_EQUAL;
224 }
Optional format interface to extend format operations.
Definition: format.h:44
enum ast_format_cmp_res(*const format_cmp)(const struct ast_format *format1, const struct ast_format *format2)
Determine if format 1 is a subset of format 2.
Definition: format.h:71
struct ast_codec * codec
Pointer to the codec in use for this format.
Definition: format.c:47
struct ast_format* ast_format_create ( struct ast_codec codec)

Create a new media format.

Parameters
codecThe codec to use
Return values
non-NULLsuccess
NULLfailure
Note
The format is returned with reference count incremented. It must be released using ao2_ref or ao2_cleanup.

Definition at line 196 of file format.c.

References ast_format_create_named(), and ast_codec::name.

Referenced by ast_format_cap_append_by_type(), and newpvt().

197 {
198  return ast_format_create_named(codec->name, codec);
199 }
const char * name
Name for this codec.
Definition: codec.h:46
struct ast_format * ast_format_create_named(const char *format_name, struct ast_codec *codec)
Create a new media format with a specific name.
Definition: format.c:157
struct ast_format* ast_format_create_named ( const char *  format_name,
struct ast_codec codec 
)

Create a new media format with a specific name.

Parameters
format_nameThe name to use for the format
codecThe codec to use
Note
This creation function should be used when the name of the codec cannot be explicitly used for the name of the format. This is the case for codecs with multiple sample rates
The format is returned with reference count incremented. It must be released using ao2_ref or ao2_cleanup.
Return values
non-NULLsuccess
NULLfailure

Definition at line 157 of file format.c.

References AO2_ALLOC_OPT_LOCK_NOLOCK, ao2_bump, ao2_ref, ao2_t_alloc_options, ast_format::channel_count, ast_format::codec, ast_codec::description, format_destroy(), ast_format::interface, format_interface::interface, ast_format::name, ast_codec::name, OBJ_SEARCH_KEY, and S_OR.

Referenced by ast_format_clone(), and ast_format_create().

158 {
159  struct ast_format *format;
161 
162  format = ao2_t_alloc_options(sizeof(*format), format_destroy,
164  if (!format) {
165  return NULL;
166  }
167  format->name = format_name;
168  format->codec = ao2_bump(codec);
169  format->channel_count = 1;
170 
171  format_interface = ao2_find(interfaces, codec->name, OBJ_SEARCH_KEY);
172  if (format_interface) {
173  format->interface = format_interface->interface;
174  ao2_ref(format_interface, -1);
175  }
176 
177  return format;
178 }
const char * name
Name for this codec.
Definition: codec.h:46
const char * name
Definition: format.c:45
The arg parameter is a search key, but is not an object.
Definition: astobj2.h:1101
const struct ast_format_interface * interface
Pointer to the format interface itself.
Definition: format.c:59
Definition of a media format.
Definition: format.c:43
#define ao2_t_alloc_options(data_size, destructor_fn, options, debug_msg)
Allocate and initialize an object.
Definition: astobj2.h:402
static struct ao2_container * interfaces
Container for registered format interfaces.
Definition: format.c:65
#define ao2_bump(obj)
Bump refcount on an AO2 object by one, returning the object.
Definition: astobj2.h:480
static void format_destroy(void *obj)
Destructor for media formats.
Definition: format.c:146
#define ao2_ref(o, delta)
Reference/unreference an object and return the old refcount.
Definition: astobj2.h:459
const char * description
Brief description.
Definition: codec.h:48
struct ast_codec * codec
Pointer to the codec in use for this format.
Definition: format.c:47
Structure used when registering a format interface.
Definition: format.c:57
const struct ast_format_interface * interface
Pointer to the optional format interface.
Definition: format.c:51
unsigned int channel_count
The number if audio channels used, if more than one an interleaved format is required.
Definition: format.c:53
#define S_OR(a, b)
returns the equivalent of logic or for strings: first one if not empty, otherwise second one...
Definition: strings.h:80
unsigned int ast_format_determine_length ( const struct ast_format format,
unsigned int  samples 
)

Get the length (in milliseconds) for the format with a given number of samples.

Parameters
formatThe media format
samplesThe number of samples
Returns
length of media (in milliseconds)

Definition at line 384 of file format.c.

References ast_codec_determine_length(), and ast_format::codec.

385 {
386  return ast_codec_determine_length(format->codec, samples);
387 }
struct ast_codec * codec
Pointer to the codec in use for this format.
Definition: format.c:47
unsigned int ast_codec_determine_length(const struct ast_codec *codec, unsigned int samples)
Get the length of media (in milliseconds) given a number of samples.
Definition: codec.c:408
void ast_format_generate_sdp_fmtp ( const struct ast_format format,
unsigned int  payload,
struct ast_str **  str 
)

This function is used to produce an fmtp SDP line for an Asterisk format. The attributes present on the Asterisk format are translated into the SDP equivalent.

Parameters
formatto generate an fmtp line for
payloadnumerical payload for the fmtp line
strstructure that the fmtp line will be appended to

Definition at line 305 of file format.c.

References ao2_ref, ast_format::codec, ast_codec::name, and OBJ_SEARCH_KEY.

306 {
307  const struct ast_format_interface *interface = format->interface;
308 
309  if (!interface) {
310  struct format_interface *format_interface = ao2_find(interfaces, format->codec->name, OBJ_SEARCH_KEY);
311  if (format_interface) {
312  interface = format_interface->interface;
313  ao2_ref(format_interface, -1);
314  }
315  }
316 
318  return;
319  }
320 
321  interface->format_generate_sdp_fmtp(format, payload, str);
322 }
const char * name
Name for this codec.
Definition: codec.h:46
Optional format interface to extend format operations.
Definition: format.h:44
The arg parameter is a search key, but is not an object.
Definition: astobj2.h:1101
const struct ast_format_interface * interface
Pointer to the format interface itself.
Definition: format.c:59
static struct ao2_container * interfaces
Container for registered format interfaces.
Definition: format.c:65
void(*const format_generate_sdp_fmtp)(const struct ast_format *format, unsigned int payload, struct ast_str **str)
Generate SDP attribute information from an ast_format structure.
Definition: format.h:121
#define ao2_ref(o, delta)
Reference/unreference an object and return the old refcount.
Definition: astobj2.h:459
struct ast_codec * codec
Pointer to the codec in use for this format.
Definition: format.c:47
Structure used when registering a format interface.
Definition: format.c:57
void* ast_format_get_attribute_data ( const struct ast_format format)

Get the attribute data on a format.

Parameters
formatThe media format
Returns
Currently set attribute data

Definition at line 125 of file format.c.

References ast_format::attribute_data.

Referenced by ilbctolin_framein(), lintoilbc_frameout(), test_core_format_attribute_get(), test_core_format_attribute_set(), test_core_format_clone(), test_core_format_cmp(), test_core_format_destroy(), test_core_format_generate_sdp_fmtp(), test_core_format_get_joint(), and test_core_format_parse_sdp_fmtp().

126 {
127  return format->attribute_data;
128 }
void * attribute_data
Attribute specific data, implementation specific.
Definition: format.c:49
unsigned int ast_format_get_channel_count ( const struct ast_format format)

Get the channel count on a format.

Parameters
formatThe media format
Returns
Currently set channel count

Definition at line 135 of file format.c.

References ast_format::channel_count.

Referenced by softmix_bridge_join().

136 {
137  return format->channel_count;
138 }
unsigned int channel_count
The number if audio channels used, if more than one an interleaved format is required.
Definition: format.c:53
struct ast_codec* ast_format_get_codec ( const struct ast_format format)

Get the codec associated with a format.

Parameters
formatThe media format
Returns
The codec
Note
The reference count of the returned codec is increased by 1 and must be decremented

Definition at line 324 of file format.c.

References ao2_bump, and ast_format::codec.

Referenced by ast_codec_samples_count(), ast_format_cache_get_by_codec(), ast_format_cap_append_by_type(), and ast_translator_best_choice().

325 {
326  return ao2_bump(format->codec);
327 }
#define ao2_bump(obj)
Bump refcount on an AO2 object by one, returning the object.
Definition: astobj2.h:480
struct ast_codec * codec
Pointer to the codec in use for this format.
Definition: format.c:47
unsigned int ast_format_get_codec_id ( const struct ast_format format)

Get the codec identifier associated with a format.

Parameters
formatThe media format
Returns
codec identifier

Definition at line 329 of file format.c.

References ast_format::codec, and ast_codec::id.

Referenced by ast_format_cap_get_compatible_format(), ast_format_cap_get_format_framing(), ast_format_cap_iscompatible_format(), ast_format_cap_remove(), ast_format_compatibility_codec2bitfield(), and ast_slinfactory_feed().

330 {
331  return format->codec->id;
332 }
unsigned int id
Internal unique identifier for this codec, set at registration time (starts at 1) ...
Definition: codec.h:44
struct ast_codec * codec
Pointer to the codec in use for this format.
Definition: format.c:47
const char* ast_format_get_codec_name ( const struct ast_format format)

Get the codec name associated with a format.

Parameters
formatThe media format
Returns
The codec name

Definition at line 339 of file format.c.

References ast_format::codec, and ast_codec::name.

Referenced by ast_rtp_engine_load_format().

340 {
341  return format->codec->name;
342 }
const char * name
Name for this codec.
Definition: codec.h:46
struct ast_codec * codec
Pointer to the codec in use for this format.
Definition: format.c:47
unsigned int ast_format_get_default_ms ( const struct ast_format format)

Get the default framing size (in milliseconds) for a format.

Parameters
formatThe media format
Returns
default framing size in milliseconds

Definition at line 359 of file format.c.

References ast_format::codec, and ast_codec::default_ms.

Referenced by ast_format_cap_get_format_framing(), ast_rtp_write(), and multicast_rtp_write().

360 {
361  return format->codec->default_ms;
362 }
unsigned int default_ms
Default length of media carried (in milliseconds) in a frame.
Definition: codec.h:58
struct ast_codec * codec
Pointer to the codec in use for this format.
Definition: format.c:47
unsigned int ast_format_get_maximum_ms ( const struct ast_format format)

Get the maximum amount of media carried in this format.

Parameters
formatThe media format
Returns
maximum framing size in milliseconds

Definition at line 369 of file format.c.

References ast_format::codec, and ast_codec::maximum_ms.

Referenced by create_outgoing_sdp_stream().

370 {
371  return format->codec->maximum_ms;
372 }
unsigned int maximum_ms
Maximum length of media that can be carried (in milliseconds) in a frame.
Definition: codec.h:56
struct ast_codec * codec
Pointer to the codec in use for this format.
Definition: format.c:47
unsigned int ast_format_get_minimum_bytes ( const struct ast_format format)

Get the minimum number of bytes expected in a frame for this format.

Parameters
formatThe media format
Returns
minimum expected bytes in a frame for this format

Definition at line 374 of file format.c.

References ast_format::codec, and ast_codec::minimum_bytes.

Referenced by ast_rtp_write(), and multicast_rtp_write().

375 {
376  return format->codec->minimum_bytes;
377 }
unsigned int minimum_bytes
Length in bytes of the data payload of a minimum_ms frame.
Definition: codec.h:60
struct ast_codec * codec
Pointer to the codec in use for this format.
Definition: format.c:47
unsigned int ast_format_get_minimum_ms ( const struct ast_format format)

Get the minimum amount of media carried in this format.

Parameters
formatThe media format
Returns
minimum framing size in milliseconds

Definition at line 364 of file format.c.

References ast_format::codec, and ast_codec::minimum_ms.

Referenced by ast_rtp_write(), and multicast_rtp_write().

365 {
366  return format->codec->minimum_ms;
367 }
struct ast_codec * codec
Pointer to the codec in use for this format.
Definition: format.c:47
unsigned int minimum_ms
Minimum length of media that can be carried (in milliseconds) in a frame.
Definition: codec.h:54
const char* ast_format_get_name ( const struct ast_format format)
unsigned int ast_format_get_sample_rate ( const struct ast_format format)

Get the sample rate of a media format.

Parameters
formatThe media format
Returns
sample rate

Definition at line 379 of file format.c.

References ast_format::codec, and ast_codec::sample_rate.

Referenced by ast_channel_make_compatible_helper(), ast_ratestream(), ast_rtp_dtmf_begin(), ast_rtp_engine_load_format(), ast_rtp_get_rate(), ast_translate(), ast_translator_best_choice(), ast_translator_build_path(), ogg_speex_open(), schedule_delivery(), snoop_determine_format(), stasis_app_control_snoop(), and waitstream_core().

380 {
381  return format->codec->sample_rate ?: 8000;
382 }
struct ast_codec * codec
Pointer to the codec in use for this format.
Definition: format.c:47
unsigned int sample_rate
Sample rate (number of samples carried in a second)
Definition: codec.h:52
int ast_format_get_smoother_flags ( const struct ast_format format)

Get smoother flags for this format.

Since
13.17.0
Parameters
formatThe media format
Returns
smoother flags for the provided format

Definition at line 349 of file format.c.

References ast_format::codec, and ast_codec::smoother_flags.

Referenced by ast_rtp_write(), and multicast_rtp_write().

350 {
351  return format->codec->smoother_flags;
352 }
unsigned int smoother_flags
Flags to be passed to the smoother.
Definition: codec.h:80
struct ast_codec * codec
Pointer to the codec in use for this format.
Definition: format.c:47
enum ast_media_type ast_format_get_type ( const struct ast_format format)

Get the media type of a format.

Parameters
formatThe media format
Returns
the media type

Definition at line 354 of file format.c.

References ast_format::codec, and ast_codec::type.

Referenced by __ast_read(), ast_ari_recordings_get_stored_file(), ast_format_cap_append_from_cap(), ast_format_cap_get_best_by_type(), ast_format_cap_has_type(), ast_format_cap_remove_by_type(), ast_format_cap_replace_from_cap(), ast_openvstream(), ast_playstream(), ast_rtp_codecs_get_stream_type(), ast_rtp_engine_load_format(), ast_translator_best_choice(), ast_translator_build_path(), ast_write_stream(), ast_writestream(), create_outgoing_sdp_stream(), and jingle_add_payloads_to_description().

355 {
356  return format->codec->type;
357 }
struct ast_codec * codec
Pointer to the codec in use for this format.
Definition: format.c:47
enum ast_media_type type
Type of media this codec contains.
Definition: codec.h:50
int ast_format_init ( void  )

Initialize media format support.

Return values
0success
-1failure

Definition at line 77 of file format.c.

References AO2_ALLOC_OPT_LOCK_RWLOCK, ao2_container_alloc_hash, ast_register_cleanup(), FORMAT_INTERFACE_BUCKETS, and format_shutdown().

78 {
80  FORMAT_INTERFACE_BUCKETS, format_interface_hash_fn, NULL, format_interface_cmp_fn);
81  if (!interfaces) {
82  return -1;
83  }
84 
86 
87  return 0;
88 }
static struct ao2_container * interfaces
Container for registered format interfaces.
Definition: format.c:65
int ast_register_cleanup(void(*func)(void))
Register a function to be executed before Asterisk gracefully exits.
Definition: clicompat.c:19
#define FORMAT_INTERFACE_BUCKETS
Number of buckets to use for format interfaces (should be prime for performance reasons) ...
Definition: format.c:40
#define ao2_container_alloc_hash(ao2_options, container_options, n_buckets, hash_fn, sort_fn, cmp_fn)
Allocate and initialize a hash container with the desired number of buckets.
Definition: astobj2.h:1303
static void format_shutdown(void)
Function called when the process is shutting down.
Definition: format.c:71
struct ast_format* ast_format_joint ( const struct ast_format format1,
const struct ast_format format2 
)

Get a common joint capability between two formats.

Return values
non-NULLif joint capability exists
NULLif no joint capability exists
Note
The returned format must be treated as immutable.

Definition at line 226 of file format.c.

References ao2_bump, ast_format_cmp(), AST_FORMAT_CMP_EQUAL, ast_format::attribute_data, and ast_format::codec.

Referenced by ast_format_cap_get_compatible_format().

227 {
228  const struct ast_format_interface *interface;
229 
230  if (format1->codec != format2->codec) {
231  return NULL;
232  }
233 
234  /* If the two formats are the same structure OR if the codec is the same and no attributes
235  * exist we can immediately return a format with reference count bumped up, since they are
236  * the same.
237  */
238  if ((ast_format_cmp(format1, format2) == AST_FORMAT_CMP_EQUAL && !format1->attribute_data && !format2->attribute_data)) {
239  return ao2_bump((struct ast_format*)format1);
240  }
241 
242  interface = format1->interface ? format1->interface : format2->interface;
243 
244  /* If there is attribute data on either there has to be an interface */
245  return interface->format_get_joint(format1, format2);
246 }
Optional format interface to extend format operations.
Definition: format.h:44
Definition of a media format.
Definition: format.c:43
#define ao2_bump(obj)
Bump refcount on an AO2 object by one, returning the object.
Definition: astobj2.h:480
void * attribute_data
Attribute specific data, implementation specific.
Definition: format.c:49
struct ast_codec * codec
Pointer to the codec in use for this format.
Definition: format.c:47
enum ast_format_cmp_res ast_format_cmp(const struct ast_format *format1, const struct ast_format *format2)
Compare two formats.
Definition: format.c:201
struct ast_format* ast_format_parse_sdp_fmtp ( const struct ast_format format,
const char *  attributes 
)

This function is used to have a media format aware module parse and interpret SDP attribute information. Once interpreted this information is stored on the format itself using Asterisk format attributes.

Parameters
formatto set
attributesstring containing the fmtp line from the SDP
Return values
non-NULLsuccess, attribute values were valid
NULLfailure, values were not acceptable

Definition at line 286 of file format.c.

References ao2_bump, ao2_ref, ast_format::codec, ast_codec::name, and OBJ_SEARCH_KEY.

Referenced by ast_rtp_codecs_payloads_set_rtpmap_type_rate().

287 {
288  const struct ast_format_interface *interface = format->interface;
289 
290  if (!interface) {
291  struct format_interface *format_interface = ao2_find(interfaces, format->codec->name, OBJ_SEARCH_KEY);
292  if (format_interface) {
293  interface = format_interface->interface;
294  ao2_ref(format_interface, -1);
295  }
296  }
297 
299  return ao2_bump((struct ast_format*)format);
300  }
301 
302  return interface->format_parse_sdp_fmtp(format, attributes);
303 }
const char * name
Name for this codec.
Definition: codec.h:46
Optional format interface to extend format operations.
Definition: format.h:44
The arg parameter is a search key, but is not an object.
Definition: astobj2.h:1101
const struct ast_format_interface * interface
Pointer to the format interface itself.
Definition: format.c:59
Definition of a media format.
Definition: format.c:43
static struct ao2_container * interfaces
Container for registered format interfaces.
Definition: format.c:65
#define ao2_bump(obj)
Bump refcount on an AO2 object by one, returning the object.
Definition: astobj2.h:480
#define ao2_ref(o, delta)
Reference/unreference an object and return the old refcount.
Definition: astobj2.h:459
struct ast_codec * codec
Pointer to the codec in use for this format.
Definition: format.c:47
struct ast_format *(*const format_parse_sdp_fmtp)(const struct ast_format *format, const char *attributes)
Parse SDP attribute information, interpret it, and store it in the format structure.
Definition: format.h:110
Structure used when registering a format interface.
Definition: format.c:57
void ast_format_set_attribute_data ( struct ast_format format,
void *  attribute_data 
)

Set the attribute data on a format.

Parameters
formatThe media format
attribute_dataThe attribute data

Definition at line 130 of file format.c.

References ast_format::attribute_data.

Referenced by test_core_format_clone().

131 {
132  format->attribute_data = attribute_data;
133 }
void * attribute_data
Attribute specific data, implementation specific.
Definition: format.c:49
void ast_format_set_channel_count ( struct ast_format format,
unsigned int  channel_count 
)

Set the channel count on a format.

Parameters
formatThe media format
channel_countThe number of audio channels used

Definition at line 140 of file format.c.

References ast_format::channel_count.

141 {
142  format->channel_count = channel_count;
143 }
unsigned int channel_count
The number if audio channels used, if more than one an interleaved format is required.
Definition: format.c:53