2 * Copyright (C) 2005-2013 Team XBMC
5 * This Program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2, or (at your option)
10 * This Program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with XBMC; see the file COPYING. If not, see
17 * <http://www.gnu.org/licenses/>.
22 // FileShoutcast.cpp: implementation of the CShoutcastFile class.
24 //////////////////////////////////////////////////////////////////////
26 #include "threads/SystemClock.h"
28 #include "ShoutcastFile.h"
29 #include "guilib/GUIWindowManager.h"
31 #include "utils/RegExp.h"
32 #include "utils/HTMLUtil.h"
33 #include "utils/CharsetConverter.h"
34 #include "utils/TimeUtils.h"
35 #include "ApplicationMessenger.h"
36 #include "utils/log.h"
37 #include "FileCache.h"
40 using namespace XFILE;
41 using namespace MUSIC_INFO;
43 CShoutcastFile::CShoutcastFile() :
44 IFile(), CThread("ShoutcastFile")
53 CShoutcastFile::~CShoutcastFile()
59 int64_t CShoutcastFile::GetPosition()
61 return m_file.GetPosition()-m_discarded;
64 int64_t CShoutcastFile::GetLength()
69 bool CShoutcastFile::Open(const CURL& url)
72 url2.SetProtocolOptions("noshout=true&Icy-MetaData=1");
73 url2.SetProtocol("http");
76 if ((result=m_file.Open(url2.Get())))
78 m_tag.SetTitle(m_file.GetHttpHeader().GetValue("icy-name"));
79 if (m_tag.GetTitle().IsEmpty())
80 m_tag.SetTitle(m_file.GetHttpHeader().GetValue("ice-name")); // icecast
81 m_tag.SetGenre(m_file.GetHttpHeader().GetValue("icy-genre"));
82 if (m_tag.GetGenre().empty())
83 m_tag.SetGenre(m_file.GetHttpHeader().GetValue("ice-genre")); // icecast
84 m_tag.SetLoaded(true);
86 m_metaint = atoi(m_file.GetHttpHeader().GetValue("icy-metaint").c_str());
89 m_buffer = new char[16*255];
97 unsigned int CShoutcastFile::Read(void* lpBuf, int64_t uiBufSize)
99 if (m_currint >= m_metaint && m_metaint > 0)
101 unsigned char header;
102 m_file.Read(&header,1);
103 ReadTruncated(m_buffer, header*16);
104 if (ExtractTagInfo(m_buffer)
105 // this is here to workaround issues caused by application posting callbacks to itself (3cf882d9)
106 // the callback will set an empty tag in the info manager item, while we think we have ours set
107 || (m_file.GetPosition() < 10*m_metaint && !m_tagPos))
109 m_tagPos = m_file.GetPosition();
112 m_discarded += header*16+1;
118 toRead = std::min((unsigned int)uiBufSize,(unsigned int)m_metaint-m_currint);
120 toRead = std::min((unsigned int)uiBufSize,(unsigned int)16*255);
121 toRead = m_file.Read(lpBuf,toRead);
126 int64_t CShoutcastFile::Seek(int64_t iFilePosition, int iWhence)
131 void CShoutcastFile::Close()
139 bool CShoutcastFile::ExtractTagInfo(const char* buf)
141 CStdString strBuffer = buf;
142 g_charsetConverter.unknownToUTF8(strBuffer);
146 CStdStringW wBuffer, wConverted;
147 g_charsetConverter.utf8ToW(strBuffer, wBuffer, false);
148 HTML::CHTMLUtil::ConvertHTMLToW(wBuffer, wConverted);
149 g_charsetConverter.wToUTF8(wConverted, strBuffer);
151 CRegExp reTitle(true);
152 reTitle.RegComp("StreamTitle=\'(.*?)\';");
154 if (reTitle.RegFind(strBuffer.c_str()) != -1)
156 std::string newtitle = reTitle.GetReplaceString("\\1");
157 result = (m_tag.GetTitle() != newtitle);
158 m_tag.SetTitle(newtitle);
164 void CShoutcastFile::ReadTruncated(char* buf2, int size)
169 int read = m_file.Read(buf,size);
175 int CShoutcastFile::IoControl(EIoControl control, void* payload)
177 if (control == IOCTRL_SET_CACHE)
178 m_cacheReader = (CFileCache*)payload;
180 return IFile::IoControl(control, payload);
183 void CShoutcastFile::Process()
190 if (m_tagChange.WaitMSec(500))
192 while (!m_bStop && m_cacheReader->GetPosition() < m_tagPos)
194 CApplicationMessenger::Get().SetCurrentSongTag(m_tag);