WifiDisplaySource.h revision eb11600a248cfe5b95ddd3e5aaae02bd2ab65276
1/*
2 * Copyright 2012, The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *     http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#ifndef WIFI_DISPLAY_SOURCE_H_
18
19#define WIFI_DISPLAY_SOURCE_H_
20
21#include "ANetworkSession.h"
22
23#include <media/stagefright/foundation/AHandler.h>
24
25#include <netinet/in.h>
26
27namespace android {
28
29#define REQUIRE_HDCP    0
30
31struct IHDCP;
32struct IRemoteDisplayClient;
33struct ParsedMessage;
34
35// Represents the RTSP server acting as a wifi display source.
36// Manages incoming connections, sets up Playback sessions as necessary.
37struct WifiDisplaySource : public AHandler {
38    static const unsigned kWifiDisplayDefaultPort = 7236;
39
40    WifiDisplaySource(
41            const sp<ANetworkSession> &netSession,
42            const sp<IRemoteDisplayClient> &client);
43
44    status_t start(const char *iface);
45    status_t stop();
46
47protected:
48    virtual ~WifiDisplaySource();
49    virtual void onMessageReceived(const sp<AMessage> &msg);
50
51private:
52    struct PlaybackSession;
53
54#if REQUIRE_HDCP
55    struct HDCPObserver;
56#endif
57
58    enum {
59        kWhatStart,
60        kWhatRTSPNotify,
61        kWhatStop,
62        kWhatReapDeadClients,
63        kWhatPlaybackSessionNotify,
64        kWhatKeepAlive,
65        kWhatHDCPNotify,
66    };
67
68    struct ResponseID {
69        int32_t mSessionID;
70        int32_t mCSeq;
71
72        bool operator<(const ResponseID &other) const {
73            return mSessionID < other.mSessionID
74                || (mSessionID == other.mSessionID
75                        && mCSeq < other.mCSeq);
76        }
77    };
78
79    typedef status_t (WifiDisplaySource::*HandleRTSPResponseFunc)(
80            int32_t sessionID, const sp<ParsedMessage> &msg);
81
82    static const int64_t kReaperIntervalUs = 1000000ll;
83
84    static const int64_t kPlaybackSessionTimeoutSecs = 30;
85
86    static const int64_t kPlaybackSessionTimeoutUs =
87        kPlaybackSessionTimeoutSecs * 1000000ll;
88
89    sp<ANetworkSession> mNetSession;
90    sp<IRemoteDisplayClient> mClient;
91    struct in_addr mInterfaceAddr;
92    int32_t mSessionID;
93
94    uint32_t mStopReplyID;
95
96    int32_t mClientSessionID;
97
98    struct ClientInfo {
99        AString mRemoteIP;
100        AString mLocalIP;
101        int32_t mLocalPort;
102        int32_t mPlaybackSessionID;
103        sp<PlaybackSession> mPlaybackSession;
104    };
105    ClientInfo mClientInfo;
106
107    bool mReaperPending;
108
109    int32_t mNextCSeq;
110
111    KeyedVector<ResponseID, HandleRTSPResponseFunc> mResponseHandlers;
112
113#if REQUIRE_HDCP
114    bool mIsHDCP2_0;
115    int32_t mHDCPPort;
116    sp<IHDCP> mHDCP;
117    sp<HDCPObserver> mHDCPObserver;
118
119    bool mHDCPInitializationComplete;
120    bool mSetupTriggerDeferred;
121
122    status_t makeHDCP();
123#endif
124
125    status_t sendM1(int32_t sessionID);
126    status_t sendM3(int32_t sessionID);
127    status_t sendM4(int32_t sessionID);
128    status_t sendM5(int32_t sessionID, bool requestShutdown);
129    status_t sendM16(int32_t sessionID);
130
131    status_t onReceiveM1Response(
132            int32_t sessionID, const sp<ParsedMessage> &msg);
133
134    status_t onReceiveM3Response(
135            int32_t sessionID, const sp<ParsedMessage> &msg);
136
137    status_t onReceiveM4Response(
138            int32_t sessionID, const sp<ParsedMessage> &msg);
139
140    status_t onReceiveM5Response(
141            int32_t sessionID, const sp<ParsedMessage> &msg);
142
143    status_t onReceiveM16Response(
144            int32_t sessionID, const sp<ParsedMessage> &msg);
145
146    void registerResponseHandler(
147            int32_t sessionID, int32_t cseq, HandleRTSPResponseFunc func);
148
149    status_t onReceiveClientData(const sp<AMessage> &msg);
150
151    status_t onOptionsRequest(
152            int32_t sessionID,
153            int32_t cseq,
154            const sp<ParsedMessage> &data);
155
156    status_t onSetupRequest(
157            int32_t sessionID,
158            int32_t cseq,
159            const sp<ParsedMessage> &data);
160
161    status_t onPlayRequest(
162            int32_t sessionID,
163            int32_t cseq,
164            const sp<ParsedMessage> &data);
165
166    status_t onPauseRequest(
167            int32_t sessionID,
168            int32_t cseq,
169            const sp<ParsedMessage> &data);
170
171    status_t onTeardownRequest(
172            int32_t sessionID,
173            int32_t cseq,
174            const sp<ParsedMessage> &data);
175
176    status_t onGetParameterRequest(
177            int32_t sessionID,
178            int32_t cseq,
179            const sp<ParsedMessage> &data);
180
181    status_t onSetParameterRequest(
182            int32_t sessionID,
183            int32_t cseq,
184            const sp<ParsedMessage> &data);
185
186    void sendErrorResponse(
187            int32_t sessionID,
188            const char *errorDetail,
189            int32_t cseq);
190
191    static void AppendCommonResponse(
192            AString *response, int32_t cseq, int32_t playbackSessionID = -1ll);
193
194    void scheduleReaper();
195    void scheduleKeepAlive(int32_t sessionID);
196
197    int32_t makeUniquePlaybackSessionID() const;
198
199    sp<PlaybackSession> findPlaybackSession(
200            const sp<ParsedMessage> &data, int32_t *playbackSessionID) const;
201
202    // Disconnects the current client and shuts down its playback session
203    // (if any). The reason for the disconnection is OK for orderly shutdown
204    // or a nonzero error code.
205    // A listener is notified accordingly.
206    void disconnectClient(status_t err);
207
208    void finishStop();
209    void finishStop2();
210
211    DISALLOW_EVIL_CONSTRUCTORS(WifiDisplaySource);
212};
213
214}  // namespace android
215
216#endif  // WIFI_DISPLAY_SOURCE_H_
217