82 lines
3.8 KiB
C++
82 lines
3.8 KiB
C++
#pragma once
|
|
|
|
#include <pipes/rtc/PeerConnection.h>
|
|
#include <pipes/rtc/channels/ApplicationChannel.h>
|
|
#include <pipes/rtc/channels/AudioChannel.h>
|
|
|
|
namespace ts {
|
|
namespace server {
|
|
class WebClient;
|
|
}
|
|
namespace web {
|
|
class VoiceBridge {
|
|
public:
|
|
typedef std::function<void(const pipes::buffer_view&, bool /* is sequence start */)> cb_voice_data;
|
|
typedef std::function<void(const rtc::IceCandidate&)> cb_ice_candidate;
|
|
typedef std::function<void(const std::string& /* sdpMid */, int /* sdpMLineIndex */)> cb_ice_candidate_finish;
|
|
typedef std::function<void()> cb_initialized;
|
|
typedef std::function<void()> cb_failed;
|
|
|
|
std::shared_ptr<rtc::DataChannel> voice_channel() { return this->voice_channel_; }
|
|
std::shared_ptr<rtc::DataChannel> voice_whisper_channel() { return this->voice_whisper_channel_; }
|
|
|
|
explicit VoiceBridge(const std::shared_ptr<server::WebClient>&);
|
|
virtual ~VoiceBridge();
|
|
|
|
bool initialize(std::string&);
|
|
bool parse_offer(const std::string&);
|
|
int apply_ice(const std::deque<std::shared_ptr<rtc::IceCandidate>>& /* candidates */);
|
|
void remote_ice_finished();
|
|
std::string generate_answer();
|
|
|
|
cb_ice_candidate callback_ice_candidate;
|
|
cb_ice_candidate_finish callback_ice_candidate_finished;
|
|
cb_voice_data callback_voice_data;
|
|
cb_voice_data callback_voice_whisper_data;
|
|
cb_initialized callback_initialized;
|
|
cb_failed callback_failed;
|
|
|
|
void execute_tick();
|
|
private:
|
|
struct VoiceStateData {
|
|
uint16_t sequence_packet_id{0};
|
|
bool muted{true};
|
|
|
|
std::weak_ptr<rtc::AudioChannel>& channel;
|
|
cb_voice_data& callback;
|
|
};
|
|
|
|
static void callback_log(void* ptr, pipes::Logger::LogLevel level, const std::string& name, const std::string& message, ...);
|
|
|
|
inline int server_id();
|
|
inline std::shared_ptr<server::WebClient> owner();
|
|
|
|
void handle_media_stream(const std::shared_ptr<rtc::Channel>& /* stream */);
|
|
void handle_data_channel(const std::shared_ptr<rtc::DataChannel> & /* channel */);
|
|
|
|
void handle_audio_voice_data(const std::shared_ptr<rtc::MediaChannel>& /* channel */, const pipes::buffer_view& /* buffer */, size_t /* payload offset */);
|
|
void handle_audio_voice_whisper_data(const std::shared_ptr<rtc::MediaChannel>& /* channel */, const pipes::buffer_view& /* buffer */, size_t /* payload offset */);
|
|
|
|
static void handle_audio_voice_x_data(VoiceStateData* /* state */, const pipes::buffer_view& /* buffer */, size_t /* payload offset */);
|
|
|
|
std::weak_ptr<server::WebClient> _owner;
|
|
std::chrono::system_clock::time_point offer_timestamp;
|
|
std::unique_ptr<rtc::PeerConnection> connection;
|
|
|
|
std::shared_ptr<rtc::DataChannel> voice_channel_{};
|
|
std::shared_ptr<rtc::DataChannel> voice_whisper_channel_{};
|
|
|
|
std::weak_ptr<rtc::AudioChannel> incoming_voice_channel_{};
|
|
std::weak_ptr<rtc::AudioChannel> incoming_whisper_channel_{};
|
|
|
|
VoiceStateData voice_state{
|
|
.channel = this->incoming_voice_channel_,
|
|
.callback = this->callback_voice_data
|
|
};
|
|
VoiceStateData whisper_state{
|
|
.channel = this->incoming_whisper_channel_,
|
|
.callback = this->callback_voice_whisper_data
|
|
};
|
|
};
|
|
}
|
|
} |