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"
24a556c4822fc205db0d27834ba5b637c351d73ffaAndreas Huber#include "rtp/RTPSender.h"
25d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber
26b8c7bd418f0ee5b88923b0e0817e3a4acc53cf8dAndreas Huber#include <binder/IServiceManager.h>
278ba01021b573889802e67e029225a96f0dfa471aAndy McFadden#include <gui/IGraphicBufferProducer.h>
28b8c7bd418f0ee5b88923b0e0817e3a4acc53cf8dAndreas Huber#include <media/IHDCP.h>
29b8c7bd418f0ee5b88923b0e0817e3a4acc53cf8dAndreas Huber#include <media/IMediaPlayerService.h>
300b73d4730202fcad53aefc4314a06e7b95f442f0Andreas Huber#include <media/IRemoteDisplayClient.h>
31d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber#include <media/stagefright/foundation/ABuffer.h>
32d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber#include <media/stagefright/foundation/ADebug.h>
33d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber#include <media/stagefright/foundation/AMessage.h>
348060060217ff16cd67c8f6a15c649f44c343acf0Andreas Huber#include <media/stagefright/foundation/ParsedMessage.h>
35d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber#include <media/stagefright/MediaErrors.h>
36cd77d4a1d38b7609a03f6826a1ff5fa7c98aa34fAndreas Huber#include <media/stagefright/Utils.h>
37d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber
38bcf09f8c995221e75c7cd328f25c7cc6d2b5f7c9Andreas Huber#include <arpa/inet.h>
39bd08e2f93bafd02abf2c25d740e9fb8bce455a99Andreas Huber#include <cutils/properties.h>
40bcf09f8c995221e75c7cd328f25c7cc6d2b5f7c9Andreas Huber
41d243c04534d1b74bd66625c5c96a9b918d8838bfAndreas Huber#include <ctype.h>
42d243c04534d1b74bd66625c5c96a9b918d8838bfAndreas Huber
43d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Hubernamespace android {
44d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber
45cd77d4a1d38b7609a03f6826a1ff5fa7c98aa34fAndreas Huber// static
46cd77d4a1d38b7609a03f6826a1ff5fa7c98aa34fAndreas Huberconst AString WifiDisplaySource::sUserAgent = MakeUserAgent();
47cd77d4a1d38b7609a03f6826a1ff5fa7c98aa34fAndreas Huber
480b73d4730202fcad53aefc4314a06e7b95f442f0Andreas HuberWifiDisplaySource::WifiDisplaySource(
490b73d4730202fcad53aefc4314a06e7b95f442f0Andreas Huber        const sp<ANetworkSession> &netSession,
500b530f1050150bb751ae642d5a9dce34141d9475Andreas Huber        const sp<IRemoteDisplayClient> &client,
510b530f1050150bb751ae642d5a9dce34141d9475Andreas Huber        const char *path)
52ad0d97c7cf620e96a0b088dd9461645a3f8900b7Andreas Huber    : mState(INITIALIZED),
53ad0d97c7cf620e96a0b088dd9461645a3f8900b7Andreas Huber      mNetSession(netSession),
540b73d4730202fcad53aefc4314a06e7b95f442f0Andreas Huber      mClient(client),
55d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber      mSessionID(0),
56ea4bbfdcad9478ea19257fb19a32de68a2dfd958Andreas Huber      mStopReplyID(0),
57d243c04534d1b74bd66625c5c96a9b918d8838bfAndreas Huber      mChosenRTPPort(-1),
58e7bd24af08ef0722fb124a550662bcec48c56f86Andreas Huber      mUsingPCMAudio(false),
59c92bed3a73c06e90217f8f199ca0b517aa7595d2Andreas Huber      mClientSessionID(0),
60d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber      mReaperPending(false),
610328ec08dc1e90caa2a9e0c4e107d8ddaa74af20Andreas Huber      mNextCSeq(1),
620328ec08dc1e90caa2a9e0c4e107d8ddaa74af20Andreas Huber      mUsingHDCP(false),
630328ec08dc1e90caa2a9e0c4e107d8ddaa74af20Andreas Huber      mIsHDCP2_0(false),
640328ec08dc1e90caa2a9e0c4e107d8ddaa74af20Andreas Huber      mHDCPPort(0),
650328ec08dc1e90caa2a9e0c4e107d8ddaa74af20Andreas Huber      mHDCPInitializationComplete(false),
660b530f1050150bb751ae642d5a9dce34141d9475Andreas Huber      mSetupTriggerDeferred(false),
670b530f1050150bb751ae642d5a9dce34141d9475Andreas Huber      mPlaybackSessionEstablished(false) {
680b530f1050150bb751ae642d5a9dce34141d9475Andreas Huber    if (path != NULL) {
690b530f1050150bb751ae642d5a9dce34141d9475Andreas Huber        mMediaPath.setTo(path);
700b530f1050150bb751ae642d5a9dce34141d9475Andreas Huber    }
710b530f1050150bb751ae642d5a9dce34141d9475Andreas Huber
725abf87f9af48149972eeb851ecaea679911da040Andreas Huber    mSupportedSourceVideoFormats.disableAll();
7394a483bf2bd699275673d9cd57cb125d48572f30Andreas Huber
7494a483bf2bd699275673d9cd57cb125d48572f30Andreas Huber    mSupportedSourceVideoFormats.setNativeResolution(
7594a483bf2bd699275673d9cd57cb125d48572f30Andreas Huber            VideoFormats::RESOLUTION_CEA, 5);  // 1280x720 p30
76308bcaa44e578279e61be32b572fdb0b11b1e4c7Chong Zhang
771ad3eb9441eb509c792c61aa0181b0e74dbe9984Chong Zhang    // Enable all resolutions up to 1280x720p30
781ad3eb9441eb509c792c61aa0181b0e74dbe9984Chong Zhang    mSupportedSourceVideoFormats.enableResolutionUpto(
79308bcaa44e578279e61be32b572fdb0b11b1e4c7Chong Zhang            VideoFormats::RESOLUTION_CEA, 5,
80308bcaa44e578279e61be32b572fdb0b11b1e4c7Chong Zhang            VideoFormats::PROFILE_CHP,  // Constrained High Profile
81308bcaa44e578279e61be32b572fdb0b11b1e4c7Chong Zhang            VideoFormats::LEVEL_32);    // Level 3.2
82d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber}
83d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber
84d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas HuberWifiDisplaySource::~WifiDisplaySource() {
85d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber}
86d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber
875131d127a042ee88f903370be88845dc8c9f8578Andreas Huberstatic status_t PostAndAwaitResponse(
885131d127a042ee88f903370be88845dc8c9f8578Andreas Huber        const sp<AMessage> &msg, sp<AMessage> *response) {
895131d127a042ee88f903370be88845dc8c9f8578Andreas Huber    status_t err = msg->postAndAwaitResponse(response);
90d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber
91d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber    if (err != OK) {
92d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber        return err;
93d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber    }
94d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber
955131d127a042ee88f903370be88845dc8c9f8578Andreas Huber    if (response == NULL || !(*response)->findInt32("err", &err)) {
96d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber        err = OK;
97d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber    }
98d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber
99d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber    return err;
100d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber}
101d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber
1025131d127a042ee88f903370be88845dc8c9f8578Andreas Huberstatus_t WifiDisplaySource::start(const char *iface) {
1035131d127a042ee88f903370be88845dc8c9f8578Andreas Huber    CHECK_EQ(mState, INITIALIZED);
1045131d127a042ee88f903370be88845dc8c9f8578Andreas Huber
1055131d127a042ee88f903370be88845dc8c9f8578Andreas Huber    sp<AMessage> msg = new AMessage(kWhatStart, id());
1065131d127a042ee88f903370be88845dc8c9f8578Andreas Huber    msg->setString("iface", iface);
1075131d127a042ee88f903370be88845dc8c9f8578Andreas Huber
1085131d127a042ee88f903370be88845dc8c9f8578Andreas Huber    sp<AMessage> response;
1095131d127a042ee88f903370be88845dc8c9f8578Andreas Huber    return PostAndAwaitResponse(msg, &response);
1105131d127a042ee88f903370be88845dc8c9f8578Andreas Huber}
1115131d127a042ee88f903370be88845dc8c9f8578Andreas Huber
112d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huberstatus_t WifiDisplaySource::stop() {
113d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber    sp<AMessage> msg = new AMessage(kWhatStop, id());
114d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber
115d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber    sp<AMessage> response;
1165131d127a042ee88f903370be88845dc8c9f8578Andreas Huber    return PostAndAwaitResponse(msg, &response);
1175131d127a042ee88f903370be88845dc8c9f8578Andreas Huber}
118d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber
1195131d127a042ee88f903370be88845dc8c9f8578Andreas Huberstatus_t WifiDisplaySource::pause() {
1205131d127a042ee88f903370be88845dc8c9f8578Andreas Huber    sp<AMessage> msg = new AMessage(kWhatPause, id());
121d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber
1225131d127a042ee88f903370be88845dc8c9f8578Andreas Huber    sp<AMessage> response;
1235131d127a042ee88f903370be88845dc8c9f8578Andreas Huber    return PostAndAwaitResponse(msg, &response);
1245131d127a042ee88f903370be88845dc8c9f8578Andreas Huber}
125d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber
1265131d127a042ee88f903370be88845dc8c9f8578Andreas Huberstatus_t WifiDisplaySource::resume() {
1275131d127a042ee88f903370be88845dc8c9f8578Andreas Huber    sp<AMessage> msg = new AMessage(kWhatResume, id());
1285131d127a042ee88f903370be88845dc8c9f8578Andreas Huber
1295131d127a042ee88f903370be88845dc8c9f8578Andreas Huber    sp<AMessage> response;
1305131d127a042ee88f903370be88845dc8c9f8578Andreas Huber    return PostAndAwaitResponse(msg, &response);
131d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber}
132d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber
133d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Hubervoid WifiDisplaySource::onMessageReceived(const sp<AMessage> &msg) {
134d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber    switch (msg->what()) {
135d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber        case kWhatStart:
136d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber        {
137d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber            uint32_t replyID;
138d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber            CHECK(msg->senderAwaitsResponse(&replyID));
139d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber
140bcf09f8c995221e75c7cd328f25c7cc6d2b5f7c9Andreas Huber            AString iface;
141bcf09f8c995221e75c7cd328f25c7cc6d2b5f7c9Andreas Huber            CHECK(msg->findString("iface", &iface));
142bcf09f8c995221e75c7cd328f25c7cc6d2b5f7c9Andreas Huber
143bcf09f8c995221e75c7cd328f25c7cc6d2b5f7c9Andreas Huber            status_t err = OK;
144bcf09f8c995221e75c7cd328f25c7cc6d2b5f7c9Andreas Huber
145bcf09f8c995221e75c7cd328f25c7cc6d2b5f7c9Andreas Huber            ssize_t colonPos = iface.find(":");
146bcf09f8c995221e75c7cd328f25c7cc6d2b5f7c9Andreas Huber
147bcf09f8c995221e75c7cd328f25c7cc6d2b5f7c9Andreas Huber            unsigned long port;
148bcf09f8c995221e75c7cd328f25c7cc6d2b5f7c9Andreas Huber
149bcf09f8c995221e75c7cd328f25c7cc6d2b5f7c9Andreas Huber            if (colonPos >= 0) {
150bcf09f8c995221e75c7cd328f25c7cc6d2b5f7c9Andreas Huber                const char *s = iface.c_str() + colonPos + 1;
151bcf09f8c995221e75c7cd328f25c7cc6d2b5f7c9Andreas Huber
152bcf09f8c995221e75c7cd328f25c7cc6d2b5f7c9Andreas Huber                char *end;
153bcf09f8c995221e75c7cd328f25c7cc6d2b5f7c9Andreas Huber                port = strtoul(s, &end, 10);
154bcf09f8c995221e75c7cd328f25c7cc6d2b5f7c9Andreas Huber
155bcf09f8c995221e75c7cd328f25c7cc6d2b5f7c9Andreas Huber                if (end == s || *end != '\0' || port > 65535) {
156bcf09f8c995221e75c7cd328f25c7cc6d2b5f7c9Andreas Huber                    err = -EINVAL;
157bcf09f8c995221e75c7cd328f25c7cc6d2b5f7c9Andreas Huber                } else {
158bcf09f8c995221e75c7cd328f25c7cc6d2b5f7c9Andreas Huber                    iface.erase(colonPos, iface.size() - colonPos);
159bcf09f8c995221e75c7cd328f25c7cc6d2b5f7c9Andreas Huber                }
160bcf09f8c995221e75c7cd328f25c7cc6d2b5f7c9Andreas Huber            } else {
161bcf09f8c995221e75c7cd328f25c7cc6d2b5f7c9Andreas Huber                port = kWifiDisplayDefaultPort;
162bcf09f8c995221e75c7cd328f25c7cc6d2b5f7c9Andreas Huber            }
163bcf09f8c995221e75c7cd328f25c7cc6d2b5f7c9Andreas Huber
164bcf09f8c995221e75c7cd328f25c7cc6d2b5f7c9Andreas Huber            if (err == OK) {
165bd08e2f93bafd02abf2c25d740e9fb8bce455a99Andreas Huber                if (inet_aton(iface.c_str(), &mInterfaceAddr) != 0) {
166bcf09f8c995221e75c7cd328f25c7cc6d2b5f7c9Andreas Huber                    sp<AMessage> notify = new AMessage(kWhatRTSPNotify, id());
167d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber
168bcf09f8c995221e75c7cd328f25c7cc6d2b5f7c9Andreas Huber                    err = mNetSession->createRTSPServer(
169bd08e2f93bafd02abf2c25d740e9fb8bce455a99Andreas Huber                            mInterfaceAddr, port, notify, &mSessionID);
170bcf09f8c995221e75c7cd328f25c7cc6d2b5f7c9Andreas Huber                } else {
171bcf09f8c995221e75c7cd328f25c7cc6d2b5f7c9Andreas Huber                    err = -EINVAL;
172bcf09f8c995221e75c7cd328f25c7cc6d2b5f7c9Andreas Huber                }
173c86ef45279185b474bd6af0a7ae407f8ab577f13Andreas Huber            }
174c86ef45279185b474bd6af0a7ae407f8ab577f13Andreas Huber
1756ea551fa13b69e5ce359a7dba7485d857a005304Andreas Huber            mState = AWAITING_CLIENT_CONNECTION;
176ad0d97c7cf620e96a0b088dd9461645a3f8900b7Andreas Huber
177d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber            sp<AMessage> response = new AMessage;
178d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber            response->setInt32("err", err);
179d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber            response->postReply(replyID);
180d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber            break;
181d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber        }
182d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber
183d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber        case kWhatRTSPNotify:
184d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber        {
185d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber            int32_t reason;
186d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber            CHECK(msg->findInt32("reason", &reason));
187d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber
188d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber            switch (reason) {
189d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber                case ANetworkSession::kWhatError:
190d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber                {
191d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber                    int32_t sessionID;
192d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber                    CHECK(msg->findInt32("sessionID", &sessionID));
193d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber
194d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber                    int32_t err;
195d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber                    CHECK(msg->findInt32("err", &err));
196d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber
197d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber                    AString detail;
198d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber                    CHECK(msg->findString("detail", &detail));
199d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber
200d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber                    ALOGE("An error occurred in session %d (%d, '%s/%s').",
201d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber                          sessionID,
202d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber                          err,
203d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber                          detail.c_str(),
204d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber                          strerror(-err));
205d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber
206d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber                    mNetSession->destroySession(sessionID);
207d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber
208c92bed3a73c06e90217f8f199ca0b517aa7595d2Andreas Huber                    if (sessionID == mClientSessionID) {
209ef7d3793fa9bbfb25253626ede9a020ee9280a17Andreas Huber                        mClientSessionID = 0;
210c92bed3a73c06e90217f8f199ca0b517aa7595d2Andreas Huber
211ef7d3793fa9bbfb25253626ede9a020ee9280a17Andreas Huber                        mClient->onDisplayError(
212ef7d3793fa9bbfb25253626ede9a020ee9280a17Andreas Huber                                IRemoteDisplayClient::kDisplayErrorUnknown);
213c92bed3a73c06e90217f8f199ca0b517aa7595d2Andreas Huber                    }
214d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber                    break;
215d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber                }
216d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber
217d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber                case ANetworkSession::kWhatClientConnected:
218d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber                {
219d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber                    int32_t sessionID;
220d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber                    CHECK(msg->findInt32("sessionID", &sessionID));
221d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber
222c92bed3a73c06e90217f8f199ca0b517aa7595d2Andreas Huber                    if (mClientSessionID > 0) {
223c92bed3a73c06e90217f8f199ca0b517aa7595d2Andreas Huber                        ALOGW("A client tried to connect, but we already "
224c92bed3a73c06e90217f8f199ca0b517aa7595d2Andreas Huber                              "have one.");
225d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber
226c92bed3a73c06e90217f8f199ca0b517aa7595d2Andreas Huber                        mNetSession->destroySession(sessionID);
227c92bed3a73c06e90217f8f199ca0b517aa7595d2Andreas Huber                        break;
228c92bed3a73c06e90217f8f199ca0b517aa7595d2Andreas Huber                    }
229c92bed3a73c06e90217f8f199ca0b517aa7595d2Andreas Huber
230ad0d97c7cf620e96a0b088dd9461645a3f8900b7Andreas Huber                    CHECK_EQ(mState, AWAITING_CLIENT_CONNECTION);
231ad0d97c7cf620e96a0b088dd9461645a3f8900b7Andreas Huber
232c92bed3a73c06e90217f8f199ca0b517aa7595d2Andreas Huber                    CHECK(msg->findString("client-ip", &mClientInfo.mRemoteIP));
233c92bed3a73c06e90217f8f199ca0b517aa7595d2Andreas Huber                    CHECK(msg->findString("server-ip", &mClientInfo.mLocalIP));
234c92bed3a73c06e90217f8f199ca0b517aa7595d2Andreas Huber
235c92bed3a73c06e90217f8f199ca0b517aa7595d2Andreas Huber                    if (mClientInfo.mRemoteIP == mClientInfo.mLocalIP) {
236c92bed3a73c06e90217f8f199ca0b517aa7595d2Andreas Huber                        // Disallow connections from the local interface
237c92bed3a73c06e90217f8f199ca0b517aa7595d2Andreas Huber                        // for security reasons.
238c92bed3a73c06e90217f8f199ca0b517aa7595d2Andreas Huber                        mNetSession->destroySession(sessionID);
239c92bed3a73c06e90217f8f199ca0b517aa7595d2Andreas Huber                        break;
240c92bed3a73c06e90217f8f199ca0b517aa7595d2Andreas Huber                    }
241d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber
242c92bed3a73c06e90217f8f199ca0b517aa7595d2Andreas Huber                    CHECK(msg->findInt32(
243c92bed3a73c06e90217f8f199ca0b517aa7595d2Andreas Huber                                "server-port", &mClientInfo.mLocalPort));
244c92bed3a73c06e90217f8f199ca0b517aa7595d2Andreas Huber                    mClientInfo.mPlaybackSessionID = -1;
245c92bed3a73c06e90217f8f199ca0b517aa7595d2Andreas Huber
246c92bed3a73c06e90217f8f199ca0b517aa7595d2Andreas Huber                    mClientSessionID = sessionID;
247c92bed3a73c06e90217f8f199ca0b517aa7595d2Andreas Huber
248c92bed3a73c06e90217f8f199ca0b517aa7595d2Andreas Huber                    ALOGI("We now have a client (%d) connected.", sessionID);
249d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber
250ad0d97c7cf620e96a0b088dd9461645a3f8900b7Andreas Huber                    mState = AWAITING_CLIENT_SETUP;
251ad0d97c7cf620e96a0b088dd9461645a3f8900b7Andreas Huber
252d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber                    status_t err = sendM1(sessionID);
253d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber                    CHECK_EQ(err, (status_t)OK);
254d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber                    break;
255d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber                }
256d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber
257d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber                case ANetworkSession::kWhatData:
258d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber                {
259b8c7bd418f0ee5b88923b0e0817e3a4acc53cf8dAndreas Huber                    status_t err = onReceiveClientData(msg);
260b8c7bd418f0ee5b88923b0e0817e3a4acc53cf8dAndreas Huber
261b8c7bd418f0ee5b88923b0e0817e3a4acc53cf8dAndreas Huber                    if (err != OK) {
262ef7d3793fa9bbfb25253626ede9a020ee9280a17Andreas Huber                        mClient->onDisplayError(
263ef7d3793fa9bbfb25253626ede9a020ee9280a17Andreas Huber                                IRemoteDisplayClient::kDisplayErrorUnknown);
264b8c7bd418f0ee5b88923b0e0817e3a4acc53cf8dAndreas Huber                    }
2655131d127a042ee88f903370be88845dc8c9f8578Andreas Huber
2665131d127a042ee88f903370be88845dc8c9f8578Andreas Huber#if 0
2675131d127a042ee88f903370be88845dc8c9f8578Andreas Huber                    // testing only.
2685131d127a042ee88f903370be88845dc8c9f8578Andreas Huber                    char val[PROPERTY_VALUE_MAX];
2695131d127a042ee88f903370be88845dc8c9f8578Andreas Huber                    if (property_get("media.wfd.trigger", val, NULL)) {
2705131d127a042ee88f903370be88845dc8c9f8578Andreas Huber                        if (!strcasecmp(val, "pause") && mState == PLAYING) {
2715131d127a042ee88f903370be88845dc8c9f8578Andreas Huber                            mState = PLAYING_TO_PAUSED;
2725131d127a042ee88f903370be88845dc8c9f8578Andreas Huber                            sendTrigger(mClientSessionID, TRIGGER_PAUSE);
273ff9297ac908aa01e44fda4ab9ca7a4bb514c00fdAndreas Huber                        } else if (!strcasecmp(val, "play")
274ff9297ac908aa01e44fda4ab9ca7a4bb514c00fdAndreas Huber                                    && mState == PAUSED) {
2755131d127a042ee88f903370be88845dc8c9f8578Andreas Huber                            mState = PAUSED_TO_PLAYING;
2765131d127a042ee88f903370be88845dc8c9f8578Andreas Huber                            sendTrigger(mClientSessionID, TRIGGER_PLAY);
2775131d127a042ee88f903370be88845dc8c9f8578Andreas Huber                        }
2785131d127a042ee88f903370be88845dc8c9f8578Andreas Huber                    }
2795131d127a042ee88f903370be88845dc8c9f8578Andreas Huber#endif
280d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber                    break;
281d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber                }
282d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber
283126568c7aeeb5570789e70a310477f44dbdbd885Andreas Huber                case ANetworkSession::kWhatNetworkStall:
284126568c7aeeb5570789e70a310477f44dbdbd885Andreas Huber                {
285126568c7aeeb5570789e70a310477f44dbdbd885Andreas Huber                    break;
286126568c7aeeb5570789e70a310477f44dbdbd885Andreas Huber                }
287126568c7aeeb5570789e70a310477f44dbdbd885Andreas Huber
288d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber                default:
289d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber                    TRESPASS();
290d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber            }
291d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber            break;
292d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber        }
293d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber
294d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber        case kWhatStop:
295d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber        {
296eb11600a248cfe5b95ddd3e5aaae02bd2ab65276Andreas Huber            CHECK(msg->senderAwaitsResponse(&mStopReplyID));
297d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber
298ad0d97c7cf620e96a0b088dd9461645a3f8900b7Andreas Huber            CHECK_LT(mState, AWAITING_CLIENT_TEARDOWN);
299ad0d97c7cf620e96a0b088dd9461645a3f8900b7Andreas Huber
300ad0d97c7cf620e96a0b088dd9461645a3f8900b7Andreas Huber            if (mState >= AWAITING_CLIENT_PLAY) {
301ad0d97c7cf620e96a0b088dd9461645a3f8900b7Andreas Huber                // We have a session, i.e. a previous SETUP succeeded.
302ad0d97c7cf620e96a0b088dd9461645a3f8900b7Andreas Huber
3035131d127a042ee88f903370be88845dc8c9f8578Andreas Huber                status_t err = sendTrigger(
3045131d127a042ee88f903370be88845dc8c9f8578Andreas Huber                        mClientSessionID, TRIGGER_TEARDOWN);
3050b73d4730202fcad53aefc4314a06e7b95f442f0Andreas Huber
306ea4bbfdcad9478ea19257fb19a32de68a2dfd958Andreas Huber                if (err == OK) {
307ad0d97c7cf620e96a0b088dd9461645a3f8900b7Andreas Huber                    mState = AWAITING_CLIENT_TEARDOWN;
308ad0d97c7cf620e96a0b088dd9461645a3f8900b7Andreas Huber
309ad0d97c7cf620e96a0b088dd9461645a3f8900b7Andreas Huber                    (new AMessage(kWhatTeardownTriggerTimedOut, id()))->post(
310ad0d97c7cf620e96a0b088dd9461645a3f8900b7Andreas Huber                            kTeardownTriggerTimeouSecs * 1000000ll);
311ad0d97c7cf620e96a0b088dd9461645a3f8900b7Andreas Huber
312ea4bbfdcad9478ea19257fb19a32de68a2dfd958Andreas Huber                    break;
313ea4bbfdcad9478ea19257fb19a32de68a2dfd958Andreas Huber                }
314ad0d97c7cf620e96a0b088dd9461645a3f8900b7Andreas Huber
315ad0d97c7cf620e96a0b088dd9461645a3f8900b7Andreas Huber                // fall through.
316b8c7bd418f0ee5b88923b0e0817e3a4acc53cf8dAndreas Huber            }
317d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber
318eb11600a248cfe5b95ddd3e5aaae02bd2ab65276Andreas Huber            finishStop();
319d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber            break;
320d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber        }
321d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber
3225131d127a042ee88f903370be88845dc8c9f8578Andreas Huber        case kWhatPause:
3235131d127a042ee88f903370be88845dc8c9f8578Andreas Huber        {
3245131d127a042ee88f903370be88845dc8c9f8578Andreas Huber            uint32_t replyID;
3255131d127a042ee88f903370be88845dc8c9f8578Andreas Huber            CHECK(msg->senderAwaitsResponse(&replyID));
3265131d127a042ee88f903370be88845dc8c9f8578Andreas Huber
3275131d127a042ee88f903370be88845dc8c9f8578Andreas Huber            status_t err = OK;
3285131d127a042ee88f903370be88845dc8c9f8578Andreas Huber
3295131d127a042ee88f903370be88845dc8c9f8578Andreas Huber            if (mState != PLAYING) {
3305131d127a042ee88f903370be88845dc8c9f8578Andreas Huber                err = INVALID_OPERATION;
3315131d127a042ee88f903370be88845dc8c9f8578Andreas Huber            } else {
3325131d127a042ee88f903370be88845dc8c9f8578Andreas Huber                mState = PLAYING_TO_PAUSED;
3335131d127a042ee88f903370be88845dc8c9f8578Andreas Huber                sendTrigger(mClientSessionID, TRIGGER_PAUSE);
3345131d127a042ee88f903370be88845dc8c9f8578Andreas Huber            }
3355131d127a042ee88f903370be88845dc8c9f8578Andreas Huber
3365131d127a042ee88f903370be88845dc8c9f8578Andreas Huber            sp<AMessage> response = new AMessage;
3375131d127a042ee88f903370be88845dc8c9f8578Andreas Huber            response->setInt32("err", err);
3385131d127a042ee88f903370be88845dc8c9f8578Andreas Huber            response->postReply(replyID);
3395131d127a042ee88f903370be88845dc8c9f8578Andreas Huber            break;
3405131d127a042ee88f903370be88845dc8c9f8578Andreas Huber        }
3415131d127a042ee88f903370be88845dc8c9f8578Andreas Huber
3425131d127a042ee88f903370be88845dc8c9f8578Andreas Huber        case kWhatResume:
3435131d127a042ee88f903370be88845dc8c9f8578Andreas Huber        {
3445131d127a042ee88f903370be88845dc8c9f8578Andreas Huber            uint32_t replyID;
3455131d127a042ee88f903370be88845dc8c9f8578Andreas Huber            CHECK(msg->senderAwaitsResponse(&replyID));
3465131d127a042ee88f903370be88845dc8c9f8578Andreas Huber
3475131d127a042ee88f903370be88845dc8c9f8578Andreas Huber            status_t err = OK;
3485131d127a042ee88f903370be88845dc8c9f8578Andreas Huber
3495131d127a042ee88f903370be88845dc8c9f8578Andreas Huber            if (mState != PAUSED) {
3505131d127a042ee88f903370be88845dc8c9f8578Andreas Huber                err = INVALID_OPERATION;
3515131d127a042ee88f903370be88845dc8c9f8578Andreas Huber            } else {
3525131d127a042ee88f903370be88845dc8c9f8578Andreas Huber                mState = PAUSED_TO_PLAYING;
3535131d127a042ee88f903370be88845dc8c9f8578Andreas Huber                sendTrigger(mClientSessionID, TRIGGER_PLAY);
3545131d127a042ee88f903370be88845dc8c9f8578Andreas Huber            }
3555131d127a042ee88f903370be88845dc8c9f8578Andreas Huber
3565131d127a042ee88f903370be88845dc8c9f8578Andreas Huber            sp<AMessage> response = new AMessage;
3575131d127a042ee88f903370be88845dc8c9f8578Andreas Huber            response->setInt32("err", err);
3585131d127a042ee88f903370be88845dc8c9f8578Andreas Huber            response->postReply(replyID);
3595131d127a042ee88f903370be88845dc8c9f8578Andreas Huber            break;
3605131d127a042ee88f903370be88845dc8c9f8578Andreas Huber        }
3615131d127a042ee88f903370be88845dc8c9f8578Andreas Huber
362d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber        case kWhatReapDeadClients:
363d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber        {
364d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber            mReaperPending = false;
365d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber
366c92bed3a73c06e90217f8f199ca0b517aa7595d2Andreas Huber            if (mClientSessionID == 0
367c92bed3a73c06e90217f8f199ca0b517aa7595d2Andreas Huber                    || mClientInfo.mPlaybackSession == NULL) {
368c92bed3a73c06e90217f8f199ca0b517aa7595d2Andreas Huber                break;
369d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber            }
370d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber
371c92bed3a73c06e90217f8f199ca0b517aa7595d2Andreas Huber            if (mClientInfo.mPlaybackSession->getLastLifesignUs()
372c92bed3a73c06e90217f8f199ca0b517aa7595d2Andreas Huber                    + kPlaybackSessionTimeoutUs < ALooper::GetNowUs()) {
373c92bed3a73c06e90217f8f199ca0b517aa7595d2Andreas Huber                ALOGI("playback session timed out, reaping.");
374c92bed3a73c06e90217f8f199ca0b517aa7595d2Andreas Huber
375ef7d3793fa9bbfb25253626ede9a020ee9280a17Andreas Huber                mNetSession->destroySession(mClientSessionID);
376ef7d3793fa9bbfb25253626ede9a020ee9280a17Andreas Huber                mClientSessionID = 0;
377ef7d3793fa9bbfb25253626ede9a020ee9280a17Andreas Huber
378ef7d3793fa9bbfb25253626ede9a020ee9280a17Andreas Huber                mClient->onDisplayError(
379ef7d3793fa9bbfb25253626ede9a020ee9280a17Andreas Huber                        IRemoteDisplayClient::kDisplayErrorUnknown);
380c92bed3a73c06e90217f8f199ca0b517aa7595d2Andreas Huber            } else {
381d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber                scheduleReaper();
382d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber            }
383d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber            break;
384d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber        }
385d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber
386d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber        case kWhatPlaybackSessionNotify:
387d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber        {
388d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber            int32_t playbackSessionID;
389d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber            CHECK(msg->findInt32("playbackSessionID", &playbackSessionID));
390d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber
391d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber            int32_t what;
392d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber            CHECK(msg->findInt32("what", &what));
393d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber
394c92bed3a73c06e90217f8f199ca0b517aa7595d2Andreas Huber            if (what == PlaybackSession::kWhatSessionDead) {
395c92bed3a73c06e90217f8f199ca0b517aa7595d2Andreas Huber                ALOGI("playback session wants to quit.");
396c92bed3a73c06e90217f8f199ca0b517aa7595d2Andreas Huber
397ef7d3793fa9bbfb25253626ede9a020ee9280a17Andreas Huber                mClient->onDisplayError(
398ef7d3793fa9bbfb25253626ede9a020ee9280a17Andreas Huber                        IRemoteDisplayClient::kDisplayErrorUnknown);
399c92bed3a73c06e90217f8f199ca0b517aa7595d2Andreas Huber            } else if (what == PlaybackSession::kWhatSessionEstablished) {
4000b530f1050150bb751ae642d5a9dce34141d9475Andreas Huber                mPlaybackSessionEstablished = true;
4010b530f1050150bb751ae642d5a9dce34141d9475Andreas Huber
402c92bed3a73c06e90217f8f199ca0b517aa7595d2Andreas Huber                if (mClient != NULL) {
40394a483bf2bd699275673d9cd57cb125d48572f30Andreas Huber                    if (!mSinkSupportsVideo) {
40494a483bf2bd699275673d9cd57cb125d48572f30Andreas Huber                        mClient->onDisplayConnected(
40594a483bf2bd699275673d9cd57cb125d48572f30Andreas Huber                                NULL,  // SurfaceTexture
40694a483bf2bd699275673d9cd57cb125d48572f30Andreas Huber                                0, // width,
40794a483bf2bd699275673d9cd57cb125d48572f30Andreas Huber                                0, // height,
40894a483bf2bd699275673d9cd57cb125d48572f30Andreas Huber                                mUsingHDCP
40994a483bf2bd699275673d9cd57cb125d48572f30Andreas Huber                                    ? IRemoteDisplayClient::kDisplayFlagSecure
41087ecf19404586672008e98babc225e094292ceb5Chong Zhang                                    : 0,
41187ecf19404586672008e98babc225e094292ceb5Chong Zhang                                0);
41294a483bf2bd699275673d9cd57cb125d48572f30Andreas Huber                    } else {
41394a483bf2bd699275673d9cd57cb125d48572f30Andreas Huber                        size_t width, height;
41494a483bf2bd699275673d9cd57cb125d48572f30Andreas Huber
41594a483bf2bd699275673d9cd57cb125d48572f30Andreas Huber                        CHECK(VideoFormats::GetConfiguration(
41694a483bf2bd699275673d9cd57cb125d48572f30Andreas Huber                                    mChosenVideoResolutionType,
41794a483bf2bd699275673d9cd57cb125d48572f30Andreas Huber                                    mChosenVideoResolutionIndex,
41894a483bf2bd699275673d9cd57cb125d48572f30Andreas Huber                                    &width,
41994a483bf2bd699275673d9cd57cb125d48572f30Andreas Huber                                    &height,
42094a483bf2bd699275673d9cd57cb125d48572f30Andreas Huber                                    NULL /* framesPerSecond */,
42194a483bf2bd699275673d9cd57cb125d48572f30Andreas Huber                                    NULL /* interlaced */));
42294a483bf2bd699275673d9cd57cb125d48572f30Andreas Huber
42394a483bf2bd699275673d9cd57cb125d48572f30Andreas Huber                        mClient->onDisplayConnected(
424ff9297ac908aa01e44fda4ab9ca7a4bb514c00fdAndreas Huber                                mClientInfo.mPlaybackSession
425ff9297ac908aa01e44fda4ab9ca7a4bb514c00fdAndreas Huber                                    ->getSurfaceTexture(),
42694a483bf2bd699275673d9cd57cb125d48572f30Andreas Huber                                width,
42794a483bf2bd699275673d9cd57cb125d48572f30Andreas Huber                                height,
42894a483bf2bd699275673d9cd57cb125d48572f30Andreas Huber                                mUsingHDCP
42994a483bf2bd699275673d9cd57cb125d48572f30Andreas Huber                                    ? IRemoteDisplayClient::kDisplayFlagSecure
43087ecf19404586672008e98babc225e094292ceb5Chong Zhang                                    : 0,
43187ecf19404586672008e98babc225e094292ceb5Chong Zhang                                playbackSessionID);
43294a483bf2bd699275673d9cd57cb125d48572f30Andreas Huber                    }
433c92bed3a73c06e90217f8f199ca0b517aa7595d2Andreas Huber                }
434ad0d97c7cf620e96a0b088dd9461645a3f8900b7Andreas Huber
4350b530f1050150bb751ae642d5a9dce34141d9475Andreas Huber                finishPlay();
4360b530f1050150bb751ae642d5a9dce34141d9475Andreas Huber
437ad0d97c7cf620e96a0b088dd9461645a3f8900b7Andreas Huber                if (mState == ABOUT_TO_PLAY) {
438ad0d97c7cf620e96a0b088dd9461645a3f8900b7Andreas Huber                    mState = PLAYING;
439ad0d97c7cf620e96a0b088dd9461645a3f8900b7Andreas Huber                }
44096fc6cc65ca93009a759a3a874b82a35771b9714Andreas Huber            } else if (what == PlaybackSession::kWhatSessionDestroyed) {
44196fc6cc65ca93009a759a3a874b82a35771b9714Andreas Huber                disconnectClient2();
442c92bed3a73c06e90217f8f199ca0b517aa7595d2Andreas Huber            } else {
443c92bed3a73c06e90217f8f199ca0b517aa7595d2Andreas Huber                CHECK_EQ(what, PlaybackSession::kWhatBinaryData);
444d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber
445c92bed3a73c06e90217f8f199ca0b517aa7595d2Andreas Huber                int32_t channel;
446c92bed3a73c06e90217f8f199ca0b517aa7595d2Andreas Huber                CHECK(msg->findInt32("channel", &channel));
447d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber
448c92bed3a73c06e90217f8f199ca0b517aa7595d2Andreas Huber                sp<ABuffer> data;
449c92bed3a73c06e90217f8f199ca0b517aa7595d2Andreas Huber                CHECK(msg->findBuffer("data", &data));
450d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber
451c92bed3a73c06e90217f8f199ca0b517aa7595d2Andreas Huber                CHECK_LE(channel, 0xffu);
452c92bed3a73c06e90217f8f199ca0b517aa7595d2Andreas Huber                CHECK_LE(data->size(), 0xffffu);
453d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber
454c92bed3a73c06e90217f8f199ca0b517aa7595d2Andreas Huber                int32_t sessionID;
455c92bed3a73c06e90217f8f199ca0b517aa7595d2Andreas Huber                CHECK(msg->findInt32("sessionID", &sessionID));
456d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber
457c92bed3a73c06e90217f8f199ca0b517aa7595d2Andreas Huber                char header[4];
458c92bed3a73c06e90217f8f199ca0b517aa7595d2Andreas Huber                header[0] = '$';
459c92bed3a73c06e90217f8f199ca0b517aa7595d2Andreas Huber                header[1] = channel;
460c92bed3a73c06e90217f8f199ca0b517aa7595d2Andreas Huber                header[2] = data->size() >> 8;
461c92bed3a73c06e90217f8f199ca0b517aa7595d2Andreas Huber                header[3] = data->size() & 0xff;
462d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber
463c92bed3a73c06e90217f8f199ca0b517aa7595d2Andreas Huber                mNetSession->sendRequest(
464c92bed3a73c06e90217f8f199ca0b517aa7595d2Andreas Huber                        sessionID, header, sizeof(header));
465d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber
466c92bed3a73c06e90217f8f199ca0b517aa7595d2Andreas Huber                mNetSession->sendRequest(
467c92bed3a73c06e90217f8f199ca0b517aa7595d2Andreas Huber                        sessionID, data->data(), data->size());
468d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber            }
469d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber            break;
470d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber        }
471d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber
472b6777017a68ed473d61cc9d6e77c34fd5cd301ccAndreas Huber        case kWhatKeepAlive:
473b6777017a68ed473d61cc9d6e77c34fd5cd301ccAndreas Huber        {
474b6777017a68ed473d61cc9d6e77c34fd5cd301ccAndreas Huber            int32_t sessionID;
475b6777017a68ed473d61cc9d6e77c34fd5cd301ccAndreas Huber            CHECK(msg->findInt32("sessionID", &sessionID));
476b6777017a68ed473d61cc9d6e77c34fd5cd301ccAndreas Huber
477c92bed3a73c06e90217f8f199ca0b517aa7595d2Andreas Huber            if (mClientSessionID != sessionID) {
478b6777017a68ed473d61cc9d6e77c34fd5cd301ccAndreas Huber                // Obsolete event, client is already gone.
479b6777017a68ed473d61cc9d6e77c34fd5cd301ccAndreas Huber                break;
480b6777017a68ed473d61cc9d6e77c34fd5cd301ccAndreas Huber            }
481b6777017a68ed473d61cc9d6e77c34fd5cd301ccAndreas Huber
482b6777017a68ed473d61cc9d6e77c34fd5cd301ccAndreas Huber            sendM16(sessionID);
483b6777017a68ed473d61cc9d6e77c34fd5cd301ccAndreas Huber            break;
484b6777017a68ed473d61cc9d6e77c34fd5cd301ccAndreas Huber        }
485b6777017a68ed473d61cc9d6e77c34fd5cd301ccAndreas Huber
486ad0d97c7cf620e96a0b088dd9461645a3f8900b7Andreas Huber        case kWhatTeardownTriggerTimedOut:
487ad0d97c7cf620e96a0b088dd9461645a3f8900b7Andreas Huber        {
488ad0d97c7cf620e96a0b088dd9461645a3f8900b7Andreas Huber            if (mState == AWAITING_CLIENT_TEARDOWN) {
489ad0d97c7cf620e96a0b088dd9461645a3f8900b7Andreas Huber                ALOGI("TEARDOWN trigger timed out, forcing disconnection.");
490ad0d97c7cf620e96a0b088dd9461645a3f8900b7Andreas Huber
491ad0d97c7cf620e96a0b088dd9461645a3f8900b7Andreas Huber                CHECK_NE(mStopReplyID, 0);
492ad0d97c7cf620e96a0b088dd9461645a3f8900b7Andreas Huber                finishStop();
493ad0d97c7cf620e96a0b088dd9461645a3f8900b7Andreas Huber                break;
494ad0d97c7cf620e96a0b088dd9461645a3f8900b7Andreas Huber            }
495ad0d97c7cf620e96a0b088dd9461645a3f8900b7Andreas Huber            break;
496ad0d97c7cf620e96a0b088dd9461645a3f8900b7Andreas Huber        }
497ad0d97c7cf620e96a0b088dd9461645a3f8900b7Andreas Huber
498b8c7bd418f0ee5b88923b0e0817e3a4acc53cf8dAndreas Huber        case kWhatHDCPNotify:
499b8c7bd418f0ee5b88923b0e0817e3a4acc53cf8dAndreas Huber        {
500b8c7bd418f0ee5b88923b0e0817e3a4acc53cf8dAndreas Huber            int32_t msgCode, ext1, ext2;
501b8c7bd418f0ee5b88923b0e0817e3a4acc53cf8dAndreas Huber            CHECK(msg->findInt32("msg", &msgCode));
502b8c7bd418f0ee5b88923b0e0817e3a4acc53cf8dAndreas Huber            CHECK(msg->findInt32("ext1", &ext1));
503b8c7bd418f0ee5b88923b0e0817e3a4acc53cf8dAndreas Huber            CHECK(msg->findInt32("ext2", &ext2));
504b8c7bd418f0ee5b88923b0e0817e3a4acc53cf8dAndreas Huber
505eb11600a248cfe5b95ddd3e5aaae02bd2ab65276Andreas Huber            ALOGI("Saw HDCP notification code %d, ext1 %d, ext2 %d",
506b8c7bd418f0ee5b88923b0e0817e3a4acc53cf8dAndreas Huber                    msgCode, ext1, ext2);
507b8c7bd418f0ee5b88923b0e0817e3a4acc53cf8dAndreas Huber
508b8c7bd418f0ee5b88923b0e0817e3a4acc53cf8dAndreas Huber            switch (msgCode) {
509b8c7bd418f0ee5b88923b0e0817e3a4acc53cf8dAndreas Huber                case HDCPModule::HDCP_INITIALIZATION_COMPLETE:
510b8c7bd418f0ee5b88923b0e0817e3a4acc53cf8dAndreas Huber                {
511b8c7bd418f0ee5b88923b0e0817e3a4acc53cf8dAndreas Huber                    mHDCPInitializationComplete = true;
512b8c7bd418f0ee5b88923b0e0817e3a4acc53cf8dAndreas Huber
513b8c7bd418f0ee5b88923b0e0817e3a4acc53cf8dAndreas Huber                    if (mSetupTriggerDeferred) {
514b8c7bd418f0ee5b88923b0e0817e3a4acc53cf8dAndreas Huber                        mSetupTriggerDeferred = false;
515b8c7bd418f0ee5b88923b0e0817e3a4acc53cf8dAndreas Huber
5165131d127a042ee88f903370be88845dc8c9f8578Andreas Huber                        sendTrigger(mClientSessionID, TRIGGER_SETUP);
517b8c7bd418f0ee5b88923b0e0817e3a4acc53cf8dAndreas Huber                    }
518b8c7bd418f0ee5b88923b0e0817e3a4acc53cf8dAndreas Huber                    break;
519b8c7bd418f0ee5b88923b0e0817e3a4acc53cf8dAndreas Huber                }
520b8c7bd418f0ee5b88923b0e0817e3a4acc53cf8dAndreas Huber
521eb11600a248cfe5b95ddd3e5aaae02bd2ab65276Andreas Huber                case HDCPModule::HDCP_SHUTDOWN_COMPLETE:
522ef7d3793fa9bbfb25253626ede9a020ee9280a17Andreas Huber                case HDCPModule::HDCP_SHUTDOWN_FAILED:
523eb11600a248cfe5b95ddd3e5aaae02bd2ab65276Andreas Huber                {
524ef7d3793fa9bbfb25253626ede9a020ee9280a17Andreas Huber                    // Ugly hack to make sure that the call to
525ef7d3793fa9bbfb25253626ede9a020ee9280a17Andreas Huber                    // HDCPObserver::notify is completely handled before
526ef7d3793fa9bbfb25253626ede9a020ee9280a17Andreas Huber                    // we clear the HDCP instance and unload the shared
527ef7d3793fa9bbfb25253626ede9a020ee9280a17Andreas Huber                    // library :(
528ef7d3793fa9bbfb25253626ede9a020ee9280a17Andreas Huber                    (new AMessage(kWhatFinishStop2, id()))->post(300000ll);
529eb11600a248cfe5b95ddd3e5aaae02bd2ab65276Andreas Huber                    break;
530eb11600a248cfe5b95ddd3e5aaae02bd2ab65276Andreas Huber                }
531eb11600a248cfe5b95ddd3e5aaae02bd2ab65276Andreas Huber
532b8c7bd418f0ee5b88923b0e0817e3a4acc53cf8dAndreas Huber                default:
533b8c7bd418f0ee5b88923b0e0817e3a4acc53cf8dAndreas Huber                {
534ea4bbfdcad9478ea19257fb19a32de68a2dfd958Andreas Huber                    ALOGE("HDCP failure, shutting down.");
535ea4bbfdcad9478ea19257fb19a32de68a2dfd958Andreas Huber
536ef7d3793fa9bbfb25253626ede9a020ee9280a17Andreas Huber                    mClient->onDisplayError(
537ef7d3793fa9bbfb25253626ede9a020ee9280a17Andreas Huber                            IRemoteDisplayClient::kDisplayErrorUnknown);
538b8c7bd418f0ee5b88923b0e0817e3a4acc53cf8dAndreas Huber                    break;
539b8c7bd418f0ee5b88923b0e0817e3a4acc53cf8dAndreas Huber                }
540b8c7bd418f0ee5b88923b0e0817e3a4acc53cf8dAndreas Huber            }
541b8c7bd418f0ee5b88923b0e0817e3a4acc53cf8dAndreas Huber            break;
542b8c7bd418f0ee5b88923b0e0817e3a4acc53cf8dAndreas Huber        }
543ef7d3793fa9bbfb25253626ede9a020ee9280a17Andreas Huber
544ef7d3793fa9bbfb25253626ede9a020ee9280a17Andreas Huber        case kWhatFinishStop2:
545ef7d3793fa9bbfb25253626ede9a020ee9280a17Andreas Huber        {
546ef7d3793fa9bbfb25253626ede9a020ee9280a17Andreas Huber            finishStop2();
547ef7d3793fa9bbfb25253626ede9a020ee9280a17Andreas Huber            break;
548ef7d3793fa9bbfb25253626ede9a020ee9280a17Andreas Huber        }
549b8c7bd418f0ee5b88923b0e0817e3a4acc53cf8dAndreas Huber
550d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber        default:
551d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber            TRESPASS();
552d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber    }
553d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber}
554d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber
555d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Hubervoid WifiDisplaySource::registerResponseHandler(
556d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber        int32_t sessionID, int32_t cseq, HandleRTSPResponseFunc func) {
557d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber    ResponseID id;
558d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber    id.mSessionID = sessionID;
559d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber    id.mCSeq = cseq;
560d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber    mResponseHandlers.add(id, func);
561d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber}
562d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber
563d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huberstatus_t WifiDisplaySource::sendM1(int32_t sessionID) {
564d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber    AString request = "OPTIONS * RTSP/1.0\r\n";
565d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber    AppendCommonResponse(&request, mNextCSeq);
566d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber
567d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber    request.append(
568d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber            "Require: org.wfa.wfd1.0\r\n"
569d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber            "\r\n");
570d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber
571d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber    status_t err =
572d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber        mNetSession->sendRequest(sessionID, request.c_str(), request.size());
573d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber
574d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber    if (err != OK) {
575d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber        return err;
576d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber    }
577d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber
578d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber    registerResponseHandler(
579d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber            sessionID, mNextCSeq, &WifiDisplaySource::onReceiveM1Response);
580d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber
581d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber    ++mNextCSeq;
582d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber
583d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber    return OK;
584d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber}
585d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber
586d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huberstatus_t WifiDisplaySource::sendM3(int32_t sessionID) {
587d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber    AString body =
588b8c7bd418f0ee5b88923b0e0817e3a4acc53cf8dAndreas Huber        "wfd_content_protection\r\n"
589d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber        "wfd_video_formats\r\n"
590d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber        "wfd_audio_codecs\r\n"
591d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber        "wfd_client_rtp_ports\r\n";
592d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber
593d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber    AString request = "GET_PARAMETER rtsp://localhost/wfd1.0 RTSP/1.0\r\n";
594d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber    AppendCommonResponse(&request, mNextCSeq);
595d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber
596d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber    request.append("Content-Type: text/parameters\r\n");
597d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber    request.append(StringPrintf("Content-Length: %d\r\n", body.size()));
598d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber    request.append("\r\n");
599d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber    request.append(body);
600d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber
601d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber    status_t err =
602d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber        mNetSession->sendRequest(sessionID, request.c_str(), request.size());
603d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber
604d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber    if (err != OK) {
605d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber        return err;
606d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber    }
607d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber
608d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber    registerResponseHandler(
609d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber            sessionID, mNextCSeq, &WifiDisplaySource::onReceiveM3Response);
610d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber
611d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber    ++mNextCSeq;
612d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber
613d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber    return OK;
614d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber}
615d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber
616d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huberstatus_t WifiDisplaySource::sendM4(int32_t sessionID) {
617c92bed3a73c06e90217f8f199ca0b517aa7595d2Andreas Huber    CHECK_EQ(sessionID, mClientSessionID);
618d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber
61994a483bf2bd699275673d9cd57cb125d48572f30Andreas Huber    AString body;
62094a483bf2bd699275673d9cd57cb125d48572f30Andreas Huber
62194a483bf2bd699275673d9cd57cb125d48572f30Andreas Huber    if (mSinkSupportsVideo) {
62294a483bf2bd699275673d9cd57cb125d48572f30Andreas Huber        body.append("wfd_video_formats: ");
62394a483bf2bd699275673d9cd57cb125d48572f30Andreas Huber
62494a483bf2bd699275673d9cd57cb125d48572f30Andreas Huber        VideoFormats chosenVideoFormat;
62594a483bf2bd699275673d9cd57cb125d48572f30Andreas Huber        chosenVideoFormat.disableAll();
62694a483bf2bd699275673d9cd57cb125d48572f30Andreas Huber        chosenVideoFormat.setNativeResolution(
62794a483bf2bd699275673d9cd57cb125d48572f30Andreas Huber                mChosenVideoResolutionType, mChosenVideoResolutionIndex);
628308bcaa44e578279e61be32b572fdb0b11b1e4c7Chong Zhang        chosenVideoFormat.setProfileLevel(
629308bcaa44e578279e61be32b572fdb0b11b1e4c7Chong Zhang                mChosenVideoResolutionType, mChosenVideoResolutionIndex,
630308bcaa44e578279e61be32b572fdb0b11b1e4c7Chong Zhang                mChosenVideoProfile, mChosenVideoLevel);
63194a483bf2bd699275673d9cd57cb125d48572f30Andreas Huber
6325abf87f9af48149972eeb851ecaea679911da040Andreas Huber        body.append(chosenVideoFormat.getFormatSpec(true /* forM4Message */));
63394a483bf2bd699275673d9cd57cb125d48572f30Andreas Huber        body.append("\r\n");
63494a483bf2bd699275673d9cd57cb125d48572f30Andreas Huber    }
63594a483bf2bd699275673d9cd57cb125d48572f30Andreas Huber
63694a483bf2bd699275673d9cd57cb125d48572f30Andreas Huber    if (mSinkSupportsAudio) {
63794a483bf2bd699275673d9cd57cb125d48572f30Andreas Huber        body.append(
63894a483bf2bd699275673d9cd57cb125d48572f30Andreas Huber                StringPrintf("wfd_audio_codecs: %s\r\n",
63994a483bf2bd699275673d9cd57cb125d48572f30Andreas Huber                             (mUsingPCMAudio
64094a483bf2bd699275673d9cd57cb125d48572f30Andreas Huber                                ? "LPCM 00000002 00" // 2 ch PCM 48kHz
64194a483bf2bd699275673d9cd57cb125d48572f30Andreas Huber                                : "AAC 00000001 00")));  // 2 ch AAC 48kHz
64294a483bf2bd699275673d9cd57cb125d48572f30Andreas Huber    }
64394a483bf2bd699275673d9cd57cb125d48572f30Andreas Huber
64494a483bf2bd699275673d9cd57cb125d48572f30Andreas Huber    body.append(
64594a483bf2bd699275673d9cd57cb125d48572f30Andreas Huber            StringPrintf(
6467cc0c29d6a7b76520ec588437ab51d5b8eac9ebcAndreas Huber                "wfd_presentation_URL: rtsp://%s/wfd1.0/streamid=0 none\r\n",
6477cc0c29d6a7b76520ec588437ab51d5b8eac9ebcAndreas Huber                mClientInfo.mLocalIP.c_str()));
6487cc0c29d6a7b76520ec588437ab51d5b8eac9ebcAndreas Huber
649aef5c98cd3f67e0209e1fa28489078e9f40d6f46Chong Zhang    body.append(
650aef5c98cd3f67e0209e1fa28489078e9f40d6f46Chong Zhang            StringPrintf(
651aef5c98cd3f67e0209e1fa28489078e9f40d6f46Chong Zhang                "wfd_client_rtp_ports: %s\r\n", mWfdClientRtpPorts.c_str()));
652d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber
653d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber    AString request = "SET_PARAMETER rtsp://localhost/wfd1.0 RTSP/1.0\r\n";
654d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber    AppendCommonResponse(&request, mNextCSeq);
655d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber
656d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber    request.append("Content-Type: text/parameters\r\n");
657d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber    request.append(StringPrintf("Content-Length: %d\r\n", body.size()));
658d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber    request.append("\r\n");
659d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber    request.append(body);
660d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber
661d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber    status_t err =
662d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber        mNetSession->sendRequest(sessionID, request.c_str(), request.size());
663d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber
664d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber    if (err != OK) {
665d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber        return err;
666d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber    }
667d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber
668d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber    registerResponseHandler(
669d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber            sessionID, mNextCSeq, &WifiDisplaySource::onReceiveM4Response);
670d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber
671d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber    ++mNextCSeq;
672d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber
673d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber    return OK;
674d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber}
675d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber
6765131d127a042ee88f903370be88845dc8c9f8578Andreas Huberstatus_t WifiDisplaySource::sendTrigger(
6775131d127a042ee88f903370be88845dc8c9f8578Andreas Huber        int32_t sessionID, TriggerType triggerType) {
678ea4bbfdcad9478ea19257fb19a32de68a2dfd958Andreas Huber    AString body = "wfd_trigger_method: ";
6795131d127a042ee88f903370be88845dc8c9f8578Andreas Huber    switch (triggerType) {
6805131d127a042ee88f903370be88845dc8c9f8578Andreas Huber        case TRIGGER_SETUP:
6815131d127a042ee88f903370be88845dc8c9f8578Andreas Huber            body.append("SETUP");
6825131d127a042ee88f903370be88845dc8c9f8578Andreas Huber            break;
6835131d127a042ee88f903370be88845dc8c9f8578Andreas Huber        case TRIGGER_TEARDOWN:
6845131d127a042ee88f903370be88845dc8c9f8578Andreas Huber            ALOGI("Sending TEARDOWN trigger.");
6855131d127a042ee88f903370be88845dc8c9f8578Andreas Huber            body.append("TEARDOWN");
6865131d127a042ee88f903370be88845dc8c9f8578Andreas Huber            break;
6875131d127a042ee88f903370be88845dc8c9f8578Andreas Huber        case TRIGGER_PAUSE:
6885131d127a042ee88f903370be88845dc8c9f8578Andreas Huber            body.append("PAUSE");
6895131d127a042ee88f903370be88845dc8c9f8578Andreas Huber            break;
6905131d127a042ee88f903370be88845dc8c9f8578Andreas Huber        case TRIGGER_PLAY:
6915131d127a042ee88f903370be88845dc8c9f8578Andreas Huber            body.append("PLAY");
6925131d127a042ee88f903370be88845dc8c9f8578Andreas Huber            break;
6935131d127a042ee88f903370be88845dc8c9f8578Andreas Huber        default:
6945131d127a042ee88f903370be88845dc8c9f8578Andreas Huber            TRESPASS();
695ea4bbfdcad9478ea19257fb19a32de68a2dfd958Andreas Huber    }
696ea4bbfdcad9478ea19257fb19a32de68a2dfd958Andreas Huber
697ea4bbfdcad9478ea19257fb19a32de68a2dfd958Andreas Huber    body.append("\r\n");
698d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber
699d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber    AString request = "SET_PARAMETER rtsp://localhost/wfd1.0 RTSP/1.0\r\n";
700d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber    AppendCommonResponse(&request, mNextCSeq);
701d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber
702d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber    request.append("Content-Type: text/parameters\r\n");
703d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber    request.append(StringPrintf("Content-Length: %d\r\n", body.size()));
704d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber    request.append("\r\n");
705d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber    request.append(body);
706d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber
707d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber    status_t err =
708d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber        mNetSession->sendRequest(sessionID, request.c_str(), request.size());
709d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber
710d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber    if (err != OK) {
711d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber        return err;
712d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber    }
713d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber
714d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber    registerResponseHandler(
715d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber            sessionID, mNextCSeq, &WifiDisplaySource::onReceiveM5Response);
716d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber
717d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber    ++mNextCSeq;
718d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber
719d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber    return OK;
720d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber}
721d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber
722b6777017a68ed473d61cc9d6e77c34fd5cd301ccAndreas Huberstatus_t WifiDisplaySource::sendM16(int32_t sessionID) {
723b6777017a68ed473d61cc9d6e77c34fd5cd301ccAndreas Huber    AString request = "GET_PARAMETER rtsp://localhost/wfd1.0 RTSP/1.0\r\n";
724b6777017a68ed473d61cc9d6e77c34fd5cd301ccAndreas Huber    AppendCommonResponse(&request, mNextCSeq);
725b6777017a68ed473d61cc9d6e77c34fd5cd301ccAndreas Huber
726c92bed3a73c06e90217f8f199ca0b517aa7595d2Andreas Huber    CHECK_EQ(sessionID, mClientSessionID);
727c92bed3a73c06e90217f8f199ca0b517aa7595d2Andreas Huber    request.append(
728c92bed3a73c06e90217f8f199ca0b517aa7595d2Andreas Huber            StringPrintf("Session: %d\r\n", mClientInfo.mPlaybackSessionID));
729a438123bd96c7faf145683876702387efe5628d9Andreas Huber    request.append("\r\n");  // Empty body
730b6777017a68ed473d61cc9d6e77c34fd5cd301ccAndreas Huber
731b6777017a68ed473d61cc9d6e77c34fd5cd301ccAndreas Huber    status_t err =
732b6777017a68ed473d61cc9d6e77c34fd5cd301ccAndreas Huber        mNetSession->sendRequest(sessionID, request.c_str(), request.size());
733b6777017a68ed473d61cc9d6e77c34fd5cd301ccAndreas Huber
734b6777017a68ed473d61cc9d6e77c34fd5cd301ccAndreas Huber    if (err != OK) {
735b6777017a68ed473d61cc9d6e77c34fd5cd301ccAndreas Huber        return err;
736b6777017a68ed473d61cc9d6e77c34fd5cd301ccAndreas Huber    }
737b6777017a68ed473d61cc9d6e77c34fd5cd301ccAndreas Huber
738b6777017a68ed473d61cc9d6e77c34fd5cd301ccAndreas Huber    registerResponseHandler(
739b6777017a68ed473d61cc9d6e77c34fd5cd301ccAndreas Huber            sessionID, mNextCSeq, &WifiDisplaySource::onReceiveM16Response);
740b6777017a68ed473d61cc9d6e77c34fd5cd301ccAndreas Huber
741b6777017a68ed473d61cc9d6e77c34fd5cd301ccAndreas Huber    ++mNextCSeq;
742b6777017a68ed473d61cc9d6e77c34fd5cd301ccAndreas Huber
7431ad3eb9441eb509c792c61aa0181b0e74dbe9984Chong Zhang    scheduleKeepAlive(sessionID);
7441ad3eb9441eb509c792c61aa0181b0e74dbe9984Chong Zhang
745b6777017a68ed473d61cc9d6e77c34fd5cd301ccAndreas Huber    return OK;
746b6777017a68ed473d61cc9d6e77c34fd5cd301ccAndreas Huber}
747b6777017a68ed473d61cc9d6e77c34fd5cd301ccAndreas Huber
748d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huberstatus_t WifiDisplaySource::onReceiveM1Response(
74984333e0475bc911adc16417f4ca327c975cf6c36Andreas Huber        int32_t /* sessionID */, const sp<ParsedMessage> &msg) {
750d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber    int32_t statusCode;
751d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber    if (!msg->getStatusCode(&statusCode)) {
752d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber        return ERROR_MALFORMED;
753d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber    }
754d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber
755d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber    if (statusCode != 200) {
756d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber        return ERROR_UNSUPPORTED;
757d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber    }
758d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber
759d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber    return OK;
760d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber}
761d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber
762d243c04534d1b74bd66625c5c96a9b918d8838bfAndreas Huber// sink_audio_list := ("LPCM"|"AAC"|"AC3" HEXDIGIT*8 HEXDIGIT*2)
763d243c04534d1b74bd66625c5c96a9b918d8838bfAndreas Huber//                       (", " sink_audio_list)*
764d243c04534d1b74bd66625c5c96a9b918d8838bfAndreas Huberstatic void GetAudioModes(const char *s, const char *prefix, uint32_t *modes) {
765d243c04534d1b74bd66625c5c96a9b918d8838bfAndreas Huber    *modes = 0;
766d243c04534d1b74bd66625c5c96a9b918d8838bfAndreas Huber
767d243c04534d1b74bd66625c5c96a9b918d8838bfAndreas Huber    size_t prefixLen = strlen(prefix);
768d243c04534d1b74bd66625c5c96a9b918d8838bfAndreas Huber
769d243c04534d1b74bd66625c5c96a9b918d8838bfAndreas Huber    while (*s != '0') {
770d243c04534d1b74bd66625c5c96a9b918d8838bfAndreas Huber        if (!strncmp(s, prefix, prefixLen) && s[prefixLen] == ' ') {
771d243c04534d1b74bd66625c5c96a9b918d8838bfAndreas Huber            unsigned latency;
772d243c04534d1b74bd66625c5c96a9b918d8838bfAndreas Huber            if (sscanf(&s[prefixLen + 1], "%08x %02x", modes, &latency) != 2) {
773d243c04534d1b74bd66625c5c96a9b918d8838bfAndreas Huber                *modes = 0;
774d243c04534d1b74bd66625c5c96a9b918d8838bfAndreas Huber            }
775d243c04534d1b74bd66625c5c96a9b918d8838bfAndreas Huber
776d243c04534d1b74bd66625c5c96a9b918d8838bfAndreas Huber            return;
777d243c04534d1b74bd66625c5c96a9b918d8838bfAndreas Huber        }
778d243c04534d1b74bd66625c5c96a9b918d8838bfAndreas Huber
779d243c04534d1b74bd66625c5c96a9b918d8838bfAndreas Huber        char *commaPos = strchr(s, ',');
780d243c04534d1b74bd66625c5c96a9b918d8838bfAndreas Huber        if (commaPos != NULL) {
781d243c04534d1b74bd66625c5c96a9b918d8838bfAndreas Huber            s = commaPos + 1;
782d243c04534d1b74bd66625c5c96a9b918d8838bfAndreas Huber
783d243c04534d1b74bd66625c5c96a9b918d8838bfAndreas Huber            while (isspace(*s)) {
784d243c04534d1b74bd66625c5c96a9b918d8838bfAndreas Huber                ++s;
785d243c04534d1b74bd66625c5c96a9b918d8838bfAndreas Huber            }
786d243c04534d1b74bd66625c5c96a9b918d8838bfAndreas Huber        } else {
787d243c04534d1b74bd66625c5c96a9b918d8838bfAndreas Huber            break;
788d243c04534d1b74bd66625c5c96a9b918d8838bfAndreas Huber        }
789d243c04534d1b74bd66625c5c96a9b918d8838bfAndreas Huber    }
790d243c04534d1b74bd66625c5c96a9b918d8838bfAndreas Huber}
791d243c04534d1b74bd66625c5c96a9b918d8838bfAndreas Huber
792d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huberstatus_t WifiDisplaySource::onReceiveM3Response(
793d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber        int32_t sessionID, const sp<ParsedMessage> &msg) {
794d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber    int32_t statusCode;
795d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber    if (!msg->getStatusCode(&statusCode)) {
796d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber        return ERROR_MALFORMED;
797d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber    }
798d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber
799d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber    if (statusCode != 200) {
800d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber        return ERROR_UNSUPPORTED;
801d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber    }
802d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber
803b8c7bd418f0ee5b88923b0e0817e3a4acc53cf8dAndreas Huber    sp<Parameters> params =
804b8c7bd418f0ee5b88923b0e0817e3a4acc53cf8dAndreas Huber        Parameters::Parse(msg->getContent(), strlen(msg->getContent()));
805b8c7bd418f0ee5b88923b0e0817e3a4acc53cf8dAndreas Huber
806b8c7bd418f0ee5b88923b0e0817e3a4acc53cf8dAndreas Huber    if (params == NULL) {
807b8c7bd418f0ee5b88923b0e0817e3a4acc53cf8dAndreas Huber        return ERROR_MALFORMED;
808b8c7bd418f0ee5b88923b0e0817e3a4acc53cf8dAndreas Huber    }
809b8c7bd418f0ee5b88923b0e0817e3a4acc53cf8dAndreas Huber
810b8c7bd418f0ee5b88923b0e0817e3a4acc53cf8dAndreas Huber    AString value;
811d243c04534d1b74bd66625c5c96a9b918d8838bfAndreas Huber    if (!params->findParameter("wfd_client_rtp_ports", &value)) {
812d243c04534d1b74bd66625c5c96a9b918d8838bfAndreas Huber        ALOGE("Sink doesn't report its choice of wfd_client_rtp_ports.");
813d243c04534d1b74bd66625c5c96a9b918d8838bfAndreas Huber        return ERROR_MALFORMED;
814d243c04534d1b74bd66625c5c96a9b918d8838bfAndreas Huber    }
815d243c04534d1b74bd66625c5c96a9b918d8838bfAndreas Huber
8167cc0c29d6a7b76520ec588437ab51d5b8eac9ebcAndreas Huber    unsigned port0 = 0, port1 = 0;
817d243c04534d1b74bd66625c5c96a9b918d8838bfAndreas Huber    if (sscanf(value.c_str(),
818d243c04534d1b74bd66625c5c96a9b918d8838bfAndreas Huber               "RTP/AVP/UDP;unicast %u %u mode=play",
819d243c04534d1b74bd66625c5c96a9b918d8838bfAndreas Huber               &port0,
8207cc0c29d6a7b76520ec588437ab51d5b8eac9ebcAndreas Huber               &port1) == 2
8217cc0c29d6a7b76520ec588437ab51d5b8eac9ebcAndreas Huber        || sscanf(value.c_str(),
8227cc0c29d6a7b76520ec588437ab51d5b8eac9ebcAndreas Huber               "RTP/AVP/TCP;unicast %u %u mode=play",
8237cc0c29d6a7b76520ec588437ab51d5b8eac9ebcAndreas Huber               &port0,
8247cc0c29d6a7b76520ec588437ab51d5b8eac9ebcAndreas Huber               &port1) == 2) {
8257cc0c29d6a7b76520ec588437ab51d5b8eac9ebcAndreas Huber            if (port0 == 0 || port0 > 65535 || port1 != 0) {
8267cc0c29d6a7b76520ec588437ab51d5b8eac9ebcAndreas Huber                ALOGE("Sink chose its wfd_client_rtp_ports poorly (%s)",
8277cc0c29d6a7b76520ec588437ab51d5b8eac9ebcAndreas Huber                      value.c_str());
8287cc0c29d6a7b76520ec588437ab51d5b8eac9ebcAndreas Huber
8297cc0c29d6a7b76520ec588437ab51d5b8eac9ebcAndreas Huber                return ERROR_MALFORMED;
8307cc0c29d6a7b76520ec588437ab51d5b8eac9ebcAndreas Huber            }
8317cc0c29d6a7b76520ec588437ab51d5b8eac9ebcAndreas Huber    } else if (strcmp(value.c_str(), "RTP/AVP/TCP;interleaved mode=play")) {
8327cc0c29d6a7b76520ec588437ab51d5b8eac9ebcAndreas Huber        ALOGE("Unsupported value for wfd_client_rtp_ports (%s)",
833d243c04534d1b74bd66625c5c96a9b918d8838bfAndreas Huber              value.c_str());
834d243c04534d1b74bd66625c5c96a9b918d8838bfAndreas Huber
8357cc0c29d6a7b76520ec588437ab51d5b8eac9ebcAndreas Huber        return ERROR_UNSUPPORTED;
836d243c04534d1b74bd66625c5c96a9b918d8838bfAndreas Huber    }
837d243c04534d1b74bd66625c5c96a9b918d8838bfAndreas Huber
8387cc0c29d6a7b76520ec588437ab51d5b8eac9ebcAndreas Huber    mWfdClientRtpPorts = value;
839d243c04534d1b74bd66625c5c96a9b918d8838bfAndreas Huber    mChosenRTPPort = port0;
840d243c04534d1b74bd66625c5c96a9b918d8838bfAndreas Huber
84194a483bf2bd699275673d9cd57cb125d48572f30Andreas Huber    if (!params->findParameter("wfd_video_formats", &value)) {
84294a483bf2bd699275673d9cd57cb125d48572f30Andreas Huber        ALOGE("Sink doesn't report its choice of wfd_video_formats.");
84394a483bf2bd699275673d9cd57cb125d48572f30Andreas Huber        return ERROR_MALFORMED;
84494a483bf2bd699275673d9cd57cb125d48572f30Andreas Huber    }
845