Home | History | Annotate | Download | only in media
      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 #ifndef DATA_SOURCE_H_
     18 
     19 #define DATA_SOURCE_H_
     20 
     21 #include <sys/types.h>
     22 #include <media/stagefright/MediaErrors.h>
     23 #include <media/DataSourceBase.h>
     24 #include <media/IDataSource.h>
     25 #include <media/MediaExtractorPluginApi.h>
     26 #include <utils/Errors.h>
     27 #include <utils/RefBase.h>
     28 #include <utils/threads.h>
     29 #include <drm/DrmManagerClient.h>
     30 
     31 
     32 namespace android {
     33 
     34 class String8;
     35 
     36 class DataSource : public DataSourceBase, public virtual RefBase {
     37 public:
     38     DataSource() : mWrapper(NULL) {}
     39 
     40     // returns a pointer to IDataSource if it is wrapped.
     41     virtual sp<IDataSource> getIDataSource() const {
     42         return nullptr;
     43     }
     44 
     45     virtual String8 toString() {
     46         return String8("<unspecified>");
     47     }
     48 
     49     virtual status_t reconnectAtOffset(off64_t /*offset*/) {
     50         return ERROR_UNSUPPORTED;
     51     }
     52 
     53     ////////////////////////////////////////////////////////////////////////////
     54 
     55     // for DRM
     56     virtual sp<DecryptHandle> DrmInitialization(const char * /*mime*/ = NULL) {
     57         return NULL;
     58     }
     59 
     60     virtual String8 getUri() {
     61         return String8();
     62     }
     63 
     64     virtual bool getUri(char *uriString, size_t bufferSize) final {
     65         int ret = snprintf(uriString, bufferSize, "%s", getUri().c_str());
     66         return ret >= 0 && static_cast<size_t>(ret) < bufferSize;
     67     }
     68 
     69     virtual String8 getMIMEType() const {
     70         return String8("application/octet-stream");
     71     }
     72 
     73     CDataSource *wrap() {
     74         if (mWrapper) {
     75             return mWrapper;
     76         }
     77         mWrapper = new CDataSource();
     78         mWrapper->handle = this;
     79 
     80         mWrapper->readAt = [](void *handle, off64_t offset, void *data, size_t size) -> ssize_t {
     81             return ((DataSource*)handle)->readAt(offset, data, size);
     82         };
     83         mWrapper->getSize = [](void *handle, off64_t *size) -> status_t {
     84             return ((DataSource*)handle)->getSize(size);
     85         };
     86         mWrapper->flags = [](void *handle) -> uint32_t {
     87             return ((DataSource*)handle)->flags();
     88         };
     89         mWrapper->getUri = [](void *handle, char *uriString, size_t bufferSize) -> bool {
     90             return ((DataSource*)handle)->getUri(uriString, bufferSize);
     91         };
     92         return mWrapper;
     93     }
     94 
     95 protected:
     96     virtual ~DataSource() {
     97         delete mWrapper;
     98     }
     99 
    100 private:
    101     CDataSource *mWrapper;
    102     DataSource(const DataSource &);
    103     DataSource &operator=(const DataSource &);
    104 };
    105 
    106 }  // namespace android
    107 
    108 #endif  // DATA_SOURCE_H_
    109