|
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 #include "qt7backend.h" |
|
43 |
|
44 #import <QTKit/QTDataReference.h> |
|
45 #import <QTKit/QTMovie.h> |
|
46 #import <QTKit/QTMovieView.h> |
|
47 #import <QTKit/QTMovieLayer.h> |
|
48 #import <AppKit/NSImage.h> |
|
49 |
|
50 |
|
51 #include "qt7playercontrol.h" |
|
52 #include "qt7movievideowidget.h" |
|
53 #include "qt7playersession.h" |
|
54 #include "qcvdisplaylink.h" |
|
55 #include <QtCore/qdebug.h> |
|
56 #include <QtCore/qcoreapplication.h> |
|
57 |
|
58 #include <QGLWidget> |
|
59 |
|
60 #include <CoreFoundation/CoreFoundation.h> |
|
61 |
|
62 #import <QuartzCore/QuartzCore.h> |
|
63 |
|
64 #include "math.h" |
|
65 |
|
66 QT_USE_NAMESPACE |
|
67 |
|
68 class GLVideoWidget : public QGLWidget |
|
69 { |
|
70 public: |
|
71 |
|
72 GLVideoWidget(QWidget *parent, const QGLFormat &format) |
|
73 : QGLWidget(format, parent), |
|
74 m_texRef(0), |
|
75 m_nativeSize(640,480), |
|
76 m_aspectRatioMode(Qt::KeepAspectRatio) |
|
77 { |
|
78 setAutoFillBackground(false); |
|
79 } |
|
80 |
|
81 void initializeGL() |
|
82 { |
|
83 QColor bgColor = palette().color(QPalette::Background); |
|
84 glClearColor(bgColor.redF(), bgColor.greenF(), bgColor.blueF(), bgColor.alphaF()); |
|
85 } |
|
86 |
|
87 void resizeGL(int w, int h) |
|
88 { |
|
89 glMatrixMode(GL_MODELVIEW); |
|
90 glLoadIdentity(); |
|
91 glMatrixMode(GL_PROJECTION); |
|
92 glLoadIdentity(); |
|
93 glViewport(0, 0, GLsizei(w), GLsizei(h)); |
|
94 gluOrtho2D(0, GLsizei(w), 0, GLsizei(h)); |
|
95 updateGL(); |
|
96 } |
|
97 |
|
98 void paintGL() |
|
99 { |
|
100 glClear(GL_COLOR_BUFFER_BIT); |
|
101 if (!m_texRef) |
|
102 return; |
|
103 |
|
104 glPushMatrix(); |
|
105 glDisable(GL_CULL_FACE); |
|
106 GLenum target = CVOpenGLTextureGetTarget(m_texRef); |
|
107 glEnable(target); |
|
108 |
|
109 glBindTexture(target, CVOpenGLTextureGetName(m_texRef)); |
|
110 glTexParameterf(target, GL_TEXTURE_MIN_FILTER, GL_LINEAR); |
|
111 glTexParameterf(target, GL_TEXTURE_MAG_FILTER, GL_LINEAR); |
|
112 GLfloat lowerLeft[2], lowerRight[2], upperRight[2], upperLeft[2]; |
|
113 CVOpenGLTextureGetCleanTexCoords(m_texRef, lowerLeft, lowerRight, upperRight, upperLeft); |
|
114 |
|
115 glBegin(GL_QUADS); |
|
116 QRect rect = displayRect(); |
|
117 glTexCoord2f(lowerLeft[0], lowerLeft[1]); |
|
118 glVertex2i(rect.topLeft().x(), rect.topLeft().y()); |
|
119 glTexCoord2f(lowerRight[0], lowerRight[1]); |
|
120 glVertex2i(rect.topRight().x() + 1, rect.topRight().y()); |
|
121 glTexCoord2f(upperRight[0], upperRight[1]); |
|
122 glVertex2i(rect.bottomRight().x() + 1, rect.bottomRight().y() + 1); |
|
123 glTexCoord2f(upperLeft[0], upperLeft[1]); |
|
124 glVertex2i(rect.bottomLeft().x(), rect.bottomLeft().y() + 1); |
|
125 glEnd(); |
|
126 glPopMatrix(); |
|
127 } |
|
128 |
|
129 void setCVTexture(CVOpenGLTextureRef texRef) |
|
130 { |
|
131 if (m_texRef) |
|
132 CVOpenGLTextureRelease(m_texRef); |
|
133 |
|
134 m_texRef = texRef; |
|
135 |
|
136 if (m_texRef) |
|
137 CVOpenGLTextureRetain(m_texRef); |
|
138 |
|
139 if (isVisible()) { |
|
140 makeCurrent(); |
|
141 paintGL(); |
|
142 swapBuffers(); |
|
143 } |
|
144 } |
|
145 |
|
146 QSize sizeHint() const |
|
147 { |
|
148 return m_nativeSize; |
|
149 } |
|
150 |
|
151 void setNativeSize(const QSize &size) |
|
152 { |
|
153 m_nativeSize = size; |
|
154 } |
|
155 |
|
156 void setAspectRatioMode(Qt::AspectRatioMode mode) |
|
157 { |
|
158 if (m_aspectRatioMode != mode) { |
|
159 m_aspectRatioMode = mode; |
|
160 update(); |
|
161 } |
|
162 } |
|
163 |
|
164 private: |
|
165 QRect displayRect() const |
|
166 { |
|
167 QRect displayRect = rect(); |
|
168 |
|
169 if (m_aspectRatioMode == Qt::KeepAspectRatio) { |
|
170 QSize size = m_nativeSize; |
|
171 size.scale(displayRect.size(), Qt::KeepAspectRatio); |
|
172 |
|
173 displayRect = QRect(QPoint(0, 0), size); |
|
174 displayRect.moveCenter(rect().center()); |
|
175 } |
|
176 return displayRect; |
|
177 } |
|
178 |
|
179 CVOpenGLTextureRef m_texRef; |
|
180 QSize m_nativeSize; |
|
181 Qt::AspectRatioMode m_aspectRatioMode; |
|
182 }; |
|
183 |
|
184 QT7MovieVideoWidget::QT7MovieVideoWidget(QObject *parent) |
|
185 :QT7VideoWidgetControl(parent), |
|
186 m_movie(0), |
|
187 m_videoWidget(0), |
|
188 m_fullscreen(false), |
|
189 m_aspectRatioMode(Qt::KeepAspectRatio), |
|
190 m_brightness(0), |
|
191 m_contrast(0), |
|
192 m_hue(0), |
|
193 m_saturation(0) |
|
194 { |
|
195 #ifdef QT_DEBUG_QT7 |
|
196 qDebug() << "QT7MovieVideoWidget"; |
|
197 #endif |
|
198 |
|
199 QGLFormat format = QGLFormat::defaultFormat(); |
|
200 format.setSwapInterval(1); // Vertical sync (avoid tearing) |
|
201 m_videoWidget = new GLVideoWidget(0, format); |
|
202 |
|
203 m_displayLink = new QCvDisplayLink(this); |
|
204 |
|
205 connect(m_displayLink, SIGNAL(tick(CVTimeStamp)), SLOT(updateVideoFrame(CVTimeStamp))); |
|
206 |
|
207 if (!createVisualContext()) { |
|
208 qWarning() << "QT7MovieVideoWidget: failed to create visual context"; |
|
209 } |
|
210 } |
|
211 |
|
212 bool QT7MovieVideoWidget::createVisualContext() |
|
213 { |
|
214 #ifdef QUICKTIME_C_API_AVAILABLE |
|
215 m_videoWidget->makeCurrent(); |
|
216 |
|
217 AutoReleasePool pool; |
|
218 CGLContextObj cglContext = CGLGetCurrentContext(); |
|
219 NSOpenGLPixelFormat *nsglPixelFormat = [NSOpenGLView defaultPixelFormat]; |
|
220 CGLPixelFormatObj cglPixelFormat = static_cast<CGLPixelFormatObj>([nsglPixelFormat CGLPixelFormatObj]); |
|
221 |
|
222 CFTypeRef keys[] = { kQTVisualContextOutputColorSpaceKey }; |
|
223 CGColorSpaceRef colorSpace = NULL; |
|
224 CMProfileRef sysprof = NULL; |
|
225 |
|
226 // Get the Systems Profile for the main display |
|
227 if (CMGetSystemProfile(&sysprof) == noErr) { |
|
228 // Create a colorspace with the systems profile |
|
229 colorSpace = CGColorSpaceCreateWithPlatformColorSpace(sysprof); |
|
230 CMCloseProfile(sysprof); |
|
231 } |
|
232 |
|
233 if (!colorSpace) |
|
234 colorSpace = CGColorSpaceCreateDeviceRGB(); |
|
235 |
|
236 CFDictionaryRef textureContextAttributes = CFDictionaryCreate(kCFAllocatorDefault, |
|
237 (const void **)keys, |
|
238 (const void **)&colorSpace, 1, |
|
239 &kCFTypeDictionaryKeyCallBacks, |
|
240 &kCFTypeDictionaryValueCallBacks); |
|
241 |
|
242 OSStatus err = QTOpenGLTextureContextCreate(kCFAllocatorDefault, |
|
243 cglContext, |
|
244 cglPixelFormat, |
|
245 textureContextAttributes, |
|
246 &m_visualContext); |
|
247 if (err != noErr) |
|
248 qWarning() << "Could not create visual context (OpenGL)"; |
|
249 |
|
250 |
|
251 return (err == noErr); |
|
252 #endif // QUICKTIME_C_API_AVAILABLE |
|
253 |
|
254 return false; |
|
255 } |
|
256 |
|
257 QT7MovieVideoWidget::~QT7MovieVideoWidget() |
|
258 { |
|
259 m_displayLink->stop(); |
|
260 [(QTMovie*)m_movie release]; |
|
261 delete m_videoWidget; |
|
262 } |
|
263 |
|
264 QWidget *QT7MovieVideoWidget::videoWidget() |
|
265 { |
|
266 return m_videoWidget; |
|
267 } |
|
268 |
|
269 void QT7MovieVideoWidget::setupVideoOutput() |
|
270 { |
|
271 AutoReleasePool pool; |
|
272 |
|
273 #ifdef QT_DEBUG_QT7 |
|
274 qDebug() << "QT7MovieVideoWidget::setupVideoOutput" << m_movie; |
|
275 #endif |
|
276 |
|
277 if (m_movie == 0) { |
|
278 m_displayLink->stop(); |
|
279 return; |
|
280 } |
|
281 |
|
282 NSSize size = [[(QTMovie*)m_movie attributeForKey:@"QTMovieNaturalSizeAttribute"] sizeValue]; |
|
283 m_nativeSize = QSize(size.width, size.height); |
|
284 m_videoWidget->setNativeSize(m_nativeSize); |
|
285 |
|
286 #ifdef QUICKTIME_C_API_AVAILABLE |
|
287 // targets a Movie to render into a visual context |
|
288 SetMovieVisualContext([(QTMovie*)m_movie quickTimeMovie], m_visualContext); |
|
289 #endif |
|
290 |
|
291 m_displayLink->start(); |
|
292 } |
|
293 |
|
294 void QT7MovieVideoWidget::setMovie(void *movie) |
|
295 { |
|
296 if (m_movie == movie) |
|
297 return; |
|
298 |
|
299 if (m_movie) { |
|
300 #ifdef QUICKTIME_C_API_AVAILABLE |
|
301 SetMovieVisualContext([(QTMovie*)m_movie quickTimeMovie], nil); |
|
302 #endif |
|
303 [(QTMovie*)m_movie release]; |
|
304 } |
|
305 |
|
306 m_movie = movie; |
|
307 [(QTMovie*)m_movie retain]; |
|
308 |
|
309 setupVideoOutput(); |
|
310 } |
|
311 |
|
312 void QT7MovieVideoWidget::updateNaturalSize(const QSize &newSize) |
|
313 { |
|
314 if (m_nativeSize != newSize) { |
|
315 m_nativeSize = newSize; |
|
316 setupVideoOutput(); |
|
317 } |
|
318 } |
|
319 |
|
320 bool QT7MovieVideoWidget::isFullScreen() const |
|
321 { |
|
322 return m_fullscreen; |
|
323 } |
|
324 |
|
325 void QT7MovieVideoWidget::setFullScreen(bool fullScreen) |
|
326 { |
|
327 m_fullscreen = fullScreen; |
|
328 } |
|
329 |
|
330 QSize QT7MovieVideoWidget::nativeSize() const |
|
331 { |
|
332 return m_nativeSize; |
|
333 } |
|
334 |
|
335 Qt::AspectRatioMode QT7MovieVideoWidget::aspectRatioMode() const |
|
336 { |
|
337 return m_aspectRatioMode; |
|
338 } |
|
339 |
|
340 void QT7MovieVideoWidget::setAspectRatioMode(Qt::AspectRatioMode mode) |
|
341 { |
|
342 m_aspectRatioMode = mode; |
|
343 m_videoWidget->setAspectRatioMode(mode); |
|
344 } |
|
345 |
|
346 int QT7MovieVideoWidget::brightness() const |
|
347 { |
|
348 return m_brightness; |
|
349 } |
|
350 |
|
351 void QT7MovieVideoWidget::setBrightness(int brightness) |
|
352 { |
|
353 m_brightness = brightness; |
|
354 updateColors(); |
|
355 } |
|
356 |
|
357 int QT7MovieVideoWidget::contrast() const |
|
358 { |
|
359 return m_contrast; |
|
360 } |
|
361 |
|
362 void QT7MovieVideoWidget::setContrast(int contrast) |
|
363 { |
|
364 m_contrast = contrast; |
|
365 updateColors(); |
|
366 } |
|
367 |
|
368 int QT7MovieVideoWidget::hue() const |
|
369 { |
|
370 return m_hue; |
|
371 } |
|
372 |
|
373 void QT7MovieVideoWidget::setHue(int hue) |
|
374 { |
|
375 m_hue = hue; |
|
376 updateColors(); |
|
377 } |
|
378 |
|
379 int QT7MovieVideoWidget::saturation() const |
|
380 { |
|
381 return m_saturation; |
|
382 } |
|
383 |
|
384 void QT7MovieVideoWidget::setSaturation(int saturation) |
|
385 { |
|
386 m_saturation = saturation; |
|
387 updateColors(); |
|
388 } |
|
389 |
|
390 void QT7MovieVideoWidget::updateColors() |
|
391 { |
|
392 #ifdef QUICKTIME_C_API_AVAILABLE |
|
393 if (m_movie) { |
|
394 QTMovie *movie = (QTMovie*)m_movie; |
|
395 |
|
396 Float32 value; |
|
397 value = m_brightness/100.0; |
|
398 SetMovieVisualBrightness([movie quickTimeMovie], value, 0); |
|
399 value = pow(2, m_contrast/50.0); |
|
400 SetMovieVisualContrast([movie quickTimeMovie], value, 0); |
|
401 value = m_hue/100.0; |
|
402 SetMovieVisualHue([movie quickTimeMovie], value, 0); |
|
403 value = 1.0+m_saturation/100.0; |
|
404 SetMovieVisualSaturation([movie quickTimeMovie], value, 0); |
|
405 } |
|
406 #endif |
|
407 } |
|
408 |
|
409 void QT7MovieVideoWidget::updateVideoFrame(const CVTimeStamp &ts) |
|
410 { |
|
411 #ifdef QUICKTIME_C_API_AVAILABLE |
|
412 AutoReleasePool pool; |
|
413 // check for new frame |
|
414 if (m_visualContext && QTVisualContextIsNewImageAvailable(m_visualContext, &ts)) { |
|
415 CVOpenGLTextureRef currentFrame = NULL; |
|
416 |
|
417 // get a "frame" (image buffer) from the Visual Context, indexed by the provided time |
|
418 OSStatus status = QTVisualContextCopyImageForTime(m_visualContext, NULL, &ts, ¤tFrame); |
|
419 |
|
420 // the above call may produce a null frame so check for this first |
|
421 // if we have a frame, then draw it |
|
422 if (status == noErr && currentFrame) { |
|
423 #ifdef QT_DEBUG_QT7 |
|
424 qDebug() << "render video frame"; |
|
425 #endif |
|
426 m_videoWidget->setCVTexture(currentFrame); |
|
427 CVOpenGLTextureRelease(currentFrame); |
|
428 } |
|
429 QTVisualContextTask(m_visualContext); |
|
430 } |
|
431 #else |
|
432 Q_UNUSED(ts); |
|
433 #endif |
|
434 } |
|
435 |
|
436 #include "moc_qt7movievideowidget.cpp" |