summaryrefslogtreecommitdiff
path: root/bdplayer.c
blob: 631bfcd290fbc1ddbdd812c5d699ceb6f4b9fffa (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
/*
 * bdplayer.c: A player for BluRay discs
 *
 * See the README file for copyright information and how to reach the author.
 *
 */

#include "bdplayer.h"

#include <vdr/remote.h>
#include <vdr/tools.h>
#include <vdr/status.h>

#include <libbluray/bluray.h>
#include <libbluray/meta_data.h>

#define MIN_TITLE_LENGTH   (180)               // seconds
#define M2TS_SIZE          (188 + 4)           // size of m2ts packet
#define ALIGNED_UNIT_SIZE  (32 * M2TS_SIZE)    // size of aligned unit (32 packets)

// --- cBDPlayer --------------------------------------------------------

class cBDPlayer : public cPlayer, cThread {
private:
  BLURAY *bd;
  BLURAY_TITLE_INFO *title_info;

  enum ePlayModes { pmPlay, pmPause };
  ePlayModes playMode;

  uchar buffer[ALIGNED_UNIT_SIZE];
  int   pos, packs;

  bool DoRead(void);
  bool DoPlay(void);

  virtual void Activate(bool On);

  void UpdateTracks(unsigned int current_clip);
  void HandleEvents(BD_EVENT *ev);
  void Empty();

protected:
  void Action(void);

public:
  cBDPlayer(BLURAY *bd);
  ~cBDPlayer();

  void Goto(int Seconds);
  void SkipSeconds(int seconds);
  void Play();
  void Pause();

  virtual bool GetIndex(int &Current, int &Total, bool SnapToIFrame = false);
  virtual bool GetReplayMode(bool &Play, bool &Forward, int &Speed);
};

cBDPlayer::cBDPlayer(BLURAY *Bd)
{
  bd = Bd;
  title_info = NULL;
  playMode = pmPlay;
  pos = packs = 0;
}

cBDPlayer::~cBDPlayer()
{
  if (title_info) {
    bd_free_title_info(title_info);
    title_info = NULL;
  }

  if (bd) {
    bd_close(bd);
    bd = NULL;
  }
}

void cBDPlayer::UpdateTracks(unsigned int current_clip)
{
  if (title_info && current_clip < title_info->clip_count) {
    BLURAY_CLIP_INFO *clip = &title_info->clips[current_clip];
    int i;

    DeviceClrAvailableTracks();

    for (i = 0; i < clip->audio_stream_count; i++) {
      DeviceSetAvailableTrack(ttDolby, i,
                              clip->audio_streams[i].pid,
                              (const char *)clip->audio_streams[i].lang);
    }
    for (i = 0; i < clip->pg_stream_count; i++) {
      DeviceSetAvailableTrack(ttSubtitle, i,
                              clip->pg_streams[i].pid,
                              (const char *)clip->pg_streams[i].lang);
    }
  }
}

void cBDPlayer::HandleEvents(BD_EVENT *ev)
{
  while (ev->event != BD_EVENT_NONE) {

    switch (ev->event) {

    //case BD_EVENT_ANGLE:
    //case BD_EVENT_TITLE:

    case BD_EVENT_PLAYLIST:
      if (title_info) {
        bd_free_title_info(title_info);
        title_info = NULL;
      }
      title_info = bd_get_playlist_info(bd, ev->param, 0);
      break;

    case BD_EVENT_PLAYITEM:
      UpdateTracks(ev->param);
      break;

    //case BD_EVENT_CHAPTER:

    case BD_EVENT_END_OF_TITLE:
      isyslog("END_OF_TITLE");
      Cancel(-1);
      break;

    default:
      break;
    }

    /* get next event */
    if (!bd_get_event(bd, ev))
      break;
  }
}

bool cBDPlayer::DoRead()
{
  BD_EVENT ev = {0, 0};

  LOCK_THREAD;

  pos = 0;
  packs = bd_read_ext(bd, buffer, ALIGNED_UNIT_SIZE, &ev);

  if (packs == 0) {
    if (ev.event == BD_EVENT_NONE) {
      // title without video
      cCondWait::SleepMs(3);
    }
  } else if (packs < 0) {
    // ERROR
    esyslog("bd_read() error");
    return false;
  }
  packs /= 192;

  HandleEvents(&ev);
  return true;
}

bool cBDPlayer::DoPlay()
{
  cPoller Poller;

  if (DevicePoll(Poller, 10)) {

    LOCK_THREAD;

    for (;pos < packs; pos++) {

      uint16_t pid = (((buffer)[1+4+pos*192] << 8) | (buffer)[2+4+pos*192]) & 0x1fff;
      if (pid >= 1200 && pid < 1300) {
	// skip PG streams
	continue;
      }
      if (pid >= 1400 && pid < 1500) {
	// skip IG streams
	continue;
      }

      int w = PlayTs(buffer + pos*192 + 4, 188, false);

      if (w == 188) {
	continue;
      } else if (w > 0) {
	esyslog("PlayTs() error: partial ts packet accepted");
	continue;
      } else if (w == 0) {
	//esyslog("PlayTs() error: data not accepted");
	break;
      } else {
	esyslog("PlayTs() error");
	return false;
      }
    }
  }

  return true;
}

void cBDPlayer::Activate(bool On)
{
  if (On && bd) {
    Start();
  } else {
    Cancel(6);
  }
}

void cBDPlayer::Action()
{
  DoRead();

  while (Running()) {

    if (pos >= packs) {
      if (!DoRead()) {
	break;
      }
    }

    if (!DoPlay()) {
      break;
    }
  }

  isyslog("End BluRay playback");
}

void cBDPlayer::SkipSeconds(int seconds)
{
  uint64_t tick = bd_tell_time(bd);
  if (tick < 0) {
    isyslog("bd_tell_time() failed");
    return;
  }

  seconds += tick / 90000;
  if (seconds < 0) {
    seconds = 0;
  }

  Goto(seconds);
}

void cBDPlayer::Goto(int seconds)
{
  LOCK_THREAD;

  Empty();
  uint64_t tick = seconds;
  tick *= 90000;

  isyslog("Seek to %d", seconds);
  bd_seek_time(bd, tick);
}

void cBDPlayer::Empty(void)
{
  LOCK_THREAD;

  pos = packs = 0;

  DeviceClear();
}

void cBDPlayer::Pause(void)
{
  // from vdr-1.7.34
  if (playMode == pmPause) {
    Play();
  } else {
    LOCK_THREAD;

    DeviceFreeze();
    playMode = pmPause;
  }
}

void cBDPlayer::Play(void)
{
  // from vdr-1.7.34
  if (playMode != pmPlay) {
    LOCK_THREAD;

    DevicePlay();
    playMode = pmPlay;
  }
}

bool cBDPlayer::GetIndex(int &Current, int &Total, bool SnapToIFrame)
{
  LOCK_THREAD;

  if (title_info) {
    Total = title_info->duration / 90000 * 25;
    Current = bd_tell_time(bd) / 90000 * 25;
    return true;
  }

  Current = Total = -1;
  return false;
}

bool cBDPlayer::GetReplayMode(bool &Play, bool &Forward, int &Speed)
{
  Play = (playMode == pmPlay);
  Forward = true;
  Speed = -1;
  return true;
}

// --- cBDControl -------------------------------------------------------

#define MODETIMEOUT       3 // seconds

int cBDControl::active = 0;

cBDControl::cBDControl(cBDPlayer *Player)
:cControl(Player)
{
  player = Player;
  active++;

  displayReplay = NULL;
  visible = modeOnly = shown = false;
  lastCurrent = lastTotal = -1;
  lastPlay = lastForward = false;
  lastSpeed = -2; // an invalid value
  timeoutShow = 0;
  timeSearchActive = false;

  disc_name = tr("BluRay");

  cStatus::MsgReplaying(this, "BluRay", NULL, true);
}

cBDControl::~cBDControl()
{
  active--;

  delete player;

  cStatus::MsgReplaying(this, NULL, NULL, false);
}

cControl *cBDControl::Create(const char *Path)
{
  const struct meta_dl *meta_data = NULL;
  BLURAY *bd;

  /* open disc */
  bd = bd_open(Path, NULL);
  if (!bd) {
    isyslog("opening BluRay disc %s failed", Path);
    return NULL;
  }

  /* load title list */
  unsigned num_title_idx = bd_get_titles(bd, TITLES_RELEVANT, MIN_TITLE_LENGTH);
  if (num_title_idx < 1) {
    esyslog("BluRay: no titles found");
    return NULL;
  }
  isyslog("BluRay: %d titles", num_title_idx);

  /* guess the main title */

  unsigned title_idx = 0;
  uint64_t duration = 0;
  int playlist = 99999;

  for (unsigned i = 0; i < num_title_idx; i++) {
    BLURAY_TITLE_INFO *info = bd_get_title_info(bd, i, 0);
    if (info) {
      if (info->duration > duration) {
        title_idx = i;
        duration  = info->duration;
        playlist  = info->playlist;
      }
      bd_free_title_info(info);
    }
  }
  isyslog("BluRay main title: #%d (%05d.mpls)\n", title_idx, playlist);

  /* init event queue */
  bd_get_event(bd, NULL);

  /* select playlist */
  if (bd_select_title(bd, title_idx) <= 0) {
    esyslog("bd_select_title(%d) failed", title_idx);
    return NULL;
  }

  cBDControl *control = new cBDControl(new cBDPlayer(bd));

  /* get disc name */
  meta_data = bd_get_meta(bd);
  if (meta_data && meta_data->di_name && strlen(meta_data->di_name) > 1) {
    control->disc_name = meta_data->di_name;
  }

  return control;
}

void cBDControl::Pause(void)
{
  if (player)
    player->Pause();
}

void cBDControl::Play(void)
{
  if (player)
    player->Play();
}

void cBDControl::SkipSeconds(int seconds)
{
  if (player)
    player->SkipSeconds(seconds);
}

void cBDControl::Goto(int seconds)
{
  if (player)
     player->Goto(seconds);
}

cString cBDControl::GetHeader(void)
{
  return disc_name;
}

void cBDControl::ShowTimed(int Seconds)
{
  // from vdr-1.7.34
  if (modeOnly)
     Hide();
  if (!visible) {
     shown = ShowProgress(true);
     timeoutShow = (shown && Seconds > 0) ? time(NULL) + Seconds : 0;
     }
  else if (timeoutShow && Seconds > 0)
     timeoutShow = time(NULL) + Seconds;
}

void cBDControl::Show(void)
{
  // from vdr-1.7.34
  ShowTimed();
}

void cBDControl::Hide(void)
{
  // from vdr-1.7.34
  if (visible) {
     delete displayReplay;
     displayReplay = NULL;
     SetNeedsFastResponse(false);
     visible = false;
     modeOnly = false;
     lastPlay = lastForward = false;
     lastSpeed = -2; // an invalid value
     timeSearchActive = false;
     timeoutShow = 0;
     }
}

void cBDControl::ShowMode(void)
{
  // from vdr-1.7.34
  if (visible || Setup.ShowReplayMode && !cOsd::IsOpen()) {
     bool Play, Forward;
     int Speed;
     if (GetReplayMode(Play, Forward, Speed) && (!visible || Play != lastPlay || Forward != lastForward || Speed != lastSpeed)) {
        bool NormalPlay = (Play && Speed == -1);

        if (!visible) {
           if (NormalPlay)
              return; // no need to do indicate ">" unless there was a different mode displayed before
           visible = modeOnly = true;
           displayReplay = Skins.Current()->DisplayReplay(modeOnly);
           }

        if (modeOnly && !timeoutShow && NormalPlay)
           timeoutShow = time(NULL) + MODETIMEOUT;
        displayReplay->SetMode(Play, Forward, Speed);
        lastPlay = Play;
        lastForward = Forward;
        lastSpeed = Speed;
        }
     }
}

bool cBDControl::ShowProgress(bool Initial)
{
  // from vdr-1.7.34
  int Current, Total;

  if (GetIndex(Current, Total) && Total > 0) {
     if (!visible) {
        displayReplay = Skins.Current()->DisplayReplay(modeOnly);
        SetNeedsFastResponse(true);
        visible = true;
        }
     if (Initial) {
        lastCurrent = lastTotal = -1;
        }
     if (Current != lastCurrent || Total != lastTotal) {
        if (Total != lastTotal) {
           int Index = Total;
           displayReplay->SetTotal(IndexToHMSF(Index, false, FramesPerSecond()));
           if (!Initial)
              displayReplay->Flush();
           }
        displayReplay->SetProgress(Current, Total);
        if (!Initial)
           displayReplay->Flush();
        displayReplay->SetCurrent(IndexToHMSF(Current, false, FramesPerSecond()));
        displayReplay->Flush();
        lastCurrent = Current;
        }
     lastTotal = Total;
     ShowMode();
     return true;
     }
  return false;
}

void cBDControl::TimeSearchDisplay(void)
{
  // from vdr-1.7.34
  char buf[64];
  // TRANSLATORS: note the trailing blank!
  strcpy(buf, tr("Jump: "));
  int len = strlen(buf);
  char h10 = '0' + (timeSearchTime >> 24);
  char h1  = '0' + ((timeSearchTime & 0x00FF0000) >> 16);
  char m10 = '0' + ((timeSearchTime & 0x0000FF00) >> 8);
  char m1  = '0' + (timeSearchTime & 0x000000FF);
  char ch10 = timeSearchPos > 3 ? h10 : '-';
  char ch1  = timeSearchPos > 2 ? h1  : '-';
  char cm10 = timeSearchPos > 1 ? m10 : '-';
  char cm1  = timeSearchPos > 0 ? m1  : '-';
  sprintf(buf + len, "%c%c:%c%c", ch10, ch1, cm10, cm1);
  displayReplay->SetJump(buf);
}

void cBDControl::TimeSearchProcess(eKeys Key)
{
  // from vdr-1.7.34
#define STAY_SECONDS_OFF_END 10
  int Seconds = (timeSearchTime >> 24) * 36000 + ((timeSearchTime & 0x00FF0000) >> 16) * 3600 + ((timeSearchTime & 0x0000FF00) >> 8) * 600 + (timeSearchTime & 0x000000FF) * 60;
  int Current = int(round(lastCurrent / FramesPerSecond()));
  int Total = int(round(lastTotal / FramesPerSecond()));
  switch (Key) {
    case k0 ... k9:
         if (timeSearchPos < 4) {
            timeSearchTime <<= 8;
            timeSearchTime |= Key - k0;
            timeSearchPos++;
            TimeSearchDisplay();
            }
         break;
    case kFastRew:
    case kLeft:
    case kFastFwd:
    case kRight: {
         int dir = ((Key == kRight || Key == kFastFwd) ? 1 : -1);
         if (dir > 0)
            Seconds = min(Total - Current - STAY_SECONDS_OFF_END, Seconds);
         SkipSeconds(Seconds * dir);
         timeSearchActive = false;
         }
         break;
#if 0
    case kPlayPause:
#endif
    case kPlay:
    case kUp:
    case kPause:
    case kDown:
    case kOk:
         Seconds = min(Total - STAY_SECONDS_OFF_END, Seconds);
         Goto(Seconds);
         timeSearchActive = false;
         break;
    default:
         if (!(Key & k_Flags)) // ignore repeat/release keys
            timeSearchActive = false;
         break;
    }

  if (!timeSearchActive) {
     if (timeSearchHide)
        Hide();
     else
        displayReplay->SetJump(NULL);
     ShowMode();
     }
}

void cBDControl::TimeSearch(void)
{
  // from vdr-1.7.34
  timeSearchTime = timeSearchPos = 0;
  timeSearchHide = false;
  if (modeOnly)
     Hide();
  if (!visible) {
     Show();
     if (visible)
        timeSearchHide = true;
     else
        return;
     }
  timeoutShow = 0;
  TimeSearchDisplay();
  timeSearchActive = true;
}


eOSState cBDControl::ProcessKey(eKeys Key)
{
  // from vdr-1.7.34
  if (!Active())
     return osEnd;
  if (visible) {
     if (timeoutShow && time(NULL) > timeoutShow) {
        Hide();
        ShowMode();
        timeoutShow = 0;
        }
     else if (modeOnly)
        ShowMode();
     else
        shown = ShowProgress(!shown) || shown;
     }
  if (timeSearchActive && Key != kNone) {
     TimeSearchProcess(Key);
     return osContinue;
     }
  bool DoShowMode = true;

  switch ((int)Key) {
    case kUp:
    case kPlay:   Play();
                  break;
    case kDown:
    case kPause:  Pause();
                  break;
    case kRed:    TimeSearch(); break;
    case kGreen:
    case kPrev:   SkipSeconds(-60);
                  break;
    case kYellow:
    case kNext:   SkipSeconds(60);
                  break;
    case kBlue:
    case kStop:   Hide();
                  return osEnd;
    case kBack:
                  break;
    default: {
      DoShowMode = false;
      switch (int(Key)) {
        default: {
          switch (Key) {
            case kOk:      if (visible && !modeOnly) {
                              Hide();
                              DoShowMode = true;
                              }
                           else
                              Show();
                           break;
            default:       return osUnknown;
          }
        }
      }
    }
  }
  if (DoShowMode)
    ShowMode();

  return osContinue;
}