Add support for configuration files

This commit is contained in:
Iwan Timmer 2015-07-27 21:55:19 +02:00
parent fa23af42b2
commit 3dda71dd0a
4 changed files with 288 additions and 184 deletions

View File

@ -45,6 +45,7 @@ For a demo see this [video](https://www.youtube.com/watch?v=XRW6O0bSHNw).
Streaming options:
-config <config> Load configuration file
-720 Use 1280x720 resolution (default)
-1080 Use 1920x1080 resolution
-width <width> Horizontal resolution (default 1280)

221
src/config.c Normal file
View File

@ -0,0 +1,221 @@
/*
* This file is part of Moonlight Embedded.
*
* Copyright (C) 2015 Iwan Timmer
*
* Moonlight 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.
*
* Moonlight 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 Moonlight; if not, see <http://www.gnu.org/licenses/>.
*/
#include "input/evdev.h"
#include "config.h"
#include "audio.h"
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <getopt.h>
#define MOONLIGHT_PATH "/moonlight/"
#define USER_PATHS ":~/.moonlight/:./"
bool inputAdded = false;
static bool mapped = true;
static struct option long_options[] = {
{"720", no_argument, NULL, 'a'},
{"1080", no_argument, NULL, 'b'},
{"width", required_argument, NULL, 'c'},
{"height", required_argument, NULL, 'd'},
{"30fps", no_argument, NULL, 'e'},
{"60fps", no_argument, NULL, 'f'},
{"bitrate", required_argument, NULL, 'g'},
{"packetsize", required_argument, NULL, 'h'},
{"app", required_argument, NULL, 'i'},
{"input", required_argument, NULL, 'j'},
{"mapping", required_argument, NULL, 'k'},
{"nosops", no_argument, NULL, 'l'},
{"audio", required_argument, NULL, 'm'},
{"localaudio", no_argument, NULL, 'n'},
{"config", required_argument, NULL, 'o'},
{0, 0, 0, 0},
};
char* get_path(char* name) {
const char *xdg_data_dirs = getenv("XDG_DATA_DIRS");
char *data_dirs;
if (access(name, R_OK) != -1) {
return name;
}
if (!xdg_data_dirs)
data_dirs = "/usr/share:/usr/local/share:" USER_PATHS;
else {
data_dirs = malloc(strlen(xdg_data_dirs) + strlen(USER_PATHS) + 1);
strcpy(data_dirs, xdg_data_dirs);
strcpy(data_dirs+strlen(data_dirs), USER_PATHS);
}
char *path = malloc(strlen(data_dirs)+strlen(MOONLIGHT_PATH)+strlen(name)+1);
if (path == NULL) {
fprintf(stderr, "Not enough memory\n");
exit(-1);
}
char* end;
do {
end = strstr(data_dirs, ":");
int length = end != NULL?end - data_dirs:strlen(data_dirs);
memcpy(path, data_dirs, length);
if (path[0] == '/')
sprintf(path+length, "%s%s", MOONLIGHT_PATH, name);
else
sprintf(path+length, "%s", name);
if(access(path, R_OK) != -1)
return path;
data_dirs = end + 1;
} while (end != NULL);
free(path);
return NULL;
}
static void parse_argument(int c, char* value, PCONFIGURATION config) {
switch (c) {
case 'a':
config->stream.width = 1280;
config->stream.height = 720;
break;
case 'b':
config->stream.width = 1920;
config->stream.height = 1080;
break;
case 'c':
config->stream.width = atoi(value);
break;
case 'd':
config->stream.height = atoi(value);
break;
case 'e':
config->stream.fps = 30;
break;
case 'f':
config->stream.fps = 60;
break;
case 'g':
config->stream.bitrate = atoi(value);
break;
case 'h':
config->stream.packetSize = atoi(value);
break;
case 'i':
config->app = value;
break;
case 'j':
evdev_create(value, config->mapping);
inputAdded = true;
mapped = true;
break;
case 'k':
config->mapping = get_path(value);
if (config->mapping == NULL) {
fprintf(stderr, "Unable to open custom mapping file: %s\n", value);
exit(-1);
}
mapped = false;
break;
case 'l':
config->sops = false;
break;
case 'm':
audio_device = value;
break;
case 'n':
config->localaudio = true;
break;
case 'o':
config_file_parse(value, config);
break;
case 1:
if (config->action == NULL)
config->action = value;
else if (config->address == NULL)
config->address = value;
else {
perror("Too many options");
exit(-1);
}
}
}
void config_file_parse(char* filename, PCONFIGURATION config) {
FILE* fd = fopen(filename, "r");
if (fd == NULL) {
fprintf(stderr, "Can't open configuration file: %s\n", filename);
exit(EXIT_FAILURE);
}
char *line = NULL;
size_t len = 0;
while (getline(&line, &len, fd) != -1) {
char *key = NULL, *value = NULL;
if (sscanf(line, "%ms = %ms", &key, &value) == 2) {
for (int i=0;long_options[i].name != NULL;i++) {
if (long_options[i].has_arg = required_argument && strcmp(long_options[i].name, key) == 0) {
parse_argument(long_options[i].val, value, config);
}
}
}
}
}
void config_parse(int argc, char* argv[], PCONFIGURATION config) {
config->stream.width = 1280;
config->stream.height = 720;
config->stream.fps = 60;
config->stream.bitrate = -1;
config->stream.packetSize = 1024;
config->app = "Steam";
config->action = NULL;
config->address = NULL;
config->sops = true;
config->localaudio = false;
config->mapping = get_path("mappings/default.conf");
int option_index = 0;
int c;
while ((c = getopt_long_only(argc, argv, "-abc:d:efg:h:i:j:k:lm:n", long_options, &option_index)) != -1) {
parse_argument(c, optarg, config);
}
if (config->stream.bitrate == -1) {
if (config->stream.height >= 1080 && config->stream.fps >= 60)
config->stream.bitrate = 20000;
else if (config->stream.height >= 1080 || config->stream.fps >= 60)
config->stream.bitrate = 10000;
else
config->stream.bitrate = 5000;
}
if (inputAdded && !mapped) {
fprintf(stderr, "Mapping option should be followed by the input to be mapped.\n");
exit(-1);
}
}

37
src/config.h Normal file
View File

@ -0,0 +1,37 @@
/*
* This file is part of Moonlight Embedded.
*
* Copyright (C) 2015 Iwan Timmer
*
* Moonlight 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.
*
* Moonlight 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 Moonlight; if not, see <http://www.gnu.org/licenses/>.
*/
#include "limelight-common/Limelight.h"
#include <stdbool.h>
typedef struct _CONFIGURATION {
STREAM_CONFIGURATION stream;
char* app;
char* action;
char* address;
char* mapping;
bool sops;
bool localaudio;
} CONFIGURATION, *PCONFIGURATION;
bool inputAdded;
void config_file_parse(char* filename, PCONFIGURATION config);
void config_parse(int argc, char* argv[], PCONFIGURATION config);

View File

@ -23,6 +23,7 @@
#include "video.h"
#include "audio.h"
#include "discover.h"
#include "config.h"
#include "input/evdev.h"
#include "input/udev.h"
@ -41,10 +42,6 @@
#include <netdb.h>
#include <arpa/inet.h>
#include <openssl/rand.h>
#include <getopt.h>
#define MOONLIGHT_PATH "/moonlight/"
#define USER_PATHS ":~/.moonlight/:./"
static void applist(PSERVER_DATA server) {
PAPP_LIST list;
@ -75,14 +72,14 @@ static int get_app_id(PSERVER_DATA server, const char *name) {
return -1;
}
static void stream(PSERVER_DATA server, PSTREAM_CONFIGURATION config, const char* app, bool sops, bool localaudio) {
int appId = get_app_id(server, app);
static void stream(PSERVER_DATA server, PCONFIGURATION config) {
int appId = get_app_id(server, config->app);
if (appId<0) {
fprintf(stderr, "Can't find app %s\n", app);
fprintf(stderr, "Can't find app %s\n", config->app);
exit(-1);
}
gs_start_app(server, config, appId, sops, localaudio);
gs_start_app(server, &config->stream, appId, config->sops, config->localaudio);
video_init();
evdev_init();
@ -90,7 +87,7 @@ static void stream(PSERVER_DATA server, PSTREAM_CONFIGURATION config, const char
cec_init();
#endif /* HAVE_LIBCEC */
LiStartConnection(server->address, config, &connection_callbacks, decoder_callbacks, &audio_callbacks, NULL, 0, server->serverMajorVersion);
LiStartConnection(server->address, &config->stream, &connection_callbacks, decoder_callbacks, &audio_callbacks, NULL, 0, server->serverMajorVersion);
evdev_start();
loop_main();
@ -109,6 +106,7 @@ static void help() {
printf("\tquit\t\t\tQuit the application or game being streamed\n");
printf("\thelp\t\t\tShow this help\n\n");
printf(" Streaming options\n\n");
printf("\t-config <config>\tLoad configuration file\n");
printf("\t-720\t\t\tUse 1280x720 resolution [default]\n");
printf("\t-1080\t\t\tUse 1920x1080 resolution\n");
printf("\t-width <width>\t\tHorizontal resolution (default 1280)\n");
@ -127,48 +125,6 @@ static void help() {
exit(0);
}
char* get_path(char* name) {
const char *xdg_data_dirs = getenv("XDG_DATA_DIRS");
char *data_dirs;
if (access(name, R_OK) != -1) {
return name;
}
if (!xdg_data_dirs)
data_dirs = "/usr/share:/usr/local/share:" USER_PATHS;
else {
data_dirs = malloc(strlen(xdg_data_dirs) + strlen(USER_PATHS) + 1);
strcpy(data_dirs, xdg_data_dirs);
strcpy(data_dirs+strlen(data_dirs), USER_PATHS);
}
char *path = malloc(strlen(data_dirs)+strlen(MOONLIGHT_PATH)+strlen(name)+1);
if (path == NULL) {
fprintf(stderr, "Not enough memory\n");
exit(-1);
}
char* end;
do {
end = strstr(data_dirs, ":");
int length = end != NULL?end - data_dirs:strlen(data_dirs);
memcpy(path, data_dirs, length);
if (path[0] == '/')
sprintf(path+length, "%s%s", MOONLIGHT_PATH, name);
else
sprintf(path+length, "%s", name);
if(access(path, R_OK) != -1)
return path;
data_dirs = end + 1;
} while (end != NULL);
free(path);
return NULL;
}
static void pair_check(PSERVER_DATA server) {
if (!server->paired) {
fprintf(stderr, "You must pair with the PC first\n");
@ -177,160 +133,49 @@ static void pair_check(PSERVER_DATA server) {
}
int main(int argc, char* argv[]) {
STREAM_CONFIGURATION config;
config.width = 1280;
config.height = 720;
config.fps = 60;
config.bitrate = -1;
config.packetSize = 1024;
CONFIGURATION config;
config_parse(argc, argv, &config);
static struct option long_options[] = {
{"720", no_argument, 0, 'a'},
{"1080", no_argument, 0, 'b'},
{"width", required_argument, 0, 'c'},
{"height", required_argument, 0, 'd'},
{"30fps", no_argument, 0, 'e'},
{"60fps", no_argument, 0, 'f'},
{"bitrate", required_argument, 0, 'g'},
{"packetsize", required_argument, 0, 'h'},
{"app", required_argument, 0, 'i'},
{"input", required_argument, 0, 'j'},
{"mapping", required_argument, 0, 'k'},
{"nosops", no_argument, 0, 'l'},
{"audio", required_argument, 0, 'm'},
{"localaudio", no_argument, 0, 'n'},
{0, 0, 0, 0},
};
char* app = "Steam";
char* action = NULL;
char* address = NULL;
char* mapping = get_path("mappings/default.conf");
int option_index = 0;
bool sops = true;
bool localaudio = false;
bool inputAdded = false;
bool mapped = true;
int c;
while ((c = getopt_long_only(argc, argv, "-abc:d:efg:h:i:j:k:lm:n", long_options, &option_index)) != -1) {
switch (c) {
case 'a':
config.width = 1280;
config.height = 720;
break;
case 'b':
config.width = 1920;
config.height = 1080;
break;
case 'c':
config.width = atoi(optarg);
break;
case 'd':
config.height = atoi(optarg);
break;
case 'e':
config.fps = 30;
break;
case 'f':
config.fps = 60;
break;
case 'g':
config.bitrate = atoi(optarg);
break;
case 'h':
config.packetSize = atoi(optarg);
break;
case 'i':
app = optarg;
break;
case 'j':
evdev_create(optarg, mapping);
inputAdded = true;
mapped = true;
break;
case 'k':
mapping = get_path(optarg);
if (mapping == NULL) {
fprintf(stderr, "Unable to open custom mapping file: %s\n", optarg);
exit(-1);
}
mapped = false;
break;
case 'l':
sops = false;
break;
case 'm':
audio_device = optarg;
break;
case 'n':
localaudio = true;
break;
case 1:
if (action == NULL)
action = optarg;
else if (address == NULL)
address = optarg;
else {
perror("Too many options");
exit(-1);
}
}
}
if (config.bitrate == -1) {
if (config.height >= 1080 && config.fps >= 60)
config.bitrate = 20000;
else if (config.height >= 1080 || config.fps >= 60)
config.bitrate = 10000;
else
config.bitrate = 5000;
}
if (inputAdded && !mapped) {
fprintf(stderr, "Mapping option should be followed by the input to be mapped.\n");
exit(-1);
}
if (action == NULL || strcmp("help", action) == 0)
if (config.action == NULL || strcmp("help", config.action) == 0)
help();
else if (strcmp("map", action) == 0) {
if (address == NULL) {
else if (strcmp("map", config.action) == 0) {
if (config.address == NULL) {
perror("No filename for mapping");
exit(-1);
}
udev_init(!inputAdded, mapping);
evdev_map(address);
udev_init(!inputAdded, config.mapping);
evdev_map(config.address);
exit(0);
}
if (address == NULL) {
address = malloc(MAX_ADDRESS_SIZE);
if (address == NULL) {
if (config.address == NULL) {
config.address = malloc(MAX_ADDRESS_SIZE);
if (config.address == NULL) {
perror("Not enough memory");
exit(-1);
}
address[0] = 0;
gs_discover_server(address);
if (address[0] == 0) {
config.address[0] = 0;
gs_discover_server(config.address);
if (config.address[0] == 0) {
fprintf(stderr, "Autodiscovery failed. Specify an IP address next time.\n");
exit(-1);
}
}
PSERVER_DATA server;
if (gs_init(server, address, ".") != GS_OK) {
fprintf(stderr, "Can't connect to server %s\n", address);
if (gs_init(server, config.address, ".") != GS_OK) {
fprintf(stderr, "Can't connect to server %s\n", config.address);
exit(-1);
}
if (strcmp("list", action) == 0) {
if (strcmp("list", config.action) == 0) {
pair_check(server);
applist(server);
} else if (strcmp("stream", action) == 0) {
udev_init(!inputAdded, mapping);
} else if (strcmp("stream", config.action) == 0) {
udev_init(!inputAdded, config.mapping);
pair_check(server);
stream(server, &config, app, sops, localaudio);
} else if (strcmp("pair", action) == 0) {
stream(server, &config);
} else if (strcmp("pair", config.action) == 0) {
char pin[5];
sprintf(pin, "%d%d%d%d", (int)random() % 10, (int)random() % 10, (int)random() % 10, (int)random() % 10);
printf("Please enter the following PIN on the target PC: %s\n", pin);
@ -339,9 +184,9 @@ int main(int argc, char* argv[]) {
} else {
printf("Succesfully paired\n");
}
} else if (strcmp("quit", action) == 0) {
} else if (strcmp("quit", config.action) == 0) {
pair_check(server);
gs_quit_app(server);
} else
fprintf(stderr, "%s is not a valid action\n", action);
fprintf(stderr, "%s is not a valid action\n", config.action);
}