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