VideoCodecsModel.cpp 6.7 KB
Newer Older
1 2
/*
 * VideoCodecsModel.cpp
3
 * Copyright (C) 2017-2018  Belledonne Communications, Grenoble, France
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
 *
 * This program 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 2
 * of the License, or (at your option) any later version.
 *
 * This program 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 this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 *
 *  Created on: April 3, 2017
 *      Author: Ronan Abhamon
 */

23
#include <QDebug>
24 25 26
#include <QDirIterator>
#include <QLibrary>

27 28 29 30 31
#include "app/paths/Paths.hpp"
#include "components/core/CoreManager.hpp"
#include "components/file/FileDownloader.hpp"
#include "components/file/FileExtractor.hpp"
#include "utils/Utils.hpp"
32 33 34

#include "VideoCodecsModel.hpp"

35 36
// =============================================================================

37 38
using namespace std;

39
namespace {
40
  constexpr char H264Description[] = "Provided by CISCO SYSTEM,INC";
41 42

  #ifdef Q_OS_LINUX
43 44
    constexpr char LibraryExtension[] = "so";
    constexpr char H264InstallName[] = "libopenh264.so";
45
    #ifdef Q_PROCESSOR_X86_64
46
      constexpr char PluginUrlH264[] = "http://ciscobinary.openh264.org/libopenh264-1.7.0-linux64.4.so.bz2";
47
    #else
48
      constexpr char PluginUrlH264[] = "http://ciscobinary.openh264.org/libopenh264-1.7.0-linux32.4.so.bz2";
49 50
    #endif // ifdef Q_PROCESSOR_X86_64
  #elif defined(Q_OS_WIN)
51 52
    constexpr char LibraryExtension[] = "dll";
    constexpr char H264InstallName[] = "openh264.dll";
53
    #ifdef Q_OS_WIN64
54
      constexpr char PluginUrlH264[] = "http://ciscobinary.openh264.org/openh264-1.7.0-win64.dll.bz2";
55
    #else
56
      constexpr char PluginUrlH264[] = "http://ciscobinary.openh264.org/openh264-1.7.0-win32.dll.bz2";
57 58 59
    #endif // ifdef Q_OS_WIN64
  #endif // ifdef Q_OS_LINUX
}
60

61
VideoCodecsModel::VideoCodecsModel (QObject *parent) : AbstractCodecsModel(parent) {
62
  load();
63 64
}

65 66 67
static bool downloadUpdatableCodec (
  QObject *parent,
  const QString &codecsFolder,
68 69 70 71 72 73 74
  const QString &mime,
  const QString &downloadUrl,
  const QString &installName
) {
  QString versionFilePath = codecsFolder + mime + ".txt";
  QFile versionFile(versionFilePath);

75
  if (!versionFile.exists() && !QFileInfo::exists(codecsFolder + installName))
76 77 78 79 80 81
    return false; // Must be downloaded one time before.

  if (!versionFile.open(QIODevice::ReadOnly))
    qWarning() << QStringLiteral("Unable to read codec version from: `%1`.").arg(versionFilePath);
  else if (!QString::compare(QTextStream(&versionFile).readAll(), downloadUrl, Qt::CaseInsensitive))
    return false;
82

83
  qInfo() << QStringLiteral("Updating `%1` codec...").arg(mime);
84

85 86
  FileDownloader *fileDownloader = new FileDownloader(parent);
  fileDownloader->setUrl(QUrl(downloadUrl));
87
  fileDownloader->setDownloadFolder(codecsFolder);
88

89 90
  FileExtractor *fileExtractor = new FileExtractor(fileDownloader);
  fileExtractor->setExtractFolder(codecsFolder);
91
  fileExtractor->setExtractName(installName + ".in");
92

93
  QObject::connect(fileDownloader, &FileDownloader::downloadFinished, [fileExtractor](const QString &filePath) {
94 95 96
    fileExtractor->setFile(filePath);
    fileExtractor->extract();
  });
97

98
  QObject::connect(fileDownloader, &FileDownloader::downloadFailed, [fileDownloader]() {
99 100 101
    fileDownloader->deleteLater();
  });

102
  QObject::connect(fileExtractor, &FileExtractor::extractFinished, [fileDownloader, fileExtractor, versionFilePath, downloadUrl]() {
103 104 105 106 107 108 109 110 111 112 113 114 115 116 117
    QFile versionFile(versionFilePath);
    if (!versionFile.open(QIODevice::WriteOnly)) {
      qWarning() << QStringLiteral("Unable to write codec version in: `%1`.").arg(versionFilePath);
      return;
    }
    if (versionFile.write(downloadUrl.toStdString().c_str(), downloadUrl.length()) == -1) {
      fileExtractor->remove();
      versionFile.close();
      versionFile.remove();
    }

    fileDownloader->remove();
    fileDownloader->deleteLater();
  });

118
  QObject::connect(fileExtractor, &FileExtractor::extractFailed, [fileDownloader]() {
119 120 121
    fileDownloader->remove();
    fileDownloader->deleteLater();
  });
122

123 124 125 126
  fileDownloader->download();

  return true;
}
127

128 129
void VideoCodecsModel::updateCodecs () {
  #if defined(Q_OS_LINUX) || defined(Q_OS_WIN)
130
    static const QString codecSuffix = QStringLiteral(".%1").arg(LibraryExtension);
131 132 133 134

    QDirIterator it(Utils::coreStringToAppString(Paths::getCodecsDirPath()));
    while (it.hasNext()) {
      QFileInfo info(it.next());
135
      if (info.suffix() == QLatin1String("in")) {
136 137 138 139 140 141 142 143 144 145 146 147 148 149
        QString codecName = info.completeBaseName();
        if (codecName.endsWith(codecSuffix)) {
          QString codecPath = info.dir().path() + QDir::separator() + codecName;
          QFile::remove(codecPath);
          QFile::rename(info.filePath(), codecPath);
        }
      }
    }
  #endif // if defined(Q_OS_LINUX) || defined(Q_OS_WIN)
}

void VideoCodecsModel::downloadUpdatableCodecs (QObject *parent) {
  #if defined(Q_OS_LINUX) || defined(Q_OS_WIN)
    QString codecsFolder = Utils::coreStringToAppString(Paths::getCodecsDirPath());
150
    downloadUpdatableCodec(parent, codecsFolder, "H264", PluginUrlH264, H264InstallName);
151 152 153
  #endif // if defined(Q_OS_LINUX) || defined(Q_OS_WIN)
}

154
void VideoCodecsModel::updateCodecs (list<shared_ptr<linphone::PayloadType>> &codecs) {
155 156
  CoreManager::getInstance()->getCore()->setVideoPayloadTypes(codecs);
}
157 158 159 160 161 162 163 164 165 166 167

void VideoCodecsModel::load () {
  mCodecs.clear();

  shared_ptr<linphone::Core> core = CoreManager::getInstance()->getCore();

  // Load downloaded codecs like OpenH264.
  #if defined(Q_OS_LINUX) || defined(Q_OS_WIN)
    QDirIterator it(Utils::coreStringToAppString(Paths::getCodecsDirPath()));
    while (it.hasNext()) {
      QFileInfo info(it.next());
168
      if (info.suffix() == LibraryExtension)
169 170 171
        QLibrary(info.filePath()).load();
    }
    core->reloadMsPlugins("");
172
  #endif // if defined(Q_OS_LINUX) || defined(Q_OS_WIN)
173 174 175 176 177 178 179 180 181 182 183

  // Add codecs.
  auto codecs = core->getVideoPayloadTypes();
  for (auto &codec : codecs)
    addCodec(codec);

  // Add downloadable codecs.
  #if defined(Q_OS_LINUX) || defined(Q_OS_WIN)
    if (find_if(codecs.begin(), codecs.end(), [](const shared_ptr<linphone::PayloadType> &codec) {
      return codec->getMimeType() == "H264";
    }) == codecs.end())
184
      addDownloadableCodec("H264", H264Description, PluginUrlH264, H264InstallName);
185
  #endif // if defined(Q_OS_LINUX) || defined(Q_OS_WIN)
186 187 188 189 190 191 192
}

void VideoCodecsModel::reload () {
  beginResetModel();
  load();
  endResetModel();
}