summaryrefslogtreecommitdiff
path: root/frontend.c
blob: 61e35335593199dc4d08f63e38b83352232bf638 (plain)
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
/*
 * frontend.c:
 *
 * See the main source file 'xineliboutput.c' for copyright information and
 * how to reach the author.
 *
 * $Id: frontend.c,v 1.78 2010-02-03 14:01:42 phintuka Exp $
 *
 */

#define __STDC_FORMAT_MACROS
#define __STDC_CONSTANT_MACROS
#include <inttypes.h>

#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
#include <time.h>
#include <pthread.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <netdb.h>
#include <sys/ioctl.h>
#include <sys/socket.h>

#include <vdr/config.h>
#include <vdr/tools.h>
#include <vdr/plugin.h>

#include "logdefs.h"
#include "config.h"
#include "frontend.h"
#include "device.h"

#include "tools/pes.h"
#include "tools/mpeg.h"
#include "tools/h264.h"
#include "tools/general_remote.h"
#include "tools/iso639.h"

//#define LOG_CONTROL_MESSAGES
//#define XINELIBOUTPUT_LOG_KEYS

#ifndef STARTUP_IMAGE_FILE
#  define  STARTUP_IMAGE_FILE "/usr/share/vdr/xineliboutput/logo.mpv"
#endif
#ifndef STARTUP_MAX_SIZE
#  define STARTUP_MAX_SIZE (256*1024)
#endif

//----------------------------- cXinelibThread --------------------------------

//
// keyboard control handler
//

/*static*/
void cXinelibThread::KeypressHandler(const char *keymap, const char *key, 
				     bool repeat, bool release)
{
#ifdef XINELIBOUTPUT_LOG_KEYS
  static FILE *flog = fopen("/video/keys.log","w");
  if (flog) {
    fprintf(flog,"KEY %s %s %d %d\n",keymap,key,repeat,release); fflush(flog);
  }
#endif

  TRACE("keypress_handler: " << (keymap?keymap:"") << " " << key);

  if(!key)
    return;

  if(keymap) {
    cRemote *item = Remotes.First();
    while(item) {
      if(!strcmp(item->Name(), keymap)) {
	// dirty... but only way to support learning ...
	((cGeneralRemote*)item)->Put(key, repeat, release);
	return;
      }
      item = Remotes.Next(item);
    }
    cGeneralRemote *r = new cGeneralRemote(keymap);
    if(*key)
      r->Put(key, repeat, release);
  } else {
    cRemote::Put(cKey::FromString(key));
  }
}

#include <vdr/status.h>
class cFrontendStatusMonitor : public cStatus {
  private:
    bool& m_SpuLangAuto;
  public:
    cFrontendStatusMonitor(bool& SpuLangAuto) : m_SpuLangAuto(SpuLangAuto) {};
    virtual void SetSubtitleTrack(int /*Index*/, const char * const */*Tracks*/) { m_SpuLangAuto = false; }
};

void cXinelibThread::InfoHandler(const char *info)
{
  char *pmap = strdup(info), *map = pmap, *pt;

  if(NULL != (pt=strchr(map, '\r')))
    *pt = 0;

  if(!strncmp(info, "TRACKMAP SPU", 12)) {
    int CurrentTrack = ttXSubtitleAuto;
    map += 12;
    while(*map) {
      bool Current = false;
      while(*map == ' ') map++;
      if(*map == '*') {
	Current = true;
	map++;
	if (*map == '-') {
	  CurrentTrack = atoi(map);
	  while (*map && *map != ' ') map++;
	  continue;
	}
      }
      if(*map >= '0' && *map <= '9') {
	int id = atoi(map);
	while(*map && *map != ':') map++;
	if(*map == ':') map++;
	char *lang = map;
	while(*map && *map != ' ') map++;
	if(*map == ' ') { *map = 0; map++; };
	cXinelibDevice::Instance().SetAvailableTrack(ttSubtitle, id, id+1, iso639_2_to_iso639_1(lang) ?: *cString::sprintf("%03d", id+1));
	if (Current)
	  CurrentTrack = id;
      }
    }
    if (CurrentTrack == ttXSubtitleAuto)
      cXinelibDevice::Instance().EnsureSubtitleTrack();
    else if (CurrentTrack == ttXSubtitleNone)
      cXinelibDevice::Instance().SetCurrentSubtitleTrack(ttNone, true);
    else
      cXinelibDevice::Instance().SetCurrentSubtitleTrack(eTrackType(CurrentTrack+ttSubtitleFirst), true);
  }

  else if(!strncmp(info, "TRACKMAP AUDIO", 14)) {
    map += 14;
    cXinelibDevice::Instance().ClrAvailableTracks();
    while(*map) {
      bool Current = false;
      while(*map == ' ') map++;
      if(*map == '*') {
	Current = true;
	map++;
      }
      int id = atoi(map);
      while(*map && *map != ':') map++;
      if(*map == ':') map++;
      char *lang = map;
      while(*map && *map != ' ') map++;
      if(*map == ' ') { *map = 0; map++; };
      cXinelibDevice::Instance().SetAvailableTrack(ttDolby, id, ttDolby+id, iso639_2_to_iso639_1(lang) ?: *cString::sprintf("%03d", id+1));
      if(Current)
	cXinelibDevice::Instance().SetCurrentAudioTrack((eTrackType)(ttDolby+id));
    }
  }

  else if(!strncmp(info, "METAINFO", 8)) {
    map += 8;
    while(*map) {
      while(*map == ' ') map++;
      char *next = strstr(map, "=@");
      if(!next)
	break;
      *next = 0;
      next += 2;
      char *end =  strstr(next, "@");
      if(!end)
	break;
      *end = 0;

      if(!strcmp(map, "title"))
	cXinelibDevice::Instance().SetMetaInfo(miTitle, next);
      if(!strcmp(map, "tracknumber"))
        cXinelibDevice::Instance().SetMetaInfo(miTracknumber, next);
      if(!strcmp(map, "album"))
	cXinelibDevice::Instance().SetMetaInfo(miAlbum, next);
      if(!strcmp(map, "artist"))
	cXinelibDevice::Instance().SetMetaInfo(miArtist, next);
      map = end+1;
    }
  }

  else if(!strncmp(info, "DVDBUTTONS ", 11)) {
    map += 11;
    while(*map == ' ') map++;
    cXinelibDevice::Instance().SetMetaInfo(miDvdButtons, map);
  }

  else if(!strncmp(info, "TITLE ", 6)) {
    map += 6;
    while(*map == ' ') map++;
    cXinelibDevice::Instance().SetMetaInfo(miTitle, map);
  }

  else if(!strncmp(info, "DVDTITLE ", 9)) {
    map += 9;
    while(*map == ' ') map++;
    cXinelibDevice::Instance().SetMetaInfo(miDvdTitleNo, map);
    if (*map == '0')  // DVD Menu, set spu track to 0
      cXinelibDevice::Instance().SetCurrentSubtitleTrack(ttSubtitleFirst);
  }

  else if (!strncmp(info, "WINDOW ", 7)) {
    int w, h;
    map += 7;
    while(*map == ' ') map++;
    if (2 == sscanf(map, "%dx%d", &w, &h)) {
      xc.osd_width_auto  = w;
      xc.osd_height_auto = h;
    }
  }

  free(pmap);
}

cXinelibThread::cXinelibThread(const char *Description) : cThread(Description)
{
  TRACEF("cXinelibThread::cXinelibThread");

  m_Volume = 255;
  m_bReady = false;
  m_bNoVideo = true;
  m_bLiveMode = true; /* can't be replaying when there is no output device */
  m_StreamPos = 0;
  m_LastClearPos = 0;
  m_Frames = 0;
  m_bEndOfStreamReached = false;
  m_bPlayingFile = false;
  m_StatusMonitor = NULL;
}

cXinelibThread::~cXinelibThread()
{
  TRACEF("cXinelibThread::~cXinelibThread");

  Cancel(3);

  if (m_StatusMonitor)
    DELETENULL(m_StatusMonitor);
}

//
// Thread control
//

bool cXinelibThread::IsReady(void)
{
  LOCK_THREAD;
  return m_bReady;
}

//
// Playback control
//

void cXinelibThread::SetVolume(int NewVolume)
{
  m_Volume = NewVolume;
  cString str = cString::sprintf("VOLUME %d%s", NewVolume * 100 / 255, 
				 xc.sw_volume_control ? " SW" : "");
  Xine_Control(str);
}

void cXinelibThread::TrickSpeed(int Speed, bool Backwards)
{
  TRACEF("cXinelibThread::TrickSpeed");

  cString str = cString::sprintf("TRICKSPEED %d%s", Speed, Backwards ? " Backwards" : "");
  Xine_Control(str);
}

void cXinelibThread::SetLiveMode(bool LiveModeOn)
{
  TRACEF("cXinelibThread::SetLiveMode");

  Lock();
  if(m_bLiveMode == LiveModeOn) {
    Unlock();
    return;
  }
  m_bLiveMode = LiveModeOn;
  Unlock();

  Xine_Control("LIVE", m_bLiveMode ? 1 : 0);
}

void cXinelibThread::SetStillMode(bool StillModeOn)
{
  TRACEF("cXinelibThread::SetStillMode");
  Xine_Control("STILL", StillModeOn ? 1 : 0);
}

void cXinelibThread::SetNoVideo(bool bVal)
{
  TRACEF("cXinelibThread::SetNoVideo");

  Lock();
  if(m_bNoVideo == bVal) {
    Unlock();
    return;
  }
  m_bNoVideo = bVal;
  Unlock();

  Xine_Control("NOVIDEO", m_bNoVideo ? 1 : 0);

  char *opts = NULL;
  if(xc.audio_vis_goom_opts[0] && !strcmp(xc.audio_visualization, "goom"))
    opts = xc.audio_vis_goom_opts;

  if(m_bNoVideo && strcmp(xc.audio_visualization, "none")) {
    ConfigurePostprocessing(xc.audio_visualization, true, opts);
  } else {
    ConfigurePostprocessing("AudioVisualization", false, NULL);
  }
}

void cXinelibThread::AudioStreamChanged(bool ac3, int StreamId)
{
  TRACEF("cXinelibThread::AudioStreamChanged");
  if(ac3)
    Xine_Control("AUDIOSTREAM AC3", StreamId);
  else
    Xine_Control("AUDIOSTREAM", StreamId);
}

void cXinelibThread::SetSubtitleTrack(eTrackType Track)
{
  TRACEF("cXinelibThread::SetSubtitleTrack");
  cString buf = cString::sprintf("SPUSTREAM %d%s", 
				 Track==ttNone ? ttXSubtitleNone  : (Track - ttSubtitleFirst), 
				 m_SpuLangAuto ? " auto" : "");
  Xine_Control(buf);
}

void cXinelibThread::Clear(void)
{
  TRACEF("cXinelibThread::Clear");

  char buf[128];

  {
    LOCK_THREAD;

    if (m_StreamPos == m_LastClearPos) {
      //LOGDBG("cXinelibThread::Clear(): double Clear() ignored");
      return;
    }
    m_LastClearPos = m_StreamPos;

    snprintf(buf, sizeof(buf), "DISCARD %" PRId64 " %d", m_StreamPos, m_Frames);
  }

  /* Send to control stream and data stream. If message is sent only to
   * control stream, and it is delayed, engine flush will be skipped.
   */
  Xine_Control(buf);
  Xine_Control_Sync(buf);
}

bool cXinelibThread::Flush(int TimeoutMs) 
{
  TRACEF("cXinelibThread::Flush");

  return Xine_Control("FLUSH", TimeoutMs) <= 0;
}

int cXinelibThread::Poll(cPoller& Poller, int TimeoutMs) 
{
  TRACEF("cXinelibThread::Poll");

  if(!m_bReady) {
    if(TimeoutMs>0)
      cCondWait::SleepMs(TimeoutMs);
    if(!m_bReady)
      return 0;
  }

  int n = Xine_Control("POLL", TimeoutMs);

  return max(n, 0);
}

//
// Data transfer
//

int cXinelibThread::Play_PES(const uchar *data, int len)
{
  Lock();
  m_StreamPos += len;
  m_Frames++;
  /*m_bEndOfStreamReached = false;*/
  Unlock();
  return len;
}

//
// Stream conversions
//

// Convert MPEG1 PES headers to MPEG2 PES headers

int cXinelibThread::Play_Mpeg1_PES(const uchar *data1, int len)
{
  if(!data1[0] && !data1[1] && data1[2] == 0x01 && len>7 && /* header sync bytes */
     ( IS_VIDEO_PACKET(data1) || IS_AUDIO_PACKET(data1)) && /* video / audio / ps1 stream */
     ((data1[6] & 0xC0) != 0x80) &&                         /* really mpeg1 pes */
     (len == ((data1[4]<<8) | data1[5]) + 6)) {             /* whole PES packet and nothing else */
    uchar *data2 = new uchar[len+64];
    int i1=0, i2=0, r=0;
    
    data2[i2++]=data1[i1++];  // 00 (sync)  
    data2[i2++]=data1[i1++];  // 00 (sync)
    data2[i2++]=data1[i1++];  // 01 (sync)
    data2[i2++]=data1[i1++];  // stream ID
    data2[i2++]=data1[i1++];  // len hi
    data2[i2++]=data1[i1++];  // len lo
    
    // skip stuffing
    while ((data1[i1] & 0x80) == 0x80) 
      i1++;

    if ((data1[i1] & 0xc0) == 0x40) {
      // skip STD_buffer_scale, STD_buffer_size
      i1 += 2;
    }
    
    if(len<i1+5) return len;
    
    data2[i2++] = 0x80;
    
    if ((data1[i1] & 0xf0) == 0x20) { 
      /* PTS */
      data2[i2++] = 0x80;
      data2[i2++] = 5;
      data2[i2++] = data1[i1++] & 0x0E;
      data2[i2++] = data1[i1++] & 0xFF;
      data2[i2++] = data1[i1++] & 0xFE;
      data2[i2++] = data1[i1++] & 0xFF;
      data2[i2++] = data1[i1++] & 0xFE;
    }
    else if ((data1[i1] & 0xf0) == 0x30) { 
      /* PTS & DTS */
      data2[i2++] = 0x80|0x40;
      data2[i2++] = 10;
      data2[i2++] = data1[i1++] & 0x0E;
      data2[i2++] = data1[i1++] & 0xFF;
      data2[i2++] = data1[i1++] & 0xFE;
      data2[i2++] = data1[i1++] & 0xFF;
      data2[i2++] = data1[i1++] & 0xFE;
      
      data2[i2++] = data1[i1++] & 0x0E;
      data2[i2++] = data1[i1++] & 0xFF;
      data2[i2++] = data1[i1++] & 0xFE;
      data2[i2++] = data1[i1++] & 0xFF;
      data2[i2++] = data1[i1++] & 0xFE;
    } else {
      i1++;
      data2[i2++] = 0; /* no pts, no dts */
      data2[i2++] = 0; /* header len */
    }
    
    int newlen = ((data1[4]<<8) | data1[5]) + (i2-i1), loops=0;
    data2[4] = ((newlen)&0xff00)>>8;
    data2[5] = ((newlen)&0xff);
    if(len-i1 > 0) {
      memcpy(data2+i2, data1+i1, len-i1);
      cPoller p;
      while(!Poll(p,100) && loops++ < 10) {
	LOGDBG("Play_Mpeg1_PES: poll failed");
      }
      r = Play_PES(data2,newlen+6);
    } 
  
    delete data2;
    return r==newlen+6 ? ((data1[4]<<8)|data1[5])+6 : 0;
  }
  return len; // nothing useful found ...
}

// Pack elementary MPEG stream to PES

bool cXinelibThread::Play_Mpeg2_ES(const uchar *data, int len, int streamID, bool h264)
{
  static uchar hdr_vid[] = {0x00,0x00,0x01,0xe0, 0x00,0x00,0x80,0x00,0x00}; /* mpeg2 */
  static uchar hdr_pts[] = {0x00,0x00,0x01,0xe0, 0x00,0x08,0x80,0x80,
                            0x05,0x00,0x00,0x00, 0x00,0x00}; /* mpeg2 */
  static uchar seq_end[] = {0x00,0x00,0x01,0xe0, 0x00,0x07,0x80,0x00,
			    0x00,
			    0x00,0x00,0x01,SC_SEQUENCE_END}; /* mpeg2 */
  int todo = len, done = 0, hdrlen = 9/*sizeof(hdr)*/;
  uchar *frame = new uchar[PES_CHUNK_SIZE+32];
  cPoller p;

  hdr_pts[3] = (uchar)streamID;
  Poll(p, 100);
  Play_PES(hdr_pts, sizeof(hdr_pts));

  hdr_vid[3] = (uchar)streamID;
  while(todo) {
    int blocklen = todo;
    if(blocklen > ((PES_CHUNK_SIZE - hdrlen) & 0xfffc))
      blocklen = (PES_CHUNK_SIZE - hdrlen) & 0xfffc;
    hdr_vid[4] = ((blocklen+3)&0xff00)>>8;
    hdr_vid[5] = ((blocklen+3)&0xff);

    memcpy(frame, hdr_vid, hdrlen);
    memcpy(frame+hdrlen, data+done, blocklen);

    done += blocklen;
    todo -= blocklen;

    Poll(p, 100);

    if(blocklen+hdrlen != Play_PES(frame,blocklen+hdrlen)) {      
      delete frame;
      return false;
    }
  }

  // append sequence end code to video 
  if((streamID & 0xF0) == 0xE0) { 
    seq_end[3] = (uchar)streamID;
    seq_end[12] = h264 ? NAL_END_SEQ : SC_SEQUENCE_END;
    Poll(p, 100);
    Play_PES(seq_end, sizeof(seq_end));
  }

  delete[] frame;
  return true;
}

//
// Built-in still images
//

bool cXinelibThread::QueueBlankDisplay(void)
{
  TRACEF("cXinelibThread::BlankDisplay");
  Xine_Control_Sync("BLANK");
  return true;
}

bool cXinelibThread::BlankDisplay(void)
{
  TRACEF("cXinelibThread::BlankDisplay");

  bool r = QueueBlankDisplay();
  for(int i=0; i<5 && !Flush(100); i++)
    ;
  return r;
}

bool cXinelibThread::LogoDisplay(void)
{
  TRACEF("cXinelibThread::LogoDisplay");
  
  cString Path;
  int     fd = -1;

  if(Setup.FileName()) {
    cString SetupPath = Setup.FileName();
    const char *end = strrchr(SetupPath, '/');
    if(end) {
      SetupPath.Truncate(end - (const char *)SetupPath);
      fd = open(Path=cString::sprintf("%s/plugins/xineliboutput/logo.mpv", *SetupPath), O_RDONLY);
    }
  }
  
  if(fd<0)
    fd = open(Path=STARTUP_IMAGE_FILE, O_RDONLY);
  
  if(fd >= 0) {
    uint8_t *data = (uint8_t*)malloc(STARTUP_MAX_SIZE);
    int datalen = read(fd, data, STARTUP_MAX_SIZE);
    if(datalen == STARTUP_MAX_SIZE) {
      LOGMSG("WARNING: custom startup image %s too large", *Path);
    } else if(datalen<=0) {
      LOGERR("error reading custom startup image %s", *Path);
    } else {
      LOGMSG("using custom startup image %s", *Path);
      bool r = Play_Mpeg2_ES(data, datalen, VIDEO_STREAM);
      free(data);
      for(int i=0; i<5 && !Flush(100); i++)
	;
      return r;
    }
    free(data);
    close(fd);
  }
  
  /* use default image */
  extern const unsigned char v_mpg_vdrlogo[];     // vdrlogo_720x576.c
  extern const int v_mpg_vdrlogo_length;

  bool r = Play_Mpeg2_ES(v_mpg_vdrlogo, v_mpg_vdrlogo_length, VIDEO_STREAM);
  for(int i=0; i<5 && !Flush(100); i++)
    ;
  return r;
}

bool cXinelibThread::NoSignalDisplay(void)
{
  TRACEF("cXinelibThread::NoSignalDisplay");

  extern const unsigned char v_mpg_nosignal[];     // nosignal_720x576.c
  extern const int v_mpg_nosignal_length;

  bool r = Play_Mpeg2_ES(v_mpg_nosignal, v_mpg_nosignal_length, VIDEO_STREAM);
  for(int i=0; i<5 && !Flush(100); i++)
    ;
  return r;
}

//
// Xine Control
//

int cXinelibThread::Xine_Control(const char *cmd, int p1)
{
  char buf[128];
  if(snprintf(buf, sizeof(buf), "%s %d", cmd, p1) >= (int)sizeof(buf)) {
    LOGMSG("Xine_Control %s: message too long !", cmd);
    return 0;
  }
  //buf[sizeof(buf)-1] = 0;
  return Xine_Control(buf);
}

int cXinelibThread::Xine_Control(const char *cmd, int64_t p1)
{
  char buf[128];
  if(snprintf(buf, sizeof(buf), "%s %" PRId64, cmd, p1) >= (int)sizeof(buf)) {
    LOGMSG("Xine_Control %s: message too long !", cmd);
    return 0;
  }
  //buf[sizeof(buf)-1] = 0;
  return Xine_Control(buf);
}

int cXinelibThread::Xine_Control(const char *cmd, const char *p1)
{
  char buf[1024];
  if(snprintf(buf, sizeof(buf), "%s %s", cmd, p1) >= (int)sizeof(buf)) {
    LOGMSG("Xine_Control %s: message too long !", cmd);
    return 0;
  }
  //buf[sizeof(buf)-1] = 0;
  return Xine_Control(buf);
}

bool cXinelibThread::PlayFile(const char *FileName, int Position, 
			      bool LoopPlay, ePlayMode PlayMode,
			      int TimeoutMs)
{
  TRACEF("cXinelibThread::PlayFile");

  char vis[256];

  switch(PlayMode) {
    case pmVideoOnly:
      LOGDBG("cXinelibThread::PlayFile: Video from file, audio from VDR");
      strcpy(vis, "Video");
      break;
    case pmAudioOnly:
      LOGDBG("cXinelibThread::PlayFile: Audio from file, video from VDR");
      strcpy(vis, "Audio");
      break;
    case pmAudioOnlyBlack:
      //LOGDBG("cXinelibThread::PlayFile: Audio from file, no video");
      strcpy(vis, "none");
      break;
    case pmAudioVideo:
    default:
      if(xc.audio_vis_goom_opts[0] && !strcmp(xc.audio_visualization, "goom"))
	snprintf(vis, sizeof(vis), "%s:%s", xc.audio_visualization, xc.audio_vis_goom_opts);
      else
	strn0cpy(vis, xc.audio_visualization, sizeof(vis));
      vis[sizeof(vis)-1] = 0;
      break;
  }

  char buf[4096];
  m_bEndOfStreamReached = false;
  if(snprintf(buf, sizeof(buf), "PLAYFILE %s %d %s %s",
	      LoopPlay ? "Loop" : "", Position, vis, FileName ? FileName : "")
     >= 4096) {
    LOGMSG("PlayFile: message too long !");
    return 0;
  }

  if(FileName) {
    Lock();
    m_FileName = FileName;
    m_bPlayingFile = true;
    m_SpuLangAuto = true;
    if (m_StatusMonitor)
      DELETENULL(m_StatusMonitor);
    m_StatusMonitor = new cFrontendStatusMonitor(m_SpuLangAuto);
    Unlock();
  }

  int result = PlayFileCtrl(buf, TimeoutMs);

  if(!FileName || result != 0) {
    Lock();
    m_bPlayingFile = false;
    m_FileName = NULL;
    if (m_StatusMonitor)
      DELETENULL(m_StatusMonitor);
    Unlock();
  } else {
    if(xc.extsub_size >= 0)
      Xine_Control("EXTSUBSIZE", xc.extsub_size);

    // set preferred subtitle language 
    if (Setup.DisplaySubtitles) {
      const char *langs = I18nLanguageCode(Setup.SubtitleLanguages[0]);
      if (langs) {
	char lang1[5];
	strn0cpy(lang1, langs, 4); /* truncate */
	const char *spu_lang = iso639_1_to_iso639_2(lang1);
	LOGMSG("Preferred SPU language: %s (%s)", lang1, spu_lang);
	if (spu_lang && spu_lang[0] && spu_lang[1] && !spu_lang[2])
	  Xine_Control(cString::sprintf("SPUSTREAM %s", spu_lang));
      }
    } else {
      LOGMSG("Preferred SPU language: (none)");
      Xine_Control(cString::sprintf("SPUSTREAM %d", ttXSubtitleNone));
    }
  }

  return Running() && !result;
}


//
// Configuration
//

void cXinelibThread::Configure(void)
{
    ConfigurePostprocessing(xc.deinterlace_method, xc.audio_delay, 
			    xc.audio_compression, xc.audio_equalizer,
			    xc.audio_surround, xc.speaker_type);
    ConfigureVideo(xc.hue, xc.saturation, xc.brightness, xc.sharpness, xc.noise_reduction, xc.contrast, xc.overscan, xc.vo_aspect_ratio);
    ConfigurePostprocessing("upmix",     xc.audio_upmix ? true : false, NULL);
    ConfigurePostprocessing("autocrop",  xc.autocrop  ? true : false, 
			    xc.AutocropOptions());
    ConfigurePostprocessing("swscale", xc.swscale ? true : false, 
			    xc.SwScaleOptions());
    ConfigurePostprocessing("pp", xc.ffmpeg_pp ? true : false, 
			    xc.FfmpegPpOptions());
    ConfigurePostprocessing("unsharp",xc.unsharp ? true : false, 
                            xc.UnsharpOptions());
    ConfigurePostprocessing("denoise3d",xc.denoise3d ? true : false, 
                            xc.Denoise3dOptions());

#ifdef ENABLE_TEST_POSTPLUGINS
    ConfigurePostprocessing("headphone", xc.headphone ? true : false, NULL);
#endif

    Xine_Control(cString::sprintf("SCR %s %d", 
				  xc.live_mode_sync ? "Sync"    : "NoSync",
				  xc.scr_tuning     ? xc.scr_hz : 90000));
}

int cXinelibThread::ConfigurePostprocessing(const char *deinterlace_method, 
					    int audio_delay, 
					    int audio_compression, 
					    const int *audio_equalizer,
					    int audio_surround,
					    int speaker_type) 
{
  char buf[1024];
  int r = true;

  if(strcmp(deinterlace_method, "tvtime")) 
    r = ConfigurePostprocessing("tvtime", false, NULL) && r;

  r = Xine_Control("DEINTERLACE", deinterlace_method) && r;
  r = Xine_Control("AUDIODELAY", audio_delay) && r;
  r = Xine_Control("AUDIOCOMPRESSION", audio_compression) && r;
  r = Xine_Control("AUDIOSURROUND", audio_surround) && r;
  r = Xine_Control("SPEAKERS", speaker_type) && r;
  sprintf(buf,"EQUALIZER %d %d %d %d %d %d %d %d %d %d",
	  audio_equalizer[0],audio_equalizer[1],
	  audio_equalizer[2],audio_equalizer[3],
	  audio_equalizer[4],audio_equalizer[5],
	  audio_equalizer[6],audio_equalizer[7],
	  audio_equalizer[8],audio_equalizer[9]);
  r = Xine_Control(buf) && r;

  if(m_bNoVideo && strcmp(xc.audio_visualization, "none")) {
    char *opts = NULL;
    if(xc.audio_vis_goom_opts[0] && !strcmp(xc.audio_visualization, "goom"))
      opts = xc.audio_vis_goom_opts;
    //fe->post_open(fe, xc.audio_visualization, NULL);
    r = ConfigurePostprocessing(xc.audio_visualization, true, opts) && r;
  } else {
    //fe->post_close(fe, NULL, 0);
    r = ConfigurePostprocessing("AudioVisualization", false, NULL) && r;
  }

  if(!strcmp(deinterlace_method, "tvtime")) 
    r = ConfigurePostprocessing("tvtime", true, xc.deinterlace_opts) && r;

  return r;
}

int cXinelibThread::ConfigurePostprocessing(const char *name, bool on, const char *args)
{
  char buf[1024];  
  int l;

  if(on) 
    l = snprintf(buf, sizeof(buf), "POST %s On %s", (name&&*name)?name:"0", args?args:"");
  else 
    // 0 - audio vis.
    // 1 - audio post
    // 2 - video post
    //return fe->post_close(fe, name, -1);
    l = snprintf(buf, sizeof(buf), "POST %s Off", (name&&*name)?name:"0");

  if(l >= (int)sizeof(buf)) {
    LOGMSG("ConfigurePostprocessing %s: message too long !", name);
    return 0;
  }
  //buf[sizeof(buf)-1] = 0;

  return Xine_Control(buf);
}

int cXinelibThread::ConfigureVideo(int hue, int saturation, 
				   int brightness, int sharpness,
				   int noise_reduction, int contrast,
				   int overscan, int vo_aspect_ratio)
{
  char cmd[128];
  Xine_Control("OVERSCAN", overscan);
  snprintf(cmd, sizeof(cmd),
	   "VIDEO_PROPERTIES %d %d %d %d %d %d %d", 
	  hue, saturation, brightness, sharpness, noise_reduction, contrast, vo_aspect_ratio);
  return Xine_Control(cmd);
}

//
// Playback files
//

bool cXinelibThread::EndOfStreamReached(void) 
{
  LOCK_THREAD;
  bool r = m_bEndOfStreamReached;
  return r;
}