blob: b7defae5e39a7d54e8f8eabe9ef49abe9baf2630 [file] [log] [blame]
Tristan Matthews0a329cc2013-07-17 13:20:14 -04001/* $Id$ */
2/*
3 * Copyright (C) 2008-2011 Teluu Inc. (http://www.teluu.com)
4 * Copyright (C) 2003-2008 Benny Prijono <benny@prijono.org>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 */
20#include <pjmedia/port.h>
21#include <pjmedia/errno.h>
22#include <pj/assert.h>
23#include <pj/log.h>
24#include <pj/pool.h>
25
26#define THIS_FILE "port.c"
27
28
29/**
30 * This is an auxiliary function to initialize port info for
31 * ports which deal with PCM audio.
32 */
33PJ_DEF(pj_status_t) pjmedia_port_info_init( pjmedia_port_info *info,
34 const pj_str_t *name,
35 unsigned signature,
36 unsigned clock_rate,
37 unsigned channel_count,
38 unsigned bits_per_sample,
39 unsigned samples_per_frame)
40{
41#define USEC_IN_SEC (pj_uint64_t)1000000
42 unsigned frame_time_usec, avg_bps;
43
44 pj_bzero(info, sizeof(*info));
45
46 info->signature = signature;
47 info->dir = PJMEDIA_DIR_ENCODING_DECODING;
48 info->name = *name;
49
50 frame_time_usec = (unsigned)(samples_per_frame * USEC_IN_SEC /
51 channel_count / clock_rate);
52 avg_bps = clock_rate * channel_count * bits_per_sample;
53
54 pjmedia_format_init_audio(&info->fmt, PJMEDIA_FORMAT_L16, clock_rate,
55 channel_count, bits_per_sample, frame_time_usec,
56 avg_bps, avg_bps);
57
58 return PJ_SUCCESS;
59}
60
61PJ_DEF(pj_status_t) pjmedia_port_info_init2( pjmedia_port_info *info,
62 const pj_str_t *name,
63 unsigned signature,
64 pjmedia_dir dir,
65 const pjmedia_format *fmt)
66{
67 pj_bzero(info, sizeof(*info));
68 info->signature = signature;
69 info->dir = dir;
70 info->name = *name;
71
72 pjmedia_format_copy(&info->fmt, fmt);
73
74 return PJ_SUCCESS;
75}
76
77/**
78 * Get a clock source from the port.
79 */
80PJ_DEF(pjmedia_clock_src *) pjmedia_port_get_clock_src( pjmedia_port *port,
81 pjmedia_dir dir )
82{
83 if (port && port->get_clock_src)
84 return port->get_clock_src(port, dir);
85 else
86 return NULL;
87}
88
89/**
90 * Get a frame from the port (and subsequent downstream ports).
91 */
92PJ_DEF(pj_status_t) pjmedia_port_get_frame( pjmedia_port *port,
93 pjmedia_frame *frame )
94{
95 PJ_ASSERT_RETURN(port && frame, PJ_EINVAL);
96
97 if (port->get_frame)
98 return port->get_frame(port, frame);
99 else {
100 frame->type = PJMEDIA_FRAME_TYPE_NONE;
101 return PJ_EINVALIDOP;
102 }
103}
104
105
106/**
107 * Put a frame to the port (and subsequent downstream ports).
108 */
109PJ_DEF(pj_status_t) pjmedia_port_put_frame( pjmedia_port *port,
110 pjmedia_frame *frame )
111{
112 PJ_ASSERT_RETURN(port && frame, PJ_EINVAL);
113
114 if (port->put_frame)
115 return port->put_frame(port, frame);
116 else
117 return PJ_EINVALIDOP;
118}
119
120/**
121 * Destroy port (and subsequent downstream ports)
122 */
123PJ_DEF(pj_status_t) pjmedia_port_destroy( pjmedia_port *port )
124{
125 pj_status_t status;
126
127 PJ_ASSERT_RETURN(port, PJ_EINVAL);
128
129 if (port->on_destroy)
130 status = port->on_destroy(port);
131 else
132 status = PJ_SUCCESS;
133
134 return status;
135}
136
137
138