mirror of
https://github.com/brunoherbelin/vimix.git
synced 2026-06-16 04:27:23 +02:00
Fixed Connection and Streamer mechanisms
This commit is contained in:
+6
-5
@@ -170,15 +170,15 @@ void Connection::ask()
|
||||
p << Connection::manager().connections_[0].port_handshake;
|
||||
p << osc::EndMessage;
|
||||
|
||||
UdpSocket socket;
|
||||
socket.SetEnableBroadcast(true);
|
||||
|
||||
// loop infinitely
|
||||
while(true)
|
||||
{
|
||||
// broadcast on several ports
|
||||
for(int i=HANDSHAKE_PORT; i<HANDSHAKE_PORT+MAX_HANDSHAKE; i++) {
|
||||
UdpSocket socket;
|
||||
socket.SetEnableBroadcast(true);
|
||||
socket.SendTo( IpEndpointName( "255.255.255.255", i ), p.Data(), p.Size() );
|
||||
}
|
||||
for(int i=HANDSHAKE_PORT; i<HANDSHAKE_PORT+MAX_HANDSHAKE; i++)
|
||||
socket.SendTo( IpEndpointName( i ), p.Data(), p.Size() );
|
||||
|
||||
// wait a bit
|
||||
std::this_thread::sleep_for(std::chrono::milliseconds(500));
|
||||
@@ -217,6 +217,7 @@ void ConnectionRequestListener::ProcessMessage( const osc::ReceivedMessage& m,
|
||||
std::string remote_ip(sender);
|
||||
remote_ip = remote_ip.substr(0, remote_ip.find_last_of(":"));
|
||||
|
||||
|
||||
try{
|
||||
// ping request : reply with pong
|
||||
if( std::strcmp( m.AddressPattern(), OSC_PREFIX OSC_PING) == 0 ){
|
||||
|
||||
+19
-15
@@ -82,14 +82,15 @@ const std::vector<std::string> NetworkToolkit::protocol_send_pipeline {
|
||||
const std::vector<std::string> NetworkToolkit::protocol_receive_pipeline {
|
||||
|
||||
"shmsrc socket-path=XXXX ! video/x-raw, format=RGB, framerate=30/1 ! queue max-size-buffers=10",
|
||||
"udpsrc buffer-size=200000 port=XXXX ! application/x-rtp,encoding-name=JPEG,payload=26 ! queue max-size-buffers=10 ! rtpjitterbuffer ! rtpjpegdepay ! jpegdec",
|
||||
"udpsrc buffer-size=200000 port=XXXX ! application/x-rtp,encoding-name=JPEG,payload=26,clock-rate=90000 ! queue max-size-buffers=10 ! rtpjpegdepay ! jpegdec",
|
||||
"udpsrc buffer-size=200000 port=XXXX ! application/x-rtp,encoding-name=H264,payload=96,clock-rate=90000 ! queue ! rtph264depay ! avdec_h264",
|
||||
"tcpclientsrc timeout=1 port=XXXX ! queue max-size-buffers=30 ! application/x-rtp-stream,media=video,encoding-name=JPEG,payload=26 ! rtpstreamdepay ! rtpjpegdepay ! jpegdec",
|
||||
"tcpclientsrc timeout=1 port=XXXX ! queue max-size-buffers=30 ! application/x-rtp-stream,media=video,encoding-name=H264,payload=96,clock-rate=90000 ! rtpstreamdepay ! rtph264depay ! avdec_h264"
|
||||
};
|
||||
|
||||
std::vector<std::string> ipstrings;
|
||||
std::vector<unsigned long> iplongs;
|
||||
bool initialized_ = false;
|
||||
std::vector<std::string> ipstrings_;
|
||||
std::vector<unsigned long> iplongs_;
|
||||
|
||||
|
||||
void add_interface(int fd, const char *name) {
|
||||
@@ -109,11 +110,12 @@ void add_interface(int fd, const char *name) {
|
||||
return; /* ignore */
|
||||
}
|
||||
// add only if not already listed
|
||||
if ( std::find(ipstrings.begin(), ipstrings.end(), std::string(host)) == ipstrings.end() )
|
||||
std::string hostip(host);
|
||||
if ( std::find(ipstrings_.begin(), ipstrings_.end(), hostip) == ipstrings_.end() )
|
||||
{
|
||||
ipstrings.push_back( std::string(host) );
|
||||
iplongs.push_back( GetHostByName(host) );
|
||||
// printf("%-24s%s %lu\n", name, host, GetHostByName(host));
|
||||
ipstrings_.push_back( hostip );
|
||||
iplongs_.push_back( GetHostByName(host) );
|
||||
// printf("%s %s %lu\n", name, host, GetHostByName(host));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -143,32 +145,34 @@ void list_interfaces()
|
||||
}
|
||||
}
|
||||
close(fd);
|
||||
initialized_ = true;
|
||||
}
|
||||
|
||||
std::vector<std::string> NetworkToolkit::host_ips()
|
||||
{
|
||||
if (ipstrings.empty())
|
||||
if (!initialized_)
|
||||
list_interfaces();
|
||||
|
||||
return ipstrings;
|
||||
return ipstrings_;
|
||||
}
|
||||
|
||||
|
||||
bool NetworkToolkit::is_host_ip(const std::string &ip)
|
||||
{
|
||||
if ( ip.compare("localhost") == 0)
|
||||
return true;
|
||||
|
||||
if (ipstrings.empty())
|
||||
if (!initialized_)
|
||||
list_interfaces();
|
||||
|
||||
return std::find(ipstrings.begin(), ipstrings.end(), ip) != ipstrings.end();
|
||||
return std::find(ipstrings_.begin(), ipstrings_.end(), ip) != ipstrings_.end();
|
||||
}
|
||||
|
||||
std::string NetworkToolkit::closest_host_ip(const std::string &ip)
|
||||
{
|
||||
std::string address = "localhost";
|
||||
|
||||
if (iplongs.empty())
|
||||
if (!initialized_)
|
||||
list_interfaces();
|
||||
|
||||
// discard trivial case
|
||||
@@ -178,8 +182,8 @@ std::string NetworkToolkit::closest_host_ip(const std::string &ip)
|
||||
unsigned long host = GetHostByName( ip.c_str() );
|
||||
unsigned long mini = host;
|
||||
|
||||
for (size_t i=0; i < iplongs.size(); i++){
|
||||
unsigned long diff = host > iplongs[i] ? host-iplongs[i] : iplongs[i]-host;
|
||||
for (size_t i=0; i < iplongs_.size(); i++){
|
||||
unsigned long diff = host > iplongs_[i] ? host-iplongs_[i] : iplongs_[i]-host;
|
||||
if (diff < mini) {
|
||||
mini = diff;
|
||||
index_mini = (int) i;
|
||||
@@ -187,7 +191,7 @@ std::string NetworkToolkit::closest_host_ip(const std::string &ip)
|
||||
}
|
||||
|
||||
if (index_mini>0)
|
||||
address = ipstrings[index_mini];
|
||||
address = ipstrings_[index_mini];
|
||||
|
||||
}
|
||||
|
||||
|
||||
+12
-2
@@ -94,25 +94,29 @@ Streaming::~Streaming()
|
||||
}
|
||||
}
|
||||
|
||||
bool Streaming::busy() const
|
||||
bool Streaming::busy()
|
||||
{
|
||||
bool b = false;
|
||||
|
||||
streamers_lock_.lock();
|
||||
std::vector<VideoStreamer *>::const_iterator sit = streamers_.begin();
|
||||
for (; sit != streamers_.end() && !b; sit++)
|
||||
b = (*sit)->busy() ;
|
||||
streamers_lock_.unlock();
|
||||
|
||||
return b;
|
||||
}
|
||||
|
||||
|
||||
std::vector<std::string> Streaming::listStreams() const
|
||||
std::vector<std::string> Streaming::listStreams()
|
||||
{
|
||||
std::vector<std::string> ls;
|
||||
|
||||
streamers_lock_.lock();
|
||||
std::vector<VideoStreamer *>::const_iterator sit = streamers_.begin();
|
||||
for (; sit != streamers_.end(); sit++)
|
||||
ls.push_back( (*sit)->info() );
|
||||
streamers_lock_.unlock();
|
||||
|
||||
return ls;
|
||||
}
|
||||
@@ -128,8 +132,10 @@ void Streaming::enable(bool on)
|
||||
// refuse streaming requests
|
||||
enabled_ = false;
|
||||
// ending and removing all streaming
|
||||
streamers_lock_.lock();
|
||||
for (auto sit = streamers_.begin(); sit != streamers_.end(); sit=streamers_.erase(sit))
|
||||
(*sit)->stop();
|
||||
streamers_lock_.unlock();
|
||||
Log::Info("Refusing stream requests to %s. No streaming ongoing.", Connection::manager().info().name.c_str());
|
||||
}
|
||||
}
|
||||
@@ -155,6 +161,7 @@ void Streaming::removeStream(const std::string &sender, int port)
|
||||
std::string sender_ip = sender.substr(0, sender.find_last_of(":"));
|
||||
|
||||
// parse the list for a streamers matching IP and port
|
||||
streamers_lock_.lock();
|
||||
std::vector<VideoStreamer *>::const_iterator sit = streamers_.begin();
|
||||
for (; sit != streamers_.end(); sit++){
|
||||
NetworkToolkit::StreamConfig config = (*sit)->config_;
|
||||
@@ -169,12 +176,14 @@ void Streaming::removeStream(const std::string &sender, int port)
|
||||
break;
|
||||
}
|
||||
}
|
||||
streamers_lock_.unlock();
|
||||
|
||||
}
|
||||
|
||||
void Streaming::removeStreams(const std::string &clientname)
|
||||
{
|
||||
// remove all streamers matching given IP
|
||||
streamers_lock_.lock();
|
||||
std::vector<VideoStreamer *>::const_iterator sit = streamers_.begin();
|
||||
while ( sit != streamers_.end() ){
|
||||
NetworkToolkit::StreamConfig config = (*sit)->config_;
|
||||
@@ -190,6 +199,7 @@ void Streaming::removeStreams(const std::string &clientname)
|
||||
else
|
||||
sit++;
|
||||
}
|
||||
streamers_lock_.unlock();
|
||||
}
|
||||
|
||||
void Streaming::refuseStream(const std::string &sender, int reply_to)
|
||||
|
||||
+6
-4
@@ -1,6 +1,8 @@
|
||||
#ifndef STREAMER_H
|
||||
#define STREAMER_H
|
||||
|
||||
#include <mutex>
|
||||
|
||||
#include <gst/pbutils/pbutils.h>
|
||||
#include <gst/app/gstappsrc.h>
|
||||
|
||||
@@ -45,8 +47,8 @@ public:
|
||||
void setSession(Session *se);
|
||||
void removeStreams(const std::string &clientname);
|
||||
|
||||
bool busy() const;
|
||||
std::vector<std::string> listStreams() const;
|
||||
bool busy();
|
||||
std::vector<std::string> listStreams();
|
||||
|
||||
protected:
|
||||
void addStream(const std::string &sender, int reply_to, const std::string &clientname);
|
||||
@@ -63,8 +65,8 @@ private:
|
||||
int width_;
|
||||
int height_;
|
||||
|
||||
// TODO Mutex to protect access to list of streamers
|
||||
std::vector<VideoStreamer *> streamers_;
|
||||
std::vector<VideoStreamer *> streamers_;
|
||||
std::mutex streamers_lock_;
|
||||
};
|
||||
|
||||
class VideoStreamer : public FrameGrabber
|
||||
|
||||
Reference in New Issue
Block a user