Debian Codegraph @

Xpjproject/pjsip-apps/src/samples/util.h

raw symbols SCIP
1 /*
2 * Copyright (C) 2008-2011 Teluu Inc. (http://www.teluu.com)
3 * Copyright (C) 2003-2008 Benny Prijono <[email protected]>
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 */
19 #include <stdlib.h> /* strtol() */
20
21 /* Util to display the error message for the specified error code */
22 static int app_perror( const char *sender, const char *title,
23 pj_status_t status)
24 {
25 char errmsg[PJ_ERR_MSG_SIZE];
26
27 pj_strerror(status, errmsg, sizeof(errmsg));
28
29 PJ_LOG(3,(sender, "%s: %s [code=%d]", title, errmsg, status));
30 return 1;
31 }
32
33 #define STRLIT_(expr) #expr
34 #define STRLIT(expr) STRLIT_(expr)
35 #define CHECK(expr) do { \
36 status = expr; \
37 if (status!=PJ_SUCCESS && status!=PJ_EPENDING) { \
38 PJ_PERROR(1,(THIS_FILE, status, \
39 "In " __FILE__ ":" STRLIT(__LINE__))); \
40 goto on_error; \
41 } \
42 } while (0)
43
44 /* Constants */
45 #define CLOCK_RATE 44100
46 #define NSAMPLES (CLOCK_RATE * 20 / 1000)
47 #define NCHANNELS 1
48 #define NBITS 16
49
50 /*
51 * Common sound options.
52 */
53 #define SND_USAGE \
54 " -d, --dev=NUM Sound device use device id NUM (default=-1) \n"\
55 " -r, --rate=HZ Set clock rate in samples per sec (default=44100)\n"\
56 " -c, --channel=NUM Set # of channels (default=1 for mono). \n"\
57 " -f, --frame=NUM Set # of samples per frame (default equival 20ms)\n"\
58 " -b, --bit=NUM Set # of bits per sample (default=16) \n"
59
60
61 /*
62 * This utility function parses the command line and look for
63 * common sound options.
64 */
65 pj_status_t get_snd_options(const char *app_name,
66 int argc,
67 char *argv[],
68 int *dev_id,
69 int *clock_rate,
70 int *channel_count,
71 int *samples_per_frame,
72 int *bits_per_sample)
73 {
74 struct pj_getopt_option long_options[] = {
75 { "dev", 1, 0, 'd' },
76 { "rate", 1, 0, 'r' },
77 { "channel", 1, 0, 'c' },
78 { "frame", 1, 0, 'f' },
79 { "bit", 1, 0, 'b' },
80 { NULL, 0, 0, 0 },
81 };
82 int c;
83 int option_index;
84 long val;
85 char *err;
86
87 *samples_per_frame = 0;
88
89 pj_optind = 0;
90 while((c=pj_getopt_long(argc,argv, "d:r:c:f:b:",
91 long_options, &option_index))!=-1)
92 {
93
94 switch (c) {
95 case 'd':
96 /* device */
97 val = strtol(pj_optarg, &err, 10);
98 if (*err) {
99 PJ_LOG(3,(app_name, "Error: invalid value for device id"));
100 return PJ_EINVAL;
101 }
102 *dev_id = val;
103 break;
104
105 case 'r':
106 /* rate */
107 val = strtol(pj_optarg, &err, 10);
108 if (*err) {
109 PJ_LOG(3,(app_name, "Error: invalid value for clock rate"));
110 return PJ_EINVAL;
111 }
112 *clock_rate = val;
113 break;
114
115 case 'c':
116 /* channel count */
117 val = strtol(pj_optarg, &err, 10);
118 if (*err) {
119 PJ_LOG(3,(app_name, "Error: invalid channel count"));
120 return PJ_EINVAL;
121 }
122 *channel_count = val;
123 break;
124
125 case 'f':
126 /* frame count/samples per frame */
127 val = strtol(pj_optarg, &err, 10);
128 if (*err) {
129 PJ_LOG(3,(app_name, "Error: invalid samples per frame"));
130 return PJ_EINVAL;
131 }
132 *samples_per_frame = val;
133 break;
134
135 case 'b':
136 /* bit per sample */
137 val = strtol(pj_optarg, &err, 10);
138 if (*err) {
139 PJ_LOG(3,(app_name, "Error: invalid samples bits per sample"));
140 return PJ_EINVAL;
141 }
142 *bits_per_sample = val;
143 break;
144
145 default:
146 /* Unknown options */
147 PJ_LOG(3,(app_name, "Error: unknown options '%c'", pj_optopt));
148 return PJ_EINVAL;
149 }
150
151 }
152
153 if (*samples_per_frame == 0) {
154 *samples_per_frame = *clock_rate * *channel_count * 20 / 1000;
155 }
156
157 return 0;
158 }
159
160
161 /* Dump memory pool usage. */
162 void dump_pool_usage( const char *app_name, pj_caching_pool *cp )
163 {
164 #if !defined(PJ_HAS_POOL_ALT_API) || PJ_HAS_POOL_ALT_API==0
165 pj_pool_t *p;
166 pj_size_t total_alloc = 0;
167 pj_size_t total_used = 0;
168
169 /* Accumulate memory usage in active list. */
170 p = (pj_pool_t*)cp->used_list.next;
171 while (p != (pj_pool_t*) &cp->used_list) {
172 total_alloc += pj_pool_get_capacity(p);
173 total_used += pj_pool_get_used_size(p);
174 p = p->next;
175 }
176
177 PJ_LOG(3, (app_name, "Total pool memory allocated=%lu KB, used=%lu KB",
178 total_alloc / 1000,
179 total_used / 1000));
180 #endif
181 }