File indexing completed on 2025-01-05 03:53:05

0001 /* ============================================================
0002  *
0003  * This file is a part of digiKam project
0004  * https://www.digikam.org
0005  *
0006  * Date        : 2021-07-24
0007  * Description : a MJPEG Stream server to export items on the network.
0008  *
0009  * SPDX-FileCopyrightText: 2021-2024 by Gilles Caulier <caulier dot gilles at gmail dot com>
0010  *
0011  * SPDX-License-Identifier: GPL-2.0-or-later
0012  *
0013  * ============================================================ */
0014 
0015 #include "mjpegserver_p.h"
0016 
0017 // Qt includes
0018 
0019 #include <QBuffer>
0020 #include <QByteArray>
0021 #include <QIODevice>
0022 #include <QString>
0023 #include <QMutexLocker>
0024 
0025 // Local includes
0026 
0027 #include "digikam_debug.h"
0028 
0029 namespace DigikamGenericMjpegStreamPlugin
0030 {
0031 
0032 MjpegServer::MjpegServer(const QString& address,
0033                          int port,
0034                          QObject* const parent)
0035     : QObject(parent),
0036       d      (new Private(parent))
0037 {
0038     d->open(address, port);
0039     d->setMaxClients(10);
0040 }
0041 
0042 MjpegServer::~MjpegServer()
0043 {
0044     d->close();
0045 
0046     // NOTE: QObject based => self deletion.
0047 }
0048 
0049 bool MjpegServer::setRate(int ra)
0050 {
0051     if ((ra < 1) || (ra > 100))
0052     {
0053         qCWarning(DIGIKAM_GENERAL_LOG) << "Error: rate value is out of range: " << ra;
0054         return false;
0055     }
0056 
0057     d->rate  = ra;
0058     d->delay = (int)(1000000.0 / ra);
0059     qCDebug(DIGIKAM_GENERAL_LOG) << "MJPEG Server rate       :" << d->rate;
0060 
0061     return true;
0062 }
0063 
0064 int MjpegServer::rate() const
0065 {
0066     return d->rate;
0067 }
0068 
0069 bool MjpegServer::setMaxClients(int max)
0070 {
0071     if ((max < 1) || (max > 30))
0072     {
0073         qCWarning(DIGIKAM_GENERAL_LOG) << "Error: max clients value is out of range: " << max;
0074         return false;
0075     }
0076 
0077     d->setMaxClients(max);
0078 
0079     return true;
0080 }
0081 
0082 int MjpegServer::maxClients() const
0083 {
0084     return d->maxClients();
0085 }
0086 
0087 void MjpegServer::setBlackList(const QStringList& lst)
0088 {
0089     d->blackList = lst;
0090 }
0091 
0092 QStringList MjpegServer::blackList() const
0093 {
0094     return d->blackList;
0095 }
0096 
0097 void MjpegServer::slotWriteFrame(const QByteArray& frame)
0098 {
0099     d->mutexFrame.lock();
0100     {
0101         if (!frame.isNull())
0102         {
0103             // Perform a deep-copy of image data.
0104 
0105             d->lastFrame = QByteArray(frame.data(), frame.size());
0106         }
0107     }
0108     d->mutexFrame.unlock();
0109 }
0110 
0111 void MjpegServer::start()
0112 {
0113     d->start();
0114 }
0115 
0116 void MjpegServer::stop()
0117 {
0118     d->stop();
0119 }
0120 
0121 } // namespace DigikamGenericMjpegStreamPlugin
0122 
0123 #include "moc_mjpegserver.cpp"