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