WifiDisplaySource.h revision b6777017a68ed473d61cc9d6e77c34fd5cd301cc
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
25namespace android {
26
27struct ParsedMessage;
28
29// Represents the RTSP server acting as a wifi display source.
30// Manages incoming connections, sets up Playback sessions as necessary.
31struct WifiDisplaySource : public AHandler {
32    static const unsigned kWifiDisplayDefaultPort = 7236;
33
34    WifiDisplaySource(const sp<ANetworkSession> &netSession);
35
36    status_t start(const char *iface);
37    status_t stop();
38
39protected:
40    virtual ~WifiDisplaySource();
41    virtual void onMessageReceived(const sp<AMessage> &msg);
42
43private:
44    struct PlaybackSession;
45
46    enum {
47        kWhatStart,
48        kWhatRTSPNotify,
49        kWhatStop,
50        kWhatReapDeadClients,
51        kWhatPlaybackSessionNotify,
52        kWhatKeepAlive,
53    };
54
55    struct ResponseID {
56        int32_t mSessionID;
57        int32_t mCSeq;
58
59        bool operator<(const ResponseID &other) const {
60            return mSessionID < other.mSessionID
61                || (mSessionID == other.mSessionID
62                        && mCSeq < other.mCSeq);
63        }
64    };
65
66    typedef status_t (WifiDisplaySource::*HandleRTSPResponseFunc)(
67            int32_t sessionID, const sp<ParsedMessage> &msg);
68
69    static const int64_t kReaperIntervalUs = 1000000ll;
70
71    static const int64_t kPlaybackSessionTimeoutSecs = 30;
72
73    static const int64_t kPlaybackSessionTimeoutUs =
74        kPlaybackSessionTimeoutSecs * 1000000ll;
75
76    sp<ANetworkSession> mNetSession;
77    int32_t mSessionID;
78
79    struct ClientInfo {
80        AString mRemoteIP;
81        AString mLocalIP;
82        int32_t mLocalPort;
83        int32_t mPlaybackSessionID;
84    };
85    // by sessionID.
86    KeyedVector<int32_t, ClientInfo> mClientInfos;
87
88    bool mReaperPending;
89
90    int32_t mNextCSeq;
91
92    KeyedVector<ResponseID, HandleRTSPResponseFunc> mResponseHandlers;
93
94    KeyedVector<int32_t, sp<PlaybackSession> > mPlaybackSessions;
95
96    status_t sendM1(int32_t sessionID);
97    status_t sendM3(int32_t sessionID);
98    status_t sendM4(int32_t sessionID);
99    status_t sendM5(int32_t sessionID);
100    status_t sendM16(int32_t sessionID);
101
102    status_t onReceiveM1Response(
103            int32_t sessionID, const sp<ParsedMessage> &msg);
104
105    status_t onReceiveM3Response(
106            int32_t sessionID, const sp<ParsedMessage> &msg);
107
108    status_t onReceiveM4Response(
109            int32_t sessionID, const sp<ParsedMessage> &msg);
110
111    status_t onReceiveM5Response(
112            int32_t sessionID, const sp<ParsedMessage> &msg);
113
114    status_t onReceiveM16Response(
115            int32_t sessionID, const sp<ParsedMessage> &msg);
116
117    void registerResponseHandler(
118            int32_t sessionID, int32_t cseq, HandleRTSPResponseFunc func);
119
120    void onReceiveClientData(const sp<AMessage> &msg);
121
122    void onDescribeRequest(
123            int32_t sessionID,
124            int32_t cseq,
125            const sp<ParsedMessage> &data);
126
127    void onOptionsRequest(
128            int32_t sessionID,
129            int32_t cseq,
130            const sp<ParsedMessage> &data);
131
132    void onSetupRequest(
133            int32_t sessionID,
134            int32_t cseq,
135            const sp<ParsedMessage> &data);
136
137    void onPlayRequest(
138            int32_t sessionID,
139            int32_t cseq,
140            const sp<ParsedMessage> &data);
141
142    void onPauseRequest(
143            int32_t sessionID,
144            int32_t cseq,
145            const sp<ParsedMessage> &data);
146
147    void onTeardownRequest(
148            int32_t sessionID,
149            int32_t cseq,
150            const sp<ParsedMessage> &data);
151
152    void onGetParameterRequest(
153            int32_t sessionID,
154            int32_t cseq,
155            const sp<ParsedMessage> &data);
156
157    void onSetParameterRequest(
158            int32_t sessionID,
159            int32_t cseq,
160            const sp<ParsedMessage> &data);
161
162    void sendErrorResponse(
163            int32_t sessionID,
164            const char *errorDetail,
165            int32_t cseq);
166
167    static void AppendCommonResponse(
168            AString *response, int32_t cseq, int32_t playbackSessionID = -1ll);
169
170    void scheduleReaper();
171    void scheduleKeepAlive(int32_t sessionID);
172
173    int32_t makeUniquePlaybackSessionID() const;
174
175    sp<PlaybackSession> findPlaybackSession(
176            const sp<ParsedMessage> &data, int32_t *playbackSessionID) const;
177
178    DISALLOW_EVIL_CONSTRUCTORS(WifiDisplaySource);
179};
180
181}  // namespace android
182
183#endif  // WIFI_DISPLAY_SOURCE_H_
184