aboutsummaryrefslogtreecommitdiff
path: root/Src/playlist/M3ULoader.cpp
blob: ebab35da0f5913730c5ee6ffd8b0d5bd0dba7b5f (plain) (blame)
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
#include <stdio.h>
#include <shlwapi.h>
#include <strsafe.h>
#include <fstream>
#include <string>

#include "M3ULoader.h"
#include "../nu/ns_wc.h"

#include "../WAT/WAT.h"


M3ULoader::M3ULoader() : _utf8( false )
{
	wideTitle[ 0 ] = wideFilename[ 0 ] = 0;
}

M3ULoader::~M3ULoader( void )
{
	//Close();
}

struct cmpWchar_t {
	bool operator()(const wchar_t* a, const wchar_t* b) const {
		return wcscmp(a, b) < 0;
	}
};

class M3UInfo : public ifc_plentryinfo
{
public:
	M3UInfo()                                                         {}
	M3UInfo( wchar_t *_mediahash, wchar_t *_metahash, wchar_t *_cloud_id, wchar_t *_cloud_status, wchar_t *_cloud_devices )
	{
		_extended_infos.emplace( _wcsdup( _INFO_NAME_MEDIA_HASH ),    _wcsdup( _mediahash) );
		_extended_infos.emplace( _wcsdup( _INFO_NAME_META_HASH ),     _wcsdup( _metahash ) );

		_extended_infos.emplace( _wcsdup( _INFO_NAME_CLOUD_ID ),      _wcsdup( _cloud_id ) );
		_extended_infos.emplace( _wcsdup( _INFO_NAME_CLOUD_STATUS ),  _wcsdup( _cloud_status ) );
		_extended_infos.emplace( _wcsdup( _INFO_NAME_CLOUD_DEVICES ), _wcsdup( _cloud_devices ) );
	}

	~M3UInfo()
	{
		for ( auto l_extended_infos_iterator = _extended_infos.begin(); l_extended_infos_iterator != _extended_infos.end(); ++l_extended_infos_iterator )
		{
			free( ( *l_extended_infos_iterator ).first );
			free( ( *l_extended_infos_iterator ).second );
		}

		_extended_infos.clear();
	}

	void SetExtendedInfo( const wchar_t *p_parameter_name, const wchar_t *p_parameter_value )
	{
		_extended_infos.emplace( _wcsdup( p_parameter_name ), _wcsdup( p_parameter_value ) );
	}

	const wchar_t *GetExtendedInfo( wchar_t *parameter )
	{
		//for ( auto l_extended_infos_iterator = _extended_infos.begin(); l_extended_infos_iterator != _extended_infos.end(); ++l_extended_infos_iterator )
		//{
		//	wchar_t *l_key = _wcsdup( ( *l_extended_infos_iterator ).first );
		//	if ( wcscmp( l_key, parameter ) == 0 )
		//		return _wcsdup( ( *l_extended_infos_iterator ).second );
		//}
		
		// OLD
		//std::map<wchar_t *, wchar_t *>::iterator l_extended_infos_iterator = _extended_infos.find( parameter );

		//if ( l_extended_infos_iterator != _extended_infos.end() )
		//	return _wcsdup( ( *l_extended_infos_iterator ).second );

		auto it = _extended_infos.find(parameter);
		if (_extended_infos.end() != it)
		{
			return it->second;
		}

		return 0;
	}

private:
	RECVS_DISPATCH;

	std::map<wchar_t *, wchar_t *, cmpWchar_t> _extended_infos;
};

#define CBCLASS M3UInfo
START_DISPATCH;
CB( IFC_PLENTRYINFO_GETEXTENDEDINFO, GetExtendedInfo )
END_DISPATCH;
#undef CBCLASS


int M3ULoader::OnFileHelper( ifc_playlistloadercallback *playlist, const wchar_t *trackName, const wchar_t *title, int length, const wchar_t *rootPath, ifc_plentryinfo *extraInfo )
{
	if ( length == -1000 )
		length = -1;

	wcsncpy( wideFilename, trackName, FILENAME_SIZE );
	
	int ret;

	if ( wcsstr( wideFilename, L"://" ) || PathIsRootW( wideFilename ) )
	{
		ret = playlist->OnFile( wideFilename, title, length, extraInfo );
	}
	else
	{
		wchar_t fullPath[ MAX_PATH ] = { 0 };
		if ( PathCombineW( fullPath, rootPath, wideFilename ) )
		{
			wchar_t canonicalizedPath[ MAX_PATH ] = { 0 };
			PathCanonicalizeW( canonicalizedPath, fullPath );
			ret = playlist->OnFile( canonicalizedPath, title, length, extraInfo );
		}
		else
		{
			ret = ifc_playlistloadercallback::LOAD_CONTINUE;
		}
	}

	return ret;
}

static bool StringEnds( const wchar_t *a, const wchar_t *b )
{
	size_t aLen = wcslen( a );
	size_t bLen = wcslen( b );

	if ( aLen < bLen )
		return false;  // too short

	if ( !_wcsicmp( a + aLen - bLen, b ) )
		return true;

	return false;
}

int M3ULoader::Load( const wchar_t *p_filename, ifc_playlistloadercallback *playlist )
{
	// TODO: download temp file if it's a URL
	// TODO - WDP2-198

	FILE *fp = _wfopen( p_filename, L"rt,ccs=UNICODE" );
	if ( !fp )
		return IFC_PLAYLISTLOADER_FAILED;

	fseek( fp, 0, SEEK_END );
	int size = ftell( fp );
	fseek( fp, 0, SEEK_SET );

	if ( size == -1 )
	{
		fclose( fp );
		fp = 0;

		return IFC_PLAYLISTLOADER_FAILED;
	}

	if ( StringEnds( p_filename, L".m3u8" ) )
		_utf8 = true;

	int ext = 0;

	wchar_t *p;

	const int l_linebuf_size = 2048;
	wchar_t linebuf[ l_linebuf_size ] = { 0 };

	wchar_t ext_title[ MAX_PATH ]    = { 0 };

	wchar_t ext_mediahash[ 128 ]     = { 0 };
	wchar_t ext_metahash[ 128 ]      = { 0 };

	wchar_t ext_cloud_id[ 128 ]      = { 0 };
	wchar_t ext_cloud_status[ 16 ]   = { 0 };
	wchar_t ext_cloud_devices[ 128 ] = { 0 };

	int ext_len = -1;

	wchar_t rootPath[ MAX_PATH ] = { 0 };
	const wchar_t *callbackPath = playlist->GetBasePath();
	if ( callbackPath )
		StringCchCopyW( rootPath, MAX_PATH, callbackPath );
	else
	{
		StringCchCopyW( rootPath, MAX_PATH, p_filename );
		PathRemoveFileSpecW( rootPath );
	}

	unsigned char BOM[ 3 ] = { 0, 0, 0 };
	if ( fread( BOM, 3, 1, fp ) == 1 && BOM[ 0 ] == 0xEF && BOM[ 1 ] == 0xBB && BOM[ 2 ] == 0xBF )
		_utf8 = true;
	else
		fseek( fp, 0, SEEK_SET );


	std::wstring l_separator     = L"\" ";
	std::wstring l_key_separator = L"=";


	const wchar_t _ASF[]                      = L"ASF ";
	const wchar_t _DIRECTIVE_EXTINF[]         = L"#EXTINF:";
	const wchar_t _DIRECTIVE_EXTM3U[]         = L"#EXTM3U";
	const wchar_t _DIRECTIVE_EXT_X_NS_CLOUD[] = L"#EXT-X-NS-CLOUD:";
	const wchar_t _DIRECTIVE_UTF8[]           = L"#UTF8";
	const wchar_t _END_LINE[]                 = L"\r\n";

	const int l_move_size = sizeof( wchar_t );


	wa::strings::wa_string l_key_value_pair = "";
	wa::strings::wa_string l_key            = "";
	wa::strings::wa_string l_value          = "";

	std::map<std::wstring, std::wstring> l_extended_infos;

	while ( 1 )
	{
		if ( feof( fp ) )
			break;

		linebuf[ 0 ] = 0;
		fgetws( linebuf, l_linebuf_size - 1, fp );

		linebuf[ wcscspn( linebuf, _END_LINE ) ] = 0;
		if ( wcslen( linebuf ) == 0 )
			continue;

 		if ( ext == 0 && wcsstr( linebuf, _DIRECTIVE_EXTM3U ) )
		{
			ext = 1;

			continue;
		}

		if ( !wcsncmp( linebuf, _DIRECTIVE_UTF8, 5 ) )
		{
			_utf8 = true;

			continue;
		}

		p = linebuf;

		while ( p && *p == ' ' || *p == '\t' )
			p = CharNextW( p );

		if ( *p != '#' && *p != '\n' && *p != '\r' && *p )
		{
			wchar_t buf[ 4096 ] = { 0 };

			wchar_t *p2 = CharPrevW( linebuf, linebuf + wcslen( linebuf ) ); //GetLastCharacter(linebuf);
			if ( p2 && *p2 == '\n' )
				*p2 = 0;

			if ( !wcsncmp( p, _ASF, 4 ) && wcslen( p ) > 4 )
				p += 4;

			if ( wcsncmp( p, L"\\\\", 2 ) && wcsncmp( p + 1, L":\\", 2 ) && wcsncmp( p + 1, L":/", 2 ) && !wcsstr( p, L"://" ) )
			{
				if ( p[ 0 ] == '\\' )
				{
					buf[ 0 ] = rootPath[ 0 ];
					buf[ 1 ] = rootPath[ 1 ];

					StringCchCopyW( buf + 2, 4093, p );

					//buf[ wcslen( buf ) - 1 ] = 0;
					buf[ wcscspn( buf, _END_LINE ) ] = 0;
					p = buf;
				}
			}

			int ret;

			// generate extra info from the cloud specific values (if present)
			M3UInfo info( ext_mediahash, ext_metahash, ext_cloud_id, ext_cloud_status, ext_cloud_devices );


			if ( !l_extended_infos.empty() )
			{
				for ( auto l_extended_infos_iterator = l_extended_infos.begin(); l_extended_infos_iterator != l_extended_infos.end(); ++l_extended_infos_iterator )
				{
					info.SetExtendedInfo( ( *l_extended_infos_iterator ).first.c_str(), ( *l_extended_infos_iterator ).second.c_str() );
				}
			}

			l_extended_infos.clear();

			if ( ext_title[ 0 ] )
			{
				wcsncpy( wideTitle, ext_title, FILETITLE_SIZE );
				ret = OnFileHelper( playlist, p, wideTitle, ext_len * 1000, rootPath, &info );
			}
			else
			{
				ret = OnFileHelper( playlist, p, 0, -1, rootPath, &info );
			}

			if ( ret != ifc_playlistloadercallback::LOAD_CONTINUE )
				break;

			ext_len        = -1;
			ext_title[ 0 ] = 0;
		}
		else
		{
			if ( ext && !wcsncmp( p, _DIRECTIVE_EXTINF, 8 ) )
			{
				p += 8;
				ext_len = _wtoi( p );

				int l_track_length = ext_len;
				int l_digits = ( l_track_length < 0 ? 1 : 0 );
				while ( l_track_length )
				{
					l_track_length /= 10;
					++l_digits;
				}

				p += l_digits;


				if ( p && *p )
				{
					wchar_t *p2 = CharPrevW( p, p + wcslen( p ) ); // GetLastCharacter(p);
					if ( p2 && *p2 == '\n' )
						*p2 = 0;

					while ( p && *p == ' ' )
						p = CharNextW( p );

					std::wstring l_string( p );

					int l_pos = l_string.find_first_of( L"," );

					if ( l_pos > 0 )
					{
						int  l_key_separator_pos = 0;

						wa::strings::wa_string l_line_trail( l_string.substr( 0, l_pos ) );

						while ( !l_line_trail.empty() )
						{
							int l_separator_pos = l_line_trail.find( l_separator );

							if ( l_separator_pos > 0 )
								l_key_value_pair = l_line_trail.mid( 0, l_separator_pos + 1 );
							else
								l_key_value_pair = l_line_trail;


							l_key_separator_pos = l_key_value_pair.find( l_key_separator );

							l_key   = l_key_value_pair.mid( 0, l_key_separator_pos );
							l_value = l_key_value_pair.mid( l_key_separator_pos + 1, l_key_value_pair.lengthS() - l_key_separator_pos + 1 );

							l_value.replaceAll( "\"", "" );

							l_extended_infos.emplace( l_key.GetW(), l_value.GetW() );

							if ( l_separator_pos > 0 )
								l_line_trail = l_line_trail.mid( l_separator_pos + l_move_size, l_line_trail.lengthS() - l_separator_pos + 1 );
							else
								l_line_trail.clear();
						}


						l_string = l_string.substr( l_pos + 1, l_string.size() - l_pos );

						StringCchCopyW( ext_title, MAX_PATH, l_string.c_str() );
					}
					else
						StringCchCopyW( ext_title, MAX_PATH, CharNextW( p ) );
				}
				else
				{
					ext_len        = -1;
					ext_title[ 0 ] = 0;
				}
			}
			// cloud specific playlist line for holding information about the entry
			else if ( ext && !wcsncmp( p, _DIRECTIVE_EXT_X_NS_CLOUD, 16 ) )
			{
				p += 16;
				wchar_t *pt = wcstok( p, L"," );
				while ( pt != NULL )
				{
					int end = (int)wcscspn( pt, L"=" );

					if ( !wcsncmp( pt, _INFO_NAME_MEDIA_HASH, end ) )
					{
						if ( ( lstrcpynW( ext_mediahash, pt + end + 1, 128 ) ) == NULL )
							return IFC_PLAYLISTLOADER_FAILED;
					}
					else if ( !wcsncmp( pt, _INFO_NAME_META_HASH, end ) )
					{
						if ( ( lstrcpynW( ext_metahash, pt + end + 1, 128 ) ) == NULL )
							return IFC_PLAYLISTLOADER_FAILED;
					}
					else if ( !wcsncmp( pt, _INFO_NAME_CLOUD_ID, end ) )
					{
						if ( ( lstrcpynW( ext_cloud_id, pt + end + 1, 128 ) ) == NULL )
							return IFC_PLAYLISTLOADER_FAILED;
					}
					else if ( !wcsncmp( pt, _INFO_NAME_CLOUD_STATUS, end ) )
					{
						if ( ( lstrcpynW( ext_cloud_status, pt + end + 1, 16 ) ) == NULL )
							return IFC_PLAYLISTLOADER_FAILED;
					}
					else if ( !wcsncmp( pt, _INFO_NAME_CLOUD_DEVICES, end ) )
					{
						wchar_t *p2 = pt + end + 1;
						while ( p2 && *p2 != '\n' )
							p2 = CharNextW( p2 );

						if ( p2 && *p2 == '\n' )
							*p2 = 0;

						if ( ( lstrcpynW( ext_cloud_devices, pt + end + 1, 128 ) ) == NULL )
							return IFC_PLAYLISTLOADER_FAILED;
					}

					pt = wcstok( NULL, L"," );
				}
			}
			else
			{
				ext_len                = -1;
				ext_title[ 0 ]         = 0;
				ext_mediahash[ 0 ]     = 0;
				ext_metahash[ 0 ]      = 0;
				ext_cloud_id[ 0 ]      = 0;
				ext_cloud_status[ 0 ]  = 0;
				ext_cloud_devices[ 0 ] = 0;
			}
		}
	}

	if ( fp )
		fclose( fp );

	return IFC_PLAYLISTLOADER_SUCCESS;
}


#ifdef CBCLASS
#undef CBCLASS
#endif

#define CBCLASS M3ULoader

START_DISPATCH;
CB( IFC_PLAYLISTLOADER_LOAD, Load )
#if 0
VCB( IFC_PLAYLISTLOADER_CLOSE, Close )
CB( IFC_PLAYLISTLOADER_GETITEM, GetItem )
CB( IFC_PLAYLISTLOADER_GETITEMTITLE, GetItemTitle )
CB( IFC_PLAYLISTLOADER_GETITEMLENGTHMILLISECONDS, GetItemLengthMilliseconds )
CB( IFC_PLAYLISTLOADER_GETITEMEXTENDEDINFO, GetItemExtendedInfo )
CB( IFC_PLAYLISTLOADER_NEXTITEM, NextItem )
#endif
END_DISPATCH;