1 /* 2 * Copyright 2015 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 ANDROID_CALLBACKDATASOURCE_H 18 #define ANDROID_CALLBACKDATASOURCE_H 19 20 #include <media/stagefright/DataSource.h> 21 #include <media/stagefright/foundation/ADebug.h> 22 23 namespace android { 24 25 class IDataSource; 26 class IMemory; 27 28 // A stagefright DataSource that wraps a binder IDataSource. It's a "Callback" 29 // DataSource because it calls back to the IDataSource for data. 30 class CallbackDataSource : public DataSource { 31 public: 32 CallbackDataSource(const sp<IDataSource>& iDataSource); 33 virtual ~CallbackDataSource(); 34 35 // DataSource implementation. 36 virtual status_t initCheck() const; 37 virtual ssize_t readAt(off64_t offset, void *data, size_t size); 38 virtual status_t getSize(off64_t *size); 39 virtual uint32_t flags(); 40 virtual void close(); toString()41 virtual String8 toString() { 42 return mName; 43 } 44 virtual sp<DecryptHandle> DrmInitialization(const char *mime = NULL); 45 46 private: 47 sp<IDataSource> mIDataSource; 48 sp<IMemory> mMemory; 49 bool mIsClosed; 50 String8 mName; 51 52 DISALLOW_EVIL_CONSTRUCTORS(CallbackDataSource); 53 }; 54 55 56 // A caching DataSource that wraps a CallbackDataSource. For reads smaller 57 // than kCacheSize it will read up to kCacheSize ahead and cache it. 58 // This reduces the number of binder round trips to the IDataSource and has a significant 59 // impact on time taken for filetype sniffing and metadata extraction. 60 class TinyCacheSource : public DataSource { 61 public: 62 TinyCacheSource(const sp<DataSource>& source); 63 64 virtual status_t initCheck() const; 65 virtual ssize_t readAt(off64_t offset, void* data, size_t size); 66 virtual status_t getSize(off64_t* size); 67 virtual uint32_t flags(); close()68 virtual void close() { mSource->close(); } toString()69 virtual String8 toString() { 70 return mName; 71 } 72 virtual sp<DecryptHandle> DrmInitialization(const char *mime = NULL); 73 74 private: 75 // 2kb comes from experimenting with the time-to-first-frame from a MediaPlayer 76 // with an in-memory MediaDataSource source on a Nexus 5. Beyond 2kb there was 77 // no improvement. 78 enum { 79 kCacheSize = 2048, 80 }; 81 82 sp<DataSource> mSource; 83 uint8_t mCache[kCacheSize]; 84 off64_t mCachedOffset; 85 size_t mCachedSize; 86 String8 mName; 87 88 DISALLOW_EVIL_CONSTRUCTORS(TinyCacheSource); 89 }; 90 91 }; // namespace android 92 93 #endif // ANDROID_CALLBACKDATASOURCE_H 94