-
Notifications
You must be signed in to change notification settings - Fork 22
/
htsp.c
793 lines (646 loc) · 20.9 KB
/
htsp.c
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
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
/*
pidvbip - tvheadend client for the Raspberry Pi
(C) Dave Chapman 2012-2013
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., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
#include "common.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdarg.h>
#include <sys/socket.h>
#include <sys/select.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <string.h>
#include "sha1.h"
#include "htsp.h"
#include "debug.h"
typedef struct HTSSHA1 {
uint64_t count;
uint8_t buffer[64];
uint32_t state[5];
} HTSSHA1;
void htsp_init(struct htsp_t* htsp)
{
htsp->subscriptionId = 0;
htsp->subscriptionServer = -1;
htsp->sync_completed = 0;
}
static int create_tcp_socket()
{
int sock;
if((sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0){
perror("Can't create TCP socket");
exit(1);
}
return sock;
}
static char *get_ip(char *host)
{
struct hostent *hent;
char *ip = calloc(INET_ADDRSTRLEN,1);
if((hent = gethostbyname(host)) == NULL)
{
herror("Can't get IP");
exit(1);
}
if(inet_ntop(AF_INET, (void *)hent->h_addr_list[0], ip, INET_ADDRSTRLEN) == NULL)
{
perror("Can't resolve host");
exit(1);
}
return ip;
}
int htsp_connect(struct htsp_t* htsp, int server)
{
int res;
int i;
htsp->sock[server] = create_tcp_socket();
if (htsp->ip[server] == NULL)
htsp->ip[server] = get_ip(htsp->host[server]);
fprintf(stderr,"Connecting to server %d: %s (%s) port %d...\n",server, htsp->host[server],htsp->ip[server],htsp->port[server]);
htsp->remote[server] = (struct sockaddr_in *)malloc(sizeof(struct sockaddr_in));
htsp->remote[server]->sin_family = AF_INET;
res = inet_pton(AF_INET, htsp->ip[server], (void *)(&(htsp->remote[server]->sin_addr.s_addr)));
if (res < 0) {
perror("Can't set remote->sin_addr.s_addr");
exit(1);
} else if (res == 0) {
fprintf(stderr, "%s is not a valid IP address\n", htsp->ip[server]);
return 1;
}
htsp->remote[server]->sin_port = htons(htsp->port[server]);
if (connect(htsp->sock[server], (struct sockaddr *)htsp->remote[server], sizeof(struct sockaddr)) < 0){
perror("Could not connect");
return 2;
}
return 0;
}
static char* hmf_labels[] = { "", "MAP","S64","STR","BIN","LIST","DBL" };
static int get_uint32_be(unsigned char* buf)
{
return((buf[0] << 24) | (buf[1] << 16) | (buf[2] << 8) | buf[3]);
}
static void put_uint32_be(unsigned char* buf, uint32_t x)
{
buf[0] = (x >> 24) & 0xff;
buf[1] = (x >> 16) & 0xff;
buf[2] = (x >> 8) & 0xff;
buf[3] = x & 0xff;
}
void htsp_dump_binary(unsigned char* buf, int len)
{
int i;
uint64_t x = 0;
while (len > 0) {
int type = buf[0]; if (type > 6) { type = 0; }
int namelength = buf[1];
int datalength = get_uint32_be(buf + 2);
buf += 6; len -= 6;
fprintf(stderr,"type=%d, datalen=%d %s: ",type,datalength,hmf_labels[type]);
for (i=0;i<namelength;i++) { fprintf(stderr,"%c",buf[i]); }
buf += namelength; len -= namelength;
fprintf(stderr,"=");
switch (type) {
case HMF_STR:
fprintf(stderr,"\"");
for (i=0;i<datalength;i++) { fprintf(stderr,"%c",buf[i]); }
fprintf(stderr,"\"");
break;
case HMF_BIN:
fprintf(stderr,"\"");
//for (i=0;i<datalength;i++) { fprintf(stderr," 0x%02x",buf[i]); }
fprintf(stderr,"\"");
break;
case HMF_S64:
x = 0;
for (i=datalength-1;i>=0;i--) {
x <<= 8;
x |= buf[i];
}
fprintf(stderr,"%lld",x);
fprintf(stderr," [");
for (i=0;i<datalength;i++) { fprintf(stderr," 0x%02x",buf[i]); }
fprintf(stderr," ]");
break;
case HMF_LIST:
case HMF_MAP:
fprintf(stderr,"\n");
htsp_dump_binary(buf, datalength);
break;
default:
break;
}
fprintf(stderr,"\n");
buf += datalength; len -= datalength;
}
}
void htsp_dump_message(struct htsp_message_t* msg)
{
htsp_dump_binary(msg->msg + 4, msg->msglen - 4);
}
void htsp_destroy_message(struct htsp_message_t* msg)
{
free(msg->msg);
}
int htsp_create_message(struct htsp_message_t* msg, ...)
{
va_list argp;
int type;
char* fieldname;
int fieldnamelen;
int datalen;
char* s;
int i;
uint64_t ii;
unsigned char* d;
unsigned char* p;
msg->msglen = 4;
va_start(argp,msg);
type = va_arg(argp, int);
while (type != HMF_NULL) {
msg->msglen++;
fieldname = va_arg(argp,char*);
msg->msglen += strlen(fieldname) + 1 + 4;
switch(type) {
case HMF_STR:
s = va_arg(argp,char*);
msg->msglen += strlen(s);
break;
case HMF_S64:
i = va_arg(argp,int);
ii = i;
while (ii > 0) {
msg->msglen++;
ii >>= 8;
}
break;
case HMF_BIN:
i = va_arg(argp,int);
va_arg(argp,unsigned char*);
msg->msglen += i;
break;
default:
fprintf(stderr,"FATAL ERROR: Unsupported type in htsp_create_message (%d)\n",type);
exit(1);
break;
}
type = va_arg(argp, int);
}
va_end(argp);
msg->msg = malloc(msg->msglen);
p = msg->msg;
put_uint32_be(p,msg->msglen - 4); p += 4;
va_start(argp,msg);
type = va_arg(argp, int);
while (type != HMF_NULL) {
*p++ = type;
fieldname = va_arg(argp,char*);
fieldnamelen = strlen(fieldname);
*p++ = fieldnamelen;
switch(type) {
case HMF_STR:
s = va_arg(argp,char*);
datalen = strlen(s);
put_uint32_be(p,datalen); p += 4;
memcpy(p,fieldname,fieldnamelen); p += fieldnamelen;
memcpy(p,s,datalen); p += datalen;
break;
case HMF_S64:
i = va_arg(argp,int);
ii = i;
datalen = 0;
while (ii > 0) {
datalen++;
ii >>= 8;
}
put_uint32_be(p,datalen); p += 4;
memcpy(p,fieldname,fieldnamelen); p += fieldnamelen;
ii = i;
while (ii > 0) {
*p++ = (ii & 0xff);
ii >>= 8;
}
break;
case HMF_BIN:
datalen = va_arg(argp,int);
d = va_arg(argp,unsigned char*);
put_uint32_be(p,datalen); p += 4;
memcpy(p,fieldname,fieldnamelen); p += fieldnamelen;
memcpy(p,d,datalen); p += datalen;
break;
default:
fprintf(stderr,"FATAL ERROR: Unsupported type in htsp_create_message (%d)\n",type);
exit(1);
break;
}
type = va_arg(argp, int);
}
va_end(argp);
return 0;
}
int htsp_send_message(struct htsp_t* htsp, int server, struct htsp_message_t* msg)
{
int res;
int sent = 0;
while(sent < msg->msglen)
{
res = send(htsp->sock[server], msg->msg + sent, msg->msglen-sent, 0);
if(res == -1){
perror("Can't send query");
return 1;
}
sent += res;
}
return 0;
}
int safe_recv(int sock, unsigned char* p, int bytesleft)
{
int res;
while (bytesleft) {
res = recv(sock, p, bytesleft, 0);
if (res < 0) {
fprintf(stderr,"Error in recv\n");
return res;
}
p += res;
bytesleft -= res;
}
return 0;
}
static int do_htsp_recv_message(int fd, struct htsp_message_t* msg)
{
unsigned char buf[4];
int res;
//fprintf(stderr,"Waiting for response...\n");
res = safe_recv(fd, buf, 4);
if (res < 0) {
fprintf(stderr,"Error in recv - res=%d\n",res);
return 2;
}
msg->msglen = get_uint32_be(buf);
msg->msg = malloc(msg->msglen + 4);
if (msg->msg == NULL) {
fprintf(stderr,"FATAL ERROR - out of memory (tried to allocate %d bytes)\n",msg->msglen + 4);
exit(1);
}
memcpy(msg->msg, buf, 4);
res = safe_recv(fd, msg->msg+4, msg->msglen);
return 0;
}
int htsp_recv_message(struct htsp_t* htsp, int server, struct htsp_message_t* msg, int timeout)
{
int res;
int max_fd;
int i;
fd_set fds;
if (timeout) {
struct timeval tv = { 0L, timeout*1000 }; /* timeout in ms */
FD_ZERO(&fds);
if (server >= 0) {
FD_SET(htsp->sock[server], &fds);
max_fd = htsp->sock[server];
} else {
max_fd = 0;
for (i=0;i<htsp->numservers;i++) {
FD_SET(htsp->sock[i], &fds);
if (max_fd < htsp->sock[i])
max_fd = htsp->sock[i];
}
}
if (select(max_fd + 1, &fds, NULL, NULL, &tv)==0) {
return 1;
}
}
if (server >= 0) {
msg->server = server;
return do_htsp_recv_message(htsp->sock[server], msg);
} else {
for (i=0;i<htsp->numservers;i++) {
if (FD_ISSET(htsp->sock[i],&fds)) {
msg->server = i;
return do_htsp_recv_message(htsp->sock[i], msg);
}
}
}
}
int htsp_login(struct htsp_t* htsp, int server, char* tvh_user, char* tvh_pass)
{
struct htsp_message_t msg;
int res;
HTSSHA1 *shactx = (HTSSHA1*)malloc(hts_sha1_size);
uint8_t d[20];
unsigned char* chall;
int chall_len = 0,res_access = 0;
htsp_create_message(&msg,HMF_STR,"method","hello",
HMF_STR,"clientname","pidvbip",
HMF_S64,"htspversion",1,
HMF_S64,"seq",1,
HMF_NULL);
//fprintf(stderr,"Sending hello message - %d bytes\n",msg.msglen);
if ((res = htsp_send_message(htsp,server,&msg)) > 0) {
fprintf(stderr,"Could not send message\n");
return 1;
}
htsp_destroy_message(&msg);
res = htsp_recv_message(htsp,server,&msg,0);
if (res > 0) {
fprintf(stderr,"Error receiving hello response\n");
return 1;
} else {
htsp_get_string(&msg, "method");
htsp_get_bin(&msg, "challenge", &chall, &chall_len);
}
htsp_destroy_message(&msg);
if ((tvh_user) && (tvh_pass)) {
// Now authenticate
fprintf(stderr,"Authenticating with user: %s pass: %s\n",tvh_user,tvh_pass);
hts_sha1_init(shactx);
hts_sha1_update(shactx, (const uint8_t *)tvh_pass, strlen(tvh_pass));
hts_sha1_update(shactx, (const uint8_t *)chall, chall_len);
hts_sha1_final(shactx, d);
htsp_create_message(&msg,HMF_STR,"method","authenticate",
HMF_STR,"username",tvh_user,
HMF_BIN,"digest",20,d,
HMF_NULL);
if ((res = htsp_send_message(htsp,server,&msg)) > 0) {
fprintf(stderr,"Could not send message (authenticate)\n");
return 1;
};
htsp_destroy_message(&msg);
res = htsp_recv_message(htsp,server,&msg,0);
if (res > 0) {
fprintf(stderr,"Error receiving login response\n");
return 1;
} else {
htsp_get_int(&msg, "noaccess",&res_access);
fprintf(stderr,"Received htsp (login): %d - noaccess %d\n",res,res_access);
if (res_access == 1) {
fprintf(stderr,"Login FAILURE - No access with username and password\n");
return 1;
};
};
htsp_destroy_message(&msg);
}
return 0;
}
char* htsp_get_string(struct htsp_message_t* msg, char* name)
{
unsigned char* buf = msg->msg;
int len = msg->msglen;
int msglen = get_uint32_be(buf); buf += 4; len -= 4;
int matchlen = strlen(name);
char* s;
while (len > 0) {
int type = buf[0]; if (type > 6) { type = 0; }
int namelength = buf[1];
int datalength = get_uint32_be(buf + 2);
buf += 6; len -= 6;
if ((type == HMF_STR) && (namelength==matchlen) && (memcmp(buf,name,matchlen)==0)) {
s = malloc(datalength+1);
memcpy(s,buf + namelength,datalength);
s[datalength]=0;
return s;
}
buf += namelength + datalength;
len -= namelength + datalength;
}
return NULL;
}
int htsp_get_int(struct htsp_message_t* msg, char* name, int32_t* val)
{
unsigned char* buf = msg->msg;
int len = msg->msglen;
int msglen = get_uint32_be(buf); buf += 4; len -= 4;
int matchlen = strlen(name);
while (len > 0) {
int type = buf[0]; if (type > 6) { type = 0; }
int namelength = buf[1];
int datalength = get_uint32_be(buf + 2);
buf += 6; len -= 6;
if ((type == HMF_S64) && (namelength==matchlen) && (memcmp(buf,name,matchlen)==0)) {
buf += namelength + datalength - 1; // We decode backwards, it's little-endian
*val = 0;
while (datalength > 0) {
*val <<= 8;
*val |= *buf--;
datalength--;
}
return 0;
}
buf += namelength + datalength;
len -= namelength + datalength;
}
return 1;
}
int htsp_get_uint(struct htsp_message_t* msg, char* name, uint32_t* val)
{
return htsp_get_int(msg,name,(int32_t*)val);
}
int htsp_get_int64(struct htsp_message_t* msg, char* name, int64_t* val)
{
unsigned char* buf = msg->msg;
int len = msg->msglen;
int msglen = get_uint32_be(buf); buf += 4; len -= 4;
int matchlen = strlen(name);
while (len > 0) {
int type = buf[0]; if (type > 6) { type = 0; }
int namelength = buf[1];
int datalength = get_uint32_be(buf + 2);
buf += 6; len -= 6;
if ((type == HMF_S64) && (namelength==matchlen) && (memcmp(buf,name,matchlen)==0)) {
buf += namelength + datalength - 1; // We decode backwards, it's little-endian
*val = 0;
while (datalength > 0) {
*val <<= 8;
*val |= *buf--;
datalength--;
}
return 0;
}
buf += namelength + datalength;
len -= namelength + datalength;
}
return 1;
}
int htsp_get_bin(struct htsp_message_t* msg, char* name, unsigned char** data,int* size)
{
unsigned char* buf = msg->msg;
int len = msg->msglen;
int msglen = get_uint32_be(buf); buf += 4; len -= 4;
int matchlen = strlen(name);
while (len > 0) {
int type = buf[0]; if (type > 6) { type = 0; }
int namelength = buf[1];
int datalength = get_uint32_be(buf + 2);
buf += 6; len -= 6;
if ((type == HMF_BIN) && (namelength==matchlen) && (memcmp(buf,name,matchlen)==0)) {
*data = buf + namelength;
*size = datalength;
return 0;
}
buf += namelength + datalength;
len -= namelength + datalength;
}
return 1;
}
int htsp_get_list(struct htsp_message_t* msg, char* name, unsigned char** data,int* size)
{
unsigned char* buf = msg->msg;
int len = msg->msglen;
int msglen = get_uint32_be(buf); buf += 4; len -= 4;
int matchlen = strlen(name);
while (len > 0) {
int type = buf[0]; if (type > 6) { type = 0; }
int namelength = buf[1];
int datalength = get_uint32_be(buf + 2);
buf += 6; len -= 6;
if ((type == HMF_LIST) && (namelength==matchlen) && (memcmp(buf,name,matchlen)==0)) {
*data = buf + namelength;
*size = datalength;
return 0;
}
buf += namelength + datalength;
len -= namelength + datalength;
}
return 1;
}
/* Calculate a priority value (higher the better) for a language string.
This is currently hard-coded to select English audio from Spanish DVB-T,
but should be user-configurable. */
static int audio_lang_priority(char* lang)
{
if (!lang) return 0;
/* Whitelist */
if (!strcmp(lang,"eng")) return 99;
/* The following are the codes used on Spanish DVB-T for alternative (mostly English) language tracks */
if (!strcmp(lang,"v.o")) return 10;
if (!strcmp(lang,"und")) return 1;
if (!strcmp(lang,"qaa")) return 1;
if (!strcmp(lang,"mul")) return 1;
/* Blacklist */
if (!strcmp(lang,"cat")) return -1;
if (!strcmp(lang,"spa")) return -2;
return 0;
}
int htsp_parse_subscriptionStart(struct htsp_message_t* msg, struct htsp_subscription_t* subscription)
{
unsigned char* list;
int listlen;
int i;
if (htsp_get_list(msg,"streams",&list,&listlen) > 0)
return 1;
unsigned char* buf = list;
subscription->numstreams = 0;
subscription->numaudiostreams = 0;
subscription->videostream = -1;
subscription->audiostream = -1;
int len = listlen;
while (len > 0) {
subscription->numstreams++;
int type = buf[0]; if (type > 6) { type = 0; }
int namelength = buf[1];
int datalength = get_uint32_be(buf + 2);
buf += 6 + namelength + datalength;
len -= 6 + namelength + datalength;
}
subscription->streams = malloc(subscription->numstreams * sizeof(struct htsp_stream_t));
len = listlen;
buf = list;
i = 0;
while (len > 0) {
int type = buf[0]; if (type > 6) { type = 0; }
int namelength = buf[1];
int datalength = get_uint32_be(buf + 2);
buf += 6; len -= 6;
struct htsp_message_t tmpmsg;
tmpmsg.msg = buf + namelength - 4;
tmpmsg.msglen = datalength;
htsp_get_int(&tmpmsg,"index",&subscription->streams[i].index);
char* typestr = htsp_get_string(&tmpmsg,"type");
char* lang = htsp_get_string(&tmpmsg,"language");
fprintf(stderr,"%d %s %s\n",subscription->streams[i].index, typestr, lang);
if (typestr==NULL)
return 1;
if (lang)
strncpy(subscription->streams[i].lang,lang,4);
if (strcmp(typestr,"MPEG2VIDEO")==0) {
subscription->streams[i].type = HMF_STREAM_VIDEO;
subscription->streams[i].codec = HMF_VIDEO_CODEC_MPEG2;
subscription->videostream = i;
DEBUGF("Video stream is index %d: MPEG-2\n",subscription->streams[i].index);
} else if (strcmp(typestr,"H264")==0) {
subscription->streams[i].type = HMF_STREAM_VIDEO;
subscription->streams[i].codec = HMF_VIDEO_CODEC_H264;
subscription->videostream = i;
DEBUGF("Video stream is index %d: H264\n",subscription->streams[i].index);
} else if (strcmp(typestr,"MPEG2AUDIO")==0) {
subscription->streams[i].type = HMF_STREAM_AUDIO;
subscription->streams[i].codec = HMF_AUDIO_CODEC_MPEG;
if ((subscription->audiostream == -1) || (audio_lang_priority(lang) > audio_lang_priority(subscription->streams[subscription->audiostream].lang))) {
subscription->audiostream = i;
fprintf(stderr,"Audio stream is index %d: MPEG (i=%d)\n",subscription->streams[i].index,i);
}
} else if (strcmp(typestr,"AAC")==0) {
subscription->streams[i].type = HMF_STREAM_AUDIO;
subscription->streams[i].codec = HMF_AUDIO_CODEC_AAC;
if ((subscription->audiostream == -1) || (audio_lang_priority(lang) > audio_lang_priority(subscription->streams[subscription->audiostream].lang))) {
subscription->audiostream = i;
fprintf(stderr,"Audio stream is index %d: AAC\n",subscription->streams[i].index);
}
} else if (strcmp(typestr,"AC3")==0) {
subscription->streams[i].type = HMF_STREAM_AUDIO;
subscription->streams[i].codec = HMF_AUDIO_CODEC_AC3;
/* AC3 takes precedence over MPEG, if the language is the same or greater priority */
if ((subscription->audiostream == -1) || (audio_lang_priority(lang) > audio_lang_priority(subscription->streams[subscription->audiostream].lang)) ||
((subscription->streams[subscription->audiostream].codec == HMF_AUDIO_CODEC_MPEG) && (audio_lang_priority(lang) == audio_lang_priority(subscription->streams[subscription->audiostream].lang)))) {
subscription->audiostream = i;
fprintf(stderr,"Audio stream is index %d: A/52 (%d,%d)\n",subscription->streams[i].index,audio_lang_priority(lang),audio_lang_priority(subscription->streams[subscription->audiostream].lang));
}
} else if (strcmp(typestr,"DVBSUB")==0) {
subscription->streams[i].type = HMF_STREAM_SUB;
subscription->streams[i].codec = HMF_SUB_CODEC_DVBSUB;
} else {
subscription->streams[i].type = HMF_UNKNOWN;
subscription->streams[i].codec = HMF_UNKNOWN;
fprintf(stderr,"Warning: Unknown stream type \"%s\"\n",typestr);
}
if (subscription->streams[i].type == HMF_STREAM_VIDEO) {
htsp_get_int(&tmpmsg,"width",&subscription->streams[i].width);
htsp_get_int(&tmpmsg,"height",&subscription->streams[i].height);
} else if (subscription->streams[i].type == HMF_STREAM_AUDIO) {
subscription->numaudiostreams++;
htsp_get_int(&tmpmsg,"audio_type",&subscription->streams[i].audio_type);
}
free(typestr);
if (lang != NULL) free(lang);
buf += namelength + datalength;
len -= namelength + datalength;
i++;
}
return 0;
}
int htsp_send_skip(struct htsp_t* htsp, int server, int time)
{
int res;
struct htsp_message_t msg;
res = htsp_create_message(&msg,HMF_STR,"method","subscriptionSkip",
HMF_S64,"time",time*1000000,
HMF_S64,"subscriptionId",htsp->subscriptionId,
HMF_NULL);
res = htsp_send_message(htsp,server,&msg);
fprintf(stderr,"Sent subscriptionSkip(%d) message, res=%d\n",time,res);
htsp_destroy_message(&msg);
return res;
}