Files
WiVRn/client/scenes/stream_network.cpp

172 lines
4.2 KiB
C++

/*
* WiVRn VR streaming
* Copyright (C) 2022 Guillaume Meunier <[email protected]>
* Copyright (C) 2022 Patrick Nicolas <[email protected]>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#include "stream.h"
#ifdef __ANDROID__
#include "application.h"
#endif
#include "utils/named_thread.h"
#include <spdlog/spdlog.h>
#include <uni_algo/case.h>
void scenes::stream::process_packets()
{
#ifdef __ANDROID__
application::instance().setup_jni();
#endif
while (state_ != state::shutdown)
{
try
{
network_session->poll(*this, std::chrono::milliseconds(500));
}
catch (std::exception & e)
{
spdlog::info("Exception in network thread, exiting: {}", e.what());
exit();
}
}
}
void scenes::stream::operator()(to_headset::server_message && message)
{
switch (message.kind)
{
case to_headset::server_message::kind::toast:
case to_headset::server_message::kind::toast_urgent: {
auto toast = gui_toast.lock();
toast->emplace(message.msg, message.kind == to_headset::server_message::kind::toast_urgent);
gui_status_last_change = instance.now();
break;
}
case to_headset::server_message::kind::error: {
auto queue = stream_error_queue.lock();
queue->emplace(std::move(message.msg));
break;
}
}
}
void scenes::stream::operator()(to_headset::video_stream_data_shard && shard)
{
std::shared_lock lock(decoder_mutex);
uint8_t idx = shard.stream_item_idx;
if (idx >= decoders.size())
{
// We don't know (yet?) about this stream, ignore packet
return;
}
decoders[idx].decoder->push_shard(std::move(shard));
}
void scenes::stream::operator()(to_headset::feature_control && control)
{
switch (control.f)
{
case wivrn::to_headset::feature_control::hid_input:
hid_forwarding = control.state;
return;
case wivrn::to_headset::feature_control::microphone:
if (audio_handle)
audio_handle->set_mic_state(control.state);
return;
}
}
void scenes::stream::operator()(to_headset::audio_stream_description && desc)
{
audio_handle.emplace(desc, *network_session, instance);
}
void scenes::stream::operator()(to_headset::video_stream_description && desc)
{
setup(desc);
if (not tracking_thread)
{
tracking_thread = utils::named_thread("tracking_thread", &stream::tracking, this);
}
}
void scenes::stream::operator()(to_headset::refresh_rate_change && rate)
{
session.set_refresh_rate(rate.fps);
std::shared_lock lock(decoder_mutex);
if (video_stream_description)
video_stream_description->fps = rate.fps;
}
void scenes::stream::operator()(to_headset::stream_tab_change && tab)
{
next_gui_status = tab.tab;
}
void scenes::stream::operator()(to_headset::timesync_query && query)
{
network_session->send_stream(from_headset::timesync_response{
.query = query.query,
.response = instance.now(),
});
}
void scenes::stream::operator()(audio_data && data)
{
if (audio_handle)
(*audio_handle)(std::move(data));
}
void scenes::stream::send_feedback(const wivrn::from_headset::feedback & feedback)
{
try
{
network_session->send_control(wivrn::from_headset::feedback{feedback});
}
catch (std::exception & e)
{
spdlog::warn("Exception while sending feedback packet: {}", e.what());
}
}
void scenes::stream::operator()(to_headset::application_list && l)
{
apps(std::move(l));
}
void scenes::stream::operator()(to_headset::application_icon && icon)
{
apps(std::move(icon));
}
void scenes::stream::operator()(to_headset::running_applications && apps)
{
*running_applications.lock() = std::move(apps);
}
void scenes::stream::start_application(std::string appid)
{
network_session->send_control(wivrn::from_headset::start_app{
.app_id = std::move(appid),
});
}