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