fork/rust-sdks

git clone https://git.julia.blue/fork/rust-sdks

aoife cassidychore(webrtc-sys): bump to m125_release (#505)a1b926e

main
5.9 KiB200 linesraw
1/*
2 * Copyright 2023 LiveKit
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#pragma once
18
19#include <memory>
20
21#include "api/audio/audio_frame.h"
22#include "api/audio_options.h"
23#include "api/task_queue/task_queue_factory.h"
24#include "common_audio/resampler/include/push_resampler.h"
25#include "livekit/helper.h"
26#include "livekit/media_stream_track.h"
27#include "livekit/webrtc.h"
28#include "pc/local_audio_source.h"
29#include "rtc_base/synchronization/mutex.h"
30#include "api/task_queue/task_queue_base.h"
31#include "rtc_base/task_utils/repeating_task.h"
32#include "rtc_base/thread_annotations.h"
33#include "rust/cxx.h"
34
35namespace livekit {
36class AudioTrack;
37class NativeAudioSink;
38class AudioTrackSource;
39class SourceContext;
40
41using CompleteCallback = void (*)(const livekit::SourceContext*);
42}  // namespace livekit
43#include "webrtc-sys/src/audio_track.rs.h"
44
45namespace livekit {
46
47class AudioTrack : public MediaStreamTrack {
48 private:
49  friend RtcRuntime;
50  AudioTrack(std::shared_ptr<RtcRuntime> rtc_runtime,
51             rtc::scoped_refptr<webrtc::AudioTrackInterface> track);
52
53 public:
54  ~AudioTrack();
55
56  void add_sink(const std::shared_ptr<NativeAudioSink>& sink) const;
57  void remove_sink(const std::shared_ptr<NativeAudioSink>& sink) const;
58
59 private:
60  webrtc::AudioTrackInterface* track() const {
61    return static_cast<webrtc::AudioTrackInterface*>(track_.get());
62  }
63
64  mutable webrtc::Mutex mutex_;
65
66  // Same for VideoTrack:
67  // Keep a strong reference to the added sinks, so we don't need to
68  // manage the lifetime safety on the Rust side
69  mutable std::vector<std::shared_ptr<NativeAudioSink>> sinks_;
70};
71
72class NativeAudioSink : public webrtc::AudioTrackSinkInterface {
73 public:
74  explicit NativeAudioSink(rust::Box<AudioSinkWrapper> observer,
75                           int sample_rate,
76                           int num_channels);
77  void OnData(const void* audio_data,
78              int bits_per_sample,
79              int sample_rate,
80              size_t number_of_channels,
81              size_t number_of_frames) override;
82
83 private:
84  rust::Box<AudioSinkWrapper> observer_;
85
86  int sample_rate_;
87  int num_channels_;
88
89  webrtc::AudioFrame frame_;
90  webrtc::PushResampler<int16_t> resampler_;
91};
92
93std::shared_ptr<NativeAudioSink> new_native_audio_sink(
94    rust::Box<AudioSinkWrapper> observer,
95    int sample_rate,
96    int num_channels);
97
98class AudioTrackSource {
99  class InternalSource : public webrtc::LocalAudioSource {
100   public:
101    InternalSource(const cricket::AudioOptions& options,
102                   int sample_rate,
103                   int num_channels,
104                   int buffer_size_ms,
105                   webrtc::TaskQueueFactory* task_queue_factory);
106
107    ~InternalSource() override;
108
109    SourceState state() const override;
110    bool remote() const override;
111
112    const cricket::AudioOptions options() const override;
113
114    void AddSink(webrtc::AudioTrackSinkInterface* sink) override;
115    void RemoveSink(webrtc::AudioTrackSinkInterface* sink) override;
116
117    void set_options(const cricket::AudioOptions& options);
118
119    bool capture_frame(rust::Slice<const int16_t> audio_data,
120                       uint32_t sample_rate,
121                       uint32_t number_of_channels,
122                       size_t number_of_frames,
123                       const SourceContext* ctx,
124                       void (*on_complete)(const SourceContext*));
125
126    void clear_buffer();
127
128   private:
129    mutable webrtc::Mutex mutex_;
130    std::unique_ptr<webrtc::TaskQueueBase, webrtc::TaskQueueDeleter> audio_queue_;
131    webrtc::RepeatingTaskHandle audio_task_;
132
133    std::vector<webrtc::AudioTrackSinkInterface*> sinks_ RTC_GUARDED_BY(mutex_);
134    std::vector<int16_t> buffer_ RTC_GUARDED_BY(mutex_);
135
136    const SourceContext* capture_userdata_ RTC_GUARDED_BY(mutex_);
137    void (*on_complete_)(const SourceContext*) RTC_GUARDED_BY(mutex_);
138
139    int missed_frames_ RTC_GUARDED_BY(mutex_) = 0;
140    int16_t* silence_buffer_ = nullptr;
141
142    int sample_rate_;
143    int num_channels_;
144    int queue_size_samples_;
145    int notify_threshold_samples_;
146
147    cricket::AudioOptions options_{};
148  };
149
150 public:
151  AudioTrackSource(AudioSourceOptions options,
152                   int sample_rate,
153                   int num_channels,
154                   int queue_size_ms,
155                   webrtc::TaskQueueFactory* task_queue_factory);
156
157  AudioSourceOptions audio_options() const;
158
159  void set_audio_options(const AudioSourceOptions& options) const;
160
161  bool capture_frame(rust::Slice<const int16_t> audio_data,
162                     uint32_t sample_rate,
163                     uint32_t number_of_channels,
164                     size_t number_of_frames,
165                     const SourceContext* ctx,
166                     CompleteCallback on_complete) const;
167
168  void clear_buffer() const;
169
170  rtc::scoped_refptr<InternalSource> get() const;
171
172 private:
173  rtc::scoped_refptr<InternalSource> source_;
174};
175
176std::shared_ptr<AudioTrackSource> new_audio_track_source(
177    AudioSourceOptions options,
178    int sample_rate,
179    int num_channels,
180    int queue_size_ms);
181
182static std::shared_ptr<MediaStreamTrack> audio_to_media(
183    std::shared_ptr<AudioTrack> track) {
184  return track;
185}
186
187static std::shared_ptr<AudioTrack> media_to_audio(
188    std::shared_ptr<MediaStreamTrack> track) {
189  return std::static_pointer_cast<AudioTrack>(track);
190}
191
192static std::shared_ptr<AudioTrack> _shared_audio_track() {
193  return nullptr;  // Ignore
194}
195
196static std::shared_ptr<AudioTrackSource> _shared_audio_track_source() {
197  return nullptr;  // Ignore
198}
199
200}  // namespace livekit