sink, source: Really set the fixed latency in set_fixed_latency_within_thread(),...
[platform/upstream/pulseaudio.git] / src / tests / rtstutter.c
1 /***
2   This file is part of PulseAudio.
3
4   Copyright 2008 Lennart Poettering
5
6   PulseAudio is free software; you can redistribute it and/or modify
7   it under the terms of the GNU Lesser General Public License as
8   published by the Free Software Foundation; either version 2.1 of the
9   License, or (at your option) any later version.
10
11   PulseAudio is distributed in the hope that it will be useful, but
12   WITHOUT ANY WARRANTY; without even the implied warranty of
13   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14   General Public License for more details.
15
16   You should have received a copy of the GNU Lesser General Public
17   License along with PulseAudio; if not, write to the Free Software
18   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
19   USA.
20 ***/
21
22 #ifdef HAVE_CONFIG_H
23 #include <config.h>
24 #endif
25
26 #include <stdlib.h>
27 #include <unistd.h>
28 #include <time.h>
29 #include <inttypes.h>
30
31 #ifdef HAVE_PTHREAD
32 #include <pthread.h>
33 #endif
34
35 #include <pulse/util.h>
36 #include <pulse/timeval.h>
37 #include <pulse/gccmacro.h>
38
39 #include <pulsecore/log.h>
40 #include <pulsecore/macro.h>
41 #include <pulsecore/thread.h>
42 #include <pulsecore/core-util.h>
43 #include <pulsecore/core-rtclock.h>
44
45 static int msec_lower, msec_upper;
46
47 static void work(void *p) PA_GCC_NORETURN;
48
49 static void work(void *p) {
50
51     pa_log_notice("CPU%i: Created thread.", PA_PTR_TO_UINT(p));
52
53     pa_make_realtime(12);
54
55 #ifdef HAVE_PTHREAD_SETAFFINITY_NP
56 {
57     cpu_set_t mask;
58
59     CPU_ZERO(&mask);
60     CPU_SET((size_t) PA_PTR_TO_UINT(p), &mask);
61     pa_assert_se(pthread_setaffinity_np(pthread_self(), sizeof(mask), &mask) == 0);
62 }
63 #endif
64
65     for (;;) {
66         struct timeval now, end;
67         uint64_t usec;
68
69         pa_log_notice("CPU%i: Sleeping for 1s", PA_PTR_TO_UINT(p));
70         pa_msleep(1000);
71
72         usec =
73             (uint64_t) ((((double) rand())*(double)(msec_upper-msec_lower)*PA_USEC_PER_MSEC)/RAND_MAX) +
74             (uint64_t) ((uint64_t) msec_lower*PA_USEC_PER_MSEC);
75
76         pa_log_notice("CPU%i: Freezing for %ims", PA_PTR_TO_UINT(p), (int) (usec/PA_USEC_PER_MSEC));
77
78         pa_rtclock_get(&end);
79         pa_timeval_add(&end, usec);
80
81         do {
82             pa_rtclock_get(&now);
83         } while (pa_timeval_cmp(&now, &end) < 0);
84     }
85 }
86
87 int main(int argc, char*argv[]) {
88     unsigned n;
89
90     pa_log_set_level(PA_LOG_INFO);
91
92     srand((unsigned) time(NULL));
93
94     if (argc >= 3) {
95         msec_lower = atoi(argv[1]);
96         msec_upper = atoi(argv[2]);
97     } else if (argc >= 2) {
98         msec_lower = 0;
99         msec_upper = atoi(argv[1]);
100     } else {
101         msec_lower = 0;
102         msec_upper = 1000;
103     }
104
105     pa_assert(msec_upper > 0);
106     pa_assert(msec_upper >= msec_lower);
107
108     pa_log_notice("Creating random latencies in the range of %ims to %ims.", msec_lower, msec_upper);
109
110     for (n = 1; n < pa_ncpus(); n++) {
111         pa_assert_se(pa_thread_new("rtstutter", work, PA_UINT_TO_PTR(n)));
112     }
113
114     work(PA_INT_TO_PTR(0));
115
116     return 0;
117 }