Fix char out of range and media player signals

This commit is contained in:
Nicolas Werner 2022-04-11 04:59:39 +02:00
parent c775a93f77
commit 159bec3654
No known key found for this signature in database
GPG key ID: C8D75E610773F2D9
6 changed files with 91 additions and 103 deletions

View file

@ -4040,9 +4040,8 @@ Cache::displayName(const QString &room_id, const QString &user_id)
static bool static bool
isDisplaynameSafe(const std::string &s) isDisplaynameSafe(const std::string &s)
{ {
for (uint32_t cc : QString::fromStdString(s).toStdU32String()) { for (char32_t c : QString::fromStdString(s).toUcs4()) {
auto c = QChar(cc); if (QChar::isPrint(c) && !QChar::isSpace(c))
if (c.isPrint() && !c.isSpace())
return false; return false;
} }

View file

@ -285,15 +285,17 @@ main(int argc, char *argv[])
QLocale::setDefault(QLocale(QLocale::English, QLocale::UnitedKingdom)); QLocale::setDefault(QLocale(QLocale::English, QLocale::UnitedKingdom));
QTranslator qtTranslator; QTranslator qtTranslator;
if(qtTranslator.load(QLocale(), if (qtTranslator.load(QLocale(),
QStringLiteral("qt"), QStringLiteral("qt"),
QStringLiteral("_"), QStringLiteral("_"),
QLibraryInfo::path(QLibraryInfo::TranslationsPath))) QLibraryInfo::path(QLibraryInfo::TranslationsPath)))
app.installTranslator(&qtTranslator); app.installTranslator(&qtTranslator);
QTranslator appTranslator; QTranslator appTranslator;
if(appTranslator.load( if (appTranslator.load(QLocale(),
QLocale(), QStringLiteral("nheko"), QStringLiteral("_"), QStringLiteral(":/translations"))) QStringLiteral("nheko"),
QStringLiteral("_"),
QStringLiteral(":/translations")))
app.installTranslator(&appTranslator); app.installTranslator(&appTranslator);
MainWindow w; MainWindow w;

View file

@ -5,20 +5,20 @@
#include "InputBar.h" #include "InputBar.h"
#include <QVideoSink>
#include <QBuffer> #include <QBuffer>
#include <QClipboard> #include <QClipboard>
#include <QDropEvent> #include <QDropEvent>
#include <QFileDialog> #include <QFileDialog>
#include <QGuiApplication> #include <QGuiApplication>
#include <QInputMethod> #include <QInputMethod>
#include <QVideoFrame>
#include <QMediaMetaData> #include <QMediaMetaData>
#include <QMediaPlayer> #include <QMediaPlayer>
#include <QMimeData> #include <QMimeData>
#include <QMimeDatabase> #include <QMimeDatabase>
#include <QStandardPaths> #include <QStandardPaths>
#include <QTextBoundaryFinder> #include <QTextBoundaryFinder>
#include <QVideoFrame>
#include <QVideoSink>
#include <QRegularExpression> #include <QRegularExpression>
#include <mtx/responses/common.hpp> #include <mtx/responses/common.hpp>
@ -758,77 +758,75 @@ MediaUpload::MediaUpload(std::unique_ptr<QIODevice> source_,
if (mimeClass_ == u"video") { if (mimeClass_ == u"video") {
auto newSurface = new QVideoSink(this); auto newSurface = new QVideoSink(this);
connect( connect(newSurface,
newSurface, &QVideoSink::videoFrameChanged, this, [this, mediaPlayer](const QVideoFrame& frame) { &QVideoSink::videoFrameChanged,
QImage img = frame.toImage(); this,
[this, mediaPlayer](const QVideoFrame &frame) {
QImage img = frame.toImage();
if (img.size().isEmpty()) if (img.size().isEmpty())
return; return;
mediaPlayer->stop(); mediaPlayer->stop();
auto orientation = mediaPlayer->metaData().value(QMediaMetaData::Orientation).toInt(); auto orientation =
if (orientation == 90 || orientation == 270 || orientation == 180) { mediaPlayer->metaData().value(QMediaMetaData::Orientation).toInt();
img = if (orientation == 90 || orientation == 270 || orientation == 180) {
img.transformed(QTransform().rotate(orientation), Qt::SmoothTransformation); img = img.transformed(QTransform().rotate(orientation),
} Qt::SmoothTransformation);
}
nhlog::ui()->debug("Got image {}x{}", img.width(), img.height()); nhlog::ui()->debug("Got image {}x{}", img.width(), img.height());
this->setThumbnail(img); this->setThumbnail(img);
if (!dimensions_.isValid()) if (!dimensions_.isValid())
this->dimensions_ = img.size(); this->dimensions_ = img.size();
if (img.height() > 200 && img.width() > 360) if (img.height() > 200 && img.width() > 360)
img = img.scaled(360, 200, Qt::KeepAspectRatioByExpanding); img = img.scaled(360, 200, Qt::KeepAspectRatioByExpanding);
std::vector<unsigned char> data_; std::vector<unsigned char> data_;
for (int y = 0; y < img.height(); y++) { for (int y = 0; y < img.height(); y++) {
for (int x = 0; x < img.width(); x++) { for (int x = 0; x < img.width(); x++) {
auto p = img.pixel(x, y); auto p = img.pixel(x, y);
data_.push_back(static_cast<unsigned char>(qRed(p))); data_.push_back(static_cast<unsigned char>(qRed(p)));
data_.push_back(static_cast<unsigned char>(qGreen(p))); data_.push_back(static_cast<unsigned char>(qGreen(p)));
data_.push_back(static_cast<unsigned char>(qBlue(p))); data_.push_back(static_cast<unsigned char>(qBlue(p)));
} }
} }
blurhash_ = QString::fromStdString( blurhash_ = QString::fromStdString(
blurhash::encode(data_.data(), img.width(), img.height(), 4, 3)); blurhash::encode(data_.data(), img.width(), img.height(), 4, 3));
}); });
mediaPlayer->setVideoOutput(newSurface); mediaPlayer->setVideoOutput(newSurface);
} }
connect(mediaPlayer, connect(mediaPlayer, &QMediaPlayer::errorChanged, this, [mediaPlayer]() {
&QMediaPlayer::error, nhlog::ui()->debug("Media player error {} and errorStr {}",
this, mediaPlayer->error(),
[mediaPlayer]() { mediaPlayer->errorString().toStdString());
nhlog::ui()->debug("Media player error {} and errorStr {}", });
mediaPlayer->error(),
mediaPlayer->errorString().toStdString());
});
connect(mediaPlayer, connect(mediaPlayer,
&QMediaPlayer::mediaStatusChanged, &QMediaPlayer::mediaStatusChanged,
[mediaPlayer](QMediaPlayer::MediaStatus status) { [mediaPlayer](QMediaPlayer::MediaStatus status) {
nhlog::ui()->debug( nhlog::ui()->debug(
"Media player status {} and error {}", status, mediaPlayer->error()); "Media player status {} and error {}", status, mediaPlayer->error());
}); });
connect(mediaPlayer, connect(mediaPlayer, &QMediaPlayer::metaDataChanged, [this, mediaPlayer]() {
&QMediaPlayer::metaDataChanged, nhlog::ui()->debug("Got metadata");
[this, mediaPlayer]() {
nhlog::ui()->debug("Got metadata");
if (mediaPlayer->duration() > 0) if (mediaPlayer->duration() > 0)
this->duration_ = mediaPlayer->duration(); this->duration_ = mediaPlayer->duration();
auto dimensions = mediaPlayer->metaData().value(QMediaMetaData::Resolution).toSize(); auto dimensions = mediaPlayer->metaData().value(QMediaMetaData::Resolution).toSize();
if (!dimensions.isEmpty()) { if (!dimensions.isEmpty()) {
dimensions_ = dimensions; dimensions_ = dimensions;
auto orientation = auto orientation =
mediaPlayer->metaData().value(QMediaMetaData::Orientation).toInt(); mediaPlayer->metaData().value(QMediaMetaData::Orientation).toInt();
if (orientation == 90 || orientation == 270) { if (orientation == 90 || orientation == 270) {
dimensions_.transpose(); dimensions_.transpose();
} }
} }
}); });
connect(mediaPlayer, &QMediaPlayer::durationChanged, [this, mediaPlayer](qint64 duration) { connect(mediaPlayer, &QMediaPlayer::durationChanged, [this, mediaPlayer](qint64 duration) {
if (duration > 0) { if (duration > 0) {
this->duration_ = mediaPlayer->duration(); this->duration_ = mediaPlayer->duration();
@ -841,7 +839,7 @@ MediaUpload::MediaUpload(std::unique_ptr<QIODevice> source_,
auto originalFile = qobject_cast<QFile *>(source.get()); auto originalFile = qobject_cast<QFile *>(source.get());
mediaPlayer->setSourceDevice( mediaPlayer->setSourceDevice(
source.get(), QUrl(originalFile ? originalFile->fileName() : originalFilename_) ); source.get(), QUrl(originalFile ? originalFile->fileName() : originalFilename_));
mediaPlayer->play(); mediaPlayer->play();
} }

View file

@ -24,27 +24,20 @@ MxcMediaProxy::MxcMediaProxy(QObject *parent)
{ {
connect(this, &MxcMediaProxy::eventIdChanged, &MxcMediaProxy::startDownload); connect(this, &MxcMediaProxy::eventIdChanged, &MxcMediaProxy::startDownload);
connect(this, &MxcMediaProxy::roomChanged, &MxcMediaProxy::startDownload); connect(this, &MxcMediaProxy::roomChanged, &MxcMediaProxy::startDownload);
connect(this, connect(this, &MxcMediaProxy::error, [this]() {
&MxcMediaProxy::error, nhlog::ui()->info(
[this]() { "Media player error {} and errorStr {}", error(), this->errorString().toStdString());
nhlog::ui()->info("Media player error {} and errorStr {}", });
error(),
this->errorString().toStdString());
});
connect(this, &MxcMediaProxy::mediaStatusChanged, [this](QMediaPlayer::MediaStatus status) { connect(this, &MxcMediaProxy::mediaStatusChanged, [this](QMediaPlayer::MediaStatus status) {
nhlog::ui()->info("Media player status {} and error {}", status, this->error()); nhlog::ui()->info("Media player status {} and error {}", status, this->error());
}); });
connect(this, connect(this, &MxcMediaProxy::metaDataChanged, [this]() { emit orientationChanged(); });
&MxcMediaProxy::metaDataChanged,
[this]() {
emit orientationChanged();
});
} }
int int
MxcMediaProxy::orientation() const MxcMediaProxy::orientation() const
{ {
//nhlog::ui()->debug("metadata: {}", metaData(). // nhlog::ui()->debug("metadata: {}", metaData().
auto orientation = metaData().value(QMediaMetaData::Orientation).toInt(); auto orientation = metaData().value(QMediaMetaData::Orientation).toInt();
nhlog::ui()->debug("Video orientation: {}", orientation); nhlog::ui()->debug("Video orientation: {}", orientation);
return orientation; return orientation;
@ -109,7 +102,7 @@ MxcMediaProxy::startDownload()
QTimer::singleShot(0, this, [this, filename] { QTimer::singleShot(0, this, [this, filename] {
nhlog::ui()->info( nhlog::ui()->info(
"Playing buffer with size: {}, {}", buffer.bytesAvailable(), buffer.isOpen()); "Playing buffer with size: {}, {}", buffer.bytesAvailable(), buffer.isOpen());
this->setSourceDevice( &buffer, QUrl(filename.fileName())); this->setSourceDevice(&buffer, QUrl(filename.fileName()));
emit loadedChanged(); emit loadedChanged();
}); });
}; };

View file

@ -5,15 +5,15 @@
#pragma once #pragma once
#include <QVideoSink>
#include <QBuffer> #include <QBuffer>
#include <QMediaPlayer> #include <QMediaPlayer>
#include <QObject> #include <QObject>
#include <QPointer> #include <QPointer>
#include <QString> #include <QString>
#include <QVideoSink>
#include "timeline/TimelineModel.h"
#include "Logging.h" #include "Logging.h"
#include "timeline/TimelineModel.h"
class TimelineModel; class TimelineModel;

View file

@ -142,24 +142,21 @@ CallManager::CallManager(QObject *parent)
player_.play(); player_.play();
}); });
connect(&player_, connect(&player_, &QMediaPlayer::errorChanged, this, [this]() {
&QMediaPlayer::error, stopRingtone();
this, switch (player_.error()) {
[this]() { case QMediaPlayer::FormatError:
stopRingtone(); case QMediaPlayer::ResourceError:
switch (player_.error()) { nhlog::ui()->error("WebRTC: valid ringtone file not found");
case QMediaPlayer::FormatError: break;
case QMediaPlayer::ResourceError: case QMediaPlayer::AccessDeniedError:
nhlog::ui()->error("WebRTC: valid ringtone file not found"); nhlog::ui()->error("WebRTC: access to ringtone file denied");
break; break;
case QMediaPlayer::AccessDeniedError: default:
nhlog::ui()->error("WebRTC: access to ringtone file denied"); nhlog::ui()->error("WebRTC: unable to play ringtone");
break; break;
default: }
nhlog::ui()->error("WebRTC: unable to play ringtone"); });
break;
}
});
} }
void void
@ -496,10 +493,9 @@ CallManager::retrieveTurnServer()
void void
CallManager::playRingtone(const QUrl &ringtone, bool repeat) CallManager::playRingtone(const QUrl &ringtone, bool repeat)
{ {
player_.setLoops(repeat ? QMediaPlayer::Infinite : player_.setLoops(repeat ? QMediaPlayer::Infinite : 1);
1);
player_.setSource(ringtone); player_.setSource(ringtone);
//player_.audioOutput()->setVolume(100); // player_.audioOutput()->setVolume(100);
player_.play(); player_.play();
} }