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 
getSocketARTSPConnection49     int getSocket() { return mSocket; }
50 
51 protected:
52     virtual ~ARTSPConnection();
53     virtual void onMessageReceived(const sp<AMessage> &msg);
54 
55 private:
56     enum State {
57         DISCONNECTED,
58         CONNECTING,
59         CONNECTED,
60     };
61 
62     enum {
63         kWhatConnect            = 'conn',
64         kWhatDisconnect         = 'disc',
65         kWhatCompleteConnection = 'comc',
66         kWhatSendRequest        = 'sreq',
67         kWhatReceiveResponse    = 'rres',
68         kWhatObserveBinaryData  = 'obin',
69     };
70 
71     enum AuthType {
72         NONE,
73         BASIC,
74         DIGEST
75     };
76 
77     static const int64_t kSelectTimeoutUs;
78 
79     static const AString sUserAgent;
80 
81     bool mUIDValid;
82     uid_t mUID;
83     State mState;
84     AString mUser, mPass;
85     AuthType mAuthType;
86     AString mNonce;
87     AString mRealm;
88     int mSocket;
89     int32_t mConnectionID;
90     int32_t mNextCSeq;
91     bool mReceiveResponseEventPending;
92 
93     KeyedVector<int32_t, sp<AMessage> > mPendingRequests;
94 
95     sp<AMessage> mObserveBinaryMessage;
96 
97     void performDisconnect();
98 
99     void onConnect(const sp<AMessage> &msg);
100     void onDisconnect(const sp<AMessage> &msg);
101     void onCompleteConnection(const sp<AMessage> &msg);
102     void onSendRequest(const sp<AMessage> &msg);
103     void onReceiveResponse();
104 
105     void flushPendingRequests();
106     void postReceiveReponseEvent();
107 
108     // Return false iff something went unrecoverably wrong.
109     bool receiveRTSPReponse();
110     status_t receive(void *data, size_t size);
111     bool receiveLine(AString *line);
112     sp<ABuffer> receiveBinaryData();
113     bool notifyResponseListener(const sp<ARTSPResponse> &response);
114 
115     bool parseAuthMethod(const sp<ARTSPResponse> &response);
116     void addAuthentication(AString *request);
117 
118     void addUserAgent(AString *request) const;
119 
120     status_t findPendingRequest(
121             const sp<ARTSPResponse> &response, ssize_t *index) const;
122 
123     bool handleServerRequest(const sp<ARTSPResponse> &request);
124 
125     static bool ParseSingleUnsignedLong(
126             const char *from, unsigned long *x);
127 
128     DISALLOW_EVIL_CONSTRUCTORS(ARTSPConnection);
129 };
130 
131 }  // namespace android
132 
133 #endif  // A_RTSP_CONNECTION_H_
134