Skip to content

Commit

Permalink
Stream constructors use the DEFLECT_ID and DEFLECT_HOST ENV_VARs
Browse files Browse the repository at this point in the history
  • Loading branch information
Raphael Dumusc committed Jun 3, 2016
1 parent b0a304e commit 0e2eb25
Show file tree
Hide file tree
Showing 22 changed files with 390 additions and 136 deletions.
26 changes: 13 additions & 13 deletions apps/DesktopStreamer/MainWindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -80,22 +80,22 @@ MainWindow::MainWindow()
{
setupUi( this );

connect( _hostnameComboBox, &QComboBox::currentTextChanged,
connect( _hostComboBox, &QComboBox::currentTextChanged,
[&]( const QString& text )
{
_streamButton->setEnabled( !text.isEmpty( ));
_listView->setEnabled( !text.isEmpty( ));
});

for( const auto& entry : defaultHosts )
_hostnameComboBox->addItem( entry.first, entry.second );
_hostComboBox->addItem( entry.first, entry.second );

// no default host selected initially
_hostnameComboBox->setCurrentIndex( -1 );
_hostComboBox->setCurrentIndex( -1 );

char hostname[256] = { 0 };
gethostname( hostname, 256 );
_streamnameLineEdit->setText( QString( "%1" ).arg( hostname ));
_streamIdLineEdit->setText( QString( "%1" ).arg( hostname ));

#ifdef DEFLECT_USE_QT5MACEXTRAS
_listView->setModel( new DesktopWindowsModel );
Expand Down Expand Up @@ -195,13 +195,13 @@ void MainWindow::_updateStreams()
continue;
}

const std::string name = index.isValid() ?
const std::string appName = index.isValid() ?
_listView->model()->data( index, Qt::DisplayRole ).
toString().toStdString() : std::string();
const std::string streamName = std::to_string( ++_streamID ) +
" " + name + " - " +
_streamnameLineEdit->text().toStdString();
StreamPtr stream( new Stream( *this, index, streamName, host ));
const std::string streamId = std::to_string( ++_streamID ) +
" " + appName + " - " +
_streamIdLineEdit->text().toStdString();
StreamPtr stream( new Stream( *this, index, streamId, host ));

if( !stream->isConnected( ))
{
Expand Down Expand Up @@ -235,7 +235,7 @@ void MainWindow::_updateStreams()
{
const QPersistentModelIndex index; // default == use desktop
StreamPtr stream( new Stream( *this, index,
_streamnameLineEdit->text().toStdString(),
_streamIdLineEdit->text().toStdString(),
host ));
if( stream->isConnected( ))
{
Expand Down Expand Up @@ -332,10 +332,10 @@ void MainWindow::_regulateFrameRate()
std::string MainWindow::_getStreamHost() const
{
QString streamHost;
if( _hostnameComboBox->findText(_hostnameComboBox->currentText( )) == -1 )
streamHost = _hostnameComboBox->currentText();
if( _hostComboBox->findText(_hostComboBox->currentText( )) == -1 )
streamHost = _hostComboBox->currentText();
else
streamHost = _hostnameComboBox->currentData().toString();
streamHost = _hostComboBox->currentData().toString();
return streamHost.toStdString();
}

Expand Down
8 changes: 4 additions & 4 deletions apps/DesktopStreamer/MainWindow.ui
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@
</widget>
</item>
<item row="0" column="1">
<widget class="QComboBox" name="_hostnameComboBox">
<widget class="QComboBox" name="_hostComboBox">
<property name="sizePolicy">
<sizepolicy hsizetype="Expanding" vsizetype="Fixed">
<horstretch>0</horstretch>
Expand Down Expand Up @@ -173,7 +173,7 @@
</widget>
</item>
<item row="3" column="1">
<widget class="QLineEdit" name="_streamnameLineEdit">
<widget class="QLineEdit" name="_streamIdLineEdit">
<property name="sizePolicy">
<sizepolicy hsizetype="Expanding" vsizetype="Fixed">
<horstretch>0</horstretch>
Expand Down Expand Up @@ -261,7 +261,7 @@
<connection>
<sender>_streamButton</sender>
<signal>toggled(bool)</signal>
<receiver>_streamnameLineEdit</receiver>
<receiver>_streamIdLineEdit</receiver>
<slot>setDisabled(bool)</slot>
<hints>
<hint type="sourcelabel">
Expand All @@ -277,7 +277,7 @@
<connection>
<sender>_streamButton</sender>
<signal>toggled(bool)</signal>
<receiver>_hostnameComboBox</receiver>
<receiver>_hostComboBox</receiver>
<slot>setDisabled(bool)</slot>
<hints>
<hint type="sourcelabel">
Expand Down
4 changes: 2 additions & 2 deletions apps/DesktopStreamer/Stream.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,8 @@
#define CURSOR_IMAGE_SIZE 20

Stream::Stream( const MainWindow& parent, const QPersistentModelIndex window,
const std::string& name, const std::string& host )
: deflect::Stream( name, host )
const std::string& id, const std::string& host )
: deflect::Stream( id, host )
, _parent( parent )
, _window( window )
, _cursor( QImage( CURSOR_IMAGE_FILE ).scaled(
Expand Down
2 changes: 1 addition & 1 deletion apps/DesktopStreamer/Stream.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ class Stream : public deflect::Stream
public:
/** Construct a new stream for the given desktop window. */
Stream( const MainWindow& parent, const QPersistentModelIndex window,
const std::string& name, const std::string& host );
const std::string& id, const std::string& host );
~Stream();

/**
Expand Down
4 changes: 2 additions & 2 deletions apps/QmlStreamer/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,9 @@ int main( int argc, char** argv )
"qrc:/qml/gui.qml" );
parser.addOption( qmlFileOption );

QCommandLineOption streamHostOption( "host", "Stream target hostname "
QCommandLineOption streamHostOption( "host", "Stream target host "
"(default: localhost)",
"hostname", "localhost" );
"host", "localhost" );
parser.addOption( streamHostOption );

// note: the 'name' command line option is already taken by QCoreApplication
Expand Down
20 changes: 10 additions & 10 deletions apps/SimpleStreamer/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,8 @@
bool deflectInteraction = false;
bool deflectCompressImage = true;
unsigned int deflectCompressionQuality = 75;
char* deflectHostname = NULL;
std::string deflectStreamName = "SimpleStreamer";
char* deflectHost = NULL;
std::string deflectStreamId = "SimpleStreamer";
deflect::Stream* deflectStream = NULL;

void syntax( char* app );
Expand All @@ -78,7 +78,7 @@ int main( int argc, char** argv )
{
readCommandLineArguments( argc, argv );

if( deflectHostname == NULL )
if( deflectHost == NULL )
syntax( argv[0] );

initGLWindow( argc, argv );
Expand All @@ -101,7 +101,7 @@ void readCommandLineArguments( int argc, char** argv )
case 'n':
if( i + 1 < argc )
{
deflectStreamName = argv[i+1];
deflectStreamId = argv[i+1];
++i;
}
break;
Expand All @@ -116,7 +116,7 @@ void readCommandLineArguments( int argc, char** argv )
}
}
else if( i == argc - 1 )
deflectHostname = argv[i];
deflectHost = argv[i];
}
}

Expand Down Expand Up @@ -147,7 +147,7 @@ void initGLWindow( int argc, char** argv )

void initDeflectStream()
{
deflectStream = new deflect::Stream( deflectStreamName, deflectHostname );
deflectStream = new deflect::Stream( deflectStreamId, deflectHost );
if( !deflectStream->isConnected( ))
{
std::cerr << "Could not connect to host!" << std::endl;
Expand All @@ -166,11 +166,11 @@ void initDeflectStream()

void syntax( char* app )
{
std::cerr << "syntax: " << app << " [options] <hostname>" << std::endl;
std::cerr << "syntax: " << app << " [options] <host>" << std::endl;
std::cerr << "options:" << std::endl;
std::cerr << " -n <stream name> set stream name (default SimpleStreamer)" << std::endl;
std::cerr << " -i enable interaction events (default disabled)" << std::endl;
std::cerr << " -u enable uncompressed streaming (default disabled)" << std::endl;
std::cerr << " -n <stream id> set stream identifier (default: 'SimpleStreamer')" << std::endl;
std::cerr << " -i enable interaction events (default: OFF)" << std::endl;
std::cerr << " -u enable uncompressed streaming (default: OFF)" << std::endl;

exit( 1 );
}
Expand Down
37 changes: 18 additions & 19 deletions deflect/ServerWorker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,8 @@ ServerWorker::~ServerWorker()
// We still want to remove this source so that the stream does not get stuck
// if other senders are still active / resp. the window gets closed if no
// more senders contribute to it.
if( !_streamUri.isEmpty( ))
emit removeStreamSource( _streamUri, _sourceId );
if( !_streamId.isEmpty( ))
emit removeStreamSource( _streamId, _sourceId );

if( _tcpSocket->state() == QAbstractSocket::ConnectedState )
_sendQuit();
Expand All @@ -103,7 +103,7 @@ void ServerWorker::initConnection()

void ServerWorker::closeConnection( const QString uri )
{
if( uri != _streamUri )
if( uri != _streamId )
return;

Event closeEvent;
Expand All @@ -116,7 +116,7 @@ void ServerWorker::closeConnection( const QString uri )
void ServerWorker::replyToEventRegistration( const QString uri,
const bool success )
{
if( uri != _streamUri )
if( uri != _streamId )
return;

_registeredToEvents = success;
Expand Down Expand Up @@ -196,39 +196,38 @@ void ServerWorker::_handleMessage( const MessageHeader& messageHeader,
const QString uri( messageHeader.uri );
if( uri.isEmpty( ))
{
std::cerr << "Warning: rejecting streamer with empty uri"
<< std::endl;
closeConnection( _streamUri );
std::cerr << "Warning: rejecting streamer with empty id" << std::endl;
closeConnection( _streamId );
return;
}
if( uri != _streamUri &&
if( uri != _streamId &&
messageHeader.type != MESSAGE_TYPE_PIXELSTREAM_OPEN )
{
std::cerr << "Warning: ingnoring message with incorrect stream uri: '"
std::cerr << "Warning: ingnoring message with incorrect stream id: '"
<< messageHeader.uri << "', expected: '"
<< _streamUri.toStdString() << "'" << std::endl;
<< _streamId.toStdString() << "'" << std::endl;
return;
}

switch( messageHeader.type )
{
case MESSAGE_TYPE_QUIT:
emit removeStreamSource( _streamUri, _sourceId );
_streamUri = QString();
emit removeStreamSource( _streamId, _sourceId );
_streamId = QString();
break;

case MESSAGE_TYPE_PIXELSTREAM_OPEN:
if( !_streamUri.isEmpty( ))
if( !_streamId.isEmpty( ))
{
std::cerr << "Warning: PixelStream already opened!" << std::endl;
return;
}
_streamUri = uri;
emit addStreamSource( _streamUri, _sourceId );
_streamId = uri;
emit addStreamSource( _streamId, _sourceId );
break;

case MESSAGE_TYPE_PIXELSTREAM_FINISH_FRAME:
emit receivedFrameFinished( _streamUri, _sourceId );
emit receivedFrameFinished( _streamId, _sourceId );
break;

case MESSAGE_TYPE_PIXELSTREAM:
Expand All @@ -239,7 +238,7 @@ void ServerWorker::_handleMessage( const MessageHeader& messageHeader,
{
const SizeHints* hints =
reinterpret_cast< const SizeHints* >( byteArray.data( ));
emit receivedSizeHints( _streamUri, SizeHints( *hints ));
emit receivedSizeHints( _streamId, SizeHints( *hints ));
break;
}

Expand All @@ -251,7 +250,7 @@ void ServerWorker::_handleMessage( const MessageHeader& messageHeader,
{
const bool exclusive =
(messageHeader.type == MESSAGE_TYPE_BIND_EVENTS_EX);
emit registerToEvents( _streamUri, exclusive, this );
emit registerToEvents( _streamId, exclusive, this );
}
break;

Expand All @@ -272,7 +271,7 @@ void ServerWorker::_handlePixelStreamMessage( const QByteArray& byteArray )
byteArray.right( byteArray.size() - sizeof( SegmentParameters ));
segment.imageData = imageData;

emit( receivedSegment( _streamUri, _sourceId, segment ));
emit( receivedSegment( _streamId, _sourceId, segment ));
}

void ServerWorker::_sendProtocolVersion()
Expand Down
2 changes: 1 addition & 1 deletion deflect/ServerWorker.h
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ private slots:
private:
QTcpSocket* _tcpSocket;

QString _streamUri;
QString _streamId;
int _sourceId;

bool _registeredToEvents;
Expand Down
20 changes: 13 additions & 7 deletions deflect/Socket.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,9 @@ namespace deflect

const unsigned short Socket::defaultPortNumber = DEFAULT_PORT_NUMBER;

Socket::Socket( const std::string& hostname, const unsigned short port )
: _socket( new QTcpSocket( ))
Socket::Socket( const std::string& host, const unsigned short port )
: _host( host )
, _socket( new QTcpSocket( ))
, _remoteProtocolVersion( INVALID_NETWORK_PROTOCOL_VERSION )
{
// disable warnings which occur if no QCoreApplication is present during
Expand All @@ -69,7 +70,7 @@ Socket::Socket( const std::string& hostname, const unsigned short port )
log->setEnabled( QtWarningMsg, false );
}

_connect( hostname, port );
_connect( host, port );

QObject::connect( _socket, &QTcpSocket::disconnected,
this, &Socket::disconnected );
Expand All @@ -80,6 +81,11 @@ Socket::~Socket()
delete _socket;
}

const std::string& Socket::getHost() const
{
return _host;
}

bool Socket::isConnected() const
{
return _socket->state() == QTcpSocket::ConnectedState;
Expand Down Expand Up @@ -182,17 +188,17 @@ bool Socket::_receiveHeader( MessageHeader& messageHeader )
return stream.status() == QDataStream::Ok;
}

bool Socket::_connect( const std::string& hostname, const unsigned short port )
bool Socket::_connect( const std::string& host, const unsigned short port )
{
// make sure we're disconnected
_socket->disconnectFromHost();

// open connection
_socket->connectToHost( hostname.c_str(), port );
_socket->connectToHost( host.c_str(), port );

if( !_socket->waitForConnected( RECEIVE_TIMEOUT_MS ))
{
std::cerr << "could not connect to host " << hostname << ":" << port
std::cerr << "could not connect to host " << host << ":" << port
<< std::endl;
return false;
}
Expand All @@ -201,7 +207,7 @@ bool Socket::_connect( const std::string& hostname, const unsigned short port )
if( _checkProtocolVersion( ))
return true;

std::cerr << "Protocol version check failed for host: " << hostname << ":"
std::cerr << "Protocol version check failed for host: " << host << ":"
<< port << std::endl;
_socket->disconnectFromHost();
return false;
Expand Down
Loading

0 comments on commit 0e2eb25

Please sign in to comment.