summaryrefslogtreecommitdiff
path: root/src/plugins/qnx/mediaplayer/ppsmediaplayercontrol.cpp
blob: de5e3e0cf1c89b125a67b287ae3668aedb4b541f (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
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
/****************************************************************************
**
** Copyright (C) 2016 Research In Motion
** Contact: https://www.qt.io/licensing/
**
** This file is part of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:LGPL$
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 3 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL3 included in the
** packaging of this file. Please review the following information to
** ensure the GNU Lesser General Public License version 3 requirements
** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 2.0 or (at your option) the GNU General
** Public license version 3 or any later version approved by the KDE Free
** Qt Foundation. The licenses are as published by the Free Software
** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
** included in the packaging of this file. Please review the following
** information to ensure the GNU General Public License requirements will
** be met: https://www.gnu.org/licenses/gpl-2.0.html and
** https://www.gnu.org/licenses/gpl-3.0.html.
**
** $QT_END_LICENSE$
**
****************************************************************************/

#include "ppsmediaplayercontrol.h"
#include "mmrenderervideowindowcontrol.h"

#include <QtCore/qfile.h>
#include <QtCore/qsocketnotifier.h>
#include <QtCore/private/qcore_unix_p.h>

#include <screen/screen.h>
#include <sys/pps.h>

QT_BEGIN_NAMESPACE

PpsMediaPlayerControl::PpsMediaPlayerControl(QObject *parent)
    : MmRendererMediaPlayerControl(parent),
    m_ppsStatusNotifier(0),
    m_ppsStatusFd(-1),
    m_ppsStateNotifier(0),
    m_ppsStateFd(-1)
  , m_previouslySeenState("stopped")
{
    openConnection();
}

PpsMediaPlayerControl::~PpsMediaPlayerControl()
{
    destroy();
}

void PpsMediaPlayerControl::startMonitoring(int, const QString &contextName)
{
    const QString ppsContextPath = QStringLiteral("/pps/services/multimedia/renderer/context/%1/").arg(contextName);
    const QString ppsStatusPath = ppsContextPath + QStringLiteral("/status");

    Q_ASSERT(m_ppsStatusFd == -1);
    errno = 0;
    m_ppsStatusFd = qt_safe_open(QFile::encodeName(ppsStatusPath).constData(), O_RDONLY);
    if (m_ppsStatusFd == -1) {
        emitPError(QStringLiteral("Unable to open %1: %2").arg(ppsStatusPath, qt_error_string(errno)));
        return;
    }

    Q_ASSERT(!m_ppsStatusNotifier);
    m_ppsStatusNotifier = new QSocketNotifier(m_ppsStatusFd, QSocketNotifier::Read);
    connect(m_ppsStatusNotifier, SIGNAL(activated(int)), this, SLOT(ppsReadyRead(int)));


    const QString ppsStatePath = ppsContextPath + QStringLiteral("/state");

    Q_ASSERT(m_ppsStateFd == -1);
    errno = 0;
    m_ppsStateFd = qt_safe_open(QFile::encodeName(ppsStatePath).constData(), O_RDONLY);
    if (m_ppsStateFd == -1) {
        emitPError(QStringLiteral("Unable to open %1: %2").arg(ppsStatePath, qt_error_string(errno)));
        return;
    }

    Q_ASSERT(!m_ppsStateNotifier);
    m_ppsStateNotifier = new QSocketNotifier(m_ppsStateFd, QSocketNotifier::Read);
    connect(m_ppsStateNotifier, SIGNAL(activated(int)), this, SLOT(ppsReadyRead(int)));

    //ensure we receive any initial state
    ppsReadyRead(m_ppsStatusFd);
    ppsReadyRead(m_ppsStateFd);
}

void PpsMediaPlayerControl::stopMonitoring()
{

    if (m_ppsStatusFd != -1) {
        ::close(m_ppsStatusFd);
        m_ppsStatusFd = -1;
    }

    delete m_ppsStatusNotifier;
    m_ppsStatusNotifier = 0;

    if (m_ppsStateFd != -1) {
        ::close(m_ppsStateFd);
        m_ppsStateFd = -1;
    }

    delete m_ppsStateNotifier;
    m_ppsStateNotifier = 0;
}

bool PpsMediaPlayerControl::nativeEventFilter(const QByteArray &eventType, void *message, long *result)
{
    Q_UNUSED(result)
    if (eventType == "screen_event_t") {
        screen_event_t event = static_cast<screen_event_t>(message);
        if (MmRendererVideoWindowControl *control = videoWindowControl())
            control->screenEventHandler(event);
    }

    return false;
}

void PpsMediaPlayerControl::ppsReadyRead(int fd)
{
    Q_ASSERT(fd == m_ppsStateFd || fd == m_ppsStatusFd);
    const int bufferSize = 2048;
    char buffer[bufferSize];
    const ssize_t nread = qt_safe_read(fd, buffer, bufferSize - 1);
    if (nread < 0) {
        //TODO emit error?
    }

    if (nread == 0) {
        return;
    }

    // nread is the real space necessary, not the amount read.
    if (static_cast<size_t>(nread) > bufferSize - 1) {
        //TODO emit error?
        qCritical("BBMediaPlayerControl: PPS buffer size too short; need %u.", nread + 1);
        return;
    }

    buffer[nread] = 0;

    pps_decoder_t decoder;

    if (pps_decoder_initialize(&decoder, buffer) != PPS_DECODER_OK) {
        //TODO emit error?
        qCritical("Could not initialize pps_decoder");
        pps_decoder_cleanup(&decoder);
        return;
    }

    pps_decoder_push(&decoder, 0);

    const char *value = 0;

    if (pps_decoder_get_string(&decoder, "bufferstatus", &value) == PPS_DECODER_OK)
        setMmBufferStatus(QString::fromLatin1(value));

    if (pps_decoder_get_string(&decoder, "bufferlevel", &value) == PPS_DECODER_OK)
        setMmBufferLevel(QString::fromLatin1(value));

    if (pps_decoder_get_string(&decoder, "state", &value) == PPS_DECODER_OK) {
        const QByteArray state = value;
        if (state != m_previouslySeenState && state == "stopped")
            handleMmStopped();
        m_previouslySeenState = state;
    }

    if (pps_decoder_get_string(&decoder, "position", &value) == PPS_DECODER_OK) {
        const QByteArray valueBa = QByteArray(value);
        bool ok;
        const qint64 position = valueBa.toLongLong(&ok);
        if (!ok) {
            qCritical("Could not parse position from '%s'", valueBa.constData());
        } else {
            setMmPosition(position);
        }
    }

    pps_decoder_cleanup(&decoder);
}

QT_END_NAMESPACE