~phablet-team/camera-app/trunk

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
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
/*
 * Copyright (C) 2012 Canonical, Ltd.
 *
 * Authors:
 *  Guenter Schwann <guenter.schwann@canonical.com>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; version 3.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include "advancedcamerasettings.h"

#include <QDebug>
#include <QtMultimedia/QCamera>
#include <QtMultimedia/QCameraControl>
#include <QtMultimedia/QMediaService>
#include <QtMultimedia/QVideoDeviceSelectorControl>
#include <QtMultimedia/QCameraFlashControl>
#include <QtMultimedia/QCameraExposureControl>
#include <QGuiApplication>
#include <QScreen>

#include <cmath>

// Definition of this enum value is duplicated in qtubuntu-camera
static const QCameraExposure::ExposureMode ExposureHdr = static_cast<QCameraExposure::ExposureMode>(QCameraExposure::ExposureModeVendor + 1);

AdvancedCameraSettings::AdvancedCameraSettings(QObject *parent) :
    QObject(parent),
    m_cameraObject(0),
    m_camera(0),
    m_deviceSelector(0),
    m_viewFinderControl(0),
    m_cameraFlashControl(0),
    m_cameraExposureControl(0),
    m_imageEncoderControl(0),
    m_videoEncoderControl(0),
    m_cameraInfoControl(0),
    m_hdrEnabled(false)
{
}

QCamera* AdvancedCameraSettings::cameraFromCameraObject(QObject* cameraObject) const
{
    QVariant cameraVariant = cameraObject->property("mediaObject");
    if (!cameraVariant.isValid()) {
        qWarning() << "No valid mediaObject";
        return 0;
    }

    QCamera *camera = qvariant_cast<QCamera*>(cameraVariant);
    if (camera == 0) {
        qWarning() << "No valid camera passed";
        return 0;
    }

    return camera;
}

QMediaControl* AdvancedCameraSettings::mediaControlFromCamera(QCamera *camera, const char* iid) const
{
    if (camera == 0) {
        return 0;
    }

    QMediaService *service = camera->service();
    if (service == 0) {
        qWarning() << "Camera has no Mediaservice";
        return 0;
    }

    QMediaControl *control = service->requestControl(iid);
    if (control == 0) {
        qWarning() << "No media control support for" << iid;
        return 0;
    }

    return control;
}

QVideoDeviceSelectorControl* AdvancedCameraSettings::selectorFromCamera(QCamera *camera) const
{
    QMediaControl *control = mediaControlFromCamera(camera, QVideoDeviceSelectorControl_iid);
    if (control == 0) {
        return 0;
    }

    QVideoDeviceSelectorControl *selector = qobject_cast<QVideoDeviceSelectorControl*>(control);
    if (selector == 0) {
        qWarning() << "No video device selector support";
        return 0;
    }

    return selector;
}

QCameraViewfinderSettingsControl* AdvancedCameraSettings::viewfinderFromCamera(QCamera *camera) const
{
    QMediaControl *control = mediaControlFromCamera(camera, QCameraViewfinderSettingsControl_iid);
    if (control == 0) {
        return 0;
    }

    QCameraViewfinderSettingsControl *selector = qobject_cast<QCameraViewfinderSettingsControl*>(control);
    if (selector == 0) {
        qWarning() << "No viewfinder settings support";
        return 0;
    }

    return selector;
}

QCameraControl *AdvancedCameraSettings::camcontrolFromCamera(QCamera *camera) const
{
    QMediaControl *control = mediaControlFromCamera(camera, QCameraControl_iid);
    if (control == 0) {
        return 0;
    }

    QCameraControl *camControl = qobject_cast<QCameraControl*>(control);
    if (camControl == 0) {
        qWarning() << "No camera control support";
        return 0;
    }

    return camControl;
}

QCameraFlashControl *AdvancedCameraSettings::flashControlFromCamera(QCamera *camera) const
{
    QMediaControl *control = mediaControlFromCamera(camera, QCameraFlashControl_iid);
    QCameraFlashControl *flashControl = qobject_cast<QCameraFlashControl*>(control);

    if (flashControl == 0) {
        qWarning() << "No flash control support";
    }

    return flashControl;
}

QCameraExposureControl* AdvancedCameraSettings::exposureControlFromCamera(QCamera *camera) const
{
    QMediaControl *control = mediaControlFromCamera(camera, QCameraExposureControl_iid);
    QCameraExposureControl *exposureControl = qobject_cast<QCameraExposureControl*>(control);

    if (exposureControl == 0) {
        qWarning() << "No exposure control support";
    }

    return exposureControl;
}

QImageEncoderControl* AdvancedCameraSettings::imageEncoderControlFromCamera(QCamera *camera) const
{
    QMediaControl *control = mediaControlFromCamera(camera, QImageEncoderControl_iid);
    QImageEncoderControl *imageEncoderControl = qobject_cast<QImageEncoderControl*>(control);

    if (imageEncoderControl == 0) {
        qWarning() << "No image encoder control support";
    }

    return imageEncoderControl;
}

QVideoEncoderSettingsControl* AdvancedCameraSettings::videoEncoderControlFromCamera(QCamera *camera) const
{
    QMediaControl *control = mediaControlFromCamera(camera, QVideoEncoderSettingsControl_iid);
    QVideoEncoderSettingsControl *videoEncoderControl = qobject_cast<QVideoEncoderSettingsControl*>(control);

    if (videoEncoderControl == 0) {
        qWarning() << "No video encoder settings control support";
    }

    return videoEncoderControl;
}

QCameraInfoControl* AdvancedCameraSettings::cameraInfoControlFromCamera(QCamera *camera) const
{
    QMediaControl *control = mediaControlFromCamera(camera, QCameraInfoControl_iid);
    QCameraInfoControl *infoControl = qobject_cast<QCameraInfoControl*>(control);

    if (infoControl == 0) {
        qWarning() << "No info control support";
    }

    return infoControl;
}

QObject* AdvancedCameraSettings::camera() const
{
    return m_cameraObject;
}

void AdvancedCameraSettings::setCamera(QObject *cameraObject)
{
    if (cameraObject != m_cameraObject) {
        m_cameraObject = cameraObject;

        if (m_camera != 0) {
            this->disconnect(m_camera, SIGNAL(stateChanged(QCamera::State)));
        }
        QCamera* camera = cameraFromCameraObject(cameraObject);
        m_camera = camera;
        if (m_camera != 0) {
            this->connect(m_camera, SIGNAL(stateChanged(QCamera::State)),
                          SLOT(onCameraStateChanged()));
            onCameraStateChanged();

            QVideoDeviceSelectorControl* selector = selectorFromCamera(m_camera);
            m_deviceSelector = selector;
            connect(m_deviceSelector, SIGNAL(selectedDeviceChanged(int)),
                    this, SLOT(onSelectedDeviceChanged(int)));
        }

        Q_EMIT cameraChanged();
    }
}

void AdvancedCameraSettings::onSelectedDeviceChanged(int index)
{
    Q_UNUSED(index);

    m_videoSupportedResolutions.clear();

    Q_EMIT resolutionChanged();
    Q_EMIT maximumResolutionChanged();
    Q_EMIT fittingResolutionChanged();
    Q_EMIT hasFlashChanged();
    Q_EMIT videoSupportedResolutionsChanged();
}

void AdvancedCameraSettings::readCapabilities()
{
    m_viewFinderControl = viewfinderFromCamera(m_camera);
    m_cameraControl = camcontrolFromCamera(m_camera);
    if (m_cameraControl) {
        QObject::connect(m_cameraControl,
                         SIGNAL(captureModeChanged(QCamera::CaptureModes)),
                         this, SIGNAL(resolutionChanged()));
        QObject::connect(m_cameraControl,
                         SIGNAL(captureModeChanged(QCamera::CaptureModes)),
                         this, SIGNAL(maximumResolutionChanged()));
        QObject::connect(m_cameraControl,
                         SIGNAL(captureModeChanged(QCamera::CaptureModes)),
                         this, SIGNAL(fittingResolutionChanged()));
    }

    m_cameraFlashControl = flashControlFromCamera(m_camera);
    m_cameraExposureControl = exposureControlFromCamera(m_camera);

    if (m_cameraExposureControl) {
        QVariant exposureMode = m_hdrEnabled ? QVariant::fromValue(ExposureHdr)
                                             : QVariant::fromValue(QCameraExposure::ExposureAuto);
        m_cameraExposureControl->setValue(QCameraExposureControl::ExposureMode, exposureMode);
        QObject::connect(m_cameraExposureControl,
                         SIGNAL(actualValueChanged(int)),
                         this, SLOT(onExposureValueChanged(int)));
    }

    m_imageEncoderControl = imageEncoderControlFromCamera(m_camera);
    m_videoEncoderControl = videoEncoderControlFromCamera(m_camera);
    m_cameraInfoControl = cameraInfoControlFromCamera(m_camera);
    m_videoSupportedResolutions.clear();

    Q_EMIT resolutionChanged();
    Q_EMIT maximumResolutionChanged();
    Q_EMIT fittingResolutionChanged();
    Q_EMIT hasFlashChanged();
    Q_EMIT hasHdrChanged();
    Q_EMIT hdrEnabledChanged();
    Q_EMIT encodingQualityChanged();
    Q_EMIT videoSupportedResolutionsChanged();
}

void AdvancedCameraSettings::onCameraStateChanged()
{
    if (m_camera->state() == QCamera::LoadedState || m_camera->state() == QCamera::ActiveState) {
        readCapabilities();
    }
}

QSize AdvancedCameraSettings::resolution() const
{
    if (m_viewFinderControl != 0) {
        QVariant result = m_viewFinderControl->viewfinderParameter(QCameraViewfinderSettingsControl::Resolution);
        if (result.isValid()) {
            return result.toSize();
        }
    }

    return QSize();
}

QSize AdvancedCameraSettings::imageCaptureResolution() const
{
    if (m_imageEncoderControl != 0) {
        return m_imageEncoderControl->imageSettings().resolution();
    }

    return QSize();
}

QSize AdvancedCameraSettings::videoRecorderResolution() const
{
    if (m_videoEncoderControl != 0) {
        return m_videoEncoderControl->videoSettings().resolution();
    }

    return QSize();
}

QSize AdvancedCameraSettings::maximumResolution() const
{
    if (m_imageEncoderControl) {
        QList<QSize> sizes = m_imageEncoderControl->supportedResolutions(
                                       m_imageEncoderControl->imageSettings());

        QSize maximumSize;
        long maximumPixels = 0;

        QList<QSize>::const_iterator it = sizes.begin();
        while (it != sizes.end()) {
            const long pixels = ((long)((*it).width())) * ((long)((*it).height()));
            if (pixels > maximumPixels) {
                maximumSize = *it;
                maximumPixels = pixels;
            }
            ++it;
        }

        return maximumSize;
    }

    return QSize();
}

float AdvancedCameraSettings::getScreenAspectRatio() const
{
    float screenAspectRatio;
    QScreen *screen = QGuiApplication::primaryScreen();
    Q_ASSERT(screen);
    const int kScreenWidth = screen->geometry().width();
    const int kScreenHeight = screen->geometry().height();
    Q_ASSERT(kScreenWidth > 0 && kScreenHeight > 0);

    screenAspectRatio = (kScreenWidth > kScreenHeight) ?
        ((float)kScreenWidth / (float)kScreenHeight) : ((float)kScreenHeight / (float)kScreenWidth);

    return screenAspectRatio;
}

QSize AdvancedCameraSettings::fittingResolution() const
{
    QList<float> prioritizedAspectRatios;
    prioritizedAspectRatios.append(getScreenAspectRatio());
    const float backAspectRatios[4] = { 16.0f/9.0f, 3.0f/2.0f, 4.0f/3.0f, 5.0f/4.0f };
    for (int i=0; i<4; ++i) {
        if (!prioritizedAspectRatios.contains(backAspectRatios[i])) {
            prioritizedAspectRatios.append(backAspectRatios[i]);
        }
    }

    if (m_imageEncoderControl) {
        QList<QSize> sizes = m_imageEncoderControl->supportedResolutions(
                                       m_imageEncoderControl->imageSettings());

        QSize optimalSize;
        long optimalPixels = 0;

        if (!sizes.empty()) {
            float aspectRatio;

            // Loop over all reported camera resolutions until we find the highest
            // one that matches the current prioritized aspect ratio. If it doesn't
            // find one on the current aspect ration, it selects the next ratio and
            // tries again.
            QList<float>::const_iterator ratioIt = prioritizedAspectRatios.begin();
            while (ratioIt != prioritizedAspectRatios.end()) {
                // Don't update the aspect ratio when using this function for finding
                // the optimal thumbnail size as it will affect the preview window size
                aspectRatio = (*ratioIt);

                QList<QSize>::const_iterator it = sizes.begin();
                while (it != sizes.end()) {
                    const float ratio = (float)(*it).width() / (float)(*it).height();
                    const long pixels = ((long)((*it).width())) * ((long)((*it).height()));
                    const float EPSILON = 0.02;
                    if (fabs(ratio - aspectRatio) < EPSILON && pixels > optimalPixels) {
                        optimalSize = *it;
                        optimalPixels = pixels;
                    }
                    ++it;
                }
                if (optimalPixels > 0) break;
                ++ratioIt;
            }
        }

        return optimalSize;
    }

    return QSize();
}

QStringList AdvancedCameraSettings::videoSupportedResolutions()
{
    if (m_videoEncoderControl) {
        if (m_videoSupportedResolutions.isEmpty()) {
            QString currentDeviceName = m_deviceSelector->deviceName(m_deviceSelector->selectedDevice());
            QCamera::Position cameraPosition = m_cameraInfoControl->cameraPosition(currentDeviceName);
            QList<QSize> sizes = m_videoEncoderControl->supportedResolutions(
                                                m_videoEncoderControl->videoSettings());
            Q_FOREACH(QSize size, sizes) {
                // Workaround for bug https://bugs.launchpad.net/ubuntu/+source/libhybris/+bug/1408650
                // When using the front camera on krillin, using resolution 640x480 does
                // not work properly and results in stretched videos. Remove it from
                // the list of supported resolutions.
                if (cameraPosition == QCamera::FrontFace &&
                    size.width() == 640 && size.height() == 480) {
                    continue;
                }
                m_videoSupportedResolutions.append(QString("%1x%2").arg(size.width()).arg(size.height()));
            }
        }
        return m_videoSupportedResolutions;
    } else {
        return QStringList();
    }
}


bool AdvancedCameraSettings::hasFlash() const
{
    if (m_cameraFlashControl) {
        return m_cameraFlashControl->isFlashModeSupported(QCameraExposure::FlashAuto)
            && m_cameraFlashControl->isFlashModeSupported(QCameraExposure::FlashOff)
            && m_cameraFlashControl->isFlashModeSupported(QCameraExposure::FlashOn);
    } else {
        return false;
    }
}

bool AdvancedCameraSettings::hasHdr() const
{
    if (m_cameraExposureControl) {
        bool continuous;
        if (m_cameraExposureControl->isParameterSupported(QCameraExposureControl::ExposureMode)) {
            QVariantList range = m_cameraExposureControl->supportedParameterRange(QCameraExposureControl::ExposureMode, &continuous);
            return range.contains(QVariant::fromValue(ExposureHdr));
        }
    } else {
        return false;
    }
}

bool AdvancedCameraSettings::hdrEnabled() const
{
    return m_hdrEnabled;
}

void AdvancedCameraSettings::setHdrEnabled(bool enabled)
{
    if (enabled != m_hdrEnabled) {
        m_hdrEnabled = enabled;
        if (m_cameraExposureControl) {
            QVariant exposureMode = enabled ? QVariant::fromValue(ExposureHdr)
                                            : QVariant::fromValue(QCameraExposure::ExposureAuto);
            m_cameraExposureControl->setValue(QCameraExposureControl::ExposureMode, exposureMode);
        } else {
            Q_EMIT hdrEnabledChanged();
        }
    }
}

int AdvancedCameraSettings::encodingQuality() const
{
    if (m_imageEncoderControl) {
        return m_imageEncoderControl->imageSettings().quality();
    } else {
        return QMultimedia::NormalQuality;
    }
}

void AdvancedCameraSettings::setEncodingQuality(int quality)
{
    if (m_imageEncoderControl) {
        QImageEncoderSettings settings;
        settings.setQuality((QMultimedia::EncodingQuality)quality);
        m_imageEncoderControl->setImageSettings(settings);
    }
}

void AdvancedCameraSettings::onExposureValueChanged(int parameter)
{
    if (parameter == QCameraExposureControl::ExposureMode) {
        Q_EMIT hdrEnabledChanged();
    }
}