summaryrefslogtreecommitdiffstats
path: root/src/3rdparty/phonon/qt7/backendinfo.mm
blob: e173f052a91060b9d627690dc3ed26451d0a7b7c (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
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
/*  This file is part of the KDE project.

    Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).

    This library is free software: you can redistribute it and/or modify
    it under the terms of the GNU Lesser General Public License as published by
    the Free Software Foundation, either version 2.1 or 3 of the License.

    This library 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 Lesser General Public License for more details.

    You should have received a copy of the GNU Lesser General Public License
    along with this library.  If not, see <http://www.gnu.org/licenses/>.
*/

#include "backendinfo.h"
#include "backendheader.h"

#include <AudioToolbox/AudioToolbox.h>
#include <AudioUnit/AudioUnit.h>
#include <CoreServices/CoreServices.h>

#import <QTKit/QTMovie.h>

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

QT_BEGIN_NAMESPACE

namespace Phonon
{
namespace QT7
{

QString BackendInfo::quickTimeVersionString()
{
	SInt32 version;
    OSStatus err = Gestalt(gestaltQuickTimeVersion, &version);
    if (err != noErr)
        return QString("00.0.0.0000");
    QString versionString = QString("%1%2.%3.%4.%5%6%7%8")
        .arg((version >> (7*4)) & 0xF)
        .arg((version >> (6*4)) & 0xF)
        .arg((version >> (5*4)) & 0xF)
        .arg((version >> (4*4)) & 0xF)
        .arg((version >> (3*4)) & 0xF)
        .arg((version >> (2*4)) & 0xF)
        .arg((version >> (1*4)) & 0xF)
        .arg((version >> (0*4)) & 0xF);
    return versionString;
}

bool BackendInfo::isQuickTimeVersionAvailable(int minHexVersion)
{
    // minHexVersion == 0x0741 means version 7.4.1
	SInt32 qtHexVersion;
    OSStatus err = Gestalt(gestaltQuickTimeVersion, &qtHexVersion);
    return (err == noErr) ? ((qtHexVersion >> 16) >= minHexVersion) : 0;
}

#ifdef QUICKTIME_C_API_AVAILABLE
static QString getMimeTypeTag(QTAtomContainer mimeList, int index, OSType type)
{
    QTAtom mimeAtom = QTFindChildByIndex(mimeList, kParentAtomIsContainer, type, index, 0);
    char mimeCharArray[256];
    long length;
    OSStatus err = QTCopyAtomDataToPtr(mimeList, mimeAtom, true, sizeof(mimeCharArray)-1, mimeCharArray, &length);
    if (err == noErr)
        return QString::fromAscii(mimeCharArray, length);
    return QString();
}
#endif // QUICKTIME_C_API_AVAILABLE

#ifdef QUICKTIME_C_API_AVAILABLE
QStringList BackendInfo::quickTimeMimeTypes(Scope scope)
{
    QStringList mimeTypes;
    ARGUMENT_UNSUPPORTED(scope, Out, NORMAL_ERROR, mimeTypes)

    ComponentDescription description;
    description.componentType = MovieImportType;
    description.componentSubType = 0;
    description.componentManufacturer = 0;
    description.componentFlags = hasMovieImportMIMEList | canMovieImportFiles;
    description.componentFlagsMask = canMovieImportFiles | movieImportSubTypeIsFileExtension | hasMovieImportMIMEList;
    Component component = FindNextComponent(0, &description);

    while (component) {
        QTAtomContainer mimeList = 0;
        OSStatus err = MovieImportGetMIMETypeList((MovieImportComponent)component, &mimeList);
        if (err == noErr){
            int count = QTCountChildrenOfType(mimeList, kParentAtomIsContainer, 0);
            for (int i=1; i<=count; ++i){
                QString mimeType = getMimeTypeTag(mimeList, i, kMimeInfoMimeTypeTag);
                if (mimeType.startsWith(QLatin1String("audio")) || mimeType.startsWith(QLatin1String("video"))){
                    if (err == noErr && !mimeType.isEmpty())
                        mimeTypes << mimeType;
                }
            }
         }
         QTDisposeAtomContainer(mimeList);
         component = FindNextComponent(component, &description);
    }
	mimeTypes.sort();
    return mimeTypes;
}

#else // QUICKTIME_C_API_AVAILABLE == false

QString mimeForExtensionAudio(const QString &ext)
{
	if (ext == "3g2") return QLatin1String("audio/3g2"); 
	if (ext == "3gp") return QLatin1String("audio/3gp"); 
	if (ext == "aac") return QLatin1String("audio/aac"); 
	if (ext == "ac3") return QLatin1String("audio/ac3"); 
	if (ext == "aif") return QLatin1String("audio/aif"); 
	if (ext == "aifc") return QLatin1String("audio/aifc"); 
	if (ext == "aiff") return QLatin1String("audio/aiff"); 
	if (ext == "amr") return QLatin1String("audio/amr"); 
	if (ext == "au") return QLatin1String("audio/au");
	if (ext == "bwf") return QLatin1String("audio/bwf");
	if (ext == "caf") return QLatin1String("audio/caf");
	if (ext == "cdda") return QLatin1String("audio/cdda");
	if (ext == "gsm") return QLatin1String("audio/gsm");
	if (ext == "kar") return QLatin1String("audio/kar");
	if (ext == "m1a") return QLatin1String("audio/m1a");
	if (ext == "m1s") return QLatin1String("audio/m1s");
	if (ext == "m3u") return QLatin1String("audio/m3u");
	if (ext == "m3url") return QLatin1String("audio/m3url"); 
	if (ext == "mid") return QLatin1String("audio/mid");
	if (ext == "midi") return QLatin1String("audio/midi");
	if (ext == "mka") return QLatin1String("audio/mka");
	if (ext == "mp3") return QLatin1String("audio/mp3");
	if (ext == "mp4") return QLatin1String("audio/mp4");
	if (ext == "mpa") return QLatin1String("audio/mpa");
	if (ext == "mpeg") return QLatin1String("audio/mpeg");
	if (ext == "mpg") return QLatin1String("audio/mpg");
	if (ext == "mpg4") return QLatin1String("audio/mpg4");
	if (ext == "mpm") return QLatin1String("audio/mpm"); 
	if (ext == "qcp") return QLatin1String("audio/qcp"); 
	if (ext == "sd2") return QLatin1String("audio/sd2"); 
	if (ext == "smf") return QLatin1String("audio/smf"); 
	if (ext == "snd") return QLatin1String("audio/snd"); 
	if (ext == "ulw") return QLatin1String("audio/ulw"); 
	if (ext == "wav") return QLatin1String("audio/wav"); 
	if (ext == "wax") return QLatin1String("audio/wax"); 
	if (ext == "wma") return QLatin1String("audio/wma"); 
	return QString();
}

QString mimeForExtensionVideo(const QString &ext)
{
	if (ext == "3g2") return QLatin1String("video/3g2"); 
	if (ext == "3gp") return QLatin1String("video/3gp"); 
	if (ext == "asf") return QLatin1String("video/asf"); 
	if (ext == "asx") return QLatin1String("video/asx"); 
	if (ext == "avi") return QLatin1String("video/avi"); 
	if (ext == "dif") return QLatin1String("video/dif"); 
	if (ext == "dv") return QLatin1String("video/dv"); 
	if (ext == "flc") return QLatin1String("video/flc"); 
	if (ext == "fli") return QLatin1String("video/fli"); 
	if (ext == "m15") return QLatin1String("video/m15"); 
	if (ext == "m1a") return QLatin1String("video/m1a"); 
	if (ext == "m1s") return QLatin1String("video/m1s"); 
	if (ext == "m1v") return QLatin1String("video/m1v"); 
	if (ext == "m75") return QLatin1String("video/m75"); 
	if (ext == "mkv") return QLatin1String("video/mkv"); 
	if (ext == "mp4") return QLatin1String("video/mp4"); 
	if (ext == "mpa") return QLatin1String("video/mpa"); 
	if (ext == "mpeg") return QLatin1String("video/mpeg"); 
	if (ext == "mpg") return QLatin1String("video/mpg"); 
	if (ext == "mpg4") return QLatin1String("video/mpg4"); 
	if (ext == "mpm") return QLatin1String("video/mpm"); 
	if (ext == "mpv") return QLatin1String("video/mpv"); 
	if (ext == "vfw") return QLatin1String("video/vfw"); 
	if (ext == "wm") return QLatin1String("video/wm"); 
	if (ext == "wmv") return QLatin1String("video/wmv"); 
	if (ext == "wmx") return QLatin1String("video/wmx"); 
	if (ext == "wvx") return QLatin1String("video/wvx");
	return QString();
}

QStringList BackendInfo::quickTimeMimeTypes(Scope scope)
{
    QStringList mimeTypes;
    QStringList fileExtensions;
    ARGUMENT_UNSUPPORTED(scope, Out, NORMAL_ERROR, mimeTypes)

	PhononAutoReleasePool pool;
	NSArray *fileTypes = [QTMovie movieFileTypes:QTIncludeAllTypes];
	for (NSString *type in fileTypes){
		QString formattedType = QString::fromUtf8([type UTF8String]);
		formattedType = formattedType.remove('\'').remove('.').toLower();
		QString audioMime = mimeForExtensionAudio(formattedType);
		QString videoMime = mimeForExtensionVideo(formattedType);
		if (!audioMime.isEmpty())
			mimeTypes << audioMime;
		if (!videoMime.isEmpty())
			mimeTypes << videoMime;
		if (audioMime.isEmpty() && videoMime.isEmpty())
			fileExtensions << QLatin1String("application/x-qt-") + formattedType;
	}
	mimeTypes.sort();
	fileExtensions.sort();
    return mimeTypes + fileExtensions;
}
#endif // QUICKTIME_C_API_AVAILABLE

QStringList BackendInfo::quickTimeCompressionFormats()
{
    QStringList result;

#ifdef QUICKTIME_C_API_AVAILABLE

    ComponentInstance component = 0;
    OSStatus err = OpenADefaultComponent(StandardCompressionType, StandardCompressionSubTypeAudio, &component);
    BACKEND_ASSERT3(err == noErr, "Could not open component for retrieving awailable compression formats", NORMAL_ERROR, result)

    UInt32 size;
    err = QTGetComponentPropertyInfo(component, kQTPropertyClass_SCAudio, kQTSCAudioPropertyID_AvailableCompressionFormatNamesList, 0, &size,0);
    BACKEND_ASSERT3(err == noErr, "Could not get awailable compression formats", NORMAL_ERROR, result)

    CFArrayRef formats[size];
    err = QTGetComponentProperty(component, kQTPropertyClass_SCAudio, kQTSCAudioPropertyID_AvailableCompressionFormatNamesList, size, &formats, &size);
    BACKEND_ASSERT3(err == noErr, "Could not get awailable compression formats", NORMAL_ERROR, result)

    CFIndex count = CFArrayGetCount(*formats);
    for (CFIndex i=0; i<count; ++i){
        const CFStringRef name = (const struct __CFString *) CFArrayGetValueAtIndex(*formats, i);
        result << PhononCFString::toQString(name);
    }
    
#endif // QUICKTIME_C_API_AVAILABLE
    return result;
}


QStringList BackendInfo::coreAudioCodecs(Scope scope)
{
    QStringList result;
    UInt32 size;
    OSStatus err;
    OSType *formatIDs;

    OSType encodersOrDecoders = (scope == In)
        ? kAudioFormatProperty_EncodeFormatIDs : kAudioFormatProperty_DecodeFormatIDs;

    err = AudioFormatGetPropertyInfo(encodersOrDecoders, 0, NULL,  &size);
    BACKEND_ASSERT3(err == noErr, "Could not get awailable decoders/encoders", NORMAL_ERROR, result)

    formatIDs = (OSType*)malloc(size);
    UInt32 numFormats = size / sizeof(OSType);
    err = AudioFormatGetProperty(encodersOrDecoders, 0, NULL, &size,  formatIDs);
    BACKEND_ASSERT(err == noErr, "Could not get awailable decoders/encoders", NORMAL_ERROR){
        free(formatIDs);
        return result;
    }

    for (UInt32 i=0; i<numFormats; ++i){
        AudioStreamBasicDescription absd;
        memset(&absd, 0, sizeof(absd));
        absd.mFormatID = formatIDs[i];

        CFStringRef name;
        size = sizeof(CFStringRef);
        err = AudioFormatGetProperty(kAudioFormatProperty_FormatName, sizeof(absd), &absd,  &size, &name);
        BACKEND_ASSERT(err == noErr, "Could not get awailable decoder/encoder names", NORMAL_ERROR){
            free(formatIDs);
            return result;
        }
        result << PhononCFString::toQString(name);
    }
    free(formatIDs);
    return result;
}

QStringList BackendInfo::coreAudioFileTypes(Scope scope)
{
    QStringList result;
    OSStatus err;
    UInt32 propertySize;

    OSType readOrwrite = (scope == In)
        ? kAudioFileGlobalInfo_ReadableTypes : kAudioFileGlobalInfo_WritableTypes;

    err = AudioFileGetGlobalInfoSize(readOrwrite, 0, NULL, &propertySize);
    BACKEND_ASSERT3(err == noErr, "Could not get core audio file types", NORMAL_ERROR, result)

    OSType *types = (OSType*)malloc(propertySize);
    err = AudioFileGetGlobalInfo(readOrwrite, 0, NULL, &propertySize,  types);
    BACKEND_ASSERT3(err == noErr, "Could not get core audio file types", NORMAL_ERROR, result)

    UInt32 numTypes = propertySize / sizeof(OSType);
    for (UInt32 i=0; i<numTypes; ++i){
        CFStringRef name;
        UInt32 outSize = sizeof(name);
        err = AudioFileGetGlobalInfo(kAudioFileGlobalInfo_FileTypeName, sizeof(OSType), types+i, &outSize, &name);
        BACKEND_ASSERT3(err == noErr, "Could not get core audio file type names", NORMAL_ERROR, result)
        result << PhononCFString::toQString(name);
    }
    return result;
}

}}

QT_END_NAMESPACE