基于qt,msvc2017-64bits,ffmpeg.opengl的播放器
chenshijun
2020-12-03 9e8804424408db79fcdb229a016ac87952e4e0f6
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
#include "FunctionTransfer.h"
 
#include <QThread>
#include <QDebug>
 
Qt::HANDLE FunctionTransfer::gMainThreadId = nullptr;
FunctionTransfer *FunctionTransfer::main_thread_forward = nullptr;
 
Q_DECLARE_METATYPE(std::function<void()>)
 
FunctionTransfer::FunctionTransfer(QObject *parent) :
    QObject(parent)
{
    //ÒòΪstd::function<void()>ÊÇ×Ô¶¨ÒåµÄÀàÐÍ Òª¿çÏ̴߳«µÝÐèÒªÏÈ×¢²áÒ»ÏÂ
    qRegisterMetaType<std::function<void()>>();
 
    connect(this, SIGNAL(comming(std::function<void()>)), this, SLOT(slotExec(std::function<void()>)), Qt::BlockingQueuedConnection);
    connect(this, SIGNAL(comming_noBlock(std::function<void()>)), this, SLOT(slotExec(std::function<void()>)), Qt::QueuedConnection);
}
 
FunctionTransfer::~FunctionTransfer()
{
qDebug()<<__FUNCTION__;
}
 
void FunctionTransfer::init(Qt::HANDLE id)
{
    gMainThreadId = id;
    FunctionTransfer::main_thread_forward = new FunctionTransfer();
}
 
bool FunctionTransfer::isMainThread()
{
    if (gMainThreadId == nullptr)
    {
        qDebug()<<__FILE__<<__LINE__<<__FUNCTION__<<"the main thread id is not set!";
        return false;
    }
 
    if (QThread::currentThreadId() == gMainThreadId)
    {
        return true;
    }
    else
    {
        return false;
    }
}
 
void FunctionTransfer::runInMainThread(std::function<void()> f, bool isBlock)
{
//    FunctionTransfer::main_thread_forward->exec(f, isBlock);
    if(FunctionTransfer::isMainThread())
    {
        f();
    }
    else
    {
        if (isBlock)
        {
            Q_EMIT FunctionTransfer::main_thread_forward->comming(f);
        }
        else
        {
            Q_EMIT FunctionTransfer::main_thread_forward->comming_noBlock(f);
        }
    }
}
 
void FunctionTransfer::slotExec(std::function<void()> f)
{
    f();
//    if(FunctionTransfer::isMainThread())
//    {
//        f();
//    }
//    else
//    {
//        if (isBlock)
//        {
//            Q_EMIT this->comming(f);
//        }
//        else
//        {
//            Q_EMIT this->comming_noBlock(f);
//        }
//    }
}