Utils.cpp revision e9e63bcf6c36351f1129b0bdc5e93f17f0f9f0b4
1/*
2 * Copyright (C) 2009 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//#define LOG_NDEBUG 0
18#define LOG_TAG "Utils"
19#include <utils/Log.h>
20#include <ctype.h>
21
22#include "include/ESDS.h"
23
24#include <arpa/inet.h>
25#include <cutils/properties.h>
26#include <media/openmax/OMX_Audio.h>
27#include <media/stagefright/foundation/ABuffer.h>
28#include <media/stagefright/foundation/ADebug.h>
29#include <media/stagefright/foundation/AMessage.h>
30#include <media/stagefright/MetaData.h>
31#include <media/stagefright/MediaDefs.h>
32#include <media/AudioSystem.h>
33#include <media/MediaPlayerInterface.h>
34#include <hardware/audio.h>
35#include <media/stagefright/Utils.h>
36#include <media/AudioParameter.h>
37
38namespace android {
39
40uint16_t U16_AT(const uint8_t *ptr) {
41    return ptr[0] << 8 | ptr[1];
42}
43
44uint32_t U32_AT(const uint8_t *ptr) {
45    return ptr[0] << 24 | ptr[1] << 16 | ptr[2] << 8 | ptr[3];
46}
47
48uint64_t U64_AT(const uint8_t *ptr) {
49    return ((uint64_t)U32_AT(ptr)) << 32 | U32_AT(ptr + 4);
50}
51
52uint16_t U16LE_AT(const uint8_t *ptr) {
53    return ptr[0] | (ptr[1] << 8);
54}
55
56uint32_t U32LE_AT(const uint8_t *ptr) {
57    return ptr[3] << 24 | ptr[2] << 16 | ptr[1] << 8 | ptr[0];
58}
59
60uint64_t U64LE_AT(const uint8_t *ptr) {
61    return ((uint64_t)U32LE_AT(ptr + 4)) << 32 | U32LE_AT(ptr);
62}
63
64// XXX warning: these won't work on big-endian host.
65uint64_t ntoh64(uint64_t x) {
66    return ((uint64_t)ntohl(x & 0xffffffff) << 32) | ntohl(x >> 32);
67}
68
69uint64_t hton64(uint64_t x) {
70    return ((uint64_t)htonl(x & 0xffffffff) << 32) | htonl(x >> 32);
71}
72
73status_t convertMetaDataToMessage(
74        const sp<MetaData> &meta, sp<AMessage> *format) {
75    format->clear();
76
77    const char *mime;
78    CHECK(meta->findCString(kKeyMIMEType, &mime));
79
80    sp<AMessage> msg = new AMessage;
81    msg->setString("mime", mime);
82
83    int64_t durationUs;
84    if (meta->findInt64(kKeyDuration, &durationUs)) {
85        msg->setInt64("durationUs", durationUs);
86    }
87
88    int avgBitRate;
89    if (meta->findInt32(kKeyBitRate, &avgBitRate)) {
90        msg->setInt32("bit-rate", avgBitRate);
91    }
92
93    int32_t isSync;
94    if (meta->findInt32(kKeyIsSyncFrame, &isSync) && isSync != 0) {
95        msg->setInt32("is-sync-frame", 1);
96    }
97
98    if (!strncasecmp("video/", mime, 6)) {
99        int32_t width, height;
100        CHECK(meta->findInt32(kKeyWidth, &width));
101        CHECK(meta->findInt32(kKeyHeight, &height));
102
103        msg->setInt32("width", width);
104        msg->setInt32("height", height);
105
106        int32_t sarWidth, sarHeight;
107        if (meta->findInt32(kKeySARWidth, &sarWidth)
108                && meta->findInt32(kKeySARHeight, &sarHeight)) {
109            msg->setInt32("sar-width", sarWidth);
110            msg->setInt32("sar-height", sarHeight);
111        }
112    } else if (!strncasecmp("audio/", mime, 6)) {
113        int32_t numChannels, sampleRate;
114        CHECK(meta->findInt32(kKeyChannelCount, &numChannels));
115        CHECK(meta->findInt32(kKeySampleRate, &sampleRate));
116
117        msg->setInt32("channel-count", numChannels);
118        msg->setInt32("sample-rate", sampleRate);
119
120        int32_t channelMask;
121        if (meta->findInt32(kKeyChannelMask, &channelMask)) {
122            msg->setInt32("channel-mask", channelMask);
123        }
124
125        int32_t delay = 0;
126        if (meta->findInt32(kKeyEncoderDelay, &delay)) {
127            msg->setInt32("encoder-delay", delay);
128        }
129        int32_t padding = 0;
130        if (meta->findInt32(kKeyEncoderPadding, &padding)) {
131            msg->setInt32("encoder-padding", padding);
132        }
133
134        int32_t isADTS;
135        if (meta->findInt32(kKeyIsADTS, &isADTS)) {
136            msg->setInt32("is-adts", true);
137        }
138    }
139
140    int32_t maxInputSize;
141    if (meta->findInt32(kKeyMaxInputSize, &maxInputSize)) {
142        msg->setInt32("max-input-size", maxInputSize);
143    }
144
145    int32_t rotationDegrees;
146    if (meta->findInt32(kKeyRotation, &rotationDegrees)) {
147        msg->setInt32("rotation-degrees", rotationDegrees);
148    }
149
150    uint32_t type;
151    const void *data;
152    size_t size;
153    if (meta->findData(kKeyAVCC, &type, &data, &size)) {
154        // Parse the AVCDecoderConfigurationRecord
155
156        const uint8_t *ptr = (const uint8_t *)data;
157
158        CHECK(size >= 7);
159        CHECK_EQ((unsigned)ptr[0], 1u);  // configurationVersion == 1
160        uint8_t profile = ptr[1];
161        uint8_t level = ptr[3];
162
163        // There is decodable content out there that fails the following
164        // assertion, let's be lenient for now...
165        // CHECK((ptr[4] >> 2) == 0x3f);  // reserved
166
167        size_t lengthSize = 1 + (ptr[4] & 3);
168
169        // commented out check below as H264_QVGA_500_NO_AUDIO.3gp
170        // violates it...
171        // CHECK((ptr[5] >> 5) == 7);  // reserved
172
173        size_t numSeqParameterSets = ptr[5] & 31;
174
175        ptr += 6;
176        size -= 6;
177
178        sp<ABuffer> buffer = new ABuffer(1024);
179        buffer->setRange(0, 0);
180
181        for (size_t i = 0; i < numSeqParameterSets; ++i) {
182            CHECK(size >= 2);
183            size_t length = U16_AT(ptr);
184
185            ptr += 2;
186            size -= 2;
187
188            CHECK(size >= length);
189
190            memcpy(buffer->data() + buffer->size(), "\x00\x00\x00\x01", 4);
191            memcpy(buffer->data() + buffer->size() + 4, ptr, length);
192            buffer->setRange(0, buffer->size() + 4 + length);
193
194            ptr += length;
195            size -= length;
196        }
197
198        buffer->meta()->setInt32("csd", true);
199        buffer->meta()->setInt64("timeUs", 0);
200
201        msg->setBuffer("csd-0", buffer);
202
203        buffer = new ABuffer(1024);
204        buffer->setRange(0, 0);
205
206        CHECK(size >= 1);
207        size_t numPictureParameterSets = *ptr;
208        ++ptr;
209        --size;
210
211        for (size_t i = 0; i < numPictureParameterSets; ++i) {
212            CHECK(size >= 2);
213            size_t length = U16_AT(ptr);
214
215            ptr += 2;
216            size -= 2;
217
218            CHECK(size >= length);
219
220            memcpy(buffer->data() + buffer->size(), "\x00\x00\x00\x01", 4);
221            memcpy(buffer->data() + buffer->size() + 4, ptr, length);
222            buffer->setRange(0, buffer->size() + 4 + length);
223
224            ptr += length;
225            size -= length;
226        }
227
228        buffer->meta()->setInt32("csd", true);
229        buffer->meta()->setInt64("timeUs", 0);
230        msg->setBuffer("csd-1", buffer);
231    } else if (meta->findData(kKeyHVCC, &type, &data, &size)) {
232        const uint8_t *ptr = (const uint8_t *)data;
233
234        CHECK(size >= 7);
235        CHECK_EQ((unsigned)ptr[0], 1u);  // configurationVersion == 1
236        uint8_t profile = ptr[1] & 31;
237        uint8_t level = ptr[12];
238        ptr += 22;
239        size -= 22;
240
241
242        size_t numofArrays = (char)ptr[0];
243        ptr += 1;
244        size -= 1;
245        size_t j = 0, i = 0;
246
247        sp<ABuffer> buffer = new ABuffer(1024);
248        buffer->setRange(0, 0);
249
250        for (i = 0; i < numofArrays; i++) {
251            ptr += 1;
252            size -= 1;
253
254            //Num of nals
255            size_t numofNals = U16_AT(ptr);
256
257            ptr += 2;
258            size -= 2;
259
260            for (j = 0; j < numofNals; j++) {
261                CHECK(size >= 2);
262                size_t length = U16_AT(ptr);
263
264                ptr += 2;
265                size -= 2;
266
267                CHECK(size >= length);
268
269                memcpy(buffer->data() + buffer->size(), "\x00\x00\x00\x01", 4);
270                memcpy(buffer->data() + buffer->size() + 4, ptr, length);
271                buffer->setRange(0, buffer->size() + 4 + length);
272
273                ptr += length;
274                size -= length;
275            }
276        }
277        buffer->meta()->setInt32("csd", true);
278        buffer->meta()->setInt64("timeUs", 0);
279        msg->setBuffer("csd-0", buffer);
280
281    } else if (meta->findData(kKeyESDS, &type, &data, &size)) {
282        ESDS esds((const char *)data, size);
283        CHECK_EQ(esds.InitCheck(), (status_t)OK);
284
285        const void *codec_specific_data;
286        size_t codec_specific_data_size;
287        esds.getCodecSpecificInfo(
288                &codec_specific_data, &codec_specific_data_size);
289
290        sp<ABuffer> buffer = new ABuffer(codec_specific_data_size);
291
292        memcpy(buffer->data(), codec_specific_data,
293               codec_specific_data_size);
294
295        buffer->meta()->setInt32("csd", true);
296        buffer->meta()->setInt64("timeUs", 0);
297        msg->setBuffer("csd-0", buffer);
298    } else if (meta->findData(kKeyVorbisInfo, &type, &data, &size)) {
299        sp<ABuffer> buffer = new ABuffer(size);
300        memcpy(buffer->data(), data, size);
301
302        buffer->meta()->setInt32("csd", true);
303        buffer->meta()->setInt64("timeUs", 0);
304        msg->setBuffer("csd-0", buffer);
305
306        if (!meta->findData(kKeyVorbisBooks, &type, &data, &size)) {
307            return -EINVAL;
308        }
309
310        buffer = new ABuffer(size);
311        memcpy(buffer->data(), data, size);
312
313        buffer->meta()->setInt32("csd", true);
314        buffer->meta()->setInt64("timeUs", 0);
315        msg->setBuffer("csd-1", buffer);
316    } else if (meta->findData(kKeyOpusHeader, &type, &data, &size)) {
317        sp<ABuffer> buffer = new ABuffer(size);
318        memcpy(buffer->data(), data, size);
319
320        buffer->meta()->setInt32("csd", true);
321        buffer->meta()->setInt64("timeUs", 0);
322        msg->setBuffer("csd-0", buffer);
323    }
324
325    *format = msg;
326
327    return OK;
328}
329
330static size_t reassembleAVCC(const sp<ABuffer> &csd0, const sp<ABuffer> csd1, char *avcc) {
331
332    avcc[0] = 1;        // version
333    avcc[1] = 0x64;     // profile
334    avcc[2] = 0;        // unused (?)
335    avcc[3] = 0xd;      // level
336    avcc[4] = 0xff;     // reserved+size
337
338    size_t i = 0;
339    int numparams = 0;
340    int lastparamoffset = 0;
341    int avccidx = 6;
342    do {
343        if (i >= csd0->size() - 4 ||
344                memcmp(csd0->data() + i, "\x00\x00\x00\x01", 4) == 0) {
345            if (i >= csd0->size() - 4) {
346                // there can't be another param here, so use all the rest
347                i = csd0->size();
348            }
349            ALOGV("block at %zu, last was %d", i, lastparamoffset);
350            if (lastparamoffset > 0) {
351                int size = i - lastparamoffset;
352                avcc[avccidx++] = size >> 8;
353                avcc[avccidx++] = size & 0xff;
354                memcpy(avcc+avccidx, csd0->data() + lastparamoffset, size);
355                avccidx += size;
356                numparams++;
357            }
358            i += 4;
359            lastparamoffset = i;
360        } else {
361            i++;
362        }
363    } while(i < csd0->size());
364    ALOGV("csd0 contains %d params", numparams);
365
366    avcc[5] = 0xe0 | numparams;
367    //and now csd-1
368    i = 0;
369    numparams = 0;
370    lastparamoffset = 0;
371    int numpicparamsoffset = avccidx;
372    avccidx++;
373    do {
374        if (i >= csd1->size() - 4 ||
375                memcmp(csd1->data() + i, "\x00\x00\x00\x01", 4) == 0) {
376            if (i >= csd1->size() - 4) {
377                // there can't be another param here, so use all the rest
378                i = csd1->size();
379            }
380            ALOGV("block at %zu, last was %d", i, lastparamoffset);
381            if (lastparamoffset > 0) {
382                int size = i - lastparamoffset;
383                avcc[avccidx++] = size >> 8;
384                avcc[avccidx++] = size & 0xff;
385                memcpy(avcc+avccidx, csd1->data() + lastparamoffset, size);
386                avccidx += size;
387                numparams++;
388            }
389            i += 4;
390            lastparamoffset = i;
391        } else {
392            i++;
393        }
394    } while(i < csd1->size());
395    avcc[numpicparamsoffset] = numparams;
396    return avccidx;
397}
398
399static void reassembleESDS(const sp<ABuffer> &csd0, char *esds) {
400    int csd0size = csd0->size();
401    esds[0] = 3; // kTag_ESDescriptor;
402    int esdescriptorsize = 26 + csd0size;
403    CHECK(esdescriptorsize < 268435456); // 7 bits per byte, so max is 2^28-1
404    esds[1] = 0x80 | (esdescriptorsize >> 21);
405    esds[2] = 0x80 | ((esdescriptorsize >> 14) & 0x7f);
406    esds[3] = 0x80 | ((esdescriptorsize >> 7) & 0x7f);
407    esds[4] = (esdescriptorsize & 0x7f);
408    esds[5] = esds[6] = 0; // es id
409    esds[7] = 0; // flags
410    esds[8] = 4; // kTag_DecoderConfigDescriptor
411    int configdescriptorsize = 18 + csd0size;
412    esds[9] = 0x80 | (configdescriptorsize >> 21);
413    esds[10] = 0x80 | ((configdescriptorsize >> 14) & 0x7f);
414    esds[11] = 0x80 | ((configdescriptorsize >> 7) & 0x7f);
415    esds[12] = (configdescriptorsize & 0x7f);
416    esds[13] = 0x40; // objectTypeIndication
417    esds[14] = 0x15; // not sure what 14-25 mean, they are ignored by ESDS.cpp,
418    esds[15] = 0x00; // but the actual values here were taken from a real file.
419    esds[16] = 0x18;
420    esds[17] = 0x00;
421    esds[18] = 0x00;
422    esds[19] = 0x00;
423    esds[20] = 0xfa;
424    esds[21] = 0x00;
425    esds[22] = 0x00;
426    esds[23] = 0x00;
427    esds[24] = 0xfa;
428    esds[25] = 0x00;
429    esds[26] = 5; // kTag_DecoderSpecificInfo;
430    esds[27] = 0x80 | (csd0size >> 21);
431    esds[28] = 0x80 | ((csd0size >> 14) & 0x7f);
432    esds[29] = 0x80 | ((csd0size >> 7) & 0x7f);
433    esds[30] = (csd0size & 0x7f);
434    memcpy((void*)&esds[31], csd0->data(), csd0size);
435    // data following this is ignored, so don't bother appending it
436
437}
438
439void convertMessageToMetaData(const sp<AMessage> &msg, sp<MetaData> &meta) {
440    AString mime;
441    if (msg->findString("mime", &mime)) {
442        meta->setCString(kKeyMIMEType, mime.c_str());
443    } else {
444        ALOGW("did not find mime type");
445    }
446
447    int64_t durationUs;
448    if (msg->findInt64("durationUs", &durationUs)) {
449        meta->setInt64(kKeyDuration, durationUs);
450    }
451
452    int32_t isSync;
453    if (msg->findInt32("is-sync-frame", &isSync) && isSync != 0) {
454        meta->setInt32(kKeyIsSyncFrame, 1);
455    }
456
457    if (mime.startsWith("video/")) {
458        int32_t width;
459        int32_t height;
460        if (msg->findInt32("width", &width) && msg->findInt32("height", &height)) {
461            meta->setInt32(kKeyWidth, width);
462            meta->setInt32(kKeyHeight, height);
463        } else {
464            ALOGW("did not find width and/or height");
465        }
466
467        int32_t sarWidth, sarHeight;
468        if (msg->findInt32("sar-width", &sarWidth)
469                && msg->findInt32("sar-height", &sarHeight)) {
470            meta->setInt32(kKeySARWidth, sarWidth);
471            meta->setInt32(kKeySARHeight, sarHeight);
472        }
473    } else if (mime.startsWith("audio/")) {
474        int32_t numChannels;
475        if (msg->findInt32("channel-count", &numChannels)) {
476            meta->setInt32(kKeyChannelCount, numChannels);
477        }
478        int32_t sampleRate;
479        if (msg->findInt32("sample-rate", &sampleRate)) {
480            meta->setInt32(kKeySampleRate, sampleRate);
481        }
482        int32_t channelMask;
483        if (msg->findInt32("channel-mask", &channelMask)) {
484            meta->setInt32(kKeyChannelMask, channelMask);
485        }
486        int32_t delay = 0;
487        if (msg->findInt32("encoder-delay", &delay)) {
488            meta->setInt32(kKeyEncoderDelay, delay);
489        }
490        int32_t padding = 0;
491        if (msg->findInt32("encoder-padding", &padding)) {
492            meta->setInt32(kKeyEncoderPadding, padding);
493        }
494
495        int32_t isADTS;
496        if (msg->findInt32("is-adts", &isADTS)) {
497            meta->setInt32(kKeyIsADTS, isADTS);
498        }
499    }
500
501    int32_t maxInputSize;
502    if (msg->findInt32("max-input-size", &maxInputSize)) {
503        meta->setInt32(kKeyMaxInputSize, maxInputSize);
504    }
505
506    // reassemble the csd data into its original form
507    sp<ABuffer> csd0;
508    if (msg->findBuffer("csd-0", &csd0)) {
509        if (mime.startsWith("video/")) { // do we need to be stricter than this?
510            sp<ABuffer> csd1;
511            if (msg->findBuffer("csd-1", &csd1)) {
512                char avcc[1024]; // that oughta be enough, right?
513                size_t outsize = reassembleAVCC(csd0, csd1, avcc);
514                meta->setData(kKeyAVCC, kKeyAVCC, avcc, outsize);
515            }
516        } else if (mime.startsWith("audio/")) {
517            int csd0size = csd0->size();
518            char esds[csd0size + 31];
519            reassembleESDS(csd0, esds);
520            meta->setData(kKeyESDS, kKeyESDS, esds, sizeof(esds));
521        }
522    }
523
524    int32_t timeScale;
525    if (msg->findInt32("time-scale", &timeScale)) {
526        meta->setInt32(kKeyTimeScale, timeScale);
527    }
528
529    // XXX TODO add whatever other keys there are
530
531#if 0
532    ALOGI("converted %s to:", msg->debugString(0).c_str());
533    meta->dumpToLog();
534#endif
535}
536
537AString MakeUserAgent() {
538    AString ua;
539    ua.append("stagefright/1.2 (Linux;Android ");
540
541#if (PROPERTY_VALUE_MAX < 8)
542#error "PROPERTY_VALUE_MAX must be at least 8"
543#endif
544
545    char value[PROPERTY_VALUE_MAX];
546    property_get("ro.build.version.release", value, "Unknown");
547    ua.append(value);
548    ua.append(")");
549
550    return ua;
551}
552
553status_t sendMetaDataToHal(sp<MediaPlayerBase::AudioSink>& sink,
554                           const sp<MetaData>& meta)
555{
556    int32_t sampleRate = 0;
557    int32_t bitRate = 0;
558    int32_t channelMask = 0;
559    int32_t delaySamples = 0;
560    int32_t paddingSamples = 0;
561
562    AudioParameter param = AudioParameter();
563
564    if (meta->findInt32(kKeySampleRate, &sampleRate)) {
565        param.addInt(String8(AUDIO_OFFLOAD_CODEC_SAMPLE_RATE), sampleRate);
566    }
567    if (meta->findInt32(kKeyChannelMask, &channelMask)) {
568        param.addInt(String8(AUDIO_OFFLOAD_CODEC_NUM_CHANNEL), channelMask);
569    }
570    if (meta->findInt32(kKeyBitRate, &bitRate)) {
571        param.addInt(String8(AUDIO_OFFLOAD_CODEC_AVG_BIT_RATE), bitRate);
572    }
573    if (meta->findInt32(kKeyEncoderDelay, &delaySamples)) {
574        param.addInt(String8(AUDIO_OFFLOAD_CODEC_DELAY_SAMPLES), delaySamples);
575    }
576    if (meta->findInt32(kKeyEncoderPadding, &paddingSamples)) {
577        param.addInt(String8(AUDIO_OFFLOAD_CODEC_PADDING_SAMPLES), paddingSamples);
578    }
579
580    ALOGV("sendMetaDataToHal: bitRate %d, sampleRate %d, chanMask %d,"
581          "delaySample %d, paddingSample %d", bitRate, sampleRate,
582          channelMask, delaySamples, paddingSamples);
583
584    sink->setParameters(param.toString());
585    return OK;
586}
587
588struct mime_conv_t {
589    const char* mime;
590    audio_format_t format;
591};
592
593static const struct mime_conv_t mimeLookup[] = {
594    { MEDIA_MIMETYPE_AUDIO_MPEG,        AUDIO_FORMAT_MP3 },
595    { MEDIA_MIMETYPE_AUDIO_RAW,         AUDIO_FORMAT_PCM_16_BIT },
596    { MEDIA_MIMETYPE_AUDIO_AMR_NB,      AUDIO_FORMAT_AMR_NB },
597    { MEDIA_MIMETYPE_AUDIO_AMR_WB,      AUDIO_FORMAT_AMR_WB },
598    { MEDIA_MIMETYPE_AUDIO_AAC,         AUDIO_FORMAT_AAC },
599    { MEDIA_MIMETYPE_AUDIO_VORBIS,      AUDIO_FORMAT_VORBIS },
600    { MEDIA_MIMETYPE_AUDIO_OPUS,        AUDIO_FORMAT_OPUS},
601    { 0, AUDIO_FORMAT_INVALID }
602};
603
604status_t mapMimeToAudioFormat( audio_format_t& format, const char* mime )
605{
606const struct mime_conv_t* p = &mimeLookup[0];
607    while (p->mime != NULL) {
608        if (0 == strcasecmp(mime, p->mime)) {
609            format = p->format;
610            return OK;
611        }
612        ++p;
613    }
614
615    return BAD_VALUE;
616}
617
618struct aac_format_conv_t {
619    OMX_AUDIO_AACPROFILETYPE eAacProfileType;
620    audio_format_t format;
621};
622
623static const struct aac_format_conv_t profileLookup[] = {
624    { OMX_AUDIO_AACObjectMain,        AUDIO_FORMAT_AAC_MAIN},
625    { OMX_AUDIO_AACObjectLC,          AUDIO_FORMAT_AAC_LC},
626    { OMX_AUDIO_AACObjectSSR,         AUDIO_FORMAT_AAC_SSR},
627    { OMX_AUDIO_AACObjectLTP,         AUDIO_FORMAT_AAC_LTP},
628    { OMX_AUDIO_AACObjectHE,          AUDIO_FORMAT_AAC_HE_V1},
629    { OMX_AUDIO_AACObjectScalable,    AUDIO_FORMAT_AAC_SCALABLE},
630    { OMX_AUDIO_AACObjectERLC,        AUDIO_FORMAT_AAC_ERLC},
631    { OMX_AUDIO_AACObjectLD,          AUDIO_FORMAT_AAC_LD},
632    { OMX_AUDIO_AACObjectHE_PS,       AUDIO_FORMAT_AAC_HE_V2},
633    { OMX_AUDIO_AACObjectELD,         AUDIO_FORMAT_AAC_ELD},
634    { OMX_AUDIO_AACObjectNull,        AUDIO_FORMAT_AAC},
635};
636
637void mapAACProfileToAudioFormat( audio_format_t& format, uint64_t eAacProfile)
638{
639const struct aac_format_conv_t* p = &profileLookup[0];
640    while (p->eAacProfileType != OMX_AUDIO_AACObjectNull) {
641        if (eAacProfile == p->eAacProfileType) {
642            format = p->format;
643            return;
644        }
645        ++p;
646    }
647    format = AUDIO_FORMAT_AAC;
648    return;
649}
650
651bool canOffloadStream(const sp<MetaData>& meta, bool hasVideo,
652                      bool isStreaming, audio_stream_type_t streamType)
653{
654    const char *mime;
655    if (meta == NULL) {
656        return false;
657    }
658    CHECK(meta->findCString(kKeyMIMEType, &mime));
659
660    audio_offload_info_t info = AUDIO_INFO_INITIALIZER;
661
662    info.format = AUDIO_FORMAT_INVALID;
663    if (mapMimeToAudioFormat(info.format, mime) != OK) {
664        ALOGE(" Couldn't map mime type \"%s\" to a valid AudioSystem::audio_format !", mime);
665        return false;
666    } else {
667        ALOGV("Mime type \"%s\" mapped to audio_format %d", mime, info.format);
668    }
669
670    if (AUDIO_FORMAT_INVALID == info.format) {
671        // can't offload if we don't know what the source format is
672        ALOGE("mime type \"%s\" not a known audio format", mime);
673        return false;
674    }
675
676    // Redefine aac format according to its profile
677    // Offloading depends on audio DSP capabilities.
678    int32_t aacaot = -1;
679    if (meta->findInt32(kKeyAACAOT, &aacaot)) {
680        mapAACProfileToAudioFormat(info.format,(OMX_AUDIO_AACPROFILETYPE) aacaot);
681    }
682
683    int32_t srate = -1;
684    if (!meta->findInt32(kKeySampleRate, &srate)) {
685        ALOGV("track of type '%s' does not publish sample rate", mime);
686    }
687    info.sample_rate = srate;
688
689    int32_t cmask = 0;
690    if (!meta->findInt32(kKeyChannelMask, &cmask)) {
691        ALOGV("track of type '%s' does not publish channel mask", mime);
692
693        // Try a channel count instead
694        int32_t channelCount;
695        if (!meta->findInt32(kKeyChannelCount, &channelCount)) {
696            ALOGV("track of type '%s' does not publish channel count", mime);
697        } else {
698            cmask = audio_channel_out_mask_from_count(channelCount);
699        }
700    }
701    info.channel_mask = cmask;
702
703    int64_t duration = 0;
704    if (!meta->findInt64(kKeyDuration, &duration)) {
705        ALOGV("track of type '%s' does not publish duration", mime);
706    }
707    info.duration_us = duration;
708
709    int32_t brate = -1;
710    if (!meta->findInt32(kKeyBitRate, &brate)) {
711        ALOGV("track of type '%s' does not publish bitrate", mime);
712     }
713    info.bit_rate = brate;
714
715
716    info.stream_type = streamType;
717    info.has_video = hasVideo;
718    info.is_streaming = isStreaming;
719
720    // Check if offload is possible for given format, stream type, sample rate,
721    // bit rate, duration, video and streaming
722    return AudioSystem::isOffloadSupported(info);
723}
724
725AString uriDebugString(const AString &uri, bool incognito) {
726    if (incognito) {
727        return AString("<URI suppressed>");
728    }
729
730    char prop[PROPERTY_VALUE_MAX];
731    if (property_get("media.stagefright.log-uri", prop, "false") &&
732        (!strcmp(prop, "1") || !strcmp(prop, "true"))) {
733        return uri;
734    }
735
736    // find scheme
737    AString scheme;
738    const char *chars = uri.c_str();
739    for (size_t i = 0; i < uri.size(); i++) {
740        const char c = chars[i];
741        if (!isascii(c)) {
742            break;
743        } else if (isalpha(c)) {
744            continue;
745        } else if (i == 0) {
746            // first character must be a letter
747            break;
748        } else if (isdigit(c) || c == '+' || c == '.' || c =='-') {
749            continue;
750        } else if (c != ':') {
751            break;
752        }
753        scheme = AString(uri, 0, i);
754        scheme.append("://<suppressed>");
755        return scheme;
756    }
757    return AString("<no-scheme URI suppressed>");
758}
759
760}  // namespace android
761
762