| 1 |
/* |
| 2 |
* Copyright © 2017 Mozilla Foundation |
| 3 |
* |
| 4 |
* This program is made available under an ISC-style license. See the |
| 5 |
* accompanying file LICENSE for details. |
| 6 |
*/ |
| 7 |
|
| 8 |
#include "gtest/gtest.h" |
| 9 |
#if !defined(_XOPEN_SOURCE) |
| 10 |
#define _XOPEN_SOURCE 600 |
| 11 |
#endif |
| 12 |
#include "cubeb/cubeb.h" |
| 13 |
#include <atomic> |
| 14 |
#include <math.h> |
| 15 |
#include <memory> |
| 16 |
#include <stdio.h> |
| 17 |
#include <stdlib.h> |
| 18 |
// #define ENABLE_NORMAL_LOG |
| 19 |
// #define ENABLE_VERBOSE_LOG |
| 20 |
#include "common.h" |
| 21 |
|
| 22 |
#define SAMPLE_FREQUENCY 48000 |
| 23 |
#define STREAM_FORMAT CUBEB_SAMPLE_S16LE |
| 24 |
|
| 25 |
std::atomic<bool> load_callback{false}; |
| 26 |
|
| 27 |
static long |
| 28 |
data_cb(cubeb_stream * stream, void * user, const void * inputbuffer, |
| 29 |
void * outputbuffer, long nframes) |
| 30 |
{ |
| 31 |
if (load_callback) { |
| 32 |
fprintf(stderr, "Sleeping...\n"); |
| 33 |
delay(100000); |
| 34 |
fprintf(stderr, "Sleeping done\n"); |
| 35 |
} |
| 36 |
return nframes; |
| 37 |
} |
| 38 |
|
| 39 |
static void |
| 40 |
state_cb(cubeb_stream * stream, void * /*user*/, cubeb_state state) |
| 41 |
{ |
| 42 |
ASSERT_TRUE(!!stream); |
| 43 |
|
| 44 |
switch (state) { |
| 45 |
case CUBEB_STATE_STARTED: |
| 46 |
fprintf(stderr, "stream started\n"); |
| 47 |
break; |
| 48 |
case CUBEB_STATE_STOPPED: |
| 49 |
fprintf(stderr, "stream stopped\n"); |
| 50 |
break; |
| 51 |
case CUBEB_STATE_DRAINED: |
| 52 |
FAIL() << "this test is not supposed to drain"; |
| 53 |
break; |
| 54 |
case CUBEB_STATE_ERROR: |
| 55 |
fprintf(stderr, "stream error\n"); |
| 56 |
break; |
| 57 |
default: |
| 58 |
FAIL() << "this test is not supposed to have a weird state"; |
| 59 |
break; |
| 60 |
} |
| 61 |
} |
| 62 |
|
| 63 |
TEST(cubeb, overload_callback) |
| 64 |
{ |
| 65 |
cubeb * ctx; |
| 66 |
cubeb_stream * stream; |
| 67 |
cubeb_stream_params output_params; |
| 68 |
int r; |
| 69 |
uint32_t latency_frames = 0; |
| 70 |
|
| 71 |
r = common_init(&ctx, "Cubeb callback overload"); |
| 72 |
ASSERT_EQ(r, CUBEB_OK); |
| 73 |
|
| 74 |
std::unique_ptr<cubeb, decltype(&cubeb_destroy)> cleanup_cubeb_at_exit( |
| 75 |
ctx, cubeb_destroy); |
| 76 |
|
| 77 |
// This test is specifically designed to test a behaviour of the WASAPI |
| 78 |
// backend in a specific scenario. |
| 79 |
if (strcmp(cubeb_get_backend_id(ctx), "wasapi") != 0) { |
| 80 |
return; |
| 81 |
} |
| 82 |
|
| 83 |
output_params.format = STREAM_FORMAT; |
| 84 |
output_params.rate = 48000; |
| 85 |
output_params.channels = 2; |
| 86 |
output_params.layout = CUBEB_LAYOUT_STEREO; |
| 87 |
output_params.prefs = CUBEB_STREAM_PREF_NONE; |
| 88 |
|
| 89 |
r = cubeb_get_min_latency(ctx, &output_params, &latency_frames); |
| 90 |
ASSERT_EQ(r, CUBEB_OK); |
| 91 |
|
| 92 |
r = cubeb_stream_init(ctx, &stream, "Cubeb", NULL, NULL, NULL, &output_params, |
| 93 |
latency_frames, data_cb, state_cb, NULL); |
| 94 |
ASSERT_EQ(r, CUBEB_OK); |
| 95 |
|
| 96 |
std::unique_ptr<cubeb_stream, decltype(&cubeb_stream_destroy)> |
| 97 |
cleanup_stream_at_exit(stream, cubeb_stream_destroy); |
| 98 |
|
| 99 |
cubeb_stream_start(stream); |
| 100 |
delay(500); |
| 101 |
// This causes the callback to sleep for a large number of seconds. |
| 102 |
load_callback = true; |
| 103 |
delay(500); |
| 104 |
cubeb_stream_stop(stream); |
| 105 |
} |
| 106 |
|
| 107 |
#undef SAMPLE_FREQUENCY |
| 108 |
#undef STREAM_FORMAT |