~noskcaj/ubuntu/saucy/sflphone/merge-1.2.3-2

« back to all changes in this revision

Viewing changes to daemon/libs/pjproject-2.0.1/pjsip-apps/src/samples/util.h

  • Committer: Jackson Doak
  • Date: 2013-07-10 21:04:46 UTC
  • mfrom: (20.1.3 sid)
  • Revision ID: noskcaj@ubuntu.com-20130710210446-y8f587vza807icr9
Properly merged from upstream.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* $Id: util.h 3550 2011-05-05 05:33:27Z nanang $ */
 
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 <stdlib.h>     /* strtol() */
 
21
 
 
22
/* Util to display the error message for the specified error code  */
 
23
static 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
 
 
30
    PJ_LOG(3,(sender, "%s: %s [code=%d]", title, errmsg, status));
 
31
    return 1;
 
32
}
 
33
 
 
34
 
 
35
 
 
36
/* Constants */
 
37
#define CLOCK_RATE      44100
 
38
#define NSAMPLES        (CLOCK_RATE * 20 / 1000)
 
39
#define NCHANNELS       1
 
40
#define NBITS           16
 
41
 
 
42
/*
 
43
 * Common sound options.
 
44
 */
 
45
#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
 */
 
57
pj_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)
 
65
{
 
66
    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;
 
78
 
 
79
    *samples_per_frame = 0;
 
80
 
 
81
    pj_optind = 0;
 
82
    while((c=pj_getopt_long(argc,argv, "d:r:c:f:b:",
 
83
                            long_options, &option_index))!=-1)
 
84
    {
 
85
 
 
86
        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;
 
96
 
 
97
        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;
 
106
 
 
107
        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;
 
116
 
 
117
        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;
 
141
        }
 
142
 
 
143
    }
 
144
 
 
145
    if (*samples_per_frame == 0) {
 
146
        *samples_per_frame = *clock_rate * *channel_count * 20 / 1000;
 
147
    }
 
148
 
 
149
    return 0;
 
150
}
 
151
 
 
152
 
 
153
/* Dump memory pool usage. */
 
154
void dump_pool_usage( const char *app_name, pj_caching_pool *cp )
 
155
{
 
156
#if !defined(PJ_HAS_POOL_ALT_API) || PJ_HAS_POOL_ALT_API==0
 
157
    pj_pool_t   *p;
 
158
    unsigned     total_alloc = 0;
 
159
    unsigned     total_used = 0;
 
160
 
 
161
    /* 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;
 
167
    }
 
168
 
 
169
    PJ_LOG(3, (app_name, "Total pool memory allocated=%d KB, used=%d KB",
 
170
               total_alloc / 1000,
 
171
               total_used / 1000));
 
172
#endif
 
173
}