1d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber/*
2d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber * Copyright 2012, The Android Open Source Project
3d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber *
4d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber * Licensed under the Apache License, Version 2.0 (the "License");
5d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber * you may not use this file except in compliance with the License.
6d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber * You may obtain a copy of the License at
7d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber *
8d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber *     http://www.apache.org/licenses/LICENSE-2.0
9d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber *
10d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber * Unless required by applicable law or agreed to in writing, software
11d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber * distributed under the License is distributed on an "AS IS" BASIS,
12d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber * See the License for the specific language governing permissions and
14d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber * limitations under the License.
15d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber */
16d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber
1772f6aea5afba3ff8ab7e8eab49552d65ee3bb97bAndreas Huber//#define LOG_NDEBUG 0
18d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber#define LOG_TAG "WifiDisplaySource"
19d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber#include <utils/Log.h>
20d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber
21d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber#include "WifiDisplaySource.h"
22d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber#include "PlaybackSession.h"
23b8c7bd418f0ee5b88923b0e0817e3a4acc53cf8dAndreas Huber#include "Parameters.h"
24d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber#include "ParsedMessage.h"
2590a92053219ae50ddf4bb54e3d54db2d309e2b8dAndreas Huber#include "Sender.h"
26d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber
27b8c7bd418f0ee5b88923b0e0817e3a4acc53cf8dAndreas Huber#include <binder/IServiceManager.h>
280b73d4730202fcad53aefc4314a06e7b95f442f0Andreas Huber#include <gui/ISurfaceTexture.h>
29b8c7bd418f0ee5b88923b0e0817e3a4acc53cf8dAndreas Huber#include <media/IHDCP.h>
30b8c7bd418f0ee5b88923b0e0817e3a4acc53cf8dAndreas Huber#include <media/IMediaPlayerService.h>
310b73d4730202fcad53aefc4314a06e7b95f442f0Andreas Huber#include <media/IRemoteDisplayClient.h>
32d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber#include <media/stagefright/foundation/ABuffer.h>
33d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber#include <media/stagefright/foundation/ADebug.h>
34d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber#include <media/stagefright/foundation/AMessage.h>
35d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber#include <media/stagefright/MediaErrors.h>
36d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber
37bcf09f8c995221e75c7cd328f25c7cc6d2b5f7c9Andreas Huber#include <arpa/inet.h>
38bd08e2f93bafd02abf2c25d740e9fb8bce455a99Andreas Huber#include <cutils/properties.h>
39bcf09f8c995221e75c7cd328f25c7cc6d2b5f7c9Andreas Huber
40d243c04534d1b74bd66625c5c96a9b918d8838bfAndreas Huber#include <ctype.h>
41d243c04534d1b74bd66625c5c96a9b918d8838bfAndreas Huber
42d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Hubernamespace android {
43d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber
440b73d4730202fcad53aefc4314a06e7b95f442f0Andreas HuberWifiDisplaySource::WifiDisplaySource(
450b73d4730202fcad53aefc4314a06e7b95f442f0Andreas Huber        const sp<ANetworkSession> &netSession,
460b73d4730202fcad53aefc4314a06e7b95f442f0Andreas Huber        const sp<IRemoteDisplayClient> &client)
47ad0d97c7cf620e96a0b088dd9461645a3f8900b7Andreas Huber    : mState(INITIALIZED),
48ad0d97c7cf620e96a0b088dd9461645a3f8900b7Andreas Huber      mNetSession(netSession),
490b73d4730202fcad53aefc4314a06e7b95f442f0Andreas Huber      mClient(client),
50d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber      mSessionID(0),
51ea4bbfdcad9478ea19257fb19a32de68a2dfd958Andreas Huber      mStopReplyID(0),
52d243c04534d1b74bd66625c5c96a9b918d8838bfAndreas Huber      mChosenRTPPort(-1),
53e7bd24af08ef0722fb124a550662bcec48c56f86Andreas Huber      mUsingPCMAudio(false),
54c92bed3a73c06e90217f8f199ca0b517aa7595d2Andreas Huber      mClientSessionID(0),
55d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber      mReaperPending(false),
560328ec08dc1e90caa2a9e0c4e107d8ddaa74af20Andreas Huber      mNextCSeq(1),
570328ec08dc1e90caa2a9e0c4e107d8ddaa74af20Andreas Huber      mUsingHDCP(false),
580328ec08dc1e90caa2a9e0c4e107d8ddaa74af20Andreas Huber      mIsHDCP2_0(false),
590328ec08dc1e90caa2a9e0c4e107d8ddaa74af20Andreas Huber      mHDCPPort(0),
600328ec08dc1e90caa2a9e0c4e107d8ddaa74af20Andreas Huber      mHDCPInitializationComplete(false),
610328ec08dc1e90caa2a9e0c4e107d8ddaa74af20Andreas Huber      mSetupTriggerDeferred(false)
62b8c7bd418f0ee5b88923b0e0817e3a4acc53cf8dAndreas Huber{
63d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber}
64d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber
65d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas HuberWifiDisplaySource::~WifiDisplaySource() {
66d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber}
67d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber
68bcf09f8c995221e75c7cd328f25c7cc6d2b5f7c9Andreas Huberstatus_t WifiDisplaySource::start(const char *iface) {
69ad0d97c7cf620e96a0b088dd9461645a3f8900b7Andreas Huber    CHECK_EQ(mState, INITIALIZED);
70ad0d97c7cf620e96a0b088dd9461645a3f8900b7Andreas Huber
71d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber    sp<AMessage> msg = new AMessage(kWhatStart, id());
72bcf09f8c995221e75c7cd328f25c7cc6d2b5f7c9Andreas Huber    msg->setString("iface", iface);
73d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber
74d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber    sp<AMessage> response;
75d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber    status_t err = msg->postAndAwaitResponse(&response);
76d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber
77d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber    if (err != OK) {
78d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber        return err;
79d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber    }
80d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber
81d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber    if (!response->findInt32("err", &err)) {
82d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber        err = OK;
83d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber    }
84d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber
85d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber    return err;
86d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber}
87d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber
88d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huberstatus_t WifiDisplaySource::stop() {
89d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber    sp<AMessage> msg = new AMessage(kWhatStop, id());
90d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber
91d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber    sp<AMessage> response;
92d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber    status_t err = msg->postAndAwaitResponse(&response);
93d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber
94d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber    if (err != OK) {
95d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber        return err;
96d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber    }
97d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber
98d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber    if (!response->findInt32("err", &err)) {
99d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber        err = OK;
100d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber    }
101d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber
102d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber    return err;
103d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber}
104d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber
105d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Hubervoid WifiDisplaySource::onMessageReceived(const sp<AMessage> &msg) {
106d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber    switch (msg->what()) {
107d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber        case kWhatStart:
108d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber        {
109d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber            uint32_t replyID;
110d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber            CHECK(msg->senderAwaitsResponse(&replyID));
111d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber
112bcf09f8c995221e75c7cd328f25c7cc6d2b5f7c9Andreas Huber            AString iface;
113bcf09f8c995221e75c7cd328f25c7cc6d2b5f7c9Andreas Huber            CHECK(msg->findString("iface", &iface));
114bcf09f8c995221e75c7cd328f25c7cc6d2b5f7c9Andreas Huber
115bcf09f8c995221e75c7cd328f25c7cc6d2b5f7c9Andreas Huber            status_t err = OK;
116bcf09f8c995221e75c7cd328f25c7cc6d2b5f7c9Andreas Huber
117bcf09f8c995221e75c7cd328f25c7cc6d2b5f7c9Andreas Huber            ssize_t colonPos = iface.find(":");
118bcf09f8c995221e75c7cd328f25c7cc6d2b5f7c9Andreas Huber
119bcf09f8c995221e75c7cd328f25c7cc6d2b5f7c9Andreas Huber            unsigned long port;
120bcf09f8c995221e75c7cd328f25c7cc6d2b5f7c9Andreas Huber
121bcf09f8c995221e75c7cd328f25c7cc6d2b5f7c9Andreas Huber            if (colonPos >= 0) {
122bcf09f8c995221e75c7cd328f25c7cc6d2b5f7c9Andreas Huber                const char *s = iface.c_str() + colonPos + 1;
123bcf09f8c995221e75c7cd328f25c7cc6d2b5f7c9Andreas Huber
124bcf09f8c995221e75c7cd328f25c7cc6d2b5f7c9Andreas Huber                char *end;
125bcf09f8c995221e75c7cd328f25c7cc6d2b5f7c9Andreas Huber                port = strtoul(s, &end, 10);
126bcf09f8c995221e75c7cd328f25c7cc6d2b5f7c9Andreas Huber
127bcf09f8c995221e75c7cd328f25c7cc6d2b5f7c9Andreas Huber                if (end == s || *end != '\0' || port > 65535) {
128bcf09f8c995221e75c7cd328f25c7cc6d2b5f7c9Andreas Huber                    err = -EINVAL;
129bcf09f8c995221e75c7cd328f25c7cc6d2b5f7c9Andreas Huber                } else {
130bcf09f8c995221e75c7cd328f25c7cc6d2b5f7c9Andreas Huber                    iface.erase(colonPos, iface.size() - colonPos);
131bcf09f8c995221e75c7cd328f25c7cc6d2b5f7c9Andreas Huber                }
132bcf09f8c995221e75c7cd328f25c7cc6d2b5f7c9Andreas Huber            } else {
133bcf09f8c995221e75c7cd328f25c7cc6d2b5f7c9Andreas Huber                port = kWifiDisplayDefaultPort;
134bcf09f8c995221e75c7cd328f25c7cc6d2b5f7c9Andreas Huber            }
135bcf09f8c995221e75c7cd328f25c7cc6d2b5f7c9Andreas Huber
136bcf09f8c995221e75c7cd328f25c7cc6d2b5f7c9Andreas Huber            if (err == OK) {
137bd08e2f93bafd02abf2c25d740e9fb8bce455a99Andreas Huber                if (inet_aton(iface.c_str(), &mInterfaceAddr) != 0) {
138bcf09f8c995221e75c7cd328f25c7cc6d2b5f7c9Andreas Huber                    sp<AMessage> notify = new AMessage(kWhatRTSPNotify, id());
139d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber
140bcf09f8c995221e75c7cd328f25c7cc6d2b5f7c9Andreas Huber                    err = mNetSession->createRTSPServer(
141bd08e2f93bafd02abf2c25d740e9fb8bce455a99Andreas Huber                            mInterfaceAddr, port, notify, &mSessionID);
142bcf09f8c995221e75c7cd328f25c7cc6d2b5f7c9Andreas Huber                } else {
143bcf09f8c995221e75c7cd328f25c7cc6d2b5f7c9Andreas Huber                    err = -EINVAL;
144bcf09f8c995221e75c7cd328f25c7cc6d2b5f7c9Andreas Huber                }
145bcf09f8c995221e75c7cd328f25c7cc6d2b5f7c9Andreas Huber            }
146d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber
147ad0d97c7cf620e96a0b088dd9461645a3f8900b7Andreas Huber            if (err == OK) {
148ad0d97c7cf620e96a0b088dd9461645a3f8900b7Andreas Huber                mState = AWAITING_CLIENT_CONNECTION;
149ad0d97c7cf620e96a0b088dd9461645a3f8900b7Andreas Huber            }
150ad0d97c7cf620e96a0b088dd9461645a3f8900b7Andreas Huber
151d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber            sp<AMessage> response = new AMessage;
152d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber            response->setInt32("err", err);
153d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber            response->postReply(replyID);
154d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber            break;
155d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber        }
156d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber
157d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber        case kWhatRTSPNotify:
158d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber        {
159d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber            int32_t reason;
160d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber            CHECK(msg->findInt32("reason", &reason));
161d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber
162d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber            switch (reason) {
163d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber                case ANetworkSession::kWhatError:
164d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber                {
165d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber                    int32_t sessionID;
166d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber                    CHECK(msg->findInt32("sessionID", &sessionID));
167d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber
168d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber                    int32_t err;
169d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber                    CHECK(msg->findInt32("err", &err));
170d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber
171d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber                    AString detail;
172d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber                    CHECK(msg->findString("detail", &detail));
173d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber
174d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber                    ALOGE("An error occurred in session %d (%d, '%s/%s').",
175d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber                          sessionID,
176d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber                          err,
177d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber                          detail.c_str(),
178d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber                          strerror(-err));
179d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber
180d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber                    mNetSession->destroySession(sessionID);
181d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber
182c92bed3a73c06e90217f8f199ca0b517aa7595d2Andreas Huber                    if (sessionID == mClientSessionID) {
183ef7d3793fa9bbfb25253626ede9a020ee9280a17Andreas Huber                        mClientSessionID = 0;
184c92bed3a73c06e90217f8f199ca0b517aa7595d2Andreas Huber
185ef7d3793fa9bbfb25253626ede9a020ee9280a17Andreas Huber                        mClient->onDisplayError(
186ef7d3793fa9bbfb25253626ede9a020ee9280a17Andreas Huber                                IRemoteDisplayClient::kDisplayErrorUnknown);
187c92bed3a73c06e90217f8f199ca0b517aa7595d2Andreas Huber                    }
188d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber                    break;
189d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber                }
190d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber
191d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber                case ANetworkSession::kWhatClientConnected:
192d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber                {
193d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber                    int32_t sessionID;
194d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber                    CHECK(msg->findInt32("sessionID", &sessionID));
195d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber
196c92bed3a73c06e90217f8f199ca0b517aa7595d2Andreas Huber                    if (mClientSessionID > 0) {
197c92bed3a73c06e90217f8f199ca0b517aa7595d2Andreas Huber                        ALOGW("A client tried to connect, but we already "
198c92bed3a73c06e90217f8f199ca0b517aa7595d2Andreas Huber                              "have one.");
199d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber
200c92bed3a73c06e90217f8f199ca0b517aa7595d2Andreas Huber                        mNetSession->destroySession(sessionID);
201c92bed3a73c06e90217f8f199ca0b517aa7595d2Andreas Huber                        break;
202c92bed3a73c06e90217f8f199ca0b517aa7595d2Andreas Huber                    }
203c92bed3a73c06e90217f8f199ca0b517aa7595d2Andreas Huber
204ad0d97c7cf620e96a0b088dd9461645a3f8900b7Andreas Huber                    CHECK_EQ(mState, AWAITING_CLIENT_CONNECTION);
205ad0d97c7cf620e96a0b088dd9461645a3f8900b7Andreas Huber
206c92bed3a73c06e90217f8f199ca0b517aa7595d2Andreas Huber                    CHECK(msg->findString("client-ip", &mClientInfo.mRemoteIP));
207c92bed3a73c06e90217f8f199ca0b517aa7595d2Andreas Huber                    CHECK(msg->findString("server-ip", &mClientInfo.mLocalIP));
208c92bed3a73c06e90217f8f199ca0b517aa7595d2Andreas Huber
209c92bed3a73c06e90217f8f199ca0b517aa7595d2Andreas Huber                    if (mClientInfo.mRemoteIP == mClientInfo.mLocalIP) {
210c92bed3a73c06e90217f8f199ca0b517aa7595d2Andreas Huber                        // Disallow connections from the local interface
211c92bed3a73c06e90217f8f199ca0b517aa7595d2Andreas Huber                        // for security reasons.
212c92bed3a73c06e90217f8f199ca0b517aa7595d2Andreas Huber                        mNetSession->destroySession(sessionID);
213c92bed3a73c06e90217f8f199ca0b517aa7595d2Andreas Huber                        break;
214c92bed3a73c06e90217f8f199ca0b517aa7595d2Andreas Huber                    }
215d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber
216c92bed3a73c06e90217f8f199ca0b517aa7595d2Andreas Huber                    CHECK(msg->findInt32(
217c92bed3a73c06e90217f8f199ca0b517aa7595d2Andreas Huber                                "server-port", &mClientInfo.mLocalPort));
218c92bed3a73c06e90217f8f199ca0b517aa7595d2Andreas Huber                    mClientInfo.mPlaybackSessionID = -1;
219c92bed3a73c06e90217f8f199ca0b517aa7595d2Andreas Huber
220c92bed3a73c06e90217f8f199ca0b517aa7595d2Andreas Huber                    mClientSessionID = sessionID;
221c92bed3a73c06e90217f8f199ca0b517aa7595d2Andreas Huber
222c92bed3a73c06e90217f8f199ca0b517aa7595d2Andreas Huber                    ALOGI("We now have a client (%d) connected.", sessionID);
223d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber
224ad0d97c7cf620e96a0b088dd9461645a3f8900b7Andreas Huber                    mState = AWAITING_CLIENT_SETUP;
225ad0d97c7cf620e96a0b088dd9461645a3f8900b7Andreas Huber
226d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber                    status_t err = sendM1(sessionID);
227d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber                    CHECK_EQ(err, (status_t)OK);
228d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber                    break;
229d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber                }
230d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber
231d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber                case ANetworkSession::kWhatData:
232d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber                {
233b8c7bd418f0ee5b88923b0e0817e3a4acc53cf8dAndreas Huber                    status_t err = onReceiveClientData(msg);
234b8c7bd418f0ee5b88923b0e0817e3a4acc53cf8dAndreas Huber
235b8c7bd418f0ee5b88923b0e0817e3a4acc53cf8dAndreas Huber                    if (err != OK) {
236ef7d3793fa9bbfb25253626ede9a020ee9280a17Andreas Huber                        mClient->onDisplayError(
237ef7d3793fa9bbfb25253626ede9a020ee9280a17Andreas Huber                                IRemoteDisplayClient::kDisplayErrorUnknown);
238b8c7bd418f0ee5b88923b0e0817e3a4acc53cf8dAndreas Huber                    }
239d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber                    break;
240d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber                }
241d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber
242d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber                default:
243d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber                    TRESPASS();
244d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber            }
245d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber            break;
246d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber        }
247d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber
248d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber        case kWhatStop:
249d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber        {
250eb11600a248cfe5b95ddd3e5aaae02bd2ab65276Andreas Huber            CHECK(msg->senderAwaitsResponse(&mStopReplyID));
251d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber
252ad0d97c7cf620e96a0b088dd9461645a3f8900b7Andreas Huber            CHECK_LT(mState, AWAITING_CLIENT_TEARDOWN);
253ad0d97c7cf620e96a0b088dd9461645a3f8900b7Andreas Huber
254ad0d97c7cf620e96a0b088dd9461645a3f8900b7Andreas Huber            if (mState >= AWAITING_CLIENT_PLAY) {
255ad0d97c7cf620e96a0b088dd9461645a3f8900b7Andreas Huber                // We have a session, i.e. a previous SETUP succeeded.
256ad0d97c7cf620e96a0b088dd9461645a3f8900b7Andreas Huber
257ea4bbfdcad9478ea19257fb19a32de68a2dfd958Andreas Huber                status_t err = sendM5(
258ea4bbfdcad9478ea19257fb19a32de68a2dfd958Andreas Huber                        mClientSessionID, true /* requestShutdown */);
2590b73d4730202fcad53aefc4314a06e7b95f442f0Andreas Huber
260ea4bbfdcad9478ea19257fb19a32de68a2dfd958Andreas Huber                if (err == OK) {
261ad0d97c7cf620e96a0b088dd9461645a3f8900b7Andreas Huber                    mState = AWAITING_CLIENT_TEARDOWN;
262ad0d97c7cf620e96a0b088dd9461645a3f8900b7Andreas Huber
263ad0d97c7cf620e96a0b088dd9461645a3f8900b7Andreas Huber                    (new AMessage(kWhatTeardownTriggerTimedOut, id()))->post(
264ad0d97c7cf620e96a0b088dd9461645a3f8900b7Andreas Huber                            kTeardownTriggerTimeouSecs * 1000000ll);
265ad0d97c7cf620e96a0b088dd9461645a3f8900b7Andreas Huber
266ea4bbfdcad9478ea19257fb19a32de68a2dfd958Andreas Huber                    break;
267ea4bbfdcad9478ea19257fb19a32de68a2dfd958Andreas Huber                }
268ad0d97c7cf620e96a0b088dd9461645a3f8900b7Andreas Huber
269ad0d97c7cf620e96a0b088dd9461645a3f8900b7Andreas Huber                // fall through.
270b8c7bd418f0ee5b88923b0e0817e3a4acc53cf8dAndreas Huber            }
271d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber
272eb11600a248cfe5b95ddd3e5aaae02bd2ab65276Andreas Huber            finishStop();
273d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber            break;
274d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber        }
275d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber
276d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber        case kWhatReapDeadClients:
277d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber        {
278d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber            mReaperPending = false;
279d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber
280c92bed3a73c06e90217f8f199ca0b517aa7595d2Andreas Huber            if (mClientSessionID == 0
281c92bed3a73c06e90217f8f199ca0b517aa7595d2Andreas Huber                    || mClientInfo.mPlaybackSession == NULL) {
282c92bed3a73c06e90217f8f199ca0b517aa7595d2Andreas Huber                break;
283d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber            }
284d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber
285c92bed3a73c06e90217f8f199ca0b517aa7595d2Andreas Huber            if (mClientInfo.mPlaybackSession->getLastLifesignUs()
286c92bed3a73c06e90217f8f199ca0b517aa7595d2Andreas Huber                    + kPlaybackSessionTimeoutUs < ALooper::GetNowUs()) {
287c92bed3a73c06e90217f8f199ca0b517aa7595d2Andreas Huber                ALOGI("playback session timed out, reaping.");
288c92bed3a73c06e90217f8f199ca0b517aa7595d2Andreas Huber
289ef7d3793fa9bbfb25253626ede9a020ee9280a17Andreas Huber                mNetSession->destroySession(mClientSessionID);
290ef7d3793fa9bbfb25253626ede9a020ee9280a17Andreas Huber                mClientSessionID = 0;
291ef7d3793fa9bbfb25253626ede9a020ee9280a17Andreas Huber
292ef7d3793fa9bbfb25253626ede9a020ee9280a17Andreas Huber                mClient->onDisplayError(
293ef7d3793fa9bbfb25253626ede9a020ee9280a17Andreas Huber                        IRemoteDisplayClient::kDisplayErrorUnknown);
294c92bed3a73c06e90217f8f199ca0b517aa7595d2Andreas Huber            } else {
295d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber                scheduleReaper();
296d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber            }
297d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber            break;
298d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber        }
299d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber
300d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber        case kWhatPlaybackSessionNotify:
301d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber        {
302d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber            int32_t playbackSessionID;
303d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber            CHECK(msg->findInt32("playbackSessionID", &playbackSessionID));
304d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber
305d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber            int32_t what;
306d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber            CHECK(msg->findInt32("what", &what));
307d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber
308c92bed3a73c06e90217f8f199ca0b517aa7595d2Andreas Huber            if (what == PlaybackSession::kWhatSessionDead) {
309c92bed3a73c06e90217f8f199ca0b517aa7595d2Andreas Huber                ALOGI("playback session wants to quit.");
310c92bed3a73c06e90217f8f199ca0b517aa7595d2Andreas Huber
311ef7d3793fa9bbfb25253626ede9a020ee9280a17Andreas Huber                mClient->onDisplayError(
312ef7d3793fa9bbfb25253626ede9a020ee9280a17Andreas Huber                        IRemoteDisplayClient::kDisplayErrorUnknown);
313c92bed3a73c06e90217f8f199ca0b517aa7595d2Andreas Huber            } else if (what == PlaybackSession::kWhatSessionEstablished) {
314c92bed3a73c06e90217f8f199ca0b517aa7595d2Andreas Huber                if (mClient != NULL) {
315c92bed3a73c06e90217f8f199ca0b517aa7595d2Andreas Huber                    mClient->onDisplayConnected(
316c92bed3a73c06e90217f8f199ca0b517aa7595d2Andreas Huber                            mClientInfo.mPlaybackSession->getSurfaceTexture(),
317c92bed3a73c06e90217f8f199ca0b517aa7595d2Andreas Huber                            mClientInfo.mPlaybackSession->width(),
318c92bed3a73c06e90217f8f199ca0b517aa7595d2Andreas Huber                            mClientInfo.mPlaybackSession->height(),
3190328ec08dc1e90caa2a9e0c4e107d8ddaa74af20Andreas Huber                            mUsingHDCP
3200328ec08dc1e90caa2a9e0c4e107d8ddaa74af20Andreas Huber                                ? IRemoteDisplayClient::kDisplayFlagSecure
3210328ec08dc1e90caa2a9e0c4e107d8ddaa74af20Andreas Huber                                : 0);
322c92bed3a73c06e90217f8f199ca0b517aa7595d2Andreas Huber                }
323ad0d97c7cf620e96a0b088dd9461645a3f8900b7Andreas Huber
324ad0d97c7cf620e96a0b088dd9461645a3f8900b7Andreas Huber                if (mState == ABOUT_TO_PLAY) {
325ad0d97c7cf620e96a0b088dd9461645a3f8900b7Andreas Huber                    mState = PLAYING;
326ad0d97c7cf620e96a0b088dd9461645a3f8900b7Andreas Huber                }
32796fc6cc65ca93009a759a3a874b82a35771b9714Andreas Huber            } else if (what == PlaybackSession::kWhatSessionDestroyed) {
32896fc6cc65ca93009a759a3a874b82a35771b9714Andreas Huber                disconnectClient2();
329c92bed3a73c06e90217f8f199ca0b517aa7595d2Andreas Huber            } else {
330c92bed3a73c06e90217f8f199ca0b517aa7595d2Andreas Huber                CHECK_EQ(what, PlaybackSession::kWhatBinaryData);
331d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber
332c92bed3a73c06e90217f8f199ca0b517aa7595d2Andreas Huber                int32_t channel;
333c92bed3a73c06e90217f8f199ca0b517aa7595d2Andreas Huber                CHECK(msg->findInt32("channel", &channel));
334d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber
335c92bed3a73c06e90217f8f199ca0b517aa7595d2Andreas Huber                sp<ABuffer> data;
336c92bed3a73c06e90217f8f199ca0b517aa7595d2Andreas Huber                CHECK(msg->findBuffer("data", &data));
337d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber
338c92bed3a73c06e90217f8f199ca0b517aa7595d2Andreas Huber                CHECK_LE(channel, 0xffu);
339c92bed3a73c06e90217f8f199ca0b517aa7595d2Andreas Huber                CHECK_LE(data->size(), 0xffffu);
340d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber
341c92bed3a73c06e90217f8f199ca0b517aa7595d2Andreas Huber                int32_t sessionID;
342c92bed3a73c06e90217f8f199ca0b517aa7595d2Andreas Huber                CHECK(msg->findInt32("sessionID", &sessionID));
343d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber
344c92bed3a73c06e90217f8f199ca0b517aa7595d2Andreas Huber                char header[4];
345c92bed3a73c06e90217f8f199ca0b517aa7595d2Andreas Huber                header[0] = '$';
346c92bed3a73c06e90217f8f199ca0b517aa7595d2Andreas Huber                header[1] = channel;
347c92bed3a73c06e90217f8f199ca0b517aa7595d2Andreas Huber                header[2] = data->size() >> 8;
348c92bed3a73c06e90217f8f199ca0b517aa7595d2Andreas Huber                header[3] = data->size() & 0xff;
349d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber
350c92bed3a73c06e90217f8f199ca0b517aa7595d2Andreas Huber                mNetSession->sendRequest(
351c92bed3a73c06e90217f8f199ca0b517aa7595d2Andreas Huber                        sessionID, header, sizeof(header));
352d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber
353c92bed3a73c06e90217f8f199ca0b517aa7595d2Andreas Huber                mNetSession->sendRequest(
354c92bed3a73c06e90217f8f199ca0b517aa7595d2Andreas Huber                        sessionID, data->data(), data->size());
355d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber            }
356d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber            break;
357d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber        }
358d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber
359b6777017a68ed473d61cc9d6e77c34fd5cd301ccAndreas Huber        case kWhatKeepAlive:
360b6777017a68ed473d61cc9d6e77c34fd5cd301ccAndreas Huber        {
361b6777017a68ed473d61cc9d6e77c34fd5cd301ccAndreas Huber            int32_t sessionID;
362b6777017a68ed473d61cc9d6e77c34fd5cd301ccAndreas Huber            CHECK(msg->findInt32("sessionID", &sessionID));
363b6777017a68ed473d61cc9d6e77c34fd5cd301ccAndreas Huber
364c92bed3a73c06e90217f8f199ca0b517aa7595d2Andreas Huber            if (mClientSessionID != sessionID) {
365b6777017a68ed473d61cc9d6e77c34fd5cd301ccAndreas Huber                // Obsolete event, client is already gone.
366b6777017a68ed473d61cc9d6e77c34fd5cd301ccAndreas Huber                break;
367b6777017a68ed473d61cc9d6e77c34fd5cd301ccAndreas Huber            }
368b6777017a68ed473d61cc9d6e77c34fd5cd301ccAndreas Huber
369b6777017a68ed473d61cc9d6e77c34fd5cd301ccAndreas Huber            sendM16(sessionID);
370b6777017a68ed473d61cc9d6e77c34fd5cd301ccAndreas Huber            break;
371b6777017a68ed473d61cc9d6e77c34fd5cd301ccAndreas Huber        }
372b6777017a68ed473d61cc9d6e77c34fd5cd301ccAndreas Huber
373ad0d97c7cf620e96a0b088dd9461645a3f8900b7Andreas Huber        case kWhatTeardownTriggerTimedOut:
374ad0d97c7cf620e96a0b088dd9461645a3f8900b7Andreas Huber        {
375ad0d97c7cf620e96a0b088dd9461645a3f8900b7Andreas Huber            if (mState == AWAITING_CLIENT_TEARDOWN) {
376ad0d97c7cf620e96a0b088dd9461645a3f8900b7Andreas Huber                ALOGI("TEARDOWN trigger timed out, forcing disconnection.");
377ad0d97c7cf620e96a0b088dd9461645a3f8900b7Andreas Huber
378ad0d97c7cf620e96a0b088dd9461645a3f8900b7Andreas Huber                CHECK_NE(mStopReplyID, 0);
379ad0d97c7cf620e96a0b088dd9461645a3f8900b7Andreas Huber                finishStop();
380ad0d97c7cf620e96a0b088dd9461645a3f8900b7Andreas Huber                break;
381ad0d97c7cf620e96a0b088dd9461645a3f8900b7Andreas Huber            }
382ad0d97c7cf620e96a0b088dd9461645a3f8900b7Andreas Huber            break;
383ad0d97c7cf620e96a0b088dd9461645a3f8900b7Andreas Huber        }
384ad0d97c7cf620e96a0b088dd9461645a3f8900b7Andreas Huber
385b8c7bd418f0ee5b88923b0e0817e3a4acc53cf8dAndreas Huber        case kWhatHDCPNotify:
386b8c7bd418f0ee5b88923b0e0817e3a4acc53cf8dAndreas Huber        {
387b8c7bd418f0ee5b88923b0e0817e3a4acc53cf8dAndreas Huber            int32_t msgCode, ext1, ext2;
388b8c7bd418f0ee5b88923b0e0817e3a4acc53cf8dAndreas Huber            CHECK(msg->findInt32("msg", &msgCode));
389b8c7bd418f0ee5b88923b0e0817e3a4acc53cf8dAndreas Huber            CHECK(msg->findInt32("ext1", &ext1));
390b8c7bd418f0ee5b88923b0e0817e3a4acc53cf8dAndreas Huber            CHECK(msg->findInt32("ext2", &ext2));
391b8c7bd418f0ee5b88923b0e0817e3a4acc53cf8dAndreas Huber
392eb11600a248cfe5b95ddd3e5aaae02bd2ab65276Andreas Huber            ALOGI("Saw HDCP notification code %d, ext1 %d, ext2 %d",
393b8c7bd418f0ee5b88923b0e0817e3a4acc53cf8dAndreas Huber                    msgCode, ext1, ext2);
394b8c7bd418f0ee5b88923b0e0817e3a4acc53cf8dAndreas Huber
395b8c7bd418f0ee5b88923b0e0817e3a4acc53cf8dAndreas Huber            switch (msgCode) {
396b8c7bd418f0ee5b88923b0e0817e3a4acc53cf8dAndreas Huber                case HDCPModule::HDCP_INITIALIZATION_COMPLETE:
397b8c7bd418f0ee5b88923b0e0817e3a4acc53cf8dAndreas Huber                {
398b8c7bd418f0ee5b88923b0e0817e3a4acc53cf8dAndreas Huber                    mHDCPInitializationComplete = true;
399b8c7bd418f0ee5b88923b0e0817e3a4acc53cf8dAndreas Huber
400b8c7bd418f0ee5b88923b0e0817e3a4acc53cf8dAndreas Huber                    if (mSetupTriggerDeferred) {
401b8c7bd418f0ee5b88923b0e0817e3a4acc53cf8dAndreas Huber                        mSetupTriggerDeferred = false;
402b8c7bd418f0ee5b88923b0e0817e3a4acc53cf8dAndreas Huber
403ea4bbfdcad9478ea19257fb19a32de68a2dfd958Andreas Huber                        sendM5(mClientSessionID, false /* requestShutdown */);
404b8c7bd418f0ee5b88923b0e0817e3a4acc53cf8dAndreas Huber                    }
405b8c7bd418f0ee5b88923b0e0817e3a4acc53cf8dAndreas Huber                    break;
406b8c7bd418f0ee5b88923b0e0817e3a4acc53cf8dAndreas Huber                }
407b8c7bd418f0ee5b88923b0e0817e3a4acc53cf8dAndreas Huber
408eb11600a248cfe5b95ddd3e5aaae02bd2ab65276Andreas Huber                case HDCPModule::HDCP_SHUTDOWN_COMPLETE:
409ef7d3793fa9bbfb25253626ede9a020ee9280a17Andreas Huber                case HDCPModule::HDCP_SHUTDOWN_FAILED:
410eb11600a248cfe5b95ddd3e5aaae02bd2ab65276Andreas Huber                {
411ef7d3793fa9bbfb25253626ede9a020ee9280a17Andreas Huber                    // Ugly hack to make sure that the call to
412ef7d3793fa9bbfb25253626ede9a020ee9280a17Andreas Huber                    // HDCPObserver::notify is completely handled before
413ef7d3793fa9bbfb25253626ede9a020ee9280a17Andreas Huber                    // we clear the HDCP instance and unload the shared
414ef7d3793fa9bbfb25253626ede9a020ee9280a17Andreas Huber                    // library :(
415ef7d3793fa9bbfb25253626ede9a020ee9280a17Andreas Huber                    (new AMessage(kWhatFinishStop2, id()))->post(300000ll);
416eb11600a248cfe5b95ddd3e5aaae02bd2ab65276Andreas Huber                    break;
417eb11600a248cfe5b95ddd3e5aaae02bd2ab65276Andreas Huber                }
418eb11600a248cfe5b95ddd3e5aaae02bd2ab65276Andreas Huber
419b8c7bd418f0ee5b88923b0e0817e3a4acc53cf8dAndreas Huber                default:
420b8c7bd418f0ee5b88923b0e0817e3a4acc53cf8dAndreas Huber                {
421ea4bbfdcad9478ea19257fb19a32de68a2dfd958Andreas Huber                    ALOGE("HDCP failure, shutting down.");
422ea4bbfdcad9478ea19257fb19a32de68a2dfd958Andreas Huber
423ef7d3793fa9bbfb25253626ede9a020ee9280a17Andreas Huber                    mClient->onDisplayError(
424ef7d3793fa9bbfb25253626ede9a020ee9280a17Andreas Huber                            IRemoteDisplayClient::kDisplayErrorUnknown);
425b8c7bd418f0ee5b88923b0e0817e3a4acc53cf8dAndreas Huber                    break;
426b8c7bd418f0ee5b88923b0e0817e3a4acc53cf8dAndreas Huber                }
427b8c7bd418f0ee5b88923b0e0817e3a4acc53cf8dAndreas Huber            }
428b8c7bd418f0ee5b88923b0e0817e3a4acc53cf8dAndreas Huber            break;
429b8c7bd418f0ee5b88923b0e0817e3a4acc53cf8dAndreas Huber        }
430ef7d3793fa9bbfb25253626ede9a020ee9280a17Andreas Huber
431ef7d3793fa9bbfb25253626ede9a020ee9280a17Andreas Huber        case kWhatFinishStop2:
432ef7d3793fa9bbfb25253626ede9a020ee9280a17Andreas Huber        {
433ef7d3793fa9bbfb25253626ede9a020ee9280a17Andreas Huber            finishStop2();
434ef7d3793fa9bbfb25253626ede9a020ee9280a17Andreas Huber            break;
435ef7d3793fa9bbfb25253626ede9a020ee9280a17Andreas Huber        }
436b8c7bd418f0ee5b88923b0e0817e3a4acc53cf8dAndreas Huber
437d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber        default:
438d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber            TRESPASS();
439d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber    }
440d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber}
441d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber
442d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Hubervoid WifiDisplaySource::registerResponseHandler(
443d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber        int32_t sessionID, int32_t cseq, HandleRTSPResponseFunc func) {
444d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber    ResponseID id;
445d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber    id.mSessionID = sessionID;
446d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber    id.mCSeq = cseq;
447d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber    mResponseHandlers.add(id, func);
448d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber}
449d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber
450d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huberstatus_t WifiDisplaySource::sendM1(int32_t sessionID) {
451d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber    AString request = "OPTIONS * RTSP/1.0\r\n";
452d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber    AppendCommonResponse(&request, mNextCSeq);
453d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber
454d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber    request.append(
455d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber            "Require: org.wfa.wfd1.0\r\n"
456d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber            "\r\n");
457d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber
458d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber    status_t err =
459d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber        mNetSession->sendRequest(sessionID, request.c_str(), request.size());
460d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber
461d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber    if (err != OK) {
462d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber        return err;
463d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber    }
464d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber
465d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber    registerResponseHandler(
466d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber            sessionID, mNextCSeq, &WifiDisplaySource::onReceiveM1Response);
467d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber
468d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber    ++mNextCSeq;
469d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber
470d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber    return OK;
471d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber}
472d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber
473d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huberstatus_t WifiDisplaySource::sendM3(int32_t sessionID) {
474d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber    AString body =
475b8c7bd418f0ee5b88923b0e0817e3a4acc53cf8dAndreas Huber        "wfd_content_protection\r\n"
476d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber        "wfd_video_formats\r\n"
477d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber        "wfd_audio_codecs\r\n"
478d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber        "wfd_client_rtp_ports\r\n";
479d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber
480d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber    AString request = "GET_PARAMETER rtsp://localhost/wfd1.0 RTSP/1.0\r\n";
481d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber    AppendCommonResponse(&request, mNextCSeq);
482d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber
483d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber    request.append("Content-Type: text/parameters\r\n");
484d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber    request.append(StringPrintf("Content-Length: %d\r\n", body.size()));
485d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber    request.append("\r\n");
486d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber    request.append(body);
487d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber
488d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber    status_t err =
489d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber        mNetSession->sendRequest(sessionID, request.c_str(), request.size());
490d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber
491d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber    if (err != OK) {
492d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber        return err;
493d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber    }
494d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber
495d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber    registerResponseHandler(
496d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber            sessionID, mNextCSeq, &WifiDisplaySource::onReceiveM3Response);
497d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber
498d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber    ++mNextCSeq;
499d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber
500d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber    return OK;
501d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber}
502d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber
503d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huberstatus_t WifiDisplaySource::sendM4(int32_t sessionID) {
504d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber    // wfd_video_formats:
505d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber    // 1 byte "native"
506d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber    // 1 byte "preferred-display-mode-supported" 0 or 1
507d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber    // one or more avc codec structures
508d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber    //   1 byte profile
509d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber    //   1 byte level
510d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber    //   4 byte CEA mask
511d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber    //   4 byte VESA mask
512d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber    //   4 byte HH mask
513d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber    //   1 byte latency
514d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber    //   2 byte min-slice-slice
515d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber    //   2 byte slice-enc-params
516d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber    //   1 byte framerate-control-support
517d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber    //   max-hres (none or 2 byte)
518d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber    //   max-vres (none or 2 byte)
519d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber
520c92bed3a73c06e90217f8f199ca0b517aa7595d2Andreas Huber    CHECK_EQ(sessionID, mClientSessionID);
521d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber
522bd08e2f93bafd02abf2c25d740e9fb8bce455a99Andreas Huber    AString transportString = "UDP";
523bd08e2f93bafd02abf2c25d740e9fb8bce455a99Andreas Huber
524bd08e2f93bafd02abf2c25d740e9fb8bce455a99Andreas Huber    char val[PROPERTY_VALUE_MAX];
525bd08e2f93bafd02abf2c25d740e9fb8bce455a99Andreas Huber    if (property_get("media.wfd.enable-tcp", val, NULL)
526bd08e2f93bafd02abf2c25d740e9fb8bce455a99Andreas Huber            && (!strcasecmp("true", val) || !strcmp("1", val))) {
527bd08e2f93bafd02abf2c25d740e9fb8bce455a99Andreas Huber        ALOGI("Using TCP transport.");
528bd08e2f93bafd02abf2c25d740e9fb8bce455a99Andreas Huber        transportString = "TCP";
529bd08e2f93bafd02abf2c25d740e9fb8bce455a99Andreas Huber    }
530bd08e2f93bafd02abf2c25d740e9fb8bce455a99Andreas Huber
531b8c7bd418f0ee5b88923b0e0817e3a4acc53cf8dAndreas Huber    // For 720p60:
532b8c7bd418f0ee5b88923b0e0817e3a4acc53cf8dAndreas Huber    //   use "30 00 02 02 00000040 00000000 00000000 00 0000 0000 00 none none\r\n"
533b8c7bd418f0ee5b88923b0e0817e3a4acc53cf8dAndreas Huber    // For 720p30:
534b8c7bd418f0ee5b88923b0e0817e3a4acc53cf8dAndreas Huber    //   use "28 00 02 02 00000020 00000000 00000000 00 0000 0000 00 none none\r\n"
53572ff5903df5f409ea83f74c363a52f0745ced8b8Andreas Huber    // For 720p24:
53672ff5903df5f409ea83f74c363a52f0745ced8b8Andreas Huber    //   use "78 00 02 02 00008000 00000000 00000000 00 0000 0000 00 none none\r\n"
537d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber    AString body = StringPrintf(
538d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber        "wfd_video_formats: "
5394a8b9a2363b7b7b4f98022e6d9aae8b8aa8e35e5Andreas Huber        "28 00 02 02 00000020 00000000 00000000 00 0000 0000 00 none none\r\n"
540e7bd24af08ef0722fb124a550662bcec48c56f86Andreas Huber        "wfd_audio_codecs: %s\r\n"
541de799a74064a363d26f4c1bbc5a59d1b7127f49fAndreas Huber        "wfd_presentation_URL: rtsp://%s/wfd1.0/streamid=0 none\r\n"
542d243c04534d1b74bd66625c5c96a9b918d8838bfAndreas Huber        "wfd_client_rtp_ports: RTP/AVP/%s;unicast %d 0 mode=play\r\n",
543e7bd24af08ef0722fb124a550662bcec48c56f86Andreas Huber        (mUsingPCMAudio
544e7bd24af08ef0722fb124a550662bcec48c56f86Andreas Huber            ? "LPCM 00000002 00" // 2 ch PCM 48kHz
545e7bd24af08ef0722fb124a550662bcec48c56f86Andreas Huber            : "AAC 00000001 00"),  // 2 ch AAC 48kHz
546d243c04534d1b74bd66625c5c96a9b918d8838bfAndreas Huber        mClientInfo.mLocalIP.c_str(), transportString.c_str(), mChosenRTPPort);
547d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber
548d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber    AString request = "SET_PARAMETER rtsp://localhost/wfd1.0 RTSP/1.0\r\n";
549d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber    AppendCommonResponse(&request, mNextCSeq);
550d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber
551d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber    request.append("Content-Type: text/parameters\r\n");
552d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber    request.append(StringPrintf("Content-Length: %d\r\n", body.size()));
553d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber    request.append("\r\n");
554d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber    request.append(body);
555d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber
556d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber    status_t err =
557d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber        mNetSession->sendRequest(sessionID, request.c_str(), request.size());
558d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber
559d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber    if (err != OK) {
560d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber        return err;
561d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber    }
562d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber
563d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber    registerResponseHandler(
564d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber            sessionID, mNextCSeq, &WifiDisplaySource::onReceiveM4Response);
565d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber
566d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber    ++mNextCSeq;
567d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber
568d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber    return OK;
569d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber}
570d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber
571ea4bbfdcad9478ea19257fb19a32de68a2dfd958Andreas Huberstatus_t WifiDisplaySource::sendM5(int32_t sessionID, bool requestShutdown) {
572ea4bbfdcad9478ea19257fb19a32de68a2dfd958Andreas Huber    AString body = "wfd_trigger_method: ";
573ea4bbfdcad9478ea19257fb19a32de68a2dfd958Andreas Huber    if (requestShutdown) {
574ef7d3793fa9bbfb25253626ede9a020ee9280a17Andreas Huber        ALOGI("Sending TEARDOWN trigger.");
575ea4bbfdcad9478ea19257fb19a32de68a2dfd958Andreas Huber        body.append("TEARDOWN");
576ea4bbfdcad9478ea19257fb19a32de68a2dfd958Andreas Huber    } else {
577ea4bbfdcad9478ea19257fb19a32de68a2dfd958Andreas Huber        body.append("SETUP");
578ea4bbfdcad9478ea19257fb19a32de68a2dfd958Andreas Huber    }
579ea4bbfdcad9478ea19257fb19a32de68a2dfd958Andreas Huber
580ea4bbfdcad9478ea19257fb19a32de68a2dfd958Andreas Huber    body.append("\r\n");
581d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber
582d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber    AString request = "SET_PARAMETER rtsp://localhost/wfd1.0 RTSP/1.0\r\n";
583d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber    AppendCommonResponse(&request, mNextCSeq);
584d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber
585d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber    request.append("Content-Type: text/parameters\r\n");
586d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber    request.append(StringPrintf("Content-Length: %d\r\n", body.size()));
587d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber    request.append("\r\n");
588d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber    request.append(body);
589d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber
590d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber    status_t err =
591d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber        mNetSession->sendRequest(sessionID, request.c_str(), request.size());
592d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber
593d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber    if (err != OK) {
594d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber        return err;
595d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber    }
596d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber
597d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber    registerResponseHandler(
598d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber            sessionID, mNextCSeq, &WifiDisplaySource::onReceiveM5Response);
599d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber
600d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber    ++mNextCSeq;
601d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber
602d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber    return OK;
603d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber}
604d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber
605b6777017a68ed473d61cc9d6e77c34fd5cd301ccAndreas Huberstatus_t WifiDisplaySource::sendM16(int32_t sessionID) {
606b6777017a68ed473d61cc9d6e77c34fd5cd301ccAndreas Huber    AString request = "GET_PARAMETER rtsp://localhost/wfd1.0 RTSP/1.0\r\n";
607b6777017a68ed473d61cc9d6e77c34fd5cd301ccAndreas Huber    AppendCommonResponse(&request, mNextCSeq);
608b6777017a68ed473d61cc9d6e77c34fd5cd301ccAndreas Huber
609c92bed3a73c06e90217f8f199ca0b517aa7595d2Andreas Huber    CHECK_EQ(sessionID, mClientSessionID);
610c92bed3a73c06e90217f8f199ca0b517aa7595d2Andreas Huber    request.append(
611c92bed3a73c06e90217f8f199ca0b517aa7595d2Andreas Huber            StringPrintf("Session: %d\r\n", mClientInfo.mPlaybackSessionID));
612a438123bd96c7faf145683876702387efe5628d9Andreas Huber    request.append("\r\n");  // Empty body
613b6777017a68ed473d61cc9d6e77c34fd5cd301ccAndreas Huber
614b6777017a68ed473d61cc9d6e77c34fd5cd301ccAndreas Huber    status_t err =
615b6777017a68ed473d61cc9d6e77c34fd5cd301ccAndreas Huber        mNetSession->sendRequest(sessionID, request.c_str(), request.size());
616b6777017a68ed473d61cc9d6e77c34fd5cd301ccAndreas Huber
617b6777017a68ed473d61cc9d6e77c34fd5cd301ccAndreas Huber    if (err != OK) {
618b6777017a68ed473d61cc9d6e77c34fd5cd301ccAndreas Huber        return err;
619b6777017a68ed473d61cc9d6e77c34fd5cd301ccAndreas Huber    }
620b6777017a68ed473d61cc9d6e77c34fd5cd301ccAndreas Huber
621b6777017a68ed473d61cc9d6e77c34fd5cd301ccAndreas Huber    registerResponseHandler(
622b6777017a68ed473d61cc9d6e77c34fd5cd301ccAndreas Huber            sessionID, mNextCSeq, &WifiDisplaySource::onReceiveM16Response);
623b6777017a68ed473d61cc9d6e77c34fd5cd301ccAndreas Huber
624b6777017a68ed473d61cc9d6e77c34fd5cd301ccAndreas Huber    ++mNextCSeq;
625b6777017a68ed473d61cc9d6e77c34fd5cd301ccAndreas Huber
626b6777017a68ed473d61cc9d6e77c34fd5cd301ccAndreas Huber    return OK;
627b6777017a68ed473d61cc9d6e77c34fd5cd301ccAndreas Huber}
628b6777017a68ed473d61cc9d6e77c34fd5cd301ccAndreas Huber
629d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huberstatus_t WifiDisplaySource::onReceiveM1Response(
630d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber        int32_t sessionID, const sp<ParsedMessage> &msg) {
631d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber    int32_t statusCode;
632d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber    if (!msg->getStatusCode(&statusCode)) {
633d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber        return ERROR_MALFORMED;
634d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber    }
635d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber
636d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber    if (statusCode != 200) {
637d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber        return ERROR_UNSUPPORTED;
638d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber    }
639d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber
640d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber    return OK;
641d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber}
642d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber
643d243c04534d1b74bd66625c5c96a9b918d8838bfAndreas Huber// sink_audio_list := ("LPCM"|"AAC"|"AC3" HEXDIGIT*8 HEXDIGIT*2)
644d243c04534d1b74bd66625c5c96a9b918d8838bfAndreas Huber//                       (", " sink_audio_list)*
645d243c04534d1b74bd66625c5c96a9b918d8838bfAndreas Huberstatic void GetAudioModes(const char *s, const char *prefix, uint32_t *modes) {
646d243c04534d1b74bd66625c5c96a9b918d8838bfAndreas Huber    *modes = 0;
647d243c04534d1b74bd66625c5c96a9b918d8838bfAndreas Huber
648d243c04534d1b74bd66625c5c96a9b918d8838bfAndreas Huber    size_t prefixLen = strlen(prefix);
649d243c04534d1b74bd66625c5c96a9b918d8838bfAndreas Huber
650d243c04534d1b74bd66625c5c96a9b918d8838bfAndreas Huber    while (*s != '0') {
651d243c04534d1b74bd66625c5c96a9b918d8838bfAndreas Huber        if (!strncmp(s, prefix, prefixLen) && s[prefixLen] == ' ') {
652d243c04534d1b74bd66625c5c96a9b918d8838bfAndreas Huber            unsigned latency;
653d243c04534d1b74bd66625c5c96a9b918d8838bfAndreas Huber            if (sscanf(&s[prefixLen + 1], "%08x %02x", modes, &latency) != 2) {
654d243c04534d1b74bd66625c5c96a9b918d8838bfAndreas Huber                *modes = 0;
655d243c04534d1b74bd66625c5c96a9b918d8838bfAndreas Huber            }
656d243c04534d1b74bd66625c5c96a9b918d8838bfAndreas Huber
657d243c04534d1b74bd66625c5c96a9b918d8838bfAndreas Huber            return;
658d243c04534d1b74bd66625c5c96a9b918d8838bfAndreas Huber        }
659d243c04534d1b74bd66625c5c96a9b918d8838bfAndreas Huber
660d243c04534d1b74bd66625c5c96a9b918d8838bfAndreas Huber        char *commaPos = strchr(s, ',');
661d243c04534d1b74bd66625c5c96a9b918d8838bfAndreas Huber        if (commaPos != NULL) {
662d243c04534d1b74bd66625c5c96a9b918d8838bfAndreas Huber            s = commaPos + 1;
663d243c04534d1b74bd66625c5c96a9b918d8838bfAndreas Huber
664d243c04534d1b74bd66625c5c96a9b918d8838bfAndreas Huber            while (isspace(*s)) {
665d243c04534d1b74bd66625c5c96a9b918d8838bfAndreas Huber                ++s;
666d243c04534d1b74bd66625c5c96a9b918d8838bfAndreas Huber            }
667d243c04534d1b74bd66625c5c96a9b918d8838bfAndreas Huber        } else {
668d243c04534d1b74bd66625c5c96a9b918d8838bfAndreas Huber            break;
669d243c04534d1b74bd66625c5c96a9b918d8838bfAndreas Huber        }
670d243c04534d1b74bd66625c5c96a9b918d8838bfAndreas Huber    }
671d243c04534d1b74bd66625c5c96a9b918d8838bfAndreas Huber}
672d243c04534d1b74bd66625c5c96a9b918d8838bfAndreas Huber
673d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huberstatus_t WifiDisplaySource::onReceiveM3Response(
674d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber        int32_t sessionID, const sp<ParsedMessage> &msg) {
675d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber    int32_t statusCode;
676d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber    if (!msg->getStatusCode(&statusCode)) {
677d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber        return ERROR_MALFORMED;
678d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber    }
679d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber
680d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber    if (statusCode != 200) {
681d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber        return ERROR_UNSUPPORTED;
682d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber    }
683d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber
684b8c7bd418f0ee5b88923b0e0817e3a4acc53cf8dAndreas Huber    sp<Parameters> params =
685b8c7bd418f0ee5b88923b0e0817e3a4acc53cf8dAndreas Huber        Parameters::Parse(msg->getContent(), strlen(msg->getContent()));
686b8c7bd418f0ee5b88923b0e0817e3a4acc53cf8dAndreas Huber
687b8c7bd418f0ee5b88923b0e0817e3a4acc53cf8dAndreas Huber    if (params == NULL) {
688b8c7bd418f0ee5b88923b0e0817e3a4acc53cf8dAndreas Huber        return ERROR_MALFORMED;
689b8c7bd418f0ee5b88923b0e0817e3a4acc53cf8dAndreas Huber    }
690b8c7bd418f0ee5b88923b0e0817e3a4acc53cf8dAndreas Huber
691b8c7bd418f0ee5b88923b0e0817e3a4acc53cf8dAndreas Huber    AString value;
692d243c04534d1b74bd66625c5c96a9b918d8838bfAndreas Huber    if (!params->findParameter("wfd_client_rtp_ports", &value)) {
693d243c04534d1b74bd66625c5c96a9b918d8838bfAndreas Huber        ALOGE("Sink doesn't report its choice of wfd_client_rtp_ports.");
694d243c04534d1b74bd66625c5c96a9b918d8838bfAndreas Huber        return ERROR_MALFORMED;
695d243c04534d1b74bd66625c5c96a9b918d8838bfAndreas Huber    }
696d243c04534d1b74bd66625c5c96a9b918d8838bfAndreas Huber
697d243c04534d1b74bd66625c5c96a9b918d8838bfAndreas Huber    unsigned port0, port1;
698d243c04534d1b74bd66625c5c96a9b918d8838bfAndreas Huber    if (sscanf(value.c_str(),
699d243c04534d1b74bd66625c5c96a9b918d8838bfAndreas Huber               "RTP/AVP/UDP;unicast %u %u mode=play",
700d243c04534d1b74bd66625c5c96a9b918d8838bfAndreas Huber               &port0,
701d243c04534d1b74bd66625c5c96a9b918d8838bfAndreas Huber               &port1) != 2
702d243c04534d1b74bd66625c5c96a9b918d8838bfAndreas Huber        || port0 == 0 || port0 > 65535 || port1 != 0) {
703d243c04534d1b74bd66625c5c96a9b918d8838bfAndreas Huber        ALOGE("Sink chose its wfd_client_rtp_ports poorly (%s)",
704d243c04534d1b74bd66625c5c96a9b918d8838bfAndreas Huber              value.c_str());
705d243c04534d1b74bd66625c5c96a9b918d8838bfAndreas Huber
706d243c04534d1b74bd66625c5c96a9b918d8838bfAndreas Huber        return ERROR_MALFORMED;
707d243c04534d1b74bd66625c5c96a9b918d8838bfAndreas Huber    }
708d243c04534d1b74bd66625c5c96a9b918d8838bfAndreas Huber
709d243c04534d1b74bd66625c5c96a9b918d8838bfAndreas Huber    mChosenRTPPort = port0;
710d243c04534d1b74bd66625c5c96a9b918d8838bfAndreas Huber
711d243c04534d1b74bd66625c5c96a9b918d8838bfAndreas Huber    if (!params->findParameter("wfd_audio_codecs", &value)) {
712d243c04534d1b74bd66625c5c96a9b918d8838bfAndreas Huber        ALOGE("Sink doesn't report its choice of wfd_audio_codecs.");
713d243c04534d1b74bd66625c5c96a9b918d8838bfAndreas Huber        return ERROR_MALFORMED;
714d243c04534d1b74bd66625c5c96a9b918d8838bfAndreas Huber    }
715d243c04534d1b74bd66625c5c96a9b918d8838bfAndreas Huber
716d243c04534d1b74bd66625c5c96a9b918d8838bfAndreas Huber    if  (value == "none") {
717d243c04534d1b74bd66625c5c96a9b918d8838bfAndreas Huber        ALOGE("Sink doesn't support audio at all.");
718d243c04534d1b74bd66625c5c96a9b918d8838bfAndreas Huber        return ERROR_UNSUPPORTED;
719d243c04534d1b74bd66625c5c96a9b918d8838bfAndreas Huber    }
720d243c04534d1b74bd66625c5c96a9b918d8838bfAndreas Huber
721d243c04534d1b74bd66625c5c96a9b918d8838bfAndreas Huber    uint32_t modes;
722d243c04534d1b74bd66625c5c96a9b918d8838bfAndreas Huber    GetAudioModes(value.c_str(), "AAC", &modes);
723d243c04534d1b74bd66625c5c96a9b918d8838bfAndreas Huber
724d243c04534d1b74bd66625c5c96a9b918d8838bfAndreas Huber    bool supportsAAC = (modes & 1) != 0;  // AAC 2ch 48kHz
725d243c04534d1b74bd66625c5c96a9b918d8838bfAndreas Huber
726d243c04534d1b74bd66625c5c96a9b918d8838bfAndreas Huber    GetAudioModes(value.c_str(), "LPCM", &modes);
727d243c04534d1b74bd66625c5c96a9b918d8838bfAndreas Huber
728d243c04534d1b74bd66625c5c96a9b918d8838bfAndreas Huber    bool supportsPCM = (modes & 2) != 0;  // LPCM 2ch 48kHz
729d243c04534d1b74bd66625c5c96a9b918d8838bfAndreas Huber
730d243c04534d1b74bd66625c5c96a9b918d8838bfAndreas Huber    char val[PROPERTY_VALUE_MAX];
731d243c04534d1b74bd66625c5c96a9b918d8838bfAndreas Huber    if (supportsPCM
732d243c04534d1b74bd66625c5c96a9b918d8838bfAndreas Huber            && property_get("media.wfd.use-pcm-audio", val, NULL)
733d243c04534d1b74bd66625c5c96a9b918d8838bfAndreas Huber            && (!strcasecmp("true", val) || !strcmp("1", val))) {
734d243c04534d1b74bd66625c5c96a9b918d8838bfAndreas Huber        ALOGI("Using PCM audio.");
735d243c04534d1b74bd66625c5c96a9b918d8838bfAndreas Huber        mUsingPCMAudio = true;
736d243c04534d1b74bd66625c5c96a9b918d8838bfAndreas Huber    } else if (supportsAAC) {
737d243c04534d1b74bd66625c5c96a9b918d8838bfAndreas Huber        ALOGI("Using AAC audio.");
738d243c04534d1b74bd66625c5c96a9b918d8838bfAndreas Huber        mUsingPCMAudio = false;
739d243c04534d1b74bd66625c5c96a9b918d8838bfAndreas Huber    } else if (supportsPCM) {
740d243c04534d1b74bd66625c5c96a9b918d8838bfAndreas Huber        ALOGI("Using PCM audio.");
741d243c04534d1b74bd66625c5c96a9b918d8838bfAndreas Huber        mUsingPCMAudio = true;
742d243c04534d1b74bd66625c5c96a9b918d8838bfAndreas Huber    } else {
743d243c04534d1b74bd66625c5c96a9b918d8838bfAndreas Huber        ALOGI("Sink doesn't support an audio format we do.");
744d243c04534d1b74bd66625c5c96a9b918d8838bfAndreas Huber        return ERROR_UNSUPPORTED;
745d243c04534d1b74bd66625c5c96a9b918d8838bfAndreas Huber    }
746d243c04534d1b74bd66625c5c96a9b918d8838bfAndreas Huber
7470328ec08dc1e90caa2a9e0c4e107d8ddaa74af20Andreas Huber    mUsingHDCP = false;
748b8c7bd418f0ee5b88923b0e0817e3a4acc53cf8dAndreas Huber    if (!params->findParameter("wfd_content_protection", &value)) {
7490328ec08dc1e90caa2a9e0c4e107d8ddaa74af20Andreas Huber        ALOGI("Sink doesn't appear to support content protection.");
7500328ec08dc1e90caa2a9e0c4e107d8ddaa74af20Andreas Huber    } else if (value == "none") {
7510328ec08dc1e90caa2a9e0c4e107d8ddaa74af20Andreas Huber        ALOGI("Sink does not support content protection.");
7520328ec08dc1e90caa2a9e0c4e107d8ddaa74af20Andreas Huber    } else {
7530328ec08dc1e90caa2a9e0c4e107d8ddaa74af20Andreas Huber        mUsingHDCP = true;
754b8c7bd418f0ee5b88923b0e0817e3a4acc53cf8dAndreas Huber
7550328ec08dc1e90caa2a9e0c4e107d8ddaa74af20Andreas Huber        bool isHDCP2_0 = false;
7560328ec08dc1e90caa2a9e0c4e107d8ddaa74af20Andreas Huber        if (value.startsWith("HDCP2.0 ")) {
7570328ec08dc1e90caa2a9e0c4e107d8ddaa74af20Andreas Huber            isHDCP2_0 = true;
7580328ec08dc1e90caa2a9e0c4e107d8ddaa74af20Andreas Huber        } else if (!value.startsWith("HDCP2.1 ")) {
7590328ec08dc1e90caa2a9e0c4e107d8ddaa74af20Andreas Huber            ALOGE("malformed wfd_content_protection: '%s'", value.c_str());
760b8c7bd418f0ee5b88923b0e0817e3a4acc53cf8dAndreas Huber
7610328ec08dc1e90caa2a9e0c4e107d8ddaa74af20Andreas Huber            return ERROR_MALFORMED;
7620328ec08dc1e90caa2a9e0c4e107d8ddaa74af20Andreas Huber        }
763b8c7bd418f0ee5b88923b0e0817e3a4acc53cf8dAndreas Huber
7640328ec08dc1e90caa2a9e0c4e107d8ddaa74af20Andreas Huber        int32_t hdcpPort;
7650328ec08dc1e90caa2a9e0c4e107d8ddaa74af20Andreas Huber        if (!ParsedMessage::GetInt32Attribute(
7660328ec08dc1e90caa2a9e0c4e107d8ddaa74af20Andreas Huber                    value.c_str() + 8, "port", &hdcpPort)
7670328ec08dc1e90caa2a9e0c4e107d8ddaa74af20Andreas Huber                || hdcpPort < 1 || hdcpPort > 65535) {
7680328ec08dc1e90caa2a9e0c4e107d8ddaa74af20Andreas Huber            return ERROR_MALFORMED;
7690328ec08dc1e90caa2a9e0c4e107d8ddaa74af20Andreas Huber        }
770b8c7bd418f0ee5b88923b0e0817e3a4acc53cf8dAndreas Huber
7710328ec08dc1e90caa2a9e0c4e107d8ddaa74af20Andreas Huber        mIsHDCP2_0 = isHDCP2_0;
7720328ec08dc1e90caa2a9e0c4e107d8ddaa74af20Andreas Huber        mHDCPPort = hdcpPort;
773b8c7bd418f0ee5b88923b0e0817e3a4acc53cf8dAndreas Huber
7740328ec08dc1e90caa2a9e0c4e107d8ddaa74af20Andreas Huber        status_t err = makeHDCP();
7750328ec08dc1e90caa2a9e0c4e107d8ddaa74af20Andreas Huber        if (err != OK) {
7760328ec08dc1e90caa2a9e0c4e107d8ddaa74af20Andreas Huber            ALOGE("Unable to instantiate HDCP component.");
7770328ec08dc1e90caa2a9e0c4e107d8ddaa74af20Andreas Huber            return err;
7780328ec08dc1e90caa2a9e0c4e107d8ddaa74af20Andreas Huber        }
779b8c7bd418f0ee5b88923b0e0817e3a4acc53cf8dAndreas Huber    }
780b8c7bd418f0ee5b88923b0e0817e3a4acc53cf8dAndreas Huber
781d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber    return sendM4(sessionID);
782d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber}
783d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber
784d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huberstatus_t WifiDisplaySource::onReceiveM4Response(
785d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber        int32_t sessionID, const sp<ParsedMessage> &msg) {
786d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber    int32_t statusCode;
787d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber    if (!msg->getStatusCode(&statusCode)) {
788d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber        return ERROR_MALFORMED;
789d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber    }
790d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber
791d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber    if (statusCode != 200) {
792d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber        return ERROR_UNSUPPORTED;
793d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber    }
794d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber
7950328ec08dc1e90caa2a9e0c4e107d8ddaa74af20Andreas Huber    if (mUsingHDCP && !mHDCPInitializationComplete) {
796b8c7bd418f0ee5b88923b0e0817e3a4acc53cf8dAndreas Huber        ALOGI("Deferring SETUP trigger until HDCP initialization completes.");
797b8c7bd418f0ee5b88923b0e0817e3a4acc53cf8dAndreas Huber
798b8c7bd418f0ee5b88923b0e0817e3a4acc53cf8dAndreas Huber        mSetupTriggerDeferred = true;
799b8c7bd418f0ee5b88923b0e0817e3a4acc53cf8dAndreas Huber        return OK;
800b8c7bd418f0ee5b88923b0e0817e3a4acc53cf8dAndreas Huber    }
801b8c7bd418f0ee5b88923b0e0817e3a4acc53cf8dAndreas Huber
802ea4bbfdcad9478ea19257fb19a32de68a2dfd958Andreas Huber    return sendM5(sessionID, false /* requestShutdown */);
803d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber}
804d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber
805d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huberstatus_t WifiDisplaySource::onReceiveM5Response(
806d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber        int32_t sessionID, const sp<ParsedMessage> &msg) {
807d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber    int32_t statusCode;
808d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber    if (!msg->getStatusCode(&statusCode)) {
809d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber        return ERROR_MALFORMED;
810d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber    }
811d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber
812d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber    if (statusCode != 200) {
813d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber        return ERROR_UNSUPPORTED;
814d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber    }
815d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber
816d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber    return OK;
817d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber}
818d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber
819b6777017a68ed473d61cc9d6e77c34fd5cd301ccAndreas Huberstatus_t WifiDisplaySource::onReceiveM16Response(
820b6777017a68ed473d61cc9d6e77c34fd5cd301ccAndreas Huber        int32_t sessionID, const sp<ParsedMessage> &msg) {
821b6777017a68ed473d61cc9d6e77c34fd5cd301ccAndreas Huber    // If only the response was required to include a "Session:" header...
822b6777017a68ed473d61cc9d6e77c34fd5cd301ccAndreas Huber
823c92bed3a73c06e90217f8f199ca0b517aa7595d2Andreas Huber    CHECK_EQ(sessionID, mClientSessionID);
824b6777017a68ed473d61cc9d6e77c34fd5cd301ccAndreas Huber
825c92bed3a73c06e90217f8f199ca0b517aa7595d2Andreas Huber    if (mClientInfo.mPlaybackSession != NULL) {
826c92bed3a73c06e90217f8f199ca0b517aa7595d2Andreas Huber        mClientInfo.mPlaybackSession->updateLiveness();
827b6777017a68ed473d61cc9d6e77c34fd5cd301ccAndreas Huber
828b6777017a68ed473d61cc9d6e77c34fd5cd301ccAndreas Huber        scheduleKeepAlive(sessionID);
829b6777017a68ed473d61cc9d6e77c34fd5cd301ccAndreas Huber    }
830b6777017a68ed473d61cc9d6e77c34fd5cd301ccAndreas Huber
831b6777017a68ed473d61cc9d6e77c34fd5cd301ccAndreas Huber    return OK;
832b6777017a68ed473d61cc9d6e77c34fd5cd301ccAndreas Huber}
833b6777017a68ed473d61cc9d6e77c34fd5cd301ccAndreas Huber
834d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Hubervoid WifiDisplaySource::scheduleReaper() {
835d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber    if (mReaperPending) {
836d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber        return;
837d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber    }
838d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber
839d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber    mReaperPending = true;
840d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber    (new AMessage(kWhatReapDeadClients, id()))->post(kReaperIntervalUs);
841d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber}
842d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber
843b6777017a68ed473d61cc9d6e77c34fd5cd301ccAndreas Hubervoid WifiDisplaySource::scheduleKeepAlive(int32_t sessionID) {
844b6777017a68ed473d61cc9d6e77c34fd5cd301ccAndreas Huber    // We need to send updates at least 5 secs before the timeout is set to
845b6777017a68ed473d61cc9d6e77c34fd5cd301ccAndreas Huber    // expire, make sure the timeout is greater than 5 secs to begin with.
846b6777017a68ed473d61cc9d6e77c34fd5cd301ccAndreas Huber    CHECK_GT(kPlaybackSessionTimeoutUs, 5000000ll);
847b6777017a68ed473d61cc9d6e77c34fd5cd301ccAndreas Huber
848b6777017a68ed473d61cc9d6e77c34fd5cd301ccAndreas Huber    sp<AMessage> msg = new AMessage(kWhatKeepAlive, id());
849b6777017a68ed473d61cc9d6e77c34fd5cd301ccAndreas Huber    msg->setInt32("sessionID", sessionID);
850b6777017a68ed473d61cc9d6e77c34fd5cd301ccAndreas Huber    msg->post(kPlaybackSessionTimeoutUs - 5000000ll);
851b6777017a68ed473d61cc9d6e77c34fd5cd301ccAndreas Huber}
852b6777017a68ed473d61cc9d6e77c34fd5cd301ccAndreas Huber
853b8c7bd418f0ee5b88923b0e0817e3a4acc53cf8dAndreas Huberstatus_t WifiDisplaySource::onReceiveClientData(const sp<AMessage> &msg) {
854d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber    int32_t sessionID;
855d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber    CHECK(msg->findInt32("sessionID", &sessionID));
856d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber
857d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber    sp<RefBase> obj;
858d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber    CHECK(msg->findObject("data", &obj));
859d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber
860d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber    sp<ParsedMessage> data =
861d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber        static_cast<ParsedMessage *>(obj.get());
862d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber
863d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber    ALOGV("session %d received '%s'",
864d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber          sessionID, data->debugString().c_str());
865d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber
866d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber    AString method;
867d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber    AString uri;
868d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber    data->getRequestField(0, &method);
869d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber
870d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber    int32_t cseq;
871d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber    if (!data->findInt32("cseq", &cseq)) {
872d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber        sendErrorResponse(sessionID, "400 Bad Request", -1 /* cseq */);
873b8c7bd418f0ee5b88923b0e0817e3a4acc53cf8dAndreas Huber        return ERROR_MALFORMED;
874d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber    }
875d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber
876d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber    if (method.startsWith("RTSP/")) {
877d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber        // This is a response.
878d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber
879d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber        ResponseID id;
880d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber        id.mSessionID = sessionID;
881d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber        id.mCSeq = cseq;
882d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber
883d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber        ssize_t index = mResponseHandlers.indexOfKey(id);
884d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber
885d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber        if (index < 0) {
886d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber            ALOGW("Received unsolicited server response, cseq %d", cseq);
887b8c7bd418f0ee5b88923b0e0817e3a4acc53cf8dAndreas Huber            return ERROR_MALFORMED;
888d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber        }
889d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber
890d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber        HandleRTSPResponseFunc func = mResponseHandlers.valueAt(index);
891d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber        mResponseHandlers.removeItemsAt(index);
892d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber
893d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber        status_t err = (this->*func)(sessionID, data);
894d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber
895d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber        if (err != OK) {
896d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber            ALOGW("Response handler for session %d, cseq %d returned "
897d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber                  "err %d (%s)",
898d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber                  sessionID, cseq, err, strerror(-err));
899d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber
900b8c7bd418f0ee5b88923b0e0817e3a4acc53cf8dAndreas Huber            return err;
901d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber        }
902d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber
903b8c7bd418f0ee5b88923b0e0817e3a4acc53cf8dAndreas Huber        return OK;
904b8c7bd418f0ee5b88923b0e0817e3a4acc53cf8dAndreas Huber    }
905d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber
906b8c7bd418f0ee5b88923b0e0817e3a4acc53cf8dAndreas Huber    AString version;
907b8c7bd418f0ee5b88923b0e0817e3a4acc53cf8dAndreas Huber    data->getRequestField(2, &version);
908b8c7bd418f0ee5b88923b0e0817e3a4acc53cf8dAndreas Huber    if (!(version == AString("RTSP/1.0"))) {
909b8c7bd418f0ee5b88923b0e0817e3a4acc53cf8dAndreas Huber        sendErrorResponse(sessionID, "505 RTSP Version not supported", cseq);
910b8c7bd418f0ee5b88923b0e0817e3a4acc53cf8dAndreas Huber        return ERROR_UNSUPPORTED;
911b8c7bd418f0ee5b88923b0e0817e3a4acc53cf8dAndreas Huber    }
912d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber
913b8c7bd418f0ee5b88923b0e0817e3a4acc53cf8dAndreas Huber    status_t err;
914b8c7bd418f0ee5b88923b0e0817e3a4acc53cf8dAndreas Huber    if (method == "OPTIONS") {
915b8c7bd418f0ee5b88923b0e0817e3a4acc53cf8dAndreas Huber        err = onOptionsRequest(sessionID, cseq, data);
916b8c7bd418f0ee5b88923b0e0817e3a4acc53cf8dAndreas Huber    } else if (method == "SETUP") {
917b8c7bd418f0ee5b88923b0e0817e3a4acc53cf8dAndreas Huber        err = onSetupRequest(sessionID, cseq, data);
918b8c7bd418f0ee5b88923b0e0817e3a4acc53cf8dAndreas Huber    } else if (method == "PLAY") {
919b8c7bd418f0ee5b88923b0e0817e3a4acc53cf8dAndreas Huber        err = onPlayRequest(sessionID, cseq, data);
920b8c7bd418f0ee5b88923b0e0817e3a4acc53cf8dAndreas Huber    } else if (method == "PAUSE") {
921b8c7bd418f0ee5b88923b0e0817e3a4acc53cf8dAndreas Huber        err = onPauseRequest(sessionID, cseq, data);
922b8c7bd418f0ee5b88923b0e0817e3a4acc53cf8dAndreas Huber    } else if (method == "TEARDOWN") {
923b8c7bd418f0ee5b88923b0e0817e3a4acc53cf8dAndreas Huber        err = onTeardownRequest(sessionID, cseq, data);
924b8c7bd418f0ee5b88923b0e0817e3a4acc53cf8dAndreas Huber    } else if (method == "GET_PARAMETER") {
925b8c7bd418f0ee5b88923b0e0817e3a4acc53cf8dAndreas Huber        err = onGetParameterRequest(sessionID, cseq, data);
926b8c7bd418f0ee5b88923b0e0817e3a4acc53cf8dAndreas Huber    } else if (method == "SET_PARAMETER") {
927b8c7bd418f0ee5b88923b0e0817e3a4acc53cf8dAndreas Huber        err = onSetParameterRequest(sessionID, cseq, data);
928b8c7bd418f0ee5b88923b0e0817e3a4acc53cf8dAndreas Huber    } else {
929b8c7bd418f0ee5b88923b0e0817e3a4acc53cf8dAndreas Huber        sendErrorResponse(sessionID, "405 Method Not Allowed", cseq);
930d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber
931b8c7bd418f0ee5b88923b0e0817e3a4acc53cf8dAndreas Huber        err = ERROR_UNSUPPORTED;
932b8c7bd418f0ee5b88923b0e0817e3a4acc53cf8dAndreas Huber    }
933d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber
934b8c7bd418f0ee5b88923b0e0817e3a4acc53cf8dAndreas Huber    return err;
935d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber}
936d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber
937b8c7bd418f0ee5b88923b0e0817e3a4acc53cf8dAndreas Huberstatus_t WifiDisplaySource::onOptionsRequest(
938d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber        int32_t sessionID,
939d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber        int32_t cseq,
940d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber        const sp<ParsedMessage> &data) {
941d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber    int32_t playbackSessionID;
942d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber    sp<PlaybackSession> playbackSession =
943d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber        findPlaybackSession(data, &playbackSessionID);
944d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber
945d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber    if (playbackSession != NULL) {
946d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber        playbackSession->updateLiveness();
947d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber    }
948d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber
949d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber    AString response = "RTSP/1.0 200 OK\r\n";
950d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber    AppendCommonResponse(&response, cseq);
951d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber
952d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber    response.append(
953b8c7bd418f0ee5b88923b0e0817e3a4acc53cf8dAndreas Huber            "Public: org.wfa.wfd1.0, SETUP, TEARDOWN, PLAY, PAUSE, "
954d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber            "GET_PARAMETER, SET_PARAMETER\r\n");
955d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber
956d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber    response.append("\r\n");
957d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber
958d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber    status_t err = mNetSession->sendRequest(sessionID, response.c_str());
959d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber
960b8c7bd418f0ee5b88923b0e0817e3a4acc53cf8dAndreas Huber    if (err == OK) {
961b8c7bd418f0ee5b88923b0e0817e3a4acc53cf8dAndreas Huber        err = sendM3(sessionID);
962b8c7bd418f0ee5b88923b0e0817e3a4acc53cf8dAndreas Huber    }
963b8c7bd418f0ee5b88923b0e0817e3a4acc53cf8dAndreas Huber
964b8c7bd418f0ee5b88923b0e0817e3a4acc53cf8dAndreas Huber    return err;
965d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber}
966d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber
967b8c7bd418f0ee5b88923b0e0817e3a4acc53cf8dAndreas Huberstatus_t WifiDisplaySource::onSetupRequest(
968d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber        int32_t sessionID,
969d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber        int32_t cseq,
970d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber        const sp<ParsedMessage> &data) {
971c92bed3a73c06e90217f8f199ca0b517aa7595d2Andreas Huber    CHECK_EQ(sessionID, mClientSessionID);
972c92bed3a73c06e90217f8f199ca0b517aa7595d2Andreas Huber    if (mClientInfo.mPlaybackSessionID != -1) {
973b6777017a68ed473d61cc9d6e77c34fd5cd301ccAndreas Huber        // We only support a single playback session per client.
974b6777017a68ed473d61cc9d6e77c34fd5cd301ccAndreas Huber        // This is due to the reversed keep-alive design in the wfd specs...
975b6777017a68ed473d61cc9d6e77c34fd5cd301ccAndreas Huber        sendErrorResponse(sessionID, "400 Bad Request", cseq);
976b8c7bd418f0ee5b88923b0e0817e3a4acc53cf8dAndreas Huber        return ERROR_MALFORMED;
977b6777017a68ed473d61cc9d6e77c34fd5cd301ccAndreas Huber    }
978b6777017a68ed473d61cc9d6e77c34fd5cd301ccAndreas Huber
979d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber    AString transport;
980d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber    if (!data->findString("transport", &transport)) {
981d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber        sendErrorResponse(sessionID, "400 Bad Request", cseq);
982b8c7bd418f0ee5b88923b0e0817e3a4acc53cf8dAndreas Huber        return ERROR_MALFORMED;
983d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber    }
984d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber
98590a92053219ae50ddf4bb54e3d54db2d309e2b8dAndreas Huber    Sender::TransportMode transportMode = Sender::TRANSPORT_UDP;
986d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber
987d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber    int clientRtp, clientRtcp;
988d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber    if (transport.startsWith("RTP/AVP/TCP;")) {
989d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber        AString interleaved;
990bd08e2f93bafd02abf2c25d740e9fb8bce455a99Andreas Huber        if (ParsedMessage::GetAttribute(
991d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber                    transport.c_str(), "interleaved", &interleaved)
992bd08e2f93bafd02abf2c25d740e9fb8bce455a99Andreas Huber                && sscanf(interleaved.c_str(), "%d-%d",
993bd08e2f93bafd02abf2c25d740e9fb8bce455a99Andreas Huber                          &clientRtp, &clientRtcp) == 2) {
99490a92053219ae50ddf4bb54e3d54db2d309e2b8dAndreas Huber            transportMode = Sender::TRANSPORT_TCP_INTERLEAVED;
995bd08e2f93bafd02abf2c25d740e9fb8bce455a99Andreas Huber        } else {
996bd08e2f93bafd02abf2c25d740e9fb8bce455a99Andreas Huber            bool badRequest = false;
997bd08e2f93bafd02abf2c25d740e9fb8bce455a99Andreas Huber
998