-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpidvbip.c
872 lines (734 loc) · 25.9 KB
/
pidvbip.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
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
/*
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 <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdint.h>
#include <stdarg.h>
#include <string.h>
#include <termios.h>
#include <ctype.h>
#include <sys/time.h>
#include <linux/input.h>
#include <interface/vmcs_host/vcgencmd.h>
#include "bcm_host.h"
#include "vcodec_mpeg2.h"
#include "vcodec_omx.h"
#include "acodec_mpeg.h"
#include "acodec_aac.h"
#include "acodec_a52.h"
#include "htsp.h"
#include "channels.h"
#include "events.h"
#include "debug.h"
#include "osd.h"
#include "avahi.h"
#include "cec.h"
#include "snapshot.h"
struct codecs_t {
struct codec_t vcodec; // Video
struct codec_t acodec; // Audio
struct codec_t scodec; // Subtitles
struct htsp_subscription_t subscription; // Details of the currently tuned channel
int is_paused;
};
struct codecs_t codecs;
/* TODO: Should this be global? */
struct htsp_t htsp;
int hw_mpeg2;
int mpeg2_codec_enabled(void)
{
char response[1024];
vc_gencmd_init();
vc_gencmd(response,sizeof(response), "codec_enabled MPG2");
if (strcmp(response,"MPG2=enabled")==0) {
return 1;
} else {
return 0;
}
}
/* Enable to dump video streams to files (for debugging) */
//#define DUMP_VIDEO
void process_message(char* method,struct htsp_message_t* msg,char* debugtext)
{
if ((strcmp(method,"eventAdd")==0) || (strcmp(method,"eventUpdate")==0)) {
process_event_message(method,msg);
} else if (strcmp(method,"eventDelete")==0) {
uint32_t eventId;
if (htsp_get_uint(msg,"eventId",&eventId)==0) {
//fprintf(stderr,"eventDelete: %d\n",eventId);
event_delete(eventId);
} else {
fprintf(stderr,"Warning eventDelete event not found (%d)\n",eventId);
}
} else if ((strcmp(method,"channelAdd")==0) || (strcmp(method,"channelUpdate")==0)) {
// channelName, channelNumber, channelId
int channelNumber,channelId,channelType;
uint32_t eventid,nexteventid;
char* channelName;
unsigned char* list;
int listlen;
if (htsp_get_int(msg,"channelId",&channelId) == 0) {
if (htsp_get_int(msg,"channelNumber",&channelNumber) > 0) { channelNumber = -1; }
if (htsp_get_uint(msg,"eventId",&eventid) > 0) { eventid = 0; }
if (htsp_get_uint(msg,"nextEventId",&nexteventid) > 0) { nexteventid = 0; }
channelName = htsp_get_string(msg,"channelName");
if (htsp_get_list(msg,"services",&list,&listlen) > 0)
{
channelType = CTYPE_NONE;
} else {
unsigned char* buf = list;
int type = buf[0]; if (type > 6) { type = 0; }
int namelength = buf[1];
int datalength = (buf[2] << 24) | (buf[3] << 16) | (buf[4] << 8) | buf[5];
buf += 6;
struct htsp_message_t tmpmsg;
tmpmsg.msg = buf + namelength - 4;
tmpmsg.msglen = datalength;
char* typestr = htsp_get_string(&tmpmsg,"type");
if (!typestr) {
channelType = CTYPE_UNKNOWN;
} else {
if (strncmp(typestr,"SDTV",4)==0) {
//fprintf(stderr,"Type=SDTV\n");
channelType = CTYPE_SDTV;
} else if (strncmp(typestr,"HDTV",4)==0) {
//fprintf(stderr,"Type=HDTV\n");
channelType = CTYPE_HDTV;
} else if (strncmp(typestr,"Radio",4)==0) {
//fprintf(stderr,"Type=RADIO\n");
channelType = CTYPE_RADIO;
} else {
fprintf(stderr,"Type=%s (unknown)\n",typestr);
channelType = CTYPE_UNKNOWN;
}
free(typestr);
}
}
if (strcmp(method,"channelAdd")==0) {
channels_add(channelNumber,channelId,channelName,channelType,eventid,nexteventid);
//fprintf(stderr,"channelAdd - id=%d,lcn=%d,name=%s,current_event=%d,next_event=%d\n",channelId,channelNumber,channelName,eventid,nexteventid);
} else {
channels_update(channelNumber,channelId,channelName,channelType,eventid,nexteventid);
//fprintf(stderr,"channelUpdate - id=%d,current_event=%d,next_event=%d\n",channelId,eventid,nexteventid);
}
}
} else if (strcmp(method,"queueStatus")== 0) {
/* Are we interested? */
} else if (strcmp(method,"signalStatus")== 0) {
/* Are we interested? */
} else if (strcmp(method,"dvrEntryAdd")==0) {
/* TODO */
} else {
fprintf(stderr,"%s: Received message %s\n",debugtext,method);
//htsp_dump_message(msg);
}
}
static void do_pause(struct codecs_t* codecs, int pause)
{
if (pause) {
if (!codecs->is_paused) {
/* Currently playing, pause */
fprintf(stderr,"[PAUSE]\n");
codec_pause(&codecs->acodec);
//codec_pause(&codecs->vcodec); // segfault!
codecs->is_paused = 1;
}
} else {
if (codecs->is_paused) {
/* Currently paused, resume playback */
fprintf(stderr,"[RESUME]\n");
codec_resume(&codecs->acodec);
codec_resume(&codecs->vcodec);
codecs->is_paused = 0;
}
}
}
/* The HTSP thread reads from the network and passes the incoming stream packets to the
appropriate codec (video/audio/subtitle) */
void* htsp_receiver_thread(struct codecs_t* codecs)
{
struct htsp_message_t msg;
int res;
struct packet_t* packet;
int ok = 1;
int current_subscriptionId = -1;
#ifdef DUMP_VIDEO
int fd;
static int track = 0;
char filename[32];
sprintf(filename,"video%03d.dump",++track);
fd = open(filename,O_CREAT|O_TRUNC|O_RDWR,0666);
if (fd < 0) {
fprintf(stderr,"Could not create video.dump\n");
exit(1);
}
#endif
while (ok) {
htsp_lock(&htsp);
res = htsp_recv_message(&htsp,&msg,100);
current_subscriptionId = htsp.subscriptionId;
htsp_unlock(&htsp);
if (res == 1) {
usleep(100000);
}
if (res == 0) {
int free_msg = 1; // We want to free this message, unless this flag is set to zero
char* method = htsp_get_string(&msg,"method");
//fprintf(stderr,"method=%s\n",method);
if (method != NULL) {
if (strcmp(method,"subscriptionStart")==0) {
if (htsp_parse_subscriptionStart(&msg,&codecs->subscription) > 0) {
fprintf(stderr,"FATAL ERROR: Cannot parse subscriptionStart\n");
exit(1);
}
if (codecs->subscription.videostream >= 0) {
fprintf(stderr,"[htsp_receiver_thread] - creating video codec\n");
if (codecs->subscription.streams[codecs->subscription.videostream].codec == HMF_VIDEO_CODEC_MPEG2) {
if (hw_mpeg2) {
vcodec_omx_init(&codecs->vcodec,OMX_VIDEO_CodingMPEG2);
} else {
vcodec_mpeg2_init(&codecs->vcodec);
}
} else {
vcodec_omx_init(&codecs->vcodec,OMX_VIDEO_CodingAVC);
}
codecs->vcodec.acodec = &codecs->acodec;
}
if (codecs->subscription.streams[codecs->subscription.audiostream].codec == HMF_AUDIO_CODEC_MPEG) {
acodec_mpeg_init(&codecs->acodec);
DEBUGF("Initialised mpeg codec\n");
} else if (codecs->subscription.streams[codecs->subscription.audiostream].codec == HMF_AUDIO_CODEC_AAC) {
acodec_aac_init(&codecs->acodec);
DEBUGF("Initialised AAC codec\n");
} else if (codecs->subscription.streams[codecs->subscription.audiostream].codec == HMF_AUDIO_CODEC_AC3) {
acodec_a52_init(&codecs->acodec);
DEBUGF("Initialised A/52 codec\n");
}
// TODO: Subtitle thread
} else if (strcmp(method,"subscriptionStatus")==0) {
char* status = htsp_get_string(&msg,"status");
if ((status != NULL) && (strcmp(status,"OK")!=0)) {
fprintf(stderr,"subscriptionStatus: %s\n",status);
free(status);
}
} else if (strcmp(method,"muxpkt")==0) {
int stream;
htsp_get_int(&msg,"stream",&stream);
int subscriptionId;
htsp_get_int(&msg,"subscriptionId",&subscriptionId);
if (subscriptionId == current_subscriptionId) {
if (stream==codecs->subscription.streams[codecs->subscription.videostream].index) {
packet = malloc(sizeof(*packet));
packet->buf = msg.msg;
int frametype;
htsp_get_int(&msg,"frametype",&frametype);
// htsp_dump_message(&msg);
if (htsp_get_int64(&msg,"pts",&packet->PTS) > 0) {
fprintf(stderr,"ERROR: No PTS in video packet, dropping\n");
} else {
if (htsp_get_int64(&msg,"dts",&packet->DTS) > 0) {
fprintf(stderr,"ERROR: No DTS in video packet, dropping\n");
} else {
htsp_get_bin(&msg,"payload",&packet->packet,&packet->packetlength);
#ifdef DUMP_VIDEO
write(fd,packet->packet,packet->packetlength);
#endif
codec_queue_add_item(&codecs->vcodec,packet);
free_msg = 0; // Don't free this message
}
}
} else if ((codecs->acodec.is_running) && (stream==codecs->subscription.streams[codecs->subscription.audiostream].index) &&
((codecs->subscription.streams[codecs->subscription.audiostream].codec == HMF_AUDIO_CODEC_MPEG) ||
(codecs->subscription.streams[codecs->subscription.audiostream].codec == HMF_AUDIO_CODEC_AC3) ||
(codecs->subscription.streams[codecs->subscription.audiostream].codec == HMF_AUDIO_CODEC_AAC))
) {
packet = malloc(sizeof(*packet));
packet->buf = msg.msg;
if (htsp_get_int64(&msg,"pts",&packet->PTS) > 0) {
fprintf(stderr,"ERROR: No PTS in audio packet, dropping\n");
} else {
htsp_get_bin(&msg,"payload",&packet->packet,&packet->packetlength);
codec_queue_add_item(&codecs->acodec,packet);
free_msg = 0; // Don't free this message
}
}
}
} else if (strcmp(method,"subscriptionSkip")==0) {
codec_flush_queue(&codecs->vcodec);
codec_flush_queue(&codecs->acodec);
do_pause(codecs,0);
} else {
process_message(method,&msg,"htsp_receiver_thread");
}
free(method);
}
if (free_msg)
htsp_destroy_message(&msg);
}
}
#ifdef DUMP_VIDEO
close(fd);
#endif
return 0;
}
int read_config(char* configfile,struct htsp_t *htsp)
{
FILE* fp = NULL;
char *line = NULL;
size_t len = 0;
ssize_t read;
if (configfile)
fp = fopen(configfile,"r");
if (fp == NULL)
fp = fopen("/boot/pidvbip.txt","r"); /* FAT partition in Raspbian */
if (fp == NULL)
fp = fopen("/flash/pidvbip.txt","r"); /* FAT partition in OpenELEC */
if (fp == NULL){
fprintf(stderr,"Could not open config file\n");
return -1;
}
if (fp == NULL){
fprintf(stderr,"Error reading from config file\n");
return -1;
}
read = getline(&line, &len, fp);
if (read != -1) {
htsp->host = malloc(1024);
sscanf(line,"%s %d",htsp->host,&htsp->port);
}
read = getline(&line, &len, fp);
if (read != -1) {
htsp->user = malloc(1024);
htsp->password = malloc(1024);
sscanf(line,"%s %s",htsp->user,htsp->password);
}
fclose(fp);
return 0;
}
void usage(void)
{
fprintf(stderr,"pidvbip - tvheadend client for the Raspberry Pi\n");
fprintf(stderr,"\nOptions:\n\n");
}
static int get_actual_channel(int auto_hdtv, int user_channel_id)
{
int actual_channel_id = user_channel_id;
if ((auto_hdtv) && (channels_gettype(user_channel_id)==CTYPE_SDTV)) {
uint32_t current_eventId = channels_geteventid(user_channel_id);
int hd_channel = event_find_hd_version(current_eventId);
if (hd_channel >= 0) {
fprintf(stderr,"Auto-switching to channel %d (%s)\n",channels_getlcn(hd_channel),channels_getname(hd_channel));
actual_channel_id = hd_channel;
}
}
return actual_channel_id;
}
#define KEY_RELEASE 0
#define KEY_PRESS 1
#define KEY_KEEPING_PRESSED 2
int get_input_key(int fd)
{
struct input_event ev[64];
int i;
size_t rb = read(fd, ev, sizeof(ev));
if (rb < (int) sizeof(struct input_event)) {
fprintf(stderr,"Short read\n");
return -1;
}
for (i = 0; i < (int)(rb / sizeof(struct input_event));i++) {
if (ev[i].type == EV_KEY) {
if ((ev[i].value == KEY_PRESS) || (ev[i].value == KEY_KEEPING_PRESSED)) {
fprintf(stderr,"input code %d\n",ev[1].code);
switch(ev[1].code) {
case KEY_0: return '0';
case KEY_1: return '1';
case KEY_2: return '2';
case KEY_3: return '3';
case KEY_4: return '4';
case KEY_5: return '5';
case KEY_6: return '6';
case KEY_7: return '7';
case KEY_8: return '8';
case KEY_9: return '9';
case KEY_H: return 'h';
case KEY_I: return 'i';
case KEY_Q: return 'q';
case KEY_N: return 'n';
case KEY_P: return 'p';
case KEY_PAGEUP: return 'n';
case KEY_PAGEDOWN: return 'p';
case KEY_UP: return 'u';
case KEY_DOWN: return 'd';
case KEY_LEFT: return 'l';
case KEY_RIGHT: return 'r';
// MCE remote mappings
case 226: return 'i'; // MENU
case 366: return 'r'; // RECORDEDTV
case 365: return 'c'; // GUIDE
case 386: return 'i'; // LIVETV
case 389: return 'i'; // DVDMENU
case 352: return 'i'; // OK
case 358: return 'i'; // INFO
case 402: return 'n'; // PAGEUP
case 403: return 'p'; // PAGEDOWN
default: break;
}
}
}
}
return -1;
}
void tune_channel(int actual_channel_id ) {
int res;
struct htsp_message_t msg;
//blank_video_timeout = get_time() + 8000;
fprintf(stderr,"lock0\n");
if (codecs.vcodec.thread) {
codec_stop(&codecs.vcodec);
pthread_join(codecs.vcodec.thread,NULL);
fprintf(stderr,"[main thread] - killed video thread\n");
}
if (codecs.acodec.thread) {
codec_stop(&codecs.acodec);
pthread_join(codecs.acodec.thread,NULL);
fprintf(stderr,"[main thread] - killed audio thread\n");
}
htsp_lock(&htsp);
if (htsp.subscriptionId > 0) {
res = htsp_create_message(&msg,HMF_STR,"method","unsubscribe",HMF_S64,"subscriptionId",htsp.subscriptionId,HMF_NULL);
res = htsp_send_message(&htsp,&msg);
htsp_destroy_message(&msg);
}
memset(&codecs.acodec,0,sizeof(codecs.acodec));
htsp_unlock(&htsp);
fprintf(stderr,"Tuning to channel %d - \"%s\"\n",channels_getlcn(actual_channel_id),channels_getname(actual_channel_id));
htsp_lock(&htsp);
res = htsp_create_message(&msg,HMF_STR,"method","subscribe",
HMF_S64,"channelId",actual_channel_id,
HMF_S64,"timeshiftPeriod",3600,
HMF_S64,"normts",1,
HMF_S64,"subscriptionId",++htsp.subscriptionId,
HMF_NULL);
res = htsp_send_message(&htsp,&msg);
if (res < 0) return;
htsp_unlock(&htsp);
htsp_destroy_message(&msg);
}
int main(int argc, char* argv[])
{
int res;
int channel = -1;
int user_channel_id = -1;
int actual_channel_id = -1;
int auto_hdtv = 0;
struct htsp_message_t msg;
struct osd_t osd;
pthread_t htspthread = 0;
double osd_cleartime = 0;
int inputfd;
char inputname[256] = "Unknown";
char *inputdevice = "/dev/input/event2";
htsp.host = NULL;
htsp.ip = NULL;
htsp.user = NULL;
htsp.password = NULL;
if (argc == 1) {
/* No arguments, try avahi discovery */
//avahi_discover_tvh(&htsp);
if (htsp.host == NULL) {
/* No avahi, try to read default config from /boot/config.txt */
if (read_config(NULL,&htsp) < 0) {
fprintf(stderr,"ERROR: Could not read from config file\n");
fprintf(stderr,"Create config.txt in /boot/pidvbip.txt containing lines \n");
fprintf(stderr,"host and port of the server separated by a space.\n");
fprintf(stderr,"username and password separated by a space.\n");
exit(1);
}
}
// } else if (argc==2) {
// /* One argument - config file */
} else if ((argc != 3) && (argc != 4)) {
usage();
return 1;
} else {
htsp.host = argv[1];
htsp.port = atoi(argv[2]);
}
fprintf(stderr,"Using host \"%s:%d\"\n",htsp.host,htsp.port);
osd_init(&osd);
bcm_host_init();
#ifdef ENABLE_CEC
cec_init(0);
#endif
hw_mpeg2 = mpeg2_codec_enabled();
if (hw_mpeg2) {
fprintf(stderr,"Using hardware MPEG-2 decoding\n");
} else {
fprintf(stderr,"Using software MPEG-2 decoding\n");
}
if ((inputfd = open(inputdevice, O_RDONLY)) >= 0) {
ioctl (inputfd, EVIOCGNAME (sizeof (inputname)), inputname);
fprintf(stderr,"Using %s - %s\n", inputdevice, inputname);
/* Disable auto-repeat (for now...) */
//int ioctl_params[2] = { 0, 0 };
//ioctl(inputfd,EVIOCSREP,ioctl_params);
}
htsp_init(&htsp);
if ((res = htsp_connect(&htsp)) > 0) {
fprintf(stderr,"Error connecting to htsp server, aborting.\n");
return 2;
}
if (argc==4) { channel = atoi(argv[3]); }
res = htsp_login(&htsp);
if (res > 0) {
fprintf(stderr,"Could not login to server\n");
return 3;
}
res = htsp_create_message(&msg,HMF_STR,"method","enableAsyncMetadata",HMF_S64,"epg",1,HMF_NULL);
res = htsp_send_message(&htsp,&msg);
htsp_destroy_message(&msg);
// Receive the acknowledgement from enableAsyncMetadata
res = htsp_recv_message(&htsp,&msg,0);
channels_init();
events_init();
struct termios new,orig;
tcgetattr(0, &orig);
memcpy(&new, &orig, sizeof(struct termios));
new.c_lflag &= ~(ICANON | ECHO);
new.c_cc[VTIME] = 0;
new.c_cc[VMIN] = 1;
tcsetattr(0, TCSANOW, &new);
//int num_events = 0;
int done = 0;
while (!done)
{
// TODO: Store all received channels/tags/dvr data and stop when completed message received.
res = htsp_recv_message(&htsp,&msg,0);
char* method = htsp_get_string(&msg,"method");
if (method) {
if (strcmp(method,"initialSyncCompleted")==0) {
done=1;
} else {
process_message(method,&msg,"Initial sync:");
}
free(method);
}
htsp_destroy_message(&msg);
}
//fprintf(stderr,"Initial sync completed - read data for %d events\n",num_events);
if (channels_getcount() == 0) {
fprintf(stderr,"No channels available, exiting.\n");
exit(1);
}
/* We have finished the initial connection and sync, now start the
receiving thread */
pthread_create(&htspthread,NULL,(void * (*)(void *))htsp_receiver_thread,(void*)&codecs);
user_channel_id = channels_getid(channel);
if (user_channel_id < 0)
user_channel_id = channels_getfirst();
actual_channel_id = get_actual_channel(auto_hdtv,user_channel_id);
memset(&codecs.vcodec,0,sizeof(codecs.vcodec));
memset(&codecs.acodec,0,sizeof(codecs.acodec));
codecs.is_paused = 0;
double blank_video_timeout;
tune_channel(actual_channel_id);
osd_show_info(&osd,user_channel_id,6000);
/* UI loop */
int new_channel;
double new_channel_timeout;
new_channel = -1;
new_channel_timeout = 0;
while (1) {
int c;
struct timeval tv = { 0L, 100000L }; /* 100ms */
fd_set fds;
int maxfd = 0;
FD_ZERO(&fds);
FD_SET(0, &fds);
if (inputfd >= 0) {
FD_SET(inputfd,&fds);
maxfd = inputfd;
}
c = -1;
if (select(maxfd+1, &fds, NULL, NULL, &tv)==0) {
c = -1;
} else {
if (FD_ISSET(0,&fds)) {
c = getchar();
}
if ((inputfd >= 0) && (FD_ISSET(inputfd,&fds))) {
c = get_input_key(inputfd);
}
}
#ifdef ENABLE_CEC
if (c==-1) {
c = cec_get_keypress();
}
#endif
osd_process_key(&osd,c);
if (c != -1) {
DEBUGF("char read: 0x%08x ('%c')\n", c,(isalnum(c) ? c : ' '));
switch (c) {
case '0':
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
case '9':
if (new_channel == -1) {
new_channel = c - '0';
} else {
new_channel = (new_channel * 10) + (c - '0');
/* Limit to 4 digits */
if (new_channel > 10000) {
new_channel = new_channel % 10000;
}
}
if (osd_cleartime) {
osd_clear(&osd);
osd_cleartime = 0;
}
osd_show_newchannel(&osd,new_channel);
new_channel_timeout = get_time() + 1000;
break;
case 'q':
goto done;
case 'i':
if (osd.osd_state == OSD_INFO)
osd_clear(&osd);
else
osd_show_info(&osd,user_channel_id,3000);
break;
case 'r':
osd_recordings(&osd);
break;
case 'c':
if (osd.osd_state == OSD_CHANNELLIST) {
osd_clear(&osd);
//int tmp = user_channel_id;
user_channel_id = osd.channellist_selected_channel;
int new_actual_channel_id = get_actual_channel(auto_hdtv, user_channel_id);
if (new_actual_channel_id != actual_channel_id) {
//prev_user_channel_id = tmp;
actual_channel_id = new_actual_channel_id;
//msgqueue_add(&htsp.msgqueue, HTMSG_NEW_CHANNEL | actual_channel_id);
osd_show_info(&osd, user_channel_id,3000); /* 3 second timeout */
}
} else {
osd_clear(&osd);
osd.channellist_selected_channel = user_channel_id;
osd.channellist_start_channel = user_channel_id;
static int i = 0;
int channel_tmp;
for (channel_tmp = channels_getfirst(); channel_tmp != user_channel_id; channel_tmp = channels_getnext(channel_tmp) )
{
if (i % 12 == 0) osd.channellist_start_channel = channel_tmp;
i++;
}
osd.channellist_selected_pos = 0;
osd_channellist_display(&osd);
}
break;
case 'h':
auto_hdtv = 1 - auto_hdtv;
int new_actual_channel_id = get_actual_channel(auto_hdtv,user_channel_id);
if (new_actual_channel_id != actual_channel_id) {
actual_channel_id = new_actual_channel_id;
tune_channel(actual_channel_id);
osd_show_info(&osd,user_channel_id,3000);
}
break;
case ' ':
do_pause(&codecs,1-codecs.is_paused);
htsp_lock(&htsp);
res = htsp_create_message(&msg,HMF_STR,"method","subscriptionSpeed",
HMF_S64,"speed",(codecs.is_paused == 0 ? 100 : 0),
HMF_S64,"subscriptionId",htsp.subscriptionId,
HMF_NULL);
res = htsp_send_message(&htsp,&msg);
htsp_unlock(&htsp);
htsp_destroy_message(&msg);
break;
case 'n':
user_channel_id = channels_getnext(user_channel_id);
actual_channel_id = get_actual_channel(auto_hdtv,user_channel_id);
tune_channel(actual_channel_id);
osd_show_info(&osd,user_channel_id,6000);
break;
case 'p':
user_channel_id = channels_getprev(user_channel_id);
actual_channel_id = get_actual_channel(auto_hdtv,user_channel_id);
tune_channel(actual_channel_id);
osd_show_info(&osd,user_channel_id,6000);
break;
/*
case 'u':
do_pause(&codecs,1);
htsp_send_skip(&htsp,10*60); // +10 minutes
break;
case 'd':
do_pause(&codecs,1);
htsp_send_skip(&htsp,-10*60); // -10 minutes
break;
case 'l':
do_pause(&codecs,1);
htsp_send_skip(&htsp,-30); // -30 seconds
break;
case 'r':
do_pause(&codecs,1);
htsp_send_skip(&htsp,30); // +30 seconds
break;
*/
case 'z':
break;
case 's':
save_snapshot();
break;
default:
break;
}
}
osd_update(&osd,user_channel_id);
if ((blank_video_timeout) && (get_time() > blank_video_timeout)) {
osd_blank_video(&osd,0);
blank_video_timeout = 0;
}
if ((new_channel_timeout) && (get_time() >= new_channel_timeout)) {
fprintf(stderr,"new_channel = %d\n",new_channel);
int new_channel_id = channels_getid(new_channel);
if (new_channel_id >= 0) {
user_channel_id = new_channel_id;
actual_channel_id = get_actual_channel(auto_hdtv,user_channel_id);
tune_channel(actual_channel_id);
osd_show_info(&osd,user_channel_id,6000);
} else {
osd_clear_newchannel(&osd);
fprintf(stderr,"No such channel\n");
}
new_channel = -1;
new_channel_timeout = 0;
}
}
done:
tcsetattr(0, TCSANOW, &orig);
return 0;
}