Skip to content
This repository was archived by the owner on Jan 28, 2022. It is now read-only.

Commit 63514fa

Browse files
committed
fix: mediaplayersutils thread issue
1 parent 1b45a75 commit 63514fa

File tree

2 files changed

+88
-101
lines changed

2 files changed

+88
-101
lines changed

components/media_player/sources/utils_mediaplayer.cpp

+88-100
Original file line numberDiff line numberDiff line change
@@ -41,14 +41,40 @@ MediaPlayerUtils::MediaPlayerUtils(QObject *parent) : QObject(parent) {
4141
m_startTimer = new QTimer(this);
4242
m_startTimer->setSingleShot(true);
4343
m_startTimer->setInterval(1000);
44+
45+
m_worker = new MediaPlayerUtilsWorker();
46+
m_workerThread = new QThread(this);
47+
48+
QObject::connect(m_worker, &MediaPlayerUtilsWorker::processingDone, this, &MediaPlayerUtils::onProcessingDone);
49+
50+
m_worker->moveToThread(m_workerThread);
51+
m_workerThread->start();
4452
}
4553

4654
MediaPlayerUtils::~MediaPlayerUtils() {
4755
qCDebug(CLASS_LC()) << this << "Destructor called";
4856
m_engine->removeImageProvider(m_imageProviderId);
4957
qCDebug(CLASS_LC()) << this << "Destructor: Image provider removed:" << m_imageProviderId;
5058

51-
deleteWorker();
59+
if (m_worker != nullptr) {
60+
qCDebug(CLASS_LC()) << this << "Destructor: Worker was not null";
61+
m_worker->disconnect();
62+
qCDebug(CLASS_LC()) << this << "Destructor: Signal disconnected from Worker class";
63+
delete m_worker;
64+
m_worker = nullptr;
65+
qCDebug(CLASS_LC()) << this << "Destructor: Worker deleted";
66+
}
67+
68+
if (m_workerThread != nullptr) {
69+
qCDebug(CLASS_LC()) << this << "Destructor: WorkerThread was not null";
70+
if (m_workerThread->isRunning()) {
71+
m_workerThread->exit();
72+
m_workerThread->wait(5000);
73+
}
74+
delete m_workerThread;
75+
m_workerThread = nullptr;
76+
qCDebug(CLASS_LC()) << this << "Destructor: WorkerThread was deleted";
77+
}
5278

5379
if (m_startTimer->isActive()) {
5480
m_startTimer->stop();
@@ -93,8 +119,6 @@ void MediaPlayerUtils::onProcessingDone(const QColor &pixelColor, const QImage &
93119
m_pixelColor = pixelColor;
94120
emit pixelColorChanged();
95121

96-
deleteWorker();
97-
98122
qCDebug(CLASS_LC()) << this << "Processing done";
99123
}
100124

@@ -105,49 +129,10 @@ void MediaPlayerUtils::generateImages(const QString &url) {
105129
m_prevImageURL = url;
106130
emit processingStarted();
107131

108-
if (m_startTimer->isActive()) {
109-
m_startTimer->stop();
110-
qCDebug(CLASS_LC()) << this << "Timer stopped";
111-
}
112-
113-
deleteWorker();
114-
115-
m_worker = new MediaPlayerUtilsWorker();
116-
m_workerThread = new QThread(this);
117-
118-
qCDebug(CLASS_LC()) << this << "New Worker and WorkerThread created";
119-
120-
QObject::connect(m_worker, &MediaPlayerUtilsWorker::processingDone, this, &MediaPlayerUtils::onProcessingDone);
121-
122-
m_worker->moveToThread(m_workerThread);
123-
m_workerThread->start();
124132
m_worker->generateImages(url);
125133
}
126134
}
127135

128-
void MediaPlayerUtils::deleteWorker() {
129-
if (m_worker != nullptr) {
130-
qCDebug(CLASS_LC()) << this << "Deleteworker: Worker was not null";
131-
m_worker->disconnect();
132-
qCDebug(CLASS_LC()) << this << "Deleteworker: Signal disconnected from Worker class";
133-
delete m_worker;
134-
m_worker = nullptr;
135-
qCDebug(CLASS_LC()) << this << "Deleteworker: Worker deleted";
136-
}
137-
138-
if (m_workerThread != nullptr) {
139-
qCDebug(CLASS_LC()) << this << "Deleteworker: WorkerThread was not null";
140-
if (m_workerThread->isRunning()) {
141-
m_workerThread->exit();
142-
m_workerThread->wait(5000);
143-
}
144-
delete m_workerThread;
145-
m_workerThread = nullptr;
146-
qCDebug(CLASS_LC()) << this << "Deleteworker: WorkerThread was deleted";
147-
}
148-
qCDebug(CLASS_LC()) << this << "Deleteworker: End";
149-
}
150-
151136
MediaPlayerUtilsWorker::MediaPlayerUtilsWorker(QObject *parent) : QObject(parent) {
152137
m_manager = new QNetworkAccessManager();
153138
}
@@ -167,70 +152,73 @@ void MediaPlayerUtilsWorker::generateImages(const QString &url) {
167152
}
168153

169154
void MediaPlayerUtilsWorker::generateImagesReply() {
170-
if (m_reply->error() == QNetworkReply::NoError) {
171-
// run image processing in different thread
172-
QImage image(280, 280, QImage::Format_RGB888);
173-
image.fill(Qt::black);
174-
175-
if (!image.load(m_reply, nullptr)) {
176-
qCWarning(CLASS_LC) << "ERROR LOADING IMAGE";
177-
if (m_reply) {
178-
m_reply->deleteLater();
179-
m_reply = nullptr;
155+
if (m_reply != nullptr) {
156+
m_reply->disconnect();
157+
158+
if (m_reply->error() == QNetworkReply::NoError) {
159+
// run image processing in different thread
160+
QImage image(280, 280, QImage::Format_RGB888);
161+
image.fill(Qt::black);
162+
163+
if (!image.load(m_reply, nullptr)) {
164+
qCWarning(CLASS_LC) << "ERROR LOADING IMAGE";
165+
if (m_reply) {
166+
m_reply->deleteLater();
167+
m_reply = nullptr;
168+
}
169+
return;
170+
} else {
171+
////////////////////////////////////////////////////////////////////
172+
/// GET DOMINANT COLOR
173+
////////////////////////////////////////////////////////////////////
174+
qCDebug(CLASS_LC()) << this << "Getting dominant color";
175+
// QColor m_pixelColor = dominantColor(image);
176+
177+
// shrink down image to 1x1 pixel and measure the color
178+
QImage onePixel = image;
179+
onePixel.scaled(1, 1, Qt::IgnoreAspectRatio);
180+
QColor m_pixelColor = onePixel.pixel(1, 1);
181+
182+
// change the brightness of the color if it's too bright
183+
if (m_pixelColor.lightness() > 150) {
184+
m_pixelColor.setHsv(m_pixelColor.hue(), m_pixelColor.saturation(), (m_pixelColor.value() - 80));
185+
}
186+
187+
// if the color is close to white, return black instead
188+
if (m_pixelColor.lightness() > 210) {
189+
m_pixelColor = QColor("black");
190+
}
191+
192+
////////////////////////////////////////////////////////////////////
193+
/// CREATE LARGE BACKGROUND IMAGE
194+
////////////////////////////////////////////////////////////////////
195+
qCDebug(CLASS_LC()) << this << "Creating image";
196+
// resize image
197+
image.scaledToHeight(280, Qt::SmoothTransformation);
198+
199+
// create noise layer
200+
QImage noise(":/images/mini-music-player/noise.png");
201+
noise.scaledToHeight(280, Qt::SmoothTransformation);
202+
noise = noise.convertToFormat(QImage::Format_ARGB32);
203+
204+
// merge the images together
205+
QPainter painter(&image);
206+
painter.drawImage(image.rect(), noise);
207+
painter.end();
208+
209+
qCDebug(CLASS_LC()) << this << "Creating image DONE";
210+
211+
emit processingDone(m_pixelColor, image);
180212
}
181-
return;
182213
} else {
183-
////////////////////////////////////////////////////////////////////
184-
/// GET DOMINANT COLOR
185-
////////////////////////////////////////////////////////////////////
186-
qCDebug(CLASS_LC()) << this << "Getting dominant color";
187-
// QColor m_pixelColor = dominantColor(image);
188-
189-
// shrink down image to 1x1 pixel and measure the color
190-
QImage onePixel = image;
191-
onePixel.scaled(1, 1, Qt::IgnoreAspectRatio);
192-
QColor m_pixelColor = onePixel.pixel(1, 1);
193-
194-
// change the brightness of the color if it's too bright
195-
if (m_pixelColor.lightness() > 150) {
196-
m_pixelColor.setHsv(m_pixelColor.hue(), m_pixelColor.saturation(), (m_pixelColor.value() - 80));
197-
}
198-
199-
// if the color is close to white, return black instead
200-
if (m_pixelColor.lightness() > 210) {
201-
m_pixelColor = QColor("black");
202-
}
203-
204-
////////////////////////////////////////////////////////////////////
205-
/// CREATE LARGE BACKGROUND IMAGE
206-
////////////////////////////////////////////////////////////////////
207-
qCDebug(CLASS_LC()) << this << "Creating image";
208-
// resize image
209-
image.scaledToHeight(280, Qt::SmoothTransformation);
210-
211-
// create noise layer
212-
QImage noise(":/images/mini-music-player/noise.png");
213-
noise.scaledToHeight(280, Qt::SmoothTransformation);
214-
noise = noise.convertToFormat(QImage::Format_ARGB32);
215-
216-
// merge the images together
217-
QPainter painter(&image);
218-
painter.drawImage(image.rect(), noise);
219-
painter.end();
220-
221-
qCDebug(CLASS_LC()) << this << "Creating image DONE";
222-
223-
emit processingDone(m_pixelColor, image);
214+
qCWarning(CLASS_LC) << this << "NETWORK REPLY ERROR" << m_reply->errorString();
215+
emit processingDone(QColor("black"), QImage());
224216
}
225-
} else {
226-
qCWarning(CLASS_LC) << this << "NETWORK REPLY ERROR" << m_reply->errorString();
227-
emit processingDone(QColor("black"), QImage());
228-
}
229-
if (m_reply != nullptr) {
217+
230218
delete m_reply;
231219
m_reply = nullptr;
220+
qCDebug(CLASS_LC()) << this << "Network reply deleted";
232221
}
233-
qCDebug(CLASS_LC()) << this << "Network reply deleted";
234222
}
235223

236224
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

components/media_player/sources/utils_mediaplayer.h

-1
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,6 @@ class MediaPlayerUtils : public QObject {
112112
QTimer* m_startTimer;
113113

114114
void generateImages(const QString& url);
115-
void deleteWorker();
116115

117116
QThread* m_workerThread = nullptr;
118117
MediaPlayerUtilsWorker* m_worker = nullptr;

0 commit comments

Comments
 (0)