summaryrefslogtreecommitdiff
path: root/hlsPlayer.cpp
blob: fc0c7cb94ce48124f659191fdfd60ab4f72914bd (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
#include "hlsPlayer.h"

#include <vdr/tools.h>
#include <Poco/Net/HTTPRequest.h>
#include <Poco/Net/HTTPResponse.h>
#include <Poco/Net/HTTPSClientSession.h>
#include <Poco/Exception.h>

#include <pcrecpp.h>
#include <algorithm>
#include <fstream>

#include "Plexservice.h"
#include "plexgdm.h"
#include "XmlObject.h"
#include "Media.h"
#include "Stream.h"

static cMutex hlsMutex;
const int BUFFERSIZE = 8192;

//--- cHlsSegmentLoader

cHlsSegmentLoader::cHlsSegmentLoader(std::string startm3u8, plexclient::cVideo *pVideo) {
    m_pVideo = pVideo;
    m_newList = false;
    m_bufferFilled = false;
    m_lastLoadedSegment = 0;
    m_segmentsToBuffer = 2;
    m_streamlenght = 0;
    m_lastSegmentSize = 0;
    m_pBuffer = new uchar[BUFFERSIZE];

    // Initialize members
    m_pClientSession = NULL;

    m_ringBufferSize = MEGABYTE(1);
    m_pRingbuffer = NULL;

    m_startUri = Poco::URI(startm3u8);
    m_startParser = cM3u8Parser();
    m_indexParser = cM3u8Parser();
}

cHlsSegmentLoader::~cHlsSegmentLoader() {
    // Stop Thread
    Cancel(0);

    delete m_pClientSession;
    delete[] m_pBuffer;
    delete m_pRingbuffer;
}

void cHlsSegmentLoader::SkipEmptySegments(int segmentDuration) {
    pcrecpp::RE re("&offset=(\\d+)", pcrecpp::RE_Options(PCRE_CASELESS));
    int value;
    re.PartialMatch(m_startUri.getQuery(), &value);
    if (value > segmentDuration) {
        m_lastLoadedSegment = (value / segmentDuration) - 1;
    }
}

bool cHlsSegmentLoader::LoadM3u8(std::string uri) {
    //LOCK_THREAD;
    m_startUri = Poco::URI(uri);
    return (m_newList = true);
}

void cHlsSegmentLoader::Action(void) {
    if (!LoadLists()) return;

    m_ringBufferSize = MEGABYTE(Config::GetInstance().BufferSize);

    isyslog("[plex]%s Create Ringbuffer %d MB", __FUNCTION__, Config::GetInstance().BufferSize);

    m_pRingbuffer = new cRingBufferLinear(m_ringBufferSize, 2 * TS_SIZE);

    while (Running()) {
        if (m_newList) {
            hlsMutex.Lock();
            m_bufferFilled = false;
            m_lastLoadedSegment = 0;
            LoadLists();
            m_newList = false;
            m_pRingbuffer->Clear();
            isyslog("[plex] Ringbuffer Cleared, loading new segments");
            hlsMutex.Unlock();
        }
        int count = 0;
        m_pRingbuffer->Get(count);
        if (!DoLoad() && count < TS_SIZE) {
            isyslog("[plex] No further segments to load and buffer empty, end of stream!");
            Cancel();

        }
        cCondWait::SleepMs(3);
    }
    StopLoader();
}

bool cHlsSegmentLoader::LoadLists(void) {
    if (LoadStartList()) {
        if (false == LoadIndexList()) {
            esyslog("[plex]LoadIndexList failed!");
            return false;
        }
    } else {
        esyslog("[plex]LoadStartList failed!");
        return false;
    }
    return true;
}

bool cHlsSegmentLoader::LoadIndexList(void) {
    bool res = false;
    try {
        if (m_startParser.MasterPlaylist && m_startParser.vPlaylistItems.size() > 0) {
            // Todo: make it universal, might only work for Plexmediaserver
            ConnectToServer();

            std::string startUri = m_startUri.toString();
            startUri = startUri.substr(0, startUri.find_last_of("/") + 1);

            Poco::URI indexUri(startUri + m_startParser.vPlaylistItems[0].file);

            Poco::Net::HTTPRequest request(Poco::Net::HTTPRequest::HTTP_GET, indexUri.getPath());
            AddHeader(request);
            // same server
            m_pClientSession->sendRequest(request);

            Poco::Net::HTTPResponse responseStart;
            std::istream &indexFile = m_pClientSession->receiveResponse(responseStart);

            if (responseStart.getStatus() != 200) {
                esyslog("[plex]%s Response Not Valid", __FUNCTION__);
                Poco::StreamCopier::copyStream(indexFile, std::cout);
                return res;
            }
            m_indexParser = cM3u8Parser();
            res = m_indexParser.Parse(indexFile);

            if (res) {
                // Segment URI is relative to index.m3u8
                std::string path = indexUri.getPath();
                m_segmentUriPart = path.substr(0, path.find_last_of("/") + 1);
            }
            if (m_indexParser.TargetDuration > 3) {
                m_segmentsToBuffer = 2;
            } else {
                m_segmentsToBuffer = 3;
            }

            SkipEmptySegments(m_indexParser.TargetDuration);
            m_lastSegmentSize = 0;

            // Get stream lenght
            m_streamlenght = 0;
            for (unsigned int i = 0; i < m_indexParser.vPlaylistItems.size(); i++) {
                m_streamlenght += m_indexParser.vPlaylistItems[i].length;
            }
        }

    } catch (Poco::Exception &exc) {
        esyslog("[plex] %s %s", __FUNCTION__, exc.displayText().c_str());
        res = false;
    }
    return res;
}

bool cHlsSegmentLoader::LoadStartList(void) {
    bool res = false;
    ConnectToServer();
    try {
        dsyslog("[plex]%s %s", __FUNCTION__, m_startUri.toString().c_str());
        Poco::Net::HTTPRequest request(Poco::Net::HTTPRequest::HTTP_GET, m_startUri.getPathAndQuery());
        AddHeader(request);
        m_pClientSession->sendRequest(request);

        Poco::Net::HTTPResponse responseStart;
        std::istream &startFile = m_pClientSession->receiveResponse(responseStart);

        if (responseStart.getStatus() != 200) {
            esyslog("[plex]%s Response Not Valid", __FUNCTION__);
            Poco::StreamCopier::copyStream(startFile, std::cout);
            return res;
        }

        m_startParser = cM3u8Parser();
        res = m_startParser.Parse(startFile);
        if (res) {
            // Get GUID
            pcrecpp::RE re("([0-9A-F]{8}-[0-9A-F]{4}-[0-9A-F]{4}-[0-9A-F]{4}-[0-9A-F]{12})",
                           pcrecpp::RE_Options(PCRE_CASELESS));
            string value;
            re.PartialMatch(m_startParser.vPlaylistItems[0].file, &value);
            m_sessionCookie = value;
        }
    } catch (Poco::Exception &exc) {
        esyslog("[plex] %s %s", __FUNCTION__, exc.displayText().c_str());
        res = false;
    }
    return res;
}

int cHlsSegmentLoader::EstimateSegmentSize() {
    if (&m_startParser.vPlaylistItems[0] == NULL) {
        esyslog("[plex]%s first element NULL", __FUNCTION__);
    }
    double bandw = m_startParser.vPlaylistItems[0].bandwidth / 8.0 / 1000.0 / 1000.0;

    int len = m_indexParser.TargetDuration;
    double estSize = (bandw) * len;
    estSize = std::max(estSize, 1.0);
    // default
    if (estSize <= 1) {
        estSize = 32;
    }
    return ceil(estSize);
}

bool cHlsSegmentLoader::LoadSegment(std::string segmentUri) {
    try {
        Poco::Net::HTTPRequest segmentRequest(Poco::Net::HTTPRequest::HTTP_GET, segmentUri);
        AddHeader(segmentRequest);
        m_pClientSession->sendRequest(segmentRequest);
    } catch (Poco::Exception &) {
        esyslog("[plex] %s; %s failed.", __FUNCTION__, segmentUri.c_str());
        return false;
    }
    Poco::Net::HTTPResponse segmentResponse;
    std::istream &segmentFile = m_pClientSession->receiveResponse(segmentResponse);

    if (segmentResponse.getStatus() != 200) {
        // error
        esyslog("[plex] %s; %s failed.", __FUNCTION__, segmentUri.c_str());
        return false;
    }

    if (segmentResponse.getContentLength() <= TS_SIZE) {
        // Empty segment
        dsyslog("[plex] %s: %s <= 188 byte.", __FUNCTION__, segmentUri.c_str());
        return true;
    }

    dsyslog("[plex] %s: %s successfully.", __FUNCTION__, segmentUri.c_str());
    // copy response
    segmentFile.read(reinterpret_cast<char *>(m_pBuffer), BUFFERSIZE);
    std::streamsize n = segmentFile.gcount();
    while (n > 0) {
        if (m_pRingbuffer->Free() >= n) {
            m_pRingbuffer->Put(m_pBuffer, n);

            segmentFile.read(reinterpret_cast<char *>(m_pBuffer), BUFFERSIZE);
            n = segmentFile.gcount();
            m_bufferFilled = true;
        }
        else {
            cCondWait::SleepMs(1);
        }
        if (m_newList) {
            break;
        }
    }
    return true;
}

int cHlsSegmentLoader::GetSegmentSize(int segmentIndex) {
    //dsyslog("[plex]%s Segment %d", __FUNCTION__, segmentIndex);
    if (m_indexParser.vPlaylistItems[segmentIndex].size >= TS_SIZE) {
        return m_indexParser.vPlaylistItems[segmentIndex].size;
    }
    try {
        Poco::Net::HTTPRequest req(Poco::Net::HTTPRequest::HTTP_HEAD, GetSegmentUri(segmentIndex));
        AddHeader(req);
        m_pClientSession->sendRequest(req);
        Poco::Net::HTTPResponse reqResponse;
        m_pClientSession->receiveResponse(reqResponse);
        return m_indexParser.vPlaylistItems[segmentIndex].size = reqResponse.getContentLength();
    } catch (Poco::IOException &exc) {
        esyslog("[plex]%s %s ", __FUNCTION__, exc.displayText().c_str());
        return 0;
    }
}

std::string cHlsSegmentLoader::GetSegmentUri(int segmentIndex) const {
    return m_segmentUriPart + m_indexParser.vPlaylistItems[segmentIndex].file;
}

void cHlsSegmentLoader::CloseConnection(void) {
    if (m_pClientSession)
        m_pClientSession->abort();

    delete m_pClientSession;
    m_pClientSession = NULL;
}

bool cHlsSegmentLoader::ConnectToServer(void) {
    dsyslog("[plex]%s", __FUNCTION__);
    if (!m_pClientSession) {
        if (m_startUri.getScheme().find("https") != std::string::npos) {
            m_pClientSession = new Poco::Net::HTTPSClientSession(m_startUri.getHost(), m_startUri.getPort());
        }
        else {
            m_pClientSession = new Poco::Net::HTTPClientSession(m_startUri.getHost(), m_startUri.getPort());
        }
    }

    return m_pClientSession->connected();
}

bool cHlsSegmentLoader::DoLoad(void) {
    LOCK_THREAD;
    bool result = true;
    bool recover = false;
    if (m_lastLoadedSegment < m_indexParser.vPlaylistItems.size()) {

        std::string segmentUri = GetSegmentUri(m_lastLoadedSegment);
        if ((result = LoadSegment(segmentUri))) {
            m_lastLoadedSegment++;
        } else {
            // transcoder may be died, plex bug, restart transcode session
            esyslog("[plex] %s 404, Transcoder died, see logfile from PMS", __FUNCTION__);
            recover = true;
            std::string stopUri = "/video/:/transcode/universal/stop?session=" + m_sessionCookie;
            try {

                bool ok;
                auto cSession = m_pVideo->m_pServer->MakeRequest(ok, stopUri);
                int tmp = m_lastLoadedSegment;
                int tmp2 = m_lastSegmentSize;
                CloseConnection();
                LoadLists();
                m_lastLoadedSegment = tmp;
                m_lastSegmentSize = tmp2;
            } catch (Poco::Exception &) {
                return false;
            }
        }

        m_bufferFilled = result;
    } else {
        result = false;
    }
    return recover || result;
}

bool cHlsSegmentLoader::BufferFilled(void) {
    return m_bufferFilled;
}

bool cHlsSegmentLoader::StopLoader(void) {
    dsyslog("[plex]%s", __FUNCTION__);
    try {
        std::string stopUri = "/video/:/transcode/universal/stop?session=" + m_sessionCookie;

        bool ok;
        auto cSession = m_pVideo->m_pServer->MakeRequest(ok, stopUri);

        Cancel();

        return ok;
    } catch (Poco::Exception &exc) {
        esyslog("[plex]%s %s ", __FUNCTION__, exc.displayText().c_str());
        return false;
    }
}

void cHlsSegmentLoader::AddHeader(Poco::Net::HTTPRequest &req) {
    req.add("X-Plex-Client-Identifier", Config::GetInstance().GetUUID());
//	req.add("X-Plex-Device", "PC");
    req.add("X-Plex-Model", "Linux");
    if (Config::GetInstance().UseCustomTranscodeProfile) {
        req.add("X-Plex-Device", "VDR Plex Plugin");
    } else {
        req.add("X-Plex-Device", "Plex Home Theater");
        //req.add("X-Plex-Device", "tvOS");
    }

    if (Config::GetInstance().UsePlexAccount && !m_pVideo->m_pServer->GetAuthToken().empty()) {
        // Add PlexToken to Header
        req.add("X-Plex-Token", m_pVideo->m_pServer->GetAuthToken());
    }
}

bool cHlsSegmentLoader::Active(void) {
    return Running();
}

void cHlsSegmentLoader::Ping(void) {
    dsyslog("[plex]%s", __FUNCTION__);
    try {
        std::string uri = "/video/:/transcode/universal/ping?session=" + Config::GetInstance().GetUUID();

        bool ok;
        auto cSession = m_pVideo->m_pServer->MakeRequest(ok, uri);

    } catch (Poco::Exception &exc) {
        esyslog("[plex]%s %s ", __FUNCTION__, exc.displayText().c_str());
    }
}

void cHlsSegmentLoader::ResizeRingbuffer(int newsize) {
    hlsMutex.Lock();
    isyslog("[plex] %s, Oldsize: %d, Newsize: %d", __FUNCTION__, m_ringBufferSize, newsize);
    //Create new Ringbuffer
    cRingBufferLinear *newBuffer = new cRingBufferLinear(newsize, TS_SIZE);
    // Copy old data
    int count = 0;
    uchar *pData = m_pRingbuffer->Get(count);
    newBuffer->Put(pData, count);
    // delete old buffer
    delete m_pRingbuffer;
    m_pRingbuffer = NULL;
    // assing new buffer
    m_pRingbuffer = newBuffer;
    m_ringBufferSize = newsize;
    hlsMutex.Unlock();
}

int cHlsSegmentLoader::GetStreamLenght() {
    return m_streamlenght;
}

//--- cHlsPlayer

cHlsPlayer::cHlsPlayer(std::string startm3u8, plexclient::cVideo Video, int offset) {
    dsyslog("[plex]: '%s'", __FUNCTION__);
    m_Video = Video;
    m_timeOffset = offset;
    m_jumpOffset = 0;
    m_tTimeSum = 0;
    m_doJump = false;
    m_isBuffering = false;
    AudioIndexOffset = 1000; // Just a magic number
    m_tTimer.Set(1);
    m_pSegmentLoader = new cHlsSegmentLoader(startm3u8, &m_Video);

    m_pDebugFile = NULL;
    //m_pDebugFile = new std::ofstream();
    //m_pDebugFile->open("debug.ts", std::ios::out);
}

cHlsPlayer::~cHlsPlayer() {
    dsyslog("[plex]: '%s'", __FUNCTION__);
    Cancel();
    if (m_pDebugFile) m_pDebugFile->close();
    delete m_pDebugFile;
    m_pDebugFile = NULL;

    delete m_pSegmentLoader;
    m_pSegmentLoader = NULL;
    Detach();
}

void cHlsPlayer::SetAudioAndSubtitleTracks(void) {
    //DeviceClrAvailableTracks();
    DeviceSetAvailableTrack(ttDolby, 0, 0, "Current");
    if (m_Video.m_Media.m_vStreams.size() > 0) {
        std::vector<plexclient::Stream> streams = m_Video.m_Media.m_vStreams;
        for (std::vector<plexclient::Stream>::iterator it = streams.begin(); it != streams.end(); ++it) {
            plexclient::Stream *pStream = &(*it);
            if (pStream->m_eStreamType == plexclient::StreamType::sAUDIO) {
                DeviceSetAvailableTrack(ttDolby, pStream->m_iIndex, pStream->m_iIndex + AudioIndexOffset,
                                        pStream->m_sLanguage.c_str(), pStream->m_sCodec.c_str());
            }
        }
    }
}


void cHlsPlayer::Action(void) {
    // Start SegmentLoader
    m_pSegmentLoader->Start();

    m_bFirstPlay = true;

    while (Running()) {
        if (m_doJump && m_pSegmentLoader && m_pSegmentLoader->BufferFilled()) {
            LOCK_THREAD;

            DeviceFreeze();
            m_doJump = false;
            m_bFirstPlay = true;
            std::string uri = plexclient::Plexservice::GetUniversalTranscodeUrl(&m_Video, m_jumpOffset);
            m_pSegmentLoader->LoadM3u8(uri);
            m_timeOffset = m_jumpOffset;
            DeviceClear();
            DevicePlay();
            playMode = pmPlay;
        } else if (m_pSegmentLoader && m_pSegmentLoader->BufferFilled()) {
            DoPlay();
            if (m_bFirstPlay) {
                SetAudioAndSubtitleTracks();
                ResetPlayedSeconds();
                playMode = pmPlay;
                m_bFirstPlay = false;
            }
            CountPlayedSeconds();
        } else {
            // Pause
            cCondWait::SleepMs(3);
        }
        // statusupdates to pms every 60s
        if (m_pSegmentLoader && m_tTimer.TimedOut()) {
            if (playMode == pmPause) {
                m_pSegmentLoader->Ping();
            }
            ReportProgress();
            m_tTimer.Set(60000);
        }
    }
    // set as watched if >= 90%
    int t = m_Video.m_Media.m_lDuration / 1000 * 0.9;
    if (GetPlayedSeconds() >= t) {
        SetWatched();
    }
    DeviceClear();
}

bool cHlsPlayer::DoPlay(void) {
    bool res = false;
    cPoller Poller;
    if (DevicePoll(Poller, 10)) {
        hlsMutex.Lock();

// Handle running out of packets. Buffer-> Play-> Pause-> Buffer-> Play

        for (int i = 0; i < 10; i++) {
            // Get a pointer to start of the data and the number of avaliable bytes
            int bytesAvaliable = 0;
            uchar *toPlay = m_pSegmentLoader->m_pRingbuffer->Get(bytesAvaliable);
            if (bytesAvaliable >= TS_SIZE) {
                int playedBytes = PlayTs(toPlay, TS_SIZE, false);
                // save stream to disk to debug data
                if (m_pDebugFile) {
                    m_pDebugFile->write((char *) toPlay, playedBytes);
                }

                m_pSegmentLoader->m_pRingbuffer->Del(playedBytes);
                res = true;
            } else {
                // Pause & Buffer
                break;
            }
        }
        hlsMutex.Unlock();
    }
    return res;
}

void cHlsPlayer::Activate(bool On) {
    if (On) {
        Start();
    } else {
        Cancel(1);
    }
}

bool cHlsPlayer::GetIndex(int &Current, int &Total, bool SnapToIFrame __attribute__((unused))) {
    if (m_Video.m_Media.m_lDuration > 0) {
        Total = m_Video.m_Media.m_lDuration / 1000 * FramesPerSecond(); // milliseconds
    } else {
        Total = m_pSegmentLoader->GetStreamLenght() * FramesPerSecond();
    }
    Current = GetPlayedSeconds() * FramesPerSecond();
    return true;
}

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

void cHlsPlayer::Pause(void) {
    LOCK_THREAD;
    dsyslog("[plex]%s", __FUNCTION__);
    if (playMode == pmPause) {
        Play();
    } else {
        DeviceFreeze();
        playMode = pmPause;
    }
}

void cHlsPlayer::Play(void) {
    LOCK_THREAD;
    dsyslog("[plex]%s", __FUNCTION__);
    if (playMode != pmPlay) {
        DevicePlay();
        playMode = pmPlay;
    }
}

bool cHlsPlayer::Active(void) {
    return Running() && m_pSegmentLoader && m_pSegmentLoader->Active();
}

void cHlsPlayer::Stop(void) {
    LOCK_THREAD;
    dsyslog("[plex]%s", __FUNCTION__);
    ReportProgress(true);
    if (m_pSegmentLoader)
        m_pSegmentLoader->StopLoader();
    Cancel(1);
}

double cHlsPlayer::FramesPerSecond(void) {
    return m_Video.m_Media.m_VideoFrameRate == "24p" ? 24 : DEFAULTFRAMESPERSECOND;
}

void cHlsPlayer::JumpRelative(int seconds) {
    JumpTo(GetPlayedSeconds() + seconds);
}

void cHlsPlayer::JumpTo(int seconds) {
    LOCK_THREAD;
    dsyslog("[plex]%s %d", __FUNCTION__, seconds);
    if (seconds < 0) seconds = 0;
    m_jumpOffset = seconds;
    m_doJump = true;
}

void cHlsPlayer::SetAudioTrack(eTrackType Type __attribute__((unused)), const tTrackId *TrackId) {
    LOCK_THREAD;
    dsyslog("[plex]%s %d %s", __FUNCTION__, TrackId->id, TrackId->language);
    // Check if stream availiable
    int streamId = 0;
    if (m_Video.m_Media.m_vStreams.size() > 0) {
        std::vector<plexclient::Stream> streams = m_Video.m_Media.m_vStreams;
        for (std::vector<plexclient::Stream>::iterator it = streams.begin(); it != streams.end(); ++it) {
            plexclient::Stream *pStream = &(*it);
            if (pStream->m_eStreamType == plexclient::StreamType::sAUDIO &&
                pStream->m_iIndex + AudioIndexOffset == TrackId->id) {
                streamId = pStream->m_iID;
                break;
            }
        }
    }
    // Then do the request
    if (streamId > 0) {
        std::string uri = "/library/parts/" + std::string(itoa(m_Video.m_Media.m_iPartId)) + "?audioStreamID=" +
                          std::string(itoa(streamId));

        bool ok;
        auto cSession = m_Video.m_pServer->MakeRequest(ok, uri);
        if (ok) {
            DeviceSetCurrentAudioTrack(eTrackType(ttDolby + 0)); // hacky
            DeviceSetAvailableTrack(ttDolby, 0, 0, TrackId->language);
            JumpRelative(0); // Reload Stream to get new Audio
            dsyslog("[plex]: Set AudioStream: %d\n", TrackId->id);
        }
    }
}

int cHlsPlayer::GetPlayedSeconds(void) {
    return m_timeOffset + (m_tTimeSum / 1000);
}

void cHlsPlayer::CountPlayedSeconds(void) {
    if (playMode == pmPlay) {
        unsigned long long tTmp = cTimeMs::Now();
        m_tTimeSum += (tTmp - m_tLastTime);
        m_tLastTime = tTmp;
    } else {
        m_tLastTime = cTimeMs::Now();
    }
}

void cHlsPlayer::ResetPlayedSeconds(void) {
    m_tTimeSum = 0;
    m_tLastTime = cTimeMs::Now();
}

void cHlsPlayer::ReportProgress(bool stopped) {
    std::string state;
    if (stopped) {
        state = "stopped";
    } else if (playMode == pmPlay) {
        state = "playing";
    } else {
        state = "paused";
    }

    try {
        std::string uri = "/:/progress?key=" + std::string(itoa(m_Video.m_iRatingKey)) +
                          "&identifier=com.plexapp.plugins.library&time=" +
                          std::string(itoa(GetPlayedSeconds() * 1000)) + "&state=" + state;

        bool ok;
        auto cSession = m_Video.m_pServer->MakeRequest(ok, uri);

        if (ok) {
            dsyslog("[plex] %s", __FUNCTION__);
        }
    } catch (Poco::Exception &) { }

}

void cHlsPlayer::SetWatched(void) {
    std::string uri =
            "/:/scrobble?key=" + std::string(itoa(m_Video.m_iRatingKey)) + "&identifier=com.plexapp.plugins.library";

    bool ok;
    auto cSession = m_Video.m_pServer->MakeRequest(ok, uri);

    if (ok) {
        dsyslog("[plex] %s", __FUNCTION__);
    }
}