summaryrefslogtreecommitdiffstats
path: root/src/multimedia/video/qabstractvideosurface.cpp
blob: 139024d23078212d971cd1721028019a0c4d71d3 (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
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
/****************************************************************************
**
** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
** All rights reserved.
** Contact: Nokia Corporation (qt-info@nokia.com)
**
** This file is part of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:LGPL$
** GNU Lesser General Public License Usage
** This file may be used under the terms of the GNU Lesser General Public
** License version 2.1 as published by the Free Software Foundation and
** appearing in the file LICENSE.LGPL included in the packaging of this
** file. Please review the following information to ensure the GNU Lesser
** General Public License version 2.1 requirements will be met:
** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** In addition, as a special exception, Nokia gives you certain additional
** rights. These rights are described in the Nokia Qt LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU General
** Public License version 3.0 as published by the Free Software Foundation
** and appearing in the file LICENSE.GPL included in the packaging of this
** file. Please review the following information to ensure the GNU General
** Public License version 3.0 requirements will be met:
** http://www.gnu.org/copyleft/gpl.html.
**
** Other Usage
** Alternatively, this file may be used in accordance with the terms and
** conditions contained in a signed written agreement between you and Nokia.
**
**
**
**
**
** $QT_END_LICENSE$
**
****************************************************************************/

//TESTED_COMPONENT=src/multimedia

#include "qabstractvideosurface.h"

#include "qvideosurfaceformat.h"

#include <QtCore/qvariant.h>
#include <QDebug>

QT_BEGIN_NAMESPACE

namespace
{
    class QAbstractVideoSurfacePrivateRegisterMetaTypes
    {
    public:
        QAbstractVideoSurfacePrivateRegisterMetaTypes()
        {
            qRegisterMetaType<QAbstractVideoSurface::Error>();
        }
    } _registerMetaTypes;
}


class QAbstractVideoSurfacePrivate {
public:
    QAbstractVideoSurfacePrivate()
        : error(QAbstractVideoSurface::NoError),
          active(false)
    {
    }

public:
    QVideoSurfaceFormat surfaceFormat;
    QAbstractVideoSurface::Error error;
    QSize nativeResolution;
    bool active;
};

/*!
    \class QAbstractVideoSurface
    \brief The QAbstractVideoSurface class is a base class for video presentation surfaces.
    \since 1.0
    \inmodule QtMultimedia

    A video surface presents a continuous stream of identically formatted frames, where the format
    of each frame is compatible with a stream format supplied when starting a presentation.

    The QAbstractVideoSurface class defines the standard interface that video producers use to
    inter-operate with video presentation surfaces.  It is not supposed to be instantiated directly.
    Instead, you should subclass it to create new video surfaces.

    A list of pixel formats a surface can present is given by the supportedPixelFormats() function,
    and the isFormatSupported() function will test if a video surface format is supported.  If a
    format is not supported the nearestFormat() function may be able to suggest a similar format.
    For example, if a surface supports fixed set of resolutions it may suggest the smallest
    supported resolution that contains the proposed resolution.

    The start() function takes a supported format and enables a video surface.  Once started a
    surface will begin displaying the frames it receives in the present() function.  Surfaces may
    hold a reference to the buffer of a presented video frame until a new frame is presented or
    streaming is stopped. The stop() function will disable a surface and a release any video
    buffers it holds references to.
*/

/*!
    \enum QAbstractVideoSurface::Error
    This enum describes the errors that may be returned by the error() function.

    \value NoError No error occurred.
    \value UnsupportedFormatError A video format was not supported.
    \value IncorrectFormatError A video frame was not compatible with the format of the surface.
    \value StoppedError The surface has not been started.
    \value ResourceError The surface could not allocate some resource.
*/

/*!
    Constructs a video surface with the given \a parent.
*/
QAbstractVideoSurface::QAbstractVideoSurface(QObject *parent)
    : QObject(parent),
      d_ptr(new QAbstractVideoSurfacePrivate)
{
}

/*!
    Destroys a video surface.
*/
QAbstractVideoSurface::~QAbstractVideoSurface()
{
}

/*!
    \fn QAbstractVideoSurface::supportedPixelFormats(QAbstractVideoBuffer::HandleType type) const

    Returns a list of pixel formats a video surface can present for a given handle \a type.

    The pixel formats returned for the QAbstractVideoBuffer::NoHandle type are valid for any buffer
    that can be mapped in read-only mode.

    Types that are first in the list can be assumed to be faster to render.
    \since 1.0
*/

/*!
    Tests a video surface \a format to determine if a surface can accept it.

    Returns true if the format is supported by the surface, and false otherwise.
    \since 1.0
*/
bool QAbstractVideoSurface::isFormatSupported(const QVideoSurfaceFormat &format) const
{
    return supportedPixelFormats(format.handleType()).contains(format.pixelFormat());
}

/*!
    Returns a supported video surface format that is similar to \a format.

    A similar surface format is one that has the same \l {QVideoSurfaceFormat::pixelFormat()}{pixel
    format} and \l {QVideoSurfaceFormat::handleType()}{handle type} but may differ in some of the other
    properties.  For example, if there are restrictions on the \l {QVideoSurfaceFormat::frameSize()}
    {frame sizes} a video surface can accept it may suggest a format with a larger frame size and
    a \l {QVideoSurfaceFormat::viewport()}{viewport} the size of the original frame size.

    If the format is already supported it will be returned unchanged, or if there is no similar
    supported format an invalid format will be returned.
    \since 1.0
*/
QVideoSurfaceFormat QAbstractVideoSurface::nearestFormat(const QVideoSurfaceFormat &format) const
{
    return isFormatSupported(format)
            ? format
            : QVideoSurfaceFormat();
}

/*!
    \fn QAbstractVideoSurface::supportedFormatsChanged()

    Signals that the set of formats supported by a video surface has changed.

    \since 1.0
    \sa supportedPixelFormats(), isFormatSupported()
*/

/*!
    Returns the format of a video surface.
    \since 1.0
*/
QVideoSurfaceFormat QAbstractVideoSurface::surfaceFormat() const
{
    Q_D(const QAbstractVideoSurface);
    return d->surfaceFormat;
}

/*!
    \fn QAbstractVideoSurface::surfaceFormatChanged(const QVideoSurfaceFormat &format)

    Signals that the configured \a format of a video surface has changed.

    \since 1.0
    \sa surfaceFormat(), start()
*/

/*!
    Starts a video surface presenting \a format frames.

    Returns true if the surface was started, and false if an error occurred.

    \since 1.0
    \sa isActive(), stop()
*/
bool QAbstractVideoSurface::start(const QVideoSurfaceFormat &format)
{
    Q_D(QAbstractVideoSurface);
    bool wasActive  = d->active;

    d->active = true;
    d->surfaceFormat = format;
    d->error = NoError;

    emit surfaceFormatChanged(format);

    if (!wasActive)
        emit activeChanged(true);

    return true;
}

/*!
    Stops a video surface presenting frames and releases any resources acquired in start().

    \since 1.0
    \sa isActive(), start()
*/
void QAbstractVideoSurface::stop()
{
    Q_D(QAbstractVideoSurface);
    if (d->active) {
        d->surfaceFormat = QVideoSurfaceFormat();
        d->active = false;

        emit activeChanged(false);
        emit surfaceFormatChanged(surfaceFormat());
    }
}

/*!
    Indicates whether a video surface has been started.

    Returns true if the surface has been started, and false otherwise.
    \since 1.0
*/
bool QAbstractVideoSurface::isActive() const
{
    Q_D(const QAbstractVideoSurface);
    return d->active;
}

/*!
    \fn QAbstractVideoSurface::activeChanged(bool active)

    Signals that the \a active state of a video surface has changed.

    \since 1.0
    \sa isActive(), start(), stop()
*/

/*!
    \fn QAbstractVideoSurface::present(const QVideoFrame &frame)

    Presents a video \a frame.

    Returns true if the frame was presented, and false if an error occurred.

    Not all surfaces will block until the presentation of a frame has completed.  Calling present()
    on a non-blocking surface may fail if called before the presentation of a previous frame has
    completed.  In such cases the surface may not return to a ready state until it has had an
    opportunity to process events.

    If present() fails for any other reason the surface will immediately enter the stopped state
    and an error() value will be set.

    A video surface must be in the started state for present() to succeed, and the format of the
    video frame must be compatible with the current video surface format.

    \since 1.0
    \sa error()
*/

/*!
    Returns the last error that occurred.

    If a surface fails to start(), or stops unexpectedly this function can be called to discover
    what error occurred.
    \since 1.0
*/

QAbstractVideoSurface::Error QAbstractVideoSurface::error() const
{
    Q_D(const QAbstractVideoSurface);
    return d->error;
}

/*!
    Sets the value of error() to \a error.
    \since 1.0
*/
void QAbstractVideoSurface::setError(Error error)
{
    Q_D(QAbstractVideoSurface);
    d->error = error;
}

/*!
   \property QAbstractVideoSurface::nativeResolution

   The native resolution of video surface.
   This is the resolution of video frames the surface
   can render with optimal quality and/or performance.

   The native resolution is not always known and can be changed during playback.
    \since 1.1
 */
QSize QAbstractVideoSurface::nativeResolution() const
{
    Q_D(const QAbstractVideoSurface);
    return d->nativeResolution;
}

/*!
    Set the video surface native \a resolution.
    \since 1.1
 */
void QAbstractVideoSurface::setNativeResolution(const QSize &resolution)
{
    Q_D(QAbstractVideoSurface);

    if (d->nativeResolution != resolution) {
        d->nativeResolution = resolution;

        emit nativeResolutionChanged(resolution);
    }
}
/*!
    \fn QAbstractVideoSurface::nativeResolutionChanged(const QSize &resolution);

    Signals the native \a resolution of video surface has changed.
    \since 1.1
*/

#ifndef QT_NO_DEBUG_STREAM
QDebug operator<<(QDebug dbg, const QAbstractVideoSurface::Error& error)
{
    switch (error) {
    case QAbstractVideoSurface::UnsupportedFormatError:
        return dbg.nospace() << "UnsupportedFormatError";
    case QAbstractVideoSurface::IncorrectFormatError:
        return dbg.nospace() << "IncorrectFormatError";
    case QAbstractVideoSurface::StoppedError:
        return dbg.nospace() << "StoppedError";
    case QAbstractVideoSurface::ResourceError:
        return dbg.nospace() << "ResourceError";
    default:
        return dbg.nospace() << "NoError";
    }
}
#endif


QT_END_NAMESPACE

#include "moc_qabstractvideosurface.cpp"