1 /******************************************************************************\
2 * Copyright (c) 2004-2020
3 *
4 * Author(s):
5 * Volker Fischer
6 *
7 ******************************************************************************
8 *
9 * This program is free software; you can redistribute it and/or modify it under
10 * the terms of the GNU General Public License as published by the Free Software
11 * Foundation; either version 2 of the License, or (at your option) any later
12 * version.
13 *
14 * This program is distributed in the hope that it will be useful, but WITHOUT
15 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
16 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
17 * details.
18 *
19 * You should have received a copy of the GNU General Public License along with
20 * this program; if not, write to the Free Software Foundation, Inc.,
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
22 *
23 \******************************************************************************/
24
25 #pragma once
26
27 #include <QThread>
28 #include <QDateTime>
29 #include <QFile>
30 #if QT_VERSION >= QT_VERSION_CHECK( 5, 6, 0 )
31 # include <QVersionNumber>
32 #endif
33 #include "global.h"
34 #include "buffer.h"
35 #include "util.h"
36 #include "protocol.h"
37 #include "socket.h"
38
39 /* Definitions ****************************************************************/
40 // set the time-out for the input buffer until the state changes from
41 // connected to not connected (the actual time depends on the way the error
42 // correction is implemented)
43 #define CON_TIME_OUT_SEC_MAX 30 // seconds
44
45 // number of frames for audio fade-in, 48 kHz, x samples: 3 sec / (x samples / 48 kHz)
46 #define FADE_IN_NUM_FRAMES 2250
47 #define FADE_IN_NUM_FRAMES_DBLE_FRAMESIZE 1125
48
49 enum EPutDataStat
50 {
51 PS_GEN_ERROR,
52 PS_AUDIO_OK,
53 PS_AUDIO_ERR,
54 PS_AUDIO_INVALID,
55 PS_PROT_OK,
56 PS_PROT_OK_MESS_NOT_EVALUATED,
57 PS_PROT_ERR,
58 PS_NEW_CONNECTION
59 };
60
61 /* Classes ********************************************************************/
62 class CChannel : public QObject
63 {
64 Q_OBJECT
65
66 public:
67 // we have to make "server" the default since I do not see a chance to
68 // use constructor initialization in the server for a vector of channels
69 CChannel ( const bool bNIsServer = true );
70
71 void PutProtcolData ( const int iRecCounter, const int iRecID, const CVector<uint8_t>& vecbyMesBodyData, const CHostAddress& RecHostAddr );
72
73 EPutDataStat PutAudioData ( const CVector<uint8_t>& vecbyData, const int iNumBytes, CHostAddress RecHostAddr );
74
75 EGetDataStat GetData ( CVector<uint8_t>& vecbyData, const int iNumBytes );
76
77 void PrepAndSendPacket ( CHighPrioSocket* pSocket, const CVector<uint8_t>& vecbyNPacket, const int iNPacketLen );
78
ResetTimeOutCounter()79 void ResetTimeOutCounter() { iConTimeOut = iConTimeOutStartVal; }
IsConnected()80 bool IsConnected() const { return iConTimeOut > 0; }
81 void Disconnect();
82
83 void SetEnable ( const bool bNEnStat );
IsEnabled()84 bool IsEnabled() { return bIsEnabled; }
85
SetAddress(const CHostAddress NAddr)86 void SetAddress ( const CHostAddress NAddr ) { InetAddr = NAddr; }
GetAddress()87 const CHostAddress& GetAddress() const { return InetAddr; }
88
ResetInfo()89 void ResetInfo()
90 {
91 bIsIdentified = false;
92 ChannelInfo = CChannelCoreInfo();
93 } // reset does not emit a message
94 QString GetName();
95 void SetChanInfo ( const CChannelCoreInfo& NChanInf );
GetChanInfo()96 CChannelCoreInfo& GetChanInfo() { return ChannelInfo; }
97
SetRemoteInfo(const CChannelCoreInfo ChInfo)98 void SetRemoteInfo ( const CChannelCoreInfo ChInfo ) { Protocol.CreateChanInfoMes ( ChInfo ); }
99
CreateReqChanInfoMes()100 void CreateReqChanInfoMes() { Protocol.CreateReqChanInfoMes(); }
CreateVersionAndOSMes()101 void CreateVersionAndOSMes() { Protocol.CreateVersionAndOSMes(); }
CreateMuteStateHasChangedMes(const int iChanID,const bool bIsMuted)102 void CreateMuteStateHasChangedMes ( const int iChanID, const bool bIsMuted ) { Protocol.CreateMuteStateHasChangedMes ( iChanID, bIsMuted ); }
103
104 void SetGain ( const int iChanID, const float fNewGain );
105 float GetGain ( const int iChanID );
GetFadeInGain()106 float GetFadeInGain() { return static_cast<float> ( iFadeInCnt ) / iFadeInCntMax; }
107
108 void SetPan ( const int iChanID, const float fNewPan );
109 float GetPan ( const int iChanID );
110
SetRemoteChanGain(const int iId,const float fGain)111 void SetRemoteChanGain ( const int iId, const float fGain ) { Protocol.CreateChanGainMes ( iId, fGain ); }
112
SetRemoteChanPan(const int iId,const float fPan)113 void SetRemoteChanPan ( const int iId, const float fPan ) { Protocol.CreateChanPanMes ( iId, fPan ); }
114
115 bool SetSockBufNumFrames ( const int iNewNumFrames, const bool bPreserve = false );
GetSockBufNumFrames()116 int GetSockBufNumFrames() const { return iCurSockBufNumFrames; }
117
118 void UpdateSocketBufferSize();
119
120 int GetUploadRateKbps();
121
122 // set/get network out buffer size and size factor
123 void SetAudioStreamProperties ( const EAudComprType eNewAudComprType,
124 const int iNewNetwFrameSize,
125 const int iNewNetwFrameSizeFact,
126 const int iNewNumAudioChannels );
127
SetDoAutoSockBufSize(const bool bValue)128 void SetDoAutoSockBufSize ( const bool bValue ) { bDoAutoSockBufSize = bValue; }
129
GetDoAutoSockBufSize()130 bool GetDoAutoSockBufSize() const { return bDoAutoSockBufSize; }
131
GetNetwFrameSizeFact()132 int GetNetwFrameSizeFact() const { return iNetwFrameSizeFact; }
GetCeltNumCodedBytes()133 int GetCeltNumCodedBytes() const { return iCeltNumCodedBytes; }
134
GetBufErrorRates(CVector<double> & vecErrRates,double & dLimit,double & dMaxUpLimit)135 void GetBufErrorRates ( CVector<double>& vecErrRates, double& dLimit, double& dMaxUpLimit )
136 {
137 SockBuf.GetErrorRates ( vecErrRates, dLimit, dMaxUpLimit );
138 }
139
GetAudioCompressionType()140 EAudComprType GetAudioCompressionType() { return eAudioCompressionType; }
GetNumAudioChannels()141 int GetNumAudioChannels() const { return iNumAudioChannels; }
142
143 // network protocol interface
CreateJitBufMes(const int iJitBufSize)144 void CreateJitBufMes ( const int iJitBufSize )
145 {
146 if ( ProtocolIsEnabled() )
147 {
148 Protocol.CreateJitBufMes ( iJitBufSize );
149 }
150 }
CreateClientIDMes(const int iChanID)151 void CreateClientIDMes ( const int iChanID ) { Protocol.CreateClientIDMes ( iChanID ); }
CreateReqNetwTranspPropsMes()152 void CreateReqNetwTranspPropsMes() { Protocol.CreateReqNetwTranspPropsMes(); }
CreateReqSplitMessSupportMes()153 void CreateReqSplitMessSupportMes() { Protocol.CreateReqSplitMessSupportMes(); }
CreateReqJitBufMes()154 void CreateReqJitBufMes() { Protocol.CreateReqJitBufMes(); }
CreateReqConnClientsList()155 void CreateReqConnClientsList() { Protocol.CreateReqConnClientsList(); }
CreateChatTextMes(const QString & strChatText)156 void CreateChatTextMes ( const QString& strChatText ) { Protocol.CreateChatTextMes ( strChatText ); }
CreateLicReqMes(const ELicenceType eLicenceType)157 void CreateLicReqMes ( const ELicenceType eLicenceType ) { Protocol.CreateLicenceRequiredMes ( eLicenceType ); }
158
159 // clang-format off
160 // TODO needed for compatibility to old servers >= 3.4.6 and <= 3.5.12
CreateReqChannelLevelListMes()161 void CreateReqChannelLevelListMes() { Protocol.CreateReqChannelLevelListMes(); }
162 // clang-format on
163
CreateConClientListMes(const CVector<CChannelInfo> & vecChanInfo)164 void CreateConClientListMes ( const CVector<CChannelInfo>& vecChanInfo ) { Protocol.CreateConClientListMes ( vecChanInfo ); }
165
CreateRecorderStateMes(const ERecorderState eRecorderState)166 void CreateRecorderStateMes ( const ERecorderState eRecorderState ) { Protocol.CreateRecorderStateMes ( eRecorderState ); }
167
168 CNetworkTransportProps GetNetworkTransportPropsFromCurrentSettings();
169
170 double UpdateAndGetLevelForMeterdB ( const CVector<short>& vecsAudio, const int iInSize, const bool bIsStereoIn );
171
172 protected:
173 bool ProtocolIsEnabled();
174
ResetNetworkTransportProperties()175 void ResetNetworkTransportProperties()
176 {
177 // set it to a state were no decoding is ever possible (since we want
178 // only to decode data when a network transport property message is
179 // received with the correct values)
180 eAudioCompressionType = CT_NONE;
181 iNetwFrameSizeFact = FRAME_SIZE_FACTOR_PREFERRED;
182 iNetwFrameSize = CELT_MINIMUM_NUM_BYTES;
183 iCeltNumCodedBytes = CELT_MINIMUM_NUM_BYTES;
184 iNumAudioChannels = 1; // mono
185 bUseSequenceNumber = false;
186 }
187
188 // connection parameters
189 CHostAddress InetAddr;
190
191 // channel info
192 CChannelCoreInfo ChannelInfo;
193
194 // mixer and effect settings
195 CVector<float> vecfGains;
196 CVector<float> vecfPannings;
197
198 // network jitter-buffer
199 CNetBufWithStats SockBuf;
200 int iCurSockBufNumFrames;
201 bool bDoAutoSockBufSize;
202 bool bUseSequenceNumber;
203 uint8_t iSendSequenceNumber;
204
205 // network output conversion buffer
206 CConvBuf<uint8_t> ConvBuf;
207
208 // network protocol
209 CProtocol Protocol;
210
211 int iConTimeOut;
212 int iConTimeOutStartVal;
213 int iFadeInCnt;
214 int iFadeInCntMax;
215
216 bool bIsEnabled;
217 bool bIsServer;
218 bool bIsIdentified;
219
220 int iNetwFrameSizeFact;
221 int iNetwFrameSize;
222 int iCeltNumCodedBytes;
223 int iAudioFrameSizeSamples;
224
225 EAudComprType eAudioCompressionType;
226 int iNumAudioChannels;
227
228 QMutex Mutex;
229 QMutex MutexSocketBuf;
230 QMutex MutexConvBuf;
231
232 CStereoSignalLevelMeter SignalLevelMeter;
233
234 public slots:
235 void OnSendProtMessage ( CVector<uint8_t> vecMessage );
236 void OnJittBufSizeChange ( int iNewJitBufSize );
237 void OnChangeChanGain ( int iChanID, float fNewGain );
238 void OnChangeChanPan ( int iChanID, float fNewPan );
239 void OnChangeChanInfo ( CChannelCoreInfo ChanInfo );
240 void OnNetTranspPropsReceived ( CNetworkTransportProps NetworkTransportProps );
241 void OnReqNetTranspProps();
242 void OnReqSplitMessSupport();
OnSplitMessSupported()243 void OnSplitMessSupported() { Protocol.SetSplitMessageSupported ( true ); }
244
245 void OnVersionAndOSReceived ( COSUtil::EOpSystemType eOSType, QString strVersion );
246
OnParseMessageBody(CVector<uint8_t> vecbyMesBodyData,int iRecCounter,int iRecID)247 void OnParseMessageBody ( CVector<uint8_t> vecbyMesBodyData, int iRecCounter, int iRecID )
248 {
249 // note that the return value is ignored here
250 Protocol.ParseMessageBody ( vecbyMesBodyData, iRecCounter, iRecID );
251 }
252
OnProtcolMessageReceived(int iRecCounter,int iRecID,CVector<uint8_t> vecbyMesBodyData,CHostAddress RecHostAddr)253 void OnProtcolMessageReceived ( int iRecCounter, int iRecID, CVector<uint8_t> vecbyMesBodyData, CHostAddress RecHostAddr )
254 {
255 PutProtcolData ( iRecCounter, iRecID, vecbyMesBodyData, RecHostAddr );
256 }
257
OnProtcolCLMessageReceived(int iRecID,CVector<uint8_t> vecbyMesBodyData,CHostAddress RecHostAddr)258 void OnProtcolCLMessageReceived ( int iRecID, CVector<uint8_t> vecbyMesBodyData, CHostAddress RecHostAddr )
259 {
260 emit DetectedCLMessage ( vecbyMesBodyData, iRecID, RecHostAddr );
261 }
262
OnNewConnection()263 void OnNewConnection() { emit NewConnection(); }
264
265 signals:
266 void MessReadyForSending ( CVector<uint8_t> vecMessage );
267 void NewConnection();
268 void ReqJittBufSize();
269 void JittBufSizeChanged ( int iNewJitBufSize );
270 void ServerAutoSockBufSizeChange ( int iNNumFra );
271 void ReqConnClientsList();
272 void ConClientListMesReceived ( CVector<CChannelInfo> vecChanInfo );
273 void ChanInfoHasChanged();
274 void ClientIDReceived ( int iChanID );
275 void MuteStateHasChanged ( int iChanID, bool bIsMuted );
276 void MuteStateHasChangedReceived ( int iChanID, bool bIsMuted );
277 void ReqChanInfo();
278 void ChatTextReceived ( QString strChatText );
279 void ReqNetTranspProps();
280 void LicenceRequired ( ELicenceType eLicenceType );
281 void VersionAndOSReceived ( COSUtil::EOpSystemType eOSType, QString strVersion );
282 void RecorderStateReceived ( ERecorderState eRecorderState );
283 void Disconnected();
284
285 void DetectedCLMessage ( CVector<uint8_t> vecbyMesBodyData, int iRecID, CHostAddress RecHostAddr );
286
287 void ParseMessageBody ( CVector<uint8_t> vecbyMesBodyData, int iRecCounter, int iRecID );
288 };
289