tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

test_device_changed_callback.cpp (3689B)


      1 /*
      2 * Copyright © 2018 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 /* libcubeb api/function test. Check behaviors of registering device changed
      9 * callbacks for the streams. */
     10 #include "gtest/gtest.h"
     11 #if !defined(_XOPEN_SOURCE)
     12 #define _XOPEN_SOURCE 600
     13 #endif
     14 #include "cubeb/cubeb.h"
     15 #include <memory>
     16 #include <stdio.h>
     17 
     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_FLOAT32LE
     24 #define INPUT_CHANNELS 1
     25 #define INPUT_LAYOUT CUBEB_LAYOUT_MONO
     26 #define OUTPUT_CHANNELS 2
     27 #define OUTPUT_LAYOUT CUBEB_LAYOUT_STEREO
     28 
     29 long
     30 data_callback(cubeb_stream * stream, void * user, const void * inputbuffer,
     31              void * outputbuffer, long nframes)
     32 {
     33  return 0;
     34 }
     35 
     36 void
     37 state_callback(cubeb_stream * stream, void * user, cubeb_state state)
     38 {
     39 }
     40 
     41 void
     42 device_changed_callback(void * user)
     43 {
     44  fprintf(stderr, "device changed callback\n");
     45  ASSERT_TRUE(false) << "Error: device changed callback"
     46                        " called without changing devices";
     47 }
     48 
     49 void
     50 test_registering_null_callback_twice(cubeb_stream * stream)
     51 {
     52  int r = cubeb_stream_register_device_changed_callback(stream, nullptr);
     53  if (r == CUBEB_ERROR_NOT_SUPPORTED) {
     54    return;
     55  }
     56  ASSERT_EQ(r, CUBEB_OK) << "Error registering null device changed callback";
     57 
     58  r = cubeb_stream_register_device_changed_callback(stream, nullptr);
     59  ASSERT_EQ(r, CUBEB_OK)
     60      << "Error registering null device changed callback again";
     61 }
     62 
     63 void
     64 test_registering_and_unregistering_callback(cubeb_stream * stream)
     65 {
     66  int r = cubeb_stream_register_device_changed_callback(
     67      stream, device_changed_callback);
     68  if (r == CUBEB_ERROR_NOT_SUPPORTED) {
     69    return;
     70  }
     71  ASSERT_EQ(r, CUBEB_OK) << "Error registering device changed callback";
     72 
     73  r = cubeb_stream_register_device_changed_callback(stream, nullptr);
     74  ASSERT_EQ(r, CUBEB_OK) << "Error unregistering device changed callback";
     75 }
     76 
     77 TEST(cubeb, device_changed_callbacks)
     78 {
     79  cubeb * ctx;
     80  cubeb_stream * stream;
     81  cubeb_stream_params input_params;
     82  cubeb_stream_params output_params;
     83  int r = CUBEB_OK;
     84  uint32_t latency_frames = 0;
     85 
     86  r = common_init(&ctx, "Cubeb duplex example with device change");
     87  ASSERT_EQ(r, CUBEB_OK) << "Error initializing cubeb library";
     88 
     89  if (!can_run_audio_input_test(ctx)) {
     90    return;
     91  }
     92 
     93  std::unique_ptr<cubeb, decltype(&cubeb_destroy)> cleanup_cubeb_at_exit(
     94      ctx, cubeb_destroy);
     95 
     96  /* typical user-case: mono input, stereo output, low latency. */
     97  input_params.format = STREAM_FORMAT;
     98  input_params.rate = SAMPLE_FREQUENCY;
     99  input_params.channels = INPUT_CHANNELS;
    100  input_params.layout = INPUT_LAYOUT;
    101  input_params.prefs = CUBEB_STREAM_PREF_NONE;
    102  output_params.format = STREAM_FORMAT;
    103  output_params.rate = SAMPLE_FREQUENCY;
    104  output_params.channels = OUTPUT_CHANNELS;
    105  output_params.layout = OUTPUT_LAYOUT;
    106  output_params.prefs = CUBEB_STREAM_PREF_NONE;
    107 
    108  r = cubeb_get_min_latency(ctx, &output_params, &latency_frames);
    109  ASSERT_EQ(r, CUBEB_OK) << "Could not get minimal latency";
    110 
    111  r = cubeb_stream_init(ctx, &stream, "Cubeb duplex", NULL, &input_params, NULL,
    112                        &output_params, latency_frames, data_callback,
    113                        state_callback, nullptr);
    114  ASSERT_EQ(r, CUBEB_OK) << "Error initializing cubeb stream";
    115 
    116  test_registering_null_callback_twice(stream);
    117 
    118  test_registering_and_unregistering_callback(stream);
    119 
    120  cubeb_stream_destroy(stream);
    121 }
    122 
    123 #undef SAMPLE_FREQUENCY
    124 #undef STREAM_FORMAT
    125 #undef INPUT_CHANNELS
    126 #undef INPUT_LAYOUT
    127 #undef OUTPUT_CHANNELS
    128 #undef OUTPUT_LAYOUT