-
Notifications
You must be signed in to change notification settings - Fork 240
Add Linux/Windows support for audio renderer (audio frame capture) #1128
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
alfredobs97
wants to merge
1
commit into
livekit:main
Choose a base branch
from
alfredobs97:feat/desktop-audio-renderer
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,160 @@ | ||
| #include "audio_renderer.h" | ||
|
|
||
| #include <algorithm> | ||
| #include <cstring> | ||
|
|
||
| namespace { | ||
|
|
||
| std::vector<int16_t> UpsampleAudio(const int16_t *src, int src_frames, | ||
| int out_frames, int channels) { | ||
| std::vector<int16_t> out(static_cast<size_t>(out_frames) * channels); | ||
|
|
||
| // Edge case: a single source frame — just repeat it. | ||
| if (src_frames <= 1) { | ||
| for (int f = 0; f < out_frames; ++f) { | ||
| for (int ch = 0; ch < channels; ++ch) { | ||
| out[f * channels + ch] = src[ch]; | ||
| } | ||
| } | ||
| return out; | ||
| } | ||
|
|
||
| double ratio = | ||
| static_cast<double>(src_frames) / static_cast<double>(out_frames); | ||
|
|
||
| for (int f = 0; f < out_frames; ++f) { | ||
| double src_pos = f * ratio; | ||
| int idx = std::min(static_cast<int>(src_pos), src_frames - 2); | ||
| float frac = static_cast<float>(src_pos - idx); | ||
|
|
||
| for (int ch = 0; ch < channels; ++ch) { | ||
| int16_t s0 = src[idx * channels + ch]; | ||
| int16_t s1 = src[(idx + 1) * channels + ch]; | ||
| int sample = static_cast<int>(s0 + frac * (s1 - s0)); | ||
| sample = std::clamp(sample, static_cast<int>(INT16_MIN), | ||
| static_cast<int>(INT16_MAX)); | ||
| out[f * channels + ch] = static_cast<int16_t>(sample); | ||
| } | ||
| } | ||
|
|
||
| return out; | ||
| } | ||
|
|
||
| std::vector<int16_t> DownsampleAudio(const int16_t *src, int src_frames, | ||
| int out_frames, int src_rate, | ||
| int target_rate, int channels) { | ||
| std::vector<int16_t> out(static_cast<size_t>(out_frames) * channels); | ||
| double ratio = | ||
| static_cast<double>(src_rate) / static_cast<double>(target_rate); | ||
|
|
||
| for (int f = 0; f < out_frames; ++f) { | ||
| int src_start = static_cast<int>(f * ratio); | ||
| int src_end = std::min(static_cast<int>((f + 1) * ratio), src_frames); | ||
|
|
||
| for (int ch = 0; ch < channels; ++ch) { | ||
| int64_t sum = 0; | ||
| for (int i = src_start; i < src_end; ++i) { | ||
| sum += src[i * channels + ch]; | ||
| } | ||
| int count = src_end - src_start; | ||
| out[f * channels + ch] = | ||
| count > 0 ? static_cast<int16_t>(std::clamp<int64_t>( | ||
| sum / count, INT16_MIN, INT16_MAX)) | ||
| : 0; | ||
| } | ||
| } | ||
|
|
||
| return out; | ||
| } | ||
|
|
||
| } // namespace | ||
|
|
||
| RendererAudioFormat RendererAudioFormat::FromValues( | ||
| const std::string &common_format, int sample_rate, int channels) { | ||
| RendererAudioFormat format; | ||
| format.common_format = common_format.empty() ? "int16" : common_format; | ||
| format.sample_rate = sample_rate > 0 ? sample_rate : 48000; | ||
| format.channels = channels > 0 ? channels : 1; | ||
| return format; | ||
| } | ||
|
|
||
| std::vector<int16_t> ResampleAudio(const int16_t *src, int src_frames, | ||
| int src_rate, int target_rate, | ||
| int channels, int &out_frames) { | ||
| if (src_rate == target_rate || src_frames <= 0 || channels <= 0) { | ||
| out_frames = src_frames; | ||
| return std::vector<int16_t>( | ||
| src, src + static_cast<size_t>(std::max(src_frames, 0)) * channels); | ||
| } | ||
|
|
||
| out_frames = static_cast<int>((static_cast<int64_t>(src_frames) * | ||
| target_rate) / | ||
| src_rate); | ||
| if (out_frames <= 0) { | ||
| out_frames = 0; | ||
| return {}; | ||
| } | ||
|
|
||
| if (target_rate > src_rate) { | ||
| return UpsampleAudio(src, src_frames, out_frames, channels); | ||
| } | ||
| return DownsampleAudio(src, src_frames, out_frames, src_rate, target_rate, | ||
| channels); | ||
| } | ||
|
|
||
| AudioConversionResult | ||
| ConvertAudioData(const void *audio_data, int bits_per_sample, int sample_rate, | ||
| size_t number_of_channels, size_t number_of_frames, | ||
| const RendererAudioFormat &target_format) { | ||
| AudioConversionResult result; | ||
|
|
||
| // WebRTC AudioTrackSink always delivers 16-bit signed int16 PCM. | ||
| if (bits_per_sample != 16 || number_of_channels == 0 || | ||
| number_of_frames == 0) { | ||
| return result; | ||
| } | ||
|
|
||
| int channels = static_cast<int>(number_of_channels); | ||
| int src_frames = static_cast<int>(number_of_frames); | ||
| const int16_t *src = reinterpret_cast<const int16_t *>(audio_data); | ||
|
|
||
| int out_frames = 0; | ||
| std::vector<int16_t> resampled = ResampleAudio( | ||
| src, src_frames, sample_rate, target_format.sample_rate, channels, | ||
| out_frames); | ||
| if (out_frames <= 0) { | ||
| return result; | ||
| } | ||
|
|
||
| int requested_channels = std::max(target_format.channels, 1); | ||
| int out_channels = std::min(requested_channels, channels); | ||
|
|
||
| result.frame_length = out_frames; | ||
| result.channels = out_channels; | ||
|
|
||
| if (target_format.common_format == "float32") { | ||
| result.data.resize(static_cast<size_t>(out_frames) * out_channels * 4); | ||
| for (int f = 0; f < out_frames; ++f) { | ||
| for (int ch = 0; ch < out_channels; ++ch) { | ||
| float sample = resampled[f * channels + ch] / 32767.0f; | ||
| size_t offset = (static_cast<size_t>(f) * out_channels + ch) * 4; | ||
| // memcpy relies on the host being little-endian, true for the | ||
| // x86/x64/ARM desktop targets this plugin builds for. | ||
| std::memcpy(&result.data[offset], &sample, sizeof(float)); | ||
| } | ||
| } | ||
| } else { | ||
| result.data.resize(static_cast<size_t>(out_frames) * out_channels * 2); | ||
| for (int f = 0; f < out_frames; ++f) { | ||
| for (int ch = 0; ch < out_channels; ++ch) { | ||
| int16_t sample = resampled[f * channels + ch]; | ||
| size_t offset = (static_cast<size_t>(f) * out_channels + ch) * 2; | ||
| result.data[offset] = static_cast<uint8_t>(sample & 0xFF); | ||
| result.data[offset + 1] = static_cast<uint8_t>((sample >> 8) & 0xFF); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| result.success = true; | ||
| return result; | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🔍 Potential use-after-free race between RemoveSink and concurrent OnData callback
In
stopAudioRenderer(linux/livekit_plugin.cpp:446-452), the mutex is held whileRemoveSink()is called and the renderer is erased from the map. However, ifOnDatais currently executing on the WebRTC audio thread whenRemoveSinkis called, theAudioRendererSinkobject could be destroyed (byrenderers_.erase) whileOnDatais still accessing its members (e.g.,format_,sink_). Whether this is safe depends on whether WebRTC'sRemoveSinkblocks until any in-flightOnDatacall completes. The existingVisualizerSinkhas the identical pattern (linux/livekit_plugin.cpp:365-369), so this is a pre-existing architectural concern.Was this helpful? React with 👍 or 👎 to provide feedback.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good catch to scrutinize — I verified this against both audio delivery paths used by the desktop binaries, and the pattern is safe:
RemoveSinkblocks until any in-flightOnDatacompletes, so the sink cannot be destroyed whileOnDatais executing.Remote tracks (
pc/remote_audio_source.cc, upstream WebRTC and thewebrtc-sdkfork that ships the desktop prebuilts):RemoteAudioSource::OnDataholdssink_lock_while iterating sinks and invokingsink->OnData(...), andRemoteAudioSource::RemoveSinkacquires the samesink_lock_.RemoveSinktherefore cannot return while a delivery is in flight, and no further callbacks occur after it returns.Local tracks (
webrtc-sdk/libwebrtc,src/internal/local_audio_track.h): identical discipline —AddSink,RemoveSink, andOnDataall take the samesink_lock_, withsinks_ RTC_GUARDED_BY(sink_lock_).Corroborating evidence: the wrapper itself relies on this guarantee.
AudioTrackImpl::RemoveSink(src/rtc_audio_track_impl.h) destroys theAudioTrackSinkAdapterimmediately afterrtc_track_->RemoveSink(...)returns. If the guarantee did not hold, every existing consumer — including theVisualizerSinkthat has been shipping on Linux/Windows — would already be crashing on stop.Holding the plugin
mutex_whileRemoveSinkblocks is also fine:OnDatanever touches the plugin mutex, so there is no lock-order cycle.As noted,
VisualizerSinkuses the identical pattern, so this PR intentionally keeps the two classes consistent. There is a separate pre-existing (and unrelated) subtlety shared by both classes —sink_/on_listen_called_are written on the platform thread and read on the audio thread without synchronization — which would be better addressed as a follow-up cleanup touching both sinks rather than diverging here.