|
| 1 | +/* |
| 2 | + * Copyright 2025 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 <condition_variable> |
| 20 | +#include <cstdint> |
| 21 | +#include <deque> |
| 22 | +#include <memory> |
| 23 | +#include <mutex> |
| 24 | +#include <optional> |
| 25 | +#include <string> |
| 26 | + |
| 27 | +#include "audio_frame.h" |
| 28 | +#include "ffi_handle.h" |
| 29 | +#include "participant.h" |
| 30 | +#include "track.h" |
| 31 | + |
| 32 | +namespace livekit { |
| 33 | + |
| 34 | +namespace proto { |
| 35 | +class FfiEvent; |
| 36 | +} |
| 37 | + |
| 38 | +struct AudioFrameEvent { |
| 39 | + AudioFrame frame; |
| 40 | +}; |
| 41 | + |
| 42 | +class AudioStream { |
| 43 | +public: |
| 44 | + struct Options { |
| 45 | + std::size_t capacity{0}; // 0 = unbounded |
| 46 | + int sample_rate{48000}; |
| 47 | + int num_channels{1}; |
| 48 | + std::string noise_cancellation_module; // empty = disabled |
| 49 | + std::string noise_cancellation_options_json; // empty = no options |
| 50 | + }; |
| 51 | + |
| 52 | + // Factory: create an AudioStream bound to a specific Track |
| 53 | + static std::unique_ptr<AudioStream> |
| 54 | + from_track(const std::shared_ptr<Track> &track, const Options &options); |
| 55 | + |
| 56 | + // Factory: create an AudioStream from a Participant + TrackSource |
| 57 | + static std::unique_ptr<AudioStream> from_participant(Participant &participant, |
| 58 | + TrackSource track_source, |
| 59 | + const Options &options); |
| 60 | + |
| 61 | + ~AudioStream(); |
| 62 | + |
| 63 | + AudioStream(const AudioStream &) = delete; |
| 64 | + AudioStream &operator=(const AudioStream &) = delete; |
| 65 | + AudioStream(AudioStream &&) noexcept; |
| 66 | + AudioStream &operator=(AudioStream &&) noexcept; |
| 67 | + |
| 68 | + /// Blocking read: returns true if a frame was delivered, |
| 69 | + /// false if the stream has ended (EOS or closed). |
| 70 | + bool read(AudioFrameEvent &out_event); |
| 71 | + |
| 72 | + /// Signal that we are no longer interested in frames. |
| 73 | + /// Disposes the underlying FFI stream and removes the listener. |
| 74 | + void close(); |
| 75 | + |
| 76 | +private: |
| 77 | + AudioStream() = default; |
| 78 | + |
| 79 | + void init_from_track(const std::shared_ptr<Track> &track, |
| 80 | + const Options &options); |
| 81 | + void init_from_participant(Participant &participant, TrackSource track_source, |
| 82 | + const Options &options); |
| 83 | + |
| 84 | + // FFI event handler (registered with FfiClient) |
| 85 | + void on_ffi_event(const proto::FfiEvent &event); |
| 86 | + |
| 87 | + // Queue helpers |
| 88 | + void push_frame(AudioFrameEvent &&ev); |
| 89 | + void push_eos(); |
| 90 | + |
| 91 | + mutable std::mutex mutex_; |
| 92 | + std::condition_variable cv_; |
| 93 | + std::deque<AudioFrameEvent> queue_; |
| 94 | + std::size_t capacity_{0}; |
| 95 | + bool eof_{false}; |
| 96 | + bool closed_{false}; |
| 97 | + |
| 98 | + Options options_; |
| 99 | + |
| 100 | + // Underlying FFI audio stream handle |
| 101 | + FfiHandle stream_handle_; |
| 102 | + |
| 103 | + // Listener id registered on FfiClient |
| 104 | + std::int64_t listener_id_{0}; |
| 105 | +}; |
| 106 | + |
| 107 | +} // namespace livekit |
0 commit comments