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