blob: c5d8fd6ca685100b5a9107b4c121baadd1f67da5 [file] [log] [blame]
Nanang Izzuddin7adfc862011-05-05 05:33:27 +00001/* $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 */
Benny Prijonoe7483e52006-04-06 21:08:35 +000020#include <stdlib.h> /* strtol() */
Benny Prijono6107a002006-03-17 18:01:27 +000021
Benny Prijonobc797312006-03-24 20:44:27 +000022/* Util to display the error message for the specified error code */
Benny Prijono6107a002006-03-17 18:01:27 +000023static int app_perror( const char *sender, const char *title,
24 pj_status_t status)
25{
26 char errmsg[PJ_ERR_MSG_SIZE];
27
28 pj_strerror(status, errmsg, sizeof(errmsg));
29
Benny Prijonobc797312006-03-24 20:44:27 +000030 PJ_LOG(3,(sender, "%s: %s [code=%d]", title, errmsg, status));
Benny Prijono6107a002006-03-17 18:01:27 +000031 return 1;
32}
33
Benny Prijonobc797312006-03-24 20:44:27 +000034
35
36/* Constants */
37#define CLOCK_RATE 44100
38#define NSAMPLES (CLOCK_RATE * 20 / 1000)
39#define NCHANNELS 1
40#define NBITS 16
41
Benny Prijono6107a002006-03-17 18:01:27 +000042/*
Benny Prijonobc797312006-03-24 20:44:27 +000043 * Common sound options.
Benny Prijono6107a002006-03-17 18:01:27 +000044 */
Benny Prijonobc797312006-03-24 20:44:27 +000045#define SND_USAGE \
46" -d, --dev=NUM Sound device use device id NUM (default=-1) \n"\
47" -r, --rate=HZ Set clock rate in samples per sec (default=44100)\n"\
48" -c, --channel=NUM Set # of channels (default=1 for mono). \n"\
49" -f, --frame=NUM Set # of samples per frame (default equival 20ms)\n"\
50" -b, --bit=NUM Set # of bits per sample (default=16) \n"
51
52
53/*
54 * This utility function parses the command line and look for
55 * common sound options.
56 */
Benny Prijonobf13fee2006-04-20 11:13:32 +000057pj_status_t get_snd_options(const char *app_name,
58 int argc,
59 char *argv[],
60 int *dev_id,
61 int *clock_rate,
62 int *channel_count,
63 int *samples_per_frame,
64 int *bits_per_sample)
Benny Prijono6107a002006-03-17 18:01:27 +000065{
Benny Prijonobc797312006-03-24 20:44:27 +000066 struct pj_getopt_option long_options[] = {
67 { "dev", 1, 0, 'd' },
68 { "rate", 1, 0, 'r' },
69 { "channel", 1, 0, 'c' },
70 { "frame", 1, 0, 'f' },
71 { "bit", 1, 0, 'b' },
72 { NULL, 0, 0, 0 },
73 };
74 int c;
75 int option_index;
76 long val;
77 char *err;
Benny Prijono6107a002006-03-17 18:01:27 +000078
Benny Prijonobc797312006-03-24 20:44:27 +000079 *samples_per_frame = 0;
Benny Prijono6107a002006-03-17 18:01:27 +000080
Benny Prijonobc797312006-03-24 20:44:27 +000081 pj_optind = 0;
82 while((c=pj_getopt_long(argc,argv, "d:r:c:f:b:",
83 long_options, &option_index))!=-1)
Benny Prijono6107a002006-03-17 18:01:27 +000084 {
Benny Prijono6107a002006-03-17 18:01:27 +000085
Benny Prijonobc797312006-03-24 20:44:27 +000086 switch (c) {
87 case 'd':
88 /* device */
89 val = strtol(pj_optarg, &err, 10);
90 if (*err) {
91 PJ_LOG(3,(app_name, "Error: invalid value for device id"));
92 return PJ_EINVAL;
93 }
94 *dev_id = val;
95 break;
Benny Prijono6107a002006-03-17 18:01:27 +000096
Benny Prijonobc797312006-03-24 20:44:27 +000097 case 'r':
98 /* rate */
99 val = strtol(pj_optarg, &err, 10);
100 if (*err) {
101 PJ_LOG(3,(app_name, "Error: invalid value for clock rate"));
102 return PJ_EINVAL;
103 }
104 *clock_rate = val;
105 break;
Benny Prijono6107a002006-03-17 18:01:27 +0000106
Benny Prijonobc797312006-03-24 20:44:27 +0000107 case 'c':
108 /* channel count */
109 val = strtol(pj_optarg, &err, 10);
110 if (*err) {
111 PJ_LOG(3,(app_name, "Error: invalid channel count"));
112 return PJ_EINVAL;
113 }
114 *channel_count = val;
115 break;
Benny Prijono6107a002006-03-17 18:01:27 +0000116
Benny Prijonobc797312006-03-24 20:44:27 +0000117 case 'f':
118 /* frame count/samples per frame */
119 val = strtol(pj_optarg, &err, 10);
120 if (*err) {
121 PJ_LOG(3,(app_name, "Error: invalid samples per frame"));
122 return PJ_EINVAL;
123 }
124 *samples_per_frame = val;
125 break;
126
127 case 'b':
128 /* bit per sample */
129 val = strtol(pj_optarg, &err, 10);
130 if (*err) {
131 PJ_LOG(3,(app_name, "Error: invalid samples bits per sample"));
132 return PJ_EINVAL;
133 }
134 *bits_per_sample = val;
135 break;
136
137 default:
138 /* Unknown options */
139 PJ_LOG(3,(app_name, "Error: unknown options '%c'", pj_optopt));
140 return PJ_EINVAL;
Benny Prijono6107a002006-03-17 18:01:27 +0000141 }
Benny Prijonobc797312006-03-24 20:44:27 +0000142
Benny Prijono6107a002006-03-17 18:01:27 +0000143 }
144
Benny Prijonobc797312006-03-24 20:44:27 +0000145 if (*samples_per_frame == 0) {
146 *samples_per_frame = *clock_rate * *channel_count * 20 / 1000;
147 }
148
149 return 0;
Benny Prijono6107a002006-03-17 18:01:27 +0000150}
151
Benny Prijonobc797312006-03-24 20:44:27 +0000152
153/* Dump memory pool usage. */
Benny Prijonobf13fee2006-04-20 11:13:32 +0000154void dump_pool_usage( const char *app_name, pj_caching_pool *cp )
Benny Prijono6107a002006-03-17 18:01:27 +0000155{
Benny Prijono46ecff82006-03-30 16:46:36 +0000156#if !defined(PJ_HAS_POOL_ALT_API) || PJ_HAS_POOL_ALT_API==0
Benny Prijonobc797312006-03-24 20:44:27 +0000157 pj_pool_t *p;
158 unsigned total_alloc = 0;
159 unsigned total_used = 0;
Benny Prijono6107a002006-03-17 18:01:27 +0000160
Benny Prijonobc797312006-03-24 20:44:27 +0000161 /* Accumulate memory usage in active list. */
162 p = cp->used_list.next;
163 while (p != (pj_pool_t*) &cp->used_list) {
164 total_alloc += pj_pool_get_capacity(p);
165 total_used += pj_pool_get_used_size(p);
166 p = p->next;
Benny Prijono6107a002006-03-17 18:01:27 +0000167 }
168
Benny Prijonobc797312006-03-24 20:44:27 +0000169 PJ_LOG(3, (app_name, "Total pool memory allocated=%d KB, used=%d KB",
170 total_alloc / 1000,
171 total_used / 1000));
Benny Prijono46ecff82006-03-30 16:46:36 +0000172#endif
Benny Prijono6107a002006-03-17 18:01:27 +0000173}