WifiDisplaySink.h revision fbe9d81ff5fbdc5aecdcdd13e4a5d7f019824f96
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_SINK_H_
18
19#define WIFI_DISPLAY_SINK_H_
20
21#include "ANetworkSession.h"
22
23#include <gui/Surface.h>
24#include <media/stagefright/foundation/AHandler.h>
25
26namespace android {
27
28struct ParsedMessage;
29struct RTPSink;
30
31// Represents the RTSP client acting as a wifi display sink.
32// Connects to a wifi display source and renders the incoming
33// transport stream using a MediaPlayer instance.
34struct WifiDisplaySink : public AHandler {
35    WifiDisplaySink(
36            const sp<ANetworkSession> &netSession,
37            const sp<ISurfaceTexture> &surfaceTex = NULL);
38
39    void start(const char *sourceHost, int32_t sourcePort);
40    void start(const char *uri);
41
42protected:
43    virtual ~WifiDisplaySink();
44    virtual void onMessageReceived(const sp<AMessage> &msg);
45
46private:
47    enum State {
48        UNDEFINED,
49        CONNECTING,
50        CONNECTED,
51        PAUSED,
52        PLAYING,
53    };
54
55    enum {
56        kWhatStart,
57        kWhatRTSPNotify,
58        kWhatStop,
59    };
60
61    struct ResponseID {
62        int32_t mSessionID;
63        int32_t mCSeq;
64
65        bool operator<(const ResponseID &other) const {
66            return mSessionID < other.mSessionID
67                || (mSessionID == other.mSessionID
68                        && mCSeq < other.mCSeq);
69        }
70    };
71
72    typedef status_t (WifiDisplaySink::*HandleRTSPResponseFunc)(
73            int32_t sessionID, const sp<ParsedMessage> &msg);
74
75    static const bool sUseTCPInterleaving = false;
76
77    State mState;
78    sp<ANetworkSession> mNetSession;
79    sp<ISurfaceTexture> mSurfaceTex;
80    AString mSetupURI;
81    AString mRTSPHost;
82    int32_t mSessionID;
83
84    int32_t mNextCSeq;
85
86    KeyedVector<ResponseID, HandleRTSPResponseFunc> mResponseHandlers;
87
88    sp<RTPSink> mRTPSink;
89    AString mPlaybackSessionID;
90    int32_t mPlaybackSessionTimeoutSecs;
91
92    status_t sendM2(int32_t sessionID);
93    status_t sendDescribe(int32_t sessionID, const char *uri);
94    status_t sendSetup(int32_t sessionID, const char *uri);
95    status_t sendPlay(int32_t sessionID, const char *uri);
96
97    status_t onReceiveM2Response(
98            int32_t sessionID, const sp<ParsedMessage> &msg);
99
100    status_t onReceiveDescribeResponse(
101            int32_t sessionID, const sp<ParsedMessage> &msg);
102
103    status_t onReceiveSetupResponse(
104            int32_t sessionID, const sp<ParsedMessage> &msg);
105
106    status_t configureTransport(const sp<ParsedMessage> &msg);
107
108    status_t onReceivePlayResponse(
109            int32_t sessionID, const sp<ParsedMessage> &msg);
110
111    void registerResponseHandler(
112            int32_t sessionID, int32_t cseq, HandleRTSPResponseFunc func);
113
114    void onReceiveClientData(const sp<AMessage> &msg);
115
116    void onOptionsRequest(
117            int32_t sessionID,
118            int32_t cseq,
119            const sp<ParsedMessage> &data);
120
121    void onGetParameterRequest(
122            int32_t sessionID,
123            int32_t cseq,
124            const sp<ParsedMessage> &data);
125
126    void onSetParameterRequest(
127            int32_t sessionID,
128            int32_t cseq,
129            const sp<ParsedMessage> &data);
130
131    void sendErrorResponse(
132            int32_t sessionID,
133            const char *errorDetail,
134            int32_t cseq);
135
136    static void AppendCommonResponse(AString *response, int32_t cseq);
137
138    bool ParseURL(
139            const char *url, AString *host, int32_t *port, AString *path,
140            AString *user, AString *pass);
141
142    DISALLOW_EVIL_CONSTRUCTORS(WifiDisplaySink);
143};
144
145}  // namespace android
146
147#endif  // WIFI_DISPLAY_SINK_H_
148