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