1 /******************************************************************************\
2  * \Copyright (c) 2004-2020
3  * \author    Volker Fischer
4  *
5 
6 \mainpage Jamulus source code documentation
7 
8 \section intro_sec Introduction
9 
10 The Jamulus software enables musicians to perform real-time jam sessions over the
11 internet. There is one server running the Jamulus server software which collects
12 the audio data from each Jamulus client, mixes the audio data and sends the mix
13 back to each client.
14 
15 
16 Prefix definitions for the GUI:
17 
18 label:        lbl
19 combo box:    cbx
20 line edit:    edt
21 list view:    lvw
22 check box:    chb
23 radio button: rbt
24 button:       but
25 text view:    txv
26 slider:       sld
27 LED:          led
28 group box:    grb
29 pixmap label: pxl
30 LED bar:      lbr
31 
32  ******************************************************************************
33  *
34  * This program is free software; you can redistribute it and/or modify it under
35  * the terms of the GNU General Public License as published by the Free Software
36  * Foundation; either version 2 of the License, or (at your option) any later
37  * version.
38  *
39  * This program is distributed in the hope that it will be useful, but WITHOUT
40  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
41  * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
42  * details.
43  *
44  * You should have received a copy of the GNU General Public License along with
45  * this program; if not, write to the Free Software Foundation, Inc.,
46  * 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
47  *
48 \******************************************************************************/
49 
50 #pragma once
51 
52 #if _WIN32
53 #    define _CRT_SECURE_NO_WARNINGS
54 #endif
55 
56 #include <QString>
57 #include <QEvent>
58 #include <QDebug>
59 #include <stdio.h>
60 #include <math.h>
61 #include <string>
62 #ifndef _WIN32
63 #    include <unistd.h> // solves a compile problem on recent Ubuntu
64 #endif
65 #ifdef HAVE_CONFIG_H
66 #    include "config.h"
67 #endif
68 
69 /* Definitions ****************************************************************/
70 // define this macro to get debug output
71 //#define _DEBUG_
72 #undef _DEBUG_
73 
74 // version and application name (use version from qt prject file)
75 #undef VERSION
76 #define VERSION  APP_VERSION
77 #define APP_NAME "Jamulus"
78 
79 // Windows registry key name of auto run entry for the server
80 #define AUTORUN_SERVER_REG_NAME "Jamulus server"
81 
82 // default names of the ini-file for client and server
83 #define DEFAULT_INI_FILE_NAME        "Jamulus.ini"
84 #define DEFAULT_INI_FILE_NAME_SERVER "Jamulusserver.ini"
85 
86 // file name for logging file
87 #define DEFAULT_LOG_FILE_NAME "Jamulussrvlog.txt"
88 
89 // System block size, this is the block size on which the audio coder works.
90 // All other block sizes must be a multiple of this size.
91 // Note that the UpdateAutoSetting() function assumes a value of 128.
92 #define SYSTEM_FRAME_SIZE_SAMPLES        64
93 #define DOUBLE_SYSTEM_FRAME_SIZE_SAMPLES ( 2 * SYSTEM_FRAME_SIZE_SAMPLES )
94 
95 // additional buffer for delay panning
96 #define MAX_DELAY_PANNING_SAMPLES 64
97 
98 // default server address and port numbers
99 #define DEFAULT_QOS_NUMBER            128 // CS4 (Quality of Service)
100 #define DEFAULT_SERVER_ADDRESS        "anygenre1.jamulus.io"
101 #define DEFAULT_PORT_NUMBER           22124
102 #define CENTSERV_ANY_GENRE2           "anygenre2.jamulus.io:22224"
103 #define CENTSERV_ANY_GENRE3           "anygenre3.jamulus.io:22624"
104 #define CENTSERV_GENRE_ROCK           "rock.jamulus.io:22424"
105 #define CENTSERV_GENRE_JAZZ           "jazz.jamulus.io:22324"
106 #define CENTSERV_GENRE_CLASSICAL_FOLK "classical.jamulus.io:22524"
107 #define CENTSERV_GENRE_CHORAL         "choral.jamulus.io:22724"
108 
109 // servers to check for new versions
110 #define UPDATECHECK1_ADDRESS "updatecheck1.jamulus.io"
111 #define UPDATECHECK2_ADDRESS "updatecheck2.jamulus.io"
112 
113 // getting started and software manual URL
114 #define CLIENT_GETTING_STARTED_URL "https://jamulus.io/wiki/Getting-Started"
115 #define SERVER_GETTING_STARTED_URL "https://jamulus.io/wiki/Running-a-Server"
116 #define SOFTWARE_MANUAL_URL        "https://jamulus.io/wiki/Software-Manual"
117 
118 // app update message
119 #define APP_UPGRADE_AVAILABLE_MSG_TEXT \
120     QCoreApplication::translate ( \
121         "global", \
122         "A %1 upgrade is available: <a style='color:red;' href='https://jamulus.io/upgrade?progversion=%2'>go to details and downloads</a>" )
123 
124 // determining server internal address uses well-known host and port
125 // We just need a valid, public Internet IP here. We will not send any
126 // traffic there as we will only set up an UDP socket without sending any
127 // data.
128 #define WELL_KNOWN_HOST   "1.1.1.1"              // CloudFlare
129 #define WELL_KNOWN_HOST6  "2606:4700:4700::1111" // CloudFlare
130 #define WELL_KNOWN_PORT   DEFAULT_PORT_NUMBER
131 #define IP_LOOKUP_TIMEOUT 500 // ms
132 
133 // system sample rate (the sound card and audio coder works on this sample rate)
134 #define SYSTEM_SAMPLE_RATE_HZ 48000 // Hz
135 
136 // define the allowed audio frame size factors (since the
137 // "SYSTEM_FRAME_SIZE_SAMPLES" is quite small, it may be that on some
138 // computers a larger value is required)
139 #define FRAME_SIZE_FACTOR_PREFERRED 1 // 64 samples accumulated frame size
140 #define FRAME_SIZE_FACTOR_DEFAULT   2 // 128 samples accumulated frame size
141 #define FRAME_SIZE_FACTOR_SAFE      4 // 256 samples accumulated frame size
142 
143 // define the minimum allowed number of coded bytes for CELT (the encoder
144 // gets in trouble if the value is too low)
145 #define CELT_MINIMUM_NUM_BYTES 10
146 
147 // Maximum block size for network input buffer. It is defined by the longest
148 // protocol message which is PROTMESSID_CLM_SERVER_LIST: Worst case:
149 // (2+2+1+2+2)+200*(4+2+2+1+1+2+20+2+32+2+20)=17609
150 // We add some headroom to that value.
151 #define MAX_SIZE_BYTES_NETW_BUF 20000
152 
153 // minimum/maximum network buffer size (which can be chosen by slider)
154 #define MIN_NET_BUF_SIZE_NUM_BL        1                               // number of blocks
155 #define MAX_NET_BUF_SIZE_NUM_BL        20                              // number of blocks
156 #define AUTO_NET_BUF_SIZE_FOR_PROTOCOL ( MAX_NET_BUF_SIZE_NUM_BL + 1 ) // auto set parameter (only used for protocol)
157 
158 // default network buffer size
159 #define DEF_NET_BUF_SIZE_NUM_BL 10 // number of blocks
160 
161 // audio mixer fader and panning maximum value
162 #define AUD_MIX_FADER_MAX 100
163 #define AUD_MIX_PAN_MAX   100
164 
165 // range of audio mixer fader
166 #define AUD_MIX_FADER_RANGE_DB 35.0f
167 
168 // coefficient for averaging channel levels for automatic fader adjustment
169 #define AUTO_FADER_ADJUST_ALPHA 0.2f
170 
171 // target level for auto fader adjustment in decibels
172 #define AUTO_FADER_TARGET_LEVEL_DB -30.0f
173 
174 // threshold in decibels below which the channel is considered as noise
175 // and not adjusted
176 #define AUTO_FADER_NOISE_THRESHOLD_DB -40.0f
177 
178 // maximum number of fader groups (must be consistent to audiomixerboard implementation)
179 #define MAX_NUM_FADER_GROUPS 8
180 
181 // maximum number of recognized sound cards installed in the system
182 #define MAX_NUMBER_SOUND_CARDS 129 // e.g. 16 inputs, 8 outputs + default entry (MacOS)
183 
184 // define the maximum number of audio channel for input/output we can store
185 // channel infos for (and therefore this is the maximum number of entries in
186 // the channel selection combo box regardless of the actual available number
187 // of channels by the audio device)
188 #define MAX_NUM_IN_OUT_CHANNELS 64
189 
190 // maximum number of elemts in the server address combo box
191 #define MAX_NUM_SERVER_ADDR_ITEMS 12
192 
193 // maximum number of fader settings to be stored (together with the fader tags)
194 #define MAX_NUM_STORED_FADER_SETTINGS 250
195 
196 // range for signal level meter
197 #define LOW_BOUND_SIG_METER   ( -50.0 ) // dB
198 #define UPPER_BOUND_SIG_METER ( 0.0 )   // dB
199 
200 // defines for LED level meter CLevelMeter
201 #define NUM_STEPS_LED_BAR    8
202 #define RED_BOUND_LED_BAR    7
203 #define YELLOW_BOUND_LED_BAR 5
204 
205 // maximum number of connected clients at the server (must not be larger than 256)
206 #define MAX_NUM_CHANNELS 150 // max number channels for server
207 
208 // actual number of used channels in the server
209 // this parameter can safely be changed from 1 to MAX_NUM_CHANNELS
210 // without any other changes in the code
211 #define DEFAULT_USED_NUM_CHANNELS 10 // default used number channels for server
212 
213 // Maximum number of servers registered in the server list. If you want to
214 // change this parameter, you most probably have to adjust MAX_SIZE_BYTES_NETW_BUF.
215 #define MAX_NUM_SERVERS_IN_SERVER_LIST 150 // reduced to 150 because we now have genre-based server lists
216 
217 // defines the time interval at which the ping time is updated in the GUI
218 #define PING_UPDATE_TIME_MS 500 // ms
219 
220 // defines the time interval at which the ping time is updated for the server list
221 #define PING_UPDATE_TIME_SERVER_LIST_MS 2500 // ms
222 
223 // defines the interval between Channel Level updates from the server
224 #define CHANNEL_LEVEL_UPDATE_INTERVAL 200 // number of frames at 64 samples frame size
225 
226 // time-out until a registered server is deleted from the server list if no
227 // new registering was made in minutes
228 #define SERVLIST_TIME_OUT_MINUTES 33 // minutes (should include 3 UDP registration messages)
229 
230 // poll time for server list (to check if entries are time-out)
231 #define SERVLIST_POLL_TIME_MINUTES 1 // minute
232 
233 // time interval for sending ping messages to servers in the server list
234 #define SERVLIST_UPDATE_PING_SERVERS_MS 59000 // ms
235 
236 // time until a slave server registers in the server list
237 #define SERVLIST_REGIST_INTERV_MINUTES 15 // minutes
238 
239 // defines the minimum time a server must run to be a permanent server
240 #define SERVLIST_TIME_PERMSERV_MINUTES 2880 // minutes, 2880 = 60 min * 24 h * 2 d
241 
242 // registration response timeout
243 #define REGISTER_SERVER_TIME_OUT_MS 500 // ms
244 
245 // defines the maximum number of times to retry server registration
246 // when no response is received within the timeout (before reverting
247 // to SERVLIST_REGIST_INTERV_MINUTES)
248 #define REGISTER_SERVER_RETRY_LIMIT 5 // count
249 
250 // Maximum length of fader tag and text message strings (Since for chat messages
251 // some HTML code is added, we also have to define a second length which includes
252 // this additionl HTML code. Right now the length of the HTML code is approx. 66
253 // characters. Here, we add some headroom to this number)
254 #define MAX_LEN_FADER_TAG           16
255 #define MAX_LEN_CHAT_TEXT           1600
256 #define MAX_LEN_CHAT_TEXT_PLUS_HTML 1800
257 #define MAX_LEN_SERVER_NAME         20
258 #define MAX_LEN_IP_ADDRESS          15
259 #define MAX_LEN_SERVER_CITY         20
260 #define MAX_LEN_VERSION_TEXT        30
261 
262 // define Settings tab indexes
263 #define SETTING_TAB_USER     0
264 #define SETTING_TAB_AUDIONET 1
265 #define SETTING_TAB_ADVANCED 2
266 
267 // common tool tip bottom line text
268 #define TOOLTIP_COM_END_TEXT \
269     "<br><div align=right><font size=-1><i>" + \
270         QCoreApplication::translate ( "global", \
271                                       "For more information use the \"What's " \
272                                       "This\" help (help menu, right mouse button or Shift+F1)" ) + \
273         "</i></font></div>"
274 
275 // server welcome message title (do not change for compatibility!)
276 #define WELCOME_MESSAGE_PREFIX "<b>Server Welcome Message:</b> "
277 
278 // mixer settings file name suffix
279 #define MIX_SETTINGS_FILE_SUFFIX "jch"
280 
281 #define _MAXSHORT     32767
282 #define _MINSHORT     ( -32768 )
283 #define INVALID_INDEX -1 // define invalid index as a negative value (a valid index must always be >= 0)
284 
285 #if HAVE_STDINT_H
286 #    include <stdint.h>
287 #elif HAVE_INTTYPES_H
288 #    include <inttypes.h>
289 #elif defined( _WIN32 )
290 typedef __int64          int64_t;
291 typedef __int32          int32_t;
292 typedef __int16          int16_t;
293 typedef unsigned __int64 uint64_t;
294 typedef unsigned __int32 uint32_t;
295 typedef unsigned __int16 uint16_t;
296 typedef unsigned __int8  uint8_t;
297 #elif defined( ANDROID )
298 // don't redfine types for android as these ones below don't work
299 #else
300 typedef long long          int64_t;
301 typedef int                int32_t;
302 typedef short              int16_t;
303 typedef unsigned long long uint64_t;
304 typedef unsigned int       uint32_t;
305 typedef unsigned short     uint16_t;
306 typedef unsigned char      uint8_t;
307 #endif
308 
309 /* Pseudo enum definitions -------------------------------------------------- */
310 // definition for custom event
311 #define MS_PACKET_RECEIVED 0
312 
313 /* Classes ********************************************************************/
314 class CGenErr
315 {
316 public:
strErrorMsg(strNewErrorMsg)317     CGenErr ( QString strNewErrorMsg, QString strNewErrorType = "" ) : strErrorMsg ( strNewErrorMsg ), strErrorType ( strNewErrorType ) {}
318 
GetErrorText()319     QString GetErrorText() const
320     {
321         // return formatted error text
322         if ( strErrorType.isEmpty() )
323         {
324             return strErrorMsg;
325         }
326         else
327         {
328             return strErrorType + ": " + strErrorMsg;
329         }
330     }
331 
332 protected:
333     QString strErrorMsg;
334     QString strErrorType;
335 };
336 
337 class CCustomEvent : public QEvent
338 {
339 public:
340     CCustomEvent ( int iNewMeTy, int iNewSt, int iNewChN = 0 ) :
341         QEvent ( QEvent::Type ( QEvent::User + 11 ) ),
342         iMessType ( iNewMeTy ),
343         iStatus ( iNewSt ),
344         iChanNum ( iNewChN )
345     {}
346 
347     int iMessType;
348     int iStatus;
349     int iChanNum;
350 };
351 
352 /* Prototypes for global functions ********************************************/
353 // command line parsing, TODO do not declare functions globally but in a class
354 QString UsageArguments ( char** argv );
355 
356 bool GetFlagArgument ( char** argv, int& i, QString strShortOpt, QString strLongOpt );
357 
358 bool GetStringArgument ( int argc, char** argv, int& i, QString strShortOpt, QString strLongOpt, QString& strArg );
359 
360 bool GetNumericArgument ( int     argc,
361                           char**  argv,
362                           int&    i,
363                           QString strShortOpt,
364                           QString strLongOpt,
365                           double  rRangeStart,
366                           double  rRangeStop,
367                           double& rValue );
368