1 /*
2  * Copyright © 2013 Sebastien Alaiwan
3  *
4  * This program is made available under an ISC-style license.  See the
5  * accompanying file LICENSE for details.
6  */
7 #if !defined(TEST_COMMON)
8 #define TEST_COMMON
9 
10 #if defined( _WIN32)
11 #ifndef WIN32_LEAN_AND_MEAN
12 #define WIN32_LEAN_AND_MEAN
13 #endif
14 #include <windows.h>
15 #else
16 #include <unistd.h>
17 #endif
18 
19 #include <cstdarg>
20 #include "cubeb/cubeb.h"
21 #include "cubeb_mixer.h"
22 
23 template<typename T, size_t N>
24 constexpr size_t
ARRAY_LENGTH(T (&)[N])25 ARRAY_LENGTH(T(&)[N])
26 {
27   return N;
28 }
29 
delay(unsigned int ms)30 void delay(unsigned int ms)
31 {
32 #if defined(_WIN32)
33   Sleep(ms);
34 #else
35   sleep(ms / 1000);
36   usleep(ms % 1000 * 1000);
37 #endif
38 }
39 
40 #if !defined(M_PI)
41 #define M_PI 3.14159265358979323846
42 #endif
43 
44 typedef struct {
45   char const * name;
46   unsigned int const channels;
47   cubeb_channel_layout const layout;
48 } layout_info;
49 
50 layout_info const layout_infos[CUBEB_LAYOUT_MAX] = {
51   { "undefined",      0,  CUBEB_LAYOUT_UNDEFINED },
52   { "dual mono",      2,  CUBEB_LAYOUT_DUAL_MONO },
53   { "dual mono lfe",  3,  CUBEB_LAYOUT_DUAL_MONO_LFE },
54   { "mono",           1,  CUBEB_LAYOUT_MONO },
55   { "mono lfe",       2,  CUBEB_LAYOUT_MONO_LFE },
56   { "stereo",         2,  CUBEB_LAYOUT_STEREO },
57   { "stereo lfe",     3,  CUBEB_LAYOUT_STEREO_LFE },
58   { "3f",             3,  CUBEB_LAYOUT_3F },
59   { "3f lfe",         4,  CUBEB_LAYOUT_3F_LFE },
60   { "2f1",            3,  CUBEB_LAYOUT_2F1 },
61   { "2f1 lfe",        4,  CUBEB_LAYOUT_2F1_LFE },
62   { "3f1",            4,  CUBEB_LAYOUT_3F1 },
63   { "3f1 lfe",        5,  CUBEB_LAYOUT_3F1_LFE },
64   { "2f2",            4,  CUBEB_LAYOUT_2F2 },
65   { "2f2 lfe",        5,  CUBEB_LAYOUT_2F2_LFE },
66   { "3f2",            5,  CUBEB_LAYOUT_3F2 },
67   { "3f2 lfe",        6,  CUBEB_LAYOUT_3F2_LFE },
68   { "3f3r lfe",       7,  CUBEB_LAYOUT_3F3R_LFE },
69   { "3f4 lfe",        8,  CUBEB_LAYOUT_3F4_LFE }
70 };
71 
has_available_input_device(cubeb * ctx)72 int has_available_input_device(cubeb * ctx)
73 {
74   cubeb_device_collection devices;
75   int input_device_available = 0;
76   int r;
77   /* Bail out early if the host does not have input devices. */
78   r = cubeb_enumerate_devices(ctx, CUBEB_DEVICE_TYPE_INPUT, &devices);
79   if (r != CUBEB_OK) {
80     fprintf(stderr, "error enumerating devices.");
81     return 0;
82   }
83 
84   if (devices.count == 0) {
85     fprintf(stderr, "no input device available, skipping test.\n");
86     cubeb_device_collection_destroy(ctx, &devices);
87     return 0;
88   }
89 
90   for (uint32_t i = 0; i < devices.count; i++) {
91     input_device_available |= (devices.device[i].state ==
92                                CUBEB_DEVICE_STATE_ENABLED);
93   }
94 
95   if (!input_device_available) {
96     fprintf(stderr, "there are input devices, but they are not "
97         "available, skipping\n");
98   }
99 
100   cubeb_device_collection_destroy(ctx, &devices);
101   return !!input_device_available;
102 }
103 
print_log(const char * msg,...)104 void print_log(const char * msg, ...)
105 {
106   va_list args;
107   va_start(args, msg);
108   vprintf(msg, args);
109   va_end(args);
110 }
111 
112 /** Initialize cubeb with backend override.
113  *  Create call cubeb_init passing value for CUBEB_BACKEND env var as
114  *  override. */
common_init(cubeb ** ctx,char const * ctx_name)115 int common_init(cubeb ** ctx, char const * ctx_name)
116 {
117   int r;
118   char const * backend;
119   char const * ctx_backend;
120 
121   backend = getenv("CUBEB_BACKEND");
122   r = cubeb_init(ctx, ctx_name, backend);
123   if (r == CUBEB_OK && backend) {
124     ctx_backend = cubeb_get_backend_id(*ctx);
125     if (strcmp(backend, ctx_backend) != 0) {
126       fprintf(stderr, "Requested backend `%s', got `%s'\n",
127               backend, ctx_backend);
128     }
129   }
130 
131   return r;
132 }
133 
134 #endif /* TEST_COMMON */
135