mirror of
https://github.com/moonlight-stream/moonlight-embedded.git
synced 2026-04-20 23:30:37 +00:00
Initial commit of Moonlight Embedded port to C
This commit is contained in:
55
src/audio.c
Normal file
55
src/audio.c
Normal file
@@ -0,0 +1,55 @@
|
||||
/*
|
||||
* 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 "audio.h"
|
||||
|
||||
#include <stdio.h>
|
||||
|
||||
static FILE* fd;
|
||||
static const char* fileName = "fake.opus";
|
||||
|
||||
void audio_renderer_init() {
|
||||
printf("audio_renderer_init\n");
|
||||
fd = fopen(fileName, "w");
|
||||
}
|
||||
|
||||
void audio_renderer_start() {
|
||||
printf("audio_renderer_start\n");
|
||||
}
|
||||
|
||||
void audio_renderer_stop() {
|
||||
printf("audio_renderer_stop\n");
|
||||
}
|
||||
|
||||
void audio_renderer_release() {
|
||||
printf("audio_renderer_release\n");
|
||||
fclose(fd);
|
||||
}
|
||||
|
||||
void audio_renderer_decode_and_play_sample(char* data, int length) {
|
||||
fwrite(data, length, 1, fd);
|
||||
}
|
||||
|
||||
AUDIO_RENDERER_CALLBACKS audio_callbacks = {
|
||||
.init = audio_renderer_init,
|
||||
.start = audio_renderer_start,
|
||||
.stop = audio_renderer_stop,
|
||||
.release = audio_renderer_release,
|
||||
.decodeAndPlaySample = audio_renderer_decode_and_play_sample,
|
||||
};
|
||||
22
src/audio.h
Normal file
22
src/audio.h
Normal file
@@ -0,0 +1,22 @@
|
||||
/*
|
||||
* 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"
|
||||
|
||||
extern AUDIO_RENDERER_CALLBACKS audio_callbacks;
|
||||
339
src/client.c
Normal file
339
src/client.c
Normal file
@@ -0,0 +1,339 @@
|
||||
/*
|
||||
* 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 "http.h"
|
||||
#include "xml.h"
|
||||
|
||||
#include "limelight-common/Limelight.h"
|
||||
|
||||
#include <stdbool.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <openssl/sha.h>
|
||||
#include <openssl/aes.h>
|
||||
#include <openssl/rand.h>
|
||||
#include <openssl/evp.h>
|
||||
#include <openssl/x509.h>
|
||||
#include <openssl/pem.h>
|
||||
#include <openssl/err.h>
|
||||
|
||||
static const char *uniqueFileName = "uniqueid.dat";
|
||||
static const char *certificateFileName = "client.pem";
|
||||
static const char *keyFileName = "key.pem";
|
||||
|
||||
static char unique_id[17];
|
||||
static X509 *cert;
|
||||
static char cert_hex[4096];
|
||||
static EVP_PKEY *privateKey;
|
||||
|
||||
static bool paired;
|
||||
static int currentGame;
|
||||
|
||||
static void client_load_unique_id() {
|
||||
FILE *fd = fopen(uniqueFileName, "r");
|
||||
if (fd == NULL) {
|
||||
unsigned char unique_data[16];
|
||||
RAND_bytes(unique_data, 16);
|
||||
for (int i = 0; i < 16; i += 2) {
|
||||
sprintf(unique_id + i, "%02x", unique_data[i]);
|
||||
}
|
||||
fd = fopen(uniqueFileName, "w");
|
||||
fwrite(unique_id, 16, 1, fd);
|
||||
} else {
|
||||
fread(unique_id, 16, 1, fd);
|
||||
}
|
||||
fclose(fd);
|
||||
unique_id[16] = 0;
|
||||
}
|
||||
|
||||
static void client_load_cert() {
|
||||
FILE *fd = fopen(certificateFileName, "r");
|
||||
if (fd == NULL) {
|
||||
fprintf(stderr, "Can't open certificate file");
|
||||
exit(-1);
|
||||
}
|
||||
|
||||
if (!(cert = PEM_read_X509(fd, NULL, NULL, NULL))) {
|
||||
printf("Error loading cert into memory.\n");
|
||||
exit(-1);
|
||||
}
|
||||
|
||||
rewind(fd);
|
||||
|
||||
int c;
|
||||
int length = 0;
|
||||
while ((c = fgetc(fd)) != EOF) {
|
||||
sprintf(cert_hex + length, "%02x", c);
|
||||
length += 2;
|
||||
}
|
||||
cert_hex[length] = 0;
|
||||
|
||||
fclose(fd);
|
||||
|
||||
fd = fopen(keyFileName, "r");
|
||||
PEM_read_PrivateKey(fd, &privateKey, NULL, NULL);
|
||||
fclose(fd);
|
||||
}
|
||||
|
||||
static void client_load_server_status(const char *address) {
|
||||
char url[4096];
|
||||
sprintf(url, "https://%s:47984/serverinfo?uniqueid=%s", address, unique_id);
|
||||
|
||||
struct http_data *data = http_create_data();
|
||||
http_request(url, data);
|
||||
|
||||
char *pairedText = NULL;
|
||||
char *currentGameText = NULL;
|
||||
xml_search(data->memory, data->size, "currentgame", ¤tGameText);
|
||||
xml_search(data->memory, data->size, "PairStatus", &pairedText);
|
||||
http_free_data(data);
|
||||
|
||||
paired = pairedText != NULL && strcmp(pairedText, "1") == 0;
|
||||
currentGame = currentGameText == NULL ? 0 : atoi(currentGameText);
|
||||
|
||||
free(pairedText);
|
||||
free(currentGameText);
|
||||
}
|
||||
|
||||
static void bytes_to_hex(unsigned char *in, char *out, size_t len) {
|
||||
for (int i = 0; i < len; i++) {
|
||||
sprintf(out + i * 2, "%02x", in[i]);
|
||||
}
|
||||
out[len * 2] = 0;
|
||||
}
|
||||
|
||||
static int sign_it(const char *msg, size_t mlen, unsigned char **sig, size_t *slen, EVP_PKEY *pkey) {
|
||||
int result = -1;
|
||||
|
||||
*sig = NULL;
|
||||
*slen = 0;
|
||||
|
||||
EVP_MD_CTX *ctx = EVP_MD_CTX_create();
|
||||
if (ctx == NULL) {
|
||||
printf("EVP_MD_CTX_create failed, error 0x%lx\n", ERR_get_error());
|
||||
return -1;
|
||||
}
|
||||
|
||||
const EVP_MD *md = EVP_get_digestbyname("SHA256");
|
||||
if (md == NULL) {
|
||||
printf("EVP_get_digestbyname failed, error 0x%lx\n", ERR_get_error());
|
||||
goto cleanup;
|
||||
}
|
||||
|
||||
int rc = EVP_DigestInit_ex(ctx, md, NULL);
|
||||
if (rc != 1) {
|
||||
printf("EVP_DigestInit_ex failed, error 0x%lx\n", ERR_get_error());
|
||||
goto cleanup;
|
||||
}
|
||||
|
||||
rc = EVP_DigestSignInit(ctx, NULL, md, NULL, pkey);
|
||||
if (rc != 1) {
|
||||
printf("EVP_DigestSignInit failed, error 0x%lx\n", ERR_get_error());
|
||||
goto cleanup;
|
||||
}
|
||||
|
||||
rc = EVP_DigestSignUpdate(ctx, msg, mlen);
|
||||
if (rc != 1) {
|
||||
printf("EVP_DigestSignUpdate failed, error 0x%lx\n", ERR_get_error());
|
||||
goto cleanup;
|
||||
}
|
||||
|
||||
size_t req = 0;
|
||||
rc = EVP_DigestSignFinal(ctx, NULL, &req);
|
||||
if (rc != 1) {
|
||||
printf("EVP_DigestSignFinal failed (1), error 0x%lx\n", ERR_get_error());
|
||||
goto cleanup;
|
||||
}
|
||||
|
||||
if (!(req > 0)) {
|
||||
printf("EVP_DigestSignFinal failed (2), error 0x%lx\n", ERR_get_error());
|
||||
goto cleanup;
|
||||
}
|
||||
|
||||
*sig = OPENSSL_malloc(req);
|
||||
if (*sig == NULL) {
|
||||
printf("OPENSSL_malloc failed, error 0x%lx\n", ERR_get_error());
|
||||
goto cleanup;
|
||||
}
|
||||
|
||||
*slen = req;
|
||||
rc = EVP_DigestSignFinal(ctx, *sig, slen);
|
||||
if (rc != 1) {
|
||||
printf("EVP_DigestSignFinal failed (3), return code %d, error 0x%lx\n", rc,
|
||||
ERR_get_error());
|
||||
goto cleanup;
|
||||
}
|
||||
|
||||
if (req != *slen) {
|
||||
printf("EVP_DigestSignFinal failed, mismatched signature sizes %ld, %ld\n",
|
||||
req, *slen);
|
||||
goto cleanup;
|
||||
}
|
||||
|
||||
result = 1;
|
||||
|
||||
cleanup:
|
||||
EVP_MD_CTX_destroy(ctx);
|
||||
ctx = NULL;
|
||||
|
||||
return !!result;
|
||||
}
|
||||
|
||||
static void client_pair(const char *address) {
|
||||
char url[4096];
|
||||
|
||||
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);
|
||||
|
||||
unsigned char salt_data[16];
|
||||
char salt_hex[33];
|
||||
RAND_bytes(salt_data, 16);
|
||||
bytes_to_hex(salt_data, salt_hex, 16);
|
||||
|
||||
sprintf(url, "https://%s:47984/pair?uniqueid=%s&devicename=roth&updateState=1&phrase=getservercert&salt=%s&clientcert=%s", address, unique_id, salt_hex, cert_hex);
|
||||
struct http_data *data = http_create_data();
|
||||
http_request(url, data);
|
||||
|
||||
unsigned char salt_pin[20];
|
||||
unsigned char aes_key_hash[20];
|
||||
AES_KEY aes_key;
|
||||
memcpy(salt_pin, salt_data, 16);
|
||||
memcpy(salt_pin+16, salt_pin, 4);
|
||||
SHA1(salt_pin, 20, aes_key_hash);
|
||||
AES_set_encrypt_key((unsigned char *)aes_key_hash, 128, &aes_key);
|
||||
|
||||
unsigned char challenge_data[16];
|
||||
unsigned char challenge_enc[16];
|
||||
char challenge_hex[33];
|
||||
RAND_bytes(challenge_data, 16);
|
||||
AES_encrypt(challenge_data, challenge_enc, &aes_key);
|
||||
bytes_to_hex(challenge_enc, challenge_hex, 16);
|
||||
|
||||
sprintf(url, "https://%s:47984/pair?uniqueid=%s&devicename=roth&updateState=1&clientchallenge=%s", address, unique_id, challenge_hex);
|
||||
http_request(url, data);
|
||||
|
||||
char *result;
|
||||
xml_search(data->memory, data->size, "challengeresponse", &result);
|
||||
printf("Status %s\n", result);
|
||||
|
||||
char challenge_response_data_enc[48];
|
||||
char challenge_response_data[48];
|
||||
for (int count = 0; count < strlen(result); count++) {
|
||||
sscanf(&result[count], "%2hhx", &challenge_response_data_enc[count / 2]);
|
||||
}
|
||||
free(result);
|
||||
|
||||
for (int i = 0; i < 48; i += 16) {
|
||||
AES_decrypt(&challenge_response_data_enc[i], &challenge_response_data[i], &aes_key);
|
||||
}
|
||||
|
||||
char client_secret_data[16];
|
||||
RAND_bytes(client_secret_data, 16);
|
||||
|
||||
char challenge_response[16 + 256 + 16];
|
||||
char challenge_response_hash[32];
|
||||
char challenge_response_hash_enc[32];
|
||||
char challenge_response_hex[33];
|
||||
memcpy(challenge_response, challenge_response_data + 20, 16);
|
||||
memcpy(challenge_response + 16, cert->signature->data, 256);
|
||||
memcpy(challenge_response + 16 + 256, client_secret_data, 16);
|
||||
SHA1(challenge_response, 16 + 256 + 16, challenge_response_hash);
|
||||
|
||||
for (int i = 0; i < 32; i += 16) {
|
||||
AES_encrypt(&challenge_response_hash[i], &challenge_response_hash_enc[i], &aes_key);
|
||||
}
|
||||
bytes_to_hex(challenge_response_hash_enc, challenge_response_hex, 32);
|
||||
|
||||
sprintf(url, "https://%s:47984/pair?uniqueid=%s&devicename=roth&updateState=1&serverchallengeresp=%s", address, unique_id, challenge_response_hex);
|
||||
http_request(url, data);
|
||||
xml_search(data->memory, data->size, "pairingsecret", &result);
|
||||
|
||||
char *signature = NULL;
|
||||
size_t s_len;
|
||||
if (!sign_it(client_secret_data, 16, &signature, &s_len, privateKey)) {
|
||||
fprintf(stderr, "Failed to sign data\n");
|
||||
exit(-1);
|
||||
}
|
||||
|
||||
char client_pairing_secret[16 + 256];
|
||||
char client_pairing_secret_hex[(16 + 256) * 2 + 1];
|
||||
memcpy(client_pairing_secret, client_secret_data, 16);
|
||||
memcpy(client_pairing_secret + 16, signature, 256);
|
||||
bytes_to_hex(client_pairing_secret, client_pairing_secret_hex, 16 + 256);
|
||||
|
||||
sprintf(url, "https://%s:47984/pair?uniqueid=%s&devicename=roth&updateState=1&clientpairingsecret=%s", address, unique_id, client_pairing_secret_hex);
|
||||
http_request(url, data);
|
||||
|
||||
sprintf(url, "https://%s:47984/pair?uniqueid=%s&devicename=roth&updateState=1&phrase=pairchallenge", address, unique_id, challenge_response_hex);
|
||||
http_request(url, data);
|
||||
http_free_data(data);
|
||||
}
|
||||
|
||||
struct app_list *client_applist(const char *address) {
|
||||
char url[4096];
|
||||
struct http_data *data = http_create_data();
|
||||
sprintf(url, "https://%s:47984/applist?uniqueid=%s", address, unique_id);
|
||||
http_request(url, data);
|
||||
struct app_list *list = xml_applist(data->memory, data->size);
|
||||
http_free_data(data);
|
||||
return list;
|
||||
}
|
||||
|
||||
int client_get_app_id(const char *address, char *name) {
|
||||
struct app_list *list = client_applist(address);
|
||||
while (list != NULL) {
|
||||
if (strcmp(list->name, name) == 0)
|
||||
return list->id;
|
||||
|
||||
list = list->next;
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
void client_start_app(STREAM_CONFIGURATION *config, const char *address, int appId) {
|
||||
RAND_bytes(config->remoteInputAesKey, 16);
|
||||
memset(config->remoteInputAesIv, 0, 16);
|
||||
|
||||
srand(time(NULL));
|
||||
char url[4096];
|
||||
u_int32_t rikeyid = 1;
|
||||
char rikey_hex[33];
|
||||
bytes_to_hex(config->remoteInputAesKey, rikey_hex, 16);
|
||||
|
||||
struct http_data *data = http_create_data();
|
||||
if (currentGame == 0)
|
||||
sprintf(url, "https://%s:47984/launch?uniqueid=%s&appid=%d&mode=%dx%dx%d&additionalStates=1&sops=1&rikey=%s&rikeyid=%d&localAudioPlayMode=0", address, unique_id, appId, config->width, config->height, config->fps, rikey_hex, rikeyid);
|
||||
else
|
||||
sprintf(url, "https://%s:47984/resume?uniqueid=%s&rikey=%s&rikeyid=%d", address, unique_id, rikey_hex, rikeyid);
|
||||
|
||||
http_request(url, data);
|
||||
http_free_data(data);
|
||||
}
|
||||
|
||||
void client_init(const char *address) {
|
||||
http_init();
|
||||
client_load_unique_id();
|
||||
client_load_cert();
|
||||
|
||||
client_load_server_status(address);
|
||||
if (!paired) {
|
||||
client_pair(address);
|
||||
}
|
||||
}
|
||||
27
src/client.h
Normal file
27
src/client.h
Normal file
@@ -0,0 +1,27 @@
|
||||
/*
|
||||
* 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 "xml.h"
|
||||
|
||||
#include "limelight-common/Limelight.h"
|
||||
|
||||
void client_init(const char* serverAddress);
|
||||
void client_start_app(STREAM_CONFIGURATION *config, const char* serverAddress, int appId);
|
||||
struct app_list* client_applist(const char* serverAddress);
|
||||
int client_get_app_id(const char* serverAddress, char* name);
|
||||
67
src/connection.c
Normal file
67
src/connection.c
Normal file
@@ -0,0 +1,67 @@
|
||||
/*
|
||||
* 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 "connection.h"
|
||||
|
||||
#include <stdio.h>
|
||||
|
||||
void connection_stage_starting(int stage)
|
||||
{
|
||||
printf("connection_stage_starting %d\n", stage);
|
||||
}
|
||||
|
||||
void connection_stage_complete(int stage)
|
||||
{
|
||||
printf("connection_stage_complete %d\n", stage);
|
||||
}
|
||||
|
||||
void connection_stage_failed(int stage, long iets)
|
||||
{
|
||||
printf("connection_stage_failed %d\n", stage);
|
||||
}
|
||||
|
||||
void connection_connection_started()
|
||||
{
|
||||
printf("connection_connection_started\n");
|
||||
}
|
||||
|
||||
void connection_connection_terminated()
|
||||
{
|
||||
printf("connection_connection_terminated\n");
|
||||
}
|
||||
|
||||
void connection_display_message(char *msg)
|
||||
{
|
||||
printf("connection_display_message: %s\n", msg);
|
||||
}
|
||||
|
||||
void connection_display_transient_message(char *msg)
|
||||
{
|
||||
printf("connection_display_transient_message: %s\n", msg);
|
||||
}
|
||||
|
||||
CONNECTION_LISTENER_CALLBACKS connection_callbacks = {
|
||||
.stageStarting = connection_stage_starting,
|
||||
.stageComplete = connection_stage_complete,
|
||||
.stageFailed = connection_stage_failed,
|
||||
.connectionStarted = connection_connection_started,
|
||||
.connectionTerminated = connection_connection_terminated,
|
||||
.displayMessage = connection_display_message,
|
||||
.displayTransientMessage = connection_display_transient_message,
|
||||
};
|
||||
22
src/connection.h
Normal file
22
src/connection.h
Normal file
@@ -0,0 +1,22 @@
|
||||
/*
|
||||
* 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"
|
||||
|
||||
extern CONNECTION_LISTENER_CALLBACKS connection_callbacks;
|
||||
103
src/http.c
Normal file
103
src/http.c
Normal file
@@ -0,0 +1,103 @@
|
||||
/*
|
||||
* 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 "http.h"
|
||||
|
||||
#include <string.h>
|
||||
#include <curl/curl.h>
|
||||
|
||||
static CURL *curl;
|
||||
|
||||
static const char *pCertFile = "./client.pem";
|
||||
static const char *pKeyFile = "./key.pem";
|
||||
|
||||
static size_t _write_curl(void *contents, size_t size, size_t nmemb, void *userp)
|
||||
{
|
||||
size_t realsize = size * nmemb;
|
||||
struct http_data *mem = (struct http_data *)userp;
|
||||
|
||||
mem->memory = realloc(mem->memory, mem->size + realsize + 1);
|
||||
if(mem->memory == NULL) {
|
||||
fprintf(stderr, "Not enough memory\n");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
memcpy(&(mem->memory[mem->size]), contents, realsize);
|
||||
mem->size += realsize;
|
||||
mem->memory[mem->size] = 0;
|
||||
|
||||
return realsize;
|
||||
}
|
||||
|
||||
void http_init() {
|
||||
curl = curl_easy_init();
|
||||
|
||||
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0L);
|
||||
curl_easy_setopt(curl, CURLOPT_SSLENGINE_DEFAULT, 1L);
|
||||
curl_easy_setopt(curl, CURLOPT_SSLCERTTYPE,"PEM");
|
||||
curl_easy_setopt(curl, CURLOPT_SSLCERT, pCertFile);
|
||||
curl_easy_setopt(curl, CURLOPT_SSLKEYTYPE, "PEM");
|
||||
curl_easy_setopt(curl, CURLOPT_SSLKEY, pKeyFile);
|
||||
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0L);
|
||||
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, _write_curl);
|
||||
}
|
||||
|
||||
int http_request(char* url, struct http_data* data) {
|
||||
curl_easy_setopt(curl, CURLOPT_WRITEDATA, data);
|
||||
curl_easy_setopt(curl, CURLOPT_URL, url);
|
||||
|
||||
if (data->size > 0) {
|
||||
free(data->memory);
|
||||
data->memory = malloc(1);
|
||||
if(data->memory == NULL) {
|
||||
fprintf(stderr, "Not enough memory\n");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
data->size = 0;
|
||||
}
|
||||
CURLcode res = curl_easy_perform(curl);
|
||||
|
||||
if(res != CURLE_OK) {
|
||||
fprintf(stderr, "Connection failed: %s\n", curl_easy_strerror(res));
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
void http_cleanup() {
|
||||
curl_easy_cleanup(curl);
|
||||
}
|
||||
|
||||
struct http_data* http_create_data() {
|
||||
struct http_data* data = malloc(sizeof(struct http_data));
|
||||
data->memory = malloc(1);
|
||||
if(data->memory == NULL) {
|
||||
fprintf(stderr, "Not enough memory\n");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
data->size = 0;
|
||||
|
||||
return data;
|
||||
}
|
||||
|
||||
void http_free_data(struct http_data* data) {
|
||||
free(data->memory);
|
||||
free(data);
|
||||
}
|
||||
31
src/http.h
Normal file
31
src/http.h
Normal file
@@ -0,0 +1,31 @@
|
||||
/*
|
||||
* 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 <stdlib.h>
|
||||
|
||||
struct http_data {
|
||||
char *memory;
|
||||
size_t size;
|
||||
};
|
||||
|
||||
void http_init();
|
||||
int http_request(char* url, struct http_data* data);
|
||||
|
||||
struct http_data* http_create_data();
|
||||
void http_free_data(struct http_data* data);
|
||||
61
src/main.c
Normal file
61
src/main.c
Normal file
@@ -0,0 +1,61 @@
|
||||
/*
|
||||
* 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 "client.h"
|
||||
#include "connection.h"
|
||||
#include "video.h"
|
||||
#include "audio.h"
|
||||
|
||||
#include "limelight-common/Limelight.h"
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <sys/socket.h>
|
||||
#include <netinet/in.h>
|
||||
#include <arpa/inet.h>
|
||||
#include <openssl/rand.h>
|
||||
#include <getopt.h>
|
||||
|
||||
PLATFORM_CALLBACKS platform_callbacks = {
|
||||
.threadStart = NULL,
|
||||
.debugPrint = NULL,
|
||||
};
|
||||
|
||||
int main(int argc, char* argv[]) {
|
||||
STREAM_CONFIGURATION config;
|
||||
config.width = 1280;
|
||||
config.height = 720;
|
||||
config.fps = 60;
|
||||
config.bitrate = 8000;
|
||||
config.packetSize = 1024;
|
||||
|
||||
client_init(argv[1]);
|
||||
|
||||
int appId = client_get_app_id(address, argv[2]);
|
||||
if (appId<0) {
|
||||
printf("Can't find app %s\n", app);
|
||||
exit(-1);
|
||||
}
|
||||
|
||||
client_start_app(config, argv[1], appId);
|
||||
|
||||
struct in_addr addr;
|
||||
inet_aton(address, &addr);
|
||||
LiStartConnection(addr.s_addr, config, &connection_callbacks, &decoder_callbacks, &audio_callbacks, &platform_callbacks, NULL, 0, 4);
|
||||
}
|
||||
60
src/video.c
Normal file
60
src/video.c
Normal file
@@ -0,0 +1,60 @@
|
||||
/*
|
||||
* 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 "video.h"
|
||||
|
||||
#include <stdio.h>
|
||||
|
||||
static FILE* fd;
|
||||
static const char* fileName = "fake.h264";
|
||||
|
||||
static void decoder_renderer_setup(int width, int height, int redrawRate, void* context, int drFlags) {
|
||||
printf("decoder_renderer_setup %dx%d %dfps\n", width, height, redrawRate);
|
||||
fd = fopen(fileName, "w");
|
||||
}
|
||||
|
||||
static void decoder_renderer_start() {
|
||||
printf("decoder_renderer_start\n");
|
||||
}
|
||||
|
||||
static void decoder_renderer_stop() {
|
||||
printf("decoder_renderer_stop\n");
|
||||
}
|
||||
|
||||
static void decoder_renderer_release() {
|
||||
printf("decoder_renderer_release\n");
|
||||
fclose(fd);
|
||||
}
|
||||
|
||||
static int decoder_renderer_submit_decode_unit(PDECODE_UNIT decodeUnit) {
|
||||
PLENTRY entry = decodeUnit->bufferList;
|
||||
while (entry != NULL) {
|
||||
fwrite(entry->data, entry->length, 1, fd);
|
||||
entry = entry->next;
|
||||
}
|
||||
return DR_OK;
|
||||
}
|
||||
|
||||
DECODER_RENDERER_CALLBACKS decoder_callbacks = {
|
||||
.setup = decoder_renderer_setup,
|
||||
.start = decoder_renderer_start,
|
||||
.stop = decoder_renderer_stop,
|
||||
.release = decoder_renderer_release,
|
||||
.submitDecodeUnit = decoder_renderer_submit_decode_unit,
|
||||
};
|
||||
22
src/video.h
Normal file
22
src/video.h
Normal file
@@ -0,0 +1,22 @@
|
||||
/*
|
||||
* 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"
|
||||
|
||||
extern DECODER_RENDERER_CALLBACKS decoder_callbacks;
|
||||
128
src/xml.c
Normal file
128
src/xml.c
Normal file
@@ -0,0 +1,128 @@
|
||||
/*
|
||||
* 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 "xml.h"
|
||||
|
||||
#include "expat.h"
|
||||
|
||||
#include <string.h>
|
||||
|
||||
struct xml_query {
|
||||
char *memory;
|
||||
size_t size;
|
||||
int start;
|
||||
void* data;
|
||||
};
|
||||
|
||||
static void XMLCALL _xml_start_element(void *userData, const char *name, const char **atts) {
|
||||
struct xml_query *search = (struct xml_query*) userData;
|
||||
if (strcmp(search->data, name) == 0)
|
||||
search->start++;
|
||||
}
|
||||
|
||||
static void XMLCALL _xml_end_element(void *userData, const char *name) {
|
||||
struct xml_query *search = (struct xml_query*) userData;
|
||||
if (strcmp(search->data, name) == 0)
|
||||
search->start--;
|
||||
}
|
||||
|
||||
static void XMLCALL _xml_start_applist_element(void *userData, const char *name, const char **atts) {
|
||||
struct xml_query *search = (struct xml_query*) userData;
|
||||
if (strcmp("App", name) == 0) {
|
||||
struct app_list* app = malloc(sizeof(struct app_list));
|
||||
if (app == NULL) {
|
||||
perror("Not enough memory");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
app->next = (struct app_list*) search->data;
|
||||
search->data = app;
|
||||
} else if (strcmp("ID", name) == 0 || strcmp("AppTitle", name) == 0) {
|
||||
search->memory = malloc(1);
|
||||
search->size = 0;
|
||||
search->start = 1;
|
||||
}
|
||||
}
|
||||
|
||||
static void XMLCALL _xml_end_applist_element(void *userData, const char *name) {
|
||||
struct xml_query *search = (struct xml_query*) userData;
|
||||
if (search->start) {
|
||||
struct app_list* list = (struct app_list*) search->data;
|
||||
if (strcmp("ID", name) == 0) {
|
||||
list->id = atoi(search->memory);
|
||||
free(search->memory);
|
||||
} else if (strcmp("AppTitle", name) == 0) {
|
||||
list->name = search->memory;
|
||||
}
|
||||
search->start = 0;
|
||||
}
|
||||
}
|
||||
|
||||
static void XMLCALL _xml_write_data(void *userData, const XML_Char *s, int len) {
|
||||
struct xml_query *search = (struct xml_query*) userData;
|
||||
if (search->start > 0) {
|
||||
search->memory = realloc(search->memory, search->size + len + 1);
|
||||
if(search->memory == NULL) {
|
||||
fprintf(stderr, "Not enough memory\n");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
memcpy(&(search->memory[search->size]), s, len);
|
||||
search->size += len;
|
||||
search->memory[search->size] = 0;
|
||||
}
|
||||
}
|
||||
|
||||
int xml_search(char* data, size_t len, char* node, char** result) {
|
||||
struct xml_query search;
|
||||
search.data = node;
|
||||
search.start = 0;
|
||||
search.memory = malloc(1);
|
||||
search.size = 0;
|
||||
XML_Parser parser = XML_ParserCreate("UTF-8");
|
||||
XML_SetUserData(parser, &search);
|
||||
XML_SetElementHandler(parser, _xml_start_element, _xml_end_element);
|
||||
XML_SetCharacterDataHandler(parser, _xml_write_data);
|
||||
if (! XML_Parse(parser, data, len, 1)) {
|
||||
int code = XML_GetErrorCode(parser);
|
||||
printf("%s\n", XML_ErrorString(code));
|
||||
return 1;
|
||||
}
|
||||
*result = search.memory;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
struct app_list* xml_applist(char* data, size_t len) {
|
||||
struct xml_query query;
|
||||
query.memory = malloc(1);
|
||||
query.size = 0;
|
||||
query.start = 0;
|
||||
query.data = NULL;
|
||||
XML_Parser parser = XML_ParserCreate("UTF-8");
|
||||
XML_SetUserData(parser, &query);
|
||||
XML_SetElementHandler(parser, _xml_start_applist_element, _xml_end_applist_element);
|
||||
XML_SetCharacterDataHandler(parser, _xml_write_data);
|
||||
if (! XML_Parse(parser, data, len, 1)) {
|
||||
int code = XML_GetErrorCode(parser);
|
||||
printf("%s\n", XML_ErrorString(code));
|
||||
exit(-1);
|
||||
}
|
||||
|
||||
return (struct app_list*) query.data;
|
||||
}
|
||||
29
src/xml.h
Normal file
29
src/xml.h
Normal file
@@ -0,0 +1,29 @@
|
||||
/*
|
||||
* 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 <stdio.h>
|
||||
|
||||
struct app_list {
|
||||
char* name;
|
||||
int id;
|
||||
struct app_list* next;
|
||||
};
|
||||
|
||||
int xml_search(char* data, size_t len, char* node, char** result);
|
||||
struct app_list* xml_applist(char* data, size_t len);
|
||||
Reference in New Issue
Block a user