Граф коммитов

1057 Коммитов

Автор SHA1 Сообщение Дата
Chun-Min Chang 9bc68f00b2 Remove Debug trait from DevicesData 2020-03-27 11:34:51 -07:00
Chun-Min Chang 672c79b352 Remove Debug trait from SharedDevices 2020-03-27 11:34:15 -07:00
Chun-Min Chang 7783650c21 Remove Debug trait from LatencyController 2020-03-27 11:33:17 -07:00
Chun-Min Chang 11c941fe2f Remove Debug trait from AudioUnitContext 2020-03-27 11:32:48 -07:00
Chun-Min Chang 0532fb8652 Remove Debug trait from device_property_listener 2020-03-27 11:29:11 -07:00
Chun-Min Chang 58c4014b35 Remove Debug trait from device_info 2020-03-27 11:19:59 -07:00
Chun-Min Chang d10bf1e045 Remove Debug trait from Resampler 2020-03-27 11:11:04 -07:00
Chun-Min Chang 3199fc8f03 Remove Debug trait from Mixer 2020-03-27 11:10:18 -07:00
Chun-Min Chang a9b1d2fd0c Remove Debug trait from AggregateDevice
To avoid `field is never read` error, rename `input_id` and `output_id`
to `_input_id` and `_output_id`.
2020-03-27 11:08:54 -07:00
Chun-Min Chang e41a3c898f Remove Debug trait from AudioUnitStream and CoreStreamData 2020-03-27 11:08:44 -07:00
Chun-Min Chang 20daa86ae2
Revise git hooks (#68)
* Revise pre-push script

* Remove unrelated the comments in pre-push script

* Update README for git hooks
2020-03-26 07:47:24 -07:00
Chun-Min Chang 81d0024b74
Revise `destroy_cubeb_device_info` (#67)
* Revise destroy_cubeb_device_info

* Move destroy_cubeb_device_info tests

`destroy_cubeb_device_info` is checked in the tests calling
`create_cubeb_device_info`. We only need to confirm the `friendly_name`
is set when calling `destroy_cubeb_device_info`
2020-03-26 07:46:43 -07:00
Paul Adenot 1cc8a31d42
Add a hook to check that commits are formatted and linted correctly (#66) 2020-03-25 17:45:33 -07:00
Paul Adenot aa80b3ff3b
Try using the model id as group id. (#64)
It works for the all the external devices that I have, but not for the
built-in mic/speakers and the trrs plug.
2020-03-25 15:46:45 -07:00
Chun-Min Chang ab319780de
Bail if `cargo clippy` or `cargo fmt` fails (#65)
* Bail out if 'cargo fmt' fails

* Bail out if 'cargo clippy' fails

* Move install_rustfmt_clippy.sh from before script to install

* Use loose pattern for nightly toolchain

The name of the nightly toolchain on Travis server is `nightly
(default)` instead of `night-*`.
2020-03-25 15:38:33 -07:00
Paul Adenot 3860bb3f95
Fix comments in #57 (#63)
* Use usize for various frame counters

* Fix logs

* Address review comments

* cargo fmt
2020-03-24 11:57:49 -07:00
Paul Adenot ef5f58b382
Run `cargo fmt` and make scripts executable (#62)
* Make scripts executable

* cargo fmt
2020-03-24 07:58:20 -07:00
Paul Adenot a4c83dbccd
Fix two bugs and add logs (#57)
* Add useful logging statements

* Avoid having a long input buffer if we're not using an aggregate device

This lowers the input latency: it's unnecessary to keep old data around,
we want to most current data, with a safety margin.

* Pass the correct number in minimum_resampling_input_frames

The current code makes no sense.
2020-03-23 16:44:38 -07:00
Chun-Min Chang 799518a033
Rework threading model (#55)
* Create a friendly wrapper around dispatch_queue_t

Instead of using low-level dispatch APIs directly, we should create a
wrapper around `dispatch_queue_t` that can retain or release the
reference count automatically.

* Hide low-level dispatch APIs

With the new `Queue` struct is introduced, there is no need to expose
the low-level dispatch APIs. Those low-level dispatch APIs should be
prevented from being used directly.

* Make the task queue stream-local

The stream start, stop, destroy, reinit tasks, from different streams,
will be dispatched to the same task queue and then be executed one by
one. In fact, those tasks dispatched from different streams can be run
separately in parallel since those tasks don't share data in common. We
can create a task queue per stream and dispatch the stream's own task to
its own task queue so those tasks fired from different streams can be
run in parallel. This should boost the speed of creating, staring,
stopping, or reinitializing multiple streams at the same time.

* Implement a sync task runner that runs the final task

Any task appended after the stream-destroy task should be cancelled.
After moving the task queue from being owned by the cubeb context to the
cubeb stream, we can easily cancel the stream X's job in the task queue
after stream X is destroyed, without interfering other stream's tasks.

* Give task queue an unique label

The task queue in the cubeb stream should have a unique lable so it
would be easier to identify the task queue's owner when debugging
issues.

* Merge create_dispatch_queue to Queue::new

* Merge release_dispatch_queue to Queue::drop

* Merge retain_dispatch_queue to Queue::clone

* Merge get_dispatch_context to Queue::get_context

* Merge set_dispatch_context to Queue::set_context

* Init should_cancel to false in new instead of run_final

* Replace generic type by AtomicBool type in {get, set}_context

* Rename {get,set}_context to {get,set}_should_cancel

* Merge async_dispatch to run_async

* Merge sync_dispatch to Queue::run_{sync, final}

* Move create_closure_and_executor into Queue

* Destroy AudioUnitContext properly

* Revise comments
2020-03-19 11:04:15 -07:00
Chun-Min Chang 6e3e8e8359
Fix memory leak (#54)
* Rename audiounit_device_destroy to destroy_cubeb_device_info

* Fix memory leak in test_create_cubeb_device_info

* Fix memory leak in test_device_destroy_with_different_device_id_and_group_id
2020-03-12 08:26:41 -07:00
Chun-Min Chang 996fcfae86 Revise messages in test_switch_device
Move the test log to the first line of the switch-device test. It will
be clearer about what's going on when the error log is printed.
2020-03-11 13:34:45 -07:00
Chun-Min Chang 6fac4a6235
Isolate device tests (#53)
* Isolate the device-changed tests

Sometimes we need to run the device-changed test quickly, without
running the regular tests. Isolate the device-changed tests can help
programmers to debug the device-related issues without commenting the
scripts in run_tests.sh.

* Respect default RUST_BACKTRACE setting

* Add instructions for device-tests usage

* Add instructions for stream tester usage
2020-02-26 07:57:21 -08:00
Chun-Min Chang b78e817911
Save the duplicate tests and compilings when running sanitizers (#52)
* Fix typo

* Remove the duplicate sanitizer tests

Since the tests in the sub crates will be run in run_tests.sh and the
run_tests.sh will be executed by the sanitizers, the tests in the sub
crates will be executed by the sanitizers with the command
`RUSTFLAGS="-Z sanitizer=*" sh run_tests.sh`. That is, running the tests
in under sub crate by `RUSTFLAGS="-Z sanitizer=*" cargo test` is
duplicate work.

* Simplify run_sanitizers.sh

* Reuse the compiling when building the sub crate

The run_tests.sh should use `cargo * -p <SUB_CRATE>` if it's possible.
By doing so, the compiling for <SUB_CRATE> can be reused when building
the main crate. It will save plenty of compiling time, especially when
compiling this crate for the sanitizers.
2020-02-25 08:11:35 -08:00
Chun-Min Chang 4acd80233e Destroy the stream properly 2020-02-21 13:10:18 -08:00
Chun-Min Chang 54d950a154
Run the tests in the subcrate (#51)
* Run the tests in the sub crate

* Run tests in sub crate by the sanitizers

* Add format and lint(clippy) checks in run_tests.sh

The format check should always be run, in main crate and sub crate, not
only in Travis CI server only. The format checks and lints(clippy)
checks should be moved from .travis.yml to run_tests.sh.
2020-02-20 08:19:16 -08:00
Chun-Min Chang 132d2093f7
Clean up clippy warnings and errors (#49)
* Set RUST_BACKTRACE in the environment

* Add Rust format check in Travis CI

* Add Rust clippy check in Travis CI

* Fix warning: array_into_iter

Replace `.into_iter()` by `.iter()` used in the Rust array

* Fix warning: private_in_public

Allow private struct AudioUnitStream in public interface when running tests

* Fix clippy errors in audio_unit_get_property_info

* Fix clippy errors in audio_unit_render

* Align interface of audio_unit_get_property with audio_unit_get_property_info

- Change `data` and `size` of audio_unit_get_property from raw pointers
to references
- Make the variable `size` used when calling audio_unit_get_property in
test_audiounit_scope_is_enabled live when the calling explicitly

* Align interface of audio_unit_set_property with audio_unit_get_property's one

* Fix clippy warning: redundant_field_names in create_device_info

* Fix clippy warning: let_and_return in create_blank_device

* Fix clippy warning: let_and_return in audiounit_input_callback

* Fix clippy warning: needless_return in host_time_to_ns

* Fix clippy warning: or_fun_call in get_device_label

The function within `or(...)` will always be called and potentially
allocate an object acting as the default. We don't need to call
get_device_name if get_device_source_name works

* Fix clippy warning: len_zero in Mixer::new

* Fix clippy warning: option_map_unit_fn in Finalizer::drop

* Fix clippy error: float_cmp in minimum_resampling_input_frames

* Fix clippy warning: needless_range_loop in audiounit_output_callback

* Allow clippy::mutex_atomic on set_buffer_size_sync

* Fix clippy warning: redundant_clone in get_range_of_sample_rates

* Fix clippy warning: cmp_null in DevicesData::is_empty

* Fix clippy warning: unnecessary_unwrap in reinit

* Fix clippy warning: or_fun_call in current_device

* Allow clippy::cognitive_complexity on CoreStreamData::setup

* Allow clippy::cast_ptr_alignment on get_device_stream_configuration

* Fix clippy warning: needless_range_loop in get_default_channel_order

* Fix clippy warning: missing_safety_doc for audiounit_rust_init

* Avoid using set_len in allocate_array

`set_len` is unsafe function that should be avoided if possible. On the
other hand, we should initialize the item value when allocating the
array by expected size or the number of the items.

* Fix warning: unused_parens in stream_init

* Fix warning: unused_parens in CoreStreamData::setup

* Skip test changing the default device if no device is available

* Automatically install latest clippy and rustfmt

- Download and install the latest available rustfmt if rustfmt isn't
available in the nightly
- Install latest available clippy if clippy in nightly isn't available
2020-02-18 14:09:56 -08:00
Chun-Min Chang 7fe03b4201
Bail out if the output device has no output channel (#50)
* Bail out if the output device has no output channel

* Log the device info in use

The device info will be set when the stream is initialized or reset when
the stream is reinitialized. Logging the device info helps to debug
issues, when calling underlying system APIs raise errors.
2020-02-12 12:41:54 -08:00
Chun-Min Chang ad74bad5cb Update README
- Remove the issues for `audiounit_set_channel_layout` since
`audiounit_set_channel_layout` is removed from the code
- Fix the markdownlint warnings
2020-02-12 11:32:55 -08:00
Chun-Min Chang 3f38b17626
Replace`*.get_mut()` by `*.store()` on all the `Atomic*` (#47)
`Atomic*::get_mut()` uses `UnsafeCell` to mutate the value directly, so `Atomic*::get_mut()` is only safe if it's called from a single thread. If `Atomic*::get_mut()` is called from multiple threads, or `Atomic*` is accessible as mutable references in different threads, it will cause the data race. 
Thus, for all the `Atomic*` in the current code, the `*.get_mut()` should be replaced by `*.store()` to avoid the data race. This change will solve the data race found by ThreadSanitizer(#34).

* Replace all shutdown.get_mut() to shutdown.store()

* Replace all switching_device.get_mut() to switching_device.store()

* Replace all reinit_pending.get_mut() to reinit_pending.store()

* Replace all draining.get_mut() to draining.store()

* Replace all destroy_pending.get_mut() to destroy_pending.store()
2020-02-12 11:05:36 -08:00
Chun-Min Chang b1bc781825
Run tests by sanitizers (#46)
- Add a script to run test suits by all the available sanitizers
- Add an instruction of running sanitizers in _README.md_
- Run the sanitizers on _Travis CI_ but the tests are allowed to fail
  - Since sanitizers are under development, they may false positives, or be unavailable for Nightly toolchain sometimes
2020-02-12 10:34:44 -08:00
Chun-Min Chang 4d8fcc1954
Update to audio mixer 0.1.2 and fix clippy warnings (#44)
* Update audio-mixer to 0.1.2

By ChunMinChang/audio-mixer#1, the audio mixer would be created by
`&[Channel]` instead of `Vec<Channel>`. Update the mixer to match the
changes.

* Use void type for mix buffer to avoid clippy errors
2020-01-23 10:36:33 -08:00
Chun-Min Chang dce7ddc846
Correct the removal of the aggregate device in the device list (#42)
* Add a test checking if the created aggregate device is not in the list

When creating the duplex stream, it's very likely to create an aggrgate
device as well. The aggregate device created is for internal usage so it
shouldn't be exposed to the device list.

* Correct the removal of the aggregate device in the device list

The `PRIVATE_AGGREGATE_DEVICE_NAME` is just part of the `uid` of the
created aggregate device, so we should check if the `uid` contains that
string rather than comparing them.
2020-01-21 09:58:23 -08:00
Chun-Min Chang 1423e493e4
Don't call functions in the assertions (#40)
The function calls in the `debug_assert!` will be ignored in release
build. As a result, the function call inside won't be executed. We
should not call functions in the `debug_assert!`. We should probably
not call functions in `assert!` either since the `assert!` may be
changed to `debug_assert!` someday.
2020-01-21 09:57:41 -08:00
Chun-Min Chang a4a8022586
Replace some `assert` by `debug_assert` (#39)
Some assert might not lead to fatal error can be replaced by
debug_assert. This adds a bit error-tolerance for release build.
2020-01-17 10:47:25 -08:00
Chun-Min Chang a53e94f13f
Make mixer become a standalone crate (#37)
The mixer crate is moved to https://github.com/ChunMinChang/audio-mixer
so we should import it from crates.io instead.
2020-01-06 09:09:18 -08:00
Chun-Min Chang 80978b6c02
Implement Display trait for StringRef (#36)
StringRef should implement `Display` trait to get the `to_string` method
rather than implementing `to_string` by itself. Implementing `Display`
is preferred and will make printing StringRef instance easier. Read more
in:
https://rust-lang.github.io/rust-clippy/master/index.html#inherent_to_string
2020-01-06 09:08:55 -08:00
Chun-Min Chang acb90e9bf3 Add license for mixer crate 2019-12-23 16:31:05 -08:00
Chun-Min Chang 82e7ff989a Move criterion crate to [dev-dependencies]
criterion should be put under [dev-dependencies] rather than
[dependencies] so criterion crate won't be treated as a dependency.
2019-12-20 14:01:21 -08:00
Chun-Min Chang f8b7d968a2
Implement a new any-to-any mixer (#23)
* Create an oxidized mixer

Create an oxidized cubeb-mixer, which is adapted from cubeb_mixer.cpp,
as the new mixer module. The new mixer is able to mix from any input
channels to any output channels, without predefined channel order.

The old cubeb-mixer has a strict rule for channel order. For example,
for an stereo layout, it must be 2-channel. One is front-left, the other
is front-right. And the order of the channels must be [front-left,
front-right]. However, in MacOS, the N-channel output device can actually
be set to an 1-to-N output device with any channel order. For example,
the 6-channel output device can be set as a stereo with channel order:
[unused, unused, front-left, unsued, front-right, unused], where the
unused channel should be silience.

With the new mixer, the restriction of the channel order would be loosen
and audio is able to be mixed from any channel layout to any channel
layout, without any pre-defined channel order and limitation of
channels' number.

* Move special setting from mod.rs to mixer.rs

The output audio layout would be forced to mono or stereo when output
channel count is 1 or 2. We should put this special handling in mixer.rs
rather than mod.rs so all the special setting or error handling could be
in the same place.

* Allow cubeb stream's output layout to be CUBEB_LAYOUT_UNDEFINED

* Correct test cases

* Remove deoendency of cubeb_backend::ChannelLayout in mod.rs

With the new mixer, we don't rely on cubeb_backend::ChannelLayout in
mod.rs anymore. The cubeb_backend::ChannelLayout would be used in
mixer.rs instead.

* Add debugging logs

This log would expose the user's output device settings and the stream
settings that would be mixed. It would help us to understand the user's
device setting when they report mixing-related bug.

* fix typo

* revise comments

* Expose channel info from mixer

* Remove unnecessary clone

* Remove unnecessary comments

* Correct typo

* Replace hardcode by bit-shifted value

* Use criterion crate for benchmark

- Introduce criterion
- Create benchmark tests

* Remove todo

* Apply mixer::Channel::count() to set the total channels in CHANNEL_OERDER
2019-12-20 12:45:30 -08:00
Chun-Min Chang ae43813326
Remove the test_set_channel_layout_input (#32)
Calling audiounit_get_current_channel_layout by input-only
(microphone-only) AudioUnit is prone to cause crashes intermittently.

The test_set_channel_layout_input is likely to cause intermittent crashes
since it calls audiounit_get_current_channel_layout by a input-only
AudioUnit. Removing this test can avoid the random crashes when running
`cargo test` (#31).

The cubeb implementation has no need to get the channel-layout on input
side, so the test_set_channel_layout_input is unnecessary anyway.
2019-12-20 09:37:45 -08:00
Chun-Min Chang 9d0a0e8f63
Improve device switch tests (#14)
* Remove todo-tests

* Print devices when DeviceSwitcher is created

* Speed up the device switching

The index of the current device will be calculated again and again when
the DeviceSwitcher switch the device to next candidate. We could keep
tracking the index instead of calculating it each time when switching
device. The drawback is that the DeviceSwitcher will become mutable but
it's fine since it's only used in the tests.

* Print devices before switching them

* Print the devices in TestDeviceSwitcher internaly
2019-12-19 11:42:00 -08:00
Chun-Min Chang 833a062924
Enable test_ops_stream_register_device_changed_callback (#33)
The test_ops_stream_register_device_changed_callback is disabled before
since stream_register_device_changed_callback ins't implemented in
cubeb-rs's capi_new macro. Howeverm it's already fixed in
https://github.com/djg/cubeb-rs/pull/36 so this test is able to be
turned on.
2019-12-19 11:41:39 -08:00
Chun-Min Chang 868d847c2e
Return error when get_ranges_of_device_sample_rate gives an empty vector (#30)
* Allow get_range_of_sample_rates returns empty range

The ranges returned from get_ranges_of_device_sample_rate may be an
empty vector and then the assertion checking the ranges is not empty
will be hit. We should allow to return the empty ranges in
get_range_of_sample_rates since it's possible.

* Simplify return type of get_range_of_sample_rates

In the previous change, the assertion to check if the list of the
available ranges is removed and the empty list is allowed to be
returned. However, the empty list will be treated as an error in our
usage. In that case, we could just return error result when the list is
empty. That will simplify the API usage.
2019-12-16 14:52:33 -08:00
Chun-Min Chang b1ad8b94fd
Minor cleanup (#27)
* Merge error handling, stream close, in one place

* Remove unnecessary mut for notify_state_changed
2019-12-10 17:30:33 -08:00
Mike Hommey 5fcbd99e1b Add the license to Cargo.toml (#29) 2019-12-05 11:38:50 -08:00
Chun-Min Chang b6d5b75acf
Avoid various racing issues (#26)
* Start AudioUnit in serial queue

The stream-start and stream-reinit may run in parallel and cause a
racing issue: The stream data callback may still run while the stream is
being reinitialized. Running stream-start in the same task queue where
the reinit task is could solve this racing issue.

* Stop AudioUnit in serial queue

The stream-stop and stream-reinit may run in parallel and causes a
racing issue: The stream-stop may executed before stream-restart in
reinit so the stream-stop fails silently. Running stream-stop in the
same task queue where reinit task is could solve this racing issue.

* Stop data-callback in serial queue when destorying stream

The stream-start and stream-destroy may run in parallel and cause a
racing issue: The data-callback may still be run while the stream is
being destroyed. The stream-stop in destroy fails silently in this case.
Moving stream-stop in the task queue where the stream-destroy and
stream-start are, beforing doing stream destroy task, could solve this
racing issue.

* Assert the timing for start AudioUnit

An an assertion to make sure the stream start is only allowed to be
called after the stream is initialized and before the stream is
destroyed.

* Rename variables

* Remove unnecessary mutex

The clone of the mutex that will be passed into sync_dispatch is not
really needed. Those data in the mutexes won't be operated in the same
time since the code after sync_dispatch won't be executed before the task
dispatched by sync_dispatched are finished. Hence, mutex around the data
that will be operated by the code in sync_dispatched can be omitted.

* Log state in state callback
2019-11-26 16:36:59 -08:00
Chun-Min Chang dba0344754
Add manual tests (#24)
* Move STREAM_FORMAT to the function using it

* Add a test template with default callbacks

* Revise comments

* Add manual tests: Loop in input/output/duplex streams

* Remove unnecessary type declarations

* Set RUST_BACKTRACE in test script

* Move common test code to tests/utils.rs

* Fix type

* Terminate stream by pressing q

* Create a stream tester with common testing operations

* Add a choice to quit early

* Revise stream tester

To add the command to create or destroy the stream, the cubeb context
need to be passed to the test function. Instead of using the test
template that will pass the cubeb stream, using the test template that
will give te cubeb context is the right way to create the test.

* Revise command line interface
2019-11-22 10:41:26 -08:00
Chun-Min Chang 0920240e41
Always return null-terminated strings from current_device (#25)
* Always return a null-terminated string

* Return error when both current input and output devices have no datasource
2019-11-13 09:18:08 -08:00
Chun-Min Chang cd954ca94b
Update dependencies (#22)
* Remove unused dependency
2019-10-25 08:42:19 -07:00
Chun-Min Chang 7043a0cb2e
Make audiounit_get_devices_of_type infallible (#21)
* Make audiounit_get_devices_of_type infallible

* Add debugging log
2019-10-24 10:43:13 -07:00