1 /* |
|
2 * Copyright (c) 2010 Nokia Corporation and/or its subsidiary(-ies). |
|
3 * All rights reserved. |
|
4 * This component and the accompanying materials are made available |
|
5 * under the terms of "Eclipse Public License v1.0" |
|
6 * which accompanies this distribution, and is available |
|
7 * at the URL "http://www.eclipse.org/legal/epl-v10.html". |
|
8 * |
|
9 * Initial Contributors: |
|
10 * Nokia Corporation - initial contribution. |
|
11 * |
|
12 * Contributors: |
|
13 * |
|
14 * Description: |
|
15 * |
|
16 */ |
|
17 #include <QTime> |
|
18 #include <QPixmap> |
|
19 |
|
20 #include "cxevideocapturecontroldesktop.h" |
|
21 #include "cxeviewfindercontrol.h" |
|
22 #include "cxutils.h" |
|
23 #include "cxestate.h" |
|
24 #include "cxenamespace.h" |
|
25 #include "cxefilenamegeneratordesktop.h" |
|
26 #include "cxecameradevicedesktop.h" |
|
27 #include "cxeexception.h" |
|
28 |
|
29 namespace |
|
30 { |
|
31 // Unit is milliseconds |
|
32 static const int CXENGINE_ELAPSED_TIME_TIMEOUT = 1000; // 1 second |
|
33 |
|
34 // Unit is seconds |
|
35 static const int CXENGINE_MAXIMUM_VIDEO_TIME = 90*60; // 90 minutes |
|
36 } |
|
37 |
|
38 |
|
39 /*! |
|
40 * Constructor |
|
41 */ |
|
42 CxeVideoCaptureControlDesktop::CxeVideoCaptureControlDesktop(CxeCameraDeviceDesktop &cameraDevice, |
|
43 CxeViewfinderControl &viewfinderControl, |
|
44 CxeCameraDeviceControl &cameraDeviceControl, |
|
45 CxeFilenameGenerator &nameGenerator, |
|
46 CxeQualityPresets &qualityPresets) : |
|
47 mCameraDevice(cameraDevice), |
|
48 mCameraDeviceControl(cameraDeviceControl), |
|
49 mViewfinderControl(viewfinderControl), |
|
50 mFilenameGenerator(nameGenerator), |
|
51 mQualityPresets(qualityPresets), |
|
52 mSnapshot(), |
|
53 mNewFileName(""), |
|
54 mCurrentFilename(""), |
|
55 mState(Idle), |
|
56 mElapsedTime(0) |
|
57 { |
|
58 CX_DEBUG_ENTER_FUNCTION(); |
|
59 |
|
60 qRegisterMetaType<CxeVideoCaptureControl::State> (); |
|
61 initializeStates(); |
|
62 setupElapseTimer(); |
|
63 CX_DEBUG_EXIT_FUNCTION(); |
|
64 } |
|
65 |
|
66 |
|
67 /*! |
|
68 * Destructor |
|
69 */ |
|
70 CxeVideoCaptureControlDesktop::~CxeVideoCaptureControlDesktop() |
|
71 { |
|
72 CX_DEBUG_ENTER_FUNCTION(); |
|
73 |
|
74 releaseResources(); |
|
75 |
|
76 CX_DEBUG_EXIT_FUNCTION(); |
|
77 } |
|
78 |
|
79 |
|
80 /*! |
|
81 * Initializes resources for video recording. |
|
82 */ |
|
83 void CxeVideoCaptureControlDesktop::init() |
|
84 { |
|
85 CX_DEBUG_ENTER_FUNCTION(); |
|
86 |
|
87 if (state() == Idle) { |
|
88 // start initializing resources for video capture |
|
89 initVideoRecorder(); |
|
90 } else if (state() == Initialized) { |
|
91 // video recorder already initalized. Continue to prepare video reocording. |
|
92 open(); |
|
93 } |
|
94 |
|
95 CX_DEBUG_EXIT_FUNCTION(); |
|
96 } |
|
97 |
|
98 |
|
99 /*! |
|
100 * Releases all resources |
|
101 */ |
|
102 void CxeVideoCaptureControlDesktop::deinit() |
|
103 { |
|
104 CX_DEBUG_ENTER_FUNCTION(); |
|
105 |
|
106 if (state() == Idle) { |
|
107 // nothing to do |
|
108 CX_DEBUG_EXIT_FUNCTION(); |
|
109 return; |
|
110 } |
|
111 // stop video-recording in-case if its ongoing. |
|
112 stop(); |
|
113 |
|
114 setState(Idle); |
|
115 |
|
116 CX_DEBUG_EXIT_FUNCTION(); |
|
117 } |
|
118 |
|
119 |
|
120 /*! |
|
121 * Intializes VideoRecorder for recording. |
|
122 */ |
|
123 void CxeVideoCaptureControlDesktop::initVideoRecorder() |
|
124 { |
|
125 CX_DEBUG_ENTER_FUNCTION(); |
|
126 |
|
127 if (state() != Idle) { |
|
128 // not valid state to start "open" operation |
|
129 CX_DEBUG_EXIT_FUNCTION(); |
|
130 return; |
|
131 } |
|
132 |
|
133 // update current video quality details from icm |
|
134 CxeError::Id err = CxeError::None; |
|
135 |
|
136 setState(Initialized); |
|
137 open(); |
|
138 |
|
139 if (err) { |
|
140 // In case of error |
|
141 emit videoPrepareComplete(err); |
|
142 deinit(); |
|
143 } |
|
144 |
|
145 CX_DEBUG_EXIT_FUNCTION(); |
|
146 } |
|
147 |
|
148 /*! |
|
149 * Setup the timer for elapsed time |
|
150 */ |
|
151 void CxeVideoCaptureControlDesktop::setupElapseTimer() |
|
152 { |
|
153 mRecordElapseTimer.setSingleShot(false); |
|
154 mRecordElapseTimer.setInterval(CXENGINE_ELAPSED_TIME_TIMEOUT); |
|
155 connect(&mRecordElapseTimer, SIGNAL(timeout()), this, SLOT(handleElapseTimeout())); |
|
156 } |
|
157 |
|
158 |
|
159 /*! |
|
160 * Opens file for video recording. |
|
161 */ |
|
162 void CxeVideoCaptureControlDesktop::open() |
|
163 { |
|
164 CX_DEBUG_ENTER_FUNCTION(); |
|
165 |
|
166 if (state() != Initialized) { |
|
167 // not valid state to start "open" operation |
|
168 CX_DEBUG_EXIT_FUNCTION(); |
|
169 return; |
|
170 } |
|
171 |
|
172 CxeError::Id err = CxeError::None; |
|
173 |
|
174 // generate video file name, if necessary |
|
175 if (mNewFileName.isEmpty()) { |
|
176 QString fileExt = ".mp4"; |
|
177 |
|
178 // Generate new filename and open the file for writing video data |
|
179 err = mFilenameGenerator.generateFilename(mNewFileName, fileExt); |
|
180 if (err == CxeError::None) { |
|
181 mCurrentFilename = mNewFileName; |
|
182 } else { |
|
183 // file name is not valid, re-initialize the value of current string |
|
184 // back to empty string |
|
185 mCurrentFilename = QString(""); |
|
186 } |
|
187 } |
|
188 |
|
189 if (err) { |
|
190 // error occured. |
|
191 deinit(); |
|
192 emit videoPrepareComplete(err); |
|
193 } else { |
|
194 setState(Preparing); |
|
195 prepare(); |
|
196 } |
|
197 CX_DEBUG_EXIT_FUNCTION(); |
|
198 } |
|
199 |
|
200 |
|
201 |
|
202 /*! |
|
203 * Prepare Video Recorder with necessary settings for video capture. |
|
204 */ |
|
205 void CxeVideoCaptureControlDesktop::prepare() |
|
206 { |
|
207 CX_DEBUG_ENTER_FUNCTION(); |
|
208 |
|
209 if (state() != Preparing) { |
|
210 // not valid state to continue prepare. |
|
211 CX_DEBUG_EXIT_FUNCTION(); |
|
212 return; |
|
213 } |
|
214 |
|
215 int err = CxeError::None; |
|
216 |
|
217 if (!err) { |
|
218 // prepare snapshot |
|
219 err = prepareVideoSnapshot(); |
|
220 } |
|
221 |
|
222 if (!err) { |
|
223 // prepare zoom only when there are no errors during prepare. |
|
224 emit prepareZoomForVideo(); |
|
225 } |
|
226 |
|
227 mViewfinderControl.start(); |
|
228 setState(Ready); |
|
229 |
|
230 // emit video prepare status |
|
231 emit videoPrepareComplete(CxeError::None); |
|
232 |
|
233 |
|
234 CX_DEBUG_EXIT_FUNCTION(); |
|
235 } |
|
236 |
|
237 /*! |
|
238 Prepare snapshot |
|
239 @Return symbian error code. |
|
240 */ |
|
241 int CxeVideoCaptureControlDesktop::prepareVideoSnapshot() |
|
242 { |
|
243 CX_DEBUG_ENTER_FUNCTION(); |
|
244 |
|
245 |
|
246 int err = 0; |
|
247 // Whether or not we have postcapture on, we need the snapshot for Thumbnail Manager. |
|
248 |
|
249 mSnapshot = mCameraDevice.currentSnaphot(); |
|
250 handleSnapshotEvent(CxeError::None); |
|
251 |
|
252 CX_DEBUG_EXIT_FUNCTION(); |
|
253 |
|
254 return err; |
|
255 } |
|
256 |
|
257 /*! |
|
258 * Camera events coming from ecam. |
|
259 */ |
|
260 void CxeVideoCaptureControlDesktop::handleCameraEvent(int eventUid, int error) |
|
261 { |
|
262 CX_DEBUG_ENTER_FUNCTION(); |
|
263 |
|
264 Q_UNUSED(eventUid); |
|
265 Q_UNUSED(error); |
|
266 |
|
267 CX_DEBUG_EXIT_FUNCTION(); |
|
268 } |
|
269 |
|
270 |
|
271 /*! |
|
272 * Handle Snapshot event from ecam |
|
273 */ |
|
274 void CxeVideoCaptureControlDesktop::handleSnapshotEvent(CxeError::Id error) |
|
275 { |
|
276 CX_DEBUG_ENTER_FUNCTION(); |
|
277 |
|
278 if (state() == Idle) { |
|
279 // we ignore this event, when we are not in active state(s) |
|
280 CX_DEBUG(( "wrong state, ignoring snapshot" )); |
|
281 CX_DEBUG_EXIT_FUNCTION(); |
|
282 return; |
|
283 } |
|
284 emit snapshotReady(error, QImage(), filename()); |
|
285 CX_DEBUG_EXIT_FUNCTION(); |
|
286 } |
|
287 |
|
288 |
|
289 /*! |
|
290 * Resets the video snapshot and current video filename |
|
291 */ |
|
292 void CxeVideoCaptureControlDesktop::reset() |
|
293 { |
|
294 CX_DEBUG_ENTER_FUNCTION(); |
|
295 |
|
296 // Snapshot will consume considerably memory. |
|
297 // Replace it with null pixmap to have it freed. |
|
298 mSnapshot = QPixmap(); |
|
299 // reset the current file name. |
|
300 mCurrentFilename = QString(""); |
|
301 |
|
302 CX_DEBUG_EXIT_FUNCTION(); |
|
303 } |
|
304 |
|
305 |
|
306 /*! |
|
307 *@Return current video filename |
|
308 */ |
|
309 QString CxeVideoCaptureControlDesktop::filename() const |
|
310 { |
|
311 // Simply return the current contents of mCurrentFilename. |
|
312 // If video recording was started then it returns proper filename |
|
313 // otherwise an empty string is returned. |
|
314 return mCurrentFilename; |
|
315 } |
|
316 |
|
317 |
|
318 /*! |
|
319 * @Return current video snapshot |
|
320 */ |
|
321 QPixmap CxeVideoCaptureControlDesktop::snapshot() const |
|
322 { |
|
323 return mSnapshot; |
|
324 } |
|
325 |
|
326 /*! |
|
327 * @Return QList of all supported video quality details based on the camera index |
|
328 * (primary/secondary). |
|
329 */ |
|
330 QList<CxeVideoDetails> CxeVideoCaptureControlDesktop::supportedVideoQualities() |
|
331 { |
|
332 return QList<CxeVideoDetails>(); |
|
333 } |
|
334 |
|
335 /*! |
|
336 * Starts video recording if we are in appropriate state. |
|
337 */ |
|
338 void CxeVideoCaptureControlDesktop::record() |
|
339 { |
|
340 CX_DEBUG_ENTER_FUNCTION(); |
|
341 |
|
342 if (state() == Ready || state() == Paused) { |
|
343 //we skip the playing of the sound in the desktop state for now |
|
344 setState(Recording); |
|
345 mRecordElapseTimer.start(); |
|
346 } |
|
347 |
|
348 CX_DEBUG_EXIT_FUNCTION(); |
|
349 } |
|
350 |
|
351 |
|
352 /*! |
|
353 * Pauses video recording. |
|
354 */ |
|
355 void CxeVideoCaptureControlDesktop::pause() |
|
356 { |
|
357 CX_DEBUG_ENTER_FUNCTION(); |
|
358 |
|
359 mRecordElapseTimer.stop(); |
|
360 |
|
361 setState(Paused); |
|
362 |
|
363 CX_DEBUG_EXIT_FUNCTION(); |
|
364 } |
|
365 |
|
366 |
|
367 /*! |
|
368 * Stops video recording. |
|
369 */ |
|
370 void CxeVideoCaptureControlDesktop::stop() |
|
371 { |
|
372 CX_DEBUG_ENTER_FUNCTION(); |
|
373 |
|
374 if (state() == Recording || state() == Paused) { |
|
375 mFilenameGenerator.raiseCounterValue(); |
|
376 } |
|
377 |
|
378 mViewfinderControl.stop(); |
|
379 mRecordElapseTimer.stop(); |
|
380 mElapsedTime = 0; |
|
381 |
|
382 setState(Stopping); |
|
383 setState(Initialized); |
|
384 |
|
385 CX_DEBUG_EXIT_FUNCTION(); |
|
386 } |
|
387 |
|
388 |
|
389 /*! |
|
390 * camera reference changing, release resources |
|
391 */ |
|
392 void CxeVideoCaptureControlDesktop::prepareForCameraDelete() |
|
393 { |
|
394 CX_DEBUG_ENTER_FUNCTION(); |
|
395 releaseResources(); |
|
396 CX_DEBUG_EXIT_FUNCTION(); |
|
397 } |
|
398 |
|
399 /*! |
|
400 * prepare for camera release. |
|
401 */ |
|
402 void CxeVideoCaptureControlDesktop::prepareForRelease() |
|
403 { |
|
404 CX_DEBUG_ENTER_FUNCTION(); |
|
405 deinit(); |
|
406 CX_DEBUG_EXIT_FUNCTION(); |
|
407 } |
|
408 |
|
409 |
|
410 /*! |
|
411 * new camera available, |
|
412 */ |
|
413 void CxeVideoCaptureControlDesktop::handleCameraAllocated(CxeError::Id error) |
|
414 { |
|
415 Q_UNUSED(error); |
|
416 CX_DEBUG_IN_FUNCTION(); |
|
417 } |
|
418 |
|
419 |
|
420 /*! |
|
421 * Initializes video recorder. |
|
422 */ |
|
423 void CxeVideoCaptureControlDesktop::createVideoRecorder() |
|
424 { |
|
425 CX_DEBUG_IN_FUNCTION(); |
|
426 } |
|
427 |
|
428 |
|
429 /*! |
|
430 * releases resources used by videocapture |
|
431 */ |
|
432 void CxeVideoCaptureControlDesktop::releaseResources() |
|
433 { |
|
434 CX_DEBUG_ENTER_FUNCTION(); |
|
435 |
|
436 // first de-init videocapture control |
|
437 deinit(); |
|
438 reset(); |
|
439 |
|
440 CX_DEBUG_EXIT_FUNCTION(); |
|
441 } |
|
442 |
|
443 |
|
444 /*! |
|
445 @Returns current state of videocapture |
|
446 */ |
|
447 CxeVideoCaptureControl::State CxeVideoCaptureControlDesktop::state() const |
|
448 { |
|
449 return mState; |
|
450 } |
|
451 |
|
452 /*! |
|
453 * Initialize states for videocapturecontrol |
|
454 */ |
|
455 void CxeVideoCaptureControlDesktop::initializeStates() |
|
456 { |
|
457 CX_DEBUG_IN_FUNCTION(); |
|
458 } |
|
459 |
|
460 /*! |
|
461 * calculates remaining video recording time. |
|
462 */ |
|
463 void CxeVideoCaptureControlDesktop::remainingTime(int &time) |
|
464 { |
|
465 CX_DEBUG_ENTER_FUNCTION(); |
|
466 |
|
467 time = CXENGINE_MAXIMUM_VIDEO_TIME - mElapsedTime; |
|
468 if (time < 0) { |
|
469 time = 0; |
|
470 } |
|
471 |
|
472 CX_DEBUG_EXIT_FUNCTION(); |
|
473 } |
|
474 |
|
475 |
|
476 /*! |
|
477 * Calculates elapsed recording time during video recording |
|
478 * @return Did fetching elapsed time succeed. |
|
479 */ |
|
480 bool CxeVideoCaptureControlDesktop::elapsedTime(int &time) |
|
481 { |
|
482 CX_DEBUG_ENTER_FUNCTION(); |
|
483 |
|
484 time = mElapsedTime; |
|
485 |
|
486 CX_DEBUG_EXIT_FUNCTION(); |
|
487 |
|
488 return true; |
|
489 } |
|
490 |
|
491 /*! |
|
492 * slot called when playing a sound has finished. |
|
493 */ |
|
494 void CxeVideoCaptureControlDesktop::handleSoundPlayed() |
|
495 { |
|
496 // in case of video capture stop sound playing, nothing needs to be done |
|
497 // meaning the state set elsewhere, and the video capture has been stopped already |
|
498 |
|
499 CX_DEBUG_IN_FUNCTION(); |
|
500 } |
|
501 |
|
502 |
|
503 /*! |
|
504 * setting has changed, check if we are interested. |
|
505 */ |
|
506 void CxeVideoCaptureControlDesktop::handleSettingValueChanged(const QString& settingId, |
|
507 QVariant newValue) |
|
508 { |
|
509 CX_DEBUG_ENTER_FUNCTION(); |
|
510 |
|
511 Q_UNUSED(newValue); |
|
512 |
|
513 if (settingId == CxeSettingIds::VIDEO_QUALITY) { |
|
514 // re-prepare for video |
|
515 deinit(); |
|
516 init(); |
|
517 } else if (settingId == CxeSettingIds::VIDEO_MUTE_SETTING) { |
|
518 // mute setting changed, apply the new setting and re-prepare. |
|
519 prepare(); |
|
520 } |
|
521 |
|
522 CX_DEBUG_EXIT_FUNCTION(); |
|
523 } |
|
524 |
|
525 |
|
526 |
|
527 /*! |
|
528 * Video Scene mode changed, needs updated |
|
529 */ |
|
530 void CxeVideoCaptureControlDesktop::handleSceneChanged(CxeScene &scene) |
|
531 { |
|
532 CX_DEBUG_ENTER_FUNCTION(); |
|
533 Q_UNUSED(scene); |
|
534 CX_DEBUG_EXIT_FUNCTION(); |
|
535 } |
|
536 |
|
537 void CxeVideoCaptureControlDesktop::handleElapseTimeout() |
|
538 { |
|
539 mElapsedTime++; |
|
540 |
|
541 CX_DEBUG( ("CxeVideoCaptureControlDesktop::handleElapseTimeout() <> mElapsedTime: %d", mElapsedTime ) ); |
|
542 } |
|
543 |
|
544 void CxeVideoCaptureControlDesktop::setState(CxeVideoCaptureControl::State stateId, CxeError::Id error) |
|
545 { |
|
546 mState = stateId; |
|
547 CX_DEBUG( ("CxeVideoCaptureControlDesktop::setState <> mState: %d", mState ) ); |
|
548 emit stateChanged(mState, error); |
|
549 } |
|
550 // End of file |
|