2016-02-05 20:34:58 +03:00
|
|
|
/*
|
|
|
|
* Copyright © 2016 Mozilla Foundation
|
|
|
|
*
|
|
|
|
* This program is made available under an ISC-style license. See the
|
|
|
|
* accompanying file LICENSE for details.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* libcubeb api/function test. Record the mic and check there is sound. */
|
2016-11-11 06:09:49 +03:00
|
|
|
#include "gtest/gtest.h"
|
2016-11-11 04:50:16 +03:00
|
|
|
#if !defined(_XOPEN_SOURCE)
|
2016-03-26 20:24:25 +03:00
|
|
|
#define _XOPEN_SOURCE 600
|
2016-11-11 04:50:16 +03:00
|
|
|
#endif
|
2016-02-15 12:43:52 +03:00
|
|
|
#include "cubeb/cubeb.h"
|
2017-06-02 17:40:29 +03:00
|
|
|
#include <atomic>
|
2023-11-10 16:14:22 +03:00
|
|
|
#include <math.h>
|
|
|
|
#include <memory>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
2016-02-05 20:34:58 +03:00
|
|
|
|
2023-11-10 16:14:22 +03:00
|
|
|
// #define ENABLE_NORMAL_LOG
|
|
|
|
// #define ENABLE_VERBOSE_LOG
|
2018-10-18 17:57:01 +03:00
|
|
|
#include "common.h"
|
|
|
|
|
2016-02-05 20:34:58 +03:00
|
|
|
#define SAMPLE_FREQUENCY 48000
|
|
|
|
#define STREAM_FORMAT CUBEB_SAMPLE_FLOAT32LE
|
|
|
|
|
2023-11-10 16:14:22 +03:00
|
|
|
struct user_state_record {
|
|
|
|
std::atomic<int> invalid_audio_value{0};
|
2016-02-05 20:34:58 +03:00
|
|
|
};
|
|
|
|
|
2023-11-10 16:14:22 +03:00
|
|
|
long
|
|
|
|
data_cb_record(cubeb_stream * stream, void * user, const void * inputbuffer,
|
|
|
|
void * outputbuffer, long nframes)
|
2016-02-05 20:34:58 +03:00
|
|
|
{
|
2023-11-10 16:14:22 +03:00
|
|
|
user_state_record * u = reinterpret_cast<user_state_record *>(user);
|
|
|
|
float * b = (float *)inputbuffer;
|
2016-02-05 20:34:58 +03:00
|
|
|
|
2023-11-10 16:14:22 +03:00
|
|
|
if (stream == NULL || inputbuffer == NULL || outputbuffer != NULL) {
|
2016-02-05 20:34:58 +03:00
|
|
|
return CUBEB_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (long i = 0; i < nframes; i++) {
|
2018-10-18 17:57:01 +03:00
|
|
|
if (b[i] <= -1.0 || b[i] >= 1.0) {
|
|
|
|
u->invalid_audio_value = 1;
|
2017-02-03 15:43:44 +03:00
|
|
|
break;
|
2016-02-05 20:34:58 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nframes;
|
|
|
|
}
|
|
|
|
|
2023-11-10 16:14:22 +03:00
|
|
|
void
|
|
|
|
state_cb_record(cubeb_stream * stream, void * /*user*/, cubeb_state state)
|
2016-02-05 20:34:58 +03:00
|
|
|
{
|
|
|
|
if (stream == NULL)
|
|
|
|
return;
|
|
|
|
|
|
|
|
switch (state) {
|
|
|
|
case CUBEB_STATE_STARTED:
|
2023-11-10 16:14:22 +03:00
|
|
|
fprintf(stderr, "stream started\n");
|
|
|
|
break;
|
2016-02-05 20:34:58 +03:00
|
|
|
case CUBEB_STATE_STOPPED:
|
2023-11-10 16:14:22 +03:00
|
|
|
fprintf(stderr, "stream stopped\n");
|
|
|
|
break;
|
2016-02-05 20:34:58 +03:00
|
|
|
case CUBEB_STATE_DRAINED:
|
2023-11-10 16:14:22 +03:00
|
|
|
fprintf(stderr, "stream drained\n");
|
|
|
|
break;
|
2016-02-05 20:34:58 +03:00
|
|
|
default:
|
2017-04-07 06:44:24 +03:00
|
|
|
fprintf(stderr, "unknown stream state %d\n", state);
|
2016-02-05 20:34:58 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-11-11 05:00:23 +03:00
|
|
|
TEST(cubeb, record)
|
2016-02-05 20:34:58 +03:00
|
|
|
{
|
2023-11-10 16:14:22 +03:00
|
|
|
if (cubeb_set_log_callback(CUBEB_LOG_DISABLED, nullptr /*print_log*/) !=
|
|
|
|
CUBEB_OK) {
|
2017-04-07 06:44:24 +03:00
|
|
|
fprintf(stderr, "Set log callback failed\n");
|
2017-02-03 19:38:45 +03:00
|
|
|
}
|
2023-11-10 16:14:22 +03:00
|
|
|
cubeb * ctx;
|
|
|
|
cubeb_stream * stream;
|
2016-02-05 20:34:58 +03:00
|
|
|
cubeb_stream_params params;
|
|
|
|
int r;
|
2017-06-02 17:40:29 +03:00
|
|
|
user_state_record stream_state;
|
2016-02-05 20:34:58 +03:00
|
|
|
|
2017-05-23 05:35:29 +03:00
|
|
|
r = common_init(&ctx, "Cubeb record example");
|
2017-04-07 06:44:24 +03:00
|
|
|
ASSERT_EQ(r, CUBEB_OK) << "Error initializing cubeb library";
|
2016-02-05 20:34:58 +03:00
|
|
|
|
2023-11-10 16:14:22 +03:00
|
|
|
std::unique_ptr<cubeb, decltype(&cubeb_destroy)> cleanup_cubeb_at_exit(
|
|
|
|
ctx, cubeb_destroy);
|
2017-03-29 07:06:26 +03:00
|
|
|
|
2016-02-15 12:43:52 +03:00
|
|
|
/* This test needs an available input device, skip it if this host does not
|
|
|
|
* have one. */
|
2023-04-25 15:42:06 +03:00
|
|
|
if (!can_run_audio_input_test(ctx)) {
|
2016-11-10 05:48:17 +03:00
|
|
|
return;
|
2016-02-05 20:34:58 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
params.format = STREAM_FORMAT;
|
|
|
|
params.rate = SAMPLE_FREQUENCY;
|
|
|
|
params.channels = 1;
|
2017-04-25 16:26:11 +03:00
|
|
|
params.layout = CUBEB_LAYOUT_UNDEFINED;
|
2018-01-22 19:13:00 +03:00
|
|
|
params.prefs = CUBEB_STREAM_PREF_NONE;
|
2016-02-05 20:34:58 +03:00
|
|
|
|
2023-11-10 16:14:22 +03:00
|
|
|
r = cubeb_stream_init(ctx, &stream, "Cubeb record (mono)", NULL, ¶ms,
|
|
|
|
NULL, nullptr, 4096, data_cb_record, state_cb_record,
|
|
|
|
&stream_state);
|
2017-04-07 06:44:24 +03:00
|
|
|
ASSERT_EQ(r, CUBEB_OK) << "Error initializing cubeb stream";
|
2016-02-05 20:34:58 +03:00
|
|
|
|
2017-04-06 10:52:57 +03:00
|
|
|
std::unique_ptr<cubeb_stream, decltype(&cubeb_stream_destroy)>
|
2023-11-10 16:14:22 +03:00
|
|
|
cleanup_stream_at_exit(stream, cubeb_stream_destroy);
|
2017-03-29 07:06:26 +03:00
|
|
|
|
2016-02-05 20:34:58 +03:00
|
|
|
cubeb_stream_start(stream);
|
|
|
|
delay(500);
|
|
|
|
cubeb_stream_stop(stream);
|
|
|
|
|
2017-02-03 15:43:44 +03:00
|
|
|
#ifdef __linux__
|
|
|
|
// user callback does not arrive in Linux, silence the error
|
2017-04-07 06:44:24 +03:00
|
|
|
fprintf(stderr, "Check is disabled in Linux\n");
|
2017-02-03 15:43:44 +03:00
|
|
|
#else
|
2018-10-18 17:57:01 +03:00
|
|
|
ASSERT_FALSE(stream_state.invalid_audio_value.load());
|
2017-02-03 15:43:44 +03:00
|
|
|
#endif
|
2016-02-05 20:34:58 +03:00
|
|
|
}
|
2022-12-02 17:32:53 +03:00
|
|
|
|
|
|
|
#undef SAMPLE_FREQUENCY
|
|
|
|
#undef STREAM_FORMAT
|