summaryrefslogtreecommitdiffstats
path: root/media/libmedia/IMediaHTTPConnection.cpp
blob: 23fa084d3938a3574d945ef64f7ecf711a211117 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
/*
 * Copyright (C) 2013 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

//#define LOG_NDEBUG 0
#define LOG_TAG "IMediaHTTPConnection"
#include <utils/Log.h>

#include <media/IMediaHTTPConnection.h>

#include <binder/IMemory.h>
#include <binder/Parcel.h>
#include <utils/String8.h>
#include <media/stagefright/foundation/ADebug.h>
#include <media/stagefright/MediaErrors.h>

namespace android {

enum {
    CONNECT = IBinder::FIRST_CALL_TRANSACTION,
    DISCONNECT,
    READ_AT,
    GET_SIZE,
    GET_MIME_TYPE,
    GET_URI
};

struct BpMediaHTTPConnection : public BpInterface<IMediaHTTPConnection> {
    BpMediaHTTPConnection(const sp<IBinder> &impl)
        : BpInterface<IMediaHTTPConnection>(impl) {
    }

    virtual bool connect(
            const char *uri, const KeyedVector<String8, String8> *headers) {
        Parcel data, reply;
        data.writeInterfaceToken(
                IMediaHTTPConnection::getInterfaceDescriptor());

        String16 tmp(uri);
        data.writeString16(tmp);

        tmp = String16("");
        if (headers != NULL) {
            for (size_t i = 0; i < headers->size(); ++i) {
                String16 key(headers->keyAt(i).string());
                String16 val(headers->valueAt(i).string());

                tmp.append(key);
                tmp.append(String16(": "));
                tmp.append(val);
                tmp.append(String16("\r\n"));
            }
        }
        data.writeString16(tmp);

        remote()->transact(CONNECT, data, &reply);

        int32_t exceptionCode = reply.readExceptionCode();

        if (exceptionCode) {
            return false;
        }

        sp<IBinder> binder = reply.readStrongBinder();
        mMemory = interface_cast<IMemory>(binder);

        return mMemory != NULL;
    }

    virtual void disconnect() {
        Parcel data, reply;
        data.writeInterfaceToken(
                IMediaHTTPConnection::getInterfaceDescriptor());

        remote()->transact(DISCONNECT, data, &reply);
    }

    virtual ssize_t readAt(off64_t offset, void *buffer, size_t size) {
        Parcel data, reply;
        data.writeInterfaceToken(
                IMediaHTTPConnection::getInterfaceDescriptor());

        data.writeInt64(offset);
        data.writeInt32(size);

        status_t err = remote()->transact(READ_AT, data, &reply);
        if (err != OK) {
            ALOGE("remote readAt failed");
            return UNKNOWN_ERROR;
        }

        int32_t exceptionCode = reply.readExceptionCode();

        if (exceptionCode) {
            return UNKNOWN_ERROR;
        }

        int32_t lenOrErrorCode = reply.readInt32();

        // Negative values are error codes
        if (lenOrErrorCode < 0) {
            return lenOrErrorCode;
        }

        size_t len = lenOrErrorCode;

        if (len > size) {
            ALOGE("requested %zu, got %zu", size, len);
            return ERROR_OUT_OF_RANGE;
        }
        if (len > mMemory->size()) {
            ALOGE("got %zu, but memory has %zu", len, mMemory->size());
            return ERROR_OUT_OF_RANGE;
        }
        if(buffer == NULL) {
           ALOGE("readAt got a NULL buffer");
           return UNKNOWN_ERROR;
        }
        if (mMemory->pointer() == NULL) {
           ALOGE("readAt got a NULL mMemory->pointer()");
           return UNKNOWN_ERROR;
        }

        memcpy(buffer, mMemory->pointer(), len);

        return len;
    }

    virtual off64_t getSize() {
        Parcel data, reply;
        data.writeInterfaceToken(
                IMediaHTTPConnection::getInterfaceDescriptor());

        remote()->transact(GET_SIZE, data, &reply);

        int32_t exceptionCode = reply.readExceptionCode();

        if (exceptionCode) {
            return UNKNOWN_ERROR;
        }

        return reply.readInt64();
    }

    virtual status_t getMIMEType(String8 *mimeType) {
        *mimeType = String8("");

        Parcel data, reply;
        data.writeInterfaceToken(
                IMediaHTTPConnection::getInterfaceDescriptor());

        remote()->transact(GET_MIME_TYPE, data, &reply);

        int32_t exceptionCode = reply.readExceptionCode();

        if (exceptionCode) {
            return UNKNOWN_ERROR;
        }

        *mimeType = String8(reply.readString16());

        return OK;
    }

    virtual status_t getUri(String8 *uri) {
        *uri = String8("");

        Parcel data, reply;
        data.writeInterfaceToken(
                IMediaHTTPConnection::getInterfaceDescriptor());

        remote()->transact(GET_URI, data, &reply);

        int32_t exceptionCode = reply.readExceptionCode();

        if (exceptionCode) {
            return UNKNOWN_ERROR;
        }

        *uri = String8(reply.readString16());

        return OK;
    }

private:
    sp<IMemory> mMemory;
};

IMPLEMENT_META_INTERFACE(
        MediaHTTPConnection, "android.media.IMediaHTTPConnection");

} // namespace android