Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Application does not exit after mpv_set_wakeup_callback



  • Hi Folks,
    Thanks for reading this post. I'm developing an application based on libmpv. I use the function mpv_set_wakeup_callback() to set a callback function for mpv events. This will open a separate thread. That's all I know. When MainWindow is closed, the application remains in memory until killed forecfully. I have no idea how to close all threads. I have tried mpv_terminate_destroy but still the same. Any idea is highly appreciated. Thanks in advance.


  • Lifetime Qt Champion

    @Aario This is Qt forum: is your question somehow related to Qt?



  • Yes. The problem was with Qt internals conflicting with mpv library's way of multi threading. I have solved the problem by using a QT alternative to mpv_set_wakeup_callback. My solution is available at:
    https://github.com/aario/qtible/blob/main/mediaplayer/mpveventloop.cpp

    void MPVEventLoop::eventLoop()
    {
        qInfo() << "Started event loop";
        while (1) {
            if (mustShutdown)
                break;
    
            mpv_event *event = mpv_wait_event(mpv, 0.01);
    
            switch (event->event_id) {
            case MPV_EVENT_NONE:
                break;
            case MPV_EVENT_FILE_LOADED:
                emit fileLoaded();
                break;
            case MPV_EVENT_END_FILE:
                emit endFile();
                break;
            case MPV_EVENT_PROPERTY_CHANGE: {
                mpv_event_property *property = (mpv_event_property *)event->data;
                if (strcmp(property->name, "time-pos") == 0) {
                    if (property->format == MPV_FORMAT_DOUBLE) {
                        emit timePosChanged(*(double *)property->data);
                    }
                }
                break;
            }
            default: qDebug() << "Ignoring MPV event: " << event->event_id;
            }
        }
    }
    

    And then:

        mpvEventLoop = new MPVEventLoop(mpv);
        mpvEventLoop->moveToThread(&mpvEventLoopThread);
        connect(&mpvEventLoopThread, &QThread::finished, mpvEventLoop, &QObject::deleteLater);
        connect(this, &MediaPlayer::startMPVEventLoop, mpvEventLoop, &MPVEventLoop::eventLoop);
        connect(mpvEventLoop, &MPVEventLoop::fileLoaded, this, &MediaPlayer::onMpvFileLoaded);
        connect(mpvEventLoop, &MPVEventLoop::endFile, this, &MediaPlayer::onMpvEndFile);
        connect(mpvEventLoop, &MPVEventLoop::timePosChanged, this, &MediaPlayer::onMpvTimePosChanged);
        mpvEventLoopThread.start();
        emit startMPVEventLoop();
    

Log in to reply