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 HTTP_DATASOURCE_H_ 18 19#define HTTP_DATASOURCE_H_ 20 21#include <media/stagefright/DataSource.h> 22#include <utils/String8.h> 23#include <utils/threads.h> 24 25namespace android { 26 27class HTTPStream; 28 29class HTTPDataSource : public DataSource { 30public: 31 HTTPDataSource( 32 const char *host, int port, const char *path, 33 const KeyedVector<String8, String8> *headers = NULL); 34 35 HTTPDataSource( 36 const char *uri, 37 const KeyedVector<String8, String8> *headers = NULL); 38 39 status_t connect(); 40 void disconnect(); 41 42 virtual status_t initCheck() const; 43 44 virtual ssize_t readAt(off_t offset, void *data, size_t size); 45 46 virtual status_t getSize(off_t *size); 47 48 virtual uint32_t flags(); 49 50protected: 51 virtual ~HTTPDataSource(); 52 53private: 54 enum { 55 kBufferSize = 64 * 1024, 56 57 // If we encounter a socket-read error we'll try reconnecting 58 // and restarting the read for at most this many times. 59 kMaxNumRetries = 3, 60 }; 61 62 enum State { 63 DISCONNECTED, 64 CONNECTING, 65 CONNECTED 66 }; 67 68 State mState; 69 mutable Mutex mStateLock; 70 71 String8 mHeaders; 72 73 String8 mStartingHost; 74 String8 mStartingPath; 75 int mStartingPort; 76 77 HTTPStream *mHttp; 78 79 void *mBuffer; 80 size_t mBufferLength; 81 off_t mBufferOffset; 82 83 bool mContentLengthValid; 84 unsigned long long mContentLength; 85 86 int32_t mNumRetriesLeft; 87 88 void init(const KeyedVector<String8, String8> *headers); 89 90 ssize_t sendRangeRequest(size_t offset); 91 void initHeaders(const KeyedVector<String8, String8> *overrides); 92 93 status_t connectWithRedirectsAndRange(off_t rangeStart); 94 void applyTimeoutResponse(); 95 96 HTTPDataSource(const HTTPDataSource &); 97 HTTPDataSource &operator=(const HTTPDataSource &); 98}; 99 100} // namespace android 101 102#endif // HTTP_DATASOURCE_H_ 103 104