forked from bandrews/mqttaudio
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmqttaudio.cpp
More file actions
executable file
·733 lines (627 loc) · 21.9 KB
/
mqttaudio.cpp
File metadata and controls
executable file
·733 lines (627 loc) · 21.9 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
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
#include <argp.h> // For argument parsing
#include <limits.h>
#include <signal.h> // For signal handling
#include <stdio.h> // For standard input/output functions
#include <stdint.h>
#include <stdlib.h>
#include <sysexits.h> // For standard exit codes
#include <unistd.h> // For POSIX API (e.g., getpid)
#include <vector>
#include <iostream>
#include <string>
#include <unordered_map>
#include <mosquitto.h> // For MQTT client functionality
#include "rapidjson/document.h" // For JSON parsing
#include "rapidjson/writer.h"
#include "rapidjson/stringbuffer.h"
#include "SDL.h" // For SDL library functions
#include "SDL_mixer.h" // For SDL audio mixing functions
#include "alsautil.h" // For ALSA utility functions
#include "sample.h" // For handling audio samples
#include "samplemanager.h" // For managing audio samples
#include "SDL_rwhttp.h" // For HTTP support in SDL
using namespace std;
using namespace rapidjson;
// Function prototypes
void setChannelVolume(int channel, float volume);
void pauseChannel(int channel);
void resumeChannel(int channel);
bool processCommand(Document &d);
const char *argp_program_version = "0.1.2";
const char *argp_program_bug_address = "contact@mindgeist.com";
// Global variables
int frequency = 44100; // Audio frequency in Hz
float masterVolume = 1.0f; // Master volume (0.0 to 1.0)
std::unordered_map<int, float> channelVolumes; // Map of volumes per channel
std::string server = "localhost"; // MQTT server address
unsigned int port = 1883; // MQTT server port
std::string topic = ""; // MQTT topic to subscribe to
std::string alsaDevice = ""; // ALSA PCM device to use
std::string uriprefix = ""; // Prefix for audio file URIs
vector<string> preloads; // List of samples to preload
bool run = true; // Main loop control flag
bool verbose = false; // Verbose output flag
SampleManager manager(verbose); // Sample manager instance
// Signal handler to stop the main loop
void handle_signal(int s)
{
run = false;
}
// MQTT connection callback function
void connect_callback(struct mosquitto *mosq, void *obj, int result)
{
switch (result)
{
case 0:
printf("Connected successfully.\n");
mosquitto_subscribe(mosq, NULL, topic.c_str(), 0);
return;
case 1:
fprintf(stderr, "Connection refused - unacceptable protocol version.\n");
break;
case 2:
fprintf(stderr, "Connection refused - identifier rejected.\n");
break;
case 3:
fprintf(stderr, "Connection refused - broker unavailable.\n");
break;
default:
fprintf(stderr, "Unknown error in connect callback, rc=%d\n", result);
}
exit(EX_PROTOCOL);
}
// Function to stop all sounds
void stopAll(bool alsoStopBgm)
{
if (verbose)
{
printf("Stopping all sounds, %s background music.\n", alsoStopBgm ? "including" : "excluding");
}
Mix_HaltChannel(-1); // Stop all channels
}
// Function to preload an audio sample
Sample *precacheSample(const char *file)
{
std::string filename = file;
if (uriprefix.length() > 0)
{
filename = uriprefix + filename;
}
if (verbose)
{
printf("Preloading sample '%s'\n", filename.c_str());
}
return manager.GetSample(filename.c_str());
}
// Function to play an audio sample with specified parameters
void playSample(const char *file, int channel, bool loop, float volume, bool exclusive, bool isBgm, int maxPlayLength, bool nocache)
{
// Limit the sample volume between 0.0 and 1.0
if (volume < 0.0f) volume = 0.0f;
if (volume > 1.0f) volume = 1.0f;
// Get the channel volume or set it to 1.0 if it doesn't exist
float channelVolume = 1.0f;
auto it = channelVolumes.find(channel);
if (it != channelVolumes.end())
{
channelVolume = it->second;
}
else
{
channelVolumes[channel] = channelVolume; // Initialize to 1.0
}
// Calculate the effective volume
float effectiveVolume = volume * channelVolume * masterVolume;
if (effectiveVolume < 0.0f) effectiveVolume = 0.0f;
if (effectiveVolume > 1.0f) effectiveVolume = 1.0f;
int sdlVolume = static_cast<int>(effectiveVolume * MIX_MAX_VOLUME);
if (verbose)
{
printf("Playing sound %s, on channel %d, %s, at effective volume %.2f (sample volume: %.2f, channel volume: %.2f, master volume: %.2f)\n",
file, channel, loop ? "looping" : "once", effectiveVolume, volume, channelVolume, masterVolume);
}
// Handle the nocache parameter
if (nocache)
{
manager.RemoveSample(file);
if (verbose)
{
printf("Removed sample '%s' from cache due to nocache=true.\n", file);
}
}
if (exclusive)
{
Mix_HaltChannel(-1); // Stop all channels if exclusive
}
Sample *sample = precacheSample(file); // Preload the sample
if (sample != NULL)
{
Mix_Volume(channel, sdlVolume); // Adjust the volume before playing
Mix_PlayChannelTimed(channel, sample->chunk, loop ? -1 : 0, maxPlayLength); // Play on the selected channel
}
else
{
printf("Error - could not load requested sample '%s'\n", file);
}
}
// Function to process incoming MQTT commands
bool processCommand(Document &d)
{
if (!d.IsObject())
{
fprintf(stderr, "Message is not a valid object.\n");
return false;
}
if (!d.HasMember("command") || !d["command"].IsString())
{
fprintf(stderr, "Message does not have a 'command' property that is a string.\n");
return false;
}
const char *command = d["command"].GetString();
if (0 == strcasecmp(command, "soundPlay") || 0 == strcasecmp(command, "play"))
{
// Verify that the message has all required parameters
if (!d.HasMember("message") || !d["message"].IsObject())
{
fprintf(stderr, "Message does not have a 'message' property that is an object.\n");
return false;
}
if (!d["message"].HasMember("file") || !d["message"]["file"].IsString())
{
fprintf(stderr, "Message does not have a 'file' property that is a string.\n");
return false;
}
const char *file = d["message"]["file"].GetString();
int channel = 0; // Default channel
bool loop = false;
float volume = 1.0f;
bool exclusive = false;
bool bgm = false;
bool nocache = false; // Default for nocache
int maxPlayLength = -1;
// Adjust parameters based on the message
if (d["message"].HasMember("channel") && d["message"]["channel"].IsInt())
{
channel = d["message"]["channel"].GetInt();
}
if (d["message"].HasMember("loop") && d["message"]["loop"].IsBool())
{
loop = d["message"]["loop"].GetBool();
}
if (d["message"].HasMember("volume") && d["message"]["volume"].IsFloat())
{
volume = d["message"]["volume"].GetFloat();
}
if (d["message"].HasMember("exclusive") && d["message"]["exclusive"].IsBool())
{
exclusive = d["message"]["exclusive"].GetBool();
}
if (d["message"].HasMember("bgm") && d["message"]["bgm"].IsBool())
{
bgm = d["message"]["bgm"].GetBool();
}
if (d["message"].HasMember("maxPlayLength") && d["message"]["maxPlayLength"].IsInt())
{
maxPlayLength = d["message"]["maxPlayLength"].GetInt();
}
if (d["message"].HasMember("nocache") && d["message"]["nocache"].IsBool())
{
nocache = d["message"]["nocache"].GetBool();
}
playSample(file, channel, loop, volume, exclusive, bgm, maxPlayLength, nocache);
return true;
}
else if (0 == strcasecmp(command, "soundStopAll") || 0 == strcasecmp(command, "stopall"))
{
stopAll(true);
return true;
}
else if (0 == strcasecmp(command, "soundFadeOut") || 0 == strcasecmp(command, "fadeout"))
{
if (d["message"].HasMember("time") && d["message"]["time"].IsInt())
{
int time = d["message"]["time"].GetInt();
int channel = -1; // Default to all channels
if (d["message"].HasMember("channel") && d["message"]["channel"].IsInt())
{
channel = d["message"]["channel"].GetInt(); // Specific channel
}
if (verbose)
{
printf("Fading out channel %d for %d milliseconds.\n", channel, time);
}
// Apply fade out to specified channel or all channels
if (channel == -1)
{
Mix_FadeOutChannel(-1, time); // Fade out all channels
}
else
{
Mix_FadeOutChannel(channel, time); // Fade out specific channel
}
}
return true;
}
else if (0 == strcasecmp(command, "soundPrecache") || 0 == strcasecmp(command, "precache"))
{
if (!d.HasMember("message") || !d["message"].IsObject())
{
fprintf(stderr, "Message does not have a 'message' property that is an object.\n");
return false;
}
if (!d["message"].HasMember("file") || !d["message"]["file"].IsString())
{
fprintf(stderr, "Message does not have a 'message.file' property that is a string.\n");
return false;
}
const char *file = d["message"]["file"].GetString();
precacheSample(file);
if (verbose)
{
printf("Precached sound file '%s'.\n", file);
}
return true;
}
else if (0 == strcasecmp(command, "soundSetVolume"))
{
if (!d.HasMember("message") || !d["message"].IsObject())
{
fprintf(stderr, "Message does not have a 'message' property that is an object.\n");
return false;
}
if (!d["message"].HasMember("channel") || !d["message"]["channel"].IsInt())
{
fprintf(stderr, "Message does not have a 'channel' property that is an int.\n");
return false;
}
if (!d["message"].HasMember("volume") || !d["message"]["volume"].IsFloat())
{
fprintf(stderr, "Message does not have a 'volume' property that is a float.\n");
return false;
}
int channel = d["message"]["channel"].GetInt();
float volume = d["message"]["volume"].GetFloat();
setChannelVolume(channel, volume);
return true;
}
else if (0 == strcasecmp(command, "soundPause"))
{
if (!d.HasMember("message") || !d["message"].IsObject())
{
fprintf(stderr, "Message does not have a 'message' property that is an object.\n");
return false;
}
if (!d["message"].HasMember("channel") || !d["message"]["channel"].IsInt())
{
fprintf(stderr, "Message does not have a 'channel' property that is an int.\n");
return false;
}
int channel = d["message"]["channel"].GetInt();
pauseChannel(channel);
return true;
}
else if (0 == strcasecmp(command, "soundResume"))
{
if (!d.HasMember("message") || !d["message"].IsObject())
{
fprintf(stderr, "Message does not have a 'message' property that is an object.\n");
return false;
}
if (!d["message"].HasMember("channel") || !d["message"]["channel"].IsInt())
{
fprintf(stderr, "Message does not have a 'channel' property that is an int.\n");
return false;
}
int channel = d["message"]["channel"].GetInt();
resumeChannel(channel);
return true;
}
else if (0 == strcasecmp(command, "setMasterVolume"))
{
if (d.HasMember("message") && d["message"].IsObject())
{
if (d["message"].HasMember("volume") && d["message"]["volume"].IsFloat())
{
masterVolume = d["message"]["volume"].GetFloat();
if (masterVolume < 0.0f) masterVolume = 0.0f;
if (masterVolume > 1.0f) masterVolume = 1.0f;
if (verbose)
{
printf("Master volume set to %.2f\n", masterVolume);
}
// Update the volume of all channels
for (const auto& kv : channelVolumes)
{
int channel = kv.first;
float channelVolume = kv.second;
// Recalculate the effective volume
float effectiveVolume = channelVolume * masterVolume;
if (effectiveVolume < 0.0f) effectiveVolume = 0.0f;
if (effectiveVolume > 1.0f) effectiveVolume = 1.0f;
int sdlVolume = static_cast<int>(effectiveVolume * MIX_MAX_VOLUME);
Mix_Volume(channel, sdlVolume);
if (verbose)
{
printf("Updated volume of channel %d to %.2f (effective volume: %.2f)\n", channel, channelVolume, effectiveVolume);
}
}
return true;
}
}
fprintf(stderr, "Invalid message format for setMasterVolume\n");
return false;
}
else
{
// Default case for unknown commands
fprintf(stderr, "Unknown command '%s'.\n", command);
return false;
}
}
// MQTT message callback function
void message_callback(struct mosquitto *mosq, void *obj, const struct mosquitto_message *message)
{
bool match = 0;
mosquitto_topic_matches_sub(topic.c_str(), message->topic, &match);
if (match)
{
Document d;
d.Parse((const char *)message->payload);
if (!processCommand(d))
{
fprintf(stderr, "Failed to process command '%s'.\n", (const char *)message->payload);
}
}
}
// Initializes the SDL audio subsystem
bool initSDLAudio(void)
{
SDL_Init(SDL_INIT_AUDIO);
atexit(SDL_Quit);
// Load support for OGG, MOD, and MP3 sample/music formats
int flags = MIX_INIT_OGG | MIX_INIT_MOD | MIX_INIT_MP3;
int initted = Mix_Init(flags);
if ((initted & flags) != flags)
{
fprintf(stderr, "Mix_Init: Failed to init required ogg and mod support!\n");
fprintf(stderr, "Mix_Init: %s\n", Mix_GetError());
// Handle error
return false;
}
// Set up the audio stream
int result = Mix_OpenAudio(frequency, AUDIO_S16SYS, 2, 512);
if (result < 0)
{
fprintf(stderr, "Unable to open audio: %s\n", SDL_GetError());
return false;
}
result = Mix_AllocateChannels(16);
if (result < 0)
{
fprintf(stderr, "Unable to allocate mixing channels: %s\n", SDL_GetError());
return false;
}
// Set up HTTP/CURL library
result = SDL_RWHttpInit();
if (result != 0)
{
fprintf(stderr, "Unable to initialize web download library (%s).\n", result);
return false;
}
return true;
}
// Argument parsing function
static int parse_opt(int key, char *arg, struct argp_state *state)
{
switch (key)
{
case 's':
if (arg != NULL && *arg != '\0')
{
printf("Setting MQTT server to '%s'\n", arg);
server = arg;
}
else
{
argp_error(state, "no server specified");
}
break;
case 'p':
if (arg != NULL)
{
port = atoi(arg);
printf("Setting MQTT port to %d\n", port);
}
break;
case 'u':
if (arg != NULL && *arg != '\0')
{
printf("Setting URI prefix to '%s'\n", arg);
uriprefix = arg;
}
break;
case 200: // Preload
if (arg != NULL && *arg != '\0')
{
printf("Preloading '%s'...\n", arg);
preloads.push_back(arg);
}
break;
case 'd':
if (arg != NULL && *arg != '\0')
{
printf("Setting output device to ALSA PCM device '%s'\n", arg);
setenv("SDL_AUDIODRIVER", "ALSA", true);
setenv("AUDIODEV", arg, true);
}
else
{
argp_error(state, "no ALSA PCM device specified");
}
break;
case 't':
if (arg != NULL && *arg != '\0')
{
printf("Setting MQTT topic to '%s'\n", arg);
topic = arg;
}
else
{
argp_error(state, "no topic specified");
}
break;
case 'l':
listAlsaDevices("pcm");
exit(0);
break;
case 'v':
printf("Verbose mode enabled.\n");
verbose = true;
break;
case 'f':
if (arg != NULL && *arg != '\0')
{
frequency = atoi(arg);
printf("Setting frequency to %d Hz.\n", frequency);
}
break;
case ARGP_KEY_NO_ARGS:
if (topic.empty())
{
argp_usage(state);
}
break;
}
return 0;
}
// Function to set the volume of a specific channel
void setChannelVolume(int channel, float volume)
{
// Limit volume between 0.0 and 1.0
if (volume < 0.0f) volume = 0.0f;
if (volume > 1.0f) volume = 1.0f;
// Save the channel volume
channelVolumes[channel] = volume;
// Calculate the effective volume
float effectiveVolume = volume * masterVolume;
if (effectiveVolume < 0.0f) effectiveVolume = 0.0f;
if (effectiveVolume > 1.0f) effectiveVolume = 1.0f;
int sdlVolume = static_cast<int>(effectiveVolume * MIX_MAX_VOLUME);
Mix_Volume(channel, sdlVolume);
if (verbose)
{
printf("Set volume of channel %d to %.2f (effective volume: %.2f)\n", channel, volume, effectiveVolume);
}
}
// Function to pause playback on a specific channel
void pauseChannel(int channel)
{
Mix_Pause(channel);
if (verbose)
{
printf("Paused channel %d\n", channel);
}
}
// Function to resume playback on a specific channel
void resumeChannel(int channel)
{
Mix_Resume(channel);
if (verbose)
{
printf("Resumed channel %d\n", channel);
}
}
// Main function
int main(int argc, char **argv)
{
printf("mqtt audio player %s - %s %s\n", argp_program_version, __DATE__, __TIME__);
printf("www.mindgeist.com.\n\n");
struct argp_option options[] =
{
{"server", 's', "server", 0, "The MQTT server to connect to (default localhost)"},
{"port", 'p', "port", 0, "The MQTT server port (default 1883)"},
{"topic", 't', "topic", 0, "The MQTT server topic to subscribe to (wildcards allowed)"},
{"alsa-device", 'd', "pcm", 0, "The ALSA PCM device to use (overrides SDL_AUDIODRIVER and AUDIODEV environment variables)"},
{"list-devices", 'l', 0, 0, "Lists available ALSA PCM devices for the 'd' switch"},
{"verbose", 'v', 0, 0, "Enables verbose logging"},
{"frequency", 'f', "frequency_in_khz", 0, "Sets the frequency for the sound output"},
{"uri-prefix", 'u', "prefix", 0, "Sets a prefix to be prepended to all sound file locations"},
{"preload", 200, "url", 0, "Preloads a sound sample on startup"},
{0}
};
struct argp argp = {options, parse_opt};
int retval = argp_parse(&argp, argc, argv, 0, 0, 0);
if (retval != 0)
{
return retval;
}
// Initialize the SDL library
printf("Initializing SDL library.\n");
if (!initSDLAudio())
{
return 1;
}
// Preload audio samples
for (auto &preload : preloads)
{
if (precacheSample(preload.c_str()) == NULL)
{
fprintf(stderr, "Failed to precache sample '%s'.\n", preload.c_str());
}
}
// Connect to the MQTT server
uint8_t reconnect = true;
char clientid[128];
struct mosquitto *mosq;
int rc = 0;
// Intercept SIGINT and SIGTERM to exit the MQTT loop when they occur
signal(SIGINT, handle_signal);
signal(SIGTERM, handle_signal);
mosquitto_lib_init();
memset(clientid, 0, 128);
snprintf(clientid, 127, "mqttaudio_%d", getpid());
mosq = mosquitto_new(clientid, true, 0);
if (mosq)
{
mosquitto_connect_callback_set(mosq, connect_callback);
mosquitto_message_callback_set(mosq, message_callback);
printf("Connecting to server %s\n", server.c_str());
rc = mosquitto_connect(mosq, server.c_str(), port, 60);
if (MOSQ_ERR_SUCCESS != rc)
{
fprintf(stderr, "Failed to connect to server %s (%d)\n", server.c_str(), rc);
return EX_UNAVAILABLE;
}
while (run)
{
rc = mosquitto_loop(mosq, -1, 1);
if (run && rc)
{
fprintf(stderr, "Server connection lost to server %s; attempting to reconnect.\n", server.c_str());
sleep(10);
rc = mosquitto_reconnect(mosq);
if (MOSQ_ERR_SUCCESS != rc)
{
fprintf(stderr, "Failed to reconnect to server %s (%d)\n", server.c_str(), rc);
}
else
{
fprintf(stderr, "Reconnected to server %s (%d)\n", server.c_str(), rc);
mosquitto_subscribe(mosq, NULL, topic.c_str(), 0);
}
}
}
mosquitto_destroy(mosq);
}
printf("Exiting mqtt audio player...\n");
printf("Cleaning up MQTT connection...\n");
mosquitto_lib_cleanup();
printf("Cleaning up audio samples...\n");
manager.FreeAll();
printf("Closing audio device...\n");
Mix_CloseAudio();
SDL_RWHttpShutdown();
SDL_Quit();
printf("Cleanup complete.\n");
return 0;
}