[4d1e3f]: xbmc / FileSystem / FileShoutcast.cpp Maximize Restore History

Repo status: analyzing...

Download this file

FileShoutcast.cpp    453 lines (398 with data), 11.6 kB

  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
/*
* Copyright (C) 2005-2008 Team XBMC
* http://www.xbmc.org
*
* This Program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2, or (at your option)
* any later version.
*
* This Program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with XBMC; see the file COPYING. If not, write to
* the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
* http://www.gnu.org/copyleft/gpl.html
*
*/
// FileShoutcast.cpp: implementation of the CFileShoutcast class.
//
//////////////////////////////////////////////////////////////////////
#include "system.h"
#include "AdvancedSettings.h"
#include "Application.h"
#include "FileShoutcast.h"
#include "GUISettings.h"
#include "GUIDialogProgress.h"
#include "GUIWindowManager.h"
#include "URL.h"
#include "utils/TimeUtils.h"
#include "LocalizeStrings.h"
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
// prevent inclusion of config.h from libshout
#define __SRCONFIG_H__
#include "lib/libshout/rip_manager.h"
#include "lib/libshout/filelib.h"
#undef __SRCONFIG_H__
#include "RingBuffer.h"
#include "ShoutcastRipFile.h"
#include "utils/GUIInfoManager.h"
#include "utils/log.h"
//using namespace std; On VS2010 error_code conflicts with std::error_code
using namespace XFILE;
using namespace MUSIC_INFO;
#ifndef HAS_SHOUTCAST
extern "C"
{
error_code rip_manager_start(void (*status_callback)(int message, void *data), RIP_MANAGER_OPTIONS *options) { return 0; }
void rip_manager_stop() { }
void set_rip_manager_options_defaults(RIP_MANAGER_OPTIONS*) {}
int rip_manager_get_content_type() { return 0; }
}
#endif
const int SHOUTCASTTIMEOUT = 60;
static CRingBuffer m_ringbuf;
static FileState m_fileState;
static CShoutcastRipFile m_ripFile;
static RIP_MANAGER_INFO m_ripInfo;
static ERROR_INFO m_errorInfo;
void rip_callback(int message, void *data)
{
switch (message)
{
RIP_MANAGER_INFO *info;
case RM_UPDATE:
info = (RIP_MANAGER_INFO*)data;
memcpy(&m_ripInfo, info, sizeof(m_ripInfo));
if (info->status == RM_STATUS_BUFFERING)
{
m_fileState.bBuffering = true;
}
else if ( info->status == RM_STATUS_RIPPING)
{
m_ripFile.SetRipManagerInfo( &m_ripInfo );
m_fileState.bBuffering = false;
}
else if (info->status == RM_STATUS_RECONNECTING)
{ }
break;
case RM_ERROR:
ERROR_INFO *errInfo;
errInfo = (ERROR_INFO*)data;
memcpy(&m_errorInfo, errInfo, sizeof(m_errorInfo));
m_fileState.bRipError = true;
OutputDebugString("error\n");
break;
case RM_DONE:
OutputDebugString("done\n");
m_fileState.bRipDone = true;
break;
case RM_NEW_TRACK:
char *trackName;
trackName = (char*) data;
m_ripFile.SetTrackname( trackName );
break;
case RM_STARTED:
m_fileState.bRipStarted = true;
OutputDebugString("Started\n");
break;
}
}
extern "C" {
error_code filelib_write_show(char *buf, u_long size)
{
if ((unsigned int)size > m_ringbuf.getSize())
{
CLog::Log(LOGERROR, "Shoutcast chunk too big: %lu", size);
return SR_ERROR_BUFFER_FULL;
}
while (m_ringbuf.getMaxWriteSize() < (unsigned int)size) Sleep(10);
m_ringbuf.WriteData(buf, size);
m_ripFile.Write( buf, size ); //will only write, if it has to
if (m_fileState.bBuffering)
{
if (rip_manager_get_content_type() == CONTENT_TYPE_OGG)
{
if (m_ringbuf.getMaxReadSize() > (m_ringbuf.getSize() / 8) )
{
// hack because ogg streams are very broke, force it to go.
m_fileState.bBuffering = false;
}
}
}
return SR_SUCCESS;
}
}
CFileShoutcast* m_pShoutCastRipper = NULL;
CFileShoutcast::CFileShoutcast()
{
// FIXME: without this check
// the playback stops when CFile::Stat()
// or CFile::Exists() is called
// Do we already have another file
// using the ripper?
if (!m_pShoutCastRipper)
{
m_fileState.bBuffering = true;
m_fileState.bRipDone = false;
m_fileState.bRipStarted = false;
m_fileState.bRipError = false;
m_ringbuf.Create(1024*1024); // must be big enough. some stations use 192kbps.
m_pShoutCastRipper = this;
}
}
CFileShoutcast::~CFileShoutcast()
{
// FIXME: without this check
// the playback stops when CFile::Stat()
// or CFile::Exists() is called
// Has this object initialized the ripper?
if (m_pShoutCastRipper==this)
{
rip_manager_stop();
m_pShoutCastRipper = NULL;
m_ripFile.Reset();
m_ringbuf.Destroy();
}
}
bool CFileShoutcast::CanRecord()
{
if ( !m_fileState.bRipStarted )
return false;
return m_ripFile.CanRecord();
}
bool CFileShoutcast::Record()
{
return m_ripFile.Record();
}
void CFileShoutcast::StopRecording()
{
m_ripFile.StopRecording();
}
int64_t CFileShoutcast::GetPosition()
{
return 0;
}
int64_t CFileShoutcast::GetLength()
{
return 0;
}
bool CFileShoutcast::Open(const CURL& url)
{
m_lastTime = CTimeUtils::GetTimeMS();
CGUIDialogProgress* dlgProgress = NULL;
// dvdplayer can deadlock with progress dialog so check first
if (g_application.GetCurrentPlayer() == EPC_PAPLAYER)
{
dlgProgress = (CGUIDialogProgress*)g_windowManager.GetWindow(WINDOW_DIALOG_PROGRESS);
}
set_rip_manager_options_defaults(&m_opt);
strcpy(m_opt.output_directory, "./");
m_opt.proxyurl[0] = '\0';
// Use a proxy, if the GUI was configured as such
bool bProxyEnabled = g_guiSettings.GetBool("network.usehttpproxy");
if (bProxyEnabled)
{
const CStdString &strProxyServer = g_guiSettings.GetString("network.httpproxyserver");
const CStdString &strProxyPort = g_guiSettings.GetString("network.httpproxyport");
// Should we check for valid strings here
#ifndef _LINUX
_snprintf( m_opt.proxyurl, MAX_URL_LEN, "http://%s:%s", strProxyServer.c_str(), strProxyPort.c_str() );
#else
snprintf( m_opt.proxyurl, MAX_URL_LEN, "http://%s:%s", strProxyServer.c_str(), strProxyPort.c_str() );
#endif
}
CStdString strUrl = url.Get();
strUrl.Replace("shout://", "http://");
strncpy(m_opt.url, strUrl.c_str(), MAX_URL_LEN);
sprintf(m_opt.useragent, "x%s", url.GetFileName().c_str());
if (dlgProgress)
{
dlgProgress->SetHeading(260);
dlgProgress->SetLine(0, 259);
dlgProgress->SetLine(1, strUrl);
dlgProgress->SetLine(2, "");
if (!dlgProgress->IsDialogRunning())
dlgProgress->StartModal();
dlgProgress->Progress();
}
if (rip_manager_start(rip_callback, &m_opt) != SR_SUCCESS)
{
if (dlgProgress) dlgProgress->Close();
return false;
}
int iShoutcastTimeout = 10 * SHOUTCASTTIMEOUT; //i.e: 10 * 10 = 100 * 100ms = 10s
int iCount = 0;
while (!m_fileState.bRipDone && !m_fileState.bRipStarted && !m_fileState.bRipError && (!dlgProgress || !dlgProgress->IsCanceled()))
{
if (iCount <= iShoutcastTimeout) //Normally, this isn't the problem,
//because if RIP_MANAGER fails, this would be here
//with m_fileState.bRipError
{
Sleep(100);
}
else
{
if (dlgProgress)
{
dlgProgress->SetLine(1, 257);
dlgProgress->SetLine(2, "Connection timed out...");
Sleep(1500);
dlgProgress->Close();
}
return false;
}
iCount++;
}
if (dlgProgress && dlgProgress->IsCanceled())
{
Close();
dlgProgress->Close();
return false;
}
/* store content type of stream */
m_mimetype = rip_manager_get_content_type();
//CHANGED CODE: Don't reset timer anymore.
while (!m_fileState.bRipDone && !m_fileState.bRipError && m_fileState.bBuffering && (!dlgProgress || !dlgProgress->IsCanceled()))
{
if (iCount <= iShoutcastTimeout) //Here is the real problem: Sometimes the buffer fills just to
//slowly, thus the quality of the stream will be bad, and should be
//aborted...
{
Sleep(100);
char szTmp[1024];
//g_dialog.SetCaption(0, "Shoutcast" );
sprintf(szTmp, g_localizeStrings.Get(23052).c_str(), m_ringbuf.getMaxReadSize());
if (dlgProgress)
{
dlgProgress->SetLine(2, szTmp );
dlgProgress->Progress();
}
sprintf(szTmp, "%s", m_ripInfo.filename);
for (int i = 0; i < (int)strlen(szTmp); i++)
szTmp[i] = tolower((unsigned char)szTmp[i]);
szTmp[50] = 0;
if (dlgProgress)
{
dlgProgress->SetLine(1, szTmp );
dlgProgress->Progress();
}
}
else //it's not really a connection timeout, but it's here,
//where things get boring, if connection is slow.
//trust me, i did a lot of testing... Doesn't happen often,
//but if it does it sucks to wait here forever.
//CHANGED: Other message here
{
if (dlgProgress)
{
dlgProgress->SetLine(1, 257);
dlgProgress->SetLine(2, "Connection to server too slow...");
dlgProgress->Close();
}
return false;
}
iCount++;
}
if (dlgProgress && dlgProgress->IsCanceled())
{
Close();
dlgProgress->Close();
return false;
}
if ( m_fileState.bRipError )
{
if (dlgProgress)
{
dlgProgress->SetLine(1, 257);
dlgProgress->SetLine(2, 16029);
CLog::Log(LOGERROR, "%s: error - %s", __FUNCTION__, m_errorInfo.error_str);
dlgProgress->Progress();
Sleep(1500);
dlgProgress->Close();
}
return false;
}
if (dlgProgress)
{
dlgProgress->SetLine(2, 261);
dlgProgress->Progress();
dlgProgress->Close();
}
return true;
}
unsigned int CFileShoutcast::Read(void* lpBuf, int64_t uiBufSize)
{
if (m_fileState.bRipDone)
{
OutputDebugString("Read done\n");
return 0;
}
int slept=0;
while (m_ringbuf.getMaxReadSize() <= 0)
{
Sleep(10);
if (slept += 10 > SHOUTCASTTIMEOUT*1000)
return -1;
}
int iRead = m_ringbuf.getMaxReadSize();
if (iRead > uiBufSize) iRead = (int)uiBufSize;
m_ringbuf.ReadData((char*)lpBuf, iRead);
if (CTimeUtils::GetTimeMS() - m_lastTime > 500)
{
m_lastTime = CTimeUtils::GetTimeMS();
CMusicInfoTag tag;
GetMusicInfoTag(tag);
g_infoManager.SetCurrentSongTag(tag);
}
return iRead;
}
void CFileShoutcast::outputTimeoutMessage(const char* message)
{
//g_dialog.SetCaption(0, "Shoutcast" );
//g_dialog.SetMessage(0, message );
//g_dialog.Render();
Sleep(1500);
}
int64_t CFileShoutcast::Seek(int64_t iFilePosition, int iWhence)
{
return -1;
}
void CFileShoutcast::Close()
{
OutputDebugString("Shoutcast Stopping\n");
if ( m_ripFile.IsRecording() )
m_ripFile.StopRecording();
m_ringbuf.Clear();
rip_manager_stop();
m_ripFile.Reset();
OutputDebugString("Shoutcast Stopped\n");
}
bool CFileShoutcast::IsRecording()
{
return m_ripFile.IsRecording();
}
bool CFileShoutcast::GetMusicInfoTag(CMusicInfoTag& tag)
{
m_ripFile.GetMusicInfoTag(tag);
return true;
}
CStdString CFileShoutcast::GetContent()
{
switch (m_mimetype)
{
case CONTENT_TYPE_MP3:
return "audio/mpeg";
case CONTENT_TYPE_OGG:
return "audio/ogg";
case CONTENT_TYPE_AAC:
return "audio/aac";
default:
return "application/octet-stream";
}
}