Blackberry: Fix playback from certain URLs
authorThomas McGuire <thomas.mcguire.qnx@kdab.com>
Thu, 26 Jul 2012 14:06:17 +0000 (16:06 +0200)
committerQt by Nokia <qt-info@nokia.com>
Mon, 30 Jul 2012 13:27:06 +0000 (15:27 +0200)
Previously, playback from URLs of the form "file:path/to/file" did not
work.

Change-Id: Iecd293d09f7c448438dbf259710c0aab218c2591
Reviewed-by: Sean Harmer <sean.harmer@kdab.com>
src/plugins/blackberry/bbmediaplayercontrol.cpp
src/plugins/blackberry/bbmediaplayercontrol.h

index 3a84149..1fe21de 100644 (file)
@@ -143,6 +143,42 @@ void BbMediaPlayerControl::closeConnection()
     }
 }
 
+QString BbMediaPlayerControl::resourcePathForUrl(const QUrl &url)
+{
+    // If this is a local file, mmrenderer expects the file:// prefix and an absolute path.
+    // We treat URLs without scheme as local files, most likely someone just forgot to set the
+    // file:// prefix when constructing the URL.
+    if (url.isLocalFile() || url.scheme().isEmpty()) {
+        QString relativeFilePath;
+        if (!url.scheme().isEmpty())
+            relativeFilePath = url.toLocalFile();
+        else
+            relativeFilePath = url.path();
+        const QFileInfo fileInfo(relativeFilePath);
+        return QStringLiteral("file://") + fileInfo.absoluteFilePath();
+
+    // QRC, copy to temporary file, as mmrenderer does not support resource files
+    } else if (url.scheme() == QStringLiteral("qrc")) {
+        const QString qrcPath = ':' + url.path();
+        const QFileInfo resourceFileInfo(qrcPath);
+        m_tempMediaFileName = QDir::tempPath() + QStringLiteral("/qtmedia_") +
+                              QUuid::createUuid().toString() + QStringLiteral(".") +
+                              resourceFileInfo.suffix();
+        if (!QFile::copy(qrcPath, m_tempMediaFileName)) {
+            const QString errorMsg =
+                QString("Failed to copy resource file to temporary file %1 for playback").arg(m_tempMediaFileName);
+            qBbMediaDebug() << errorMsg;
+            emit error(0, errorMsg);
+            return QString();
+        }
+        return m_tempMediaFileName;
+
+    // HTTP or similar URL, use as-is
+    } else {
+        return url.toString();
+    }
+}
+
 void BbMediaPlayerControl::attach()
 {
     if (m_media.isNull() || !m_context) {
@@ -159,35 +195,14 @@ void BbMediaPlayerControl::attach()
         return;
     }
 
-    // If this is a local file, use the full path as the resource identifier
-    QString mediaFile = m_media.canonicalUrl().toString();
-
-    // The mmrenderer does not support playback from resource files, so copy it to a temporary
-    // file
-    const QString resourcePrefix("qrc:");
-    if (mediaFile.startsWith(resourcePrefix)) {
-        mediaFile.remove(0, resourcePrefix.length() - 1);
-        const QFileInfo resourceFileInfo(mediaFile);
-        m_tempMediaFileName = QDir::tempPath() + "/qtmedia_" + QUuid::createUuid().toString() + "." +
-                              resourceFileInfo.suffix();
-        if (!QFile::copy(mediaFile, m_tempMediaFileName)) {
-            const QString errorMsg =
-                QString("Failed to copy resource file to temporary file %1 for playback").arg(m_tempMediaFileName);
-            qBbMediaDebug() << errorMsg;
-            emit error(0, errorMsg);
-            detach();
-            return;
-        }
-        mediaFile = m_tempMediaFileName;
-    }
-
-    if (m_media.canonicalUrl().scheme().isEmpty()) {
-        const QFileInfo fileInfo(mediaFile);
-        mediaFile = "file://" + fileInfo.absoluteFilePath();
+    const QString resourcePath = resourcePathForUrl(m_media.canonicalUrl());
+    if (resourcePath.isEmpty()) {
+        detach();
+        return;
     }
 
-    if (mmr_input_attach(m_context, QFile::encodeName(mediaFile), "track") != 0) {
-        emitMmError(QString("mmr_input_attach() for %1 failed").arg(mediaFile));
+    if (mmr_input_attach(m_context, QFile::encodeName(resourcePath), "track") != 0) {
+        emitMmError(QString("mmr_input_attach() for %1 failed").arg(resourcePath));
         setMediaStatus(QMediaPlayer::InvalidMedia);
         detach();
         return;
index e8a9ed8..2f304e0 100644 (file)
@@ -105,6 +105,7 @@ private Q_SLOTS:
     void continueLoadMedia();
 
 private:
+    QString resourcePathForUrl(const QUrl &url);
     void openConnection();
     void closeConnection();
     void attach();