summaryrefslogtreecommitdiff
path: root/src/QGst/bus.cpp
blob: 7f38e01dd9d15ee043798ddef2d8db9af1762aa3 (plain)
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
/*
    Copyright (C) 2010  George Kiagiadakis <kiagiadakis.george@gmail.com>

    This library is free software; you can redistribute it and/or modify
    it under the terms of the GNU Lesser General Public License as published
    by the Free Software Foundation; either version 2.1 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU Lesser General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/
#include "bus.h"
#include "message.h"
#include "../QGlib/signal.h"
#include <gst/gst.h>
#include <QtCore/QObject>
#include <QtCore/QTimerEvent>
#include <QtCore/QHash>
#include <QtCore/QBasicTimer>

namespace QGst {
namespace Private {

class BusWatch : public QObject
{
public:
    BusWatch(GstBus *bus)
        : QObject(), m_bus(bus)
    {
        m_timer.start(50, this);
    }

    void stop()
    {
        m_timer.stop();
    }

private:
    virtual void timerEvent(QTimerEvent *event)
    {
        if (event->timerId() == m_timer.timerId()) {
            dispatch();
        } else {
            QObject::timerEvent(event);
        }
    }

    void dispatch()
    {
        GstMessage *message;
        gst_object_ref(m_bus);
        while((message = gst_bus_pop(m_bus)) != NULL) {
            MessagePtr msg = MessagePtr::wrap(message, false);
            QGlib::Quark detail = gst_message_type_to_quark(static_cast<GstMessageType>(msg->type()));
            QGlib::emitWithDetail<void>(m_bus, "message", detail, msg);
        }
        gst_object_unref(m_bus);
    }

    GstBus *m_bus;
    QBasicTimer m_timer;
};

class BusWatchManager
{
public:
    void addWatch(GstBus *bus)
    {
        if (m_watches.contains(bus)) {
            m_watches[bus].second++; //reference count
        } else {
            m_watches.insert(bus, qMakePair(new BusWatch(bus), uint(1)));
            g_object_weak_ref(G_OBJECT(bus), &BusWatchManager::onBusDestroyed, this);
        }
    }

    void removeWatch(GstBus *bus)
    {
        if (m_watches.contains(bus) && --m_watches[bus].second == 0) {
            m_watches[bus].first->stop();
            m_watches[bus].first->deleteLater();
            m_watches.remove(bus);
            g_object_weak_unref(G_OBJECT(bus), &BusWatchManager::onBusDestroyed, this);
        }
    }

private:
    static void onBusDestroyed(gpointer selfPtr, GObject *busPtr)
    {
        BusWatchManager *self = static_cast<BusWatchManager*>(selfPtr);
        GstBus *bus = reinterpret_cast<GstBus*>(busPtr);

        //we cannot call removeWatch() here because g_object_weak_unref will complain
        self->m_watches[bus].first->stop();
        self->m_watches[bus].first->deleteLater();
        self->m_watches.remove(bus);
    }

    QHash< GstBus*, QPair<BusWatch*, uint> > m_watches;
};

Q_GLOBAL_STATIC(Private::BusWatchManager, s_watchManager)

} //namespace Private


//static
BusPtr Bus::create()
{
    GstBus *bus = gst_bus_new();
    if (bus) {
        gst_object_ref_sink(bus);
    }
    return BusPtr::wrap(bus, false);
}

bool Bus::hasPendingMessages() const
{
    return gst_bus_have_pending(object<GstBus>());
}

MessagePtr Bus::peek() const
{
    return MessagePtr::wrap(gst_bus_peek(object<GstBus>()), false);
}

MessagePtr Bus::pop(ClockTime timeout)
{
    return MessagePtr::wrap(gst_bus_timed_pop(object<GstBus>(), timeout), false);
}

MessagePtr Bus::pop(MessageType type, ClockTime timeout)
{
    return MessagePtr::wrap(gst_bus_timed_pop_filtered(object<GstBus>(), timeout,
                                                       static_cast<GstMessageType>(type)), false);
}

bool Bus::post(const MessagePtr & message)
{
    return gst_bus_post(object<GstBus>(), gst_message_copy(message));
}

void Bus::setFlushing(bool flush)
{
    gst_bus_set_flushing(object<GstBus>(), flush);
}

void Bus::addSignalWatch()
{
    Private::s_watchManager()->addWatch(object<GstBus>());
}

void Bus::removeSignalWatch()
{
    Private::s_watchManager()->removeWatch(object<GstBus>());
}

void Bus::enableSyncMessageEmission()
{
    gst_bus_enable_sync_message_emission(object<GstBus>());
}

void Bus::disableSyncMessageEmission()
{
    gst_bus_disable_sync_message_emission(object<GstBus>());
}

} //namespace QGst