diff options
author | Bryce Van Dyk <[email protected]> | 2017-12-04 11:09:21 -0500 |
---|---|---|
committer | Matthew Gregan <[email protected]> | 2018-01-23 08:50:28 +1300 |
commit | 701178cf160e05a835082170e978b298f60d8a58 (patch) | |
tree | 8148df62624b8b112077730927192f701454fc6d /test/test_loopback.cpp | |
parent | 2bb275d31fd6da52205b3cb25f41a83d119c0124 (diff) | |
download | cubeb-701178cf160e05a835082170e978b298f60d8a58.tar.gz cubeb-701178cf160e05a835082170e978b298f60d8a58.zip |
Add test for loopback when playback is silent
Diffstat (limited to 'test/test_loopback.cpp')
-rw-r--r-- | test/test_loopback.cpp | 60 |
1 files changed, 60 insertions, 0 deletions
diff --git a/test/test_loopback.cpp b/test/test_loopback.cpp index b6b498d..95c01fb 100644 --- a/test/test_loopback.cpp +++ b/test/test_loopback.cpp @@ -398,3 +398,63 @@ TEST(cubeb, loopback_separate_streams) run_loopback_separate_streams_test(true); run_loopback_separate_streams_test(false); } + +void run_loopback_silence_test(bool is_float) +{ + cubeb *ctx; + cubeb_stream *input_stream; + cubeb_stream_params input_params; + int r; + uint32_t latency_frames = 0; + + r = common_init(&ctx, "Cubeb loopback example: record silence"); + ASSERT_EQ(r, CUBEB_OK) << "Error initializing cubeb library"; + + std::unique_ptr<cubeb, decltype(&cubeb_destroy)> + cleanup_cubeb_at_exit(ctx, cubeb_destroy); + + input_params.format = is_float ? CUBEB_SAMPLE_FLOAT32NE : CUBEB_SAMPLE_S16LE; + input_params.rate = SAMPLE_FREQUENCY; + input_params.channels = 1; + input_params.layout = CUBEB_LAYOUT_MONO; + input_params.prefs = CUBEB_STREAM_PREF_LOOPBACK; + + std::unique_ptr<user_state_loopback> user_data(new user_state_loopback()); + ASSERT_TRUE(!!user_data) << "Error allocating user data"; + + r = cubeb_get_min_latency(ctx, &input_params, &latency_frames); + ASSERT_EQ(r, CUBEB_OK) << "Could not get minimal latency"; + + /* setup an input stream with loopback */ + r = cubeb_stream_init(ctx, &input_stream, "Cubeb loopback input only", + NULL, &input_params, NULL, NULL, latency_frames, + is_float ? data_cb_loop_input_only<float> : data_cb_loop_input_only<short>, + state_cb_loop, user_data.get()); + ASSERT_EQ(r, CUBEB_OK) << "Error initializing cubeb stream"; + + std::unique_ptr<cubeb_stream, decltype(&cubeb_stream_destroy)> + cleanup_input_stream_at_exit(input_stream, cubeb_stream_destroy); + + cubeb_stream_start(input_stream); + delay(50); + cubeb_stream_stop(input_stream); + + /* lock user data to be extra sure to not race any outstanding callbacks */ + std::lock_guard<std::mutex> lock(user_data->user_state_mutex); + std::vector<double>& input_frames = user_data->input_frames; + + /* expect to have at least ~50ms of frames */ + ASSERT_GE(input_frames.size(), SAMPLE_FREQUENCY / 20); + double EPISILON = 0.000001; + /* frames should be 0.0, but use epsilon to avoid possible issues with impls + that may use ~0.0 silence values. */ + for (double frame : input_frames) { + ASSERT_LT(abs(frame), EPISILON); + } +} + +TEST(cubeb, loopback_silence) +{ + run_loopback_silence_test(true); + run_loopback_silence_test(false); +} |