11b86fe063badb5f28c467ade39be0f4008688947Andreas Huber/*
21b86fe063badb5f28c467ade39be0f4008688947Andreas Huber * Copyright (C) 2013 The Android Open Source Project
31b86fe063badb5f28c467ade39be0f4008688947Andreas Huber *
41b86fe063badb5f28c467ade39be0f4008688947Andreas Huber * Licensed under the Apache License, Version 2.0 (the "License");
51b86fe063badb5f28c467ade39be0f4008688947Andreas Huber * you may not use this file except in compliance with the License.
61b86fe063badb5f28c467ade39be0f4008688947Andreas Huber * You may obtain a copy of the License at
71b86fe063badb5f28c467ade39be0f4008688947Andreas Huber *
81b86fe063badb5f28c467ade39be0f4008688947Andreas Huber *      http://www.apache.org/licenses/LICENSE-2.0
91b86fe063badb5f28c467ade39be0f4008688947Andreas Huber *
101b86fe063badb5f28c467ade39be0f4008688947Andreas Huber * Unless required by applicable law or agreed to in writing, software
111b86fe063badb5f28c467ade39be0f4008688947Andreas Huber * distributed under the License is distributed on an "AS IS" BASIS,
121b86fe063badb5f28c467ade39be0f4008688947Andreas Huber * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
131b86fe063badb5f28c467ade39be0f4008688947Andreas Huber * See the License for the specific language governing permissions and
141b86fe063badb5f28c467ade39be0f4008688947Andreas Huber * limitations under the License.
151b86fe063badb5f28c467ade39be0f4008688947Andreas Huber */
161b86fe063badb5f28c467ade39be0f4008688947Andreas Huber
171b86fe063badb5f28c467ade39be0f4008688947Andreas Huber//#define LOG_NDEBUG 0
181b86fe063badb5f28c467ade39be0f4008688947Andreas Huber#define LOG_TAG "IMediaHTTPConnection"
191b86fe063badb5f28c467ade39be0f4008688947Andreas Huber#include <utils/Log.h>
201b86fe063badb5f28c467ade39be0f4008688947Andreas Huber
211b86fe063badb5f28c467ade39be0f4008688947Andreas Huber#include <media/IMediaHTTPConnection.h>
221b86fe063badb5f28c467ade39be0f4008688947Andreas Huber
231b86fe063badb5f28c467ade39be0f4008688947Andreas Huber#include <binder/IMemory.h>
241b86fe063badb5f28c467ade39be0f4008688947Andreas Huber#include <binder/Parcel.h>
251b86fe063badb5f28c467ade39be0f4008688947Andreas Huber#include <utils/String8.h>
261b86fe063badb5f28c467ade39be0f4008688947Andreas Huber#include <media/stagefright/foundation/ADebug.h>
271b86fe063badb5f28c467ade39be0f4008688947Andreas Huber
281b86fe063badb5f28c467ade39be0f4008688947Andreas Hubernamespace android {
291b86fe063badb5f28c467ade39be0f4008688947Andreas Huber
301b86fe063badb5f28c467ade39be0f4008688947Andreas Huberenum {
311b86fe063badb5f28c467ade39be0f4008688947Andreas Huber    CONNECT = IBinder::FIRST_CALL_TRANSACTION,
321b86fe063badb5f28c467ade39be0f4008688947Andreas Huber    DISCONNECT,
331b86fe063badb5f28c467ade39be0f4008688947Andreas Huber    READ_AT,
341b86fe063badb5f28c467ade39be0f4008688947Andreas Huber    GET_SIZE,
351b86fe063badb5f28c467ade39be0f4008688947Andreas Huber    GET_MIME_TYPE,
36c9c7e25a4b1c34439fb365f6cfbef063884566ccMarco Nelissen    GET_URI
371b86fe063badb5f28c467ade39be0f4008688947Andreas Huber};
381b86fe063badb5f28c467ade39be0f4008688947Andreas Huber
391b86fe063badb5f28c467ade39be0f4008688947Andreas Huberstruct BpMediaHTTPConnection : public BpInterface<IMediaHTTPConnection> {
401b86fe063badb5f28c467ade39be0f4008688947Andreas Huber    BpMediaHTTPConnection(const sp<IBinder> &impl)
411b86fe063badb5f28c467ade39be0f4008688947Andreas Huber        : BpInterface<IMediaHTTPConnection>(impl) {
421b86fe063badb5f28c467ade39be0f4008688947Andreas Huber    }
431b86fe063badb5f28c467ade39be0f4008688947Andreas Huber
441b86fe063badb5f28c467ade39be0f4008688947Andreas Huber    virtual bool connect(
451b86fe063badb5f28c467ade39be0f4008688947Andreas Huber            const char *uri, const KeyedVector<String8, String8> *headers) {
461b86fe063badb5f28c467ade39be0f4008688947Andreas Huber        Parcel data, reply;
471b86fe063badb5f28c467ade39be0f4008688947Andreas Huber        data.writeInterfaceToken(
481b86fe063badb5f28c467ade39be0f4008688947Andreas Huber                IMediaHTTPConnection::getInterfaceDescriptor());
491b86fe063badb5f28c467ade39be0f4008688947Andreas Huber
501b86fe063badb5f28c467ade39be0f4008688947Andreas Huber        String16 tmp(uri);
511b86fe063badb5f28c467ade39be0f4008688947Andreas Huber        data.writeString16(tmp);
521b86fe063badb5f28c467ade39be0f4008688947Andreas Huber
531b86fe063badb5f28c467ade39be0f4008688947Andreas Huber        tmp = String16("");
541b86fe063badb5f28c467ade39be0f4008688947Andreas Huber        if (headers != NULL) {
551b86fe063badb5f28c467ade39be0f4008688947Andreas Huber            for (size_t i = 0; i < headers->size(); ++i) {
561b86fe063badb5f28c467ade39be0f4008688947Andreas Huber                String16 key(headers->keyAt(i).string());
571b86fe063badb5f28c467ade39be0f4008688947Andreas Huber                String16 val(headers->valueAt(i).string());
581b86fe063badb5f28c467ade39be0f4008688947Andreas Huber
591b86fe063badb5f28c467ade39be0f4008688947Andreas Huber                tmp.append(key);
601b86fe063badb5f28c467ade39be0f4008688947Andreas Huber                tmp.append(String16(": "));
611b86fe063badb5f28c467ade39be0f4008688947Andreas Huber                tmp.append(val);
621b86fe063badb5f28c467ade39be0f4008688947Andreas Huber                tmp.append(String16("\r\n"));
631b86fe063badb5f28c467ade39be0f4008688947Andreas Huber            }
641b86fe063badb5f28c467ade39be0f4008688947Andreas Huber        }
651b86fe063badb5f28c467ade39be0f4008688947Andreas Huber        data.writeString16(tmp);
661b86fe063badb5f28c467ade39be0f4008688947Andreas Huber
671b86fe063badb5f28c467ade39be0f4008688947Andreas Huber        remote()->transact(CONNECT, data, &reply);
681b86fe063badb5f28c467ade39be0f4008688947Andreas Huber
691b86fe063badb5f28c467ade39be0f4008688947Andreas Huber        int32_t exceptionCode = reply.readExceptionCode();
701b86fe063badb5f28c467ade39be0f4008688947Andreas Huber
711b86fe063badb5f28c467ade39be0f4008688947Andreas Huber        if (exceptionCode) {
721b86fe063badb5f28c467ade39be0f4008688947Andreas Huber            return UNKNOWN_ERROR;
731b86fe063badb5f28c467ade39be0f4008688947Andreas Huber        }
741b86fe063badb5f28c467ade39be0f4008688947Andreas Huber
751b86fe063badb5f28c467ade39be0f4008688947Andreas Huber        sp<IBinder> binder = reply.readStrongBinder();
761b86fe063badb5f28c467ade39be0f4008688947Andreas Huber        mMemory = interface_cast<IMemory>(binder);
771b86fe063badb5f28c467ade39be0f4008688947Andreas Huber
781b86fe063badb5f28c467ade39be0f4008688947Andreas Huber        return mMemory != NULL;
791b86fe063badb5f28c467ade39be0f4008688947Andreas Huber    }
801b86fe063badb5f28c467ade39be0f4008688947Andreas Huber
811b86fe063badb5f28c467ade39be0f4008688947Andreas Huber    virtual void disconnect() {
821b86fe063badb5f28c467ade39be0f4008688947Andreas Huber        Parcel data, reply;
831b86fe063badb5f28c467ade39be0f4008688947Andreas Huber        data.writeInterfaceToken(
841b86fe063badb5f28c467ade39be0f4008688947Andreas Huber                IMediaHTTPConnection::getInterfaceDescriptor());
851b86fe063badb5f28c467ade39be0f4008688947Andreas Huber
861b86fe063badb5f28c467ade39be0f4008688947Andreas Huber        remote()->transact(DISCONNECT, data, &reply);
871b86fe063badb5f28c467ade39be0f4008688947Andreas Huber    }
881b86fe063badb5f28c467ade39be0f4008688947Andreas Huber
891b86fe063badb5f28c467ade39be0f4008688947Andreas Huber    virtual ssize_t readAt(off64_t offset, void *buffer, size_t size) {
901b86fe063badb5f28c467ade39be0f4008688947Andreas Huber        Parcel data, reply;
911b86fe063badb5f28c467ade39be0f4008688947Andreas Huber        data.writeInterfaceToken(
921b86fe063badb5f28c467ade39be0f4008688947Andreas Huber                IMediaHTTPConnection::getInterfaceDescriptor());
931b86fe063badb5f28c467ade39be0f4008688947Andreas Huber
941b86fe063badb5f28c467ade39be0f4008688947Andreas Huber        data.writeInt64(offset);
951b86fe063badb5f28c467ade39be0f4008688947Andreas Huber        data.writeInt32(size);
961b86fe063badb5f28c467ade39be0f4008688947Andreas Huber
971b86fe063badb5f28c467ade39be0f4008688947Andreas Huber        status_t err = remote()->transact(READ_AT, data, &reply);
9819a9fef6d2970a615f4f33025f9141e44b7c9f34Marco Nelissen        if (err != OK) {
9919a9fef6d2970a615f4f33025f9141e44b7c9f34Marco Nelissen            ALOGE("remote readAt failed");
10019a9fef6d2970a615f4f33025f9141e44b7c9f34Marco Nelissen            return UNKNOWN_ERROR;
10119a9fef6d2970a615f4f33025f9141e44b7c9f34Marco Nelissen        }
1021b86fe063badb5f28c467ade39be0f4008688947Andreas Huber
1031b86fe063badb5f28c467ade39be0f4008688947Andreas Huber        int32_t exceptionCode = reply.readExceptionCode();
1041b86fe063badb5f28c467ade39be0f4008688947Andreas Huber
1051b86fe063badb5f28c467ade39be0f4008688947Andreas Huber        if (exceptionCode) {
1061b86fe063badb5f28c467ade39be0f4008688947Andreas Huber            return UNKNOWN_ERROR;
1071b86fe063badb5f28c467ade39be0f4008688947Andreas Huber        }
1081b86fe063badb5f28c467ade39be0f4008688947Andreas Huber
1091b86fe063badb5f28c467ade39be0f4008688947Andreas Huber        int32_t len = reply.readInt32();
1101b86fe063badb5f28c467ade39be0f4008688947Andreas Huber
1111b86fe063badb5f28c467ade39be0f4008688947Andreas Huber        if (len > 0) {
1121b86fe063badb5f28c467ade39be0f4008688947Andreas Huber            memcpy(buffer, mMemory->pointer(), len);
1131b86fe063badb5f28c467ade39be0f4008688947Andreas Huber        }
1141b86fe063badb5f28c467ade39be0f4008688947Andreas Huber
1151b86fe063badb5f28c467ade39be0f4008688947Andreas Huber        return len;
1161b86fe063badb5f28c467ade39be0f4008688947Andreas Huber    }
1171b86fe063badb5f28c467ade39be0f4008688947Andreas Huber
1181b86fe063badb5f28c467ade39be0f4008688947Andreas Huber    virtual off64_t getSize() {
1191b86fe063badb5f28c467ade39be0f4008688947Andreas Huber        Parcel data, reply;
1201b86fe063badb5f28c467ade39be0f4008688947Andreas Huber        data.writeInterfaceToken(
1211b86fe063badb5f28c467ade39be0f4008688947Andreas Huber                IMediaHTTPConnection::getInterfaceDescriptor());
1221b86fe063badb5f28c467ade39be0f4008688947Andreas Huber
1231b86fe063badb5f28c467ade39be0f4008688947Andreas Huber        remote()->transact(GET_SIZE, data, &reply);
1241b86fe063badb5f28c467ade39be0f4008688947Andreas Huber
1251b86fe063badb5f28c467ade39be0f4008688947Andreas Huber        int32_t exceptionCode = reply.readExceptionCode();
1261b86fe063badb5f28c467ade39be0f4008688947Andreas Huber
1271b86fe063badb5f28c467ade39be0f4008688947Andreas Huber        if (exceptionCode) {
1281b86fe063badb5f28c467ade39be0f4008688947Andreas Huber            return UNKNOWN_ERROR;
1291b86fe063badb5f28c467ade39be0f4008688947Andreas Huber        }
1301b86fe063badb5f28c467ade39be0f4008688947Andreas Huber
1311b86fe063badb5f28c467ade39be0f4008688947Andreas Huber        return reply.readInt64();
1321b86fe063badb5f28c467ade39be0f4008688947Andreas Huber    }
1331b86fe063badb5f28c467ade39be0f4008688947Andreas Huber
1341b86fe063badb5f28c467ade39be0f4008688947Andreas Huber    virtual status_t getMIMEType(String8 *mimeType) {
1351b86fe063badb5f28c467ade39be0f4008688947Andreas Huber        *mimeType = String8("");
1361b86fe063badb5f28c467ade39be0f4008688947Andreas Huber
1371b86fe063badb5f28c467ade39be0f4008688947Andreas Huber        Parcel data, reply;
1381b86fe063badb5f28c467ade39be0f4008688947Andreas Huber        data.writeInterfaceToken(
1391b86fe063badb5f28c467ade39be0f4008688947Andreas Huber                IMediaHTTPConnection::getInterfaceDescriptor());
1401b86fe063badb5f28c467ade39be0f4008688947Andreas Huber
1411b86fe063badb5f28c467ade39be0f4008688947Andreas Huber        remote()->transact(GET_MIME_TYPE, data, &reply);
1421b86fe063badb5f28c467ade39be0f4008688947Andreas Huber
1431b86fe063badb5f28c467ade39be0f4008688947Andreas Huber        int32_t exceptionCode = reply.readExceptionCode();
1441b86fe063badb5f28c467ade39be0f4008688947Andreas Huber
1451b86fe063badb5f28c467ade39be0f4008688947Andreas Huber        if (exceptionCode) {
1461b86fe063badb5f28c467ade39be0f4008688947Andreas Huber            return UNKNOWN_ERROR;
1471b86fe063badb5f28c467ade39be0f4008688947Andreas Huber        }
1481b86fe063badb5f28c467ade39be0f4008688947Andreas Huber
1491b86fe063badb5f28c467ade39be0f4008688947Andreas Huber        *mimeType = String8(reply.readString16());
1501b86fe063badb5f28c467ade39be0f4008688947Andreas Huber
1511b86fe063badb5f28c467ade39be0f4008688947Andreas Huber        return OK;
1521b86fe063badb5f28c467ade39be0f4008688947Andreas Huber    }
1531b86fe063badb5f28c467ade39be0f4008688947Andreas Huber
154c9c7e25a4b1c34439fb365f6cfbef063884566ccMarco Nelissen    virtual status_t getUri(String8 *uri) {
155c9c7e25a4b1c34439fb365f6cfbef063884566ccMarco Nelissen        *uri = String8("");
156c9c7e25a4b1c34439fb365f6cfbef063884566ccMarco Nelissen
157c9c7e25a4b1c34439fb365f6cfbef063884566ccMarco Nelissen        Parcel data, reply;
158c9c7e25a4b1c34439fb365f6cfbef063884566ccMarco Nelissen        data.writeInterfaceToken(
159c9c7e25a4b1c34439fb365f6cfbef063884566ccMarco Nelissen                IMediaHTTPConnection::getInterfaceDescriptor());
160c9c7e25a4b1c34439fb365f6cfbef063884566ccMarco Nelissen
161c9c7e25a4b1c34439fb365f6cfbef063884566ccMarco Nelissen        remote()->transact(GET_URI, data, &reply);
162c9c7e25a4b1c34439fb365f6cfbef063884566ccMarco Nelissen
163c9c7e25a4b1c34439fb365f6cfbef063884566ccMarco Nelissen        int32_t exceptionCode = reply.readExceptionCode();
164c9c7e25a4b1c34439fb365f6cfbef063884566ccMarco Nelissen
165c9c7e25a4b1c34439fb365f6cfbef063884566ccMarco Nelissen        if (exceptionCode) {
166c9c7e25a4b1c34439fb365f6cfbef063884566ccMarco Nelissen            return UNKNOWN_ERROR;
167c9c7e25a4b1c34439fb365f6cfbef063884566ccMarco Nelissen        }
168c9c7e25a4b1c34439fb365f6cfbef063884566ccMarco Nelissen
169c9c7e25a4b1c34439fb365f6cfbef063884566ccMarco Nelissen        *uri = String8(reply.readString16());
170c9c7e25a4b1c34439fb365f6cfbef063884566ccMarco Nelissen
171c9c7e25a4b1c34439fb365f6cfbef063884566ccMarco Nelissen        return OK;
172c9c7e25a4b1c34439fb365f6cfbef063884566ccMarco Nelissen    }
173c9c7e25a4b1c34439fb365f6cfbef063884566ccMarco Nelissen
1741b86fe063badb5f28c467ade39be0f4008688947Andreas Huberprivate:
1751b86fe063badb5f28c467ade39be0f4008688947Andreas Huber    sp<IMemory> mMemory;
1761b86fe063badb5f28c467ade39be0f4008688947Andreas Huber};
1771b86fe063badb5f28c467ade39be0f4008688947Andreas Huber
1781b86fe063badb5f28c467ade39be0f4008688947Andreas HuberIMPLEMENT_META_INTERFACE(
1791b86fe063badb5f28c467ade39be0f4008688947Andreas Huber        MediaHTTPConnection, "android.media.IMediaHTTPConnection");
1801b86fe063badb5f28c467ade39be0f4008688947Andreas Huber
1811b86fe063badb5f28c467ade39be0f4008688947Andreas Huber}  // namespace android
1821b86fe063badb5f28c467ade39be0f4008688947Andreas Huber
183