~diwic/ubuntu/lucid/pulseaudio/bugfixes

« back to all changes in this revision

Viewing changes to src/tests/alsa-time-test.c

  • Committer: Bazaar Package Importer
  • Author(s): Luke Yelavich
  • Date: 2009-05-05 14:18:20 UTC
  • mfrom: (1.2.4 upstream) (1.1.8 squeeze)
  • Revision ID: james.westby@ubuntu.com-20090505141820-rrr2mtdd1jkllvr8
Tags: 1:0.9.15-1ubuntu1
* Merge from unreleased Debian pulseaudio git, remaining changes:
  - epoch (my stupid fault :S)
  - Don't build against, and create jack package. Jack is not in main
  - use linear resampler to work better with lack of PREEMPT in jaunty's
    -generic kernel config, also change buffer size
  - Add alsa configuration files to route alsa applications via pulseaudio
  - Move libasound2-plugins from Recommends to Depends
  - Add pm-utils sleep hook to suspend (and resume) users' pulseaudio
    daemons
  - patch to fix source/sink and suspend-on-idle race
  - Make initscript more informative in the default case of per-user
    sessions
  - create /var/run/pulse, and make restart more robust
  - add status check for system wide pulseaudio instance
  - LSB {Required-*,Should-*} should specify hal instead of dbus,
    since hal is required (and already requires dbus)
  - indicate that the system pulseaudio instance is being started from the init
    script
  - Install more upstream man pages
  - Link to pacat for parec man page
  - check whether pulseaudio is running before preloading the padsp library
  - Add DEB_OPT_FLAG = -O3 as per recommendation from
    pulseaudio-discuss/2007-December/001017.html
  - cache /usr/share/sounds/ubuntu/stereo/ wav files on pulseaudio load
  - disable glitch free (use tsched=0)
  - Generate a PO template on build
  - add special case to disable pulseaudio loading if accessibility/speech
    is being used
  - the sd wrapper script should not load pulseaudio if pulseaudio is being
    used as a system service
  - add a pulseaudio apport hook
  - fix some typos in README.Debian
  - demote paprefs to suggests
  - drop padevchooser(Recommends) and pavucontrol (Suggests)
  - drop libasyncns-dev build dependency, its in universe
* add libudev-dev as a build-dependency

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#include <assert.h>
 
2
#include <inttypes.h>
 
3
#include <time.h>
 
4
 
 
5
#include <alsa/asoundlib.h>
 
6
 
 
7
static uint64_t timespec_us(const struct timespec *ts) {
 
8
    return
 
9
        ts->tv_sec * 1000000LLU +
 
10
        ts->tv_nsec / 1000LLU;
 
11
}
 
12
 
 
13
int main(int argc, char *argv[]) {
 
14
    const char *dev;
 
15
    int r;
 
16
    snd_pcm_hw_params_t *hwparams;
 
17
    snd_pcm_sw_params_t *swparams;
 
18
    snd_pcm_status_t *status;
 
19
    snd_pcm_t *pcm;
 
20
    unsigned rate = 44100;
 
21
    unsigned periods = 2;
 
22
    snd_pcm_uframes_t boundary, buffer_size = 44100/10; /* 100s */
 
23
    int dir = 1;
 
24
    struct timespec start, last_timestamp = { 0, 0 };
 
25
    uint64_t start_us;
 
26
    snd_pcm_sframes_t last_avail = 0, last_delay = 0;
 
27
    struct pollfd *pollfds;
 
28
    int n_pollfd;
 
29
    int64_t sample_count = 0;
 
30
 
 
31
    snd_pcm_hw_params_alloca(&hwparams);
 
32
    snd_pcm_sw_params_alloca(&swparams);
 
33
    snd_pcm_status_alloca(&status);
 
34
 
 
35
    r = clock_gettime(CLOCK_MONOTONIC, &start);
 
36
    assert(r == 0);
 
37
 
 
38
    start_us = timespec_us(&start);
 
39
 
 
40
    dev = argc > 1 ? argv[1] : "front:AudioPCI";
 
41
 
 
42
    r = snd_pcm_open(&pcm, dev, SND_PCM_STREAM_PLAYBACK, 0);
 
43
    assert(r == 0);
 
44
 
 
45
    r = snd_pcm_hw_params_any(pcm, hwparams);
 
46
    assert(r == 0);
 
47
 
 
48
    r = snd_pcm_hw_params_set_rate_resample(pcm, hwparams, 0);
 
49
    assert(r == 0);
 
50
 
 
51
    r = snd_pcm_hw_params_set_access(pcm, hwparams, SND_PCM_ACCESS_RW_INTERLEAVED);
 
52
    assert(r == 0);
 
53
 
 
54
    r = snd_pcm_hw_params_set_format(pcm, hwparams, SND_PCM_FORMAT_S16_LE);
 
55
    assert(r == 0);
 
56
 
 
57
    r = snd_pcm_hw_params_set_rate_near(pcm, hwparams, &rate, NULL);
 
58
    assert(r == 0);
 
59
 
 
60
    r = snd_pcm_hw_params_set_channels(pcm, hwparams, 2);
 
61
    assert(r == 0);
 
62
 
 
63
    r = snd_pcm_hw_params_set_periods_integer(pcm, hwparams);
 
64
    assert(r == 0);
 
65
 
 
66
    r = snd_pcm_hw_params_set_periods_near(pcm, hwparams, &periods, &dir);
 
67
    assert(r == 0);
 
68
 
 
69
    r = snd_pcm_hw_params_set_buffer_size_near(pcm, hwparams, &buffer_size);
 
70
    assert(r == 0);
 
71
 
 
72
    r = snd_pcm_hw_params(pcm, hwparams);
 
73
    assert(r == 0);
 
74
 
 
75
    r = snd_pcm_hw_params_current(pcm, hwparams);
 
76
    assert(r == 0);
 
77
 
 
78
    r = snd_pcm_sw_params_current(pcm, swparams);
 
79
    assert(r == 0);
 
80
 
 
81
    r = snd_pcm_sw_params_set_avail_min(pcm, swparams, 1);
 
82
    assert(r == 0);
 
83
 
 
84
    r = snd_pcm_sw_params_set_period_event(pcm, swparams, 0);
 
85
    assert(r == 0);
 
86
 
 
87
    r = snd_pcm_hw_params_get_buffer_size(hwparams, &buffer_size);
 
88
    assert(r == 0);
 
89
    r = snd_pcm_sw_params_set_start_threshold(pcm, swparams, buffer_size);
 
90
    assert(r == 0);
 
91
 
 
92
    r = snd_pcm_sw_params_get_boundary(swparams, &boundary);
 
93
    assert(r == 0);
 
94
    r = snd_pcm_sw_params_set_stop_threshold(pcm, swparams, boundary);
 
95
    assert(r == 0);
 
96
 
 
97
    r = snd_pcm_sw_params_set_tstamp_mode(pcm, swparams, SND_PCM_TSTAMP_ENABLE);
 
98
    assert(r == 0);
 
99
 
 
100
    r = snd_pcm_sw_params(pcm, swparams);
 
101
    assert(r == 0);
 
102
 
 
103
    r = snd_pcm_prepare(pcm);
 
104
    assert(r == 0);
 
105
 
 
106
    r = snd_pcm_sw_params_current(pcm, swparams);
 
107
    assert(r == 0);
 
108
 
 
109
/*     assert(snd_pcm_hw_params_is_monotonic(hwparams) > 0); */
 
110
 
 
111
    n_pollfd = snd_pcm_poll_descriptors_count(pcm);
 
112
    assert(n_pollfd > 0);
 
113
 
 
114
    pollfds = malloc(sizeof(struct pollfd) * n_pollfd);
 
115
    assert(pollfds);
 
116
 
 
117
    r = snd_pcm_poll_descriptors(pcm, pollfds, n_pollfd);
 
118
    assert(r == n_pollfd);
 
119
 
 
120
    for (;;) {
 
121
        snd_pcm_sframes_t avail, delay;
 
122
        struct timespec now, timestamp;
 
123
        unsigned short revents;
 
124
        int written = 0;
 
125
        uint64_t now_us, timestamp_us;
 
126
        snd_pcm_state_t state;
 
127
 
 
128
        r = poll(pollfds, n_pollfd, 0);
 
129
        assert(r >= 0);
 
130
 
 
131
        r = snd_pcm_poll_descriptors_revents(pcm, pollfds, n_pollfd, &revents);
 
132
        assert(r == 0);
 
133
 
 
134
        assert((revents & ~POLLOUT) == 0);
 
135
 
 
136
        avail = snd_pcm_avail(pcm);
 
137
        assert(avail >= 0);
 
138
 
 
139
        r = snd_pcm_status(pcm, status);
 
140
        assert(r == 0);
 
141
 
 
142
        /* This assertion fails from time to time. ALSA seems to be broken */
 
143
/*         assert(avail == (snd_pcm_sframes_t) snd_pcm_status_get_avail(status)); */
 
144
/*         printf("%lu %lu\n", (unsigned long) avail, (unsigned long) snd_pcm_status_get_avail(status)); */
 
145
 
 
146
        snd_pcm_status_get_htstamp(status, &timestamp);
 
147
        delay = snd_pcm_status_get_delay(status);
 
148
        state = snd_pcm_status_get_state(status);
 
149
 
 
150
        r = clock_gettime(CLOCK_MONOTONIC, &now);
 
151
        assert(r == 0);
 
152
 
 
153
        assert(!revents || avail > 0);
 
154
 
 
155
        if (avail) {
 
156
            snd_pcm_sframes_t sframes;
 
157
            static const uint16_t samples[2] = { 0, 0 };
 
158
 
 
159
            sframes = snd_pcm_writei(pcm, samples, 1);
 
160
            assert(sframes == 1);
 
161
 
 
162
            written = 1;
 
163
            sample_count++;
 
164
        }
 
165
 
 
166
        if (!written &&
 
167
            memcmp(&timestamp, &last_timestamp, sizeof(timestamp)) == 0 &&
 
168
            avail == last_avail &&
 
169
            delay == last_delay) {
 
170
            /* This is boring */
 
171
            continue;
 
172
        }
 
173
 
 
174
        now_us = timespec_us(&now);
 
175
        timestamp_us = timespec_us(&timestamp);
 
176
 
 
177
        printf("%llu\t%llu\t%llu\t%li\t%li\t%i\t%i\t%i\n",
 
178
               (unsigned long long) (now_us - start_us),
 
179
               (unsigned long long) (timestamp_us ? timestamp_us - start_us : 0),
 
180
               (unsigned long long) ((sample_count - 1 - delay) * 1000000LU / 44100),
 
181
               (signed long) avail,
 
182
               (signed long) delay,
 
183
               revents,
 
184
               written,
 
185
               state);
 
186
 
 
187
        /** When this assert is hit, most likely something bad
 
188
         * happened, i.e. the avail jumped suddenly. */
 
189
        assert((unsigned) avail <= buffer_size);
 
190
 
 
191
        last_avail = avail;
 
192
        last_delay = delay;
 
193
        last_timestamp = timestamp;
 
194
    }
 
195
 
 
196
    return 0;
 
197
}