File: Plugin.cpp

package info (click to toggle)
kwave 25.04.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 23,272 kB
  • sloc: cpp: 56,173; xml: 817; perl: 688; sh: 57; makefile: 11
file content (557 lines) | stat: -rw-r--r-- 17,655 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
/***************************************************************************
             Plugin.cpp  -  base class of all Kwave plugins
                             -------------------
    begin                : Thu Jul 27 2000
    copyright            : (C) 2000 by Thomas Eschenbacher
    email                : Thomas.Eschenbacher@gmx.de
 ***************************************************************************/

/***************************************************************************
 *                                                                         *
 *   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.                                   *
 *                                                                         *
 ***************************************************************************/

#include "config.h"

#include <errno.h>
#include <math.h>
#include <string.h>

#include <new>

#include <QApplication>
#include <QElapsedTimer>
#include <QProgressDialog>
#include <QThread>
#include <QVariantList>
#include <QWidget>

#include <KLocalizedString>

#include "libkwave/ConfirmCancelProxy.h"
#include "libkwave/Plugin.h"
#include "libkwave/PluginManager.h"
#include "libkwave/Sample.h"
#include "libkwave/SignalManager.h"
#include "libkwave/String.h"
#include "libkwave/Utils.h"
#include "libkwave/WorkerThread.h"

#ifdef DEBUG
#include <execinfo.h> // for backtrace()
#endif

/** number of updates of the progress bat per second */
#define PROGRESS_UPDATES_PER_SECOND 4

/** maximum value of the progress bar */
#define PROGRESS_MAXIMUM 1000000

/*
   A plugin can be unloaded through two different ways. The possible
   scenarios are:

   1. WITHOUT a thread
      main thread:  new --- release

   2. WITH a plugin thread
      main thread:    new --- use --- start --- release
      plugin thread:                     --- run --- release

      The plugin can be unloaded either in the main thread or in the
      context of the plugin's thread, depending on what occurs first.

 */

//***************************************************************************
Kwave::Plugin::Plugin(QObject *parent, const QVariantList &args)
    :QObject(nullptr),
     Kwave::Runnable(),
     m_plugin_manager(qobject_cast<Kwave::PluginManager *>(parent)),
     m_name(args[0].toString()),
     m_description(args[1].toString()),
     m_thread(nullptr),
     m_thread_lock(),
     m_progress_enabled(true),
     m_stop(0),
     m_progress(nullptr),
     m_confirm_cancel(nullptr),
     m_usage_count(1),
     m_usage_lock(),
     m_progress_timer(),
     m_current_progress(-1),
     m_progress_lock()
{
    // check: this must be called from the GUI thread only!
    Q_ASSERT(this->thread() == QThread::currentThread());
    Q_ASSERT(this->thread() == qApp->thread());

    Q_ASSERT(m_plugin_manager);
    connect(&m_progress_timer, SIGNAL(timeout()),
            this, SLOT(updateProgressTick()),
            Qt::DirectConnection);
}

//***************************************************************************
Kwave::Plugin::~Plugin()
{
    // inform our owner that we close. This allows the plugin to
    // delete itself
    close();

    // lock usage
    QMutexLocker lock_usage(&m_usage_lock);
    {
        QMutexLocker lock_thread(&m_thread_lock);
        if (m_thread) {
            if (m_thread->isRunning()) m_thread->wait(5000);
            if (m_thread->isRunning()) m_thread->stop();
            if (m_thread->isRunning()) m_thread->wait(1000);
            if (m_thread->isRunning()) {
                // unable to stop the thread
                qWarning("Kwave::Plugin::stop(): stale thread !");
            }
            delete m_thread;
            m_thread = nullptr;
        }
    }

    // finally get rid of the confirm/cancel proxy and the progress dialog
    closeProgressDialog(this);
}

//***************************************************************************
void Kwave::Plugin::load(QStringList &)
{
}

//***************************************************************************
void Kwave::Plugin::unload()
{
}

//***************************************************************************
QStringList *Kwave::Plugin::setup(QStringList &)
{
    QStringList *result = new(std::nothrow) QStringList();
    Q_ASSERT(result);
    return result;
}

//***************************************************************************
int Kwave::Plugin::start(QStringList &)
{
    QMutexLocker lock(&m_thread_lock);
    m_stop = false;

    // check: start() must be called from the GUI thread only!
    Q_ASSERT(this->thread() == QThread::currentThread());
    Q_ASSERT(this->thread() == qApp->thread());

    // create a progress dialog for processing mode (not used for pre-listen)
    if (m_progress_enabled && !m_progress) {
        m_progress = new(std::nothrow) QProgressDialog(parentWidget());
        Q_ASSERT(m_progress);
    }

    // set up the progress dialog when in processing (not pre-listen) mode
    if (m_progress_enabled && m_progress) {
        sample_index_t first, last;
        QVector<unsigned int> tracks;

        selection(&tracks, &first, &last, true);
        m_progress->setModal(true);
        m_progress->setVisible(false);
        m_progress->setMinimumDuration(2000);
        m_progress->setAutoClose(false);
        m_progress->setMaximum(PROGRESS_MAXIMUM);
        m_progress->setValue(0);
        m_progress->setLabelText(progressText());
        int h = m_progress->sizeHint().height();
        int w = m_progress->sizeHint().height();
        if (w < 4 * h) w = 4 * h;
        m_progress->setFixedSize(w, h);

        // use a "proxy" that asks for confirmation of cancel
        if (!m_confirm_cancel) {
            m_confirm_cancel = new(std::nothrow)
                Kwave::ConfirmCancelProxy(m_progress,
                nullptr, nullptr, this, SLOT(cancel()));
            Q_ASSERT(m_confirm_cancel);
        }
        connect(m_progress,       SIGNAL(canceled()),
                m_confirm_cancel, SLOT(cancel()));
        connect(this,             SIGNAL(setProgressText(QString)),
                m_progress,       SLOT(setLabelText(QString)),
                Qt::QueuedConnection);
        connect(this, SIGNAL(sigDone(Kwave::Plugin*)),
                this, SLOT(closeProgressDialog(Kwave::Plugin*)),
                Qt::QueuedConnection);
        m_progress->setVisible(true);
    }

    return 0;
}

//***************************************************************************
QString Kwave::Plugin::name() const
{
    return m_name;
}

//***************************************************************************
QString Kwave::Plugin::description() const
{
    return m_description;
}

//***************************************************************************
QString Kwave::Plugin::progressText()
{
    return i18n("Running plugin '%1'...", name());
}

//***************************************************************************
int Kwave::Plugin::stop()
{
    cancel();

    if (m_thread && m_thread->isRunning() &&
        (QThread::currentThread() == m_thread)) {
        qWarning("Kwave::Plugin::stop(): plugin '%s' called stop() from "
                 "within it's own worker thread (from run() ?). "
                 "This would produce a deadlock, PLEASE FIX THIS !",
                 DBG(name()));

#ifdef DEBUG
        qDebug("pthread_self()=%p, tid=%p",
               reinterpret_cast<void *>(QThread::currentThread()),
               reinterpret_cast<void *>(m_thread));
        void *buf[256];
        int n = backtrace(buf, 256);
        backtrace_symbols_fd(buf, n, 2);
#endif
        return -EBUSY;
    }

    {
        QMutexLocker lock(&m_thread_lock);
        if (m_thread) {
            if (m_thread->isRunning()) m_thread->wait(5000);
            if (m_thread->isRunning()) m_thread->stop();
            if (m_thread->isRunning()) m_thread->wait(1000);
            if (m_thread->isRunning()) {
                // unable to stop the thread
                qWarning("Kwave::Plugin::stop(): stale thread !");
            }
            delete m_thread;
            m_thread = nullptr;
        }
    }
    return 0;
}

//***************************************************************************
void Kwave::Plugin::setProgressDialogEnabled(bool enable)
{
    m_progress_enabled = enable;
}

//***************************************************************************
void Kwave::Plugin::updateProgress(qreal progress)
{
    // check: this must be called from the GUI thread only!
    Q_ASSERT(this->thread() == QThread::currentThread());
    Q_ASSERT(this->thread() == qApp->thread());

//     qDebug("Kwave::Plugin::updateProgress(%0.1f)", progress);
    QMutexLocker lock(&m_progress_lock);

    // take over the current progress
    // note: no lock needed, this is called in the GUI thread context!
    m_current_progress = qreal(PROGRESS_MAXIMUM / 100.0) * progress;

    // start the timer for updating the progress bar if it is not active
    if (!m_progress_timer.isActive() && m_progress) {
        m_progress_timer.setSingleShot(true);
        m_progress_timer.start(1000 / PROGRESS_UPDATES_PER_SECOND);
    }
}

//***************************************************************************
void Kwave::Plugin::updateProgressTick()
{
    // check: this must be called from the GUI thread only!
    Q_ASSERT(this->thread() == QThread::currentThread());
    Q_ASSERT(this->thread() == qApp->thread());

    QMutexLocker lock(&m_progress_lock);
    if (m_progress) m_progress->setValue(
        Kwave::toInt(qBound<double>(0.0, m_current_progress,
                                    PROGRESS_MAXIMUM)));
}

//***************************************************************************
void Kwave::Plugin::closeProgressDialog(Kwave::Plugin *)
{
    // check: this must be called from the GUI thread only!
    Q_ASSERT(this->thread() == QThread::currentThread());
    Q_ASSERT(this->thread() == qApp->thread());

    QMutexLocker lock(&m_progress_lock);

    // stop the progress timer
    m_progress_timer.stop();
    m_progress_timer.disconnect();

    if (m_confirm_cancel) m_confirm_cancel->disconnect();
    if (m_progress)       m_progress->disconnect();

    // NOTE: as the progress dialog is *modal*, it is higly probable
    //       that this function is called from the context of the event
    //       loop that is provided by the progress dialog
    //       => deleting this object should be done somewhere later...
    if (m_confirm_cancel) {
        m_confirm_cancel->deleteLater();
        m_confirm_cancel = nullptr;
    }

    if (m_progress) {
        m_progress->done(0);
        m_progress->deleteLater();
        m_progress = nullptr;
    }
}

//***************************************************************************
void Kwave::Plugin::cancel()
{
    if (!m_stop)
    {
        QMutexLocker lock(&m_thread_lock);
        m_stop = true;
        if (m_thread) m_thread->cancel();
    }
    emit sigCancel();
}

//***************************************************************************
int Kwave::Plugin::execute(QStringList &params)
{
    QMutexLocker lock(&m_thread_lock);
    m_stop = false;

    m_thread = new(std::nothrow) Kwave::WorkerThread(this, QVariant(params));
    Q_ASSERT(m_thread);
    if (!m_thread) return -ENOMEM;
    connect(this, SIGNAL(sigCancel()), m_thread, SLOT(cancel()),
            Qt::QueuedConnection);

    // increment the use count, it is decremented at the end of
    // the run_wrapper when the thread is finished
    use();

    // start the thread, this executes run()
    m_thread->start();

    return 0;
}

//***************************************************************************
bool Kwave::Plugin::canClose() const
{
    return !isRunning();
}

//***************************************************************************
bool Kwave::Plugin::shouldStop() const
{
    return (m_stop || (m_thread && m_thread->isInterruptionRequested()));
}

//***************************************************************************
bool Kwave::Plugin::isRunning() const
{
    return (m_thread) && m_thread->isRunning();
}

//***************************************************************************
void Kwave::Plugin::run(QStringList params)
{
    Q_UNUSED(params)
}

//***************************************************************************
void Kwave::Plugin::run_wrapper(const QVariant &params)
{
    // signal that we are running
    emit sigRunning(this);

    // start time measurement
    QElapsedTimer t;
    t.start();

    // call the plugin's run function in this worker thread context
    run(params.toStringList());

    // evaluate the elapsed time
    double seconds = static_cast<double>(t.elapsed()) * 1E-3;
    qDebug("plugin %s done, running for %0.03g seconds",
           DBG(name()), seconds);

    // emit the "done" signal
    emit sigDone(this);

    m_stop = false;
    release();
}

//***************************************************************************
void Kwave::Plugin::close()
{
    // only call stop() if we are NOT in the worker thread / run function !
    if (m_thread && m_thread->isRunning() &&
        (QThread::currentThread() != m_thread) )
    {
        // check: this must be called from the GUI thread only!
        Q_ASSERT(this->thread() == QThread::currentThread());
        Q_ASSERT(this->thread() == qApp->thread());

        closeProgressDialog(this);
        stop();
    } else if ((QThread::currentThread() == m_thread)) {
        qWarning("Kwave::Plugin::close -> called from worker thread?");
    }
}

//***************************************************************************
void Kwave::Plugin::use()
{
    QMutexLocker lock(&m_usage_lock);
    Q_ASSERT(m_usage_count); // should be at least 1 (from constructor)
    m_usage_count++;
}

//***************************************************************************
void Kwave::Plugin::release()
{
    bool finished = false;

    {
        QMutexLocker lock(&m_usage_lock);
        Q_ASSERT(m_usage_count);
        if (m_usage_count) {
            m_usage_count--;
            if (!m_usage_count) finished = true;
        }
    }

    if (finished) emit sigClosed(this);
}

//***************************************************************************
Kwave::PluginManager &Kwave::Plugin::manager() const
{
    Q_ASSERT(m_plugin_manager);
    return *m_plugin_manager;
}

//***************************************************************************
Kwave::SignalManager &Kwave::Plugin::signalManager()
{
    return manager().signalManager();
}

//***************************************************************************
QWidget *Kwave::Plugin::parentWidget() const
{
    return manager().parentWidget();
}

//***************************************************************************
QString Kwave::Plugin::signalName()
{
    return signalManager().signalName();
}

//***************************************************************************
sample_index_t Kwave::Plugin::signalLength()
{
    return manager().signalLength();
}

//***************************************************************************
double Kwave::Plugin::signalRate()
{
    return manager().signalRate();
}

//***************************************************************************
const QVector<unsigned int> Kwave::Plugin::selectedTracks()
{
    return signalManager().selectedTracks();
}

//***************************************************************************
sample_index_t Kwave::Plugin::selection(QVector<unsigned int> *tracks,
                                        sample_index_t *left,
                                        sample_index_t *right,
                                        bool expand_if_empty)
{
    sample_index_t l = manager().selectionStart();
    sample_index_t r = manager().selectionEnd();

    // expand to the whole signal if left==right and expand_if_empty is set
    if ((l == r) && (expand_if_empty)) {
        l = 0;
        r = manager().signalLength();
        if (r) r--;
    }

    // get the list of selected tracks
    if (tracks) *tracks = signalManager().selectedTracks();

    if (left)  *left  = l;
    if (right) *right = r;
    return (r != l) ? (r - l + 1) : 0;
}

//***************************************************************************
void Kwave::Plugin::selectRange(sample_index_t offset, sample_index_t length)
{
    manager().selectRange(offset, length);
}

//***************************************************************************
void Kwave::Plugin::emitCommand(const QString &command)
{
    manager().enqueueCommand(command);
}

//***************************************************************************
void Kwave::Plugin::migrateToActiveContext()
{
    manager().migratePluginToActiveContext(this);
}

//***************************************************************************
void Kwave::Plugin::setPluginManager(Kwave::PluginManager *new_plugin_manager)
{
    Q_ASSERT(new_plugin_manager);
    if (!new_plugin_manager) return;
    m_plugin_manager = new_plugin_manager;

    if (m_progress)
        m_progress->setParent(m_plugin_manager->parentWidget());
    if (m_confirm_cancel)
        m_confirm_cancel->setParent(m_plugin_manager->parentWidget());
}

//***************************************************************************
//***************************************************************************

#include "moc_Plugin.cpp"