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     const 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             i--;
     47             if (encoded.c_str()[i] == '\r' || encoded.c_str()[i] == '\n') {
     48                 encoded.erase(i, 1);
     49             }
     50         }
     51 
     52         buffer = decodeBase64(encoded);
     53 
     54         if (buffer == NULL) {
     55             ALOGE("Malformed base64 encoded content found.");
     56             return NULL;
     57         }
     58     } else {
     59 #if 0
     60         size_t dataLen = strlen(uri) - tmp.size() - 6;
     61         buffer = new ABuffer(dataLen);
     62         memcpy(buffer->data(), commaPos + 1, dataLen);
     63 
     64         // unescape
     65 #else
     66         // MediaPlayer doesn't care for this right now as we don't
     67         // play any text-based media.
     68         return NULL;
     69 #endif
     70     }
     71 
     72     // We don't really care about charset or mime type.
     73 
     74     return new DataURISource(buffer);
     75 }
     76 
     77 DataURISource::DataURISource(const sp<ABuffer> &buffer)
     78     : mBuffer(buffer) {
     79 }
     80 
     81 DataURISource::~DataURISource() {
     82 }
     83 
     84 status_t DataURISource::initCheck() const {
     85     return OK;
     86 }
     87 
     88 ssize_t DataURISource::readAt(off64_t offset, void *data, size_t size) {
     89     if ((offset < 0) || (offset >= (off64_t)mBuffer->size())) {
     90         return 0;
     91     }
     92 
     93     size_t copy = mBuffer->size() - offset;
     94     if (copy > size) {
     95         copy = size;
     96     }
     97 
     98     memcpy(data, mBuffer->data() + offset, copy);
     99 
    100     return copy;
    101 }
    102 
    103 status_t DataURISource::getSize(off64_t *size) {
    104     *size = mBuffer->size();
    105 
    106     return OK;
    107 }
    108 
    109 }  // namespace android
    110 
    111