cubeb/test/test_record.cpp

127 строки
3.1 KiB
C++
Исходник Постоянная ссылка Обычный вид История

/*
* 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. */
#include "gtest/gtest.h"
#if !defined(_XOPEN_SOURCE)
2016-03-26 20:24:25 +03:00
#define _XOPEN_SOURCE 600
#endif
Address review comments. Build system. Review comments: cubeb_resampler_speex_one_way ctor should take uint32_t. Review comments: Use the default device for test_record.cpp Review comments: use the correct path to include cubeb.h. Review comments: use std::unique_ptr instead of auto_ptr, and remove auto_ptr implementation. Review comments: Add test_duplex{,.exe} to .gitignore. Review comments: Formatting in noop_resampler::fill. Review comments: rename auto_array::resize to auto_array::reserve. Review comments: Rename the method that push silence in an auto_array push_silence. Review comments: Make test_duplex work with backend that use integers. Review comments: indent in cubeb_resampler.cpp. Review comments: call the target rate in the public interface of the resampler `target_rate`. Review comments: Clarify the comment on cubeb_speex_resampler_one_way::drain. Review comments: trailing space in cubeb_resampler_one_way::output assert. Review comments: space between T and *. Review comments: s/outut/output/. Review comments: return before {. Review comments: Use unique_ptr to create resampler objects. We can't really use std::move here, as we appear to target platforms that don't have it (Gecko on Android uses stlport that does not have std::move). Review comments: check that the delay lign creation succeeded. Review comments: Remove frame_count_at_rate. Review comments: assert speex resampling suceeds. Review comments: s/l atency/latency/. Review comments: Remove comment. Review comments: skip the tests that require an available audio input if none is available.
2016-02-15 12:43:52 +03:00
#include "cubeb/cubeb.h"
2017-06-02 17:40:29 +03:00
#include <atomic>
#include <math.h>
#include <memory>
#include <stdio.h>
#include <stdlib.h>
// #define ENABLE_NORMAL_LOG
// #define ENABLE_VERBOSE_LOG
#include "common.h"
#define SAMPLE_FREQUENCY 48000
#define STREAM_FORMAT CUBEB_SAMPLE_FLOAT32LE
struct user_state_record {
std::atomic<int> invalid_audio_value{0};
};
long
data_cb_record(cubeb_stream * stream, void * user, const void * inputbuffer,
void * outputbuffer, long nframes)
{
user_state_record * u = reinterpret_cast<user_state_record *>(user);
float * b = (float *)inputbuffer;
if (stream == NULL || inputbuffer == NULL || outputbuffer != NULL) {
return CUBEB_ERROR;
}
for (long i = 0; i < nframes; i++) {
if (b[i] <= -1.0 || b[i] >= 1.0) {
u->invalid_audio_value = 1;
break;
}
}
return nframes;
}
void
state_cb_record(cubeb_stream * stream, void * /*user*/, cubeb_state state)
{
if (stream == NULL)
return;
switch (state) {
case CUBEB_STATE_STARTED:
fprintf(stderr, "stream started\n");
break;
case CUBEB_STATE_STOPPED:
fprintf(stderr, "stream stopped\n");
break;
case CUBEB_STATE_DRAINED:
fprintf(stderr, "stream drained\n");
break;
default:
fprintf(stderr, "unknown stream state %d\n", state);
}
return;
}
TEST(cubeb, record)
{
if (cubeb_set_log_callback(CUBEB_LOG_DISABLED, nullptr /*print_log*/) !=
CUBEB_OK) {
fprintf(stderr, "Set log callback failed\n");
}
cubeb * ctx;
cubeb_stream * stream;
cubeb_stream_params params;
int r;
2017-06-02 17:40:29 +03:00
user_state_record stream_state;
r = common_init(&ctx, "Cubeb record example");
ASSERT_EQ(r, CUBEB_OK) << "Error initializing cubeb library";
std::unique_ptr<cubeb, decltype(&cubeb_destroy)> cleanup_cubeb_at_exit(
ctx, cubeb_destroy);
Address review comments. Build system. Review comments: cubeb_resampler_speex_one_way ctor should take uint32_t. Review comments: Use the default device for test_record.cpp Review comments: use the correct path to include cubeb.h. Review comments: use std::unique_ptr instead of auto_ptr, and remove auto_ptr implementation. Review comments: Add test_duplex{,.exe} to .gitignore. Review comments: Formatting in noop_resampler::fill. Review comments: rename auto_array::resize to auto_array::reserve. Review comments: Rename the method that push silence in an auto_array push_silence. Review comments: Make test_duplex work with backend that use integers. Review comments: indent in cubeb_resampler.cpp. Review comments: call the target rate in the public interface of the resampler `target_rate`. Review comments: Clarify the comment on cubeb_speex_resampler_one_way::drain. Review comments: trailing space in cubeb_resampler_one_way::output assert. Review comments: space between T and *. Review comments: s/outut/output/. Review comments: return before {. Review comments: Use unique_ptr to create resampler objects. We can't really use std::move here, as we appear to target platforms that don't have it (Gecko on Android uses stlport that does not have std::move). Review comments: check that the delay lign creation succeeded. Review comments: Remove frame_count_at_rate. Review comments: assert speex resampling suceeds. Review comments: s/l atency/latency/. Review comments: Remove comment. Review comments: skip the tests that require an available audio input if none is available.
2016-02-15 12:43:52 +03:00
/* This test needs an available input device, skip it if this host does not
* have one. */
if (!can_run_audio_input_test(ctx)) {
2016-11-10 05:48:17 +03:00
return;
}
params.format = STREAM_FORMAT;
params.rate = SAMPLE_FREQUENCY;
params.channels = 1;
params.layout = CUBEB_LAYOUT_UNDEFINED;
params.prefs = CUBEB_STREAM_PREF_NONE;
r = cubeb_stream_init(ctx, &stream, "Cubeb record (mono)", NULL, &params,
NULL, nullptr, 4096, data_cb_record, state_cb_record,
&stream_state);
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_stream_start(stream);
delay(500);
cubeb_stream_stop(stream);
#ifdef __linux__
// user callback does not arrive in Linux, silence the error
fprintf(stderr, "Check is disabled in Linux\n");
#else
ASSERT_FALSE(stream_state.invalid_audio_value.load());
#endif
}
#undef SAMPLE_FREQUENCY
#undef STREAM_FORMAT