IMediaHTTPConnection.cpp revision c9c7e25a4b1c34439fb365f6cfbef063884566cc
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
28namespace android {
29
30enum {
31    CONNECT = IBinder::FIRST_CALL_TRANSACTION,
32    DISCONNECT,
33    READ_AT,
34    GET_SIZE,
35    GET_MIME_TYPE,
36    GET_URI
37};
38
39struct 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        CHECK_EQ(err, (status_t)OK);
99
100        int32_t exceptionCode = reply.readExceptionCode();
101
102        if (exceptionCode) {
103            return UNKNOWN_ERROR;
104        }
105
106        int32_t len = reply.readInt32();
107
108        if (len > 0) {
109            memcpy(buffer, mMemory->pointer(), len);
110        }
111
112        return len;
113    }
114
115    virtual off64_t getSize() {
116        Parcel data, reply;
117        data.writeInterfaceToken(
118                IMediaHTTPConnection::getInterfaceDescriptor());
119
120        remote()->transact(GET_SIZE, data, &reply);
121
122        int32_t exceptionCode = reply.readExceptionCode();
123
124        if (exceptionCode) {
125            return UNKNOWN_ERROR;
126        }
127
128        return reply.readInt64();
129    }
130
131    virtual status_t getMIMEType(String8 *mimeType) {
132        *mimeType = String8("");
133
134        Parcel data, reply;
135        data.writeInterfaceToken(
136                IMediaHTTPConnection::getInterfaceDescriptor());
137
138        remote()->transact(GET_MIME_TYPE, data, &reply);
139
140        int32_t exceptionCode = reply.readExceptionCode();
141
142        if (exceptionCode) {
143            return UNKNOWN_ERROR;
144        }
145
146        *mimeType = String8(reply.readString16());
147
148        return OK;
149    }
150
151    virtual status_t getUri(String8 *uri) {
152        *uri = String8("");
153
154        Parcel data, reply;
155        data.writeInterfaceToken(
156                IMediaHTTPConnection::getInterfaceDescriptor());
157
158        remote()->transact(GET_URI, data, &reply);
159
160        int32_t exceptionCode = reply.readExceptionCode();
161
162        if (exceptionCode) {
163            return UNKNOWN_ERROR;
164        }
165
166        *uri = String8(reply.readString16());
167
168        return OK;
169    }
170
171private:
172    sp<IMemory> mMemory;
173};
174
175IMPLEMENT_META_INTERFACE(
176        MediaHTTPConnection, "android.media.IMediaHTTPConnection");
177
178}  // namespace android
179
180