1 | /**************************************************************************** |
2 | ** |
3 | ** Copyright (C) 2017 The Qt Company Ltd. |
4 | ** Contact: https://www.qt.io/licensing/ |
5 | ** |
6 | ** This file is part of the examples of the Qt Toolkit. |
7 | ** |
8 | ** $QT_BEGIN_LICENSE:BSD$ |
9 | ** Commercial License Usage |
10 | ** Licensees holding valid commercial Qt licenses may use this file in |
11 | ** accordance with the commercial license agreement provided with the |
12 | ** Software or, alternatively, in accordance with the terms contained in |
13 | ** a written agreement between you and The Qt Company. For licensing terms |
14 | ** and conditions see https://www.qt.io/terms-conditions. For further |
15 | ** information use the contact form at https://www.qt.io/contact-us. |
16 | ** |
17 | ** BSD License Usage |
18 | ** Alternatively, you may use this file under the terms of the BSD license |
19 | ** as follows: |
20 | ** |
21 | ** "Redistribution and use in source and binary forms, with or without |
22 | ** modification, are permitted provided that the following conditions are |
23 | ** met: |
24 | ** * Redistributions of source code must retain the above copyright |
25 | ** notice, this list of conditions and the following disclaimer. |
26 | ** * Redistributions in binary form must reproduce the above copyright |
27 | ** notice, this list of conditions and the following disclaimer in |
28 | ** the documentation and/or other materials provided with the |
29 | ** distribution. |
30 | ** * Neither the name of The Qt Company Ltd nor the names of its |
31 | ** contributors may be used to endorse or promote products derived |
32 | ** from this software without specific prior written permission. |
33 | ** |
34 | ** |
35 | ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
36 | ** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
37 | ** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
38 | ** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
39 | ** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
40 | ** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
41 | ** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
42 | ** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
43 | ** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
44 | ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
45 | ** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." |
46 | ** |
47 | ** $QT_END_LICENSE$ |
48 | ** |
49 | ****************************************************************************/ |
50 | |
51 | #include "videowidgetsurface.h" |
52 | |
53 | #include <QtWidgets> |
54 | #include <qabstractvideosurface.h> |
55 | #include <qvideosurfaceformat.h> |
56 | |
57 | VideoWidgetSurface::VideoWidgetSurface(QWidget *widget, QObject *parent) |
58 | : QAbstractVideoSurface(parent) |
59 | , widget(widget) |
60 | , imageFormat(QImage::Format_Invalid) |
61 | { |
62 | } |
63 | |
64 | //! [0] |
65 | QList<QVideoFrame::PixelFormat> VideoWidgetSurface::supportedPixelFormats( |
66 | QAbstractVideoBuffer::HandleType handleType) const |
67 | { |
68 | if (handleType == QAbstractVideoBuffer::NoHandle) { |
69 | return QList<QVideoFrame::PixelFormat>() |
70 | << QVideoFrame::Format_RGB32 |
71 | << QVideoFrame::Format_ARGB32 |
72 | << QVideoFrame::Format_ARGB32_Premultiplied |
73 | << QVideoFrame::Format_RGB565 |
74 | << QVideoFrame::Format_RGB555; |
75 | } else { |
76 | return QList<QVideoFrame::PixelFormat>(); |
77 | } |
78 | } |
79 | //! [0] |
80 | |
81 | //! [1] |
82 | bool VideoWidgetSurface::isFormatSupported(const QVideoSurfaceFormat &format) const |
83 | { |
84 | const QImage::Format imageFormat = QVideoFrame::imageFormatFromPixelFormat(format: format.pixelFormat()); |
85 | const QSize size = format.frameSize(); |
86 | |
87 | return imageFormat != QImage::Format_Invalid |
88 | && !size.isEmpty() |
89 | && format.handleType() == QAbstractVideoBuffer::NoHandle; |
90 | } |
91 | //! [1] |
92 | |
93 | //! [2] |
94 | bool VideoWidgetSurface::start(const QVideoSurfaceFormat &format) |
95 | { |
96 | const QImage::Format imageFormat = QVideoFrame::imageFormatFromPixelFormat(format: format.pixelFormat()); |
97 | const QSize size = format.frameSize(); |
98 | |
99 | if (imageFormat != QImage::Format_Invalid && !size.isEmpty()) { |
100 | this->imageFormat = imageFormat; |
101 | imageSize = size; |
102 | sourceRect = format.viewport(); |
103 | |
104 | QAbstractVideoSurface::start(format); |
105 | |
106 | widget->updateGeometry(); |
107 | updateVideoRect(); |
108 | |
109 | return true; |
110 | } else { |
111 | return false; |
112 | } |
113 | } |
114 | //! [2] |
115 | |
116 | //! [3] |
117 | void VideoWidgetSurface::stop() |
118 | { |
119 | currentFrame = QVideoFrame(); |
120 | targetRect = QRect(); |
121 | |
122 | QAbstractVideoSurface::stop(); |
123 | |
124 | widget->update(); |
125 | } |
126 | //! [3] |
127 | |
128 | //! [4] |
129 | bool VideoWidgetSurface::present(const QVideoFrame &frame) |
130 | { |
131 | if (surfaceFormat().pixelFormat() != frame.pixelFormat() |
132 | || surfaceFormat().frameSize() != frame.size()) { |
133 | setError(IncorrectFormatError); |
134 | stop(); |
135 | |
136 | return false; |
137 | } else { |
138 | currentFrame = frame; |
139 | |
140 | widget->repaint(targetRect); |
141 | |
142 | return true; |
143 | } |
144 | } |
145 | //! [4] |
146 | |
147 | //! [5] |
148 | void VideoWidgetSurface::updateVideoRect() |
149 | { |
150 | QSize size = surfaceFormat().sizeHint(); |
151 | size.scale(s: widget->size().boundedTo(otherSize: size), mode: Qt::KeepAspectRatio); |
152 | |
153 | targetRect = QRect(QPoint(0, 0), size); |
154 | targetRect.moveCenter(p: widget->rect().center()); |
155 | } |
156 | //! [5] |
157 | |
158 | //! [6] |
159 | void VideoWidgetSurface::paint(QPainter *painter) |
160 | { |
161 | if (currentFrame.map(mode: QAbstractVideoBuffer::ReadOnly)) { |
162 | const QTransform oldTransform = painter->transform(); |
163 | |
164 | if (surfaceFormat().scanLineDirection() == QVideoSurfaceFormat::BottomToTop) { |
165 | painter->scale(sx: 1, sy: -1); |
166 | painter->translate(dx: 0, dy: -widget->height()); |
167 | } |
168 | |
169 | QImage image( |
170 | currentFrame.bits(), |
171 | currentFrame.width(), |
172 | currentFrame.height(), |
173 | currentFrame.bytesPerLine(), |
174 | imageFormat); |
175 | |
176 | painter->drawImage(targetRect, image, sourceRect); |
177 | |
178 | painter->setTransform(transform: oldTransform); |
179 | |
180 | currentFrame.unmap(); |
181 | } |
182 | } |
183 | //! [6] |
184 | |