summaryrefslogtreecommitdiffstats
path: root/src/plugins/mediaservices/qt7/mediaplayer/qt7playermetadata.mm
blob: 2ea778d6a12c0f3308996ec369d643d6f0995ac8 (plain)
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
/****************************************************************************
**
** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
** All rights reserved.
** Contact: Nokia Corporation (qt-info@nokia.com)
**
** This file is part of the plugins of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:LGPL$
** No Commercial Usage
** This file contains pre-release code and may not be distributed.
** You may use this file in accordance with the terms and conditions
** contained in the Technology Preview License Agreement accompanying
** this package.
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 2.1 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL included in the
** packaging of this file.  Please review the following information to
** ensure the GNU Lesser General Public License version 2.1 requirements
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** In addition, as a special exception, Nokia gives you certain additional
** rights.  These rights are described in the Nokia Qt LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
** If you have questions regarding the use of this file, please contact
** Nokia at qt-info@nokia.com.
**
**
**
**
**
**
**
**
** $QT_END_LICENSE$
**
****************************************************************************/

#include "qt7backend.h"
#include "qt7playermetadata.h"
#include "qt7playersession.h"
#include <QtCore/qvarlengtharray.h>

#import <QTKit/QTMovie.h>

#ifdef QUICKTIME_C_API_AVAILABLE
    #include <QuickTime/QuickTime.h>
    #undef check // avoid name clash;
#endif

QT_BEGIN_NAMESPACE

QT7PlayerMetaDataControl::QT7PlayerMetaDataControl(QT7PlayerSession *session, QObject *parent)
    :QMetaDataControl(parent), m_session(session)
{
}

QT7PlayerMetaDataControl::~QT7PlayerMetaDataControl()
{
}

bool QT7PlayerMetaDataControl::isMetaDataAvailable() const
{
    return !m_tags.isEmpty();
}

bool QT7PlayerMetaDataControl::isWritable() const
{
    return false;
}

QVariant QT7PlayerMetaDataControl::metaData(QtMediaServices::MetaData key) const
{
    return m_tags.value(key);
}

void QT7PlayerMetaDataControl::setMetaData(QtMediaServices::MetaData key, QVariant const &value)
{
    Q_UNUSED(key);
    Q_UNUSED(value);
}

QList<QtMediaServices::MetaData> QT7PlayerMetaDataControl::availableMetaData() const
{
    return m_tags.keys();
}

QVariant QT7PlayerMetaDataControl::extendedMetaData(const QString &key) const
{
    Q_UNUSED(key);
    return QVariant();
}

void QT7PlayerMetaDataControl::setExtendedMetaData(const QString &key, QVariant const &value)
{
    Q_UNUSED(key);
    Q_UNUSED(value);
}

QStringList QT7PlayerMetaDataControl::availableExtendedMetaData() const
{
    return QStringList();
}

#ifdef QUICKTIME_C_API_AVAILABLE

static QString stripCopyRightSymbol(const QString &key)
{
    return key.right(key.length()-1);
}

static QString convertQuickTimeKeyToUserKey(const QString &key)
{
    if (key == QLatin1String("com.apple.quicktime.displayname"))
        return QLatin1String("nam");
    else if (key == QLatin1String("com.apple.quicktime.album"))
        return QLatin1String("alb");
    else if (key == QLatin1String("com.apple.quicktime.artist"))
        return QLatin1String("ART");
    else
        return QLatin1String("???");
}

static OSStatus readMetaValue(QTMetaDataRef metaDataRef, QTMetaDataItem item, QTPropertyClass propClass,
                              QTPropertyID id, QTPropertyValuePtr *value, ByteCount *size)
{
    QTPropertyValueType type;
    ByteCount propSize;
    UInt32 propFlags;
    OSStatus err = QTMetaDataGetItemPropertyInfo(metaDataRef, item, propClass, id, &type, &propSize, &propFlags);

    if (err == noErr) {
        *value = malloc(propSize);
        if (*value != 0) {
            err = QTMetaDataGetItemProperty(metaDataRef, item, propClass, id, propSize, *value, size);

            if (err == noErr && (type == 'code' || type == 'itsk' || type == 'itlk')) {
                // convert from native endian to big endian
                OSTypePtr pType = (OSTypePtr)*value;
                *pType = EndianU32_NtoB(*pType);
            }
        }
        else
            return -1;
    }

    return err;
}

static UInt32 getMetaType(QTMetaDataRef metaDataRef, QTMetaDataItem item)
{
    QTPropertyValuePtr value = 0;
    ByteCount ignore = 0;
    OSStatus err = readMetaValue(
            metaDataRef, item, kPropertyClass_MetaDataItem, kQTMetaDataItemPropertyID_DataType, &value, &ignore);

    if (err == noErr) {
        UInt32 type = *((UInt32 *) value);
        if (value)
            free(value);
        return type;
    }

    return 0;
}

static QString cFStringToQString(CFStringRef str)
{
    if(!str)
        return QString();
    CFIndex length = CFStringGetLength(str);
    const UniChar *chars = CFStringGetCharactersPtr(str);
    if (chars)
        return QString(reinterpret_cast<const QChar *>(chars), length);

    QVarLengthArray<UniChar> buffer(length);
    CFStringGetCharacters(str, CFRangeMake(0, length), buffer.data());
    return QString(reinterpret_cast<const QChar *>(buffer.constData()), length);
}


static QString getMetaValue(QTMetaDataRef metaDataRef, QTMetaDataItem item, SInt32 id)
{
    QTPropertyValuePtr value = 0;
    ByteCount size = 0;
    OSStatus err = readMetaValue(metaDataRef, item, kPropertyClass_MetaDataItem, id, &value, &size);
    QString string;

    if (err == noErr) {
        UInt32 dataType = getMetaType(metaDataRef, item);
        switch (dataType){
        case kQTMetaDataTypeUTF8:
        case kQTMetaDataTypeMacEncodedText:
            string = cFStringToQString(CFStringCreateWithBytes(0, (UInt8*)value, size, kCFStringEncodingUTF8, false));
            break;
        case kQTMetaDataTypeUTF16BE:
            string = cFStringToQString(CFStringCreateWithBytes(0, (UInt8*)value, size, kCFStringEncodingUTF16BE, false));
            break;
        default:
            break;
        }

        if (value)
            free(value);
    }

    return string;
}


static void readFormattedData(QTMetaDataRef metaDataRef, OSType format, QMultiMap<QString, QString> &result)
{
    QTMetaDataItem item = kQTMetaDataItemUninitialized;
    OSStatus err = QTMetaDataGetNextItem(metaDataRef, format, item, kQTMetaDataKeyFormatWildcard, 0, 0, &item);
    while (err == noErr){
        QString key = getMetaValue(metaDataRef, item, kQTMetaDataItemPropertyID_Key);
        if (format == kQTMetaDataStorageFormatQuickTime)
            key = convertQuickTimeKeyToUserKey(key);
        else
            key = stripCopyRightSymbol(key);

        if (!result.contains(key)){
            QString val = getMetaValue(metaDataRef, item, kQTMetaDataItemPropertyID_Value);
            result.insert(key, val);
        }
        err = QTMetaDataGetNextItem(metaDataRef, format, item, kQTMetaDataKeyFormatWildcard, 0, 0, &item);
    }
}
#endif


void QT7PlayerMetaDataControl::updateTags()
{
    bool wasEmpty = m_tags.isEmpty();
    m_tags.clear();

    QTMovie *movie = (QTMovie*)m_session->movie();

    if (movie) {
        QMultiMap<QString, QString> metaMap;

#ifdef QUICKTIME_C_API_AVAILABLE
        QTMetaDataRef metaDataRef;
        OSStatus err = QTCopyMovieMetaData([movie quickTimeMovie], &metaDataRef);
        if (err == noErr) {
            readFormattedData(metaDataRef, kQTMetaDataStorageFormatUserData, metaMap);
            readFormattedData(metaDataRef, kQTMetaDataStorageFormatQuickTime, metaMap);
            readFormattedData(metaDataRef, kQTMetaDataStorageFormatiTunes, metaMap);
        }
#else
        AutoReleasePool pool;
        NSString *name = [movie attributeForKey:@"QTMovieDisplayNameAttribute"];
        metaMap.insert(QLatin1String("nam"), QString::fromUtf8([name UTF8String]));
#endif // QUICKTIME_C_API_AVAILABLE

        m_tags.insert(QtMediaServices::AlbumArtist, metaMap.value(QLatin1String("ART")));
        m_tags.insert(QtMediaServices::AlbumTitle, metaMap.value(QLatin1String("alb")));
        m_tags.insert(QtMediaServices::Title, metaMap.value(QLatin1String("nam")));
        m_tags.insert(QtMediaServices::Date, metaMap.value(QLatin1String("day")));
        m_tags.insert(QtMediaServices::Genre, metaMap.value(QLatin1String("gnre")));
        m_tags.insert(QtMediaServices::TrackNumber, metaMap.value(QLatin1String("trk")));
        m_tags.insert(QtMediaServices::Description, metaMap.value(QLatin1String("des")));
    }

    if (!wasEmpty || !m_tags.isEmpty())
        emit metaDataChanged();
}

QT_END_NAMESPACE

#include "moc_qt7playermetadata.cpp"