| 1 | /**************************************************************************** | 
| 2 | ** | 
| 3 | ** Copyright (C) 2016 The Qt Company Ltd. | 
| 4 | ** Contact: https://www.qt.io/licensing/ | 
| 5 | ** | 
| 6 | ** This file is part of the plugins of the Qt Toolkit. | 
| 7 | ** | 
| 8 | ** $QT_BEGIN_LICENSE:LGPL$ | 
| 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 | ** GNU Lesser General Public License Usage | 
| 18 | ** Alternatively, this file may be used under the terms of the GNU Lesser | 
| 19 | ** General Public License version 3 as published by the Free Software | 
| 20 | ** Foundation and appearing in the file LICENSE.LGPL3 included in the | 
| 21 | ** packaging of this file. Please review the following information to | 
| 22 | ** ensure the GNU Lesser General Public License version 3 requirements | 
| 23 | ** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. | 
| 24 | ** | 
| 25 | ** GNU General Public License Usage | 
| 26 | ** Alternatively, this file may be used under the terms of the GNU | 
| 27 | ** General Public License version 2.0 or (at your option) the GNU General | 
| 28 | ** Public license version 3 or any later version approved by the KDE Free | 
| 29 | ** Qt Foundation. The licenses are as published by the Free Software | 
| 30 | ** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 | 
| 31 | ** included in the packaging of this file. Please review the following | 
| 32 | ** information to ensure the GNU General Public License requirements will | 
| 33 | ** be met: https://www.gnu.org/licenses/gpl-2.0.html and | 
| 34 | ** https://www.gnu.org/licenses/gpl-3.0.html. | 
| 35 | ** | 
| 36 | ** $QT_END_LICENSE$ | 
| 37 | ** | 
| 38 | ****************************************************************************/ | 
| 39 |  | 
| 40 | #include "qdeclarativemultimediaglobal_p.h" | 
| 41 |  | 
| 42 | #include <qcamerainfo.h> | 
| 43 | #include <qjsengine.h> | 
| 44 |  | 
| 45 | QT_BEGIN_NAMESPACE | 
| 46 |  | 
| 47 | /*! | 
| 48 | \qmltype QtMultimedia | 
| 49 | \inqmlmodule QtMultimedia | 
| 50 | \ingroup multimedia_qml | 
| 51 | \since 5.4 | 
| 52 | \brief Provides a global object with useful functions from Qt Multimedia. | 
| 53 |  | 
| 54 | The \c QtMultimedia object is a global object with utility functions and properties. | 
| 55 |  | 
| 56 | It is not instantiable; to use it, call the members of the global \c QtMultimedia object directly. | 
| 57 | For example: | 
| 58 |  | 
| 59 | \qml | 
| 60 | Camera { | 
| 61 |     deviceId: QtMultimedia.defaultCamera.deviceId | 
| 62 | } | 
| 63 | \endqml | 
| 64 |  | 
| 65 | */ | 
| 66 |  | 
| 67 | /*! | 
| 68 |     \qmlproperty object QtMultimedia::QtMultimedia::defaultCamera | 
| 69 |     \readonly | 
| 70 |  | 
| 71 |     The \c defaultCamera object provides information about the default camera on the system. | 
| 72 |  | 
| 73 |     Its properties are \c deviceId, \c displayName, \c position and \c orientation. See | 
| 74 |     \l{QtMultimedia::QtMultimedia::availableCameras}{availableCameras} for a description of each | 
| 75 |     of them. | 
| 76 |  | 
| 77 |     If there is no default camera, \c defaultCamera.deviceId will contain an empty string. | 
| 78 |  | 
| 79 |     \note This property is static; it is not updated if the system's default camera changes after the | 
| 80 |     application started. | 
| 81 | */ | 
| 82 |  | 
| 83 | /*! | 
| 84 |     \qmlproperty list<object> QtMultimedia::QtMultimedia::availableCameras | 
| 85 |     \readonly | 
| 86 |  | 
| 87 |     This property provides information about the cameras available on the system. | 
| 88 |  | 
| 89 |     Each object in the list has the following properties: | 
| 90 |  | 
| 91 |     \table | 
| 92 |     \row | 
| 93 |     \li \c deviceId | 
| 94 |     \li | 
| 95 |     This read-only property holds the unique identifier of the camera. | 
| 96 |  | 
| 97 |     You can choose which device to use with a \l Camera object by setting its | 
| 98 |     \l{Camera::deviceId}{deviceId} property to this value. | 
| 99 |  | 
| 100 |     \row | 
| 101 |     \li \c displayName | 
| 102 |     \li | 
| 103 |     This read-only property holds the human-readable name of the camera. | 
| 104 |     You can use this property to display the name of the camera in a user interface. | 
| 105 |  | 
| 106 |     \row | 
| 107 |     \li \c position | 
| 108 |     \li | 
| 109 |     This read-only property holds the physical position of the camera on the hardware system. | 
| 110 |     Please see \l{Camera::position}{Camera.position} for more information. | 
| 111 |  | 
| 112 |     \row | 
| 113 |     \li \c orientation | 
| 114 |     \li | 
| 115 |     This read-only property holds the physical orientation of the camera sensor. | 
| 116 |     Please see \l{Camera::orientation}{Camera.orientation} for more information. | 
| 117 |  | 
| 118 |     \endtable | 
| 119 |  | 
| 120 |     \note This property is static; it is not updated when cameras are added or removed from | 
| 121 |     the system, like USB cameras on a desktop platform. | 
| 122 |  | 
| 123 |     The following example shows how to display a list of available cameras. The user can change | 
| 124 |     the active camera by selecting one of the items in the list. | 
| 125 |  | 
| 126 |     \qml | 
| 127 |     Item { | 
| 128 |  | 
| 129 |         Camera { | 
| 130 |             id: camera | 
| 131 |         } | 
| 132 |  | 
| 133 |         VideoOutput { | 
| 134 |             anchors.fill: parent | 
| 135 |             source: camera | 
| 136 |         } | 
| 137 |  | 
| 138 |         ListView { | 
| 139 |             anchors.fill: parent | 
| 140 |  | 
| 141 |             model: QtMultimedia.availableCameras | 
| 142 |             delegate: Text { | 
| 143 |                 text: modelData.displayName | 
| 144 |  | 
| 145 |                 MouseArea { | 
| 146 |                     anchors.fill: parent | 
| 147 |                     onClicked: camera.deviceId = modelData.deviceId | 
| 148 |                 } | 
| 149 |             } | 
| 150 |         } | 
| 151 |     } | 
| 152 |  | 
| 153 |     \endqml | 
| 154 | */ | 
| 155 |  | 
| 156 | static QJSValue cameraInfoToJSValue(QJSEngine *jsEngine, const QCameraInfo &camera) | 
| 157 | { | 
| 158 |     QJSValue o = jsEngine->newObject(); | 
| 159 |     o.setProperty(QStringLiteral("deviceId" ), value: camera.deviceName()); | 
| 160 |     o.setProperty(QStringLiteral("displayName" ), value: camera.description()); | 
| 161 |     o.setProperty(QStringLiteral("position" ), value: int(camera.position())); | 
| 162 |     o.setProperty(QStringLiteral("orientation" ), value: camera.orientation()); | 
| 163 |     return o; | 
| 164 | } | 
| 165 |  | 
| 166 | QDeclarativeMultimediaGlobal::QDeclarativeMultimediaGlobal(QJSEngine *engine, QObject *parent) | 
| 167 |     : QObject(parent) | 
| 168 |     , m_engine(engine) | 
| 169 | { | 
| 170 | } | 
| 171 |  | 
| 172 | QJSValue QDeclarativeMultimediaGlobal::defaultCamera() const | 
| 173 | { | 
| 174 |     return cameraInfoToJSValue(jsEngine: m_engine, camera: QCameraInfo::defaultCamera()); | 
| 175 | } | 
| 176 |  | 
| 177 | QJSValue QDeclarativeMultimediaGlobal::availableCameras() const | 
| 178 | { | 
| 179 |     QList<QCameraInfo> cameras = QCameraInfo::availableCameras(); | 
| 180 |     QJSValue availableCameras = m_engine->newArray(length: cameras.count()); | 
| 181 |     for (int i = 0; i < cameras.count(); ++i) | 
| 182 |         availableCameras.setProperty(arrayIndex: i, value: cameraInfoToJSValue(jsEngine: m_engine, camera: cameras.at(i))); | 
| 183 |     return availableCameras; | 
| 184 | } | 
| 185 |  | 
| 186 | /*! | 
| 187 |     \qmlmethod real QtMultimedia::QtMultimedia::convertVolume(real volume, VolumeScale from, VolumeScale to) | 
| 188 |  | 
| 189 |     Converts an audio \a volume \a from a volume scale \a to another, and returns the result. | 
| 190 |  | 
| 191 |     Depending on the context, different scales are used to represent audio volume. All Qt Multimedia | 
| 192 |     classes that have an audio volume use a linear scale, the reason is that the loudness of a | 
| 193 |     speaker is controlled by modulating its voltage on a linear scale. The human ear on the other | 
| 194 |     hand, perceives loudness in a logarithmic way. Using a logarithmic scale for volume controls | 
| 195 |     is therefore appropriate in most applications. The decibel scale is logarithmic by nature and | 
| 196 |     is commonly used to define sound levels, it is usually used for UI volume controls in | 
| 197 |     professional audio applications. The cubic scale is a computationally cheap approximation of a | 
| 198 |     logarithmic scale, it provides more control over lower volume levels. | 
| 199 |  | 
| 200 |     Valid values for \a from and \a to are: | 
| 201 |     \list | 
| 202 |     \li QtMultimedia.LinearVolumeScale - Linear scale. \c 0.0 (0%) is silence and \c 1.0 (100%) is | 
| 203 |         full volume. All Qt Multimedia types that have an audio volume use a linear scale. | 
| 204 |     \li QtMultimedia.CubicVolumeScale - Cubic scale. \c 0.0 (0%) is silence and \c 1.0 (100%) is full | 
| 205 |         volume. | 
| 206 |     \li QtMultimedia.LogarithmicVolumeScale - Logarithmic scale. \c 0.0 (0%) is silence and \c 1.0 | 
| 207 |         (100%) is full volume. UI volume controls should usually use a logarithmic scale. | 
| 208 |     \li QtMultimedia.DecibelVolumeScale - Decibel (dB, amplitude) logarithmic scale. \c -200 is | 
| 209 |         silence and \c 0 is full volume. | 
| 210 |     \endlist | 
| 211 |  | 
| 212 |     The following example shows how the volume value from a UI volume control can be converted so | 
| 213 |     that the perceived increase in loudness is the same when increasing the volume control from 0.2 | 
| 214 |     to 0.3 as it is from 0.5 to 0.6: | 
| 215 |  | 
| 216 |     \code | 
| 217 |     Slider { | 
| 218 |         id: volumeSlider | 
| 219 |  | 
| 220 |         property real volume: QtMultimedia.convertVolume(volumeSlider.value, | 
| 221 |                                                          QtMultimedia.LogarithmicVolumeScale, | 
| 222 |                                                          QtMultimedia.LinearVolumeScale) | 
| 223 |     } | 
| 224 |  | 
| 225 |     MediaPlayer { | 
| 226 |         volume: volumeSlider.volume | 
| 227 |     } | 
| 228 |     \endcode | 
| 229 |  | 
| 230 |     \since 5.8 | 
| 231 | */ | 
| 232 | qreal QDeclarativeMultimediaGlobal::convertVolume(qreal volume, | 
| 233 |                                                   QDeclarativeMultimediaGlobal::VolumeScale from, | 
| 234 |                                                   QDeclarativeMultimediaGlobal::VolumeScale to) const | 
| 235 | { | 
| 236 |     return QAudio::convertVolume(volume, from: QAudio::VolumeScale(from), to: QAudio::VolumeScale(to)); | 
| 237 | } | 
| 238 |  | 
| 239 | QT_END_NAMESPACE | 
| 240 |  |