[UI] Added some test programs

This commit is contained in:
emeric
2015-12-08 20:44:16 +01:00
parent 108d95b20f
commit 6db3606a4b
9 changed files with 700 additions and 19 deletions
+1 -1
View File
@@ -40,6 +40,7 @@ lms_SOURCES = \
$(srcdir)/ui/common/LineEdit.cpp \
$(srcdir)/ui/resource/AvConvTranscodeStreamResource.cpp \
$(srcdir)/ui/resource/CoverResource.cpp \
$(srcdir)/ui/resource/TranscodeResource.cpp \
$(srcdir)/ui/settings/Settings.cpp \
$(srcdir)/ui/settings/SettingsAccountFormView.cpp \
$(srcdir)/ui/settings/SettingsAudioFormView.cpp \
@@ -60,5 +61,4 @@ lms_SOURCES += \
endif
lms_CXXFLAGS=-std=c++11 -Wall -I$(top_srcdir)/third-party -I$(srcdir)/ui $(MAGICKXX_CFLAGS)
lms_LDADD=$(MAGICKXX_LIBS)
+65 -14
View File
@@ -29,22 +29,55 @@ namespace Av {
#define LMS_LOG_TRANSCODE(sev) LMS_LOG(TRANSCODE, INFO) << "[" << _id << "] - "
struct EncodingInfo
{
Encoding encoding;
std::string mimetype;
int id;
};
static std::vector<EncodingInfo> encodingInfos =
{
{Encoding::MP3, "audio/mp3", 0},
{Encoding::OGA, "audio/ogg", 1},
{Encoding::OGV, "video/ogg", 2},
{Encoding::WEBMA, "audio/webm", 3},
{Encoding::WEBMV, "video/webm", 4},
{Encoding::M4A, "audio/mp4", 5},
{Encoding::M4V, "video/mp4", 6},
};
std::string encoding_to_mimetype(Encoding encoding)
{
switch(encoding)
for (auto encodingInfo : encodingInfos)
{
case Encoding::MP3: return "audio/mp3";
case Encoding::OGA: return "audio/ogg";
case Encoding::OGV: return "video/ogg";
case Encoding::WEBMA: return "audio/webm";
case Encoding::WEBMV: return "video/webm";
case Encoding::FLA: return "audio/x-flv";
case Encoding::FLV: return "video/x-flv";
case Encoding::M4A: return "audio/mp4";
case Encoding::M4V: return "video/mp4";
if (encodingInfo.encoding == encoding)
return encodingInfo.mimetype;
}
return "";
throw std::logic_error("encoding_to_mimetype failed!");
}
int encoding_to_int(Encoding encoding)
{
for (auto encodingInfo : encodingInfos)
{
if (encodingInfo.encoding == encoding)
return encodingInfo.id;
}
throw std::logic_error("encoding_to_int failed!");
}
Encoding encoding_from_int(int encodingId)
{
for (auto encodingInfo : encodingInfos)
{
if (encodingInfo.id == encodingId)
return encodingInfo.encoding;
}
throw std::logic_error("encoding_from_int failed!");
}
// TODO, parametrize?
@@ -129,6 +162,7 @@ Transcoder::start()
// in order not to block the whole forked process
args.push_back("-loglevel");
args.push_back("quiet");
args.push_back("-nostdin");
// input Offset
if (_parameters.getOffset().total_seconds() > 0)
@@ -265,8 +299,8 @@ Transcoder::start()
_child = std::make_shared<redi::ipstream>();
const redi::pstreams::pmode mode = redi::pstreams::pstdout; // | redi::pstreams::pstderr;
_child->open(avConvPath.string(), args, mode);
// Caution: stdin must have been closed before
_child->open(avConvPath.string(), args);
if (!_child->is_open())
{
LMS_LOG_TRANSCODE(DEBUG) << "Exec failed!";
@@ -279,8 +313,8 @@ Transcoder::start()
return false;
}
LMS_LOG_TRANSCODE(DEBUG) << "Stream opened!";
}
LMS_LOG_TRANSCODE(DEBUG) << "Stream opened!";
return true;
}
@@ -291,14 +325,31 @@ Transcoder::process(std::vector<unsigned char>& output, std::size_t maxSize)
if (!_child || _isComplete)
return;
if (_child->out().fail())
{
LMS_LOG_TRANSCODE(DEBUG) << "Stdout FAILED 2";
}
if (_child->out().eof())
{
LMS_LOG_TRANSCODE(DEBUG) << "Stdout ENDED 2";
}
output.resize(maxSize);
LMS_LOG_TRANSCODE(DEBUG) << "Reading up to " << output.size() << " bytes";
//Read on the output stream
_child->out().read(reinterpret_cast<char*>(&output[0]), maxSize);
output.resize(_child->out().gcount());
LMS_LOG_TRANSCODE(DEBUG) << "Read " << output.size() << " bytes";
if (_child->out().fail())
{
LMS_LOG_TRANSCODE(DEBUG) << "Stdout FAILED";
}
if (_child->out().eof())
{
LMS_LOG_TRANSCODE(DEBUG) << "Stdout EOF!";
+2
View File
@@ -47,6 +47,8 @@ enum class Encoding
};
std::string encoding_to_mimetype(Encoding encoding);
int encoding_to_int(Encoding encoding);
Encoding encoding_from_int(int encoding);
class TranscodeParameters
{
+2
View File
@@ -41,6 +41,8 @@ int main(int argc, char* argv[])
try
{
// Make pstream work with ffmpeg
close(STDIN_FILENO);
Wt::WServer server(argv[0]);
server.setServerConfiguration (argc, argv);
+174
View File
@@ -0,0 +1,174 @@
/*
* Copyright (C) 2015 Emeric Poupon
*
* This file is part of LMS.
*
* LMS 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 3 of the License, or
* (at your option) any later version.
*
* LMS 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 LMS. If not, see <http://www.gnu.org/licenses/>.
*/
#include <Wt/Http/Response>
#include "logger/Logger.hpp"
#include "TranscodeResource.hpp"
namespace UserInterface {
TranscodeResource::TranscodeResource(Database::Handler& db, Wt::WObject *parent)
: Wt::WResource(parent),
_db(db)
{
LMS_LOG(UI, DEBUG) << "CONSTRUCTING RESOURCE";
}
TranscodeResource:: ~TranscodeResource()
{
LMS_LOG(UI, DEBUG) << "DESTRUCTING RESOURCE";
beingDeleted();
}
std::string
TranscodeResource::getUrl(Database::Track::id_type trackId, Av::Encoding encoding, std::size_t offset, std::vector<std::size_t> streamIds) const
{
std::string res = url()+ "&trackid=" + std::to_string(trackId) + "&encoding=" + std::to_string(Av::encoding_to_int(encoding));
if (!streamIds.empty())
{
for (std::size_t streamId : streamIds)
res += "&stream=" + std::to_string(streamId);
}
res += "&offset=" + std::to_string(offset);
return res;
}
void
TranscodeResource::handleRequest(const Wt::Http::Request& request,
Wt::Http::Response& response)
{
// Retrieve parameters
const std::string *trackIdStr = request.getParameter("trackid");
const std::string *offsetStr = request.getParameter("offset");
const std::string *encodingStr = request.getParameter("encoding");
std::vector<std::string> streams = request.getParameterValues ("stream");
LMS_LOG(UI, DEBUG) << "Handling new request...";
try
{
std::shared_ptr<Av::Transcoder> transcoder;
// First, see if this request is for a continuation
Wt::Http::ResponseContinuation *continuation = request.continuation();
if (continuation)
{
LMS_LOG(UI, DEBUG) << "Continuation!";
transcoder = boost::any_cast<std::shared_ptr<Av::Transcoder> >(continuation->data());
if (!transcoder)
{
LMS_LOG(UI, DEBUG) << "No transcoder set -> abort!";
return;
}
}
else
{
LMS_LOG(UI, DEBUG) << "No continuation yet";
if (!trackIdStr
|| !offsetStr
|| !encodingStr)
{
LMS_LOG(UI, ERROR) << "Missing transcode parameter";
return;
}
Database::Track::id_type trackId = std::stol(*trackIdStr);
// transactions are not thread safe
std::unique_lock<std::mutex> lock(_mutex);
Wt::Dbo::Transaction transaction(_db.getSession());
Database::User::pointer user = _db.getCurrentUser();
Database::Track::pointer track = Database::Track::getById(_db.getSession(), trackId);
if (!track/* || !user */)
{
LMS_LOG(UI, ERROR) << "Missing track or user";
return;
}
Av::TranscodeParameters parameters;
// TODO
parameters.setOffset(boost::posix_time::seconds(std::stol(*offsetStr)));
parameters.setEncoding(Av::encoding_from_int(std::stol(*encodingStr)));
parameters.setBitrate(Av::Stream::Type::Audio, 96000);
for (std::string strStream: streams)
{
LMS_LOG(UI, DEBUG) << "Added stream " << std::stol(strStream);
parameters.addStream(std::stol(strStream));
}
LMS_LOG(UI, DEBUG) << "Offset set to " << parameters.getOffset();
transcoder = std::make_shared<Av::Transcoder>(track->getPath(), parameters);
transaction.commit();
LMS_LOG(UI, DEBUG) << "Mime type set to '" << Av::encoding_to_mimetype(Av::Encoding::MP3);
response.setMimeType( Av::encoding_to_mimetype(Av::Encoding::MP3) );
if (!transcoder->start())
{
LMS_LOG(UI, ERROR) << "Cannot start transcoder";
return;
}
LMS_LOG(UI, DEBUG) << "Transcoder started";
}
LMS_LOG(UI, DEBUG) << "processing data?";
if (!transcoder->isComplete())
{
std::vector<unsigned char> data;
data.reserve(_bufferSize);
LMS_LOG(UI, DEBUG) << "Processing data from transcoder...";
transcoder->process(data, _bufferSize);
LMS_LOG(UI, DEBUG) << "Processing data from transcoder DONE";
// Give the client all the output data
response.out().write(reinterpret_cast<char*>(&data[0]), data.size());
LMS_LOG(UI, DEBUG) << "Written " << data.size() << " bytes! complete = " << std::boolalpha << transcoder->isComplete();
if (!response.out())
LMS_LOG(UI, ERROR) << "Write failed!";
}
if (!transcoder->isComplete() && response.out()) {
continuation = response.createContinuation();
continuation->setData(transcoder);
LMS_LOG(UI, DEBUG) << "Continuation set to " << continuation;
}
else
LMS_LOG(UI, DEBUG) << "No more data!";
}
catch (std::invalid_argument& e)
{
LMS_LOG(UI, ERROR) << "Invalid argument: " << e.what();
}
}
} // namespace UserInterface
+55
View File
@@ -0,0 +1,55 @@
/*
* Copyright (C) 2015 Emeric Poupon
*
* This file is part of LMS.
*
* LMS 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 3 of the License, or
* (at your option) any later version.
*
* LMS 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 LMS. If not, see <http://www.gnu.org/licenses/>.
*/
#pragma once
#include <mutex>
#include <Wt/WResource>
#include "av/AvTranscoder.hpp"
#include "database/DatabaseHandler.hpp"
namespace UserInterface {
class TranscodeResource : public Wt::WResource
{
public:
TranscodeResource(Database::Handler& db, Wt::WObject *parent);
~TranscodeResource();
std::string getUrl(Database::Track::id_type trackId, Av::Encoding encoding = Av::Encoding::OGA, size_t offset_secs = 0, std::vector<size_t> streamIds = {}) const;
void handleRequest(const Wt::Http::Request& request,
Wt::Http::Response& response);
private:
std::mutex _mutex;
Database::Handler& _db;
static const std::size_t _bufferSize = 65536;
};
} // namespace UserInterface