|
1 /**************************************************************************** |
|
2 ** |
|
3 ** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies). |
|
4 ** All rights reserved. |
|
5 ** Contact: Nokia Corporation (qt-info@nokia.com) |
|
6 ** |
|
7 ** This file is part of the Qt Mobility Components. |
|
8 ** |
|
9 ** $QT_BEGIN_LICENSE:LGPL$ |
|
10 ** No Commercial Usage |
|
11 ** This file contains pre-release code and may not be distributed. |
|
12 ** You may use this file in accordance with the terms and conditions |
|
13 ** contained in the Technology Preview License Agreement accompanying |
|
14 ** this package. |
|
15 ** |
|
16 ** GNU Lesser General Public License Usage |
|
17 ** Alternatively, this file may be used under the terms of the GNU Lesser |
|
18 ** General Public License version 2.1 as published by the Free Software |
|
19 ** Foundation and appearing in the file LICENSE.LGPL included in the |
|
20 ** packaging of this file. Please review the following information to |
|
21 ** ensure the GNU Lesser General Public License version 2.1 requirements |
|
22 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. |
|
23 ** |
|
24 ** In addition, as a special exception, Nokia gives you certain additional |
|
25 ** rights. These rights are described in the Nokia Qt LGPL Exception |
|
26 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. |
|
27 ** |
|
28 ** If you have questions regarding the use of this file, please contact |
|
29 ** Nokia at qt-info@nokia.com. |
|
30 ** |
|
31 ** |
|
32 ** |
|
33 ** |
|
34 ** |
|
35 ** |
|
36 ** |
|
37 ** |
|
38 ** $QT_END_LICENSE$ |
|
39 ** |
|
40 ****************************************************************************/ |
|
41 |
|
42 #import <QTKit/QTKit.h> |
|
43 |
|
44 #include "qt7backend.h" |
|
45 |
|
46 #include "qt7playercontrol.h" |
|
47 #include "qt7movieviewrenderer.h" |
|
48 #include "qt7playersession.h" |
|
49 #include "qt7ciimagevideobuffer.h" |
|
50 #include <QtCore/qdebug.h> |
|
51 #include <QtCore/qcoreevent.h> |
|
52 #include <QtCore/qcoreapplication.h> |
|
53 |
|
54 #include <qabstractvideobuffer.h> |
|
55 #include <qabstractvideosurface.h> |
|
56 #include <qvideosurfaceformat.h> |
|
57 |
|
58 #include <QuartzCore/CIFilter.h> |
|
59 #include <QuartzCore/CIVector.h> |
|
60 |
|
61 QT_USE_NAMESPACE |
|
62 |
|
63 class NSBitmapVideoBuffer : public QAbstractVideoBuffer |
|
64 { |
|
65 public: |
|
66 NSBitmapVideoBuffer(NSBitmapImageRep *buffer) |
|
67 : QAbstractVideoBuffer(NoHandle) |
|
68 , m_buffer(buffer) |
|
69 , m_mode(NotMapped) |
|
70 { |
|
71 [m_buffer retain]; |
|
72 } |
|
73 |
|
74 virtual ~NSBitmapVideoBuffer() |
|
75 { |
|
76 [m_buffer release]; |
|
77 } |
|
78 |
|
79 MapMode mapMode() const { return m_mode; } |
|
80 |
|
81 uchar *map(MapMode mode, int *numBytes, int *bytesPerLine) |
|
82 { |
|
83 if (mode != NotMapped && m_mode == NotMapped) { |
|
84 if (numBytes) |
|
85 *numBytes = [m_buffer bytesPerPlane]; |
|
86 |
|
87 if (bytesPerLine) |
|
88 *bytesPerLine = [m_buffer bytesPerRow]; |
|
89 |
|
90 m_mode = mode; |
|
91 |
|
92 return [m_buffer bitmapData]; |
|
93 } else { |
|
94 return 0; |
|
95 } |
|
96 } |
|
97 |
|
98 void unmap() { m_mode = NotMapped; } |
|
99 |
|
100 private: |
|
101 NSBitmapImageRep *m_buffer; |
|
102 MapMode m_mode; |
|
103 }; |
|
104 |
|
105 |
|
106 #define VIDEO_TRANSPARENT(m) -(void)m:(NSEvent *)e{[[self superview] m:e];} |
|
107 |
|
108 @interface HiddenQTMovieView : QTMovieView |
|
109 { |
|
110 @private |
|
111 QWidget *m_window; |
|
112 QT7MovieViewRenderer *m_renderer; |
|
113 } |
|
114 |
|
115 - (HiddenQTMovieView *) initWithRenderer:(QT7MovieViewRenderer *)renderer; |
|
116 - (void) setRenderer:(QT7MovieViewRenderer *)renderer; |
|
117 - (void) setDrawRect:(const QRect &)rect; |
|
118 - (CIImage *) view:(QTMovieView *)view willDisplayImage:(CIImage *)img; |
|
119 @end |
|
120 |
|
121 @implementation HiddenQTMovieView |
|
122 |
|
123 - (HiddenQTMovieView *) initWithRenderer:(QT7MovieViewRenderer *)renderer |
|
124 { |
|
125 self = [super initWithFrame:NSZeroRect]; |
|
126 if (self) { |
|
127 [self setControllerVisible:NO]; |
|
128 [self setDelegate:self]; |
|
129 |
|
130 self->m_renderer = renderer; |
|
131 |
|
132 self->m_window = new QWidget; |
|
133 self->m_window->setWindowOpacity(0.0); |
|
134 self->m_window->show(); |
|
135 self->m_window->hide(); |
|
136 |
|
137 [(NSView *)(self->m_window->winId()) addSubview:self]; |
|
138 [self setDrawRect:QRect(0,0,1,1)]; |
|
139 } |
|
140 return self; |
|
141 } |
|
142 |
|
143 - (void) dealloc |
|
144 { |
|
145 [super dealloc]; |
|
146 } |
|
147 |
|
148 - (void) setRenderer:(QT7MovieViewRenderer *)renderer |
|
149 { |
|
150 m_renderer = renderer; |
|
151 } |
|
152 |
|
153 - (void) setDrawRect:(const QRect &)rect |
|
154 { |
|
155 NSRect nsrect; |
|
156 nsrect.origin.x = rect.x(); |
|
157 nsrect.origin.y = rect.y(); |
|
158 nsrect.size.width = rect.width(); |
|
159 nsrect.size.height = rect.height(); |
|
160 [self setFrame:nsrect]; |
|
161 } |
|
162 |
|
163 - (CIImage *) view:(QTMovieView *)view willDisplayImage:(CIImage *)img |
|
164 { |
|
165 // This method is called from QTMovieView just |
|
166 // before the image will be drawn. |
|
167 Q_UNUSED(view); |
|
168 if (m_renderer) { |
|
169 CGRect bounds = [img extent]; |
|
170 int w = bounds.size.width; |
|
171 int h = bounds.size.height; |
|
172 |
|
173 QVideoFrame frame; |
|
174 |
|
175 QAbstractVideoSurface *surface = m_renderer->surface(); |
|
176 if (!surface || !surface->isActive()) |
|
177 return img; |
|
178 |
|
179 if (surface->surfaceFormat().handleType() == QAbstractVideoBuffer::CoreImageHandle) { |
|
180 //surface supports rendering of opengl based CIImage |
|
181 frame = QVideoFrame(new QT7CIImageVideoBuffer(img), QSize(w,h), QVideoFrame::Format_RGB32 ); |
|
182 } else { |
|
183 //Swap R and B colors |
|
184 CIFilter *colorSwapFilter = [CIFilter filterWithName: @"CIColorMatrix" keysAndValues: |
|
185 @"inputImage", img, |
|
186 @"inputRVector", [CIVector vectorWithX: 0 Y: 0 Z: 1 W: 0], |
|
187 @"inputGVector", [CIVector vectorWithX: 0 Y: 1 Z: 0 W: 0], |
|
188 @"inputBVector", [CIVector vectorWithX: 1 Y: 0 Z: 0 W: 0], |
|
189 @"inputAVector", [CIVector vectorWithX: 0 Y: 0 Z: 0 W: 1], |
|
190 @"inputBiasVector", [CIVector vectorWithX: 0 Y: 0 Z: 0 W: 0], |
|
191 nil]; |
|
192 CIImage *img = [colorSwapFilter valueForKey: @"outputImage"]; |
|
193 NSBitmapImageRep *bitmap =[[NSBitmapImageRep alloc] initWithCIImage:img]; |
|
194 //requesting the bitmap data is slow, |
|
195 //but it's better to do it here to avoid blocking the main thread for a long. |
|
196 [bitmap bitmapData]; |
|
197 frame = QVideoFrame(new NSBitmapVideoBuffer(bitmap), QSize(w,h), QVideoFrame::Format_RGB32 ); |
|
198 [bitmap release]; |
|
199 } |
|
200 |
|
201 if (m_renderer) |
|
202 m_renderer->renderFrame(frame); |
|
203 } |
|
204 |
|
205 return img; |
|
206 } |
|
207 |
|
208 // Override this method so that the movie doesn't stop if |
|
209 // the window becomes invisible |
|
210 - (void)viewWillMoveToWindow:(NSWindow *)newWindow |
|
211 { |
|
212 Q_UNUSED(newWindow); |
|
213 } |
|
214 |
|
215 |
|
216 VIDEO_TRANSPARENT(mouseDown); |
|
217 VIDEO_TRANSPARENT(mouseDragged); |
|
218 VIDEO_TRANSPARENT(mouseUp); |
|
219 VIDEO_TRANSPARENT(mouseMoved); |
|
220 VIDEO_TRANSPARENT(mouseEntered); |
|
221 VIDEO_TRANSPARENT(mouseExited); |
|
222 VIDEO_TRANSPARENT(rightMouseDown); |
|
223 VIDEO_TRANSPARENT(rightMouseDragged); |
|
224 VIDEO_TRANSPARENT(rightMouseUp); |
|
225 VIDEO_TRANSPARENT(otherMouseDown); |
|
226 VIDEO_TRANSPARENT(otherMouseDragged); |
|
227 VIDEO_TRANSPARENT(otherMouseUp); |
|
228 VIDEO_TRANSPARENT(keyDown); |
|
229 VIDEO_TRANSPARENT(keyUp); |
|
230 VIDEO_TRANSPARENT(scrollWheel) |
|
231 |
|
232 @end |
|
233 |
|
234 |
|
235 QT7MovieViewRenderer::QT7MovieViewRenderer(QObject *parent) |
|
236 :QT7VideoRendererControl(parent), |
|
237 m_movie(0), |
|
238 m_movieView(0), |
|
239 m_surface(0), |
|
240 m_pendingRenderEvent(false) |
|
241 { |
|
242 } |
|
243 |
|
244 QT7MovieViewRenderer::~QT7MovieViewRenderer() |
|
245 { |
|
246 [(HiddenQTMovieView*)m_movieView setRenderer:0]; |
|
247 |
|
248 QMutexLocker locker(&m_mutex); |
|
249 m_currentFrame = QVideoFrame(); |
|
250 [(HiddenQTMovieView*)m_movieView release]; |
|
251 } |
|
252 |
|
253 void QT7MovieViewRenderer::setupVideoOutput() |
|
254 { |
|
255 AutoReleasePool pool; |
|
256 |
|
257 #ifdef QT_DEBUG_QT7 |
|
258 qDebug() << "QT7MovieViewRenderer::setupVideoOutput" << m_movie << m_surface; |
|
259 #endif |
|
260 |
|
261 HiddenQTMovieView *movieView = (HiddenQTMovieView*)m_movieView; |
|
262 |
|
263 if (movieView && !m_movie) { |
|
264 [movieView setMovie:nil]; |
|
265 } |
|
266 |
|
267 if (m_movie) { |
|
268 NSSize size = [[(QTMovie*)m_movie attributeForKey:@"QTMovieNaturalSizeAttribute"] sizeValue]; |
|
269 |
|
270 m_nativeSize = QSize(size.width, size.height); |
|
271 |
|
272 if (!movieView) { |
|
273 movieView = [[HiddenQTMovieView alloc] initWithRenderer:this]; |
|
274 m_movieView = movieView; |
|
275 [movieView setControllerVisible:NO]; |
|
276 } |
|
277 |
|
278 [movieView setMovie:(QTMovie*)m_movie]; |
|
279 [movieView setDrawRect:QRect(QPoint(0,0), m_nativeSize)]; |
|
280 } |
|
281 |
|
282 if (m_surface && !m_nativeSize.isEmpty()) { |
|
283 bool coreImageFrameSupported = !m_surface->supportedPixelFormats(QAbstractVideoBuffer::CoreImageHandle).isEmpty() && |
|
284 !m_surface->supportedPixelFormats(QAbstractVideoBuffer::GLTextureHandle).isEmpty(); |
|
285 |
|
286 QVideoSurfaceFormat format(m_nativeSize, QVideoFrame::Format_RGB32, |
|
287 coreImageFrameSupported ? QAbstractVideoBuffer::CoreImageHandle : QAbstractVideoBuffer::NoHandle); |
|
288 |
|
289 if (m_surface->isActive() && m_surface->surfaceFormat() != format) { |
|
290 #ifdef QT_DEBUG_QT7 |
|
291 qDebug() << "Surface format was changed, stop the surface."; |
|
292 #endif |
|
293 m_surface->stop(); |
|
294 } |
|
295 |
|
296 if (!m_surface->isActive()) { |
|
297 #ifdef QT_DEBUG_QT7 |
|
298 qDebug() << "Starting the surface with format" << format; |
|
299 #endif |
|
300 if (!m_surface->start(format)) |
|
301 qWarning() << "failed to start video surface" << m_surface->error(); |
|
302 } |
|
303 } |
|
304 } |
|
305 |
|
306 void QT7MovieViewRenderer::setMovie(void *movie) |
|
307 { |
|
308 if (movie == m_movie) |
|
309 return; |
|
310 |
|
311 QMutexLocker locker(&m_mutex); |
|
312 m_movie = movie; |
|
313 setupVideoOutput(); |
|
314 } |
|
315 |
|
316 void QT7MovieViewRenderer::updateNaturalSize(const QSize &newSize) |
|
317 { |
|
318 if (m_nativeSize != newSize) { |
|
319 m_nativeSize = newSize; |
|
320 setupVideoOutput(); |
|
321 } |
|
322 } |
|
323 |
|
324 QAbstractVideoSurface *QT7MovieViewRenderer::surface() const |
|
325 { |
|
326 return m_surface; |
|
327 } |
|
328 |
|
329 void QT7MovieViewRenderer::setSurface(QAbstractVideoSurface *surface) |
|
330 { |
|
331 if (surface == m_surface) |
|
332 return; |
|
333 |
|
334 QMutexLocker locker(&m_mutex); |
|
335 |
|
336 if (m_surface && m_surface->isActive()) |
|
337 m_surface->stop(); |
|
338 |
|
339 m_surface = surface; |
|
340 setupVideoOutput(); |
|
341 } |
|
342 |
|
343 void QT7MovieViewRenderer::renderFrame(const QVideoFrame &frame) |
|
344 { |
|
345 |
|
346 QMutexLocker locker(&m_mutex); |
|
347 m_currentFrame = frame; |
|
348 |
|
349 if (!m_pendingRenderEvent) |
|
350 qApp->postEvent(this, new QEvent(QEvent::User), Qt::HighEventPriority); |
|
351 |
|
352 m_pendingRenderEvent = true; |
|
353 } |
|
354 |
|
355 bool QT7MovieViewRenderer::event(QEvent *event) |
|
356 { |
|
357 if (event->type() == QEvent::User) { |
|
358 QMutexLocker locker(&m_mutex); |
|
359 m_pendingRenderEvent = false; |
|
360 if (m_surface->isActive()) |
|
361 m_surface->present(m_currentFrame); |
|
362 } |
|
363 |
|
364 return QT7VideoRendererControl::event(event); |
|
365 } |