forked from FD-/RPiPlay
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rpiplay.cpp
executable file
·550 lines (489 loc) · 21 KB
/
rpiplay.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
/**
* RPiPlay - An open-source AirPlay mirroring server for Raspberry Pi
* Copyright (C) 2019 Florian Draschbacher
*
* 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 3 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 <stddef.h>
#include <cstring>
#include <signal.h>
#include <unistd.h>
#include <string>
#include <vector>
#include <fstream>
#include "log.h"
#include "lib/raop.h"
#include "lib/stream.h"
#include "lib/logger.h"
#include "lib/dnssd.h"
#include "renderers/video_renderer.h"
#include "renderers/audio_renderer.h"
#define VERSION "1.2"
#define DEFAULT_NAME "RPiPlay"
#define DEFAULT_BACKGROUND_MODE BACKGROUND_MODE_ON
#define DEFAULT_AUDIO_DEVICE AUDIO_DEVICE_HDMI
#define DEFAULT_LOW_LATENCY false
#define DEFAULT_DEBUG_LOG false
#define LOWEST_ALLOWED_PORT 1024
#define HIGHEST_PORT 65535
#define DEFAULT_ROTATE 0
#define DEFAULT_FLIP FLIP_NONE
#define DEFAULT_HW_ADDRESS { (char) 0x48, (char) 0x5d, (char) 0x60, (char) 0x7c, (char) 0xee, (char) 0x22 }
int start_server(std::vector<char> hw_addr, std::string name, unsigned short display[5],
unsigned short tcp[3], unsigned short udp[3], bool debug_log,
video_renderer_config_t const *video_config, audio_renderer_config_t const *audio_config);
int stop_server();
typedef video_renderer_t *(*video_init_func_t)(logger_t *logger, video_renderer_config_t const *config);
typedef audio_renderer_t *(*audio_init_func_t)(logger_t *logger, video_renderer_t *video_renderer, audio_renderer_config_t const *config);
typedef struct video_renderer_list_entry_s {
const char *name;
const char *description;
video_init_func_t init_func;
} video_renderer_list_entry_t;
typedef struct audio_renderer_list_entry_s {
const char *name;
const char *description;
audio_init_func_t init_func;
} audio_renderer_list_entry_t;
static bool running = false;
static dnssd_t *dnssd = NULL;
static raop_t *raop = NULL;
static video_init_func_t video_init_func = NULL;
static audio_init_func_t audio_init_func = NULL;
static video_renderer_t *video_renderer = NULL;
static audio_renderer_t *audio_renderer = NULL;
static logger_t *render_logger = NULL;
static const video_renderer_list_entry_t video_renderers[] = {
#if defined(HAS_RPI_RENDERER)
{"rpi", "Raspberry Pi OpenMAX accelerated H.264 renderer", video_renderer_rpi_init},
#endif
#if defined(HAS_GSTREAMER_RENDERER)
{"gstreamer", "GStreamer H.264 renderer", video_renderer_gstreamer_init},
#endif
#if defined(HAS_DUMMY_RENDERER)
{"dummy", "Dummy renderer; does not actually display video", video_renderer_dummy_init},
#endif
};
static const audio_renderer_list_entry_t audio_renderers[] = {
#if defined(HAS_RPI_RENDERER)
{"rpi", "AAC renderer using fdk-aac for decoding and OpenMAX for rendering", audio_renderer_rpi_init},
#endif
#if defined(HAS_GSTREAMER_RENDERER)
{"gstreamer", "GStreamer audio renderer", audio_renderer_gstreamer_init},
#endif
#if defined(HAS_DUMMY_RENDERER)
{"dummy", "Dummy renderer; does not actually play audio", audio_renderer_dummy_init},
#endif
};
static void signal_handler(int sig) {
switch (sig) {
case SIGINT:
case SIGTERM:
running = 0;
break;
}
}
static void init_signals(void) {
struct sigaction sigact;
sigact.sa_handler = signal_handler;
sigemptyset(&sigact.sa_mask);
sigact.sa_flags = 0;
sigaction(SIGINT, &sigact, NULL);
sigaction(SIGTERM, &sigact, NULL);
}
static int parse_hw_addr(std::string str, std::vector<char> &hw_addr) {
for (int i = 0; i < str.length(); i += 3) {
hw_addr.push_back((char) stol(str.substr(i), NULL, 16));
}
return 0;
}
std::string find_mac() {
std::ifstream iface_stream("/sys/class/net/eth0/address");
if (!iface_stream) {
iface_stream.open("/sys/class/net/wlan0/address");
}
if (!iface_stream) return "";
std::string mac_address;
iface_stream >> mac_address;
iface_stream.close();
return mac_address;
}
static video_init_func_t find_video_init_func(const char *name) {
for (int i = 0; i < sizeof(video_renderers)/sizeof(video_renderers[0]); i++) {
if (!strcmp(name, video_renderers[i].name)) {
return video_renderers[i].init_func;
}
}
return NULL;
}
static audio_init_func_t find_audio_init_func(const char *name) {
for (int i = 0; i < sizeof(audio_renderers)/sizeof(audio_renderers[0]); i++) {
if (!strcmp(name, audio_renderers[i].name)) {
return audio_renderers[i].init_func;
}
}
return NULL;
}
void print_info(char *name) {
printf("RPiPlay %s: An open-source AirPlay mirroring server for Raspberry Pi\n", VERSION);
printf("Usage: %s [-n name] [-b (on|auto|off)] [-r (90|180|270)] [-l] [-a (hdmi|analog|off)] [-vr renderer] [-ar renderer]\n", name);
printf("Options:\n");
printf("-n name Specify the network name of the AirPlay server\n");
printf("-s wxh[@r] Set mirror resolution [refresh_rate] default 1920x1080[@60]\n");
printf("-o Set mirror \"overscanned\" mode on\n");
printf("-fps n Set maximum allowed streaming framerate, default 30\n");
printf("-p Use legacy ports UDP 6000:6001:7011 TCP 7000:7001:7100\n");
printf("-p ( | tcp | udp) n Use (TCP and UDP | TCP | UDP) ports n,n+1,n+2. range 1024-65535\n");
printf(" \"n\" = n1,n2,n3 specifies ports individually; \"n1,n2\" gives n3 = n2+1\n");
printf("-b (on|auto|off) Show black background always, only during active connection, or never\n");
printf("-r (90|180|270) Specify image rotation in multiples of 90 degrees\n");
printf("-f (horiz|vert|both) Specify image flipping (horiz = horizontal, vert = vertical, both = both)\n");
printf("-l Enable low-latency mode (disables render clock)\n");
printf("-a (hdmi|analog|off) Set audio output device\n");
printf("-vr renderer Set video renderer to use. Available renderers:\n");
for (int i = 0; i < sizeof(video_renderers)/sizeof(video_renderers[0]); i++) {
printf(" %s: %s%s\n", video_renderers[i].name, video_renderers[i].description, i == 0 ? " [Default]" : "");
}
printf("-ar renderer Set audio renderer to use. Available renderers:\n");
for (int i = 0; i < sizeof(audio_renderers)/sizeof(audio_renderers[0]); i++) {
printf(" %s: %s%s\n", audio_renderers[i].name, audio_renderers[i].description, i == 0 ? " [Default]" : "");
}
printf("-d Enable debug logging\n");
printf("-v/-h Displays this help and version information\n");
}
bool option_has_value(const int i, const int argc, std::string option, const char *next_arg) {
if (i >= argc - 1 || next_arg[0] == '-') {
LOGE("invalid: \"%s\" had no argument", option.c_str());
return false;
}
return true;
}
static bool get_display_settings (std::string value, unsigned short *w, unsigned short *h, unsigned short *r) {
// assume str = wxh@r is valid if w and h are positive decimal integers
// with no more than 4 digits, r < 256 (stored in one byte).
char *end;
std::size_t pos = value.find_first_of("x");
if (pos == std::string::npos) return false;
std::string str1 = value.substr(pos+1);
value.erase(pos);
if (value.length() == 0 || value.length() > 4 || value[0] == '-') return false;
*w = (unsigned short) strtoul(value.c_str(), &end, 10);
if (*end || *w == 0) return false;
pos = str1.find_first_of("@");
if(pos != std::string::npos) {
std::string str2 = str1.substr(pos+1);
if (str2.length() == 0 || str2.length() > 3 || str2[0] == '-') return false;
*r = (unsigned short) strtoul(str2.c_str(), &end, 10);
if (*end || *r == 0 || *r > 255) return false;
str1.erase(pos);
}
if (str1.length() == 0 || str1.length() > 4 || str1[0] == '-') return false;
*h = (unsigned short) strtoul(str1.c_str(), &end, 10);
if (*end || *h == 0) return false;
return true;
}
static bool get_fps (const char *str, unsigned short *n) {
// str must be a positive decimal integer < 256 (stored in one byte)
char *end;
if (strlen(str) == 0 || strlen(str) > 3 || str[0] == '-') return false;
*n = (unsigned short) strtoul(str, &end, 10);
if (*end || *n == 0 || *n > 255) return false;
return true;
}
static bool get_ports (int nports, std::string option, const char * value, unsigned short * const port) {
//valid entries are comma-separated values port_1,port_2,...,port_r, 0 < r <= nports
//where ports are distinct, and are in the allowed range.
//missing values are consecutive to last given value (at least one value needed).
char *end;
unsigned long l;
std::size_t pos;
std::string val(value), str;
for (int i = 0; i <= nports ; i++) {
if(i == nports) break;
pos = val.find_first_of(',');
str = val.substr(0,pos);
if(str.length() == 0 || str.length() > 5 || str[0] == '-') break;
l = strtoul(str.c_str(), &end, 10);
if (*end || l < LOWEST_ALLOWED_PORT || l > HIGHEST_PORT) break;
*(port + i) = (unsigned short) l;
for (int j = 0; j < i ; j++) {
if( *(port + j) == *(port + i)) break;
}
if(pos == std::string::npos) {
if (nports + *(port + i) > i + 1 + HIGHEST_PORT) break;
for (int j = i + 1; j < nports; j++) {
*(port + j) = *(port + j - 1) + 1;
}
return true;
}
val.erase(0, pos+1);
}
LOGE("invalid \"%s %s\", all %d ports must be in range [%d,%d]",
option.c_str(), value, nports, LOWEST_ALLOWED_PORT, HIGHEST_PORT);
return false;
}
int main(int argc, char *argv[]) {
init_signals();
std::string server_name = DEFAULT_NAME;
std::vector<char> server_hw_addr = DEFAULT_HW_ADDRESS;
bool debug_log = DEFAULT_DEBUG_LOG;
unsigned short display[5] = {0}, tcp[3] = {0}, udp[3] = {0};
video_renderer_config_t video_config;
video_config.background_mode = DEFAULT_BACKGROUND_MODE;
video_config.low_latency = DEFAULT_LOW_LATENCY;
video_config.rotation = DEFAULT_ROTATE;
video_config.flip = DEFAULT_FLIP;
audio_renderer_config_t audio_config;
audio_config.device = DEFAULT_AUDIO_DEVICE;
audio_config.low_latency = DEFAULT_LOW_LATENCY;
// Default to the best available renderer
video_init_func = video_renderers[0].init_func;
audio_init_func = audio_renderers[0].init_func;
// Parse arguments
for (int i = 1; i < argc; i++) {
std::string arg(argv[i]);
if (arg == "-n") {
if (i == argc - 1) continue;
server_name = std::string(argv[++i]);
} else if (arg == "-s") {
if (!option_has_value(i, argc, arg, argv[i+1])) exit(1);
std::string value(argv[++i]);
if (!get_display_settings(value, &display[0], &display[1], &display[2])) {
LOGE("invalid \"-s %s\"; -s wxh : max w,h=9999; -s wxh@r : max r=255",
argv[i]);
exit(1);
}
} else if (arg == "-fps") {
if (!option_has_value(i, argc, arg, argv[i+1])) exit(1);
if (!get_fps(argv[++i], &display[3])) {
LOGE("invalid \"-fps %s\"; -fps n : max n=255, default n=30", argv[i]);
exit(1);
}
} else if (arg == "-o") {
display[4] = 1;
} else if (arg == "-p") {
if (i == argc - 1 || argv[i + 1][0] == '-') {
tcp[0] = 7100; tcp[1] = 7000; tcp[2] = 7001;
udp[0] = 7011; udp[1] = 6001; udp[2] = 6000;
continue;
}
std::string value(argv[++i]);
if (value == "tcp") {
arg.append(" tcp");
if(!get_ports(3, arg, argv[++i], tcp)) exit(1);
} else if (value == "udp") {
arg.append( " udp");
if(!get_ports(3, arg, argv[++i], udp)) exit(1);
} else {
if(!get_ports(3, arg, argv[i], tcp)) exit(1);
for (int j = 1; j < 3; j++) {
udp[j] = tcp[j];
}
}
} else if (arg == "-b") {
// For backwards-compatibility, make just -b disable the background
if (i == argc - 1 || argv[i + 1][0] == '-') {
video_config.background_mode = BACKGROUND_MODE_OFF;
continue;
}
std::string background_mode(argv[++i]);
video_config.background_mode = background_mode == "off" ? BACKGROUND_MODE_OFF :
background_mode == "auto" ? BACKGROUND_MODE_AUTO :
BACKGROUND_MODE_ON;
} else if (arg == "-a") {
if (i == argc - 1) continue;
std::string audio_device_name(argv[++i]);
audio_config.device = audio_device_name == "hdmi" ? AUDIO_DEVICE_HDMI :
audio_device_name == "analog" ? AUDIO_DEVICE_ANALOG :
AUDIO_DEVICE_NONE;
} else if (arg == "-l") {
video_config.low_latency = !video_config.low_latency;
audio_config.low_latency = !audio_config.low_latency;
} else if (arg == "-r") {
video_config.rotation = atoi(argv[++i]);
} else if (arg == "-f") {
if (i == argc - 1) continue;
std::string flip_type(argv[++i]);
video_config.flip = flip_type == "horiz" ? FLIP_HORIZONTAL :
flip_type == "vert" ? FLIP_VERTICAL :
flip_type == "both" ? FLIP_BOTH :
FLIP_NONE;
} else if (arg == "-d") {
debug_log = !debug_log;
} else if (arg == "-vr") {
if (i == argc - 1) {
fprintf(stderr, "Error: You must supply the name of a video renderer after the -vr argument.\n");
exit(1);
}
video_init_func = find_video_init_func(argv[++i]);
if (!video_init_func) {
fprintf(stderr, "Error: Unable to locate video renderer \"%s\".\n", argv[i]);
exit(1);
}
} else if (arg == "-ar") {
if (i == argc - 1) {
fprintf(stderr, "Error: You must supply the name of an audio renderer after the -ar argument.\n");
exit(1);
}
audio_init_func = find_audio_init_func(argv[++i]);
if (!audio_init_func) {
fprintf(stderr, "Error: Unable to locate audio renderer \"%s\".\n", argv[i]);
exit(1);
}
} else if (arg == "-h" || arg == "-v") {
print_info(argv[0]);
exit(0);
} else {
LOGE("unknown option %s, stopping", arg.c_str());
exit(1);
}
}
std::string mac_address = find_mac();
if (!mac_address.empty()) {
server_hw_addr.clear();
parse_hw_addr(mac_address, server_hw_addr);
}
if (start_server(server_hw_addr, server_name, display, tcp, udp,
debug_log, &video_config, &audio_config) != 0) {
return 1;
}
running = true;
while (running) {
sleep(1);
}
LOGI("Stopping...");
stop_server();
}
// Server callbacks
extern "C" void conn_init(void *cls) {
if (video_renderer) video_renderer->funcs->update_background(video_renderer, 1);
}
extern "C" void conn_destroy(void *cls) {
if (video_renderer) video_renderer->funcs->update_background(video_renderer, -1);
}
extern "C" void audio_process(void *cls, raop_ntp_t *ntp, aac_decode_struct *data) {
if (audio_renderer != NULL) {
audio_renderer->funcs->render_buffer(audio_renderer, ntp, data->data, data->data_len, data->pts);
}
}
extern "C" void video_process(void *cls, raop_ntp_t *ntp, h264_decode_struct *data) {
if (video_renderer != NULL) {
video_renderer->funcs->render_buffer(video_renderer, ntp, data->data, data->data_len, data->pts, data->frame_type);
}
}
extern "C" void audio_flush(void *cls) {
if (audio_renderer) audio_renderer->funcs->flush(audio_renderer);
}
extern "C" void video_flush(void *cls) {
if (video_renderer) video_renderer->funcs->flush(video_renderer);
}
extern "C" void audio_set_volume(void *cls, float volume) {
if (audio_renderer != NULL) {
audio_renderer->funcs->set_volume(audio_renderer, volume);
}
}
extern "C" void log_callback(void *cls, int level, const char *msg) {
switch (level) {
case LOGGER_DEBUG: {
LOGD("%s", msg);
break;
}
case LOGGER_WARNING: {
LOGW("%s", msg);
break;
}
case LOGGER_INFO: {
LOGI("%s", msg);
break;
}
case LOGGER_ERR: {
LOGE("%s", msg);
break;
}
default:
break;
}
}
int start_server(std::vector<char> hw_addr, std::string name, unsigned short display[5],
unsigned short tcp[3], unsigned short udp[3], bool debug_log,
video_renderer_config_t const *video_config, audio_renderer_config_t const *audio_config) {
raop_callbacks_t raop_cbs;
memset(&raop_cbs, 0, sizeof(raop_cbs));
raop_cbs.conn_init = conn_init;
raop_cbs.conn_destroy = conn_destroy;
raop_cbs.audio_process = audio_process;
raop_cbs.video_process = video_process;
raop_cbs.audio_flush = audio_flush;
raop_cbs.video_flush = video_flush;
raop_cbs.audio_set_volume = audio_set_volume;
raop = raop_init(10, &raop_cbs);
if (raop == NULL) {
LOGE("Error initializing raop!");
return -1;
}
/* write desired display pixel width, pixel height, refresh_rate, max_fps, overscanned to raop*\
/* (value = 0 for default values 1920,1080,60,30); these are just info sent to the client*/
raop_set_display(raop, display[0], display[1], display[2], display[3], display[4]);
/* network port selection (ports listed as "0" will be dynamically assigned) */
raop_set_tcp_ports(raop, tcp);
raop_set_udp_ports(raop, udp);
raop_set_log_callback(raop, log_callback, NULL);
raop_set_log_level(raop, debug_log ? RAOP_LOG_DEBUG : LOGGER_INFO);
render_logger = logger_init();
logger_set_callback(render_logger, log_callback, NULL);
logger_set_level(render_logger, debug_log ? LOGGER_DEBUG : LOGGER_INFO);
if (video_config->low_latency) logger_log(render_logger, LOGGER_INFO, "Using low-latency mode");
if ((video_renderer = video_init_func(render_logger, video_config)) == NULL) {
LOGE("Could not init video renderer");
return -1;
}
if (audio_config->device == AUDIO_DEVICE_NONE) {
LOGI("Audio disabled");
} else if ((audio_renderer = audio_init_func(render_logger, video_renderer, audio_config)) ==
NULL) {
LOGE("Could not init audio renderer");
return -1;
}
if (video_renderer) video_renderer->funcs->start(video_renderer);
if (audio_renderer) audio_renderer->funcs->start(audio_renderer);
unsigned short port = raop_get_port(raop);
raop_start(raop, &port);
raop_set_port(raop, port);
int error;
dnssd = dnssd_init(name.c_str(), strlen(name.c_str()), hw_addr.data(), hw_addr.size(), &error);
if (error) {
LOGE("Could not initialize dnssd library!");
return -2;
}
raop_set_dnssd(raop, dnssd);
dnssd_register_raop(dnssd, port);
if (tcp[2]) {
port = tcp[2];
} else {
port = (port != HIGHEST_PORT ? port + 1 : port - 1);
}
dnssd_register_airplay(dnssd, port);
return 0;
}
int stop_server() {
raop_destroy(raop);
dnssd_unregister_raop(dnssd);
dnssd_unregister_airplay(dnssd);
// If we don't destroy these two in the correct order, we get a deadlock from the ilclient library
if (audio_renderer) audio_renderer->funcs->destroy(audio_renderer);
if (video_renderer) video_renderer->funcs->destroy(video_renderer);
logger_destroy(render_logger);
return 0;
}