summaryrefslogtreecommitdiffstats
path: root/media/libcubeb/test/test_device_changed_callback.cpp
blob: 890ebd9e411bdf0d33970ce1c146a4e8b8f02813 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
/*
 * Copyright © 2018 Mozilla Foundation
 *
 * This program is made available under an ISC-style license.  See the
 * accompanying file LICENSE for details.
 */

/* libcubeb api/function test. Check behaviors of registering device changed
 * callbacks for the streams. */
#include "gtest/gtest.h"
#if !defined(_XOPEN_SOURCE)
#define _XOPEN_SOURCE 600
#endif
#include <stdio.h>
#include <memory>
#include "cubeb/cubeb.h"

//#define ENABLE_NORMAL_LOG
//#define ENABLE_VERBOSE_LOG
#include "common.h"

#define SAMPLE_FREQUENCY 48000
#define STREAM_FORMAT CUBEB_SAMPLE_FLOAT32LE
#define INPUT_CHANNELS 1
#define INPUT_LAYOUT CUBEB_LAYOUT_MONO
#define OUTPUT_CHANNELS 2
#define OUTPUT_LAYOUT CUBEB_LAYOUT_STEREO

long data_callback(cubeb_stream * stream, void * user, const void * inputbuffer, void * outputbuffer, long nframes)
{
  return 0;
}

void state_callback(cubeb_stream * stream, void * user, cubeb_state state)
{
}

void device_changed_callback(void * user)
{
  fprintf(stderr, "device changed callback\n");
  ASSERT_TRUE(false) << "Error: device changed callback"
                        " called without changing devices";
}

void test_registering_null_callback_twice(cubeb_stream * stream)
{
  int r = cubeb_stream_register_device_changed_callback(stream, nullptr);
  if (r == CUBEB_ERROR_NOT_SUPPORTED) {
    return;
  }
  ASSERT_EQ(r, CUBEB_OK) << "Error registering null device changed callback";

  r = cubeb_stream_register_device_changed_callback(stream, nullptr);
  ASSERT_EQ(r, CUBEB_OK) << "Error registering null device changed callback again";
}

void test_registering_and_unregistering_callback(cubeb_stream * stream)
{
  int r = cubeb_stream_register_device_changed_callback(stream, device_changed_callback);
  if (r == CUBEB_ERROR_NOT_SUPPORTED) {
    return;
  }
  ASSERT_EQ(r, CUBEB_OK) << "Error registering device changed callback";

  r = cubeb_stream_register_device_changed_callback(stream, nullptr);
  ASSERT_EQ(r, CUBEB_OK) << "Error unregistering device changed callback";
}

TEST(cubeb, device_changed_callbacks)
{
  cubeb * ctx;
  cubeb_stream * stream;
  cubeb_stream_params input_params;
  cubeb_stream_params output_params;
  int r = CUBEB_OK;
  uint32_t latency_frames = 0;


  r = common_init(&ctx, "Cubeb duplex example with device change");
  ASSERT_EQ(r, CUBEB_OK) << "Error initializing cubeb library";

  if (!can_run_audio_input_test(ctx)) {
    return;
  }

  std::unique_ptr<cubeb, decltype(&cubeb_destroy)>
    cleanup_cubeb_at_exit(ctx, cubeb_destroy);

  /* typical user-case: mono input, stereo output, low latency. */
  input_params.format = STREAM_FORMAT;
  input_params.rate = SAMPLE_FREQUENCY;
  input_params.channels = INPUT_CHANNELS;
  input_params.layout = INPUT_LAYOUT;
  input_params.prefs = CUBEB_STREAM_PREF_NONE;
  output_params.format = STREAM_FORMAT;
  output_params.rate = SAMPLE_FREQUENCY;
  output_params.channels = OUTPUT_CHANNELS;
  output_params.layout = OUTPUT_LAYOUT;
  output_params.prefs = CUBEB_STREAM_PREF_NONE;

  r = cubeb_get_min_latency(ctx, &output_params, &latency_frames);
  ASSERT_EQ(r, CUBEB_OK) << "Could not get minimal latency";

  r = cubeb_stream_init(ctx, &stream, "Cubeb duplex",
                        NULL, &input_params, NULL, &output_params,
                        latency_frames, data_callback, state_callback, nullptr);
  ASSERT_EQ(r, CUBEB_OK) << "Error initializing cubeb stream";

  test_registering_null_callback_twice(stream);

  test_registering_and_unregistering_callback(stream);

  cubeb_stream_destroy(stream);
}

#undef SAMPLE_FREQUENCY
#undef STREAM_FORMAT
#undef INPUT_CHANNELS
#undef INPUT_LAYOUT
#undef OUTPUT_CHANNELS
#undef OUTPUT_LAYOUT