RESULT eServiceTS::unpause() { if (!m_streamthread->running()) { int is_streaming = !strncmp(m_filename.c_str(), "http://", 7); int srcfd = -1; if (is_streaming) srcfd = openHttpConnection(m_filename); else srcfd = ::open(m_filename.c_str(), O_RDONLY); if (srcfd < 0) { eDebug("Cannot open source stream: %s", m_filename.c_str()); return 1; } m_decodedemux->flush(); m_streamthread->start(srcfd, m_destfd); m_decoder->play(); } else eDebug("unpause but thread already running!"); return 0; }
void eHttpStream::thread() { hasStarted(); ret_code = openHttpConnection(); eDebug("eHttpStream::open: ret %d", ret_code); sock_mutex.unlock(); }
int eHttpStream::openHttpConnection() { int port; std::string hostname; std::string uri = url; std::string request; size_t buflen = 1024; char *linebuf = NULL; int result; char proto[100]; int statuscode = 0; char statusmsg[100]; bool redirected = false; close(); int pathindex = uri.find("/", 7); if (pathindex > 0) { hostname = uri.substr(7, pathindex - 7); uri = uri.substr(pathindex, uri.length() - pathindex); } else { hostname = uri.substr(7, uri.length() - 7); uri = ""; } int customportindex = hostname.find(":"); if (customportindex > 0) { port = atoi(hostname.substr(customportindex + 1, hostname.length() - customportindex - 1).c_str()); hostname = hostname.substr(0, customportindex); } else if (customportindex == 0) { port = atoi(hostname.substr(1, hostname.length() - 1).c_str()); hostname = "localhost"; } else { port = 80; } streamSocket = connect(hostname.c_str(), port, 10); if (streamSocket < 0) goto error; request = "GET "; request.append(uri).append(" HTTP/1.1\r\n"); request.append("Host: ").append(hostname).append("\r\n"); request.append("Accept: */*\r\n"); request.append("Connection: close\r\n"); request.append("\r\n"); writeAll(streamSocket, request.c_str(), request.length()); linebuf = (char*)malloc(buflen); result = readLine(streamSocket, &linebuf, &buflen); if (result <= 0) goto error; result = sscanf(linebuf, "%99s %d %99s", proto, &statuscode, statusmsg); if (result != 3 || (statuscode != 200 && statuscode != 302)) { eDebug("eHttpStream::open: wrong http response code: %d", statuscode); goto error; } while (result > 0) { result = readLine(streamSocket, &linebuf, &buflen); if (statuscode == 302 && sscanf(linebuf, "Location: %999s", this->url) == 1) { eDebug("eHttpStream::open: redirecting"); if (openHttpConnection() < 0) goto error; redirected = true; break; } } if (statuscode == 302 && !redirected) goto error; free(linebuf); return 0; error: eDebug("eHttpStream::open failed"); free(linebuf); close(); return -1; }