aboutsummaryrefslogtreecommitdiffhomepage
path: root/test
diff options
context:
space:
mode:
authorChun-Min Chang <[email protected]>2018-07-18 14:36:53 -0700
committerMatthew Gregan <[email protected]>2018-07-19 09:36:53 +1200
commit4b7442ee48b3d5267d2d01bf8513bb205f2309ca (patch)
treed56c5a0b16cf59b8a87714edb9daca9c93bdb66f /test
parent6c4704304288c45d8198173ac4a6fe27d82b1e7c (diff)
downloadcubeb-4b7442ee48b3d5267d2d01bf8513bb205f2309ca.tar.gz
cubeb-4b7442ee48b3d5267d2d01bf8513bb205f2309ca.zip
audiounit: Reduce duplicate code in audiounit_stream_get_current_device (#453)
* Add a test for cubeb_stream_get_current_device() and cubeb_stream_device_destroy() * Reduce duplicates in audiounit_stream_get_current_device() The audiounit_stream_get_current_device() will set the default device's name for input and output by the following steps: 1. Get default device's data, whose type is uint32 2. Convert the uint32 data into a string 3. Set the string into cubeb_device's name Hence we can split it into functions to do the above steps: i. audiounit_get_default_device_data(...) Do the step 1 ii. allocate_and_convert_uint32_into_string(...) Do the step 2 and 3. It will allocate memory for a string and put the converted string right there. (If we don't allocate and then set the values into the memory at the same time, we need to find a way to check the memory's boundary.) They are called by a function named audiounit_get_default_device_name(...), with cubeb_device_type parameter to set the device name of input or output. And audiounit_get_default_device_name(...) will be used in audiounit_stream_get_current_device(). * Replace allocate_and_convert_uint32_into_string(...) by convert_uint32_into_string(...) 1. Rename allocate_and_convert_uint32_into_string(...) to convert_uint32_into_string(...) 2. Return a unique_ptr<char[]> from convert_uint32_into_string(...) pointing to an allocated memory * Hard-coding the convertion from uint32 into string This change is to make sure the conversion only ever needs to handle 4 bytes. * Add more information to log when calling audiounit_stream_get_current_device(...) We add a log with error message when we cannot get datasource data from the devices. However, we don't return an error code in this case since it's quite common when we try getting that data from USB devices. We will convert the datasource data into a string. If there is no data, the string is empty. Instead of logging when we cannot get the datasource data, it's better to log that the converted name is empty. It'll give more meaning (Users are more likely confused about what the empty datasource means.). * Rename audiounit_get_default_device_data() to audiounit_get_default_device_datasource() We should use an explicit name for this function since the same device will return different datasources. For example, the default input device on macbook pro will return "imic" if it uses the default internal microphone or "emic" when it uses an external microphone plugged in audio jack. TODO: it's better to rename audiounit_stream_get_current_device to audiounit_stream_get_current_device_source. The reason is same as above.
Diffstat (limited to 'test')
-rw-r--r--test/test_devices.cpp45
1 files changed, 45 insertions, 0 deletions
diff --git a/test/test_devices.cpp b/test/test_devices.cpp
index 10c14c3..f9df89f 100644
--- a/test/test_devices.cpp
+++ b/test/test_devices.cpp
@@ -205,3 +205,48 @@ TEST(cubeb, enumerate_devices)
cubeb_stream_destroy(stream);
}
+
+TEST(cubeb, stream_get_current_device)
+{
+ cubeb * ctx = NULL;
+ int r = common_init(&ctx, "Cubeb audio test");
+ ASSERT_EQ(r, CUBEB_OK) << "Error initializing cubeb library";
+
+ std::unique_ptr<cubeb, decltype(&cubeb_destroy)>
+ cleanup_cubeb_at_exit(ctx, cubeb_destroy);
+
+ fprintf(stdout, "Getting current devices for backend %s\n",
+ cubeb_get_backend_id(ctx));
+
+ cubeb_stream * stream = NULL;
+ cubeb_stream_params input_params;
+ cubeb_stream_params output_params;
+
+ input_params.format = output_params.format = CUBEB_SAMPLE_FLOAT32NE;
+ input_params.rate = output_params.rate = 48000;
+ input_params.channels = output_params.channels = 1;
+ input_params.layout = output_params.layout = CUBEB_LAYOUT_MONO;
+ input_params.prefs = output_params.prefs = CUBEB_STREAM_PREF_NONE;
+
+ r = cubeb_stream_init(ctx, &stream, "Cubeb duplex",
+ NULL, &input_params, NULL, &output_params,
+ 1024, data_cb_duplex, state_cb_duplex, nullptr);
+ ASSERT_EQ(r, CUBEB_OK) << "Error initializing cubeb stream";
+ std::unique_ptr<cubeb_stream, decltype(&cubeb_stream_destroy)>
+ cleanup_stream_at_exit(stream, cubeb_stream_destroy);
+
+ cubeb_device * device;
+ r = cubeb_stream_get_current_device(stream, &device);
+ if (r == CUBEB_ERROR_NOT_SUPPORTED) {
+ fprintf(stderr, "Getting current device is not supported"
+ " for this backend, skipping this test.\n");
+ return;
+ }
+ ASSERT_EQ(r, CUBEB_OK) << "Error getting current devices";
+
+ fprintf(stdout, "Current output device: %s\n", device->output_name);
+ fprintf(stdout, "Current input device: %s\n", device->input_name);
+
+ r = cubeb_stream_device_destroy(stream, device);
+ ASSERT_EQ(r, CUBEB_OK) << "Error destroying current devices";
+} \ No newline at end of file