data.c revision 71065fbf12abafd4c2a0dc85c81f13b564ff69fb
1/*
2 * Copyright (C) 2010 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17/** Data locator, data format, data source, and data sink support */
18
19#include "sles_allinclusive.h"
20#ifdef ANDROID  // FIXME This file should be portable
21#include "android/channels.h"
22#endif
23
24
25/** \brief Check a data locator and make local deep copy */
26
27static SLresult checkDataLocator(const char *name, void *pLocator, DataLocator *pDataLocator,
28        SLuint32 allowedDataLocatorMask)
29{
30    assert(NULL != name && NULL != pDataLocator);
31    SLresult result = SL_RESULT_SUCCESS;
32
33    SLuint32 locatorType;
34    if (NULL == pLocator) {
35        pDataLocator->mLocatorType = locatorType = SL_DATALOCATOR_NULL;
36    } else {
37        locatorType = *(SLuint32 *)pLocator;
38        switch (locatorType) {
39
40        case SL_DATALOCATOR_ADDRESS:
41            pDataLocator->mAddress = *(SLDataLocator_Address *)pLocator;
42            // if length is greater than zero, then the address must be non-NULL
43            if ((0 < pDataLocator->mAddress.length) && (NULL == pDataLocator->mAddress.pAddress)) {
44                SL_LOGE("%s: pAddress=NULL", name);
45                result = SL_RESULT_PARAMETER_INVALID;
46            }
47            break;
48
49        case SL_DATALOCATOR_BUFFERQUEUE:
50#ifdef ANDROID
51        // This is an alias that is _not_ converted; the rest of the code must check for both
52        // locator types. That's because it is only an alias for audio players, not audio recorder
53        // objects so we have to remember the distinction.
54        case SL_DATALOCATOR_ANDROIDSIMPLEBUFFERQUEUE:
55#endif
56            pDataLocator->mBufferQueue = *(SLDataLocator_BufferQueue *)pLocator;
57            // number of buffers must be specified, there is no default value, and can't be too big
58            if (!((1 <= pDataLocator->mBufferQueue.numBuffers) &&
59                (pDataLocator->mBufferQueue.numBuffers <= 255))) {
60                SL_LOGE("%s: numBuffers=%u", name, pDataLocator->mBufferQueue.numBuffers);
61                result = SL_RESULT_PARAMETER_INVALID;
62            }
63            break;
64
65        case SL_DATALOCATOR_IODEVICE:
66            {
67            pDataLocator->mIODevice = *(SLDataLocator_IODevice *)pLocator;
68            SLuint32 deviceType = pDataLocator->mIODevice.deviceType;
69            SLObjectItf device = pDataLocator->mIODevice.device;
70            if (NULL != device) {
71                pDataLocator->mIODevice.deviceID = 0;
72                SLuint32 expectedObjectID;
73                switch (deviceType) {
74                case SL_IODEVICE_LEDARRAY:
75                    expectedObjectID = SL_OBJECTID_LEDDEVICE;
76                    break;
77                case SL_IODEVICE_VIBRA:
78                    expectedObjectID = SL_OBJECTID_VIBRADEVICE;
79                    break;
80                case XA_IODEVICE_CAMERA:
81                    expectedObjectID = XA_OBJECTID_CAMERADEVICE;
82                    break;
83                case XA_IODEVICE_RADIO:
84                    expectedObjectID = XA_OBJECTID_RADIODEVICE;
85                    break;
86                // audio input and audio output cannot be specified via objects
87                case SL_IODEVICE_AUDIOINPUT:
88                // case SL_IODEVICE_AUDIOOUTPUT:   // does not exist in 1.0.1, added in 1.1
89                default:
90                    SL_LOGE("%s: deviceType=%u", name, deviceType);
91                    pDataLocator->mIODevice.device = NULL;
92                    expectedObjectID = 0;
93                    result = SL_RESULT_PARAMETER_INVALID;
94                }
95                if (result == SL_RESULT_SUCCESS) {
96                    // check that device has the correct object ID and is realized,
97                    // and acquire a strong reference to it
98                    result = AcquireStrongRef((IObject *) device, expectedObjectID);
99                    if (SL_RESULT_SUCCESS != result) {
100                        SL_LOGE("%s: locatorType=IODEVICE, but device field %p has wrong " \
101                            "object ID or is not realized", name, device);
102                        pDataLocator->mIODevice.device = NULL;
103                    }
104                }
105            } else {
106                SLuint32 deviceID = pDataLocator->mIODevice.deviceID;
107                switch (deviceType) {
108                case SL_IODEVICE_LEDARRAY:
109                    if (SL_DEFAULTDEVICEID_LED != deviceID) {
110                        SL_LOGE("%s: invalid LED deviceID=%u", name, deviceID);
111                        result = SL_RESULT_PARAMETER_INVALID;
112                    }
113                    break;
114                case SL_IODEVICE_VIBRA:
115                    if (SL_DEFAULTDEVICEID_VIBRA != deviceID) {
116                        SL_LOGE("%s: invalid vibra deviceID=%u", name, deviceID);
117                        result = SL_RESULT_PARAMETER_INVALID;
118                    }
119                    break;
120                case SL_IODEVICE_AUDIOINPUT:
121                    if (SL_DEFAULTDEVICEID_AUDIOINPUT != deviceID) {
122                        SL_LOGE("%s: invalid audio input deviceID=%u", name, deviceID);
123                        result = SL_RESULT_PARAMETER_INVALID;
124                    }
125                    break;
126                case XA_IODEVICE_RADIO:
127                    // no default device ID for radio; see Khronos bug XXXX
128                    break;
129                case XA_IODEVICE_CAMERA:
130                    if (XA_DEFAULTDEVICEID_CAMERA != deviceID) {
131                        SL_LOGE("%s: invalid audio input deviceID=%u", name, deviceID);
132                        result = XA_RESULT_PARAMETER_INVALID;
133                    }
134                    break;
135                // case SL_IODEVICE_AUDIOOUTPUT:
136                    // does not exist in 1.0.1, added in 1.1
137                    // break;
138                default:
139                    SL_LOGE("%s: deviceType=%u is invalid", name, deviceType);
140                    result = SL_RESULT_PARAMETER_INVALID;
141                }
142            }
143            }
144            break;
145
146        case SL_DATALOCATOR_MIDIBUFFERQUEUE:
147            pDataLocator->mMIDIBufferQueue = *(SLDataLocator_MIDIBufferQueue *)pLocator;
148            if (0 == pDataLocator->mMIDIBufferQueue.tpqn) {
149                pDataLocator->mMIDIBufferQueue.tpqn = 192;
150            }
151            // number of buffers must be specified, there is no default value, and can't be too big
152            if (!((1 <= pDataLocator->mMIDIBufferQueue.numBuffers) &&
153                (pDataLocator->mMIDIBufferQueue.numBuffers <= 255))) {
154                SL_LOGE("%s: SLDataLocator_MIDIBufferQueue.numBuffers=%d", name,
155                        pDataLocator->mMIDIBufferQueue.numBuffers);
156                result = SL_RESULT_PARAMETER_INVALID;
157            }
158            break;
159
160        case SL_DATALOCATOR_OUTPUTMIX:
161            pDataLocator->mOutputMix = *(SLDataLocator_OutputMix *)pLocator;
162            // check that output mix object has the correct object ID and is realized,
163            // and acquire a strong reference to it
164            result = AcquireStrongRef((IObject *) pDataLocator->mOutputMix.outputMix,
165                SL_OBJECTID_OUTPUTMIX);
166            if (SL_RESULT_SUCCESS != result) {
167                SL_LOGE("%s: locatorType=SL_DATALOCATOR_OUTPUTMIX, but outputMix field %p does " \
168                    "not refer to an SL_OBJECTID_OUTPUTMIX or the output mix is not realized", \
169                    name, pDataLocator->mOutputMix.outputMix);
170                pDataLocator->mOutputMix.outputMix = NULL;
171            }
172            break;
173
174        case XA_DATALOCATOR_NATIVEDISPLAY:
175            pDataLocator->mNativeDisplay = *(XADataLocator_NativeDisplay *)pLocator;
176            // hWindow is NDK C ANativeWindow * and hDisplay must be NULL
177            if (pDataLocator->mNativeDisplay.hWindow == NULL) {
178                SL_LOGE("%s: hWindow must be non-NULL ANativeWindow *", name);
179                result = SL_RESULT_PARAMETER_INVALID;
180            }
181            if (pDataLocator->mNativeDisplay.hDisplay != NULL) {
182                SL_LOGE("%s: hDisplay must be NULL, but is %p", name,
183                        pDataLocator->mNativeDisplay.hDisplay);
184                result = SL_RESULT_PARAMETER_INVALID;
185            }
186            break;
187
188        case SL_DATALOCATOR_URI:
189            {
190            pDataLocator->mURI = *(SLDataLocator_URI *)pLocator;
191            if (NULL == pDataLocator->mURI.URI) {
192                SL_LOGE("%s: invalid URI=NULL", name);
193                result = SL_RESULT_PARAMETER_INVALID;
194            } else {
195                // NTH verify URI address for validity
196                size_t len = strlen((const char *) pDataLocator->mURI.URI);
197                SLchar *myURI = (SLchar *) malloc(len + 1);
198                if (NULL == myURI) {
199                    result = SL_RESULT_MEMORY_FAILURE;
200                } else {
201                    memcpy(myURI, pDataLocator->mURI.URI, len + 1);
202                    // Verify that another thread didn't change the NUL-terminator after we used it
203                    // to determine length of string to copy. It's OK if the string became shorter.
204                    if ('\0' != myURI[len]) {
205                        free(myURI);
206                        myURI = NULL;
207                        result = SL_RESULT_PARAMETER_INVALID;
208                    }
209                }
210                pDataLocator->mURI.URI = myURI;
211            }
212            }
213            break;
214
215#ifdef ANDROID
216        case SL_DATALOCATOR_ANDROIDFD:
217        {
218            pDataLocator->mFD = *(SLDataLocator_AndroidFD *)pLocator;
219            SL_LOGV("%s: fd=%d offset=%lld length=%lld", name, pDataLocator->mFD.fd,
220                    pDataLocator->mFD.offset, pDataLocator->mFD.length);
221            // NTH check against process fd limit
222            if (0 > pDataLocator->mFD.fd) {
223                SL_LOGE("%s: fd=%d\n", name, pDataLocator->mFD.fd);
224                result = SL_RESULT_PARAMETER_INVALID;
225            }
226            break;
227        }
228        case SL_DATALOCATOR_ANDROIDBUFFERQUEUE:
229        {
230            pDataLocator->mABQ = *(SLDataLocator_AndroidBufferQueue*)pLocator;
231            // number of buffers must be specified, there is no default value, and can't be too big
232            if (!((1 <= pDataLocator->mBufferQueue.numBuffers) &&
233                    (pDataLocator->mBufferQueue.numBuffers <= 255))) {
234                SL_LOGE("%s: numBuffers=%u", name, pDataLocator->mABQ.numBuffers);
235                result = SL_RESULT_PARAMETER_INVALID;
236            }
237            break;
238        }
239#endif
240
241        case SL_DATALOCATOR_NULL:   // a NULL pointer is allowed, but not a pointer to NULL
242        default:
243            SL_LOGE("%s: locatorType=%u", name, locatorType);
244            result = SL_RESULT_PARAMETER_INVALID;
245        }
246
247        // Verify that another thread didn't change the locatorType field after we used it
248        // to determine sizeof struct to copy.
249        if ((SL_RESULT_SUCCESS == result) && (locatorType != pDataLocator->mLocatorType)) {
250            SL_LOGE("%s: locatorType changed from %u to %u", name, locatorType,
251                    pDataLocator->mLocatorType);
252            result = SL_RESULT_PRECONDITIONS_VIOLATED;
253        }
254
255    }
256
257    // Verify that the data locator type is allowed in this context
258    if (SL_RESULT_SUCCESS == result) {
259        SLuint32 actualMask;
260        switch (locatorType) {
261        case SL_DATALOCATOR_NULL:
262        case SL_DATALOCATOR_URI:
263        case SL_DATALOCATOR_ADDRESS:
264        case SL_DATALOCATOR_IODEVICE:
265        case SL_DATALOCATOR_OUTPUTMIX:
266        case XA_DATALOCATOR_NATIVEDISPLAY:
267        case SL_DATALOCATOR_BUFFERQUEUE:
268        case SL_DATALOCATOR_MIDIBUFFERQUEUE:
269            actualMask = 1L << locatorType;
270            break;
271#ifdef ANDROID
272        case SL_DATALOCATOR_ANDROIDFD:
273        case SL_DATALOCATOR_ANDROIDSIMPLEBUFFERQUEUE:
274        case SL_DATALOCATOR_ANDROIDBUFFERQUEUE:
275            actualMask = 0x100L << (locatorType - SL_DATALOCATOR_ANDROIDFD);
276            break;
277#endif
278        default:
279            assert(false);
280            actualMask = 0L;
281            break;
282        }
283        if (!(allowedDataLocatorMask & actualMask)) {
284            SL_LOGE("%s: data locator type 0x%x not allowed", name, locatorType);
285            result = SL_RESULT_CONTENT_UNSUPPORTED;
286        }
287    }
288
289    return result;
290}
291
292
293/** \brief Free the local deep copy of a data locator */
294
295static void freeDataLocator(DataLocator *pDataLocator)
296{
297    switch (pDataLocator->mLocatorType) {
298    case SL_DATALOCATOR_NULL:
299    case SL_DATALOCATOR_ADDRESS:
300    case SL_DATALOCATOR_BUFFERQUEUE:
301    case SL_DATALOCATOR_MIDIBUFFERQUEUE:
302    case XA_DATALOCATOR_NATIVEDISPLAY:
303        break;
304    case SL_DATALOCATOR_URI:
305        if (NULL != pDataLocator->mURI.URI) {
306            free(pDataLocator->mURI.URI);
307            pDataLocator->mURI.URI = NULL;
308        }
309        pDataLocator->mURI.URI = NULL;
310        break;
311    case SL_DATALOCATOR_IODEVICE:
312        if (NULL != pDataLocator->mIODevice.device) {
313            ReleaseStrongRef((IObject *) pDataLocator->mIODevice.device);
314            pDataLocator->mIODevice.device = NULL;
315        }
316        break;
317    case SL_DATALOCATOR_OUTPUTMIX:
318        if (NULL != pDataLocator->mOutputMix.outputMix) {
319            ReleaseStrongRef((IObject *) pDataLocator->mOutputMix.outputMix);
320            pDataLocator->mOutputMix.outputMix = NULL;
321        }
322        break;
323#ifdef ANDROID
324    case SL_DATALOCATOR_ANDROIDBUFFERQUEUE:
325    case SL_DATALOCATOR_ANDROIDSIMPLEBUFFERQUEUE:
326    case SL_DATALOCATOR_ANDROIDFD:
327        break;
328#endif
329    default:
330        // an invalid data locator is caught earlier when making the copy
331        assert(false);
332        break;
333    }
334}
335
336
337/** \brief Check a data format and make local deep copy */
338
339static SLresult checkDataFormat(const char *name, void *pFormat, DataFormat *pDataFormat,
340        SLuint32 allowedDataFormatMask)
341{
342    assert(NULL != name && NULL != pDataFormat);
343    SLresult result = SL_RESULT_SUCCESS;
344    const SLuint32 *df_representation = NULL; // pointer to representation field, if it exists
345    SLuint32 formatType;
346    if (NULL == pFormat) {
347        pDataFormat->mFormatType = formatType = SL_DATAFORMAT_NULL;
348    } else {
349        formatType = *(SLuint32 *)pFormat;
350        switch (formatType) {
351        case SL_ANDROID_DATAFORMAT_PCM_EX:
352            pDataFormat->mPCMEx.representation =
353                    ((SLAndroidDataFormat_PCM_EX *)pFormat)->representation;
354            switch (pDataFormat->mPCMEx.representation) {
355            case SL_ANDROID_PCM_REPRESENTATION_SIGNED_INT:
356            case SL_ANDROID_PCM_REPRESENTATION_UNSIGNED_INT:
357            case SL_ANDROID_PCM_REPRESENTATION_FLOAT:
358                df_representation = &pDataFormat->mPCMEx.representation;
359                break;
360            default:
361                SL_LOGE("%s: unsupported representation: %d", name,
362                        pDataFormat->mPCMEx.representation);
363                result = SL_RESULT_PARAMETER_INVALID;
364                break;
365            }
366            // SL_ANDROID_DATAFORMAT_PCM_EX - fall through to next test.
367        case SL_DATAFORMAT_PCM:
368            pDataFormat->mPCM = *(SLDataFormat_PCM *)pFormat;
369            do {
370
371                // check the channel count
372                // FIXME Android and 8-channel positional assumptions here
373                switch (pDataFormat->mPCM.numChannels) {
374                case 1:     // mono
375                case 2:     // stereo
376                case 4:     // QUAD
377                case 6:     // 5.1
378                case 8:     // 7.1
379                    break;
380                case 0:     // unknown
381                    result = SL_RESULT_PARAMETER_INVALID;
382                    break;
383                default:    // multi-channel
384                    result = SL_RESULT_CONTENT_UNSUPPORTED;
385                    break;
386                }
387                if (SL_RESULT_SUCCESS != result) {
388                    SL_LOGE("%s: numChannels=%u", name, (unsigned) pDataFormat->mPCM.numChannels);
389                    break;
390                }
391
392                // check the sampling rate
393                if (pDataFormat->mPCM.samplesPerSec == 0) {
394                    result = SL_RESULT_PARAMETER_INVALID;
395                } else if (pDataFormat->mPCM.samplesPerSec < SL_SAMPLINGRATE_8 ||
396                        pDataFormat->mPCM.samplesPerSec > SL_SAMPLINGRATE_192) {
397                    result = SL_RESULT_CONTENT_UNSUPPORTED;
398                }
399                if (SL_RESULT_SUCCESS != result) {
400                    SL_LOGE("%s: samplesPerSec=%u", name, pDataFormat->mPCM.samplesPerSec);
401                    break;
402                }
403
404                // check the container bit depth and representation
405                switch (pDataFormat->mPCM.containerSize) {
406                case 8:
407                    if (df_representation != NULL &&
408                            *df_representation != SL_ANDROID_PCM_REPRESENTATION_UNSIGNED_INT) {
409                        result = SL_RESULT_PARAMETER_INVALID;
410                    }
411                    break;
412                case 16:
413                case 24:
414                    if (df_representation != NULL &&
415                            *df_representation != SL_ANDROID_PCM_REPRESENTATION_SIGNED_INT) {
416                        result = SL_RESULT_PARAMETER_INVALID;
417                    }
418                    break;
419                case 32:
420                    if (df_representation != NULL
421                            && *df_representation != SL_ANDROID_PCM_REPRESENTATION_SIGNED_INT
422                            && *df_representation != SL_ANDROID_PCM_REPRESENTATION_FLOAT) {
423                        result = SL_RESULT_PARAMETER_INVALID;
424                    }
425                    break;
426                default:
427                    result = SL_RESULT_PARAMETER_INVALID;
428                    break;
429                }
430                if (SL_RESULT_SUCCESS != result) {
431                    SL_LOGE("%s: containerSize=%u", name, pDataFormat->mPCM.containerSize);
432                    break;
433                }
434
435                // sample size cannot be zero, and container size cannot be less than sample size
436                if (pDataFormat->mPCM.bitsPerSample == 0 ||
437                        pDataFormat->mPCM.containerSize < pDataFormat->mPCM.bitsPerSample) {
438                    result = SL_RESULT_PARAMETER_INVALID;
439                }
440                if (SL_RESULT_SUCCESS != result) {
441                    SL_LOGE("%s: containerSize=%u, bitsPerSample=%u", name,
442                            (unsigned) pDataFormat->mPCM.containerSize,
443                            (unsigned) pDataFormat->mPCM.bitsPerSample);
444                    break;
445                }
446
447                // check the channel mask
448                // FIXME Android and 8-channel positional assumptions here
449                switch (pDataFormat->mPCM.channelMask) {
450                case SL_SPEAKER_FRONT_LEFT | SL_SPEAKER_FRONT_RIGHT:
451                    if (2 != pDataFormat->mPCM.numChannels) {
452                        result = SL_RESULT_PARAMETER_INVALID;
453                    }
454                    break;
455                case SL_SPEAKER_FRONT_LEFT:
456                case SL_SPEAKER_FRONT_RIGHT:
457                case SL_SPEAKER_FRONT_CENTER:
458                    if (1 != pDataFormat->mPCM.numChannels) {
459                        result = SL_RESULT_PARAMETER_INVALID;
460                    }
461                    break;
462#ifdef ANDROID
463                case SL_ANDROID_SPEAKER_QUAD:
464                    if (4 != pDataFormat->mPCM.numChannels) {
465                        result = SL_RESULT_PARAMETER_INVALID;
466                    }
467                    break;
468                case SL_ANDROID_SPEAKER_5DOT1:
469                    if (6 != pDataFormat->mPCM.numChannels) {
470                        result = SL_RESULT_PARAMETER_INVALID;
471                    }
472                    break;
473                case SL_ANDROID_SPEAKER_7DOT1:
474                    if (8 != pDataFormat->mPCM.numChannels) {
475                        result = SL_RESULT_PARAMETER_INVALID;
476                    }
477                    break;
478#endif
479                case 0:
480                    // The default of front left rather than center for mono may be non-intuitive,
481                    // but the left channel is the first channel for stereo or multichannel content.
482                    pDataFormat->mPCM.channelMask = pDataFormat->mPCM.numChannels == 2 ?
483                        SL_SPEAKER_FRONT_LEFT | SL_SPEAKER_FRONT_RIGHT : SL_SPEAKER_FRONT_LEFT;
484                    break;
485                default:
486                    result = SL_RESULT_PARAMETER_INVALID;
487                    break;
488                }
489                if (SL_RESULT_SUCCESS != result) {
490                    SL_LOGE("%s: channelMask=0x%x numChannels=%u", name,
491                        pDataFormat->mPCM.channelMask, pDataFormat->mPCM.numChannels);
492                    break;
493                }
494
495                // check the endianness / byte order
496                switch (pDataFormat->mPCM.endianness) {
497                case SL_BYTEORDER_LITTLEENDIAN:
498                case SL_BYTEORDER_BIGENDIAN:
499                    break;
500                // native is proposed but not yet in spec
501                default:
502                    result = SL_RESULT_PARAMETER_INVALID;
503                    break;
504                }
505                if (SL_RESULT_SUCCESS != result) {
506                    SL_LOGE("%s: endianness=%u", name, (unsigned) pDataFormat->mPCM.endianness);
507                    break;
508                }
509
510                // here if all checks passed successfully
511
512            } while(0);
513            break;
514
515        case SL_DATAFORMAT_MIME:
516            pDataFormat->mMIME = *(SLDataFormat_MIME *)pFormat;
517            if (NULL != pDataFormat->mMIME.mimeType) {
518                // NTH check address for validity
519                size_t len = strlen((const char *) pDataFormat->mMIME.mimeType);
520                SLchar *myMIME = (SLchar *) malloc(len + 1);
521                if (NULL == myMIME) {
522                    result = SL_RESULT_MEMORY_FAILURE;
523                } else {
524                    memcpy(myMIME, pDataFormat->mMIME.mimeType, len + 1);
525                    // make sure MIME string was not modified asynchronously
526                    if ('\0' != myMIME[len]) {
527                        free(myMIME);
528                        myMIME = NULL;
529                        result = SL_RESULT_PRECONDITIONS_VIOLATED;
530                    }
531                }
532                pDataFormat->mMIME.mimeType = myMIME;
533            }
534            break;
535
536        case XA_DATAFORMAT_RAWIMAGE:
537            pDataFormat->mRawImage = *(XADataFormat_RawImage *)pFormat;
538            switch (pDataFormat->mRawImage.colorFormat) {
539            case XA_COLORFORMAT_MONOCHROME:
540            case XA_COLORFORMAT_8BITRGB332:
541            case XA_COLORFORMAT_12BITRGB444:
542            case XA_COLORFORMAT_16BITARGB4444:
543            case XA_COLORFORMAT_16BITARGB1555:
544            case XA_COLORFORMAT_16BITRGB565:
545            case XA_COLORFORMAT_16BITBGR565:
546            case XA_COLORFORMAT_18BITRGB666:
547            case XA_COLORFORMAT_18BITARGB1665:
548            case XA_COLORFORMAT_19BITARGB1666:
549            case XA_COLORFORMAT_24BITRGB888:
550            case XA_COLORFORMAT_24BITBGR888:
551            case XA_COLORFORMAT_24BITARGB1887:
552            case XA_COLORFORMAT_25BITARGB1888:
553            case XA_COLORFORMAT_32BITBGRA8888:
554            case XA_COLORFORMAT_32BITARGB8888:
555            case XA_COLORFORMAT_YUV411PLANAR:
556            case XA_COLORFORMAT_YUV420PLANAR:
557            case XA_COLORFORMAT_YUV420SEMIPLANAR:
558            case XA_COLORFORMAT_YUV422PLANAR:
559            case XA_COLORFORMAT_YUV422SEMIPLANAR:
560            case XA_COLORFORMAT_YCBYCR:
561            case XA_COLORFORMAT_YCRYCB:
562            case XA_COLORFORMAT_CBYCRY:
563            case XA_COLORFORMAT_CRYCBY:
564            case XA_COLORFORMAT_YUV444INTERLEAVED:
565            case XA_COLORFORMAT_RAWBAYER8BIT:
566            case XA_COLORFORMAT_RAWBAYER10BIT:
567            case XA_COLORFORMAT_RAWBAYER8BITCOMPRESSED:
568            case XA_COLORFORMAT_L2:
569            case XA_COLORFORMAT_L4:
570            case XA_COLORFORMAT_L8:
571            case XA_COLORFORMAT_L16:
572            case XA_COLORFORMAT_L24:
573            case XA_COLORFORMAT_L32:
574            case XA_COLORFORMAT_18BITBGR666:
575            case XA_COLORFORMAT_24BITARGB6666:
576            case XA_COLORFORMAT_24BITABGR6666:
577                break;
578            case XA_COLORFORMAT_UNUSED:
579            default:
580                result = XA_RESULT_PARAMETER_INVALID;
581                SL_LOGE("%s: unsupported color format %d", name,
582                    pDataFormat->mRawImage.colorFormat);
583                break;
584            }
585            // no checks for height, width, or stride
586            break;
587
588        default:
589            result = SL_RESULT_PARAMETER_INVALID;
590            SL_LOGE("%s: formatType=%u", name, (unsigned) formatType);
591            break;
592
593        }
594
595        // make sure format type was not modified asynchronously
596        if ((SL_RESULT_SUCCESS == result) && (formatType != pDataFormat->mFormatType)) {
597            SL_LOGE("%s: formatType changed from %u to %u", name, formatType,
598                    pDataFormat->mFormatType);
599            result = SL_RESULT_PRECONDITIONS_VIOLATED;
600        }
601
602    }
603
604    // Verify that the data format type is allowed in this context
605    if (SL_RESULT_SUCCESS == result) {
606        SLuint32 actualMask;
607        switch (formatType) {
608        case SL_DATAFORMAT_NULL:
609        case SL_DATAFORMAT_MIME:
610        case SL_DATAFORMAT_PCM:
611        case SL_ANDROID_DATAFORMAT_PCM_EX:
612        case XA_DATAFORMAT_RAWIMAGE:
613            actualMask = 1L << formatType;
614            break;
615        default:
616            assert(false);
617            actualMask = 0L;
618            break;
619        }
620        if (!(allowedDataFormatMask & actualMask)) {
621            SL_LOGE("%s: data format %d not allowed", name, formatType);
622            result = SL_RESULT_CONTENT_UNSUPPORTED;
623        }
624    }
625
626    return result;
627}
628
629
630/** \brief Check interface ID compatibility with respect to a particular source
631 *         and sink data locator format
632 */
633
634SLresult checkSourceSinkVsInterfacesCompatibility(const DataLocatorFormat *pSrcDataLocatorFormat,
635        const DataLocatorFormat *pSinkDataLocatorFormat,
636        const ClassTable *clazz, unsigned requiredMask) {
637    int index;
638    switch (pSrcDataLocatorFormat->mLocator.mLocatorType) {
639    case SL_DATALOCATOR_URI:
640#ifdef ANDROID
641    case SL_DATALOCATOR_ANDROIDFD:
642#endif
643        // URIs and FD can be sources when "playing" to an OutputMix or a Buffer Queue for decode
644        // so we don't prevent the retrieval of the BufferQueue interfaces for those sources
645        switch (pSinkDataLocatorFormat->mLocator.mLocatorType) {
646        case SL_DATALOCATOR_BUFFERQUEUE:
647#ifdef ANDROID
648        case SL_DATALOCATOR_ANDROIDSIMPLEBUFFERQUEUE:
649#endif
650            break;
651        default:
652            // can't require SLBufferQueueItf or its alias SLAndroidSimpleBufferQueueItf
653            // if the data sink is not a buffer queue
654            index = clazz->mMPH_to_index[MPH_BUFFERQUEUE];
655#ifdef ANDROID
656            assert(index == clazz->mMPH_to_index[MPH_ANDROIDSIMPLEBUFFERQUEUE]);
657#endif
658            if (0 <= index) {
659                if (requiredMask & (1 << index)) {
660                    SL_LOGE("can't require SL_IID_BUFFERQUEUE "
661#ifdef ANDROID
662                            "or SL_IID_ANDROIDSIMPLEBUFFERQUEUE "
663#endif
664                            "with a non-buffer queue data sink");
665                    return SL_RESULT_FEATURE_UNSUPPORTED;
666                }
667            }
668            break;
669        }
670        break;
671
672    case SL_DATALOCATOR_BUFFERQUEUE:
673#ifdef ANDROID
674    case SL_DATALOCATOR_ANDROIDSIMPLEBUFFERQUEUE:
675#endif
676        // can't require SLSeekItf if data source is a buffer queue
677        index = clazz->mMPH_to_index[MPH_SEEK];
678        if (0 <= index) {
679            if (requiredMask & (1 << index)) {
680                SL_LOGE("can't require SL_IID_SEEK with a buffer queue data source");
681                return SL_RESULT_FEATURE_UNSUPPORTED;
682            }
683        }
684        // can't require SLMuteSoloItf if data source is a mono buffer queue
685        index = clazz->mMPH_to_index[MPH_MUTESOLO];
686        if (0 <= index) {
687            if ((requiredMask & (1 << index)) &&
688                    (SL_DATAFORMAT_PCM == pSrcDataLocatorFormat->mFormat.mFormatType) &&
689                    (1 == pSrcDataLocatorFormat->mFormat.mPCM.numChannels)) {
690                SL_LOGE("can't require SL_IID_MUTESOLO with a mono buffer queue data source");
691                return SL_RESULT_FEATURE_UNSUPPORTED;
692            }
693        }
694        break;
695
696#ifdef ANDROID
697    case SL_DATALOCATOR_ANDROIDBUFFERQUEUE:
698        // can't require SLSeekItf if data source is an Android buffer queue
699        index = clazz->mMPH_to_index[MPH_SEEK];
700        if (0 <= index) {
701            if (requiredMask & (1 << index)) {
702                SL_LOGE("can't require SL_IID_SEEK with a SL_DATALOCATOR_ANDROIDBUFFERQUEUE "\
703                        "source");
704                return SL_RESULT_FEATURE_UNSUPPORTED;
705            }
706        }
707        switch (pSinkDataLocatorFormat->mLocator.mLocatorType) {
708        // for use-case AAC decode from SLAndroidBufferQueueItf with AAC ADTS data
709        case SL_DATALOCATOR_BUFFERQUEUE:
710        case SL_DATALOCATOR_ANDROIDSIMPLEBUFFERQUEUE:
711            break;
712        // for use-case audio playback from SLAndroidBufferQueueItf with MP2TS data
713        case SL_DATALOCATOR_OUTPUTMIX:
714            break;
715        default:
716            SL_LOGE("Invalid sink for SL_DATALOCATOR_ANDROIDBUFFERQUEUE source");
717            return SL_RESULT_FEATURE_UNSUPPORTED;
718            break;
719        }
720        break;
721#endif
722    case SL_DATALOCATOR_ADDRESS:
723    case SL_DATALOCATOR_MIDIBUFFERQUEUE:
724    case XA_DATALOCATOR_NATIVEDISPLAY:
725        // any special checks here???
726    default:
727        // can't require SLBufferQueueItf or its alias SLAndroidSimpleBufferQueueItf
728        // if the data source is not a buffer queue
729        index = clazz->mMPH_to_index[MPH_BUFFERQUEUE];
730#ifdef ANDROID
731        assert(index == clazz->mMPH_to_index[MPH_ANDROIDSIMPLEBUFFERQUEUE]);
732#endif
733        if (0 <= index) {
734            if (requiredMask & (1 << index)) {
735                SL_LOGE("can't require SL_IID_BUFFERQUEUE "
736#ifdef ANDROID
737                        "or SL_IID_ANDROIDSIMPLEBUFFERQUEUE "
738#endif
739                        "with a non-buffer queue data source");
740                return SL_RESULT_FEATURE_UNSUPPORTED;
741            }
742        }
743        break;
744    }
745    return SL_RESULT_SUCCESS;
746}
747
748
749/** \brief Free the local deep copy of a data format */
750
751static void freeDataFormat(DataFormat *pDataFormat)
752{
753    switch (pDataFormat->mFormatType) {
754    case SL_DATAFORMAT_MIME:
755        if (NULL != pDataFormat->mMIME.mimeType) {
756            free(pDataFormat->mMIME.mimeType);
757            pDataFormat->mMIME.mimeType = NULL;
758        }
759        break;
760    case SL_ANDROID_DATAFORMAT_PCM_EX:
761    case SL_DATAFORMAT_PCM:
762    case XA_DATAFORMAT_RAWIMAGE:
763    case SL_DATAFORMAT_NULL:
764        break;
765    default:
766        // an invalid data format is caught earlier during the copy
767        assert(false);
768        break;
769    }
770}
771
772
773/** \brief Check a data source and make local deep copy */
774
775SLresult checkDataSource(const char *name, const SLDataSource *pDataSrc,
776        DataLocatorFormat *pDataLocatorFormat, SLuint32 allowedDataLocatorMask,
777        SLuint32 allowedDataFormatMask)
778{
779    assert(NULL != name && NULL != pDataLocatorFormat);
780    pDataLocatorFormat->u.mSource.pLocator = &pDataLocatorFormat->mLocator;
781    pDataLocatorFormat->u.mSource.pFormat = &pDataLocatorFormat->mFormat;
782
783    if (NULL == pDataSrc) {
784        pDataLocatorFormat->mLocator.mLocatorType = SL_DATALOCATOR_NULL;
785        pDataLocatorFormat->mFormat.mFormatType = SL_DATAFORMAT_NULL;
786        if ((allowedDataLocatorMask & DATALOCATOR_MASK_NULL) &&
787                (allowedDataFormatMask & DATAFORMAT_MASK_NULL)) {
788            return SL_RESULT_SUCCESS;
789        }
790        SL_LOGE("%s: data source cannot be NULL", name);
791        return SL_RESULT_PARAMETER_INVALID;
792    }
793    SLDataSource myDataSrc = *pDataSrc;
794    SLresult result;
795    result = checkDataLocator(name, myDataSrc.pLocator, &pDataLocatorFormat->mLocator,
796            allowedDataLocatorMask);
797    if (SL_RESULT_SUCCESS != result) {
798        return result;
799    }
800
801    switch (pDataLocatorFormat->mLocator.mLocatorType) {
802    case SL_DATALOCATOR_URI:
803        allowedDataFormatMask &= DATAFORMAT_MASK_MIME;
804        break;
805    case SL_DATALOCATOR_ADDRESS:
806    case SL_DATALOCATOR_BUFFERQUEUE:
807        allowedDataFormatMask &= DATAFORMAT_MASK_PCM | DATAFORMAT_MASK_PCM_EX;
808        break;
809    // Per the spec, the pFormat field is ignored in some cases
810    case SL_DATALOCATOR_IODEVICE:
811        myDataSrc.pFormat = NULL;
812        // fall through
813    case SL_DATALOCATOR_NULL:
814    case SL_DATALOCATOR_MIDIBUFFERQUEUE:
815        allowedDataFormatMask &= DATAFORMAT_MASK_NULL;
816        break;
817    case SL_DATALOCATOR_OUTPUTMIX:
818    case XA_DATALOCATOR_NATIVEDISPLAY:
819        allowedDataFormatMask = DATAFORMAT_MASK_NONE;
820        break;
821#ifdef ANDROID
822    case SL_DATALOCATOR_ANDROIDFD:
823        allowedDataFormatMask &= DATAFORMAT_MASK_MIME;
824        break;
825    case SL_DATALOCATOR_ANDROIDSIMPLEBUFFERQUEUE:
826        allowedDataFormatMask &= DATAFORMAT_MASK_PCM | DATAFORMAT_MASK_PCM_EX;
827        break;
828    case SL_DATALOCATOR_ANDROIDBUFFERQUEUE:
829        allowedDataFormatMask &= DATAFORMAT_MASK_MIME;;
830        break;
831#endif
832    default:
833        // invalid data locator type is caught earlier
834        assert(false);
835        allowedDataFormatMask = DATAFORMAT_MASK_NONE;
836        break;
837    }
838
839    result = checkDataFormat(name, myDataSrc.pFormat, &pDataLocatorFormat->mFormat,
840            allowedDataFormatMask);
841    if (SL_RESULT_SUCCESS != result) {
842        freeDataLocator(&pDataLocatorFormat->mLocator);
843        return result;
844    }
845
846    return SL_RESULT_SUCCESS;
847}
848
849
850/** \brief Check a data sink and make local deep copy */
851
852SLresult checkDataSink(const char *name, const SLDataSink *pDataSink,
853        DataLocatorFormat *pDataLocatorFormat, SLuint32 allowedDataLocatorMask,
854        SLuint32 allowedDataFormatMask)
855{
856    assert(NULL != name && NULL != pDataLocatorFormat);
857    pDataLocatorFormat->u.mSink.pLocator = &pDataLocatorFormat->mLocator;
858    pDataLocatorFormat->u.mSink.pFormat = &pDataLocatorFormat->mFormat;
859
860    if (NULL == pDataSink) {
861        pDataLocatorFormat->mLocator.mLocatorType = SL_DATALOCATOR_NULL;
862        pDataLocatorFormat->mFormat.mFormatType = SL_DATAFORMAT_NULL;
863        if ((allowedDataLocatorMask & DATALOCATOR_MASK_NULL) &&
864                (allowedDataFormatMask & DATAFORMAT_MASK_NULL)) {
865            return SL_RESULT_SUCCESS;
866        }
867        SL_LOGE("%s: data sink cannot be NULL", name);
868        return SL_RESULT_PARAMETER_INVALID;
869    }
870    SLDataSink myDataSink = *pDataSink;
871    SLresult result;
872    result = checkDataLocator(name, myDataSink.pLocator, &pDataLocatorFormat->mLocator,
873            allowedDataLocatorMask);
874    if (SL_RESULT_SUCCESS != result) {
875        return result;
876    }
877
878    switch (pDataLocatorFormat->mLocator.mLocatorType) {
879    case SL_DATALOCATOR_URI:
880        allowedDataFormatMask &= DATAFORMAT_MASK_MIME;
881        break;
882    case SL_DATALOCATOR_ADDRESS:
883    case SL_DATALOCATOR_BUFFERQUEUE:
884        allowedDataFormatMask &= DATAFORMAT_MASK_PCM | DATAFORMAT_MASK_PCM_EX;
885        break;
886    // Per the spec, the pFormat field is ignored in some cases
887    case SL_DATALOCATOR_IODEVICE:
888    case SL_DATALOCATOR_OUTPUTMIX:
889    case XA_DATALOCATOR_NATIVEDISPLAY:
890        myDataSink.pFormat = NULL;
891        // fall through
892    case SL_DATALOCATOR_NULL:
893    case SL_DATALOCATOR_MIDIBUFFERQUEUE:
894        allowedDataFormatMask &= DATAFORMAT_MASK_NULL;
895        break;
896#ifdef ANDROID
897    case SL_DATALOCATOR_ANDROIDFD:
898        allowedDataFormatMask = DATAFORMAT_MASK_NONE;
899        break;
900    case SL_DATALOCATOR_ANDROIDSIMPLEBUFFERQUEUE:
901        allowedDataFormatMask &= DATAFORMAT_MASK_PCM | DATAFORMAT_MASK_PCM_EX;
902        break;
903    case SL_DATALOCATOR_ANDROIDBUFFERQUEUE:
904        allowedDataFormatMask = DATAFORMAT_MASK_NONE;
905        break;
906#endif
907    default:
908        // invalid data locator type is caught earlier
909        assert(false);
910        allowedDataFormatMask = DATAFORMAT_MASK_NONE;
911        break;
912    }
913
914    result = checkDataFormat(name, myDataSink.pFormat, &pDataLocatorFormat->mFormat,
915            allowedDataFormatMask);
916    if (SL_RESULT_SUCCESS != result) {
917        freeDataLocator(&pDataLocatorFormat->mLocator);
918        return result;
919    }
920
921    return SL_RESULT_SUCCESS;
922}
923
924
925/** \brief Free the local deep copy of a data locator format */
926
927void freeDataLocatorFormat(DataLocatorFormat *dlf)
928{
929    assert(NULL != dlf);
930    freeDataLocator(&dlf->mLocator);
931    freeDataFormat(&dlf->mFormat);
932}
933