Home | History | Annotate | Download | only in libstagefright
      1 /*
      2  * Copyright (C) 2014 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 #include <media/stagefright/DataURISource.h>
     17 
     18 #include <media/stagefright/foundation/ABuffer.h>
     19 #include <media/stagefright/foundation/AString.h>
     20 #include <media/stagefright/foundation/base64.h>
     21 
     22 namespace android {
     23 
     24 // static
     25 sp<DataURISource> DataURISource::Create(const char *uri) {
     26     if (strncasecmp("data:", uri, 5)) {
     27         return NULL;
     28     }
     29 
     30     const char *commaPos = strrchr(uri, ',');
     31 
     32     if (commaPos == NULL) {
     33         return NULL;
     34     }
     35 
     36     sp<ABuffer> buffer;
     37 
     38     AString tmp(&uri[5], commaPos - &uri[5]);
     39 
     40     if (tmp.endsWith(";base64")) {
     41 
     42         // strip all CR and LF characters.
     43         const char *src = commaPos+1;
     44         int len = strlen(src) + 1;
     45         char *cleansed = (char *) malloc(len);
     46         if (cleansed == NULL) return NULL;
     47         char *keeping = cleansed;
     48         int left = len;
     49         for (int i = 0; i < len ; i++)
     50         {
     51             const char c = *src++;
     52             if (c == '\r' || c == '\n') {
     53                 continue;
     54             }
     55             *keeping++ = c;
     56             left--;
     57         }
     58         memset(keeping, 0, left);
     59 
     60         AString encoded(cleansed);
     61         free(cleansed);
     62 
     63         buffer = decodeBase64(encoded);
     64 
     65         if (buffer == NULL) {
     66             ALOGE("Malformed base64 encoded content found.");
     67             return NULL;
     68         }
     69     } else {
     70 #if 0
     71         size_t dataLen = strlen(uri) - tmp.size() - 6;
     72         buffer = new ABuffer(dataLen);
     73         memcpy(buffer->data(), commaPos + 1, dataLen);
     74 
     75         // unescape
     76 #else
     77         // MediaPlayer doesn't care for this right now as we don't
     78         // play any text-based media.
     79         return NULL;
     80 #endif
     81     }
     82 
     83     // We don't really care about charset or mime type.
     84 
     85     return new DataURISource(buffer);
     86 }
     87 
     88 DataURISource::DataURISource(const sp<ABuffer> &buffer)
     89     : mBuffer(buffer) {
     90 }
     91 
     92 DataURISource::~DataURISource() {
     93 }
     94 
     95 status_t DataURISource::initCheck() const {
     96     return OK;
     97 }
     98 
     99 ssize_t DataURISource::readAt(off64_t offset, void *data, size_t size) {
    100     if ((offset < 0) || (offset >= (off64_t)mBuffer->size())) {
    101         return 0;
    102     }
    103 
    104     size_t copy = mBuffer->size() - offset;
    105     if (copy > size) {
    106         copy = size;
    107     }
    108 
    109     memcpy(data, mBuffer->data() + offset, copy);
    110 
    111     return copy;
    112 }
    113 
    114 status_t DataURISource::getSize(off64_t *size) {
    115     *size = mBuffer->size();
    116 
    117     return OK;
    118 }
    119 
    120 }  // namespace android
    121 
    122