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/>.
23 #include "VTPSession.h"
24 #include "utils/URIUtils.h"
26 #include "utils/log.h"
27 #include "utils/StringUtils.h"
28 #include "video/VideoInfoTag.h"
30 using namespace XFILE;
34 : m_socket(INVALID_SOCKET)
37 m_session = new CVTPSession();
45 void CVTPFile::Close()
47 if(m_socket != INVALID_SOCKET)
49 m_session->AbortStreamLive();
50 closesocket(m_socket);
54 m_socket = INVALID_SOCKET;
57 bool CVTPFile::Open(const CURL& url2)
61 if(url.GetHostName() == "")
62 url.SetHostName("localhost");
64 if(url.GetPort() == 0)
67 CStdString path(url.GetFileName());
69 if (StringUtils::StartsWith(path, "channels/"))
72 CStdString channel = path.substr(9);
73 if(!URIUtils::HasExtension(channel, ".ts"))
75 CLog::Log(LOGERROR, "%s - invalid channel url %s", __FUNCTION__, channel.c_str());
78 URIUtils::RemoveExtension(channel);
80 if(!m_session->Open(url.GetHostName(), url.GetPort()))
83 m_channel = atoi(channel.c_str());
84 m_socket = m_session->GetStreamLive(m_channel);
88 CLog::Log(LOGERROR, "%s - invalid path specified %s", __FUNCTION__, path.c_str());
95 unsigned int CVTPFile::Read(void* buffer, int64_t size)
97 if(m_socket == INVALID_SOCKET)
109 FD_SET(m_socket, &set_r);
110 FD_SET(m_socket, &set_e);
111 res = select(FD_SETSIZE, &set_r, NULL, &set_e, &tv);
114 CLog::Log(LOGERROR, "CVTPFile::Read - select failed");
119 CLog::Log(LOGERROR, "CVTPFile::Read - timeout waiting for data");
123 res = recv(m_socket, (char*)buffer, (size_t)size, 0);
126 CLog::Log(LOGERROR, "CVTPFile::Read - failed");
131 CLog::Log(LOGERROR, "CVTPFile::Read - eof");
138 int64_t CVTPFile::Seek(int64_t pos, int whence)
140 CLog::Log(LOGDEBUG, "CVTPFile::Seek - seek to pos %"PRId64", whence %d", pos, whence);
144 bool CVTPFile::NextChannel(bool preview/* = false*/)
146 if(m_session == NULL)
149 int channel = m_channel;
150 while(++channel < 1000)
152 if(!m_session->CanStreamLive(channel))
155 if(m_socket != INVALID_SOCKET)
157 shutdown(m_socket, SHUT_RDWR);
158 m_session->AbortStreamLive();
159 closesocket(m_socket);
163 m_socket = m_session->GetStreamLive(m_channel);
164 if(m_socket != INVALID_SOCKET)
170 bool CVTPFile::PrevChannel(bool preview/* = false*/)
172 if(m_session == NULL)
175 int channel = m_channel;
178 if(!m_session->CanStreamLive(channel))
181 m_session->AbortStreamLive();
183 if(m_socket != INVALID_SOCKET)
185 shutdown(m_socket, SHUT_RDWR);
186 m_session->AbortStreamLive();
187 closesocket(m_socket);
191 m_socket = m_session->GetStreamLive(m_channel);
192 if(m_socket != INVALID_SOCKET)
198 bool CVTPFile::SelectChannel(unsigned int channel)
200 if(!m_session->CanStreamLive(channel))
203 m_session->AbortStreamLive();
205 if(m_socket != INVALID_SOCKET)
207 shutdown(m_socket, SHUT_RDWR);
208 m_session->AbortStreamLive();
209 closesocket(m_socket);
213 m_socket = m_session->GetStreamLive(m_channel);
214 if(m_socket != INVALID_SOCKET)
220 int CVTPFile::IoControl(EIoControl request, void* param)
222 if(request == IOCTRL_SEEK_POSSIBLE)