void QMplayer::newConnection() { QTcpSocket *con = tcpServer->nextPendingConnection(); connect(con, SIGNAL(disconnected()), con, SLOT(deleteLater())); QByteArray req; while(con->waitForReadyRead(100)) { req += con->readAll(); } QString res; QTextStream buf(&req); buf.setCodec(QTextCodec::codecForName("utf8")); QString line; QString reqPath = ""; // path from http request QString filename = ""; // file on the disk QString encPath = ""; // transcoded file QString host = ""; QFile file; bool sendFile = false; int itemIndex = 0; for(;;) { line = buf.readLine(); if(line.isNull()) { break; } if(line.startsWith("GET ")) { int index = req.indexOf(' ', 4); if(index > 0) { reqPath = req.mid(4, index - 4); for(int i = 0; i < reqPath.count(); i++) { if(reqPath.at(i) == '%') { i += 2; if(i >= reqPath.count()) { break; } QChar ch = (QChar)(reqPath.mid(i - 1, 2).toInt(0, 16)); reqPath.replace(i - 2, 3, ch); } } } } else if(line.startsWith("Host: ")) { host = line.right(line.length() - 6).trimmed(); } } res.append("HTTP/1.0 200 OK\r\n"); res.append("Content-Type: text/html; charset=utf-8\r\n\r\n"); res.append("<html>"); res.append("<body>"); if(reqPath.length() == 0) { res.append("Valid GET request not found"); } else if(host.length() == 0) { res.append("Host not found in html header"); } else if(reqPath == "/") { QString dir = ""; for(int i = NUM_MENU_ITEMS; i < lw->count(); i++) { QListWidgetItem *item = lw->item(i); QString path = item->text(); bool isDir = isDirectory(path); if(isDir) { res.append(path); res.append("</br>"); dir = pathToUrl(path); } else { res.append("\r\n<a href=\"http://"); res.append(host); res.append(dir); res.append(pathToUrl(path)); res.append("\">"); res.append(path); res.append("</a></br>\r\n"); } } } else { res.append(reqPath); res.append("</br>"); QString dir = ""; QString testDir; for(int i = NUM_MENU_ITEMS; i < lw->count(); i++) { QListWidgetItem *item = lw->item(i); QString path = item->text(); QString testPath = pathToUrl(path); bool isDir = isDirectory(path); if(isDir) { dir = path; testDir = testPath; } else if(reqPath == (testDir + testPath)) { filename = dir; if(!filename.endsWith('/') && !filename.endsWith('\\')) { filename.append('/'); } filename.append(path); itemIndex = i; break; } } if(filename.length() == 0) { res.append("file not found"); } else { encPath = getEncFilename(filename, ".qmplayer"); QFileInfo fi(encPath); if(fi.exists()) { QString size; size.setNum(fi.size()); res.append("file size: "); res.append(size); if(processRunning(process)) { res.append("</br>encoding in progress</br>Reload page to check status or download"); } else { QString mime = "text/plain"; if(reqPath.endsWith(".avi")) { mime ="video/avi"; } else if(reqPath.endsWith(".mp4")) { mime = "video/mp4"; } else if(reqPath.endsWith(".ogv")) { mime = "video/theora"; } else if(reqPath.endsWith(".ogg")) { mime = "audio/vorbis"; } else if(reqPath.endsWith(".mp3")) { mime = "audio/mpeg"; } else if(reqPath.endsWith(".flac")) { mime = "audio/flac"; } res.clear(); res.append("HTTP/1.0 200 OK\r\n"); res.append("Content-Type: "); res.append(mime); res.append("\r\nContent-Length: "); res.append(size); res.append("\r\n\r\n"); sendFile = true; } } else if(processRunning(process)) { res.append("Another encoding is in progress"); } else { if(startMencoder(filename, encPath)) { // FIXME: whatever we send to back, browser wont show anything res.append("Encoding started</br>Reload page to check status or download"); } else { res.append("Failed to start mencoder:</br>"); res.append(process->errorString()); } } } } if(!sendFile) { res.append("</body>"); res.append("</html>"); } con->write(res.toUtf8()); if(sendFile) { QFile f(encPath); char buf[4096]; f.open(QFile::ReadOnly); int count; while((count = f.read(buf, 4096)) > 0) { con->write(buf, count); con->flush(); con->waitForBytesWritten(-1); } } con->close(); con->disconnectFromHost(); }
void QMplayer::okClicked() { if(screen == QMplayer::ScreenInit || screen == QMplayer::ScreenEncoding) { QString dir = ""; bool hit = false; QStringList list; QStringList downList; QStringList nameList; QMessageBox::StandardButton answer = QMessageBox::NoButton; QMessageBox::StandardButton moreAnswer = QMessageBox::NoButton; list << mpargs; list << mplist; if (mpgui) { QListWidgetItem *sel = lw->currentItem(); if(sel == NULL) { return; } if(sel == scanItem) { scan(); return; } if(sel == sharingItem) { sharing(); return; } if(sel == encodingItem) { if(screen != ScreenEncoding) { showScreen(ScreenEncoding); if(lw->count() > NUM_MENU_ITEMS) { QListWidgetItem *item = lw->item(NUM_MENU_ITEMS); item->setSelected(true); } } else { showScreen(ScreenInit); } return; } for(int i = NUM_MENU_ITEMS; i < lw->count(); i++) { QListWidgetItem *item = lw->item(i); QString path = item->text(); bool isDir = isDirectory(path); if(isDir) { if(hit) { break; } dir = path; } hit |= (item == sel); if(!hit || isDir) { continue; } if(!dir.startsWith("http://")) { list.append(dir + "/" + path); // add local files to playlist continue; } if(moreAnswer == QMessageBox::NoButton) { answer = QMessageBox::question(this, "qmplayer", path, QMessageBox::Save | QMessageBox::Open | QMessageBox::Cancel); } if(answer == QMessageBox::Cancel) { break; } if(answer == QMessageBox::Open) { list.append(dir + pathToUrl(path)); // append to playlist if we just play (no download) } downList.append(dir + pathToUrl(path)); nameList.append(path); if(moreAnswer != QMessageBox::YesToAll) { moreAnswer = QMessageBox::question(this, "qmplayer", tr("Next file?"), QMessageBox::Yes | QMessageBox::No | QMessageBox::YesToAll); if(moreAnswer == QMessageBox::No) { break; } } } for(int i = 0; i < downList.count(); i++) { QString url = downList[i]; int slashIndex = url.lastIndexOf('/'); if(slashIndex < 0) { continue; } QString filename = nameList[i]; QString destPath = QDir::homePath() + "/" + filename; if(QDir("/media/card").exists()) { destPath = "/media/card/" + filename; } bool justCheck = list.contains(url); if(!download(url, destPath, filename, justCheck)) { return; } if(justCheck) { continue; } // Add downloaded file to list list.append(destPath); } } if(list.count() > mpargs.count()) { if(screen == ScreenEncoding) { encode(list.at(0)); } else { if (isPlaylist(list[mpargs.count()])) list.insert(mpargs.count(),"-playlist"); play(list); } } } else if(screen == QMplayer::ScreenPlay) { if(processRunning(process)) { process->write(" "); } showScreen(QMplayer::ScreenStopped); } else if(screen == QMplayer::ScreenStopped) { if(processRunning(process)) { process->write(" "); #ifdef QTOPIA // Workaround unpause not working for alsa out in mplayer glamo. // We send left key to make mplayer start playing. process->write("\x1b""[D"); #endif } showScreen(QMplayer::ScreenPlay); } else if(screen == QMplayer::ScreenConnect) { runClient(); } else if(screen == QMplayer::ScreenTube) { this->screen = QMplayer::ScreenInit; okClicked(); } }
void QMplayer::newConnection() { QTcpSocket *con = tcpServer->nextPendingConnection(); connect(con, SIGNAL(disconnected()), con, SLOT(deleteLater())); QByteArray req; while(con->waitForReadyRead(100)) { req += con->readAll(); } QString res; QTextStream buf(&req); buf.setCodec(QTextCodec::codecForName("utf8")); QString line; QString reqPath = ""; // path from http request QString filename = ""; // file on the disk QString encPath = ""; // transcoded file QString host = ""; QFile file; bool sendFile = false; int itemIndex = 0; for(;;) { line = buf.readLine(); if(line.isNull()) { break; } if(line.startsWith("GET ")) { int index = req.indexOf(' ', 4); if(index > 0) { reqPath = req.mid(4, index - 4); for(int i = 0; i < reqPath.count(); i++) { if(reqPath.at(i) == '%') { i += 2; if(i >= reqPath.count()) { break; } QChar ch = (QChar)(reqPath.mid(i - 1, 2).toInt(0, 16)); reqPath.replace(i - 2, 3, ch); } } } } else if(line.startsWith("Host: ")) { host = line.right(line.length() - 6).trimmed(); } } res.append("HTTP/1.0 200 OK\r\n"); res.append("Content-Type: text/html; charset=utf-8\r\n\r\n"); res.append("<html>"); res.append("<body>"); if(reqPath.length() == 0) { res.append("Valid GET request not found"); } else if(host.length() == 0) { res.append("Host not found in html header"); } else if(reqPath == "/") { QString dir = ""; for(int i = 2; i < lw->count(); i++) { QListWidgetItem *item = lw->item(i); QString path = item->text(); bool isDir = isDirectory(path); if(isDir) { res.append(path); res.append("</br>"); dir = pathToUrl(path); } else { res.append("\r\n<a href=\"http://"); res.append(host); res.append(dir); res.append(pathToUrl(path)); res.append("\">"); res.append(path); res.append("</a></br>\r\n"); } } } else { res.append(reqPath); res.append("</br>"); QString dir = ""; QString testDir; for(int i = 2; i < lw->count(); i++) { QListWidgetItem *item = lw->item(i); QString path = item->text(); QString testPath = pathToUrl(path); bool isDir = isDirectory(path); if(isDir) { dir = path; testDir = testPath; } else if(reqPath == (testDir + testPath)) { filename = dir; if(!filename.endsWith('/') && !filename.endsWith('\\')) { filename.append('/'); } filename.append(path); itemIndex = i; break; } } if(filename.length() == 0) { res.append("file not found"); } else { encPath = filename; // Encode just avi files for now if(encPath.endsWith(".avi") || encPath.endsWith(".mp4")) { encPath.insert(encPath.lastIndexOf('.'), ".qmplayer"); } QFileInfo fi(encPath); if(fi.exists()) { QString size; size.setNum(fi.size()); res.append("file size: "); res.append(size); if(processRunning(process)) { res.append("</br>encoding in progress</br>Reload page to check status or download"); } else { QString mime = "text/plain"; if(reqPath.endsWith(".avi")) { mime ="video/avi"; } else if(reqPath.endsWith(".mp4")) { mime = "video/mp4"; } else if(reqPath.endsWith(".ogv")) { mime = "video/theora"; } else if(reqPath.endsWith(".ogg")) { mime = "audio/vorbis"; } else if(reqPath.endsWith(".mp3")) { mime = "audio/mpeg"; } res.clear(); res.append("HTTP/1.0 200 OK\r\n"); res.append("Content-Type: "); res.append(mime); res.append("\r\nContent-Length: "); res.append(size); res.append("\r\n\r\n"); sendFile = true; } } else if(processRunning(process)) { res.append("Another encoding is in progress"); } else { QStringList args; args.append(filename); args.append("-ovc"); args.append("lavc"); args.append("-lavcopts"); args.append("vcodec=mpeg4:vhq:vbitrate=300"); args.append("-vf"); args.append("scale=320:240,eq2=1.2:0.5:-0.25,rotate=2"); args.append("-oac"); args.append("mp3lame"); args.append("-ofps"); args.append("15"); args.append("-lameopts"); args.append("br=64:cbr"); args.append("-o"); args.append(encPath); process = new QProcess(this); process->setProcessChannelMode(QProcess::ForwardedChannels); process->start("mencoder", args); #ifdef Q_WS_WIN PROCESS_INFORMATION *pi = (PROCESS_INFORMATION *) process->pid(); SetPriorityClass(pi->hProcess, IDLE_PRIORITY_CLASS); #endif // FIXME: whatever we send to back, browser wont show anything if(process->waitForStarted(10000)) { res.append("Encoding started</br>Reload page to check status or download"); } else { res.append("Failed to start mencoder:</br>"); res.append(process->errorString()); delete(process); process = NULL; } } } } if(!sendFile) { res.append("</body>"); res.append("</html>"); } con->write(res.toUtf8()); if(sendFile) { QFile f(encPath); char buf[4096]; f.open(QFile::ReadOnly); int count; while((count = f.read(buf, 4096)) > 0) { con->write(buf, count); con->flush(); con->waitForBytesWritten(-1); } } con->close(); con->disconnectFromHost(); }