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