1 /* 2 * Copyright (C) 2013 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 //#define LOG_NDEBUG 0 18 #define LOG_TAG "IMediaHTTPConnection" 19 #include <utils/Log.h> 20 21 #include <media/IMediaHTTPConnection.h> 22 23 #include <binder/IMemory.h> 24 #include <binder/Parcel.h> 25 #include <utils/String8.h> 26 #include <media/stagefright/foundation/ADebug.h> 27 #include <media/stagefright/MediaErrors.h> 28 29 namespace android { 30 31 enum { 32 CONNECT = IBinder::FIRST_CALL_TRANSACTION, 33 DISCONNECT, 34 READ_AT, 35 GET_SIZE, 36 GET_MIME_TYPE, 37 GET_URI 38 }; 39 40 struct BpMediaHTTPConnection : public BpInterface<IMediaHTTPConnection> { 41 BpMediaHTTPConnection(const sp<IBinder> &impl) 42 : BpInterface<IMediaHTTPConnection>(impl) { 43 } 44 45 virtual bool connect( 46 const char *uri, const KeyedVector<String8, String8> *headers) { 47 Parcel data, reply; 48 data.writeInterfaceToken( 49 IMediaHTTPConnection::getInterfaceDescriptor()); 50 51 String16 tmp(uri); 52 data.writeString16(tmp); 53 54 tmp = String16(""); 55 if (headers != NULL) { 56 for (size_t i = 0; i < headers->size(); ++i) { 57 String16 key(headers->keyAt(i).string()); 58 String16 val(headers->valueAt(i).string()); 59 60 tmp.append(key); 61 tmp.append(String16(": ")); 62 tmp.append(val); 63 tmp.append(String16("\r\n")); 64 } 65 } 66 data.writeString16(tmp); 67 68 remote()->transact(CONNECT, data, &reply); 69 70 int32_t exceptionCode = reply.readExceptionCode(); 71 72 if (exceptionCode) { 73 return UNKNOWN_ERROR; 74 } 75 76 sp<IBinder> binder = reply.readStrongBinder(); 77 mMemory = interface_cast<IMemory>(binder); 78 79 return mMemory != NULL; 80 } 81 82 virtual void disconnect() { 83 Parcel data, reply; 84 data.writeInterfaceToken( 85 IMediaHTTPConnection::getInterfaceDescriptor()); 86 87 remote()->transact(DISCONNECT, data, &reply); 88 } 89 90 virtual ssize_t readAt(off64_t offset, void *buffer, size_t size) { 91 Parcel data, reply; 92 data.writeInterfaceToken( 93 IMediaHTTPConnection::getInterfaceDescriptor()); 94 95 data.writeInt64(offset); 96 data.writeInt32(size); 97 98 status_t err = remote()->transact(READ_AT, data, &reply); 99 if (err != OK) { 100 ALOGE("remote readAt failed"); 101 return UNKNOWN_ERROR; 102 } 103 104 int32_t exceptionCode = reply.readExceptionCode(); 105 106 if (exceptionCode) { 107 return UNKNOWN_ERROR; 108 } 109 110 size_t len = reply.readInt32(); 111 112 if (len > size) { 113 ALOGE("requested %zu, got %zu", size, len); 114 return ERROR_OUT_OF_RANGE; 115 } 116 if (len > mMemory->size()) { 117 ALOGE("got %zu, but memory has %zu", len, mMemory->size()); 118 return ERROR_OUT_OF_RANGE; 119 } 120 121 memcpy(buffer, mMemory->pointer(), len); 122 123 return len; 124 } 125 126 virtual off64_t getSize() { 127 Parcel data, reply; 128 data.writeInterfaceToken( 129 IMediaHTTPConnection::getInterfaceDescriptor()); 130 131 remote()->transact(GET_SIZE, data, &reply); 132 133 int32_t exceptionCode = reply.readExceptionCode(); 134 135 if (exceptionCode) { 136 return UNKNOWN_ERROR; 137 } 138 139 return reply.readInt64(); 140 } 141 142 virtual status_t getMIMEType(String8 *mimeType) { 143 *mimeType = String8(""); 144 145 Parcel data, reply; 146 data.writeInterfaceToken( 147 IMediaHTTPConnection::getInterfaceDescriptor()); 148 149 remote()->transact(GET_MIME_TYPE, data, &reply); 150 151 int32_t exceptionCode = reply.readExceptionCode(); 152 153 if (exceptionCode) { 154 return UNKNOWN_ERROR; 155 } 156 157 *mimeType = String8(reply.readString16()); 158 159 return OK; 160 } 161 162 virtual status_t getUri(String8 *uri) { 163 *uri = String8(""); 164 165 Parcel data, reply; 166 data.writeInterfaceToken( 167 IMediaHTTPConnection::getInterfaceDescriptor()); 168 169 remote()->transact(GET_URI, data, &reply); 170 171 int32_t exceptionCode = reply.readExceptionCode(); 172 173 if (exceptionCode) { 174 return UNKNOWN_ERROR; 175 } 176 177 *uri = String8(reply.readString16()); 178 179 return OK; 180 } 181 182 private: 183 sp<IMemory> mMemory; 184 }; 185 186 IMPLEMENT_META_INTERFACE( 187 MediaHTTPConnection, "android.media.IMediaHTTPConnection"); 188 189 } // namespace android 190 191