1 /*
2  * Copyright (C) 2010 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 A_RTSP_CONNECTION_H_
18 
19 #define A_RTSP_CONNECTION_H_
20 
21 #include <media/stagefright/foundation/AHandler.h>
22 #include <media/stagefright/foundation/AString.h>
23 
24 namespace android {
25 
26 struct ABuffer;
27 
28 struct ARTSPResponse : public RefBase {
29     unsigned long mStatusCode;
30     AString mStatusLine;
31     KeyedVector<AString,AString> mHeaders;
32     sp<ABuffer> mContent;
33 };
34 
35 struct ARTSPConnection : public AHandler {
36     explicit ARTSPConnection(bool uidValid = false, uid_t uid = 0);
37 
38     void connect(const char *url, const sp<AMessage> &reply);
39     void disconnect(const sp<AMessage> &reply);
40 
41     void sendRequest(const char *request, const sp<AMessage> &reply);
42 
43     void observeBinaryData(const sp<AMessage> &reply);
44 
45     static bool ParseURL(
46             const char *url, AString *host, unsigned *port, AString *path,
47             AString *user, AString *pass);
48 
49 protected:
50     virtual ~ARTSPConnection();
51     virtual void onMessageReceived(const sp<AMessage> &msg);
52 
53 private:
54     enum State {
55         DISCONNECTED,
56         CONNECTING,
57         CONNECTED,
58     };
59 
60     enum {
61         kWhatConnect            = 'conn',
62         kWhatDisconnect         = 'disc',
63         kWhatCompleteConnection = 'comc',
64         kWhatSendRequest        = 'sreq',
65         kWhatReceiveResponse    = 'rres',
66         kWhatObserveBinaryData  = 'obin',
67     };
68 
69     enum AuthType {
70         NONE,
71         BASIC,
72         DIGEST
73     };
74 
75     static const int64_t kSelectTimeoutUs;
76 
77     static const AString sUserAgent;
78 
79     bool mUIDValid;
80     uid_t mUID;
81     State mState;
82     AString mUser, mPass;
83     AuthType mAuthType;
84     AString mNonce;
85     AString mRealm;
86     int mSocket;
87     int32_t mConnectionID;
88     int32_t mNextCSeq;
89     bool mReceiveResponseEventPending;
90 
91     KeyedVector<int32_t, sp<AMessage> > mPendingRequests;
92 
93     sp<AMessage> mObserveBinaryMessage;
94 
95     void performDisconnect();
96 
97     void onConnect(const sp<AMessage> &msg);
98     void onDisconnect(const sp<AMessage> &msg);
99     void onCompleteConnection(const sp<AMessage> &msg);
100     void onSendRequest(const sp<AMessage> &msg);
101     void onReceiveResponse();
102 
103     void flushPendingRequests();
104     void postReceiveReponseEvent();
105 
106     // Return false iff something went unrecoverably wrong.
107     bool receiveRTSPReponse();
108     status_t receive(void *data, size_t size);
109     bool receiveLine(AString *line);
110     sp<ABuffer> receiveBinaryData();
111     bool notifyResponseListener(const sp<ARTSPResponse> &response);
112 
113     bool parseAuthMethod(const sp<ARTSPResponse> &response);
114     void addAuthentication(AString *request);
115 
116     void addUserAgent(AString *request) const;
117 
118     status_t findPendingRequest(
119             const sp<ARTSPResponse> &response, ssize_t *index) const;
120 
121     bool handleServerRequest(const sp<ARTSPResponse> &request);
122 
123     static bool ParseSingleUnsignedLong(
124             const char *from, unsigned long *x);
125 
126     DISALLOW_EVIL_CONSTRUCTORS(ARTSPConnection);
127 };
128 
129 }  // namespace android
130 
131 #endif  // A_RTSP_CONNECTION_H_
132