From 1b263a0cfb3ca348e736c1baf8650d3c5ff6ed35 Mon Sep 17 00:00:00 2001 From: Gokert Date: Mon, 29 May 2023 21:51:24 +0300 Subject: [PATCH 1/2] added Camera feature --- application/sources/MainWindow.cpp | 10 +- general_use/include/variables.h | 4 +- gstreamer_interface/CMakeLists.txt | 3 + gstreamer_interface/include/Reciver.h | 20 +- gstreamer_interface/include/Session.h | 21 +- gstreamer_interface/include/SessionManager.h | 28 ++- gstreamer_interface/include/Transmiter.h | 11 +- gstreamer_interface/sources/Reciver.cpp | 201 +++++++++-------- gstreamer_interface/sources/Session.cpp | 4 +- .../sources/SessionManager.cpp | 212 ++++++++++++------ gstreamer_interface/sources/Transmiter.cpp | 171 +++++++------- 11 files changed, 397 insertions(+), 288 deletions(-) diff --git a/application/sources/MainWindow.cpp b/application/sources/MainWindow.cpp index 2efc1bf..a83130c 100644 --- a/application/sources/MainWindow.cpp +++ b/application/sources/MainWindow.cpp @@ -110,11 +110,11 @@ void MainWindow::makeDeviceConnections(DeviceWidget* device) { connect(device, &DeviceWidget::sendStartAudioSession, &streaming_session_manager, &SessionManager::onStartAudioSession); connect(device, &DeviceWidget::sendStopAudioSession, &streaming_session_manager, &SessionManager::onKillAudioSession); - // connect(devoce, &DeviceWidget::sendChangeBitrait, &streaming_session_manager, &SessionManager::); - // connect(devoce, &DeviceWidget::sendChangeVolume, &streaming_session_manager, &SessionManager::); - // connect(devoce, &DeviceWidget::sendSetCameraCaptureMode, &streaming_session_manager, &SessionManager::); - // connect(devoce, &DeviceWidget::sendSetScreenCaptureMode, &streaming_session_manager, &SessionManager::); - // connect(devoce, &DeviceWidget::sendToggleRecording, &streaming_session_manager, &SessionManager::); + connect(device, &DeviceWidget::sendChangeBitrait, &streaming_session_manager, &SessionManager::onSetBitrate); + connect(device, &DeviceWidget::sendChangeVolume, &streaming_session_manager, &SessionManager::onSetVolume); + connect(device, &DeviceWidget::sendSetCameraCaptureMode, &streaming_session_manager, &SessionManager::setCameraCaptureMode); + connect(device, &DeviceWidget::sendSetScreenCaptureMode, &streaming_session_manager, &SessionManager::setScreenCaptureMode); + //connect(devoce, &DeviceWidget::sendToggleRecording, &streaming_session_manager, &SessionManager::); } void MainWindow::onSettingsButtonPressed() { diff --git a/general_use/include/variables.h b/general_use/include/variables.h index 2b542e2..70985d9 100644 --- a/general_use/include/variables.h +++ b/general_use/include/variables.h @@ -1,8 +1,8 @@ #pragma once #include -#define PROJECT_PATH "/home/anton/Documents/GitHubSide/2023_1_RelayTeam" +#define PROJECT_PATH "/home/andr/project/2023_1_RelayTeam" -#define RESOURCES_PATH "/home/anton/Documents/GitHubSide/2023_1_RelayTeam/resources" +#define RESOURCES_PATH "/home/andr/project/2023_1_RelayTeam/resources" const QDir Q_RESOURCE_DIR(RESOURCES_PATH); diff --git a/gstreamer_interface/CMakeLists.txt b/gstreamer_interface/CMakeLists.txt index 4fd459a..53a7a85 100644 --- a/gstreamer_interface/CMakeLists.txt +++ b/gstreamer_interface/CMakeLists.txt @@ -1,6 +1,7 @@ project(gstreamer_interface VERSION 0.1 LANGUAGES CXX) find_package(Qt${QT_VERSION} REQUIRED COMPONENTS + Widgets Core Network ) @@ -15,9 +16,11 @@ add_library(${PROJECT_NAME} SHARED ) target_link_libraries(${PROJECT_NAME} PRIVATE + Qt${QT_VERSION}::Widgets Qt${QT_VERSION}::Core Qt${QT_VERSION}::Network general_use + gstvideo-1.0 ) target_include_directories(${PROJECT_NAME} PUBLIC diff --git a/gstreamer_interface/include/Reciver.h b/gstreamer_interface/include/Reciver.h index fd0670e..ca0aa4b 100644 --- a/gstreamer_interface/include/Reciver.h +++ b/gstreamer_interface/include/Reciver.h @@ -5,6 +5,8 @@ #include #include +#include +#include #include "Session.h" @@ -12,6 +14,8 @@ class Reciver : public Session { private: Q_OBJECT + QWidget* window; + void addLinkVideo(); void addLinkAudio(); @@ -22,29 +26,27 @@ class Reciver : public Session { void startReceive(); +protected: + bool eventFilter(QObject* obj, QEvent* event) override; + public: - explicit Reciver(const qint16 video_port, const qint16 audio_port); - explicit Reciver(const qint16 audio_port); + explicit Reciver(const QHostAddress& ip_address_, const qint16 video_port, const qint16 audio_port); + explicit Reciver(const QHostAddress& ip_address_, const qint16 audio_port); ~Reciver(); public slots: + void onEbableCamera() override; void onEnableVideo() override; void onEnableAudio() override; - void onDisableVideo() override; - - void onDisableAudio() override; - void onStartSession() override; void onKillSession() override; - void onSetVolume(const float volume) override; - - void onSetBitrate(const int bitrate) override; + void onSetVolume(const float volume); signals: void mainWindowClosed(); diff --git a/gstreamer_interface/include/Session.h b/gstreamer_interface/include/Session.h index 9926eac..fbecf3a 100644 --- a/gstreamer_interface/include/Session.h +++ b/gstreamer_interface/include/Session.h @@ -1,7 +1,10 @@ #pragma once #include +#include +#include +#include #include #include #include @@ -27,10 +30,9 @@ class Session : public QObject { Session(const QHostAddress& ip_address_, const qint32 video_port_, const qint32 audio_port_); Session(const QHostAddress& ip_address_, const qint32 audio_port_); Session(const qint32 video_port_, const qint32 audio_port_); - Session(const qint32 audio_port_); - ~Session(); + Session(const qint32 audio_port_); - CustomData data; + CustomData customData; QHostAddress ip_address; qint32 video_port; qint32 audio_port; @@ -40,24 +42,19 @@ class Session : public QObject { public: const static char* representIP(const QHostAddress& ext_ip_address); + ~Session(); + public slots: + virtual void onEbableCamera() = 0; virtual void onEnableVideo() = 0; virtual void onEnableAudio() = 0; - virtual void onDisableVideo() = 0; - - virtual void onDisableAudio() = 0; - virtual void onStartSession() = 0; virtual void onKillSession() = 0; - virtual void onSetVolume(const float volume) = 0; - - virtual void onSetBitrate(const int bitrate) = 0; - signals: void EnableVideo(); @@ -68,4 +65,6 @@ public slots: void DisableAudio(); void sendSessionKilled(); + + void closeWindow(const QHostAddress& ip_address_, QString pipelineName); }; diff --git a/gstreamer_interface/include/SessionManager.h b/gstreamer_interface/include/SessionManager.h index 87adf4b..caa5dcf 100644 --- a/gstreamer_interface/include/SessionManager.h +++ b/gstreamer_interface/include/SessionManager.h @@ -16,6 +16,10 @@ class SessionManager : public QObject { void startThread(Session* session, const QString session_type); + void killSessionIp4(const QHostAddress ip_address, const QString key); + + void killSessionIp6(const QHostAddress ip_address, const QString key); + public: SessionManager(); @@ -24,6 +28,8 @@ class SessionManager : public QObject { public slots: void onStartVideoSession(const QHostAddress ip_address); + void onStartCameraSession(const QHostAddress ip_address); + void onStartAudioSession(const QHostAddress ip_address); void onKillVideoSession(const QHostAddress ip_address); @@ -32,19 +38,31 @@ public slots: void onStartReceivingSession(const QHostAddress ip_address, const QString session_type); - void onKillVideoReciver(const QHostAddress ip_address); - - void onKillAudioReciver(const QHostAddress ip_address); + void onKillCameraSession(const QHostAddress ip_address); void onReceivedPorts(const QHostAddress ip_address, qint32 video_port, qint32 audio_port); + void onReceivedCameraPorts(const QHostAddress ip_address, qint32 video_port); + + void onCloseWindow(const QHostAddress& ip_address_, const QString session_type); + void onSetVolume(const QHostAddress ip_address, const int volume); void onSetBitrate(const QHostAddress ip_address, const int bitrate); + void setCameraCaptureMode(const QHostAddress ip_address); + + void setScreenCaptureMode(const QHostAddress ip_address); + signals: void sendErrorOccured(const QString error_string); + void sendCameraSession(const QHostAddress ip_address, const qint32 video_port); + + void senStartCameraSession(const QHostAddress ip_address,const qint32 video_port); + + void sendSetCameraPort(const QHostAddress ip_address, const qint32 video_port); + void setVolume(const int volume); void setBitrate(const int bitrate); @@ -59,7 +77,9 @@ public slots: void sendKillAll(); + void setPorts(const QHostAddress ip_address, const qint32 session_type); + void sendStartReciver(const QHostAddress local_ip6, const QString session_type); - void sendSetPorts(const QHostAddress ip_address, qint32 video_port, qint32 audio_port); + void sendSetPorts(const QHostAddress ip_address, const qint32 video_port, const qint32 audio_port); }; diff --git a/gstreamer_interface/include/Transmiter.h b/gstreamer_interface/include/Transmiter.h index 2a9fb07..f26f268 100644 --- a/gstreamer_interface/include/Transmiter.h +++ b/gstreamer_interface/include/Transmiter.h @@ -30,19 +30,18 @@ class Transmiter : public Session { ~Transmiter(); public slots: + void onEbableCamera() override; + void onEnableVideo() override; void onEnableAudio() override; - void onDisableVideo() override; - - void onDisableAudio() override; - void onStartSession() override; void onKillSession() override; - void onSetVolume(float volume) override; + void onSetBitrate(const int bitrate); + +signals: - void onSetBitrate(const int bitrate) override; }; diff --git a/gstreamer_interface/sources/Reciver.cpp b/gstreamer_interface/sources/Reciver.cpp index 47b4582..ef99b37 100644 --- a/gstreamer_interface/sources/Reciver.cpp +++ b/gstreamer_interface/sources/Reciver.cpp @@ -1,10 +1,13 @@ #include "Reciver.h" +#include "qevent.h" + void Reciver::addLinkVideo() { + qDebug() << "onEnableVideo RECIVER"; GstElement *pipeline, *capsfilter1, *capsfilter2, *udpsrc, *queue1, *vp8dec, *rtpvp8depay, *videoconvert, *queue2, *autovideosink; GstCaps *caps, *caps2; - if (data.pipeline == NULL) { - data.pipeline = gst_pipeline_new("pipeline"); + if (customData.pipeline == NULL) { + customData.pipeline = gst_pipeline_new("video"); } udpsrc = gst_element_factory_make("udpsrc", "udpsrc"); @@ -13,11 +16,12 @@ void Reciver::addLinkVideo() { rtpvp8depay = gst_element_factory_make("rtpvp8depay", "rtpvp8depay"); vp8dec = gst_element_factory_make("vp8dec", "vp8dec"); videoconvert = gst_element_factory_make("videoconvert", "videoconvert"); - // capsfilter2 = gst_element_factory_make("capsfilter", "capsfilterVideo2"); + capsfilter2 = gst_element_factory_make("capsfilter", "capsfilterVideo2"); queue2 = gst_element_factory_make("queue", "queue2"); - autovideosink = gst_element_factory_make("autovideosink", "videosink"); + autovideosink = gst_element_factory_make("glimagesink", "videosink"); - if (!data.pipeline || !udpsrc || !rtpvp8depay || !vp8dec || !videoconvert || !autovideosink || !queue1 || !queue2 || !capsfilter1) { + if (!customData.pipeline || !udpsrc || !rtpvp8depay || !vp8dec || !videoconvert || !autovideosink || !queue1 || !queue2 || + !capsfilter1 || !capsfilter2) { g_printerr("Not all elements could be created. Exiting.\n"); return; } @@ -28,25 +32,33 @@ void Reciver::addLinkVideo() { caps2 = gst_caps_new_simple("video/x-raw", "width", G_TYPE_INT, 1024, "height", G_TYPE_INT, 600, NULL); g_object_set(G_OBJECT(capsfilter1), "caps", caps, NULL); - // g_object_set(G_OBJECT(capsfilter2), "caps", caps2, NULL); + g_object_set(G_OBJECT(capsfilter2), "caps", caps2, NULL); gst_caps_unref(caps); - // gst_caps_unref(caps2); + gst_caps_unref(caps2); - gst_bin_add_many(GST_BIN(data.pipeline), udpsrc, queue1, capsfilter1, rtpvp8depay, vp8dec, videoconvert, queue2, autovideosink, NULL); + gst_bin_add_many(GST_BIN(customData.pipeline), udpsrc, queue1, capsfilter1, rtpvp8depay, vp8dec, videoconvert, queue2, capsfilter2, + autovideosink, NULL); - if (!gst_element_link_many(udpsrc, queue1, capsfilter1, rtpvp8depay, vp8dec, videoconvert, queue2, autovideosink, NULL)) { + if (!gst_element_link_many(udpsrc, queue1, capsfilter1, rtpvp8depay, vp8dec, videoconvert, queue2, capsfilter2, autovideosink, NULL)) { g_printerr("Could not link all elements. Exiting.\n"); return; } g_object_set(udpsrc, "address", representIP(ip_address), "port", video_port, NULL); + + window = new QWidget(); + window->resize(1024, 600); + window->show(); + WId xwinid = window->winId(); + gst_video_overlay_set_window_handle(GST_VIDEO_OVERLAY(autovideosink), xwinid); + window->installEventFilter(this); } void Reciver::addLinkAudio() { GstElement *udpsrc, *depay, *parse, *decode, *convert, *autovideosink, *audioresample, *capsfilter, *queue; GstCaps* caps; - if (data.pipeline == NULL) { - data.pipeline = gst_pipeline_new("pipeline"); + if (customData.pipeline == NULL) { + customData.pipeline = gst_pipeline_new("audio"); } udpsrc = gst_element_factory_make("udpsrc", "udpsrcAudio"); queue = gst_element_factory_make("queue", "bufferAudio"); @@ -59,8 +71,8 @@ void Reciver::addLinkAudio() { volume = gst_element_factory_make("volume", "volumeAudio"); autovideosink = gst_element_factory_make("autoaudiosink", "autovideosinkAudio"); - if (!data.pipeline || !udpsrc || !depay || !parse || !decode || !convert || !autovideosink || !audioresample || !capsfilter || !queue || - !volume) { + if (!customData.pipeline || !udpsrc || !depay || !parse || !decode || !convert || !autovideosink || !audioresample || !capsfilter || + !queue || !volume) { g_printerr("Not all elements could be created. Exiting.\n"); return; } @@ -71,10 +83,10 @@ void Reciver::addLinkAudio() { g_object_set(G_OBJECT(capsfilter), "caps", caps, NULL); gst_caps_unref(caps); - // g_object_set(volume, "volume", 0.5, NULL); + g_object_set(volume, "volume", 0.5, NULL); - gst_bin_add_many(GST_BIN(data.pipeline), udpsrc, queue, capsfilter, depay, parse, decode, convert, audioresample, volume, autovideosink, - NULL); + gst_bin_add_many(GST_BIN(customData.pipeline), udpsrc, queue, capsfilter, depay, parse, decode, convert, audioresample, volume, + autovideosink, NULL); if (!gst_element_link_many(udpsrc, queue, capsfilter, depay, parse, decode, convert, audioresample, volume, autovideosink, NULL)) { g_printerr("Could not link all elements. Exiting.\n"); @@ -84,79 +96,109 @@ void Reciver::addLinkAudio() { g_object_set(udpsrc, "address", representIP(ip_address), "port", audio_port, NULL); } -void Reciver::removeVideo() { - gst_element_set_state(data.pipeline, GST_STATE_PAUSED); - gst_bin_get_by_name(GST_BIN(data.pipeline), "udpsrcVideo"); - gst_bin_get_by_name(GST_BIN(data.pipeline), "queue1"); - gst_bin_get_by_name(GST_BIN(data.pipeline), "capsfilterVideo1"); - gst_bin_get_by_name(GST_BIN(data.pipeline), "rtpvp8depay"); - gst_bin_get_by_name(GST_BIN(data.pipeline), "vp8dec"); - gst_bin_get_by_name(GST_BIN(data.pipeline), "videoconvert"); - gst_bin_get_by_name(GST_BIN(data.pipeline), "queue2"); - gst_bin_get_by_name(GST_BIN(data.pipeline), "videosink"); - gst_element_set_state(data.pipeline, GST_STATE_PLAYING); -} - -void Reciver::removeAudio() { - gst_element_set_state(data.pipeline, GST_STATE_PAUSED); - gst_bin_get_by_name(GST_BIN(data.pipeline), "udpsrcAudio"); - gst_bin_get_by_name(GST_BIN(data.pipeline), "bufferAudio"); - gst_bin_get_by_name(GST_BIN(data.pipeline), "capsfilterAudio"); - gst_bin_get_by_name(GST_BIN(data.pipeline), "depayAudio"); - gst_bin_get_by_name(GST_BIN(data.pipeline), "parseAudio"); - gst_bin_get_by_name(GST_BIN(data.pipeline), "decodeAudio"); - gst_bin_get_by_name(GST_BIN(data.pipeline), "convertAudio"); - gst_bin_get_by_name(GST_BIN(data.pipeline), "audioresampleAudio"); - gst_bin_get_by_name(GST_BIN(data.pipeline), "volumeAudio"); - gst_bin_get_by_name(GST_BIN(data.pipeline), "autovideosinkAudio"); - gst_element_set_state(data.pipeline, GST_STATE_PLAYING); -} - void Reciver::startReceive() { - gst_element_set_state(data.pipeline, GST_STATE_NULL); - // gst_element_set_state(data.pipeline, GST_STATE_READY); - gst_element_set_state(data.pipeline, GST_STATE_PLAYING); - - data.loop = g_main_loop_new(NULL, FALSE); + gst_element_set_state(customData.pipeline, GST_STATE_NULL); + gst_element_set_state(customData.pipeline, GST_STATE_PLAYING); + customData.loop = g_main_loop_new(NULL, FALSE); + customData.bus = gst_element_get_bus(customData.pipeline); - data.bus = gst_element_get_bus(data.pipeline); + gst_bus_add_watch(customData.bus, static_cast(busCallback), this); - gst_bus_add_watch(data.bus, static_cast(busCallback), this); + gst_object_unref(customData.bus); - gst_object_unref(data.bus); - - g_main_loop_run(data.loop); + g_main_loop_run(customData.loop); } -Reciver::Reciver(const qint16 video_port, const qint16 audio_port) : Session(video_port, audio_port) { - qDebug() << "constr Reciver VIDEO" << this; +Reciver::Reciver(const QHostAddress& ip_address_, const qint16 video_port, const qint16 audio_port) + : Session(ip_address_, video_port, audio_port) { + qDebug() << "Constructor Reciver" << this; + if (!g_thread_supported()) { + g_thread_init(NULL); + } } -Reciver::Reciver(const qint16 audio_port) : Session(audio_port) { - qDebug() << "constr Reciver AUDIO" << this; +Reciver::Reciver(const QHostAddress& ip_address_, const qint16 audio_port) : Session(ip_address_, audio_port) { + qDebug() << "Constructor Reciver" << this; + if (!g_thread_supported()) { + g_thread_init(NULL); + } } Reciver::~Reciver() { - qDebug() << "Destruct Receiver" + qDebug() << "Destructor Receiver" << "(" << this << ")"; onKillSession(); + if (window != nullptr){ + delete window; + } +} + +bool Reciver::eventFilter(QObject* obj, QEvent* event) { + if (event->type() == QEvent::Close) { + QString pipelineName = gst_element_get_name(customData.pipeline); + emit closeWindow(ip_address, pipelineName); + return true; + } + + return QObject::eventFilter(obj, event); +} + +void Reciver::onEbableCamera() { + qDebug() << "onEbableCamera Reciver"; + gst_init(nullptr, nullptr); + + GstElement *source, *queue1, *udpsrc, *depay, *decoder, *queue2, *sink; + GstCaps *caps, *caps2; + if (customData.pipeline == NULL) { + customData.pipeline = gst_pipeline_new("camera"); + } + + source = gst_element_factory_make("udpsrc", "udp-source"); + queue1 = gst_element_factory_make("queue", "queue1"); + depay = gst_element_factory_make("rtpvp8depay", "rtpvp8-depay"); + decoder = gst_element_factory_make("vp8dec", "vp8-decoder"); + queue2 = gst_element_factory_make("queue", "queue3"); + sink = gst_element_factory_make("glimagesink", "video-sink"); + + if (!customData.pipeline || !source || !queue1 || !depay || !decoder || !queue2 || !sink) { + g_printerr("Not all elements could be created. Exiting.\n"); + return; + } + + g_object_set(G_OBJECT(source), "caps", + gst_caps_new_simple("application/x-rtp", "encoding-name", G_TYPE_STRING, "VP8", "payload", G_TYPE_INT, 96, NULL), NULL); + + gst_bin_add_many(GST_BIN(customData.pipeline), source, queue1, depay, decoder, queue2, sink, NULL); + + if (!gst_element_link_many(source, queue1, depay, decoder, queue2, sink, NULL)) { + g_printerr("Could not link all elements. Reciver. Exiting.\n"); + return; + } + g_object_set(source, "port", 5001, NULL); + window = new QWidget(); + window->resize(1024, 600); + window->show(); + WId xwinid = window->winId(); + gst_video_overlay_set_window_handle(GST_VIDEO_OVERLAY(sink), xwinid); + window->installEventFilter(this); + + startReceive(); } void Reciver::onSetVolume(float volume_) { g_object_set(volume, "volume", volume_, NULL); } -void Reciver::onSetBitrate(const int bitrate) {} - void Reciver::onEnableVideo() { - if (!data.pipeline) { + qDebug() << "onEnableVideo RECIVER"; + if (!customData.pipeline) { gst_init(nullptr, nullptr); addLinkVideo(); startReceive(); } else { - gst_object_unref(data.bus); - gst_object_unref(data.loop); - gst_element_set_state(data.pipeline, GST_STATE_PAUSED); + gst_object_unref(customData.bus); + gst_object_unref(customData.loop); + gst_element_set_state(customData.pipeline, GST_STATE_PAUSED); addLinkVideo(); startReceive(); @@ -164,41 +206,24 @@ void Reciver::onEnableVideo() { } void Reciver::onEnableAudio() { - if (!data.pipeline) { + if (!customData.pipeline) { gst_init(nullptr, nullptr); addLinkAudio(); startReceive(); } else { - gst_object_unref(data.bus); - gst_object_unref(data.loop); - gst_element_set_state(data.pipeline, GST_STATE_PAUSED); + gst_object_unref(customData.bus); + gst_object_unref(customData.loop); + gst_element_set_state(customData.pipeline, GST_STATE_PAUSED); addLinkAudio(); startReceive(); } } -void Reciver::onDisableVideo() { - removeVideo(); -} - -void Reciver::onDisableAudio() { - removeAudio(); -} - void Reciver::onStartSession() { - /*на данный момент ен используется*/ - /* qDebug() << "Starting video receiver"; - gst_init(nullptr, nullptr); - addLinkVideo(); - addLinkAudio(); - startReceive();*/ } void Reciver::onKillSession() { - removeVideo(); - gst_element_set_state(data.pipeline, GST_STATE_NULL); - // if (data.loop) g_main_loop_unref(data.loop); - // if (data.bus) gst_object_unref(data.bus); - // if (data.pipeline) gst_object_unref(data.pipeline); + //removeVideo(); + gst_element_set_state(customData.pipeline, GST_STATE_NULL); } diff --git a/gstreamer_interface/sources/Session.cpp b/gstreamer_interface/sources/Session.cpp index 9e2261a..b148b1e 100644 --- a/gstreamer_interface/sources/Session.cpp +++ b/gstreamer_interface/sources/Session.cpp @@ -50,14 +50,12 @@ gboolean Session::busCallback(GstBus* bus, GstMessage* msg, gpointer data) { break; default: - qDebug() << "1"; + qDebug() << "busCallback message"; break; } return TRUE; } -void Session::onSetBitrate(const int bitrate) {} - const char* Session::representIP(const QHostAddress& ext_ip_address) { char* ip_char_string = nullptr; if (ext_ip_address.protocol() == QAbstractSocket::IPv4Protocol) { diff --git a/gstreamer_interface/sources/SessionManager.cpp b/gstreamer_interface/sources/SessionManager.cpp index e243492..eaf8bd6 100644 --- a/gstreamer_interface/sources/SessionManager.cpp +++ b/gstreamer_interface/sources/SessionManager.cpp @@ -17,59 +17,130 @@ void SessionManager::startThread(Session* session, const QString session_type) { connect(thread, &QThread::started, session, &Session::onEnableVideo); } else if (session_type == "audio") { connect(thread, &QThread::started, session, &Session::onEnableAudio); + } else if (session_type == "camera") { + connect(thread, &QThread::started, session, &Session::onEbableCamera); } connect(thread, &QThread::finished, thread, &QThread::deleteLater); connect(session, &Session::sendSessionKilled, thread, &QThread::quit); connect(session, &Session::sendSessionKilled, session, &Session::deleteLater); - connect(this, &SessionManager::setVolume, session, &Session::onSetVolume); - connect(this, &SessionManager::setBitrate, session, &Session::onSetBitrate); + connect(this, &SessionManager::setVolume, static_cast(session), &Reciver::onSetVolume); + connect(this, &SessionManager::setBitrate, static_cast(session), &Transmiter::onSetBitrate); connect(this, &SessionManager::sendKillAll, session, &Session::deleteLater); + connect(this, &SessionManager::setPorts, this, &SessionManager::onReceivedCameraPorts); + connect(session, &Session::closeWindow, this, &SessionManager::onCloseWindow); + connect(static_cast(session), &Transmiter::closeWindow, this, &SessionManager::sendVideoSessionKilled); thread->start(); } +void SessionManager::killSessionIp4(const QHostAddress ip_address, const QString key) +{ + if (live_sessions.contains(qMakePair(QHostAddress(ip_address.toIPv4Address()), key))) { + auto it = live_sessions[qMakePair(QHostAddress(ip_address.toIPv4Address()), key)]; + QMetaObject::invokeMethod(it.get(), "onKillSession", Qt::QueuedConnection); + live_sessions.remove(qMakePair(QHostAddress(ip_address.toIPv4Address()), key)); + qDebug() << "onKillSession" << ip_address << key; + } + +} + +void SessionManager::killSessionIp6(const QHostAddress ip_address, const QString key) +{ + if (live_sessions.contains(qMakePair(QHostAddress(ip_address.toIPv6Address()), key))) { + auto it = live_sessions[qMakePair(QHostAddress(ip_address.toIPv6Address()), key)]; + QMetaObject::invokeMethod(it.get(), "onKillSession", Qt::QueuedConnection); + live_sessions.remove(qMakePair(QHostAddress(ip_address.toIPv6Address()), key)); + qDebug() << "onKillSession" << QHostAddress(ip_address.toIPv6Address()) << key; + } +} + +//void SessionManager::insertSession(const QHostAddress ip_address, const QString session_type, qint32 video_port, qint32 audio_port) +//{ +// QPair keyTransmiter = qMakePair(QHostAddress(ip_address), QString(session_type)); +// auto itTransmiter = std::make_unique(ip_address, video_port, audio_port); +// startThread(itTransmiter.get(), "video"); +// live_sessions.insert(keyTransmiter, std::move(itTransmiter)); +//} + + void SessionManager::onStartVideoSession(const QHostAddress ip_address) { emit sendStartReciver(ip_address, "video"); +} - onSetVolume(ip_address, 0); +void SessionManager::onStartCameraSession(const QHostAddress ip_address) { + emit sendStartReciver(ip_address, "camera"); } void SessionManager::onStartAudioSession(const QHostAddress ip_address) { emit sendStartReciver(ip_address, "audio"); - // onSetBitrate(ip_address, 800); } void SessionManager::onKillVideoSession(const QHostAddress ip_address) { QString transKey = "TransmiterVideo"; QString recKey = "ReciverVideo"; - if (live_sessions.contains(qMakePair(ip_address, transKey))) { - auto it = live_sessions[qMakePair(ip_address, transKey)]; - QMetaObject::invokeMethod(it.get(), "onKillSession", Qt::QueuedConnection); - live_sessions.remove(qMakePair(ip_address, transKey)); - } - if (live_sessions.contains(qMakePair(ip_address, recKey))) { - auto it = live_sessions[qMakePair(ip_address, recKey)]; - QMetaObject::invokeMethod(it.get(), "onKillSession", Qt::QueuedConnection); - live_sessions.remove(qMakePair(ip_address, recKey)); - } + + killSessionIp4(ip_address, transKey); + killSessionIp6(ip_address, recKey); + onKillCameraSession(ip_address); } void SessionManager::onKillAudioSession(const QHostAddress ip_address) { QString key = "TransmiterAudio"; - if (live_sessions.contains(qMakePair(ip_address, key))) { - auto it = live_sessions[qMakePair(ip_address, key)]; - QMetaObject::invokeMethod(it.get(), "onKillSession", Qt::QueuedConnection); - live_sessions.remove(qMakePair(ip_address, key)); - } + killSessionIp4(ip_address, key); QString recAudioKey = "ReciverAudio"; - if (live_sessions.contains(qMakePair(ip_address, recAudioKey))) { - auto it = live_sessions[qMakePair(ip_address, recAudioKey)]; - QMetaObject::invokeMethod(it.get(), "onKillSession", Qt::QueuedConnection); - live_sessions.remove(qMakePair(ip_address, recAudioKey)); + killSessionIp6(ip_address, recAudioKey); +} + +void SessionManager::onKillCameraSession(const QHostAddress ip_address) { + QString recKeyCam = "ReciverCamera"; + QString transKeyCam = "TransmiterCamera"; + + killSessionIp4(ip_address, recKeyCam); + killSessionIp6(ip_address, recKeyCam); + + killSessionIp4(ip_address, transKeyCam); + killSessionIp6(ip_address, transKeyCam); +} + +void SessionManager::onReceivedPorts(const QHostAddress ip_address, qint32 video_port, qint32 audio_port) { + qDebug() << "Starting transmittion session" << ip_address.toString() << " " << video_port << " " << audio_port; + if (video_port != -1) { + QPair keyTransmiter = qMakePair(QHostAddress(ip_address), QString("TransmiterVideo")); + auto itTransmiter = std::make_unique(ip_address, video_port, audio_port); + qDebug() << "VIDEO_PORT" << video_port; + startThread(itTransmiter.get(), "video"); + live_sessions.insert(keyTransmiter, std::move(itTransmiter)); + return; + } + + if (audio_port != -1) { + QPair key = qMakePair(QHostAddress(ip_address), QString("TransmiterAudio")); + auto it = std::make_unique(ip_address, audio_port); + qDebug() << "AUDIO_PORT" << audio_port; + startThread(it.get(), "audio"); + live_sessions.insert(key, std::move(it)); + return; + } + + if (audio_port == -1 && video_port == -1) { + QPair keyTransmiter = qMakePair(ip_address, QString("TransmiterCamera")); + auto itTransmiter = std::make_unique(ip_address, video_port, -1); + qDebug() << "CAMERA_PORT" << video_port; + startThread(itTransmiter.get(), "camera"); + live_sessions.insert(keyTransmiter, std::move(itTransmiter)); + return; } } +void SessionManager::onReceivedCameraPorts(const QHostAddress ip_address, qint32 video_port) { + QPair keyTransmiter = qMakePair(ip_address, QString("TransmiterCamera")); + auto itTransmiter = std::make_unique(ip_address, video_port, -1); + qDebug() << "CAMERA_PORT" << video_port; + startThread(itTransmiter.get(), "camera"); + live_sessions.insert(keyTransmiter, std::move(itTransmiter)); +} + void SessionManager::onStartReceivingSession(const QHostAddress ip_address, const QString session_type) { qDebug() << "Starting receiving session" << ip_address << " " << session_type; const qint16& video_port = 5228; @@ -77,7 +148,7 @@ void SessionManager::onStartReceivingSession(const QHostAddress ip_address, cons if (session_type == "audio") { QPair key = qMakePair(ip_address, QString("ReciverAudio")); - auto it = std::make_unique(audio_port); + auto it = std::make_unique(ip_address, audio_port); startThread(it.get(), "audio"); live_sessions.insert(key, std::move(it)); emit sendSetPorts(ip_address, -1, audio_port); // use -1 for unused ports @@ -85,68 +156,77 @@ void SessionManager::onStartReceivingSession(const QHostAddress ip_address, cons if (session_type == "video") { QPair key = qMakePair(ip_address, QString("ReciverVideo")); - auto it = std::make_unique(video_port, audio_port); + auto it = std::make_unique(ip_address, video_port, audio_port); startThread(it.get(), "video"); live_sessions.insert(key, std::move(it)); emit sendSetPorts(ip_address, video_port, -1); // use -1 for unused ports } -} -void SessionManager::onKillVideoReciver(const QHostAddress ip_address) { - QString key = "ReciverVideo"; - if (live_sessions.contains(qMakePair(ip_address, key))) { - auto it = live_sessions[qMakePair(ip_address, key)]; - // it->quit(); - live_sessions.remove(qMakePair(ip_address, key)); + if (session_type == "camera") { + QPair key = qMakePair(ip_address, QString("ReciverCamera")); + auto it = std::make_unique(ip_address, video_port); + startThread(it.get(), "camera"); + live_sessions.insert(key, std::move(it)); + emit sendSetPorts(ip_address, -1, -1); // use -1 for unused ports } } -void SessionManager::onKillAudioReciver(const QHostAddress ip_address) { - QString key = "ReciverAudio"; - if (live_sessions.contains(qMakePair(ip_address, key))) { - auto it = live_sessions[qMakePair(ip_address, key)]; - // it->quit(); - live_sessions.remove(qMakePair(ip_address, key)); - } -} +void SessionManager::onCloseWindow(const QHostAddress& ip_address_, const QString session_type) { + qDebug() << "Window closing" << ip_address_ << "session_type: " << session_type; + + if (session_type == "video") { + onKillVideoSession(ip_address_); -void SessionManager::onReceivedPorts(const QHostAddress ip_address, qint32 video_port, qint32 audio_port) { - qDebug() << "Starting transmittion session" << ip_address.toString() << " " << video_port << " " << audio_port; - if (video_port != 65535) { - QString keyReciver = "ReciverVideo"; - QPair keyTransmiter = qMakePair(QHostAddress(ip_address), QString("TransmiterVideo")); - auto itTransmiter = std::make_unique(ip_address, video_port, audio_port); - qDebug() << "VIDEO_PORT"; - startThread(itTransmiter.get(), "video"); - live_sessions.insert(keyTransmiter, std::move(itTransmiter)); - return; } - if (audio_port != 65535) { - QPair key = qMakePair(QHostAddress(ip_address), QString("TransmiterAudio")); - auto it = std::make_unique(ip_address, audio_port); - qDebug() << "AUDIO_PORT"; - startThread(it.get(), "audio"); - live_sessions.insert(key, std::move(it)); - return; + else if (session_type == "camera") { + onKillCameraSession(ip_address_); } } -void SessionManager::onSetVolume(const QHostAddress ip_address, const int volume) { - QString key = "ReciverAudio"; - if (live_sessions.contains(qMakePair(ip_address, key))) { - auto it = live_sessions[qMakePair(ip_address, key)]; - // it->onSetVolume(volume); - // emit setVolume(volume); +void SessionManager::onSetVolume(const QHostAddress ip_address, const int volume_) { + float volume = volume_ / 100.0f; + if (volume == 1) { + volume = 0.99; + } + QString key = "ReciverAudio" ; + + if (live_sessions.contains(qMakePair(QHostAddress(ip_address.toIPv6Address()), key))) { + auto it = live_sessions[qMakePair(QHostAddress(ip_address.toIPv6Address()), key)]; QMetaObject::invokeMethod(it.get(), "onSetVolume", Qt::DirectConnection, Q_ARG(float, volume)); } } void SessionManager::onSetBitrate(const QHostAddress ip_address, const int bitrate) { QString key = "TransmiterVideo"; + + qDebug() << "Set bitrate" << bitrate; if (live_sessions.contains(qMakePair(ip_address, key))) { auto it = live_sessions[qMakePair(ip_address, key)]; - emit setBitrate(bitrate); - // QMetaObject::invokeMethod(it.get(), "onSetVolume", Qt::QueuedConnection, a); - // it->onSetBitrate(bitrate); + QMetaObject::invokeMethod(it.get(), "onSetBitrate", Qt::DirectConnection, Q_ARG(int, bitrate)); } } + +void SessionManager::setCameraCaptureMode(const QHostAddress ip_address) +{ + qDebug() << "Set camera capture mode"; + + if (live_sessions.contains(qMakePair(QHostAddress(ip_address.toIPv6Address()), QString("ReciverCamera"))) || live_sessions.contains(qMakePair(ip_address, QString("TransmiterCamera")))){ + onKillCameraSession(ip_address); + onKillVideoSession(ip_address); + + emit sendStartReciver(ip_address, "video"); + } + +} + +void SessionManager::setScreenCaptureMode(const QHostAddress ip_address) +{ + qDebug() << "Set screen capture mode"; + + if (live_sessions.contains(qMakePair(QHostAddress(ip_address.toIPv6Address()), QString("ReciverVideo"))) || live_sessions.contains(qMakePair(ip_address, QString("TransmiterVideo")))){ + onKillVideoSession(ip_address); + onKillCameraSession(ip_address); + emit sendStartReciver(ip_address, "camera"); + } + +} diff --git a/gstreamer_interface/sources/Transmiter.cpp b/gstreamer_interface/sources/Transmiter.cpp index 121d244..2f77cdd 100644 --- a/gstreamer_interface/sources/Transmiter.cpp +++ b/gstreamer_interface/sources/Transmiter.cpp @@ -3,10 +3,10 @@ void Transmiter::addLinkVideo() { GstElement *ximagesrc, *queue1, *capsfilter1, *capsfilter2, *videoscale, *videoconvert1, *videoconvert2, *rtpvp8pay, *queue2, *udpsink1; GstCaps *caps1, *caps2; - if (data.pipeline == NULL) { - data.pipeline = gst_pipeline_new("pipeline"); + if (customData.pipeline == NULL) { + customData.pipeline = gst_pipeline_new("video"); } - data.pipeline = gst_pipeline_new("my-pipeline"); + //customData.pipeline = gst_pipeline_new("my-pipeline"); ximagesrc = gst_element_factory_make("ximagesrc", "ximagesrc"); // queue1 = gst_element_factory_make("queue", "queue1"); // capsfilter1 = gst_element_factory_make("capsfilter", "capsfilterVideo1"); @@ -21,37 +21,27 @@ void Transmiter::addLinkVideo() { queue2 = gst_element_factory_make("queue", "queue2"); udpsink1 = gst_element_factory_make("udpsink", "udpsink1"); - if (!data.pipeline || !ximagesrc || !vp8enc || !videoconvert1 || !videoscale || !videoconvert2 || !queue2 || !queue1 || !rtpvp8pay || - !udpsink1) { - g_printerr("Not all elements could be created\n"); + if (!customData.pipeline || !ximagesrc || !vp8enc || !videoconvert1 || !videoscale || !videoconvert2 || !queue2 || !queue1 || + !rtpvp8pay || !udpsink1 || !capsfilter1) { + g_printerr("Not all elements could be created Transmiter\n"); return; } - - // caps1 = gst_caps_new_simple("video/x-raw", "framerate", GST_TYPE_FRACTION, 30, 1, NULL); - caps1 = gst_caps_new_simple("video/x-raw", "profile", G_TYPE_STRING, "main", "width", G_TYPE_INT, 1024, "height", G_TYPE_INT, 600, NULL); - // g_object_set(G_OBJECT(vp8enc), "width", 1024, "height", 600, NULL); - // g_object_set(vp8enc, "min-quantizer", 10, NULL); - g_object_set(G_OBJECT(capsfilter1), "caps", caps1, NULL); - // g_object_set(G_OBJECT(capsfilter2), "caps", caps2, NULL); - // g_object_set(videoscale, "add-borders", TRUE, 0x00000000, "width", 1024, "height", 600, NULL); gst_caps_unref(caps1); - // - // gst_caps_unref(caps2); - gst_bin_add_many(GST_BIN(data.pipeline), ximagesrc, videoconvert1, videoscale, capsfilter1, videoconvert2, queue1, vp8enc, rtpvp8pay, - queue2, udpsink1, NULL); + gst_bin_add_many(GST_BIN(customData.pipeline), ximagesrc, videoconvert1, videoscale, capsfilter1, videoconvert2, queue1, vp8enc, + rtpvp8pay, queue2, udpsink1, NULL); if (gst_element_link_many(ximagesrc, videoconvert1, videoscale, capsfilter1, videoconvert2, queue1, vp8enc, rtpvp8pay, queue2, udpsink1, NULL) != TRUE) { g_printerr( "Failed to link elements: ximagesrc -> videoscale -> videoconvert -> x264enc -> " "rtph264pay -> udpsink1\n"); - gst_object_unref(data.pipeline); + gst_object_unref(customData.pipeline); return; } @@ -62,8 +52,8 @@ void Transmiter::addLinkVideo() { void Transmiter::addLinkAudio() { GstElement *alsasrc, *audioconvert, *audioresample, *opusenc, *rtpopuspay, *udpsink2; - if (data.pipeline == NULL) { - data.pipeline = gst_pipeline_new("pipeline"); + if (customData.pipeline == NULL) { + customData.pipeline = gst_pipeline_new("audio"); } alsasrc = gst_element_factory_make("alsasrc", "alsasrc"); @@ -73,85 +63,101 @@ void Transmiter::addLinkAudio() { rtpopuspay = gst_element_factory_make("rtpopuspay", "rtpopuspay"); udpsink2 = gst_element_factory_make("udpsink", "udpsink2"); - if (!data.pipeline || !alsasrc || !audioconvert || !audioresample || !opusenc || !rtpopuspay || !udpsink2) { - g_printerr("Not all elements could be created\n"); + if (!customData.pipeline || !alsasrc || !audioconvert || !audioresample || !opusenc || !rtpopuspay || !udpsink2) { + g_printerr("Not all elements could be created.Transmiter\n"); return; } - gst_bin_add_many(GST_BIN(data.pipeline), alsasrc, audioconvert, audioresample, opusenc, rtpopuspay, udpsink2, NULL); + gst_bin_add_many(GST_BIN(customData.pipeline), alsasrc, audioconvert, audioresample, opusenc, rtpopuspay, udpsink2, NULL); if (gst_element_link_many(alsasrc, audioconvert, audioresample, opusenc, rtpopuspay, udpsink2, NULL) != TRUE) { g_printerr( "Failed to link elements: ximagesrc -> videoscale -> videoconvert -> x264enc -> " "rtph264pay -> udpsink1\n"); - gst_object_unref(data.pipeline); + gst_object_unref(customData.pipeline); return; } g_object_set(udpsink2, "sync", FALSE, "host", representIP(ip_address), "port", audio_port, NULL); } -void Transmiter::removeVideo() { - gst_element_set_state(data.pipeline, GST_STATE_PAUSED); - gst_bin_get_by_name(GST_BIN(data.pipeline), "ximagesrc"); - gst_bin_get_by_name(GST_BIN(data.pipeline), "videoconvert1"); - gst_bin_get_by_name(GST_BIN(data.pipeline), "videoscale"); - gst_bin_get_by_name(GST_BIN(data.pipeline), "capsfilterVideo1"); - gst_bin_get_by_name(GST_BIN(data.pipeline), "videoconvert2"); - gst_bin_get_by_name(GST_BIN(data.pipeline), "queue1"); - gst_bin_get_by_name(GST_BIN(data.pipeline), "vp8enc"); - gst_bin_get_by_name(GST_BIN(data.pipeline), "rtpvp8pay"); - gst_bin_get_by_name(GST_BIN(data.pipeline), "queue2"); - gst_bin_get_by_name(GST_BIN(data.pipeline), "udpsink1"); - gst_element_set_state(data.pipeline, GST_STATE_PLAYING); -} - -void Transmiter::removeAudio() { - gst_element_set_state(data.pipeline, GST_STATE_PAUSED); - gst_bin_get_by_name(GST_BIN(data.pipeline), "alsasrc"); - gst_bin_get_by_name(GST_BIN(data.pipeline), "audioconvert"); - gst_bin_get_by_name(GST_BIN(data.pipeline), "audioresample"); - gst_bin_get_by_name(GST_BIN(data.pipeline), "opusenc"); - gst_bin_get_by_name(GST_BIN(data.pipeline), "rtpopuspay"); - gst_bin_get_by_name(GST_BIN(data.pipeline), "udpsink2"); - gst_element_set_state(data.pipeline, GST_STATE_PLAYING); -} - void Transmiter::startSend() { - data.bus = gst_element_get_bus(data.pipeline); + customData.loop = g_main_loop_new(NULL, FALSE); + customData.bus = gst_element_get_bus(customData.pipeline); // qDebug() << data.pipeline; - gst_bus_add_watch(data.bus, static_cast(busCallback), this); + gst_bus_add_watch(customData.bus, static_cast(busCallback), this); - gst_element_set_state(data.pipeline, GST_STATE_PLAYING); - gst_bus_timed_pop_filtered(data.bus, GST_CLOCK_TIME_NONE, static_cast(GST_MESSAGE_ERROR | GST_MESSAGE_EOS)); + // gst_element_set_state(data.pipeline, GST_STATE_NULL); + gst_element_set_state(customData.pipeline, GST_STATE_PLAYING); + gst_bus_timed_pop_filtered(customData.bus, GST_CLOCK_TIME_NONE, static_cast(GST_MESSAGE_ERROR | GST_MESSAGE_EOS)); } Transmiter::Transmiter(const QHostAddress& ip_address_, const qint32 video_port_, const qint32 audio_port_) : Session(ip_address_, video_port_, audio_port_) { - qDebug() << "Constructor TransmiterVideo" << this; + qDebug() << "Constructor Transmiter" << this; } Transmiter::Transmiter(const QHostAddress& ip_address_, const qint32 audio_port_) : Session(ip_address_, audio_port_) { - qDebug() << "Constructor TransmiterAudio" << this; + qDebug() << "Constructor Transmiter" << this; } Transmiter::~Transmiter() { - qDebug() << "Transmiter Destructor" + qDebug() << "Destructor Transmiter" << "(" << this << ")"; + QString pipelineName = gst_element_get_name(customData.pipeline); + emit closeWindow(ip_address, pipelineName); onKillSession(); - // gst_element_set_state(data.pipeline, GST_STATE_NULL); +} + +void Transmiter::onEbableCamera() { + qDebug() << "onEbableCamera Transmiter "; + GstElement *v4l2src, *videoconvert, *videoscale, *vp8enc, *rtpvp8pay, *udpsink; + + gst_init(nullptr, nullptr); + if (customData.pipeline == NULL) { + customData.pipeline = gst_pipeline_new("camera"); + } + + v4l2src = gst_element_factory_make("v4l2src", "v4l2src"); + videoconvert = gst_element_factory_make("videoconvert", "videoconvert"); + videoscale = gst_element_factory_make("videoscale", "videoscale"); + // capsfilter = gst_element_factory_make("capsfilter", "capsfilterVideo1"); + vp8enc = gst_element_factory_make("vp8enc", "vp8enc"); + rtpvp8pay = gst_element_factory_make("rtpvp8pay", "rtpvp8pay"); + udpsink = gst_element_factory_make("udpsink", "udpsink1"); + if (!customData.pipeline || !v4l2src || !vp8enc || !videoconvert || !videoscale || !rtpvp8pay || !udpsink) { + g_printerr("Not all elements could be created Transmiter\n"); + return; + } + + g_object_set(v4l2src, "device", "/dev/video0", NULL); + + g_object_set(udpsink, "sync", FALSE, "host", representIP(ip_address), "port", 5001, NULL); + + gst_bin_add_many(GST_BIN(customData.pipeline), v4l2src, videoconvert, videoscale, vp8enc, rtpvp8pay, udpsink, NULL); + + if (gst_element_link_many(v4l2src, videoconvert, videoscale, vp8enc, rtpvp8pay, udpsink, NULL) != TRUE) { + g_printerr( + "Failed to link elements: v4l2src -> videoscale -> videoconvert -> x264enc -> " + "rtph264pay -> udpsink1\n"); + gst_object_unref(customData.pipeline); + return; + } + + startSend(); } void Transmiter::onEnableVideo() { - if (!data.pipeline) { + qDebug() << "onEnableVideo TRANSMITER"; + if (!customData.pipeline) { gst_init(nullptr, nullptr); addLinkVideo(); startSend(); } else { - gst_object_unref(data.bus); - gst_object_unref(data.loop); - gst_element_set_state(data.pipeline, GST_STATE_PAUSED); + gst_object_unref(customData.bus); + gst_object_unref(customData.loop); + gst_element_set_state(customData.pipeline, GST_STATE_PAUSED); addLinkVideo(); startSend(); @@ -159,53 +165,30 @@ void Transmiter::onEnableVideo() { } void Transmiter::onEnableAudio() { - if (!data.pipeline) { + if (!customData.pipeline) { gst_init(nullptr, nullptr); addLinkAudio(); startSend(); } else { - gst_object_unref(data.bus); - gst_object_unref(data.loop); - gst_element_set_state(data.pipeline, GST_STATE_PAUSED); + gst_object_unref(customData.bus); + gst_object_unref(customData.loop); + gst_element_set_state(customData.pipeline, GST_STATE_PAUSED); addLinkAudio(); startSend(); } } -void Transmiter::onDisableVideo() { - removeVideo(); -} - -void Transmiter::onDisableAudio() { - removeAudio(); -} - -void Transmiter::onStartSession() { - /*на данный момент не используемая функция*/ - /*qDebug() << "Starting video transmition"; - - gst_init(nullptr, nullptr); - addLinkVideo(); - addLinkAudio(); - startSend();*/ -} +void Transmiter::onStartSession() {} void Transmiter::onKillSession() { - gst_element_set_state(data.pipeline, GST_STATE_NULL); - // if (data.loop) g_main_loop_unref(data.loop); - // if (data.bus) gst_object_unref(data.bus); - // if (data.pipeline) gst_object_unref(data.pipeline); + gst_element_set_state(customData.pipeline, GST_STATE_NULL); } -void Transmiter::onSetVolume(float volume) {} - void Transmiter::onSetBitrate(const int bitrate) { qDebug() << "set bitrate"; - gst_element_set_state(data.pipeline, GST_STATE_PAUSED); + gst_element_set_state(customData.pipeline, GST_STATE_PAUSED); g_object_set(G_OBJECT(vp8enc), "target-bitrate", bitrate, NULL); - gst_element_set_state(data.pipeline, GST_STATE_PLAYING); + gst_element_set_state(customData.pipeline, GST_STATE_PLAYING); } - -// void Transmiter::onSetVolume(float volume) {} From 07dadf6c9d05dc8700c254c2ccdc1efd753690c4 Mon Sep 17 00:00:00 2001 From: Gokert Date: Fri, 2 Jun 2023 13:38:44 +0300 Subject: [PATCH 2/2] fix camera --- gstreamer_interface/CMakeLists.txt | 43 +++--- gstreamer_interface/include/Reciver.h | 7 + gstreamer_interface/include/Session.h | 19 ++- gstreamer_interface/include/SessionManager.h | 25 +++- gstreamer_interface/include/Transmiter.h | 12 +- gstreamer_interface/sources/Reciver.cpp | 139 ++++++++++++++++-- .../sources/SessionManager.cpp | 103 ++++++++----- gstreamer_interface/sources/Transmiter.cpp | 78 ++++++---- 8 files changed, 331 insertions(+), 95 deletions(-) diff --git a/gstreamer_interface/CMakeLists.txt b/gstreamer_interface/CMakeLists.txt index 53a7a85..9a0fee0 100644 --- a/gstreamer_interface/CMakeLists.txt +++ b/gstreamer_interface/CMakeLists.txt @@ -1,11 +1,15 @@ project(gstreamer_interface VERSION 0.1 LANGUAGES CXX) find_package(Qt${QT_VERSION} REQUIRED COMPONENTS + MultimediaWidgets + Multimedia Widgets - Core - Network + Core + Network + ) + file(GLOB SRC_FILES ${CMAKE_CURRENT_LIST_DIR}/sources/*.cpp ${CMAKE_CURRENT_LIST_DIR}/include/*.h @@ -16,6 +20,9 @@ add_library(${PROJECT_NAME} SHARED ) target_link_libraries(${PROJECT_NAME} PRIVATE + Qt${QT_VERSION}::MultimediaWidgets + + Qt${QT_VERSION}::Multimedia Qt${QT_VERSION}::Widgets Qt${QT_VERSION}::Core Qt${QT_VERSION}::Network @@ -63,37 +70,37 @@ else() pkg_check_modules(GST REQUIRED gstreamer-1.0) target_link_libraries(${PROJECT_NAME} PRIVATE - ${GST_LIBRARIES} + ${GST_LIBRARIES} ) message("====${GST_INCLUDE_DIRS}") target_include_directories(${PROJECT_NAME} PUBLIC - ${GST_INCLUDE_DIRS} + ${GST_INCLUDE_DIRS} ) endif() if(BUILD_TESTS) - file(GLOB TEST_FILES - ${CMAKE_CURRENT_LIST_DIR}/tests/*.cpp - ) + file(GLOB TEST_FILES + ${CMAKE_CURRENT_LIST_DIR}/tests/*.cpp + ) find_package(GTest REQUIRED) - add_executable(gstreamer_interface_tests - ${TEST_FILES} - ) + add_executable(gstreamer_interface_tests + ${TEST_FILES} + ) target_link_libraries(gstreamer_interface_tests - GTest::Main - ${PROJECT_NAME} - Qt${QT_VERSION}::Core - Qt${QT_VERSION}::Network - general_use - ${GSTREAMER_LIBRARIES} - ) + GTest::Main + ${PROJECT_NAME} + Qt${QT_VERSION}::Core + Qt${QT_VERSION}::Network + general_use + ${GSTREAMER_LIBRARIES} + ) enable_testing() - add_test(NAME unit_tests COMMAND tests) + add_test(NAME unit_tests COMMAND tests) endif() diff --git a/gstreamer_interface/include/Reciver.h b/gstreamer_interface/include/Reciver.h index ca0aa4b..6fdfab4 100644 --- a/gstreamer_interface/include/Reciver.h +++ b/gstreamer_interface/include/Reciver.h @@ -14,7 +14,10 @@ class Reciver : public Session { private: Q_OBJECT + QWidget* windowCamera; QWidget* window; + QWidget* full; + GstElement* volume; void addLinkVideo(); @@ -36,6 +39,10 @@ class Reciver : public Session { ~Reciver(); public slots: + void onStartCameraRecording(WId id, QWidget* window1) override; + + void onGetCameraWidget(QWidget* camera); + void onEbableCamera() override; void onEnableVideo() override; diff --git a/gstreamer_interface/include/Session.h b/gstreamer_interface/include/Session.h index fbecf3a..93cc936 100644 --- a/gstreamer_interface/include/Session.h +++ b/gstreamer_interface/include/Session.h @@ -12,6 +12,21 @@ #include #include +#include +#include +#include +#include +#include + +#include + +#include +#include +#include + + + + class Session : public QObject { private: Q_OBJECT @@ -36,8 +51,6 @@ class Session : public QObject { QHostAddress ip_address; qint32 video_port; qint32 audio_port; - GstElement* volume; - GstElement* vp8enc; public: const static char* representIP(const QHostAddress& ext_ip_address); @@ -45,6 +58,8 @@ class Session : public QObject { ~Session(); public slots: + virtual void onStartCameraRecording(WId id, QWidget* window1) = 0; + virtual void onEbableCamera() = 0; virtual void onEnableVideo() = 0; diff --git a/gstreamer_interface/include/SessionManager.h b/gstreamer_interface/include/SessionManager.h index caa5dcf..37cbbbf 100644 --- a/gstreamer_interface/include/SessionManager.h +++ b/gstreamer_interface/include/SessionManager.h @@ -8,11 +8,28 @@ #include #include +#include +#include +#include +#include +#include + +#include + +#include + +#include + + class SessionManager : public QObject { private: Q_OBJECT - QHash, std::shared_ptr> live_sessions; // map - // void handleException(GstreamerError error); // Provides flowless application work after gstreamer errors. + QHash, Session*> live_sessions; + QMediaCaptureSession *captureSession; + QWidget* window1; + QCamera* camera; + + void startThread(Session* session, const QString session_type); @@ -26,6 +43,8 @@ class SessionManager : public QObject { ~SessionManager(); public slots: + WId onStartCameraRecording(); + void onStartVideoSession(const QHostAddress ip_address); void onStartCameraSession(const QHostAddress ip_address); @@ -82,4 +101,6 @@ public slots: void sendStartReciver(const QHostAddress local_ip6, const QString session_type); void sendSetPorts(const QHostAddress ip_address, const qint32 video_port, const qint32 audio_port); + + void sendCameraRecording(WId id); }; diff --git a/gstreamer_interface/include/Transmiter.h b/gstreamer_interface/include/Transmiter.h index f26f268..4211eb3 100644 --- a/gstreamer_interface/include/Transmiter.h +++ b/gstreamer_interface/include/Transmiter.h @@ -13,6 +13,14 @@ class Transmiter : public Session { private: Q_OBJECT + bool running; + GstElement* vp8enc; + WId id; + + //QWidget* window; + + bool isRunning(); + void addLinkVideo(); void addLinkAudio(); @@ -30,6 +38,8 @@ class Transmiter : public Session { ~Transmiter(); public slots: + void onStartCameraRecording(WId id, QWidget* window1) override; + void onEbableCamera() override; void onEnableVideo() override; @@ -43,5 +53,5 @@ public slots: void onSetBitrate(const int bitrate); signals: - + //void getWidget(); }; diff --git a/gstreamer_interface/sources/Reciver.cpp b/gstreamer_interface/sources/Reciver.cpp index ef99b37..0f1bbc5 100644 --- a/gstreamer_interface/sources/Reciver.cpp +++ b/gstreamer_interface/sources/Reciver.cpp @@ -1,6 +1,9 @@ #include "Reciver.h" +#include "gst/video/videooverlay.h" +#include "qboxlayout.h" #include "qevent.h" +#include "qvideowidget.h" void Reciver::addLinkVideo() { qDebug() << "onEnableVideo RECIVER"; @@ -44,14 +47,49 @@ void Reciver::addLinkVideo() { return; } - g_object_set(udpsrc, "address", representIP(ip_address), "port", video_port, NULL); + g_object_set(udpsrc, "port", video_port, NULL); window = new QWidget(); window->resize(1024, 600); window->show(); WId xwinid = window->winId(); - gst_video_overlay_set_window_handle(GST_VIDEO_OVERLAY(autovideosink), xwinid); - window->installEventFilter(this); + + if (windowCamera != nullptr){ + qDebug() << "\n 01ddddAAAA1\n\n" ; + //full = new ; + full = new QWidget(window); ; + + gst_video_overlay_set_window_handle(GST_VIDEO_OVERLAY(autovideosink), xwinid); + // window->installEventFilter(this); + //QHBoxLayout* layout = new QHBoxLayout(); + QGridLayout* layout = new QGridLayout(); + layout->addWidget(window, 0,0,2,2); + layout->addWidget(windowCamera,1,1,1,1); + + + + full->setLayout(layout); + full->installEventFilter(this); + + + } + else { + qDebug() << "\n 11NOOO11\n\n" ; + gst_video_overlay_set_window_handle(GST_VIDEO_OVERLAY(autovideosink), xwinid); + window->installEventFilter(this); + } + + //gst_video_overlay_set_window_handle(GST_VIDEO_OVERLAY(autovideosink), xwinid); + //window->installEventFilter(this); + + + //QWidget* parentWidget = new QWidget(); + //QWidget* childWidget = new QWidget(parentWidget); + + //QVBoxLayout* layout = new QVBoxLayout(parentWidget); + //layout->addWidget(childWidget); + + //parentWidget->show(); } void Reciver::addLinkAudio() { @@ -112,6 +150,8 @@ void Reciver::startReceive() { Reciver::Reciver(const QHostAddress& ip_address_, const qint16 video_port, const qint16 audio_port) : Session(ip_address_, video_port, audio_port) { qDebug() << "Constructor Reciver" << this; + window = nullptr; + windowCamera = nullptr; if (!g_thread_supported()) { g_thread_init(NULL); } @@ -119,6 +159,8 @@ Reciver::Reciver(const QHostAddress& ip_address_, const qint16 video_port, const Reciver::Reciver(const QHostAddress& ip_address_, const qint16 audio_port) : Session(ip_address_, audio_port) { qDebug() << "Constructor Reciver" << this; + window = nullptr; + windowCamera = nullptr; if (!g_thread_supported()) { g_thread_init(NULL); } @@ -130,9 +172,24 @@ Reciver::~Reciver() { onKillSession(); if (window != nullptr){ delete window; + window=nullptr; + } + if (windowCamera!=nullptr){ + delete windowCamera; + windowCamera = nullptr; } } +void Reciver::onStartCameraRecording(WId id, QWidget* window1) +{ + // windowCamera = window1; +} + +void Reciver::onGetCameraWidget(QWidget* camera_) +{ + // windowCamera = camera_; +} + bool Reciver::eventFilter(QObject* obj, QEvent* event) { if (event->type() == QEvent::Close) { QString pipelineName = gst_element_get_name(customData.pipeline); @@ -144,9 +201,11 @@ bool Reciver::eventFilter(QObject* obj, QEvent* event) { } void Reciver::onEbableCamera() { - qDebug() << "onEbableCamera Reciver"; + qDebug() << "\n\nonEbableCamera Reciver\n"; gst_init(nullptr, nullptr); + + GstElement *source, *queue1, *udpsrc, *depay, *decoder, *queue2, *sink; GstCaps *caps, *caps2; if (customData.pipeline == NULL) { @@ -174,15 +233,75 @@ void Reciver::onEbableCamera() { g_printerr("Could not link all elements. Reciver. Exiting.\n"); return; } - g_object_set(source, "port", 5001, NULL); - window = new QWidget(); - window->resize(1024, 600); - window->show(); - WId xwinid = window->winId(); + + g_object_set(source, "port", 5004, NULL); + windowCamera = new QWidget(); + windowCamera->resize(640, 480); + windowCamera->show(); + WId xwinid = windowCamera->winId(); gst_video_overlay_set_window_handle(GST_VIDEO_OVERLAY(sink), xwinid); - window->installEventFilter(this); + windowCamera->installEventFilter(this); startReceive(); + + +// GstElement *capsfilter1, *capsfilter2, *udpsrc, *queue1, *vp8dec, *rtpvp8depay, *videoconvert, *queue2, *autovideosink; +// GstCaps *caps, *caps2; +// if (customData.pipeline == NULL) { +// customData.pipeline = gst_pipeline_new("video"); +// } + +// udpsrc = gst_element_factory_make("udpsrc", "udpsrc"); +// queue1 = gst_element_factory_make("queue", "queue1"); +// capsfilter1 = gst_element_factory_make("capsfilter", "capsfilterVideo1"); +// rtpvp8depay = gst_element_factory_make("rtpvp8depay", "rtpvp8depay"); +// vp8dec = gst_element_factory_make("vp8dec", "vp8dec"); +// videoconvert = gst_element_factory_make("videoconvert", "videoconvert"); +// capsfilter2 = gst_element_factory_make("capsfilter", "capsfilterVideo2"); +// queue2 = gst_element_factory_make("queue", "queue2"); +// autovideosink = gst_element_factory_make("glimagesink", "videosink"); + +// if (!customData.pipeline || !udpsrc || !rtpvp8depay || !vp8dec || !videoconvert || !autovideosink || !queue1 || !queue2 || +// !capsfilter1 || !capsfilter2) { +// g_printerr("Not all elements could be created. Exiting.\n"); +// return; +// } + +// caps = gst_caps_new_simple("application/x-rtp", "media", G_TYPE_STRING, "video", "clock-rate", G_TYPE_INT, 90000, "encoding-name", +// G_TYPE_STRING, "VP8", "payload", G_TYPE_INT, 96, NULL); + +// caps2 = gst_caps_new_simple("video/x-raw", NULL); + +// g_object_set(G_OBJECT(capsfilter1), "caps", caps, NULL); +// g_object_set(G_OBJECT(capsfilter2), "caps", caps2, NULL); +// gst_caps_unref(caps); +// gst_caps_unref(caps2); + +// gst_bin_add_many(GST_BIN(customData.pipeline), udpsrc, queue1, capsfilter1, rtpvp8depay, vp8dec, videoconvert, queue2, capsfilter2, +// autovideosink, NULL); + +// if (!gst_element_link_many(udpsrc, queue1, capsfilter1, rtpvp8depay, vp8dec, videoconvert, queue2, capsfilter2, autovideosink, NULL)) { +// g_printerr("Could not link all elements. Exiting.\n"); +// return; +// } + +// g_object_set(udpsrc, "port", 5004, NULL); + +// window = new QWidget(); +// window->resize(1024, 600); +// window->show(); +// WId xwinid = window->winId(); +// gst_video_overlay_set_window_handle(GST_VIDEO_OVERLAY(autovideosink), xwinid); +// window->installEventFilter(this); + +// windowCamera = new QWidget(); +// //windowCamera->resize(640, 480); +// windowCamera->show(); +// WId xwinid = windowCamera->winId(); +// gst_video_overlay_set_window_handle(GST_VIDEO_OVERLAY(autovideosink), xwinid); +// windowCamera->installEventFilter(this); + + } void Reciver::onSetVolume(float volume_) { diff --git a/gstreamer_interface/sources/SessionManager.cpp b/gstreamer_interface/sources/SessionManager.cpp index eaf8bd6..d026753 100644 --- a/gstreamer_interface/sources/SessionManager.cpp +++ b/gstreamer_interface/sources/SessionManager.cpp @@ -3,14 +3,32 @@ #include #include "qobjectdefs.h" +#include -SessionManager::SessionManager() {} + + +SessionManager::SessionManager() {camera=nullptr;captureSession=nullptr;window1=nullptr;} SessionManager::~SessionManager() { emit sendKillAll(); } -void SessionManager::startThread(Session* session, const QString session_type) { // ensure thread and object deletion later. +WId SessionManager::onStartCameraRecording() +{ + captureSession = new QMediaCaptureSession; + camera = new QCamera; + captureSession->setCamera(camera); + window1 = new QVideoWidget; + captureSession->setVideoOutput(window1); + + camera->start(); + qDebug() << "WINID" << window1->winId(); + + window1->show(); + return window1->winId(); +} + +void SessionManager::startThread(Session* session, const QString session_type) { QThread* thread = new QThread(); session->moveToThread(thread); if (session_type == "video") { @@ -28,6 +46,9 @@ void SessionManager::startThread(Session* session, const QString session_type) { connect(this, &SessionManager::sendKillAll, session, &Session::deleteLater); connect(this, &SessionManager::setPorts, this, &SessionManager::onReceivedCameraPorts); connect(session, &Session::closeWindow, this, &SessionManager::onCloseWindow); + + // connect(this, &SessionManager::sendCameraRecording, session, &Session::onStartCameraRecording, Qt::QueuedConnection); + connect(static_cast(session), &Transmiter::closeWindow, this, &SessionManager::sendVideoSessionKilled); thread->start(); @@ -37,8 +58,9 @@ void SessionManager::killSessionIp4(const QHostAddress ip_address, const QString { if (live_sessions.contains(qMakePair(QHostAddress(ip_address.toIPv4Address()), key))) { auto it = live_sessions[qMakePair(QHostAddress(ip_address.toIPv4Address()), key)]; - QMetaObject::invokeMethod(it.get(), "onKillSession", Qt::QueuedConnection); + QMetaObject::invokeMethod(it, "onKillSession", Qt::QueuedConnection); live_sessions.remove(qMakePair(QHostAddress(ip_address.toIPv4Address()), key)); + delete it; qDebug() << "onKillSession" << ip_address << key; } @@ -48,23 +70,15 @@ void SessionManager::killSessionIp6(const QHostAddress ip_address, const QString { if (live_sessions.contains(qMakePair(QHostAddress(ip_address.toIPv6Address()), key))) { auto it = live_sessions[qMakePair(QHostAddress(ip_address.toIPv6Address()), key)]; - QMetaObject::invokeMethod(it.get(), "onKillSession", Qt::QueuedConnection); + QMetaObject::invokeMethod(it, "onKillSession", Qt::QueuedConnection); live_sessions.remove(qMakePair(QHostAddress(ip_address.toIPv6Address()), key)); + delete it; qDebug() << "onKillSession" << QHostAddress(ip_address.toIPv6Address()) << key; } } -//void SessionManager::insertSession(const QHostAddress ip_address, const QString session_type, qint32 video_port, qint32 audio_port) -//{ -// QPair keyTransmiter = qMakePair(QHostAddress(ip_address), QString(session_type)); -// auto itTransmiter = std::make_unique(ip_address, video_port, audio_port); -// startThread(itTransmiter.get(), "video"); -// live_sessions.insert(keyTransmiter, std::move(itTransmiter)); -//} - - void SessionManager::onStartVideoSession(const QHostAddress ip_address) { - emit sendStartReciver(ip_address, "video"); + emit sendStartReciver(ip_address, "video"); } void SessionManager::onStartCameraSession(const QHostAddress ip_address) { @@ -95,39 +109,55 @@ void SessionManager::onKillAudioSession(const QHostAddress ip_address) { void SessionManager::onKillCameraSession(const QHostAddress ip_address) { QString recKeyCam = "ReciverCamera"; QString transKeyCam = "TransmiterCamera"; + QString transKey = "TransmiterVideo"; + QString recKey = "ReciverVideo"; + killSessionIp4(ip_address, recKeyCam); killSessionIp6(ip_address, recKeyCam); - + killSessionIp4(ip_address, transKey); + killSessionIp6(ip_address, recKey); killSessionIp4(ip_address, transKeyCam); killSessionIp6(ip_address, transKeyCam); + + if (camera){ camera->stop(); camera->deleteLater(); camera=nullptr;} + if (window1){ window1->close(); window1=nullptr;} + if (captureSession){captureSession->deleteLater(); captureSession=nullptr;} } void SessionManager::onReceivedPorts(const QHostAddress ip_address, qint32 video_port, qint32 audio_port) { qDebug() << "Starting transmittion session" << ip_address.toString() << " " << video_port << " " << audio_port; if (video_port != -1) { QPair keyTransmiter = qMakePair(QHostAddress(ip_address), QString("TransmiterVideo")); - auto itTransmiter = std::make_unique(ip_address, video_port, audio_port); + auto itTransmiter = new Transmiter(ip_address, video_port, audio_port); qDebug() << "VIDEO_PORT" << video_port; - startThread(itTransmiter.get(), "video"); + startThread(itTransmiter, "video"); live_sessions.insert(keyTransmiter, std::move(itTransmiter)); return; } if (audio_port != -1) { QPair key = qMakePair(QHostAddress(ip_address), QString("TransmiterAudio")); - auto it = std::make_unique(ip_address, audio_port); + auto it = new Transmiter(ip_address, audio_port); qDebug() << "AUDIO_PORT" << audio_port; - startThread(it.get(), "audio"); + startThread(it, "audio"); live_sessions.insert(key, std::move(it)); return; } if (audio_port == -1 && video_port == -1) { + WId id = onStartCameraRecording(); + QPair keyTransmiter = qMakePair(ip_address, QString("TransmiterCamera")); - auto itTransmiter = std::make_unique(ip_address, video_port, -1); + auto itTransmiter = new Transmiter(ip_address, video_port, -1); qDebug() << "CAMERA_PORT" << video_port; - startThread(itTransmiter.get(), "camera"); + QMetaObject::invokeMethod(itTransmiter, "onStartCameraRecording", Qt::DirectConnection, Q_ARG(WId, id),Q_ARG(QWidget*, window1)); + + //QMetaObject::invokeMethod(itTransmiter, "onStartCameraRecording", Qt::DirectConnection, Q_ARG(QWidget*, window1->winId())); + + startThread(itTransmiter, "camera"); + emit sendStartReciver(ip_address, "video"); + live_sessions.insert(keyTransmiter, std::move(itTransmiter)); return; } @@ -135,9 +165,9 @@ void SessionManager::onReceivedPorts(const QHostAddress ip_address, qint32 video void SessionManager::onReceivedCameraPorts(const QHostAddress ip_address, qint32 video_port) { QPair keyTransmiter = qMakePair(ip_address, QString("TransmiterCamera")); - auto itTransmiter = std::make_unique(ip_address, video_port, -1); + auto itTransmiter = new Transmiter(ip_address, video_port, -1); qDebug() << "CAMERA_PORT" << video_port; - startThread(itTransmiter.get(), "camera"); + startThread(itTransmiter, "camera"); live_sessions.insert(keyTransmiter, std::move(itTransmiter)); } @@ -148,26 +178,31 @@ void SessionManager::onStartReceivingSession(const QHostAddress ip_address, cons if (session_type == "audio") { QPair key = qMakePair(ip_address, QString("ReciverAudio")); - auto it = std::make_unique(ip_address, audio_port); - startThread(it.get(), "audio"); + auto it = new Reciver(ip_address, audio_port); + startThread(it, "audio"); live_sessions.insert(key, std::move(it)); - emit sendSetPorts(ip_address, -1, audio_port); // use -1 for unused ports + emit sendSetPorts(ip_address, -1, audio_port); } if (session_type == "video") { + QPair key = qMakePair(ip_address, QString("ReciverVideo")); - auto it = std::make_unique(ip_address, video_port, audio_port); - startThread(it.get(), "video"); + auto it = new Reciver(ip_address, video_port, audio_port); + startThread(it, "video"); live_sessions.insert(key, std::move(it)); - emit sendSetPorts(ip_address, video_port, -1); // use -1 for unused ports - } + emit sendSetPorts(ip_address, video_port, -1); + +} if (session_type == "camera") { + QPair key = qMakePair(ip_address, QString("ReciverCamera")); - auto it = std::make_unique(ip_address, video_port); - startThread(it.get(), "camera"); + auto it = new Reciver(ip_address, video_port); + startThread(it, "camera"); + live_sessions.insert(key, std::move(it)); emit sendSetPorts(ip_address, -1, -1); // use -1 for unused ports + } } @@ -192,7 +227,7 @@ void SessionManager::onSetVolume(const QHostAddress ip_address, const int volume if (live_sessions.contains(qMakePair(QHostAddress(ip_address.toIPv6Address()), key))) { auto it = live_sessions[qMakePair(QHostAddress(ip_address.toIPv6Address()), key)]; - QMetaObject::invokeMethod(it.get(), "onSetVolume", Qt::DirectConnection, Q_ARG(float, volume)); + QMetaObject::invokeMethod(it, "onSetVolume", Qt::DirectConnection, Q_ARG(float, volume)); } } @@ -202,7 +237,7 @@ void SessionManager::onSetBitrate(const QHostAddress ip_address, const int bitra qDebug() << "Set bitrate" << bitrate; if (live_sessions.contains(qMakePair(ip_address, key))) { auto it = live_sessions[qMakePair(ip_address, key)]; - QMetaObject::invokeMethod(it.get(), "onSetBitrate", Qt::DirectConnection, Q_ARG(int, bitrate)); + QMetaObject::invokeMethod(it, "onSetBitrate", Qt::DirectConnection, Q_ARG(int, bitrate)); } } diff --git a/gstreamer_interface/sources/Transmiter.cpp b/gstreamer_interface/sources/Transmiter.cpp index 2f77cdd..691cc96 100644 --- a/gstreamer_interface/sources/Transmiter.cpp +++ b/gstreamer_interface/sources/Transmiter.cpp @@ -1,4 +1,11 @@ #include "Transmiter.h" +#include "qvideowidget.h" +#include + +bool Transmiter::isRunning() +{ + return running; +} void Transmiter::addLinkVideo() { GstElement *ximagesrc, *queue1, *capsfilter1, *capsfilter2, *videoscale, *videoconvert1, *videoconvert2, *rtpvp8pay, *queue2, *udpsink1; @@ -48,6 +55,8 @@ void Transmiter::addLinkVideo() { g_object_set(udpsink1, "sync", FALSE, "host", representIP(ip_address), "port", video_port, NULL); // g_object_set(x264enc, "pass", 17, "tune", 4, "bitrate", 2000, "speed-preset", 0x00000005, NULL); g_object_set(G_OBJECT(vp8enc), "deadline", 1, "target-bitrate", 2000000, NULL); + + } void Transmiter::addLinkAudio() { @@ -110,42 +119,55 @@ Transmiter::~Transmiter() { onKillSession(); } +void Transmiter::onStartCameraRecording(WId id_, QWidget* window1) +{ + id=id_; + qDebug()<<"\nPISDOSS 1T\n"; + +} + void Transmiter::onEbableCamera() { - qDebug() << "onEbableCamera Transmiter "; - GstElement *v4l2src, *videoconvert, *videoscale, *vp8enc, *rtpvp8pay, *udpsink; - gst_init(nullptr, nullptr); - if (customData.pipeline == NULL) { - customData.pipeline = gst_pipeline_new("camera"); - } + qDebug()<<"\nPISDOSS onEbableCamera 2T\n" << id; - v4l2src = gst_element_factory_make("v4l2src", "v4l2src"); - videoconvert = gst_element_factory_make("videoconvert", "videoconvert"); - videoscale = gst_element_factory_make("videoscale", "videoscale"); - // capsfilter = gst_element_factory_make("capsfilter", "capsfilterVideo1"); - vp8enc = gst_element_factory_make("vp8enc", "vp8enc"); - rtpvp8pay = gst_element_factory_make("rtpvp8pay", "rtpvp8pay"); - udpsink = gst_element_factory_make("udpsink", "udpsink1"); - if (!customData.pipeline || !v4l2src || !vp8enc || !videoconvert || !videoscale || !rtpvp8pay || !udpsink) { - g_printerr("Not all elements could be created Transmiter\n"); - return; - } + qDebug() << "onEbableCamera Transmiter "; + GstElement *v4l2src, *videoconvert, *videoscale, *vp8enc, *rtpvp8pay, *udpsink; - g_object_set(v4l2src, "device", "/dev/video0", NULL); - g_object_set(udpsink, "sync", FALSE, "host", representIP(ip_address), "port", 5001, NULL); - gst_bin_add_many(GST_BIN(customData.pipeline), v4l2src, videoconvert, videoscale, vp8enc, rtpvp8pay, udpsink, NULL); + gst_init(nullptr, nullptr); + if (customData.pipeline == NULL) { + customData.pipeline = gst_pipeline_new("camera"); + } - if (gst_element_link_many(v4l2src, videoconvert, videoscale, vp8enc, rtpvp8pay, udpsink, NULL) != TRUE) { - g_printerr( - "Failed to link elements: v4l2src -> videoscale -> videoconvert -> x264enc -> " - "rtph264pay -> udpsink1\n"); - gst_object_unref(customData.pipeline); - return; - } + v4l2src = gst_element_factory_make("ximagesrc", "v4l2src"); + videoconvert = gst_element_factory_make("videoconvert", "videoconvert"); + videoscale = gst_element_factory_make("videoscale", "videoscale"); + // capsfilter = gst_element_factory_make("capsfilter", "capsfilterVideo1"); + vp8enc = gst_element_factory_make("vp8enc", "vp8enc"); + rtpvp8pay = gst_element_factory_make("rtpvp8pay", "rtpvp8pay"); + udpsink = gst_element_factory_make("udpsink", "udpsink1"); + if (!customData.pipeline || !v4l2src || !vp8enc || !videoconvert || !videoscale || !rtpvp8pay || !udpsink) { + g_printerr("Not all elements could be created Transmiter\n"); + return; + } + + g_object_set(v4l2src, "xid", id, NULL); + + g_object_set(udpsink, "sync", FALSE, "host", representIP(ip_address), "port", 5004, NULL); + + gst_bin_add_many(GST_BIN(customData.pipeline), v4l2src, videoconvert, videoscale, vp8enc, rtpvp8pay, udpsink, NULL); + + if (gst_element_link_many(v4l2src, videoconvert, videoscale, vp8enc, rtpvp8pay, udpsink, NULL) != TRUE) { + g_printerr( + "Failed to link elements: v4l2src -> videoscale -> videoconvert -> x264enc -> " + "rtph264pay -> udpsink1\n"); + gst_object_unref(customData.pipeline); + return; + } + qDebug() << "\nЯ запустил трансивер камеру\n" ; + startSend(); - startSend(); } void Transmiter::onEnableVideo() {