Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
53 commits
Select commit Hold shift + click to select a range
1e0d08d
init
rogurotus Mar 22, 2023
4155ba3
temp
rogurotus Mar 22, 2023
60038bd
test
rogurotus Mar 23, 2023
17cbeed
aaa
rogurotus Mar 24, 2023
675ad00
2 ch
rogurotus Mar 24, 2023
3d4b54a
temp
rogurotus Mar 24, 2023
3cbecef
ch
rogurotus Mar 24, 2023
f32d887
fix
rogurotus Mar 27, 2023
b8b2724
temp
rogurotus Mar 27, 2023
65e2efc
temp
rogurotus Mar 29, 2023
6018242
test
rogurotus Mar 29, 2023
57e2b1f
temp
rogurotus Mar 30, 2023
c0937d9
a
rogurotus Mar 30, 2023
6b17bce
temp
rogurotus Apr 4, 2023
cd4150b
pre
rogurotus Apr 5, 2023
3a9ca1b
refactor
rogurotus Apr 6, 2023
fd2e205
refactor
rogurotus Apr 6, 2023
7f28708
add docs
rogurotus Apr 7, 2023
a93dd95
fix device change
rogurotus Apr 7, 2023
2e741d8
delete trash
rogurotus Apr 7, 2023
3521c4e
temp
rogurotus Apr 7, 2023
04244a0
windows
rogurotus Apr 10, 2023
0d8c740
refactor
rogurotus Apr 11, 2023
72e8edf
refactor
rogurotus Apr 11, 2023
e3aab76
try fix swift
rogurotus Apr 11, 2023
7fb3eb1
retry
rogurotus Apr 11, 2023
aa182b1
fix
rogurotus Apr 11, 2023
125fede
fix
rogurotus Apr 11, 2023
b09893f
refactor
rogurotus Apr 11, 2023
5294d11
fix
rogurotus Apr 11, 2023
a8f68a3
fmt
rogurotus Apr 11, 2023
b7dee8a
mock macos
rogurotus Apr 11, 2023
820d2ac
fix ci
rogurotus Apr 11, 2023
a592a80
temp
rogurotus Apr 12, 2023
4b0fbd5
temp
rogurotus Apr 12, 2023
386bcfe
a
rogurotus Apr 12, 2023
56c2c13
refactoring
rogurotus Apr 14, 2023
7ee50f4
temp
rogurotus Apr 20, 2023
10f0493
fix change window
rogurotus Apr 20, 2023
21c606d
refactor
rogurotus Apr 21, 2023
7dc4484
refactor
rogurotus Apr 21, 2023
9d0c4bb
refactor
rogurotus Apr 24, 2023
56d3749
add doc
rogurotus Apr 24, 2023
12efdb2
fix dart
rogurotus Apr 24, 2023
0b3a975
add docs
rogurotus May 2, 2023
0369582
fix
rogurotus May 2, 2023
f344cd8
fix windows
rogurotus May 2, 2023
779eb96
fix CI
rogurotus May 2, 2023
bab53b8
fix
rogurotus May 2, 2023
9b38244
fix
rogurotus May 2, 2023
1418f69
fix
rogurotus May 2, 2023
fef2c25
fix test
rogurotus May 3, 2023
37b5ee9
fix linux
rogurotus May 18, 2023
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion crates/libwebrtc-sys/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ fn download_libwebrtc() -> anyhow::Result<()> {
break;
};
hasher.update(&buffer[0..count]);
let _ = out_file.write(&buffer[0..count])?;
let _: usize = out_file.write(&buffer[0..count])?;
}

if format!("{:x}", hasher.finalize()) != expected_hash {
Expand Down
143 changes: 143 additions & 0 deletions crates/libwebrtc-sys/include/adm.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
#pragma once


#define WEBRTC_INCLUDE_INTERNAL_AUDIO_DEVICE 1
#include <iostream>
#include "api/task_queue/task_queue_factory.h"
#include "modules/audio_device/audio_device_impl.h"

#include <memory>

#include "api/media_stream_interface.h"
#include "api/sequence_checker.h"
#include "modules/audio_device/audio_device_buffer.h"
#include "modules/audio_device/audio_device_generic.h"
#include "modules/audio_device/include/audio_device.h"
#include "modules/audio_device/include/audio_device_defines.h"

#include "rtc_base/event.h"
#include "rtc_base/platform_thread.h"
#include "rtc_base/synchronization/mutex.h"
#include "rtc_base/thread_annotations.h"

#include "modules/audio_mixer/audio_mixer_impl.h"

#include "api/audio/audio_mixer.h"

#include "api/audio/audio_frame.h"

#include "custom_audio.h"

#if defined(WEBRTC_USE_X11)
#include <X11/Xlib.h>
#endif

#include "microphone_module.h"
#include "system_audio_module.h"

#if defined(WEBRTC_LINUX)
#include "modules/audio_device/linux/audio_mixer_manager_pulse_linux.h"
#include "modules/audio_device/linux/pulseaudiosymboltable_linux.h"
#include "linux_system_audio_module.h"
#include "linux_microphone_module.h"
#endif

#if defined(WEBRTC_WIN)
#include "windows_microphone_module.h"
#include "windows_system_audio_module.h"
#endif

#if defined(WEBRTC_MAC)
#include "macos_microphone_module.h"
#include "macos_system_audio_module.h"
#endif



class AudioSourceManager {
public:
// Creates a `AudioSource` from a microphone.
virtual rtc::scoped_refptr<AudioSource> CreateMicrophoneSource() = 0;
// Creates a `AudioSource` from a system audio.
virtual rtc::scoped_refptr<AudioSource> CreateSystemSource() = 0;
// Enumerates possible system audio sources.
virtual std::vector<AudioSourceInfo> EnumerateSystemSource() const = 0;
// Sets the system audio source.
virtual void SetRecordingSource(int id) = 0;
// Sets the volume of the system audio capture.
virtual void SetSystemAudioVolume(float volume) = 0;
// Returns the current volume of the system audio capture.
virtual float GetSystemAudioVolume() const = 0;
// Adds `AudioSource` to `AudioSourceManager`.
virtual void AddSource(rtc::scoped_refptr<AudioSource> source) = 0;
// Removes `AudioSource` to `AudioSourceManager`.
virtual void RemoveSource(rtc::scoped_refptr<AudioSource> source) = 0;
};


class CustomAudioDeviceModule : public webrtc::AudioDeviceModuleImpl, public AudioSourceManager {
public:
CustomAudioDeviceModule(AudioLayer audio_layer, webrtc::TaskQueueFactory* task_queue_factory);
~CustomAudioDeviceModule();


static rtc::scoped_refptr<CustomAudioDeviceModule> Create(
AudioLayer audio_layer,
webrtc::TaskQueueFactory* task_queue_factory);

static rtc::scoped_refptr<CustomAudioDeviceModule> CreateForTest(
AudioLayer audio_layer,
webrtc::TaskQueueFactory* task_queue_factory);

// Mixes source and sends on.
void RecordProcess();

// Main initializaton and termination.
int32_t Init() override;
int32_t Terminate();
int32_t StartRecording() override;
int32_t SetRecordingDevice(uint16_t index) override;
int32_t InitMicrophone() override;
bool MicrophoneIsInitialized() const override;

// Microphone volume controls.
int32_t MicrophoneVolumeIsAvailable(bool* available) override;
int32_t SetMicrophoneVolume(uint32_t volume) override;
int32_t MicrophoneVolume(uint32_t* volume) const override;
int32_t MaxMicrophoneVolume(uint32_t* maxVolume) const override;
int32_t MinMicrophoneVolume(uint32_t* minVolume) const override;

// AudioSourceManager interface.
rtc::scoped_refptr<AudioSource> CreateMicrophoneSource() override;
rtc::scoped_refptr<AudioSource> CreateSystemSource() override;
std::vector<AudioSourceInfo> EnumerateSystemSource() const override;
void SetRecordingSource(int id) override;
void SetSystemAudioVolume(float level) override;
float GetSystemAudioVolume() const override;
void AddSource(rtc::scoped_refptr<AudioSource> source) override;
void RemoveSource(rtc::scoped_refptr<AudioSource> source) override;

// Microphone mute control.
int32_t MicrophoneMuteIsAvailable(bool* available) override;
int32_t SetMicrophoneMute(bool enable) override;
int32_t MicrophoneMute(bool* enabled) const override;

private:
// Mixes `AudioSource` to send.
rtc::scoped_refptr<webrtc::AudioMixerImpl> mixer = webrtc::AudioMixerImpl::Create();

// `AudioSource` for mixing.
std::vector<rtc::scoped_refptr<AudioSource>> sources;
std::mutex source_mutex;

// Audio capture module.
std::unique_ptr<MicrophoneModuleInterface> audio_recorder;
std::unique_ptr<SystemModuleInterface> system_recorder;

// Thread for processing audio frames.
rtc::PlatformThread ptrThreadRec;

// Used to wait for audio sources.
std::condition_variable cv;
bool quit = false;
};
24 changes: 24 additions & 0 deletions crates/libwebrtc-sys/include/audio_source_manager_proxy.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#pragma once

#include "adm.h"
#include "pc/proxy.h"

class AudioSourceManagerProxy : AudioSourceManager {
AudioSourceManagerProxy(rtc::Thread* primary_thread,
rtc::scoped_refptr<CustomAudioDeviceModule> c);
public:
static std::unique_ptr<AudioSourceManager> Create(
rtc::Thread* primary_thread,
rtc::scoped_refptr<CustomAudioDeviceModule> c);
rtc::scoped_refptr<AudioSource> CreateMicrophoneSource() override;
rtc::scoped_refptr<AudioSource> CreateSystemSource() override;
std::vector<AudioSourceInfo> EnumerateSystemSource() const override;
void AddSource(rtc::scoped_refptr<AudioSource> source) override;
void RemoveSource(rtc::scoped_refptr<AudioSource> source) override;
void SetRecordingSource(int id) override;
void SetSystemAudioVolume(float level) override;
float GetSystemAudioVolume() const override;
private:
rtc::scoped_refptr<CustomAudioDeviceModule> adm;
rtc::Thread* primary_thread_;
};
51 changes: 51 additions & 0 deletions crates/libwebrtc-sys/include/bridge.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@
#include "media/base/fake_frame_source.h"
#include "pc/test/fake_video_track_source.h"
#include "modules/audio_device/include/test_audio_device.h"
#include "audio_source_manager_proxy.h"
#include "adm.h"


namespace bridge {

Expand Down Expand Up @@ -103,12 +106,60 @@ using RtpReceiverInterface = rtc::scoped_refptr<webrtc::RtpReceiverInterface>;
using MediaStreamTrackInterface =
rtc::scoped_refptr<webrtc::MediaStreamTrackInterface>;

using CustomAudioDeviceModule = rtc::scoped_refptr<::CustomAudioDeviceModule>;
using AudioSource = rtc::scoped_refptr<::AudioSource>;
using AudioSourceManager = ::AudioSourceManager;
using AudioSourceInfo = ::AudioSourceInfo;


// Creates a new proxied `AudioDeviceModule` for the given `AudioLayer`.
std::unique_ptr<AudioDeviceModule> create_audio_device_module(
Thread& worker_thread,
AudioLayer audio_layer,
TaskQueueFactory& task_queue_factory);

// Creates a new `CustomAudioDeviceModule` for the given `AudioLayer`.
std::unique_ptr<CustomAudioDeviceModule> create_custom_audio_device_module(
Thread& worker_thread,
AudioLayer audio_layer,
TaskQueueFactory& task_queue_factory);

// Creates a new proxied `AudioDeviceModule` from the provided `CustomAudioDeviceModule`.
std::unique_ptr<AudioDeviceModule> custom_audio_device_module_proxy_upcast(std::unique_ptr<CustomAudioDeviceModule> adm, Thread& worker_thread);

// Creates a new `AudioSourceManager` for the given `CustomAudioDeviceModule`.
std::unique_ptr<AudioSourceManager> create_source_manager(const CustomAudioDeviceModule& adm, Thread& worker_thread);

// Creates a new `AudioSource` from microphone.
std::unique_ptr<AudioSource> create_source_microphone(AudioSourceManager& manager);

// Creates a new `AudioSource` from system.
std::unique_ptr<AudioSource> create_system_audio_source(AudioSourceManager& manager);

// Enumerates possible system audio sources.
std::unique_ptr<std::vector<AudioSourceInfo>> enumerate_system_audio_source(const AudioSourceManager& manager);

// Sets the system audio source.
void set_system_audio_source(AudioSourceManager& manager, int64_t id);

// Returns `AudioSourceInfo` id.
int64_t system_source_id(const AudioSourceInfo& source);

// Sets the volume of the system audio capture.
void set_system_audio_source_volume(AudioSourceManager& manager, float level);

// Returns the current volume of the system audio capture.
float system_audio_source_volume(const AudioSourceManager& manager);

// Returns `AudioSourceInfo` title.
std::unique_ptr<std::string> system_source_title(const AudioSourceInfo& source);

// Adds `AudioSource` to `AudioSourceManager`.
void add_source(AudioSourceManager& manager, const AudioSource& source);

// Removes `AudioSource` from `AudioSourceManager`.
void remove_source(AudioSourceManager& manager, const AudioSource& source);

// Creates a new fake `AudioDeviceModule`.
std::unique_ptr<AudioDeviceModule> create_fake_audio_device_module(
TaskQueueFactory& task_queue_factory);
Expand Down
54 changes: 54 additions & 0 deletions crates/libwebrtc-sys/include/custom_audio.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
#pragma once

#include <condition_variable>
#include <mutex>
#include "api/audio/audio_frame.h"
#include "api/audio/audio_mixer.h"
#include "common_audio/resampler/include/push_resampler.h"

class RefCountedAudioSource : public webrtc::AudioMixer::Source,
public rtc::RefCountInterface {};

class AudioSource : public rtc::RefCountedObject<RefCountedAudioSource> {
public:
// Overwrites `audio_frame`. The data_ field is overwritten with
// 10 ms of new audio (either 1 or 2 interleaved channels) at
// `sample_rate_hz`. All fields in `audio_frame` must be updated.
webrtc::AudioMixer::Source::AudioFrameInfo GetAudioFrameWithInfo(
int sample_rate_hz,
webrtc::AudioFrame* audio_frame) override;

// A way for a mixer implementation to distinguish participants.
int Ssrc() const override;

// A way for this source to say that GetAudioFrameWithInfo called
// with this sample rate or higher will not cause quality loss.
int PreferredSampleRate() const;

// Updates the audio frame data.
void UpdateFrame(const int16_t* source,
int size,
int sample_rate,
int channels);

// Mutes the source until the next frame.
void Mute();

// Prepares an audio frame.
void FrameProcessing(int sample_rate_hz, webrtc::AudioFrame* audio_frame);

private:
// Current audio data.
webrtc::AudioFrame frame_;
// Converts an audio date to the specified rate.
webrtc::PushResampler<int16_t> render_resampler_;
// Buffer for converted audio data.
int16_t resample_buffer[webrtc::AudioFrame::kMaxDataSizeSamples];

// Provides synchronization for sending audio frames.
std::mutex mutex_;
std::condition_variable cv_;
std::atomic<bool> frame_available_ = false;
std::atomic<bool> mute_ = false;
std::chrono::time_point<std::chrono::system_clock> mute_clock_;
};
Loading