-
Notifications
You must be signed in to change notification settings - Fork 244
Expand file tree
/
Copy pathserver.h
More file actions
390 lines (297 loc) · 15.6 KB
/
server.h
File metadata and controls
390 lines (297 loc) · 15.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
/******************************************************************************\
* Copyright (c) 2004-2026
*
* Author(s):
* Volker Fischer
*
******************************************************************************
*
* 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 <QDateTime>
#include <QHostAddress>
#include <QFileInfo>
#include <algorithm>
#ifdef USE_OPUS_SHARED_LIB
# include "opus/opus_custom.h"
#else
# include "opus_custom.h"
#endif
#include "global.h"
#include "buffer.h"
#include "signalhandler.h"
#include "socket.h"
#include "channel.h"
#include "util.h"
#include "serverlogging.h"
#include "serverlist.h"
#include "recorder/jamcontroller.h"
#include "threadpool.h"
/* Definitions ****************************************************************/
// no valid channel number
#define INVALID_CHANNEL_ID ( MAX_NUM_CHANNELS + 1 )
/* Classes ********************************************************************/
template<unsigned int slotId>
class CServerSlots : public CServerSlots<slotId - 1>
{
public:
void OnSendProtMessCh ( CVector<uint8_t> mess ) { SendProtMessage ( slotId - 1, mess ); }
void OnReqConnClientsListCh() { CreateAndSendChanListForThisChan ( slotId - 1 ); }
void OnChatTextReceivedCh ( QString strChatText ) { CreateAndSendChatTextForAllConChannels ( slotId - 1, strChatText ); }
void OnMuteStateHasChangedCh ( int iChanID, bool bIsMuted ) { CreateOtherMuteStateChanged ( slotId - 1, iChanID, bIsMuted ); }
void OnServerAutoSockBufSizeChangeCh ( int iNNumFra ) { CreateAndSendJitBufMessage ( slotId - 1, iNNumFra ); }
protected:
virtual void SendProtMessage ( int iChID, CVector<uint8_t> vecMessage ) = 0;
virtual void CreateAndSendChanListForThisChan ( const int iCurChanID ) = 0;
virtual void CreateAndSendChatTextForAllConChannels ( const int iCurChanID, const QString& strChatText ) = 0;
virtual void CreateOtherMuteStateChanged ( const int iCurChanID, const int iOtherChanID, const bool bIsMuted ) = 0;
virtual void CreateAndSendJitBufMessage ( const int iCurChanID, const int iNNumFra ) = 0;
};
template<>
class CServerSlots<0>
{};
class CServer : public QObject, public CServerSlots<MAX_NUM_CHANNELS>
{
Q_OBJECT
public:
CServer ( const int iNewMaxNumChan,
const QString& strLoggingFileName,
const QString& strServerBindIP,
const quint16 iPortNumber,
const quint16 iQosNumber,
const QString& strHTMLStatusFileName,
const QString& strDirectoryAddress,
const QString& strServerListFileName,
const QString& strServerInfo,
const QString& strServerListFilter,
const QString& strServerPublicIP,
const QString& strNewWelcomeMessage,
const QString& strRecordingDirName,
const bool bNDisconnectAllClientsOnQuit,
const bool bNUseDoubleSystemFrameSize,
const bool bNUseMultithreading,
const bool bDisableRecording,
const bool bNDelayPan,
const bool bNEnableIPv6,
const ELicenceType eNLicenceType );
virtual ~CServer();
void Start();
void Stop();
bool IsRunning() { return HighPrecisionTimer.isActive(); }
bool PutAudioData ( const CVector<uint8_t>& vecbyRecBuf, const int iNumBytesRead, const CHostAddress& HostAdr, int& iCurChanID );
int GetNumberOfConnectedClients();
void GetConCliParam ( CVector<CHostAddress>& vecHostAddresses,
CVector<QString>& vecsName,
CVector<int>& veciJitBufNumFrames,
CVector<int>& veciNetwFrameSizeFact,
CVector<CChannelCoreInfo>& vecChanInfo );
void CreateCLServerListReqVerAndOSMes ( const CHostAddress& InetAddr ) { ConnLessProtocol.CreateCLReqVersionAndOSMes ( InetAddr ); }
// IPv6 Enabled
bool IsIPv6Enabled() { return bEnableIPv6; }
// GUI settings ------------------------------------------------------------
int GetClientNumAudioChannels ( const int iChanNum ) { return vecChannels[iChanNum].GetNumAudioChannels(); }
void SetDirectoryType ( const EDirectoryType eNCSAT ) { ServerListManager.SetDirectoryType ( eNCSAT ); }
EDirectoryType GetDirectoryType() { return ServerListManager.GetDirectoryType(); }
bool IsDirectory() { return ServerListManager.IsDirectory(); }
ESvrRegStatus GetSvrRegStatus() { return ServerListManager.GetSvrRegStatus(); }
void SetServerName ( const QString& strNewName ) { ServerListManager.SetServerName ( strNewName ); }
QString GetServerName() { return ServerListManager.GetServerName(); }
void SetServerCity ( const QString& strNewCity ) { ServerListManager.SetServerCity ( strNewCity ); }
QString GetServerCity() { return ServerListManager.GetServerCity(); }
void SetServerCountry ( const QLocale::Country eNewCountry ) { ServerListManager.SetServerCountry ( eNewCountry ); }
QLocale::Country GetServerCountry() { return ServerListManager.GetServerCountry(); }
bool GetRecorderInitialised() { return JamController.GetRecorderInitialised(); }
void SetEnableRecording ( bool bNewEnableRecording );
bool GetDisableRecording() { return bDisableRecording; }
QString GetRecorderErrMsg() { return JamController.GetRecorderErrMsg(); }
bool GetRecordingEnabled() { return JamController.GetRecordingEnabled(); }
void RequestNewRecording() { JamController.RequestNewRecording(); }
void SetRecordingDir ( QString newRecordingDir )
{
JamController.SetRecordingDir ( newRecordingDir, iServerFrameSizeSamples, bDisableRecording );
}
QString GetRecordingDir() { return JamController.GetRecordingDir(); }
void SetWelcomeMessage ( const QString& strNWelcMess );
QString GetWelcomeMessage() { return strWelcomeMessage; }
void SetDirectoryAddress ( const QString& sNDirectoryAddress ) { ServerListManager.SetDirectoryAddress ( sNDirectoryAddress ); }
QString GetDirectoryAddress() { return ServerListManager.GetDirectoryAddress(); }
QString GetServerListFileName() { return ServerListManager.GetServerListFileName(); }
bool SetServerListFileName ( QString strFilename ) { return ServerListManager.SetServerListFileName ( strFilename ); }
void SetAutoRunMinimized ( const bool NAuRuMin ) { bAutoRunMinimized = NAuRuMin; }
bool GetAutoRunMinimized() { return bAutoRunMinimized; }
void SetEnableDelayPanning ( bool bDelayPanningOn ) { bDelayPan = bDelayPanningOn; }
bool IsDelayPanningEnabled() { return bDelayPan; }
protected:
// access functions for actual channels
bool IsConnected ( const int iChanNum ) { return vecChannels[iChanNum].IsConnected(); }
int FindChannel ( const CHostAddress& CheckAddr, const bool bAllowNew = false );
void InitChannel ( const int iNewChanID, const CHostAddress& InetAddr );
void FreeChannel ( const int iCurChanID );
void DumpChannels ( const QString& title );
CVector<CChannelInfo> CreateChannelList();
virtual void CreateAndSendChanListForAllConChannels();
virtual void CreateAndSendChanListForThisChan ( const int iCurChanID );
virtual void CreateAndSendChatTextForAllConChannels ( const int iCurChanID, const QString& strChatText );
virtual void CreateOtherMuteStateChanged ( const int iCurChanID, const int iOtherChanID, const bool bIsMuted );
virtual void CreateAndSendJitBufMessage ( const int iCurChanID, const int iNNumFra );
virtual void SendProtMessage ( int iChID, CVector<uint8_t> vecMessage );
template<unsigned int slotId>
inline void connectChannelSignalsToServerSlots();
void WriteHTMLChannelList();
void WriteHTMLServerQuit();
static void DecodeReceiveDataBlocks ( CServer* pServer, const int iStartChanCnt, const int iStopChanCnt, const int iNumClients );
static void MixEncodeTransmitDataBlocks ( CServer* pServer, const int iStartChanCnt, const int iStopChanCnt, const int iNumClients );
void DecodeReceiveData ( const int iChanCnt, const int iNumClients );
void MixEncodeTransmitData ( const int iChanCnt, const int iNumClients );
virtual void customEvent ( QEvent* pEvent );
void CreateAndSendRecorderStateForAllConChannels();
// if server mode is normal or double system frame size
bool bUseDoubleSystemFrameSize;
int iServerFrameSizeSamples;
// variables needed for multithreading support
bool bUseMultithreading;
int iMaxNumThreads;
CVector<std::future<void>> Futures;
bool CreateLevelsForAllConChannels ( const int iNumClients,
const CVector<int>& vecNumAudioChannels,
const CVector<CVector<int16_t>> vecvecsData,
CVector<uint16_t>& vecLevelsOut );
// do not use the vector class since CChannel does not have appropriate
// copy constructor/operator
CChannel vecChannels[MAX_NUM_CHANNELS];
int iMaxNumChannels;
int iCurNumChannels;
int vecChannelOrder[MAX_NUM_CHANNELS];
QMutex MutexChanOrder;
CProtocol ConnLessProtocol;
QMutex Mutex;
QMutex MutexWelcomeMessage;
bool bChannelIsNowDisconnected;
// audio encoder/decoder
OpusCustomMode* Opus64Mode[MAX_NUM_CHANNELS];
OpusCustomEncoder* Opus64EncoderMono[MAX_NUM_CHANNELS];
OpusCustomDecoder* Opus64DecoderMono[MAX_NUM_CHANNELS];
OpusCustomEncoder* Opus64EncoderStereo[MAX_NUM_CHANNELS];
OpusCustomDecoder* Opus64DecoderStereo[MAX_NUM_CHANNELS];
OpusCustomMode* OpusMode[MAX_NUM_CHANNELS];
OpusCustomEncoder* OpusEncoderMono[MAX_NUM_CHANNELS];
OpusCustomDecoder* OpusDecoderMono[MAX_NUM_CHANNELS];
OpusCustomEncoder* OpusEncoderStereo[MAX_NUM_CHANNELS];
OpusCustomDecoder* OpusDecoderStereo[MAX_NUM_CHANNELS];
CConvBuf<int16_t> DoubleFrameSizeConvBufIn[MAX_NUM_CHANNELS];
CConvBuf<int16_t> DoubleFrameSizeConvBufOut[MAX_NUM_CHANNELS];
CVector<QString> vstrChatColors;
CVector<int> vecChanIDsCurConChan;
CVector<CVector<float>> vecvecfGains;
CVector<CVector<float>> vecvecfPannings;
CVector<CVector<int16_t>> vecvecsData;
CVector<CVector<int16_t>> vecvecsData2;
CVector<int> vecNumAudioChannels;
CVector<int> vecNumFrameSizeConvBlocks;
CVector<int> vecUseDoubleSysFraSizeConvBuf;
CVector<EAudComprType> vecAudioComprType;
CVector<CVector<int16_t>> vecvecsSendData;
CVector<CVector<float>> vecvecfIntermediateProcBuf;
CVector<CVector<uint8_t>> vecvecbyCodedData;
// Channel levels
CVector<uint16_t> vecChannelLevels;
// actual working objects
CHighPrioSocket Socket;
// logging
CServerLogging Logging;
// channel level update frame interval counter
int iFrameCount;
// HTML file server status
bool bWriteStatusHTMLFile;
QString strServerHTMLFileListName;
CHighPrecisionTimer HighPrecisionTimer;
// server list
CServerListManager ServerListManager;
// jam recorder
recorder::CJamController JamController;
bool bDisableRecording;
// GUI settings
bool bAutoRunMinimized;
// for delay panning
bool bDelayPan;
// enable IPv6
bool bEnableIPv6;
// messaging
QString strWelcomeMessage;
ELicenceType eLicenceType;
bool bDisconnectAllClientsOnQuit;
CSignalHandler* pSignalHandler;
std::unique_ptr<CThreadPool> pThreadPool;
signals:
void Started();
void Stopped();
void ClientDisconnected ( const int iChID );
void SvrRegStatusChanged();
void AudioFrame ( const int iChID,
const QString stChName,
const CHostAddress RecHostAddr,
const int iNumAudChan,
const CVector<int16_t> vecsData );
void CLVersionAndOSReceived ( CHostAddress InetAddr, COSUtil::EOpSystemType eOSType, QString strVersion );
// pass through from jam controller
void RestartRecorder();
void StopRecorder();
void RecordingSessionStarted ( QString sessionDir );
void EndRecorderThread();
public slots:
void OnTimer();
void OnNewConnection ( int iChID, int iTotChans, CHostAddress RecHostAddr );
void OnServerFull ( CHostAddress RecHostAddr );
void OnSendCLProtMessage ( CHostAddress InetAddr, CVector<uint8_t> vecMessage );
void OnProtocolCLMessageReceived ( int iRecID, CVector<uint8_t> vecbyMesBodyData, CHostAddress RecHostAddr );
void OnProtocolMessageReceived ( int iRecCounter, int iRecID, CVector<uint8_t> vecbyMesBodyData, CHostAddress RecHostAddr );
void OnCLPingReceived ( CHostAddress InetAddr, int iMs ) { ConnLessProtocol.CreateCLPingMes ( InetAddr, iMs ); }
void OnCLPingWithNumClientsReceived ( CHostAddress InetAddr, int iMs, int )
{
ConnLessProtocol.CreateCLPingWithNumClientsMes ( InetAddr, iMs, GetNumberOfConnectedClients() );
}
void OnCLSendEmptyMes ( CHostAddress TargetInetAddr )
{
// only send empty message if not a directory
if ( !ServerListManager.IsDirectory() )
{
ConnLessProtocol.CreateCLEmptyMes ( TargetInetAddr );
}
}
void OnCLReqServerList ( CHostAddress InetAddr ) { ServerListManager.RetrieveAll ( InetAddr ); }
void OnCLReqVersionAndOS ( CHostAddress InetAddr ) { ConnLessProtocol.CreateCLVersionAndOSMes ( InetAddr ); }
void OnCLReqConnClientsList ( CHostAddress InetAddr ) { ConnLessProtocol.CreateCLConnClientsListMes ( InetAddr, CreateChannelList() ); }
void OnCLRegisterServerReceived ( CHostAddress InetAddr, CHostAddress LInetAddr, CServerCoreInfo ServerInfo )
{
ServerListManager.Append ( InetAddr, LInetAddr, ServerInfo );
}
void OnCLRegisterServerExReceived ( CHostAddress InetAddr,
CHostAddress LInetAddr,
CServerCoreInfo ServerInfo,
COSUtil::EOpSystemType,
QString strVersion )
{
ServerListManager.Append ( InetAddr, LInetAddr, ServerInfo, strVersion );
}
void OnCLRegisterServerResp ( CHostAddress /* unused */, ESvrRegResult eResult ) { ServerListManager.StoreRegistrationResult ( eResult ); }
void OnCLUnregisterServerReceived ( CHostAddress InetAddr ) { ServerListManager.Remove ( InetAddr ); }
void OnCLDisconnection ( CHostAddress InetAddr );
void OnAboutToQuit();
void OnHandledSignal ( int sigNum );
};
Q_DECLARE_METATYPE ( CVector<int16_t> )