summaryrefslogtreecommitdiff
path: root/hlsPlayer.h
blob: 1e88debb1aef70fdb2872cc318dbdcfbd0315b87 (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
#ifndef HLSPLAYER_H
#define HLSPLAYER_H

#include <string>
#include <iostream>
#include <sstream>

#include <vdr/thread.h>
#include <vdr/player.h>
#include <vdr/ringbuffer.h>
#include <vdr/tools.h>

#include <Poco/Net/HTTPClientSession.h>
#include <Poco/Net/HTTPRequest.h>
#include <Poco/URI.h>

#include "m3u8Parser.h"
#include "Config.h"
#include "PVideo.h"
#include "Media.h"

class cHlsSegmentLoader : public cThread
{
private:
	int m_ringBufferSize;
	unsigned int m_segmentsToBuffer;
	unsigned int m_lastLoadedSegment;
	int m_lastSegmentSize;
	bool m_bufferFilled;
	bool m_newList;
	int m_streamlenght;

	uchar* m_pBuffer;

	Poco::Net::HTTPClientSession* m_pClientSession;
	Poco::URI m_startUri;
	std::string m_sessionUriPart;
	std::string m_segmentUriPart;
	std::string m_sessionCookie;

	cM3u8Parser m_startParser;
	cM3u8Parser m_indexParser;

	bool ConnectToServer(void);
	void CloseConnection(void);
	bool LoadStartList(void);
	bool LoadIndexList(void);
	std::string GetSegmentUri(int segmentIndex) const;
	int GetSegmentSize(int segmentIndex);
	bool LoadSegment(std::string uri);
	int EstimateSegmentSize();
	bool LoadLists(void);
	void ResizeRingbuffer(int newsize);
	void SkipEmptySegments(int segmentLenght);
	

protected:
	void Action(void);
	bool DoLoad(void);

public:
	cHlsSegmentLoader(std::string startm3u8);
	~cHlsSegmentLoader();

	cRingBufferLinear*  m_pRingbuffer;
	bool BufferFilled(void);
	bool Active(void);
	bool StopLoader(void);
	bool LoadM3u8(std::string uri);
	void AddHeader(Poco::Net::HTTPRequest& req);
	void Ping(void);
	int GetStreamLenght();
};

class cHlsPlayer : public cPlayer, cThread
{
private:
	std::ofstream* m_pDebugFile;
	int AudioIndexOffset;
	cHlsSegmentLoader* m_pSegmentLoader;
	plexclient::Video m_Video;

	int m_jumpOffset;
	int m_timeOffset;
	bool m_doJump;
	bool m_isBuffering;

	int m_videoLenght;
	int m_actualSegment;
	int m_actualTime;
	long m_lastValidSTC;
	
	unsigned long long m_tLastTime;
	unsigned long long m_tTimeSum;
	bool m_bFirstPlay;
	cTimeMs m_tTimer;

	enum ePlayModes { pmPlay, pmPause };
	ePlayModes playMode;

	virtual void Activate(bool On);
	int GetPlayedSeconds(void);
	void CountPlayedSeconds(void);
	void ResetPlayedSeconds(void);
	void ReportProgress(bool stopped = false);
	void SetWatched(void);

protected:
	void Action(void);
	bool DoPlay(void);


public:
	//static cMutex s_mutex;
	cHlsPlayer(std::string startm3u8, plexclient::Video Video, int offset = 0);
	~cHlsPlayer();

	virtual bool GetIndex(int &Current, int &Total, bool SnapToIFrame = false);
	virtual bool GetReplayMode(bool &Play, bool &Forward, int &Speed);
	virtual double FramesPerSecond(void);
	virtual void SetAudioTrack(eTrackType Type, const tTrackId *TrackId);
	void Pause(void);
	void Play(void);
	void Stop(void);
	bool Active(void);
	void JumpTo(int seconds);
	void JumpRelative(int seconds);
	void SetAudioAndSubtitleTracks(void);

};

#endif // HLSPLAYER_H