BlueGit

ict rusty-teams

Files

rusty-teams / vendor / webrtc-sys / src / audio_mixer.cpp

audio_mixer.cpp

audio_mixer.cpp 3.3 KiB Download Raw
1/*
2 * Copyright 2025 LiveKit, Inc.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "livekit/audio_mixer.h"
18
19#include <iostream>
20#include <memory>
21
22#include "api/audio/audio_frame.h"
23#include "api/audio/audio_mixer.h"
24#include "modules/audio_mixer/audio_mixer_impl.h"
25#include "webrtc-sys/src/audio_mixer.rs.h"
26
27namespace livekit_ffi {
28
29AudioMixer::AudioMixer() {
30 audio_mixer_ = webrtc::AudioMixerImpl::Create();
31}
32
33void AudioMixer::add_source(rust::Box<AudioMixerSourceWrapper> source) {
34 auto native_source = std::make_shared<AudioMixerSource>(std::move(source));
35
36 webrtc::MutexLock lock(&sources_mutex_);
37 audio_mixer_->AddSource(native_source.get());
38 sources_.push_back(native_source);
39}
40
41void AudioMixer::remove_source(int source_ssrc) {
42 webrtc::MutexLock lock(&sources_mutex_);
43 auto it = std::find_if(
44 sources_.begin(), sources_.end(),
45 [source_ssrc](const auto& s) { return s->Ssrc() == source_ssrc; });
46
47 if (it != sources_.end()) {
48 audio_mixer_->RemoveSource(it->get());
49 sources_.erase(it);
50 }
51}
52
53size_t AudioMixer::mix(size_t number_of_channels) {
54 audio_mixer_->Mix(number_of_channels, &frame_);
55 return frame_.num_channels() * frame_.samples_per_channel();
56}
57
58const int16_t* AudioMixer::data() const {
59 return frame_.data();
60}
61
62std::unique_ptr<AudioMixer> create_audio_mixer() {
63 return std::make_unique<AudioMixer>();
64}
65
66AudioMixerSource::AudioMixerSource(rust::Box<AudioMixerSourceWrapper> source)
67 : source_(std::move(source)) {}
68
69int AudioMixerSource::Ssrc() const {
70 return source_->ssrc();
71}
72
73int AudioMixerSource::PreferredSampleRate() const {
74 return source_->preferred_sample_rate();
75}
76
77webrtc::AudioMixer::Source::AudioFrameInfo
78AudioMixerSource::GetAudioFrameWithInfo(int sample_rate,
79 webrtc::AudioFrame* audio_frame) {
80 NativeAudioFrame frame(audio_frame);
81
82 livekit_ffi::AudioFrameInfo result =
83 source_->get_audio_frame_with_info(sample_rate, frame);
84
85 if (result == livekit_ffi::AudioFrameInfo::Normal) {
86 return webrtc::AudioMixer::Source::AudioFrameInfo::kNormal;
87 } else if (result == livekit_ffi::AudioFrameInfo::Muted) {
88 return webrtc::AudioMixer::Source::AudioFrameInfo::kMuted;
89 } else {
90 return webrtc::AudioMixer::Source::AudioFrameInfo::kError;
91 }
92}
93
94void NativeAudioFrame::update_frame(uint32_t timestamp,
95 const int16_t* data,
96 size_t samples_per_channel,
97 int sample_rate_hz,
98 size_t num_channels) {
99 frame_->UpdateFrame(timestamp, data, samples_per_channel, sample_rate_hz,
100 webrtc::AudioFrame::SpeechType::kNormalSpeech,
101 webrtc::AudioFrame::VADActivity::kVadUnknown,
102 num_channels);
103}
104
105} // namespace livekit_ffi