This repository has been archived by the owner on Jun 22, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
orp.h
468 lines (412 loc) · 9.72 KB
/
orp.h
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
//////////////////////////////////////////////////////////////////////////////
//
// Open Remote Play
// http://code.google.com/p/open-rp/
//
//////////////////////////////////////////////////////////////////////////////
//
// 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 2 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.,
// 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
//
//////////////////////////////////////////////////////////////////////////////
#ifndef _ORP_H
#define _ORP_H
using namespace std;
#include <curl/curl.h>
#include <openssl/aes.h>
#include <SDL/SDL.h>
#include <SDL/SDL_thread.h>
#include <SDL/SDL_image.h>
#include <SDL/SDL_net.h>
#include <SDL/SDL_ttf.h>
extern "C" {
#include <libavutil/avutil.h>
#include <libavcodec/avcodec.h>
#include <libswscale/swscale.h>
};
#include <string>
#include <vector>
#include <deque>
#include <queue>
#include "yuv.h"
#include "orp-conf.h"
#define ORP_PREMO_VMAJOR 0
#define ORP_PREMO_VMINOR 3
#define ORP_FRAME_WIDTH 480
#define ORP_FRAME_HEIGHT 272
#define ORP_OVERLAY_HEIGHT 32
#define ORP_SESSION_LEN 16
#define ORP_PADSTATE_MAX 60
#define ORP_PADSTATE_LEN 128
#define ORP_CLOCKFREQ 90000
#define ORP_VIDEO_MAXQUEUE 2
#define ORP_AUDIO_BUF_LEN 1024
#define ORP_AUDIO_DIFFAVGNB 20
#define ORP_AUDIO_NOSYNC 100.0
#define ORP_AUDIO_SAMPLE_CORRECTION_PERCENT_MAX 10
#define ORP_USER_AGENT "premo/1.0.0 libhttp/1.0.0"
#define ORP_GET_SESSION "/sce/premo/session"
#define ORP_GET_CTRL "/sce/premo/session/ctrl"
#define ORP_GET_VIDEO "/sce/premo/session/video"
#define ORP_GET_AUDIO "/sce/premo/session/audio"
#define ORP_POST_PAD "/sce/premo/session/pad"
// PSP PAD offsets and bits
#define ORP_PAD_PSP_HOME 0x0101
#define ORP_PAD_PSP_SELECT 0x0501
#define ORP_PAD_PSP_L3 0x0502
#define ORP_PAD_PSP_R3 0x0504
#define ORP_PAD_PSP_START 0x0508
#define ORP_PAD_PSP_DPUP 0x0510
#define ORP_PAD_PSP_DPRIGHT 0x0520
#define ORP_PAD_PSP_DPDOWN 0x0540
#define ORP_PAD_PSP_DPLEFT 0x0580
#define ORP_PAD_PSP_R2 0x0701
#define ORP_PAD_PSP_L2 0x0702
#define ORP_PAD_PSP_L1 0x0704
#define ORP_PAD_PSP_R1 0x0708
#define ORP_PAD_PSP_TRI 0x0710
#define ORP_PAD_PSP_CIRCLE 0x0720
#define ORP_PAD_PSP_X 0x0740
#define ORP_PAD_PSP_SQUARE 0x0780
#define ORP_PAD_PSP_RXAXIS 0x08
#define ORP_PAD_PSP_RYAXIS 0x0a
#define ORP_PAD_PSP_LXAXIS 0x0c
#define ORP_PAD_PSP_LYAXIS 0x0e
#define ORP_DS3_SELECT 0x00
#define ORP_DS3_L3 0x01
#define ORP_DS3_R3 0x02
#define ORP_DS3_START 0x03
#define ORP_DS3_DPUP 0x04
#define ORP_DS3_DPRIGHT 0x05
#define ORP_DS3_DPDOWN 0x06
#define ORP_DS3_DPLEFT 0x07
#define ORP_DS3_L2 0x08
#define ORP_DS3_R2 0x09
#define ORP_DS3_L1 0x0a
#define ORP_DS3_R1 0x0b
#define ORP_DS3_TRI 0x0c
#define ORP_DS3_CIRCLE 0x0d
#define ORP_DS3_X 0x0e
#define ORP_DS3_SQUARE 0x0f
#define ORP_DS3_HOME 0x10
#define ORP_PAD_TIMESTAMP 0x40
#define ORP_PAD_EVENTID 0x48
#define ORP_PAD_KEYUP 0x10000000
#define ORP_PAD_KEYDOWN 0x20000000
#define ORP_SRCH_REPLIES 5
#define ORP_SRCH_TIMEOUT 30
// UDP broadcast from PSP to *:9293
#define ORP_ANNOUNCE_SRCH "SRCH"
struct PktAnnounceSrch_t {
Uint8 id[4];
};
#define CreatePktAnnounceSrch(pkt) \
memcpy(pkt.id, ORP_ANNOUNCE_SRCH, 4);
// UDP reply from PlayStation 3, 156 bytes
#define ORP_ANNOUNCE_RESP "RESP"
struct PktAnnounceResp_t {
Uint8 id[4]; // 4
Uint8 unk0[6]; // 10
Uint8 ps3_mac[ORP_MAC_LEN]; // 16
Uint8 ps3_nickname[ORP_NICKNAME_LEN];
// 144
#define ORP_PS3_NPX_LEN 12
Uint8 npx[ORP_PS3_NPX_LEN];
// 156
};
enum orpHeader {
HEADER_NULL,
HEADER_APP_REASON,
HEADER_AUDIO_BITRATE,
HEADER_AUDIO_BITRATE_ABILITY,
HEADER_AUDIO_CHANNELS,
HEADER_AUDIO_CLOCKFREQ,
HEADER_AUDIO_CODEC,
HEADER_AUDIO_CONFIG,
HEADER_AUDIO_SAMPLERATE,
HEADER_AUTH,
HEADER_CTRL_BITRATE,
HEADER_CTRL_MAXBITRATE,
HEADER_CTRL_MODE,
HEADER_EXEC_MODE,
HEADER_MODE,
HEADER_NONCE,
HEADER_PAD_ASSIGN,
HEADER_PAD_COMPLETE,
HEADER_PAD_INDEX,
HEADER_PAD_INFO,
HEADER_PLATFORM_INFO,
HEADER_POWER_CONTROL,
HEADER_PS3_NICKNAME,
HEADER_PSPID,
HEADER_SESSIONID,
HEADER_SIGNINID,
HEADER_TRANS,
HEADER_TRANS_MODE,
HEADER_USERNAME,
HEADER_VERSION,
HEADER_VIDEO_BITRATE,
HEADER_VIDEO_BITRATE_ABILITY,
HEADER_VIDEO_CLOCKFREQ,
HEADER_VIDEO_CODEC,
HEADER_VIDEO_CONFIG,
HEADER_VIDEO_FRAMERATE,
HEADER_VIDEO_FRAMERATE_ABILITY,
HEADER_VIDEO_RESOLUTION,
HEADER_VIDEO_RESOLUTION_ABILITY,
};
struct orpHeader_t {
enum orpHeader header;
string name;
};
struct orpHeaderValue_t {
enum orpHeader header;
string value;
};
enum orpCtrlMode {
CTRL_CHANGE_BITRATE,
CTRL_SESSION_TERM,
CTRL_NULL,
};
struct orpCtrlMode_t {
enum orpCtrlMode mode;
string param1;
string param2;
};
enum orpCtrlBitrate {
CTRL_BR_256,
CTRL_BR_384,
CTRL_BR_512,
CTRL_BR_768,
CTRL_BR_1024
};
#define ORP_KBMAP_LEN 60
#define ORP_KBMAP_SX 4
#define ORP_KBMAP_SY 2
struct orpKeyboardMap_t {
SDLKey sym;
Uint32 mod;
Sint8 x;
Sint8 y;
};
enum orpEvent {
EVENT_ERROR,
EVENT_RESTORE,
EVENT_SHUTDOWN,
EVENT_STREAM_EXIT
};
enum orpViewSize {
VIEW_NORMAL,
VIEW_MEDIUM,
VIEW_LARGE,
VIEW_FULLSCREEN
};
struct orpView_t {
enum orpViewSize size;
enum orpViewSize prev;
SDL_Surface *view;
SDL_Overlay *overlay;
SDL_Surface *status_bg;
SDL_YUVSurface *status_yuv;
Uint32 status_ticks;
bool status_sticky;
SDL_Rect fs;
SDL_Rect scale;
SDL_Rect desktop;
SDL_mutex *lock;
};
enum orpAuthType {
orpAUTH_NORMAL,
orpAUTH_CHANGE_BITRATE,
orpAUTH_SESSION_TERM
};
struct orpKey_t {
Uint8 skey0[ORP_KEY_LEN];
Uint8 skey1[ORP_KEY_LEN];
Uint8 skey2[ORP_KEY_LEN];
Uint8 psp_id[ORP_KEY_LEN];
Uint8 pkey[ORP_KEY_LEN];
Uint8 xor_pkey[ORP_KEY_LEN];
Uint8 nonce[ORP_KEY_LEN];
Uint8 xor_nonce[ORP_KEY_LEN];
Uint8 iv1[ORP_KEY_LEN];
Uint8 *auth_normal;
Uint8 *auth_change_bitrate;
Uint8 *auth_session_term;
};
struct orpConfig_t {
Uint8 ps3_mac[ORP_MAC_LEN];
char psp_owner[ORP_NICKNAME_LEN];
Uint8 psp_mac[ORP_MAC_LEN];
char ps3_addr[ORP_HOSTNAME_LEN];
Uint16 ps3_port;
bool ps3_search;
bool ps3_wolr;
bool net_public;
struct orpKey_t key;
char psn_login[ORP_NICKNAME_LEN];
enum orpCtrlBitrate bitrate;
};
struct orpStreamPacketHeader_t {
Uint8 magic[2]; // 2
Uint16 frame; // 4
Uint32 clock; // 8
Uint8 root[4]; // 12
Uint16 unk2; // 14
Uint16 unk3; // 16
Uint16 len; // 18
Uint16 unk4; // 20
Uint16 unk5; // 22
Uint16 unk6; // 24
Uint16 unk7; // 26
Uint16 unk8; // 28
Uint16 unk9; // 30
Uint16 unk10; // 32
};
struct orpStreamPacket_t {
struct orpStreamPacketHeader_t header;
AVPacket pkt;
};
struct orpStreamData_t {
Uint32 len;
Uint32 pos;
Uint8 *data;
SDL_mutex *lock;
SDL_cond *cond;
queue<struct orpStreamPacket_t *> pkt;
};
struct orpClock_t {
Uint32 audio;
Uint32 audio_freq;
Uint32 video;
Uint32 video_freq;
Uint32 master;
Uint32 decode;
Uint32 audio_queue;
Uint32 video_queue;
SDL_mutex *lock;
};
struct orpThreadAudioDecode_t {
bool terminate;
AVCodec *codec;
Sint32 channels;
Sint32 sample_rate;
Sint32 bit_rate;
struct orpClock_t *clock;
struct orpStreamData_t *stream;
};
struct orpThreadVideoDecode_t {
bool terminate;
AVCodec *codec;
Sint32 frame_rate;
Uint32 clock_offset;
struct orpView_t *view;
struct orpClock_t *clock;
struct orpStreamData_t *stream;
};
enum orpStreamType {
ST_AUDIO,
ST_VIDEO
};
struct orpConfigStream_t {
string name;
enum orpStreamType type;
#ifdef ORP_DUMP_STREAM_HEADER
FILE *h_header;
#endif
#ifdef ORP_DUMP_STREAM_DATA
FILE *h_data;
#endif
#ifdef ORP_DUMP_STREAM_RAW
FILE *h_raw;
#endif
string url;
string host;
Uint16 port;
string codec;
string session_id;
AES_KEY aes_key;
struct orpKey_t key;
struct orpStreamData_t *stream;
};
struct orpAudioFrame_t {
Uint32 len;
Uint8 *data;
Uint32 clock;
Uint32 consumed;
};
struct orpConfigAudioFeed_t {
SDL_mutex *lock;
Sint32 channels;
Sint32 sample_rate;
double audio_diff_cum;
double audio_diff_avg_coef;
double audio_diff_threshold;
Uint32 audio_diff_avg_count;
Uint32 clock_offset;
struct orpClock_t *clock;
deque<struct orpAudioFrame_t *> frame;
};
struct orpCodec_t {
string name;
AVCodec *codec;
};
class OpenRemotePlay
{
public:
OpenRemotePlay(struct orpConfig_t *config);
~OpenRemotePlay();
bool SessionCreate(void);
void SessionDestroy(void);
protected:
bool terminate;
struct orpConfig_t config;
vector<struct orpCodec_t *> codec;
struct orpView_t view;
string session_id;
char *ps3_nickname;
ostringstream os_caption;
string exec_mode;
SDL_Thread *thread_video_connection;
SDL_Thread *thread_video_decode;
SDL_Thread *thread_audio_connection;
SDL_Thread *thread_audio_decode;
SDL_Joystick *js;
struct orpClock_t clock;
#ifdef ORP_CLOCK_DEBUG
SDL_TimerID timer;
#endif
TCPsocket skt_pad;
TTF_Font *font_small;
TTF_Font *font_normal;
SDL_Surface *splash;
SDL_Surface *mode_game;
SDL_Surface *mode_ps1;
SDL_Surface *mode_vsh;
Uint32 rmask, gmask, bmask, amask;
bool CreateView(void);
bool CreateKeys(const string &nonce,
enum orpAuthType type = orpAUTH_NORMAL);
bool SetCaption(const char *caption);
AVCodec *GetCodec(const string &name);
Sint32 ControlPerform(CURL *curl, struct orpCtrlMode_t *mode);
Sint32 SendPadState(Uint8 *pad, Uint32 id,
Uint32 &count, Uint32 timestamp, vector<string> &headers);
Sint32 SessionControl(CURL *curl);
Sint32 SessionPerform(void);
void DisplayError(const char *text);
void UpdateOverlay(void);
};
#endif // _ORP_H
// vi: ts=4