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

#include <Poco/Net/HTTPRequest.h>
#include <Poco/Net/HTTPResponse.h>
//#include <Poco/StreamCopier.h>

#include <pcrecpp.h>

//--- cHlsSegmentLoader

cHlsSegmentLoader::cHlsSegmentLoader(std::string startm3u8)
{
	m_bufferFilled = false;
	m_lastLoadedSegment = 0;
	m_loadedSegments = 0;
	m_segmentsToBuffer = 3;
	m_pBuffer = new uchar[8192];

	// Initialize members
	m_pClientSession = NULL;

	m_ringBufferSize = MEGABYTE(32);
	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::Action(void)
{
	if( LoadStartList() ) {
		if( false == LoadIndexList() ) {
			esyslog("[plex]LoadIndexList failed!");
		}
	}
	else {
		esyslog("[plex]LoadStartList failed!");
		return;
	}


	int estSize = EstimateSegmentSize();
	m_ringBufferSize = MEGABYTE(estSize*3);

	isyslog("[plex]%s Create Ringbuffer %d MB", __FUNCTION__, estSize*3);

	m_pRingbuffer = new cRingBufferLinear(m_ringBufferSize, 2*TS_SIZE);
	
	while(Running()) {
		DoLoad();
		cCondWait::SleepMs(3);
	}
}

bool cHlsSegmentLoader::LoadIndexList(void)
{
	bool res = false;
	ConnectToServer();
	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__);
			return res;
		}

		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 = 1;
		}
		else {
			m_segmentsToBuffer = 2;
		}
	}
	return res;
}

bool cHlsSegmentLoader::LoadStartList(void)
{
	bool res = false;
	ConnectToServer();

	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__);
		return res;
	}

	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;
	}
	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 = max(estSize, 1.0); // default
	return ceil(estSize);
}

bool cHlsSegmentLoader::LoadSegment(std::string segmentUri)
{
	Poco::Net::HTTPRequest segmentRequest(Poco::Net::HTTPRequest::HTTP_GET, segmentUri);
	AddHeader(segmentRequest);
	m_pClientSession->sendRequest(segmentRequest);

	Poco::Net::HTTPResponse segmentResponse;
	std::istream& segmentFile = m_pClientSession->receiveResponse(segmentResponse);

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

	// copy response

	int m = 0;
	segmentFile.read(reinterpret_cast<char*>(m_pBuffer), sizeof(m_pBuffer));
	std::streamsize n = segmentFile.gcount();
	while(n > 0) {
		m = m_pRingbuffer->Put(m_pBuffer, n);
		if(m < n) {
			//
			esyslog("[plex]%s oops, this should not happen. Segment doesn't fitted completly into ringbuffer", __FUNCTION__);
			break;
		} else {
			segmentFile.read(reinterpret_cast<char*>(m_pBuffer), sizeof(m_pBuffer));
			n = segmentFile.gcount();
		}
	}
	return true;
}

int cHlsSegmentLoader::GetSegmentSize(int segmentIndex)
{
	//dsyslog("[plex]%s Segment %d", __FUNCTION__, segmentIndex);
	if(m_indexParser.vPlaylistItems[segmentIndex].size > 0) {
		return m_indexParser.vPlaylistItems[segmentIndex].size;
	}
	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();
}

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)
		m_pClientSession = new Poco::Net::HTTPClientSession(m_startUri.getHost(), m_startUri.getPort());

	return m_pClientSession->connected();
}

bool cHlsSegmentLoader::DoLoad(void)
{
	bool result = true;

	int nextSegmentSize = GetSegmentSize(m_lastLoadedSegment + 1);
	while(m_pRingbuffer->Free() > nextSegmentSize) {

		if(m_lastLoadedSegment + 1 <  m_indexParser.vPlaylistItems.size()) {
			std::string segmentUri = GetSegmentUri(++m_lastLoadedSegment);
			result = LoadSegment(segmentUri);
			m_loadedSegments++;
		} else {
			// out of segments
			StopLoader();
			result = false;
		}
		m_bufferFilled = m_lastLoadedSegment >= m_segmentsToBuffer;
		nextSegmentSize = GetSegmentSize(m_lastLoadedSegment + 1);
	}
	return m_bufferFilled = result;
}

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

bool cHlsSegmentLoader::StopLoader(void)
{
	dsyslog("[plex]%s", __FUNCTION__);
	std::string stopUri = "/video/:/transcode/universal/stop?session=" + m_sessionCookie;
	Poco::Net::HTTPRequest req(Poco::Net::HTTPRequest::HTTP_GET, stopUri);
	m_pClientSession->sendRequest(req);
	Poco::Net::HTTPResponse reqResponse;
	m_pClientSession->receiveResponse(reqResponse);

	Cancel();

	return reqResponse.getStatus() == 200;
}
void cHlsSegmentLoader::AddHeader(Poco::Net::HTTPRequest& req)
{
	req.add("X-Plex-Client-Identifier", Config::GetInstance().GetUUID());
	req.add("X-Plex-Product", "Plex Home Theater");
	req.add("X-Plex-Device", "PC");
	req.add("X-Plex-Platform", "Plex Home Theater");
	req.add("X-Plex-Model", "Linux");
}

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

//--- cHlsPlayer

cHlsPlayer::cHlsPlayer(std::string startm3u8, plexclient::Video* Video)
{
	m_pSegmentLoader = new cHlsSegmentLoader(startm3u8);
	m_pVideo = Video;
}

cHlsPlayer::~cHlsPlayer()
{
	delete m_pSegmentLoader;
}


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

	while (Running()) {
		if(m_pSegmentLoader->BufferFilled()) {
			DoPlay();
		} else {
			// Pause
			cCondWait::SleepMs(3);
		}
	}
}

bool cHlsPlayer::DoPlay(void)
{
	cPoller Poller;
	if(DevicePoll(Poller, 10)) {
		LOCK_THREAD;

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

		for(int i = 0; i < 100; 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);
				m_pSegmentLoader->m_pRingbuffer->Del(playedBytes);
			} else {
				// Pause & Buffer
				break;
			}
		}
	}
	return true;
}

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

bool cHlsPlayer::GetIndex(int& Current, int& Total, bool SnapToIFrame)
{
	long stc = DeviceGetSTC();
	Total = m_pVideo->m_pMedia->m_lDuration / 1000 * FramesPerSecond(); // milliseconds
	Current = stc / (100 * 1000) * FramesPerSecond(); // 100ns per Tick
	return true;
}

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

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

		DeviceFreeze();
		playMode = pmPause;
	}
}

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

		DevicePlay();
		playMode = pmPlay;
	}
}

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

void cHlsPlayer::Stop(void)
{
	if (m_pSegmentLoader)
		m_pSegmentLoader->StopLoader();
	Cancel(1);
}

double cHlsPlayer::FramesPerSecond(void)
{
	return m_pVideo->m_pMedia->m_VideoFrameRate ? m_pVideo->m_pMedia->m_VideoFrameRate : DEFAULTFRAMESPERSECOND;
}