-
Notifications
You must be signed in to change notification settings - Fork 226
/
Copy pathclient.cpp
1676 lines (1394 loc) · 59.2 KB
/
client.cpp
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
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/******************************************************************************\
* Copyright (c) 2004-2024
*
* 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
*
\******************************************************************************/
#include "client.h"
/* Implementation *************************************************************/
CClient::CClient ( const quint16 iPortNumber,
const quint16 iQosNumber,
const QString& strConnOnStartupAddress,
const QString& strMIDISetup,
const bool bNoAutoJackConnect,
const QString& strNClientName,
const bool bNEnableIPv6,
const bool bNMuteMeInPersonalMix ) :
ChannelInfo(),
strClientName ( strNClientName ),
Channel ( false ), /* we need a client channel -> "false" */
CurOpusEncoder ( nullptr ),
CurOpusDecoder ( nullptr ),
eAudioCompressionType ( CT_OPUS ),
iCeltNumCodedBytes ( OPUS_NUM_BYTES_MONO_LOW_QUALITY ),
iOPUSFrameSizeSamples ( DOUBLE_SYSTEM_FRAME_SIZE_SAMPLES ),
eAudioQuality ( AQ_NORMAL ),
eAudioChannelConf ( CC_MONO ),
iNumAudioChannels ( 1 ),
bIsInitializationPhase ( true ),
bMuteOutStream ( false ),
fMuteOutStreamGain ( 1.0f ),
Socket ( &Channel, iPortNumber, iQosNumber, "", bNEnableIPv6 ),
Sound ( AudioCallback, this, strMIDISetup, bNoAutoJackConnect, strNClientName ),
iAudioInFader ( AUD_FADER_IN_MIDDLE ),
bReverbOnLeftChan ( false ),
iReverbLevel ( 0 ),
iInputBoost ( 1 ),
iSndCrdPrefFrameSizeFactor ( FRAME_SIZE_FACTOR_DEFAULT ),
iSndCrdFrameSizeFactor ( FRAME_SIZE_FACTOR_DEFAULT ),
bSndCrdConversionBufferRequired ( false ),
iSndCardMonoBlockSizeSamConvBuff ( 0 ),
bFraSiFactPrefSupported ( false ),
bFraSiFactDefSupported ( false ),
bFraSiFactSafeSupported ( false ),
eGUIDesign ( GD_ORIGINAL ),
eMeterStyle ( MT_LED_STRIPE ),
bEnableAudioAlerts ( false ),
bEnableOPUS64 ( false ),
bJitterBufferOK ( true ),
bEnableIPv6 ( bNEnableIPv6 ),
bMuteMeInPersonalMix ( bNMuteMeInPersonalMix ),
iServerSockBufNumFrames ( DEF_NET_BUF_SIZE_NUM_BL ),
pSignalHandler ( CSignalHandler::getSingletonP() )
{
int iOpusError;
OpusMode = opus_custom_mode_create ( SYSTEM_SAMPLE_RATE_HZ, DOUBLE_SYSTEM_FRAME_SIZE_SAMPLES, &iOpusError );
Opus64Mode = opus_custom_mode_create ( SYSTEM_SAMPLE_RATE_HZ, SYSTEM_FRAME_SIZE_SAMPLES, &iOpusError );
// init audio encoders and decoders
OpusEncoderMono = opus_custom_encoder_create ( OpusMode, 1, &iOpusError ); // mono encoder legacy
OpusDecoderMono = opus_custom_decoder_create ( OpusMode, 1, &iOpusError ); // mono decoder legacy
OpusEncoderStereo = opus_custom_encoder_create ( OpusMode, 2, &iOpusError ); // stereo encoder legacy
OpusDecoderStereo = opus_custom_decoder_create ( OpusMode, 2, &iOpusError ); // stereo decoder legacy
Opus64EncoderMono = opus_custom_encoder_create ( Opus64Mode, 1, &iOpusError ); // mono encoder OPUS64
Opus64DecoderMono = opus_custom_decoder_create ( Opus64Mode, 1, &iOpusError ); // mono decoder OPUS64
Opus64EncoderStereo = opus_custom_encoder_create ( Opus64Mode, 2, &iOpusError ); // stereo encoder OPUS64
Opus64DecoderStereo = opus_custom_decoder_create ( Opus64Mode, 2, &iOpusError ); // stereo decoder OPUS64
// we require a constant bit rate
opus_custom_encoder_ctl ( OpusEncoderMono, OPUS_SET_VBR ( 0 ) );
opus_custom_encoder_ctl ( OpusEncoderStereo, OPUS_SET_VBR ( 0 ) );
opus_custom_encoder_ctl ( Opus64EncoderMono, OPUS_SET_VBR ( 0 ) );
opus_custom_encoder_ctl ( Opus64EncoderStereo, OPUS_SET_VBR ( 0 ) );
// for 64 samples frame size we have to adjust the PLC behavior to avoid loud artifacts
opus_custom_encoder_ctl ( Opus64EncoderMono, OPUS_SET_PACKET_LOSS_PERC ( 35 ) );
opus_custom_encoder_ctl ( Opus64EncoderStereo, OPUS_SET_PACKET_LOSS_PERC ( 35 ) );
// we want as low delay as possible
opus_custom_encoder_ctl ( OpusEncoderMono, OPUS_SET_APPLICATION ( OPUS_APPLICATION_RESTRICTED_LOWDELAY ) );
opus_custom_encoder_ctl ( OpusEncoderStereo, OPUS_SET_APPLICATION ( OPUS_APPLICATION_RESTRICTED_LOWDELAY ) );
opus_custom_encoder_ctl ( Opus64EncoderMono, OPUS_SET_APPLICATION ( OPUS_APPLICATION_RESTRICTED_LOWDELAY ) );
opus_custom_encoder_ctl ( Opus64EncoderStereo, OPUS_SET_APPLICATION ( OPUS_APPLICATION_RESTRICTED_LOWDELAY ) );
// set encoder low complexity for legacy 128 samples frame size
opus_custom_encoder_ctl ( OpusEncoderMono, OPUS_SET_COMPLEXITY ( 1 ) );
opus_custom_encoder_ctl ( OpusEncoderStereo, OPUS_SET_COMPLEXITY ( 1 ) );
// Connections -------------------------------------------------------------
// connections for the protocol mechanism
QObject::connect ( &Channel, &CChannel::MessReadyForSending, this, &CClient::OnSendProtMessage );
QObject::connect ( &Channel, &CChannel::DetectedCLMessage, this, &CClient::OnDetectedCLMessage );
QObject::connect ( &Channel, &CChannel::ReqJittBufSize, this, &CClient::OnReqJittBufSize );
QObject::connect ( &Channel, &CChannel::JittBufSizeChanged, this, &CClient::OnJittBufSizeChanged );
QObject::connect ( &Channel, &CChannel::ReqChanInfo, this, &CClient::OnReqChanInfo );
// The first ConClientListMesReceived handler performs the necessary cleanup and has to run first:
QObject::connect ( &Channel, &CChannel::ConClientListMesReceived, this, &CClient::OnConClientListMesReceived );
QObject::connect ( &Channel, &CChannel::Disconnected, this, &CClient::Disconnected );
QObject::connect ( &Channel, &CChannel::NewConnection, this, &CClient::OnNewConnection );
QObject::connect ( &Channel, &CChannel::ChatTextReceived, this, &CClient::ChatTextReceived );
QObject::connect ( &Channel, &CChannel::ClientIDReceived, this, &CClient::OnClientIDReceived );
QObject::connect ( &Channel, &CChannel::MuteStateHasChangedReceived, this, &CClient::OnMuteStateHasChangedReceived );
QObject::connect ( &Channel, &CChannel::LicenceRequired, this, &CClient::LicenceRequired );
QObject::connect ( &Channel, &CChannel::VersionAndOSReceived, this, &CClient::VersionAndOSReceived );
QObject::connect ( &Channel, &CChannel::RecorderStateReceived, this, &CClient::RecorderStateReceived );
QObject::connect ( &ConnLessProtocol, &CProtocol::CLMessReadyForSending, this, &CClient::OnSendCLProtMessage );
QObject::connect ( &ConnLessProtocol, &CProtocol::CLServerListReceived, this, &CClient::CLServerListReceived );
QObject::connect ( &ConnLessProtocol, &CProtocol::CLRedServerListReceived, this, &CClient::CLRedServerListReceived );
QObject::connect ( &ConnLessProtocol, &CProtocol::CLConnClientsListMesReceived, this, &CClient::CLConnClientsListMesReceived );
QObject::connect ( &ConnLessProtocol, &CProtocol::CLPingReceived, this, &CClient::OnCLPingReceived );
QObject::connect ( &ConnLessProtocol, &CProtocol::CLPingWithNumClientsReceived, this, &CClient::OnCLPingWithNumClientsReceived );
QObject::connect ( &ConnLessProtocol, &CProtocol::CLDisconnection, this, &CClient::OnCLDisconnection );
QObject::connect ( &ConnLessProtocol, &CProtocol::CLVersionAndOSReceived, this, &CClient::CLVersionAndOSReceived );
QObject::connect ( &ConnLessProtocol, &CProtocol::CLChannelLevelListReceived, this, &CClient::OnCLChannelLevelListReceived );
// other
QObject::connect ( &Sound, &CSound::ReinitRequest, this, &CClient::OnSndCrdReinitRequest );
QObject::connect ( &Sound, &CSound::ControllerInFaderLevel, this, &CClient::OnControllerInFaderLevel );
QObject::connect ( &Sound, &CSound::ControllerInPanValue, this, &CClient::OnControllerInPanValue );
QObject::connect ( &Sound, &CSound::ControllerInFaderIsSolo, this, &CClient::OnControllerInFaderIsSolo );
QObject::connect ( &Sound, &CSound::ControllerInFaderIsMute, this, &CClient::OnControllerInFaderIsMute );
QObject::connect ( &Sound, &CSound::ControllerInMuteMyself, this, &CClient::OnControllerInMuteMyself );
QObject::connect ( &Socket, &CHighPrioSocket::InvalidPacketReceived, this, &CClient::OnInvalidPacketReceived );
QObject::connect ( pSignalHandler, &CSignalHandler::HandledSignal, this, &CClient::OnHandledSignal );
// start timer so that elapsed time works
PreciseTime.start();
// set gain delay timer to single-shot and connect handler function
TimerGainOrPan.setSingleShot ( true );
QObject::connect ( &TimerGainOrPan, &QTimer::timeout, this, &CClient::OnTimerRemoteChanGainOrPan );
// start the socket (it is important to start the socket after all
// initializations and connections)
Socket.Start();
// do an immediate start if a server address is given
if ( !strConnOnStartupAddress.isEmpty() )
{
SetServerAddr ( strConnOnStartupAddress );
Start();
}
}
CClient::~CClient()
{
// if we were running, stop sound device
if ( Sound.IsRunning() )
{
Sound.Stop();
}
// free audio encoders and decoders
opus_custom_encoder_destroy ( OpusEncoderMono );
opus_custom_decoder_destroy ( OpusDecoderMono );
opus_custom_encoder_destroy ( OpusEncoderStereo );
opus_custom_decoder_destroy ( OpusDecoderStereo );
opus_custom_encoder_destroy ( Opus64EncoderMono );
opus_custom_decoder_destroy ( Opus64DecoderMono );
opus_custom_encoder_destroy ( Opus64EncoderStereo );
opus_custom_decoder_destroy ( Opus64DecoderStereo );
// free audio modes
opus_custom_mode_destroy ( OpusMode );
opus_custom_mode_destroy ( Opus64Mode );
}
void CClient::OnSendProtMessage ( CVector<uint8_t> vecMessage )
{
// the protocol queries me to call the function to send the message
// send it through the network
Socket.SendPacket ( vecMessage, Channel.GetAddress() );
}
void CClient::OnSendCLProtMessage ( CHostAddress InetAddr, CVector<uint8_t> vecMessage )
{
// the protocol queries me to call the function to send the message
// send it through the network
Socket.SendPacket ( vecMessage, InetAddr );
}
void CClient::OnInvalidPacketReceived ( CHostAddress RecHostAddr )
{
// message could not be parsed, check if the packet comes
// from the server we just connected -> if yes, send
// disconnect message since the server may not know that we
// are not connected anymore
if ( Channel.GetAddress() == RecHostAddr )
{
ConnLessProtocol.CreateCLDisconnection ( RecHostAddr );
}
}
void CClient::OnDetectedCLMessage ( CVector<uint8_t> vecbyMesBodyData, int iRecID, CHostAddress RecHostAddr )
{
// connection less messages are always processed
ConnLessProtocol.ParseConnectionLessMessageBody ( vecbyMesBodyData, iRecID, RecHostAddr );
}
void CClient::OnJittBufSizeChanged ( int iNewJitBufSize )
{
// we received a jitter buffer size changed message from the server,
// only apply this value if auto jitter buffer size is enabled
if ( GetDoAutoSockBufSize() )
{
// Note: Do not use the "SetServerSockBufNumFrames" function for setting
// the new server jitter buffer size since then a message would be sent
// to the server which is incorrect.
iServerSockBufNumFrames = iNewJitBufSize;
}
}
void CClient::OnNewConnection()
{
// a new connection was successfully initiated, send infos and request
// connected clients list
Channel.SetRemoteInfo ( ChannelInfo );
// We have to send a connected clients list request since it can happen
// that we just had connected to the server and then disconnected but
// the server still thinks that we are connected (the server is still
// waiting for the channel time-out). If we now connect again, we would
// not get the list because the server does not know about a new connection.
// Same problem is with the jitter buffer message.
Channel.CreateReqConnClientsList();
CreateServerJitterBufferMessage();
//### TODO: BEGIN ###//
// needed for compatibility to old servers >= 3.4.6 and <= 3.5.12
Channel.CreateReqChannelLevelListMes();
//### TODO: END ###//
}
void CClient::OnMuteStateHasChangedReceived ( int iServerChanID, bool bIsMuted )
{
// map iChanID from server channel ID to client channel ID
int iChanID = FindClientChannel ( iServerChanID, false );
if ( iChanID != INVALID_INDEX )
{
emit MuteStateHasChangedReceived ( iChanID, bIsMuted );
}
}
void CClient::OnCLChannelLevelListReceived ( CHostAddress InetAddr, CVector<uint16_t> vecLevelList )
{
// reorder levels from server channel order to client channel order
if ( ReorderLevelList ( vecLevelList ) )
{
emit CLChannelLevelListReceived ( InetAddr, vecLevelList );
}
}
void CClient::OnConClientListMesReceived ( CVector<CChannelInfo> vecChanInfo )
{
// translate from server channel IDs to client channel IDs
// ALSO here is where we allocate and free client channels as required
const int iNumConnectedClients = vecChanInfo.Size();
int i, iSrvIdx;
// on a new connection, a server sends an empty channel list before sending
// the real channel list (see #1010). To avoid this discarding "our" channel
// that we have just created, we skip this processing and just pass the empty
// list to the emitted signal.
if ( iNumConnectedClients != 0 )
{
// this relies on the received client list being in order of server channel ID
for ( i = 0, iSrvIdx = 0; i < iNumConnectedClients && iSrvIdx < MAX_NUM_CHANNELS; )
{
// server channel ID of this entry
const int iServerChannelID = vecChanInfo[i].iChanID;
// find matching client channel ID, creating new if necessary,
// update channel number to be client-side
vecChanInfo[i].iChanID = FindClientChannel ( iServerChannelID, true );
// discard any lower server channels that are no longer in our local list
while ( iSrvIdx < iServerChannelID )
{
const int iId = FindClientChannel ( iSrvIdx, false );
if ( iId != INVALID_INDEX )
{
// iSrvIdx contains a server channel number that has now gone
FreeClientChannel ( iSrvIdx );
}
iSrvIdx++;
}
i++; // next list entry
iSrvIdx++; // next local server channel
}
// have now run out of active channels, discard any remaining from our local list
// note that iActiveChannels will reduce as remaining channels are freed
while ( iActiveChannels > iNumConnectedClients && iSrvIdx < MAX_NUM_CHANNELS )
{
const int iId = FindClientChannel ( iSrvIdx, false );
if ( iId != INVALID_INDEX )
{
// iSrvIdx contains a server channel number that has now gone
FreeClientChannel ( iSrvIdx );
}
iSrvIdx++;
}
Q_ASSERT ( iActiveChannels == iNumConnectedClients );
}
// pass the received list onwards, now containing client channel IDs
emit ConClientListMesReceived ( vecChanInfo );
}
void CClient::CreateServerJitterBufferMessage()
{
// per definition in the client: if auto jitter buffer is enabled, both,
// the client and server shall use an auto jitter buffer
if ( GetDoAutoSockBufSize() )
{
// in case auto jitter buffer size is enabled, we have to transmit a
// special value
Channel.CreateJitBufMes ( AUTO_NET_BUF_SIZE_FOR_PROTOCOL );
}
else
{
Channel.CreateJitBufMes ( GetServerSockBufNumFrames() );
}
}
void CClient::OnCLPingReceived ( CHostAddress InetAddr, int iMs )
{
// make sure we are running and the server address is correct
if ( IsRunning() && ( InetAddr == Channel.GetAddress() ) )
{
// take care of wrap arounds (if wrapping, do not use result)
const int iCurDiff = EvaluatePingMessage ( iMs );
if ( iCurDiff >= 0 )
{
iCurPingTime = iCurDiff; // store for use by gain message sending
emit PingTimeReceived ( iCurDiff );
}
}
}
void CClient::OnCLPingWithNumClientsReceived ( CHostAddress InetAddr, int iMs, int iNumClients )
{
// take care of wrap arounds (if wrapping, do not use result)
const int iCurDiff = EvaluatePingMessage ( iMs );
if ( iCurDiff >= 0 )
{
emit CLPingTimeWithNumClientsReceived ( InetAddr, iCurDiff, iNumClients );
}
}
int CClient::PreparePingMessage()
{
// transmit the current precise time (in ms)
return PreciseTime.elapsed();
}
int CClient::EvaluatePingMessage ( const int iMs )
{
// calculate difference between received time in ms and current time in ms
return PreciseTime.elapsed() - iMs;
}
void CClient::SetDoAutoSockBufSize ( const bool bValue )
{
// first, set new value in the channel object
Channel.SetDoAutoSockBufSize ( bValue );
// inform the server about the change
CreateServerJitterBufferMessage();
}
// In order not to flood the server with gain or pan change messages, particularly when using
// a MIDI controller, a timer is used to limit the rate at which such messages are generated.
// This avoids a potential long backlog of messages, since each must be ACKed before the next
// can be sent, and this ACK is subject to the latency of the server connection.
//
// When the first gain or pan change message is requested after an idle period (i.e. the timer is not
// running), it will be sent immediately, and a timer started. The timer period is dependent on
// the current ping time to the remote server.
//
// If a gain or pan change message is requested while the timer is still running, the new value is not sent,
// but just stored in newGain or newPan within clientChannels[iId], and the minGainOrPanId and maxGainOrPanId
// updated to note the range of IDs that must be checked when the time expires (this will usually be a single
// channel unless channel grouping is being used). This avoids having to check all possible channels.
//
// When the timer fires, the channels minGainOrPanId <= iId < maxGainOrPanId are checked by comparing the
// last sent values in oldGain or oldPan with any pending values in newGain or newPan, and if they differ,
// the new value is sent, updating oldGain or oldPan with the sent value. If any new values are sent,
// the timer is restarted so that further immediate updates will be pended.
void CClient::SetRemoteChanGain ( const int iId, const float fGain, const bool bIsMyOwnFader )
{
QMutexLocker locker ( &MutexGainOrPan );
CClientChannel* clientChan = &clientChannels[iId];
// if this gain is for my own channel, apply the value for the Mute Myself function
if ( bIsMyOwnFader )
{
fMuteOutStreamGain = fGain;
}
if ( TimerGainOrPan.isActive() )
{
// just update the new value for sending later;
// will compare with oldGain when the timer fires
clientChan->newGain = fGain;
// update range of channel IDs to check in the timer
if ( iId < minGainOrPanId )
minGainOrPanId = iId; // first value to check
if ( iId >= maxGainOrPanId )
maxGainOrPanId = iId + 1; // first value NOT to check
return;
}
// here the timer was not active:
// send the actual gain and reset the range of channel IDs to empty
clientChan->oldGain = clientChan->newGain = fGain;
Channel.SetRemoteChanGain ( clientChan->iServerChannelID, fGain ); // translate client channel to server channel ID
StartTimerGainOrPan();
}
void CClient::OnTimerRemoteChanGainOrPan()
{
QMutexLocker locker ( &MutexGainOrPan );
bool bSent = false;
for ( int iId = minGainOrPanId; iId < maxGainOrPanId; iId++ )
{
CClientChannel* clientChan = &clientChannels[iId];
if ( clientChan->newGain != clientChan->oldGain )
{
// send new gain and record as old gain
float fGain = clientChan->oldGain = clientChan->newGain;
Channel.SetRemoteChanGain ( clientChan->iServerChannelID, fGain ); // translate client channel to server channel ID
bSent = true;
}
if ( clientChan->newPan != clientChan->oldPan )
{
// send new pan and record as old pan
float fPan = clientChan->oldPan = clientChan->newPan;
Channel.SetRemoteChanPan ( clientChan->iServerChannelID, fPan ); // translate client channel to server channel ID
bSent = true;
}
}
// if a new gain or pan has been sent, reset the range of channel IDs to empty and start timer
if ( bSent )
{
StartTimerGainOrPan();
}
}
// reset the range of channel IDs to check and start the delay timer
void CClient::StartTimerGainOrPan()
{
maxGainOrPanId = 0;
minGainOrPanId = MAX_NUM_CHANNELS;
// start timer to delay sending further updates
// use longer delay when connected to server with higher ping time,
// double the ping time in order to allow a bit of overhead for other messages
if ( iCurPingTime < DEFAULT_GAIN_DELAY_PERIOD_MS / 2 )
{
TimerGainOrPan.start ( DEFAULT_GAIN_DELAY_PERIOD_MS );
}
else
{
TimerGainOrPan.start ( iCurPingTime * 2 );
}
}
void CClient::SetRemoteChanPan ( const int iId, const float fPan )
{
QMutexLocker locker ( &MutexGainOrPan );
CClientChannel* clientChan = &clientChannels[iId];
if ( TimerGainOrPan.isActive() )
{
// just update the new value for sending later;
// will compare with oldPan when the timer fires
clientChan->newPan = fPan;
// update range of channel IDs to check in the timer
if ( iId < minGainOrPanId )
minGainOrPanId = iId; // first value to check
if ( iId >= maxGainOrPanId )
maxGainOrPanId = iId + 1; // first value NOT to check
return;
}
// here the timer was not active:
// send the actual gain and reset the range of channel IDs to empty
clientChan->oldPan = clientChan->newPan = fPan;
Channel.SetRemoteChanPan ( clientChan->iServerChannelID, fPan ); // translate client channel to server channel ID
StartTimerGainOrPan();
}
bool CClient::SetServerAddr ( QString strNAddr )
{
CHostAddress HostAddress;
#ifdef CLIENT_NO_SRV_CONNECT
if ( NetworkUtil().ParseNetworkAddress ( strNAddr, HostAddress, bEnableIPv6 ) )
#else
if ( NetworkUtil().ParseNetworkAddressWithSrvDiscovery ( strNAddr, HostAddress, bEnableIPv6 ) )
#endif
{
// apply address to the channel
Channel.SetAddress ( HostAddress );
return true;
}
else
{
return false; // invalid address
}
}
bool CClient::GetAndResetbJitterBufferOKFlag()
{
// get the socket buffer put status flag and reset it
const bool bSocketJitBufOKFlag = Socket.GetAndResetbJitterBufferOKFlag();
if ( !bJitterBufferOK )
{
// our jitter buffer get status is not OK so the overall status of the
// jitter buffer is also not OK (we do not have to consider the status
// of the socket buffer put status flag)
// reset flag before returning the function
bJitterBufferOK = true;
return false;
}
// the jitter buffer get (our own status flag) is OK, the final status
// now depends on the jitter buffer put status flag from the socket
// since per definition the jitter buffer status is OK if both the
// put and get status are OK
return bSocketJitBufOKFlag;
}
void CClient::SetSndCrdPrefFrameSizeFactor ( const int iNewFactor )
{
// first check new input parameter
if ( ( iNewFactor == FRAME_SIZE_FACTOR_PREFERRED ) || ( iNewFactor == FRAME_SIZE_FACTOR_DEFAULT ) || ( iNewFactor == FRAME_SIZE_FACTOR_SAFE ) )
{
// init with new parameter, if client was running then first
// stop it and restart again after new initialization
const bool bWasRunning = Sound.IsRunning();
if ( bWasRunning )
{
Sound.Stop();
}
// set new parameter
iSndCrdPrefFrameSizeFactor = iNewFactor;
// init with new block size index parameter
Init();
if ( bWasRunning )
{
// restart client
Sound.Start();
}
}
}
void CClient::SetEnableOPUS64 ( const bool eNEnableOPUS64 )
{
// init with new parameter, if client was running then first
// stop it and restart again after new initialization
const bool bWasRunning = Sound.IsRunning();
if ( bWasRunning )
{
Sound.Stop();
}
// set new parameter
bEnableOPUS64 = eNEnableOPUS64;
Init();
if ( bWasRunning )
{
Sound.Start();
}
}
void CClient::SetAudioQuality ( const EAudioQuality eNAudioQuality )
{
// init with new parameter, if client was running then first
// stop it and restart again after new initialization
const bool bWasRunning = Sound.IsRunning();
if ( bWasRunning )
{
Sound.Stop();
}
// set new parameter
eAudioQuality = eNAudioQuality;
Init();
if ( bWasRunning )
{
Sound.Start();
}
}
void CClient::SetAudioChannels ( const EAudChanConf eNAudChanConf )
{
// init with new parameter, if client was running then first
// stop it and restart again after new initialization
const bool bWasRunning = Sound.IsRunning();
if ( bWasRunning )
{
Sound.Stop();
}
// set new parameter
eAudioChannelConf = eNAudChanConf;
Init();
if ( bWasRunning )
{
Sound.Start();
}
}
QString CClient::SetSndCrdDev ( const QString strNewDev )
{
// if client was running then first
// stop it and restart again after new initialization
const bool bWasRunning = Sound.IsRunning();
if ( bWasRunning )
{
Sound.Stop();
}
const QString strError = Sound.SetDev ( strNewDev );
// init again because the sound card actual buffer size might
// be changed on new device
Init();
if ( bWasRunning )
{
// restart client
Sound.Start();
}
// in case of an error inform the GUI about it
if ( !strError.isEmpty() )
{
emit SoundDeviceChanged ( strError );
}
return strError;
}
void CClient::SetSndCrdLeftInputChannel ( const int iNewChan )
{
// if client was running then first
// stop it and restart again after new initialization
const bool bWasRunning = Sound.IsRunning();
if ( bWasRunning )
{
Sound.Stop();
}
Sound.SetLeftInputChannel ( iNewChan );
Init();
if ( bWasRunning )
{
// restart client
Sound.Start();
}
}
void CClient::SetSndCrdRightInputChannel ( const int iNewChan )
{
// if client was running then first
// stop it and restart again after new initialization
const bool bWasRunning = Sound.IsRunning();
if ( bWasRunning )
{
Sound.Stop();
}
Sound.SetRightInputChannel ( iNewChan );
Init();
if ( bWasRunning )
{
// restart client
Sound.Start();
}
}
void CClient::SetSndCrdLeftOutputChannel ( const int iNewChan )
{
// if client was running then first
// stop it and restart again after new initialization
const bool bWasRunning = Sound.IsRunning();
if ( bWasRunning )
{
Sound.Stop();
}
Sound.SetLeftOutputChannel ( iNewChan );
Init();
if ( bWasRunning )
{
// restart client
Sound.Start();
}
}
void CClient::SetSndCrdRightOutputChannel ( const int iNewChan )
{
// if client was running then first
// stop it and restart again after new initialization
const bool bWasRunning = Sound.IsRunning();
if ( bWasRunning )
{
Sound.Stop();
}
Sound.SetRightOutputChannel ( iNewChan );
Init();
if ( bWasRunning )
{
// restart client
Sound.Start();
}
}
void CClient::OnSndCrdReinitRequest ( int iSndCrdResetType )
{
QString strError = "";
// audio device notifications can come at any time and they are in a
// different thread, therefore we need a mutex here
MutexDriverReinit.lock();
{
// in older QT versions, enums cannot easily be used in signals without
// registering them -> workaroud: we use the int type and cast to the enum
const ESndCrdResetType eSndCrdResetType = static_cast<ESndCrdResetType> ( iSndCrdResetType );
// if client was running then first
// stop it and restart again after new initialization
const bool bWasRunning = Sound.IsRunning();
if ( bWasRunning )
{
Sound.Stop();
}
// perform reinit request as indicated by the request type parameter
if ( eSndCrdResetType != RS_ONLY_RESTART )
{
if ( eSndCrdResetType != RS_ONLY_RESTART_AND_INIT )
{
// reinit the driver if requested
// (we use the currently selected driver)
strError = Sound.SetDev ( Sound.GetDev() );
}
// init client object (must always be performed if the driver
// was changed)
Init();
}
if ( bWasRunning )
{
// restart client
Sound.Start();
}
}
MutexDriverReinit.unlock();
// inform GUI about the sound card device change
emit SoundDeviceChanged ( strError );
}
void CClient::OnHandledSignal ( int sigNum )
{
#ifdef _WIN32
// Windows does not actually get OnHandledSignal triggered
QCoreApplication::instance()->exit();
Q_UNUSED ( sigNum )
#else
switch ( sigNum )
{
case SIGINT:
case SIGTERM:
// if connected, terminate connection (needed for headless mode)
if ( IsRunning() )
{
Stop();
}
// this should trigger OnAboutToQuit
QCoreApplication::instance()->exit();
break;
default:
break;
}
#endif
}
void CClient::OnControllerInFaderLevel ( int iChannelIdx, int iValue )
{
// in case of a headless client the faders cannot be moved so we need
// to send the controller information directly to the server
#ifdef HEADLESS
// only apply new fader level if channel index is valid
if ( ( iChannelIdx >= 0 ) && ( iChannelIdx < MAX_NUM_CHANNELS ) )
{
SetRemoteChanGain ( iChannelIdx, MathUtils::CalcFaderGain ( iValue ), false );
}
#endif
emit ControllerInFaderLevel ( iChannelIdx, iValue );
}
void CClient::OnControllerInPanValue ( int iChannelIdx, int iValue )
{
// in case of a headless client the panners cannot be moved so we need
// to send the controller information directly to the server
#ifdef HEADLESS
// channel index is valid
SetRemoteChanPan ( iChannelIdx, static_cast<float> ( iValue ) / AUD_MIX_PAN_MAX );
#endif
emit ControllerInPanValue ( iChannelIdx, iValue );
}
void CClient::OnControllerInFaderIsSolo ( int iChannelIdx, bool bIsSolo )
{
// in case of a headless client the buttons are not displayed so we need
// to send the controller information directly to the server
#ifdef HEADLESS
// FIXME: no idea what to do here.
#endif
emit ControllerInFaderIsSolo ( iChannelIdx, bIsSolo );
}
void CClient::OnControllerInFaderIsMute ( int iChannelIdx, bool bIsMute )
{
// in case of a headless client the buttons are not displayed so we need
// to send the controller information directly to the server
#ifdef HEADLESS
// FIXME: no idea what to do here.
#endif
emit ControllerInFaderIsMute ( iChannelIdx, bIsMute );
}
void CClient::OnControllerInMuteMyself ( bool bMute )
{
// in case of a headless client the buttons are not displayed so we need
// to send the controller information directly to the server
#ifdef HEADLESS
// FIXME: no idea what to do here.
#endif
emit ControllerInMuteMyself ( bMute );
}
void CClient::OnClientIDReceived ( int iServerChanID )
{
// if we have just connected to a running server, iActiveChannels will be 0
// if iActiveChannels is not 0, the server must have been restarted on the fly
// in that case, channels might have changed, so clear our list to get it afresh.
if ( iActiveChannels != 0 )
{
qInfo() << "> Server restarted?";
ClearClientChannels();
}
// allocate and map client-side channel 0
int iChanID = FindClientChannel ( iServerChanID, true ); // should always return channel 0
// for headless mode we support to mute our own signal in the personal mix
// (note that the check for headless is done in the main.cpp and must not
// be checked here)
if ( bMuteMeInPersonalMix )
{
SetRemoteChanGain ( iChanID, 0, false );
}
emit ClientIDReceived ( iChanID );
}
void CClient::Start()
{
// init object
Init();
// initialise client channels
ClearClientChannels();
// enable channel
Channel.SetEnable ( true );
// start audio interface
Sound.Start();
}
void CClient::Stop()
{
// stop audio interface
Sound.Stop();
// disable channel
Channel.SetEnable ( false );
// wait for approx. 100 ms to make sure no audio packet is still in the
// network queue causing the channel to be reconnected right after having
// received the disconnect message (seems not to gain much, disconnect is
// still not working reliably)
QTime DieTime = QTime::currentTime().addMSecs ( 100 );
while ( QTime::currentTime() < DieTime )
{
// exclude user input events because if we use AllEvents, it happens
// that if the user initiates a connection and disconnection quickly