forked from jamulussoftware/jamulus
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[stereo-mix] Add a stereo mix server
A stereo mix server is a local TCP server that outputs the mixed sound as s16le stereo @ 48000 hz. The output can be streamed to ffmpeg for sending to (e.g.) an Icecast server: nc localhost $STEREO_MIX_PORT \ | ffmpeg -f s16le -ar 48000 -ac 2 -i - \ -acodec libmp3lame -ab 128k -f mp3 $ICECAST_URL The implementation for `CServer::MixStream` comes from the Streamer2 PR: jamulussoftware#967 By making the stream system pull-based (consumers ask Jamulus for data), rather than push-based (Jamulus sends data to a streaming server), the implementation is greatly simplified. * No need to implement ways to toggle streaming on/off, as streaming starts when a client connects and ends when the client disconnects. * No need to deal with launching, monitoring, and killing sub-processes. * Works with all OSes. Co-authored-by: dingodoppelt <[email protected]>
- Loading branch information
1 parent
c40d39c
commit 04e71bd
Showing
5 changed files
with
166 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
/******************************************************************************\ | ||
* Copyright (c) 2021 | ||
* | ||
* Author(s): | ||
* dtinth | ||
* | ||
****************************************************************************** | ||
* | ||
* This program is free software; you can redistribute it and/or modify it under | ||
* the terms of the GNU General Public License as published by the Free Software | ||
* Foundation; either version 2 of the License, or (at your option) any later | ||
* version. | ||
* | ||
* This program is distributed in the hope that it will be useful, but WITHOUT | ||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS | ||
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more | ||
* details. | ||
* | ||
* You should have received a copy of the GNU General Public License along with | ||
* this program; if not, write to the Free Software Foundation, Inc., | ||
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA | ||
* | ||
\******************************************************************************/ | ||
|
||
#include "stereomixserver.h" | ||
|
||
CStereoMixServer::CStereoMixServer ( CServer* pServer, int iPort ) : | ||
QObject ( pServer ), | ||
pServer ( pServer ), | ||
iPort ( iPort ), | ||
pTransportServer ( new QTcpServer ( this ) ) | ||
{ | ||
connect ( pTransportServer, &QTcpServer::newConnection, this, &CStereoMixServer::OnNewConnection ); | ||
connect ( pServer, &CServer::StreamFrame, this, &CStereoMixServer::OnStreamFrame ); | ||
} | ||
|
||
CStereoMixServer::~CStereoMixServer() | ||
{ | ||
if ( pTransportServer->isListening() ) | ||
{ | ||
qInfo() << "- stopping stereo mix server"; | ||
pTransportServer->close(); | ||
} | ||
} | ||
|
||
void CStereoMixServer::Start() | ||
{ | ||
if ( iPort < 0 ) | ||
{ | ||
return; | ||
} | ||
if ( pTransportServer->listen ( QHostAddress ( "127.0.0.1" ), iPort ) ) | ||
{ | ||
qInfo() << "- stereo mix server started on port" << pTransportServer->serverPort(); | ||
} | ||
else | ||
{ | ||
qInfo() << "- unable to start stereo mix server:" << pTransportServer->errorString(); | ||
} | ||
} | ||
|
||
void CStereoMixServer::OnNewConnection() | ||
{ | ||
QTcpSocket* pSocket = pTransportServer->nextPendingConnection(); | ||
if ( !pSocket ) | ||
{ | ||
return; | ||
} | ||
|
||
qInfo() << "- sending stereo mix to:" << pSocket->peerAddress().toString(); | ||
vecClients.append ( pSocket ); | ||
|
||
connect ( pSocket, &QTcpSocket::disconnected, [this, pSocket]() { | ||
qInfo() << "- finish sending stereo mix to:" << pSocket->peerAddress().toString(); | ||
vecClients.removeAll ( pSocket ); | ||
pSocket->deleteLater(); | ||
} ); | ||
} | ||
|
||
void CStereoMixServer::OnStreamFrame ( const int iServerFrameSizeSamples, const CVector<int16_t>& data ) | ||
{ | ||
for ( auto socket : vecClients ) | ||
{ | ||
socket->write ( reinterpret_cast<const char*> ( &data[0] ), sizeof ( int16_t ) * ( 2 * iServerFrameSizeSamples ) ); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
/******************************************************************************\ | ||
* Copyright (c) 2021 | ||
* | ||
* Author(s): | ||
* dtinth | ||
* | ||
****************************************************************************** | ||
* | ||
* This program is free software; you can redistribute it and/or modify it under | ||
* the terms of the GNU General Public License as published by the Free Software | ||
* Foundation; either version 2 of the License, or (at your option) any later | ||
* version. | ||
* | ||
* This program is distributed in the hope that it will be useful, but WITHOUT | ||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS | ||
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more | ||
* details. | ||
* | ||
* You should have received a copy of the GNU General Public License along with | ||
* this program; if not, write to the Free Software Foundation, Inc., | ||
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA | ||
* | ||
\******************************************************************************/ | ||
|
||
#pragma once | ||
|
||
#include <QObject> | ||
#include <QTcpServer> | ||
#include <QTcpSocket> | ||
#include <memory> | ||
#include "util.h" | ||
#include "server.h" | ||
|
||
typedef std::function<void ( const QJsonObject&, QJsonObject& )> CRpcHandler; | ||
|
||
/* Classes ********************************************************************/ | ||
class CStereoMixServer : public QObject | ||
{ | ||
Q_OBJECT | ||
|
||
public: | ||
CStereoMixServer ( CServer* pServer, int iPort ); | ||
virtual ~CStereoMixServer(); | ||
|
||
void Start(); | ||
|
||
private: | ||
int iPort; | ||
QTcpServer* pTransportServer; | ||
CServer* pServer; | ||
QVector<QTcpSocket*> vecClients; | ||
|
||
protected slots: | ||
void OnNewConnection(); | ||
void OnStreamFrame ( const int iServerFrameSizeSamples, const CVector<int16_t>& data ); | ||
}; |