| 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 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 "qmediaresource.h" | 
| 41 |  | 
| 42 | #include <QtCore/qsize.h> | 
| 43 | #include <QtCore/qurl.h> | 
| 44 | #include <QtCore/qvariant.h> | 
| 45 |  | 
| 46 | QT_BEGIN_NAMESPACE | 
| 47 |  | 
| 48 | static void qRegisterMediaResourceMetaTypes() | 
| 49 | { | 
| 50 |     qRegisterMetaType<QMediaResource>(); | 
| 51 |     qRegisterMetaType<QMediaResourceList>(); | 
| 52 | } | 
| 53 |  | 
| 54 | Q_CONSTRUCTOR_FUNCTION(qRegisterMediaResourceMetaTypes) | 
| 55 |  | 
| 56 |  | 
| 57 | /*! | 
| 58 |     \class QMediaResource | 
| 59 |  | 
| 60 |     \brief The QMediaResource class provides a description of a media resource. | 
| 61 |     \inmodule QtMultimedia | 
| 62 |     \ingroup multimedia | 
| 63 |     \ingroup multimedia_playback | 
| 64 |     \obsolete | 
| 65 |  | 
| 66 |     A media resource is composed of a \l {url()}{URL} containing the | 
| 67 |     location of the resource and a set of properties that describe the | 
| 68 |     format of the resource.  The properties provide a means to assess a | 
| 69 |     resource without first attempting to load it, and in situations where | 
| 70 |     media be represented by multiple alternative representations provide a | 
| 71 |     means to select the appropriate resource. | 
| 72 |  | 
| 73 |     Media made available by a remote services can often be available in | 
| 74 |     multiple encodings or quality levels, this allows a client to select | 
| 75 |     an appropriate resource based on considerations such as codecs supported, | 
| 76 |     network bandwidth, and display constraints.  QMediaResource includes | 
| 77 |     information such as the \l {mimeType()}{MIME type}, \l {audioCodec()}{audio} | 
| 78 |     and \l {videoCodec()}{video} codecs, \l {audioBitRate()}{audio} and | 
| 79 |     \l {videoBitRate()}{video} bit rates, and \l {resolution()}{resolution} | 
| 80 |     so these constraints and others can be evaluated. | 
| 81 |  | 
| 82 |     The only mandatory property of a QMediaResource is the url(). | 
| 83 |  | 
| 84 |     \sa QMediaContent | 
| 85 | */ | 
| 86 |  | 
| 87 | /*! | 
| 88 |     \typedef QMediaResourceList | 
| 89 |  | 
| 90 |     Synonym for \c QList<QMediaResource> | 
| 91 |  | 
| 92 |     \relates QMediaResource | 
| 93 | */ | 
| 94 |  | 
| 95 | /*! | 
| 96 |     Constructs a null media resource. | 
| 97 | */ | 
| 98 | QMediaResource::QMediaResource() | 
| 99 | { | 
| 100 | } | 
| 101 |  | 
| 102 | /*! | 
| 103 |     Constructs a media resource with the given \a mimeType from a \a url. | 
| 104 | */ | 
| 105 | QMediaResource::QMediaResource(const QUrl &url, const QString &mimeType) | 
| 106 | { | 
| 107 |     values.insert(akey: Url, avalue: url); | 
| 108 |     values.insert(akey: MimeType, avalue: mimeType); | 
| 109 | } | 
| 110 |  | 
| 111 | /*! | 
| 112 |     Constructs a media resource with the given \a mimeType from a network \a request. | 
| 113 | */ | 
| 114 | QMediaResource::QMediaResource(const QNetworkRequest &request, const QString &mimeType) | 
| 115 | { | 
| 116 |     values.insert(akey: Request, avalue: QVariant::fromValue(value: request)); | 
| 117 |     values.insert(akey: Url, avalue: request.url()); | 
| 118 |     values.insert(akey: MimeType, avalue: mimeType); | 
| 119 | } | 
| 120 |  | 
| 121 | /*! | 
| 122 |     Constructs a copy of a media resource \a other. | 
| 123 | */ | 
| 124 | QMediaResource::QMediaResource(const QMediaResource &other) | 
| 125 |     : values(other.values) | 
| 126 | { | 
| 127 | } | 
| 128 |  | 
| 129 | /*! | 
| 130 |     Assigns the value of \a other to a media resource. | 
| 131 | */ | 
| 132 | QMediaResource &QMediaResource::operator =(const QMediaResource &other) | 
| 133 | { | 
| 134 |     values = other.values; | 
| 135 |  | 
| 136 |     return *this; | 
| 137 | } | 
| 138 |  | 
| 139 | /*! | 
| 140 |     Destroys a media resource. | 
| 141 | */ | 
| 142 | QMediaResource::~QMediaResource() | 
| 143 | { | 
| 144 | } | 
| 145 |  | 
| 146 |  | 
| 147 | /*! | 
| 148 |     Compares a media resource to \a other. | 
| 149 |  | 
| 150 |     Returns true if the resources are identical, and false otherwise. | 
| 151 | */ | 
| 152 | bool QMediaResource::operator ==(const QMediaResource &other) const | 
| 153 | { | 
| 154 |     // Compare requests directly as QNetworkRequests are "custom types". | 
| 155 |     for (auto it = values.cbegin(), end = values.cend(); it != end; ++it) { | 
| 156 |         switch (it.key()) { | 
| 157 |         case Request: | 
| 158 |             if (request() != other.request()) | 
| 159 |                 return false; | 
| 160 |         break; | 
| 161 |         default: | 
| 162 |             if (it.value() != other.values.value(akey: it.key())) | 
| 163 |                 return false; | 
| 164 |         } | 
| 165 |     } | 
| 166 |     return true; | 
| 167 | } | 
| 168 |  | 
| 169 | /*! | 
| 170 |     Compares a media resource to \a other. | 
| 171 |  | 
| 172 |     Returns true if they are different, and false otherwise. | 
| 173 | */ | 
| 174 | bool QMediaResource::operator !=(const QMediaResource &other) const | 
| 175 | { | 
| 176 |     return !(*this == other); | 
| 177 | } | 
| 178 |  | 
| 179 | /*! | 
| 180 |     Identifies if a media resource is null. | 
| 181 |  | 
| 182 |     Returns true if the resource is null, and false otherwise. | 
| 183 | */ | 
| 184 | bool QMediaResource::isNull() const | 
| 185 | { | 
| 186 |     return values.isEmpty(); | 
| 187 | } | 
| 188 |  | 
| 189 | /*! | 
| 190 |     Returns the URL of a media resource. | 
| 191 | */ | 
| 192 | QUrl QMediaResource::url() const | 
| 193 | { | 
| 194 |     return qvariant_cast<QUrl>(v: values.value(akey: Url)); | 
| 195 | } | 
| 196 |  | 
| 197 | /*! | 
| 198 |     Returns the network request associated with this media resource. | 
| 199 | */ | 
| 200 | QNetworkRequest QMediaResource::request() const | 
| 201 | { | 
| 202 |     if(values.contains(akey: Request)) | 
| 203 |         return qvariant_cast<QNetworkRequest>(v: values.value(akey: Request)); | 
| 204 |  | 
| 205 |     return QNetworkRequest(url()); | 
| 206 | } | 
| 207 |  | 
| 208 | /*! | 
| 209 |     Returns the MIME type of a media resource. | 
| 210 |  | 
| 211 |     This may be null if the MIME type is unknown. | 
| 212 | */ | 
| 213 | QString QMediaResource::mimeType() const | 
| 214 | { | 
| 215 |     return qvariant_cast<QString>(v: values.value(akey: MimeType)); | 
| 216 | } | 
| 217 |  | 
| 218 | /*! | 
| 219 |     Returns the language of a media resource as an ISO 639-2 code. | 
| 220 |  | 
| 221 |     This may be null if the language is unknown. | 
| 222 | */ | 
| 223 | QString QMediaResource::language() const | 
| 224 | { | 
| 225 |     return qvariant_cast<QString>(v: values.value(akey: Language)); | 
| 226 | } | 
| 227 |  | 
| 228 | /*! | 
| 229 |     Sets the \a language of a media resource. | 
| 230 | */ | 
| 231 | void QMediaResource::setLanguage(const QString &language) | 
| 232 | { | 
| 233 |     if (!language.isNull()) | 
| 234 |         values.insert(akey: Language, avalue: language); | 
| 235 |     else | 
| 236 |         values.remove(akey: Language); | 
| 237 | } | 
| 238 |  | 
| 239 | /*! | 
| 240 |     Returns the audio codec of a media resource. | 
| 241 |  | 
| 242 |     This may be null if the media resource does not contain an audio stream, or the codec is | 
| 243 |     unknown. | 
| 244 | */ | 
| 245 | QString QMediaResource::audioCodec() const | 
| 246 | { | 
| 247 |     return qvariant_cast<QString>(v: values.value(akey: AudioCodec)); | 
| 248 | } | 
| 249 |  | 
| 250 | /*! | 
| 251 |     Sets the audio \a codec of a media resource. | 
| 252 | */ | 
| 253 | void QMediaResource::setAudioCodec(const QString &codec) | 
| 254 | { | 
| 255 |     if (!codec.isNull()) | 
| 256 |         values.insert(akey: AudioCodec, avalue: codec); | 
| 257 |     else | 
| 258 |         values.remove(akey: AudioCodec); | 
| 259 | } | 
| 260 |  | 
| 261 | /*! | 
| 262 |     Returns the video codec of a media resource. | 
| 263 |  | 
| 264 |     This may be null if the media resource does not contain a video stream, or the codec is | 
| 265 |     unknonwn. | 
| 266 | */ | 
| 267 | QString QMediaResource::videoCodec() const | 
| 268 | { | 
| 269 |     return qvariant_cast<QString>(v: values.value(akey: VideoCodec)); | 
| 270 | } | 
| 271 |  | 
| 272 | /*! | 
| 273 |     Sets the video \a codec of media resource. | 
| 274 | */ | 
| 275 | void QMediaResource::setVideoCodec(const QString &codec) | 
| 276 | { | 
| 277 |     if (!codec.isNull()) | 
| 278 |         values.insert(akey: VideoCodec, avalue: codec); | 
| 279 |     else | 
| 280 |         values.remove(akey: VideoCodec); | 
| 281 | } | 
| 282 |  | 
| 283 | /*! | 
| 284 |     Returns the size in bytes of a media resource. | 
| 285 |  | 
| 286 |     This may be zero if the size is unknown. | 
| 287 | */ | 
| 288 | qint64 QMediaResource::dataSize() const | 
| 289 | { | 
| 290 |     return qvariant_cast<qint64>(v: values.value(akey: DataSize)); | 
| 291 | } | 
| 292 |  | 
| 293 | /*! | 
| 294 |     Sets the \a size in bytes of a media resource. | 
| 295 | */ | 
| 296 | void QMediaResource::setDataSize(const qint64 size) | 
| 297 | { | 
| 298 |     if (size != 0) | 
| 299 |         values.insert(akey: DataSize, avalue: size); | 
| 300 |     else | 
| 301 |         values.remove(akey: DataSize); | 
| 302 | } | 
| 303 |  | 
| 304 | /*! | 
| 305 |     Returns the bit rate in bits per second of a media resource's audio stream. | 
| 306 |  | 
| 307 |     This may be zero if the bit rate is unknown, or the resource contains no audio stream. | 
| 308 | */ | 
| 309 | int QMediaResource::audioBitRate() const | 
| 310 | { | 
| 311 |     return values.value(akey: AudioBitRate).toInt(); | 
| 312 | } | 
| 313 |  | 
| 314 | /*! | 
| 315 |     Sets the bit \a rate in bits per second of a media resource's video stream. | 
| 316 | */ | 
| 317 | void QMediaResource::setAudioBitRate(int rate) | 
| 318 | { | 
| 319 |     if (rate != 0) | 
| 320 |         values.insert(akey: AudioBitRate, avalue: rate); | 
| 321 |     else | 
| 322 |         values.remove(akey: AudioBitRate); | 
| 323 | } | 
| 324 |  | 
| 325 | /*! | 
| 326 |     Returns the audio sample rate of a media resource. | 
| 327 |  | 
| 328 |     This may be zero if the sample size is unknown, or the resource contains no audio stream. | 
| 329 | */ | 
| 330 | int QMediaResource::sampleRate() const | 
| 331 | { | 
| 332 |     return qvariant_cast<int>(v: values.value(akey: SampleRate)); | 
| 333 | } | 
| 334 |  | 
| 335 | /*! | 
| 336 |     Sets the audio \a sampleRate of a media resource. | 
| 337 | */ | 
| 338 | void QMediaResource::setSampleRate(int sampleRate) | 
| 339 | { | 
| 340 |     if (sampleRate != 0) | 
| 341 |         values.insert(akey: SampleRate, avalue: sampleRate); | 
| 342 |     else | 
| 343 |         values.remove(akey: SampleRate); | 
| 344 | } | 
| 345 |  | 
| 346 | /*! | 
| 347 |     Returns the number of audio channels in a media resource. | 
| 348 |  | 
| 349 |     This may be zero if the sample size is unknown, or the resource contains no audio stream. | 
| 350 | */ | 
| 351 | int QMediaResource::channelCount() const | 
| 352 | { | 
| 353 |     return qvariant_cast<int>(v: values.value(akey: ChannelCount)); | 
| 354 | } | 
| 355 |  | 
| 356 | /*! | 
| 357 |     Sets the number of audio \a channels in a media resource. | 
| 358 | */ | 
| 359 | void QMediaResource::setChannelCount(int channels) | 
| 360 | { | 
| 361 |     if (channels != 0) | 
| 362 |         values.insert(akey: ChannelCount, avalue: channels); | 
| 363 |     else | 
| 364 |         values.remove(akey: ChannelCount); | 
| 365 | } | 
| 366 |  | 
| 367 | /*! | 
| 368 |     Returns the bit rate in bits per second of a media resource's video stream. | 
| 369 |  | 
| 370 |     This may be zero if the bit rate is unknown, or the resource contains no video stream. | 
| 371 | */ | 
| 372 | int QMediaResource::videoBitRate() const | 
| 373 | { | 
| 374 |     return values.value(akey: VideoBitRate).toInt(); | 
| 375 | } | 
| 376 |  | 
| 377 | /*! | 
| 378 |     Sets the bit \a rate in bits per second of a media resource's video stream. | 
| 379 | */ | 
| 380 | void QMediaResource::setVideoBitRate(int rate) | 
| 381 | { | 
| 382 |     if (rate != 0) | 
| 383 |         values.insert(akey: VideoBitRate, avalue: rate); | 
| 384 |     else | 
| 385 |         values.remove(akey: VideoBitRate); | 
| 386 | } | 
| 387 |  | 
| 388 | /*! | 
| 389 |     Returns the resolution in pixels of a media resource. | 
| 390 |  | 
| 391 |     This may be null is the resolution is unknown, or the resource contains no pixel data (i.e. the | 
| 392 |     resource is an audio stream. | 
| 393 | */ | 
| 394 | QSize QMediaResource::resolution() const | 
| 395 | { | 
| 396 |     return qvariant_cast<QSize>(v: values.value(akey: Resolution)); | 
| 397 | } | 
| 398 |  | 
| 399 | /*! | 
| 400 |     Sets the \a resolution in pixels of a media resource. | 
| 401 | */ | 
| 402 | void QMediaResource::setResolution(const QSize &resolution) | 
| 403 | { | 
| 404 |     if (resolution.width() != -1 || resolution.height() != -1) | 
| 405 |         values.insert(akey: Resolution, avalue: resolution); | 
| 406 |     else | 
| 407 |         values.remove(akey: Resolution); | 
| 408 | } | 
| 409 |  | 
| 410 | /*! | 
| 411 |     Sets the \a width and \a height in pixels of a media resource. | 
| 412 | */ | 
| 413 | void QMediaResource::setResolution(int width, int height) | 
| 414 | { | 
| 415 |     if (width != -1 || height != -1) | 
| 416 |         values.insert(akey: Resolution, avalue: QSize(width, height)); | 
| 417 |     else | 
| 418 |         values.remove(akey: Resolution); | 
| 419 | } | 
| 420 | QT_END_NAMESPACE | 
| 421 |  |