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 <objbase.h>
15 #include <windows.h>
16 #else
17 #include <unistd.h>
18 #endif
19 
20 #include <cstdarg>
21 #include "cubeb/cubeb.h"
22 #include "cubeb_mixer.h"
23 
24 template<typename T, size_t N>
25 constexpr size_t
ARRAY_LENGTH(T (&)[N])26 ARRAY_LENGTH(T(&)[N])
27 {
28   return N;
29 }
30 
delay(unsigned int ms)31 void delay(unsigned int ms)
32 {
33 #if defined(_WIN32)
34   Sleep(ms);
35 #else
36   sleep(ms / 1000);
37   usleep(ms % 1000 * 1000);
38 #endif
39 }
40 
41 #if !defined(M_PI)
42 #define M_PI 3.14159265358979323846
43 #endif
44 
45 typedef struct {
46   char const * name;
47   unsigned int const channels;
48   uint32_t const layout;
49 } layout_info;
50 
has_available_input_device(cubeb * ctx)51 int has_available_input_device(cubeb * ctx)
52 {
53   cubeb_device_collection devices;
54   int input_device_available = 0;
55   int r;
56   /* Bail out early if the host does not have input devices. */
57   r = cubeb_enumerate_devices(ctx, CUBEB_DEVICE_TYPE_INPUT, &devices);
58   if (r != CUBEB_OK) {
59     fprintf(stderr, "error enumerating devices.");
60     return 0;
61   }
62 
63   if (devices.count == 0) {
64     fprintf(stderr, "no input device available, skipping test.\n");
65     cubeb_device_collection_destroy(ctx, &devices);
66     return 0;
67   }
68 
69   for (uint32_t i = 0; i < devices.count; i++) {
70     input_device_available |= (devices.device[i].state ==
71                                CUBEB_DEVICE_STATE_ENABLED);
72   }
73 
74   if (!input_device_available) {
75     fprintf(stderr, "there are input devices, but they are not "
76         "available, skipping\n");
77   }
78 
79   cubeb_device_collection_destroy(ctx, &devices);
80   return !!input_device_available;
81 }
82 
print_log(const char * msg,...)83 void print_log(const char * msg, ...)
84 {
85   va_list args;
86   va_start(args, msg);
87   vprintf(msg, args);
88   va_end(args);
89 }
90 
91 /** Initialize cubeb with backend override.
92  *  Create call cubeb_init passing value for CUBEB_BACKEND env var as
93  *  override. */
common_init(cubeb ** ctx,char const * ctx_name)94 int common_init(cubeb ** ctx, char const * ctx_name)
95 {
96 #ifdef ENABLE_NORMAL_LOG
97   if (cubeb_set_log_callback(CUBEB_LOG_NORMAL, print_log) != CUBEB_OK) {
98     fprintf(stderr, "Set normal log callback failed\n");
99   }
100 #endif
101 
102 #ifdef ENABLE_VERBOSE_LOG
103   if (cubeb_set_log_callback(CUBEB_LOG_VERBOSE, print_log) != CUBEB_OK) {
104     fprintf(stderr, "Set verbose log callback failed\n");
105   }
106 #endif
107 
108   int r;
109   char const * backend;
110   char const * ctx_backend;
111 
112   backend = getenv("CUBEB_BACKEND");
113   r = cubeb_init(ctx, ctx_name, backend);
114   if (r == CUBEB_OK && backend) {
115     ctx_backend = cubeb_get_backend_id(*ctx);
116     if (strcmp(backend, ctx_backend) != 0) {
117       fprintf(stderr, "Requested backend `%s', got `%s'\n",
118               backend, ctx_backend);
119     }
120   }
121 
122   return r;
123 }
124 
125 #if defined( _WIN32)
126 class TestEnvironment : public ::testing::Environment {
127 public:
SetUp()128   void SetUp() override {
129     hr = CoInitializeEx(nullptr, COINIT_MULTITHREADED);
130   }
131 
TearDown()132   void TearDown() override {
133     if (SUCCEEDED(hr)) {
134       CoUninitialize();
135     }
136   }
137 
138 private:
139   HRESULT hr;
140 };
141 
142 ::testing::Environment* const foo_env = ::testing::AddGlobalTestEnvironment(new TestEnvironment);
143 #endif
144 
145 #endif /* TEST_COMMON */
146