1 /* $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  */
pjmedia_port_info_init(pjmedia_port_info * info,const pj_str_t * name,unsigned signature,unsigned clock_rate,unsigned channel_count,unsigned bits_per_sample,unsigned samples_per_frame)33 PJ_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_ASSERT_RETURN(clock_rate && channel_count, PJ_EINVAL);
45 
46     pj_bzero(info, sizeof(*info));
47 
48     info->signature = signature;
49     info->dir = PJMEDIA_DIR_ENCODING_DECODING;
50     info->name = *name;
51 
52     frame_time_usec = (unsigned)(samples_per_frame * USEC_IN_SEC /
53 				 channel_count / clock_rate);
54     avg_bps = clock_rate * channel_count * bits_per_sample;
55 
56     pjmedia_format_init_audio(&info->fmt, PJMEDIA_FORMAT_L16, clock_rate,
57 			      channel_count, bits_per_sample, frame_time_usec,
58 			      avg_bps, avg_bps);
59 
60     return PJ_SUCCESS;
61 }
62 
pjmedia_port_info_init2(pjmedia_port_info * info,const pj_str_t * name,unsigned signature,pjmedia_dir dir,const pjmedia_format * fmt)63 PJ_DEF(pj_status_t) pjmedia_port_info_init2( pjmedia_port_info *info,
64 					     const pj_str_t *name,
65 					     unsigned signature,
66 					     pjmedia_dir dir,
67 					     const pjmedia_format *fmt)
68 {
69     pj_bzero(info, sizeof(*info));
70     info->signature = signature;
71     info->dir = dir;
72     info->name = *name;
73 
74     pjmedia_format_copy(&info->fmt, fmt);
75 
76     return PJ_SUCCESS;
77 }
78 
79 /**
80  * Get a clock source from the port.
81  */
pjmedia_port_get_clock_src(pjmedia_port * port,pjmedia_dir dir)82 PJ_DEF(pjmedia_clock_src *) pjmedia_port_get_clock_src( pjmedia_port *port,
83                                                         pjmedia_dir dir )
84 {
85     if (port && port->get_clock_src)
86 	return port->get_clock_src(port, dir);
87     else
88 	return NULL;
89 }
90 
91 /**
92  * Get a frame from the port (and subsequent downstream ports).
93  */
pjmedia_port_get_frame(pjmedia_port * port,pjmedia_frame * frame)94 PJ_DEF(pj_status_t) pjmedia_port_get_frame( pjmedia_port *port,
95 					    pjmedia_frame *frame )
96 {
97     PJ_ASSERT_RETURN(port && frame, PJ_EINVAL);
98 
99     if (port->get_frame)
100 	return port->get_frame(port, frame);
101     else {
102 	frame->type = PJMEDIA_FRAME_TYPE_NONE;
103 	return PJ_EINVALIDOP;
104     }
105 }
106 
107 
108 /**
109  * Put a frame to the port (and subsequent downstream ports).
110  */
pjmedia_port_put_frame(pjmedia_port * port,pjmedia_frame * frame)111 PJ_DEF(pj_status_t) pjmedia_port_put_frame( pjmedia_port *port,
112 					    pjmedia_frame *frame )
113 {
114     PJ_ASSERT_RETURN(port && frame, PJ_EINVAL);
115 
116     if (port->put_frame)
117 	return port->put_frame(port, frame);
118     else
119 	return PJ_EINVALIDOP;
120 }
121 
122 /**
123  * Destroy port (and subsequent downstream ports)
124  */
pjmedia_port_destroy(pjmedia_port * port)125 PJ_DEF(pj_status_t) pjmedia_port_destroy( pjmedia_port *port )
126 {
127     pj_status_t status;
128 
129     PJ_ASSERT_RETURN(port, PJ_EINVAL);
130 
131     if (port->on_destroy)
132 	status = port->on_destroy(port);
133     else
134 	status = PJ_SUCCESS;
135 
136     return status;
137 }
138 
139 
140 
141