summaryrefslogtreecommitdiffstats
path: root/media/libmedia/IHDCP.cpp
blob: 493f5a47e9b70ee24e957ebd7206035a85b51d89 (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
205
206
/*
 * Copyright (C) 2012 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 "IHDCP"
#include <utils/Log.h>

#include <binder/Parcel.h>
#include <media/IHDCP.h>
#include <media/stagefright/MediaErrors.h>
#include <media/stagefright/foundation/ADebug.h>

namespace android {

enum {
    OBSERVER_NOTIFY = IBinder::FIRST_CALL_TRANSACTION,
    HDCP_SET_OBSERVER,
    HDCP_INIT_ASYNC,
    HDCP_SHUTDOWN_ASYNC,
    HDCP_ENCRYPT,
};

struct BpHDCPObserver : public BpInterface<IHDCPObserver> {
    BpHDCPObserver(const sp<IBinder> &impl)
        : BpInterface<IHDCPObserver>(impl) {
    }

    virtual void notify(
            int msg, int ext1, int ext2, const Parcel *obj) {
        Parcel data, reply;
        data.writeInterfaceToken(IHDCPObserver::getInterfaceDescriptor());
        data.writeInt32(msg);
        data.writeInt32(ext1);
        data.writeInt32(ext2);
        if (obj && obj->dataSize() > 0) {
            data.appendFrom(const_cast<Parcel *>(obj), 0, obj->dataSize());
        }
        remote()->transact(OBSERVER_NOTIFY, data, &reply, IBinder::FLAG_ONEWAY);
    }
};

IMPLEMENT_META_INTERFACE(HDCPObserver, "android.hardware.IHDCPObserver");

struct BpHDCP : public BpInterface<IHDCP> {
    BpHDCP(const sp<IBinder> &impl)
        : BpInterface<IHDCP>(impl) {
    }

    virtual status_t setObserver(const sp<IHDCPObserver> &observer) {
        Parcel data, reply;
        data.writeInterfaceToken(IHDCP::getInterfaceDescriptor());
        data.writeStrongBinder(observer->asBinder());
        remote()->transact(HDCP_SET_OBSERVER, data, &reply);
        return reply.readInt32();
    }

    virtual status_t initAsync(const char *host, unsigned port) {
        Parcel data, reply;
        data.writeInterfaceToken(IHDCP::getInterfaceDescriptor());
        data.writeCString(host);
        data.writeInt32(port);
        remote()->transact(HDCP_INIT_ASYNC, data, &reply);
        return reply.readInt32();
    }

    virtual status_t shutdownAsync() {
        Parcel data, reply;
        data.writeInterfaceToken(IHDCP::getInterfaceDescriptor());
        remote()->transact(HDCP_SHUTDOWN_ASYNC, data, &reply);
        return reply.readInt32();
    }

    virtual status_t encrypt(
            const void *inData, size_t size, uint32_t streamCTR,
            uint64_t *outInputCTR, void *outData) {
        Parcel data, reply;
        data.writeInterfaceToken(IHDCP::getInterfaceDescriptor());
        data.writeInt32(size);
        data.write(inData, size);
        data.writeInt32(streamCTR);
        remote()->transact(HDCP_ENCRYPT, data, &reply);

        status_t err = reply.readInt32();

        if (err != OK) {
            *outInputCTR = 0;

            return err;
        }

        *outInputCTR = reply.readInt64();
        reply.read(outData, size);

        return err;
    }
};

IMPLEMENT_META_INTERFACE(HDCP, "android.hardware.IHDCP");

status_t BnHDCPObserver::onTransact(
        uint32_t code, const Parcel &data, Parcel *reply, uint32_t flags) {
    switch (code) {
        case OBSERVER_NOTIFY:
        {
            CHECK_INTERFACE(IHDCPObserver, data, reply);

            int msg = data.readInt32();
            int ext1 = data.readInt32();
            int ext2 = data.readInt32();

            Parcel obj;
            if (data.dataAvail() > 0) {
                obj.appendFrom(
                        const_cast<Parcel *>(&data),
                        data.dataPosition(),
                        data.dataAvail());
            }

            notify(msg, ext1, ext2, &obj);

            return OK;
        }

        default:
            return BBinder::onTransact(code, data, reply, flags);
    }
}

status_t BnHDCP::onTransact(
        uint32_t code, const Parcel &data, Parcel *reply, uint32_t flags) {
    switch (code) {
        case HDCP_SET_OBSERVER:
        {
            CHECK_INTERFACE(IHDCP, data, reply);

            sp<IHDCPObserver> observer =
                interface_cast<IHDCPObserver>(data.readStrongBinder());

            reply->writeInt32(setObserver(observer));
            return OK;
        }

        case HDCP_INIT_ASYNC:
        {
            CHECK_INTERFACE(IHDCP, data, reply);

            const char *host = data.readCString();
            unsigned port = data.readInt32();

            reply->writeInt32(initAsync(host, port));
            return OK;
        }

        case HDCP_SHUTDOWN_ASYNC:
        {
            CHECK_INTERFACE(IHDCP, data, reply);

            reply->writeInt32(shutdownAsync());
            return OK;
        }

        case HDCP_ENCRYPT:
        {
            size_t size = data.readInt32();

            void *inData = malloc(2 * size);
            void *outData = (uint8_t *)inData + size;

            data.read(inData, size);

            uint32_t streamCTR = data.readInt32();
            uint64_t inputCTR;
            status_t err = encrypt(inData, size, streamCTR, &inputCTR, outData);

            reply->writeInt32(err);

            if (err == OK) {
                reply->writeInt64(inputCTR);
                reply->write(outData, size);
            }

            free(inData);
            inData = outData = NULL;

            return OK;
        }

        default:
            return BBinder::onTransact(code, data, reply, flags);
    }
}

}  // namespace android