Commit 3bb0905d authored by danmei.chen's avatar danmei.chen

openh264: rename extractFile and store version of lib

videoCodec: rename codec after installation and update codec when there is new version
parent faa8ca00
...@@ -179,12 +179,14 @@ void AbstractCodecsModel::addCodec (shared_ptr<linphone::PayloadType> &codec) { ...@@ -179,12 +179,14 @@ void AbstractCodecsModel::addCodec (shared_ptr<linphone::PayloadType> &codec) {
void AbstractCodecsModel::addDownloadableCodec ( void AbstractCodecsModel::addDownloadableCodec (
const QString &mime, const QString &mime,
const QString &fileName,
const QString &downloadUrl, const QString &downloadUrl,
const QString &encoderDescription const QString &encoderDescription
) { ) {
QVariantMap map; QVariantMap map;
map["mime"] = mime; map["mime"] = mime;
map["fileName"] = fileName;
map["downloadUrl"] = downloadUrl; map["downloadUrl"] = downloadUrl;
map["encoderDescription"] = encoderDescription; map["encoderDescription"] = encoderDescription;
......
...@@ -67,7 +67,7 @@ protected: ...@@ -67,7 +67,7 @@ protected:
) override; ) override;
void addCodec (std::shared_ptr<linphone::PayloadType> &codec); void addCodec (std::shared_ptr<linphone::PayloadType> &codec);
void addDownloadableCodec (const QString &mime, const QString &downloadUrl, const QString &encoderDescription); void addDownloadableCodec (const QString &mime, const QString &fileName, const QString &downloadUrl, const QString &encoderDescription);
QString getCodecsFolder () const; QString getCodecsFolder () const;
......
...@@ -34,6 +34,7 @@ ...@@ -34,6 +34,7 @@
using namespace std; using namespace std;
namespace { namespace {
constexpr char cH264FileName[] = "openh264";
constexpr char cH264Description[] = "Provided by CISCO SYSTEM,INC"; constexpr char cH264Description[] = "Provided by CISCO SYSTEM,INC";
#ifdef Q_OS_LINUX #ifdef Q_OS_LINUX
...@@ -55,8 +56,57 @@ namespace { ...@@ -55,8 +56,57 @@ namespace {
VideoCodecsModel::VideoCodecsModel (QObject *parent) : AbstractCodecsModel(parent) { VideoCodecsModel::VideoCodecsModel (QObject *parent) : AbstractCodecsModel(parent) {
load(); load();
// update codec if there is a new version
#if defined(Q_OS_LINUX) || defined(Q_OS_WIN)
QString codecsFolder = Utils::coreStringToAppString(Paths::getCodecsDirPath());
QString filePath = QDir::cleanPath(codecsFolder) + QDir::separator() + cH264FileName + ".txt";
if(updateCodecVersion(filePath, cPluginUrlH264)) {
mFileDownloader = new FileDownloader();
mFileDownloader->setUrl(QUrl(cPluginUrlH264));
mFileDownloader->setDownloadFolder(codecsFolder);
mFileExtractor = new FileExtractor();
mFileExtractor->setExtractFolder(codecsFolder);
mFileDownloader->download();
QObject::connect(mFileDownloader, &FileDownloader::downloadFinished, this, &VideoCodecsModel::extract);
QObject::connect(mFileDownloader, &FileDownloader::downloadFailed, this, &VideoCodecsModel::installFileFailed);
QObject::connect(mFileExtractor, &FileExtractor::extractFinished, this, &VideoCodecsModel::endInstallFile);
QObject::connect(mFileExtractor, &FileExtractor::extractFailed, this, &VideoCodecsModel::installFileFailed);
}
#endif
}
VideoCodecsModel::~VideoCodecsModel () {
qInfo() << QStringLiteral("Delete VideoCodecsModel");
if (mFileDownloader) mFileDownloader = nullptr;
if (mFileExtractor) mFileExtractor = nullptr;
}
bool VideoCodecsModel::updateCodecVersion(const QString &filePath, const QString &newVersion) {
QFile versionFile(filePath);
if (!versionFile.exists()) return false;
if (!versionFile.open(QIODevice::ReadOnly)) {
qWarning() << QStringLiteral("Unable to read version from file.");
return false;
}
QString version;
QTextStream s1(&versionFile);
version.append(s1.readAll());
versionFile.close();
return QString::compare(newVersion, version, Qt::CaseInsensitive)>0;
}
void VideoCodecsModel::installFileFailed() {
qWarning() << QStringLiteral("Unable to install codec `%1` .").arg(cH264FileName);
} }
void VideoCodecsModel::updateCodecs (list<shared_ptr<linphone::PayloadType>> &codecs) { void VideoCodecsModel::updateCodecs (list<shared_ptr<linphone::PayloadType>> &codecs) {
CoreManager::getInstance()->getCore()->setVideoPayloadTypes(codecs); CoreManager::getInstance()->getCore()->setVideoPayloadTypes(codecs);
} }
...@@ -87,10 +137,22 @@ void VideoCodecsModel::load () { ...@@ -87,10 +137,22 @@ void VideoCodecsModel::load () {
if (find_if(codecs.begin(), codecs.end(), [](const shared_ptr<linphone::PayloadType> &codec) { if (find_if(codecs.begin(), codecs.end(), [](const shared_ptr<linphone::PayloadType> &codec) {
return codec->getMimeType() == "H264"; return codec->getMimeType() == "H264";
}) == codecs.end()) }) == codecs.end())
addDownloadableCodec("H264", cPluginUrlH264, cH264Description); addDownloadableCodec("H264", cH264FileName, cPluginUrlH264, cH264Description);
#endif #endif
} }
void VideoCodecsModel::extract(const QString &filePath) {
mFileExtractor->setFile(filePath);
mFileExtractor->extract();
}
void VideoCodecsModel::endInstallFile() {
mFileDownloader->remove();
mFileDownloader->writeVersion(cH264FileName);
mFileExtractor->rename(cH264FileName);
reload();
}
void VideoCodecsModel::reload () { void VideoCodecsModel::reload () {
beginResetModel(); beginResetModel();
load(); load();
......
...@@ -25,6 +25,9 @@ ...@@ -25,6 +25,9 @@
#include "AbstractCodecsModel.hpp" #include "AbstractCodecsModel.hpp"
#include "../file/FileDownloader.hpp"
#include "../file/FileExtractor.hpp"
// ============================================================================= // =============================================================================
class VideoCodecsModel : public AbstractCodecsModel { class VideoCodecsModel : public AbstractCodecsModel {
...@@ -32,13 +35,20 @@ class VideoCodecsModel : public AbstractCodecsModel { ...@@ -32,13 +35,20 @@ class VideoCodecsModel : public AbstractCodecsModel {
public: public:
VideoCodecsModel (QObject *parent = Q_NULLPTR); VideoCodecsModel (QObject *parent = Q_NULLPTR);
~VideoCodecsModel () = default; ~VideoCodecsModel ();
private: private:
void updateCodecs (std::list<std::shared_ptr<linphone::PayloadType>> &codecs) override; void updateCodecs (std::list<std::shared_ptr<linphone::PayloadType>> &codecs) override;
bool updateCodecVersion (const QString &filePath, const QString &newVersion);
void load (); void load ();
void extract(const QString &filePath);
void installFileFailed();
void endInstallFile();
void reload () override; void reload () override;
QTimer *mTimer = nullptr;
FileDownloader *mFileDownloader = nullptr;
FileExtractor *mFileExtractor = nullptr;
}; };
#endif // VIDEO_CODECS_MODEL_H_ #endif // VIDEO_CODECS_MODEL_H_
...@@ -27,7 +27,7 @@ ...@@ -27,7 +27,7 @@
#include "FileDownloader.hpp" #include "FileDownloader.hpp"
// ============================================================================= // =============================================================================
using namespace std;
namespace { namespace {
constexpr char cDefaultFileName[] = "download"; constexpr char cDefaultFileName[] = "download";
} }
...@@ -101,6 +101,25 @@ bool FileDownloader::remove () { ...@@ -101,6 +101,25 @@ bool FileDownloader::remove () {
return mDestinationFile.exists() && !mDestinationFile.isOpen() && mDestinationFile.remove(); return mDestinationFile.exists() && !mDestinationFile.isOpen() && mDestinationFile.remove();
} }
void FileDownloader::writeVersion(const QString &newFileName) {
QFile versionFile;
versionFile.setFileName(
QDir::cleanPath(mDownloadFolder) + QDir::separator() + newFileName + ".txt"
);
Q_ASSERT(!versionFile.isOpen());
if (versionFile.exists()) versionFile.remove();
if (!versionFile.open(QIODevice::WriteOnly)) {
qWarning() << QStringLiteral("Unable to write version to file.");
return;
}
string version = mUrl.toString().toStdString();
versionFile.write(version.c_str(), qstrlen(version.c_str()));
versionFile.close();
}
void FileDownloader::emitOutputError () { void FileDownloader::emitOutputError () {
qWarning() << QStringLiteral("Could not write into `%1` (%2).") qWarning() << QStringLiteral("Could not write into `%1` (%2).")
.arg(mDestinationFile.fileName()).arg(mDestinationFile.errorString()); .arg(mDestinationFile.fileName()).arg(mDestinationFile.errorString());
......
...@@ -19,7 +19,8 @@ ...@@ -19,7 +19,8 @@
* Created on: February 6, 2018 * Created on: February 6, 2018
* Author: Danmei Chen * Author: Danmei Chen
*/ */
#ifndef FILE_DOWNLOADER_H_
#define FILE_DOWNLOADER_H_
#include <QObject> #include <QObject>
#include <QtNetwork> #include <QtNetwork>
...@@ -41,6 +42,9 @@ class FileDownloader : public QObject { ...@@ -41,6 +42,9 @@ class FileDownloader : public QObject {
public: public:
Q_INVOKABLE void download (); Q_INVOKABLE void download ();
Q_INVOKABLE bool remove(); Q_INVOKABLE bool remove();
Q_INVOKABLE void setUrl (const QUrl &url);
Q_INVOKABLE void setDownloadFolder (const QString &downloadFolder);
Q_INVOKABLE void writeVersion (const QString &newFileName);
signals: signals:
void urlChanged (const QUrl &url); void urlChanged (const QUrl &url);
...@@ -53,10 +57,8 @@ signals: ...@@ -53,10 +57,8 @@ signals:
private: private:
QUrl getUrl () const; QUrl getUrl () const;
void setUrl (const QUrl &url);
QString getDownloadFolder () const; QString getDownloadFolder () const;
void setDownloadFolder (const QString &downloadFolder);
qint64 getReadBytes () const; qint64 getReadBytes () const;
void setReadBytes (qint64 readBytes); void setReadBytes (qint64 readBytes);
...@@ -86,3 +88,4 @@ private: ...@@ -86,3 +88,4 @@ private:
QPointer<QNetworkReply> mNetworkReply; QPointer<QNetworkReply> mNetworkReply;
QNetworkAccessManager mManager; QNetworkAccessManager mManager;
}; };
#endif // FILE_DOWNLOADER_H_
\ No newline at end of file
...@@ -116,6 +116,8 @@ void FileExtractor::extract () { ...@@ -116,6 +116,8 @@ void FileExtractor::extract () {
mDestinationFile.setFileName( mDestinationFile.setFileName(
QDir::cleanPath(mExtractFolder) + QDir::separator() + fileInfo.completeBaseName() QDir::cleanPath(mExtractFolder) + QDir::separator() + fileInfo.completeBaseName()
); );
//TODO not sure
mDestinationFile.remove();
if (!mDestinationFile.open(QIODevice::WriteOnly)) { if (!mDestinationFile.open(QIODevice::WriteOnly)) {
emitOutputError(); emitOutputError();
return; return;
...@@ -127,6 +129,18 @@ void FileExtractor::extract () { ...@@ -127,6 +129,18 @@ void FileExtractor::extract () {
mTimer->start(); mTimer->start();
} }
bool FileExtractor::remove () {
return mDestinationFile.exists() && !mDestinationFile.isOpen() && mDestinationFile.remove();
}
bool FileExtractor::rename (const QString &newFileName) {
const QString filePath = mExtractFolder + newFileName + "." + QFileInfo(mDestinationFile).suffix();
//delete old file
QFile oldFile(filePath);
if(oldFile.exists()) oldFile.remove();
return mDestinationFile.exists() && !mDestinationFile.isOpen() && mDestinationFile.rename(filePath);
}
QString FileExtractor::getFile () const { QString FileExtractor::getFile () const {
return mFile; return mFile;
} }
......
...@@ -47,6 +47,10 @@ public: ...@@ -47,6 +47,10 @@ public:
~FileExtractor (); ~FileExtractor ();
Q_INVOKABLE void extract (); Q_INVOKABLE void extract ();
Q_INVOKABLE bool remove ();
Q_INVOKABLE bool rename (const QString &newFileName);
Q_INVOKABLE void setExtractFolder (const QString &extractFolder);
Q_INVOKABLE void setFile (const QString &file);
signals: signals:
void fileChanged (const QString &file); void fileChanged (const QString &file);
...@@ -59,10 +63,8 @@ signals: ...@@ -59,10 +63,8 @@ signals:
private: private:
QString getFile () const; QString getFile () const;
void setFile (const QString &file);
QString getExtractFolder () const; QString getExtractFolder () const;
void setExtractFolder (const QString &extractFolder);
qint64 getReadBytes () const; qint64 getReadBytes () const;
void setReadBytes (qint64 readBytes); void setReadBytes (qint64 readBytes);
......
...@@ -17,6 +17,7 @@ DialogPlus { ...@@ -17,6 +17,7 @@ DialogPlus {
property alias installFolder: fileDownloader.downloadFolder property alias installFolder: fileDownloader.downloadFolder
property bool extract: false property bool extract: false
property string fileName property string fileName
property string newFileName
property bool _installing: false property bool _installing: false
property int _exitStatus: -1 // Not downloaded for the moment. property int _exitStatus: -1 // Not downloaded for the moment.
...@@ -29,8 +30,11 @@ DialogPlus { ...@@ -29,8 +30,11 @@ DialogPlus {
} }
function _endInstall (exitStatus) { function _endInstall (exitStatus) {
if (dialog.extract) if (dialog.extract) {
fileDownloader.remove() fileDownloader.remove()
fileDownloader.writeVersion(newFileName)
fileExtractor.rename(newFileName)
}
dialog._exitStatus = exitStatus dialog._exitStatus = exitStatus
dialog._installing = false dialog._installing = false
} }
...@@ -108,13 +112,16 @@ DialogPlus { ...@@ -108,13 +112,16 @@ DialogPlus {
} }
Text { Text {
id: text
property var target: fileDownloader
anchors.right: parent.right anchors.right: parent.right
color: OnlineInstallerDialogStyle.column.text.color color: OnlineInstallerDialogStyle.column.text.color
font.pointSize: OnlineInstallerDialogStyle.column.text.pointSize font.pointSize: OnlineInstallerDialogStyle.column.text.pointSize
text: { text: {
var fileSize = Utils.formatSize(fileDownloader.totalBytes) var fileSize = Utils.formatSize(target.totalBytes)
return Utils.formatSize(fileDownloader.readBytes) + '/' + fileSize return Utils.formatSize(target.readBytes) + '/' + fileSize
} }
} }
...@@ -126,6 +133,7 @@ DialogPlus { ...@@ -126,6 +133,7 @@ DialogPlus {
fileExtractor.file = filePath fileExtractor.file = filePath
if (dialog.extract) { if (dialog.extract) {
progressBar.target = fileExtractor progressBar.target = fileExtractor
text.target = fileExtractor
fileExtractor.extract() fileExtractor.extract()
} else { } else {
dialog._endInstall(1) dialog._endInstall(1)
......
...@@ -105,6 +105,7 @@ function openCodecOnlineInstallerDialog (window, codecInfo, cb) { ...@@ -105,6 +105,7 @@ function openCodecOnlineInstallerDialog (window, codecInfo, cb) {
downloadUrl: codecInfo.downloadUrl, downloadUrl: codecInfo.downloadUrl,
extract: true, extract: true,
fileName: codecInfo.mime, fileName: codecInfo.mime,
newFileName: codecInfo.fileName,
installFolder: VideoCodecsModel.codecsFolder installFolder: VideoCodecsModel.codecsFolder
}, function (status) { }, function (status) {
if (status) { if (status) {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment