blob: ac2e3ae1b8560c37e13eda065754206c4751f56f [file] [log] [blame]
Andreas Huber7a747b82010-06-07 15:19:40 -07001/*
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
24namespace android {
25
26struct ABuffer;
27
28struct ARTSPResponse : public RefBase {
29 unsigned long mStatusCode;
30 AString mStatusLine;
31 KeyedVector<AString,AString> mHeaders;
32 sp<ABuffer> mContent;
33};
34
35struct ARTSPConnection : public AHandler {
36 ARTSPConnection();
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
Andreas Huber0416da72010-08-26 11:17:32 -070043 void observeBinaryData(const sp<AMessage> &reply);
44
Andreas Hubera0b442e2010-10-20 15:00:34 -070045 static bool ParseURL(
46 const char *url, AString *host, unsigned *port, AString *path,
47 AString *user, AString *pass);
48
Andreas Huber7a747b82010-06-07 15:19:40 -070049protected:
50 virtual ~ARTSPConnection();
51 virtual void onMessageReceived(const sp<AMessage> &msg);
52
53private:
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',
Andreas Huber0416da72010-08-26 11:17:32 -070066 kWhatObserveBinaryData = 'obin',
Andreas Huber7a747b82010-06-07 15:19:40 -070067 };
68
Andreas Hubera0b442e2010-10-20 15:00:34 -070069 enum AuthType {
70 NONE,
71 BASIC,
72 DIGEST
73 };
74
Andreas Huber7a747b82010-06-07 15:19:40 -070075 static const int64_t kSelectTimeoutUs;
76
77 State mState;
Andreas Hubera0b442e2010-10-20 15:00:34 -070078 AString mUser, mPass;
79 AuthType mAuthType;
80 AString mNonce;
Andreas Huber7a747b82010-06-07 15:19:40 -070081 int mSocket;
82 int32_t mConnectionID;
83 int32_t mNextCSeq;
84 bool mReceiveResponseEventPending;
85
86 KeyedVector<int32_t, sp<AMessage> > mPendingRequests;
87
Andreas Huber0416da72010-08-26 11:17:32 -070088 sp<AMessage> mObserveBinaryMessage;
89
Andreas Huberb0e73812011-03-29 13:34:28 -070090 AString mUserAgent;
91
Andreas Huber7a747b82010-06-07 15:19:40 -070092 void onConnect(const sp<AMessage> &msg);
93 void onDisconnect(const sp<AMessage> &msg);
94 void onCompleteConnection(const sp<AMessage> &msg);
95 void onSendRequest(const sp<AMessage> &msg);
96 void onReceiveResponse();
97
98 void flushPendingRequests();
99 void postReceiveReponseEvent();
100
101 // Return false iff something went unrecoverably wrong.
102 bool receiveRTSPReponse();
Andreas Huber0416da72010-08-26 11:17:32 -0700103 status_t receive(void *data, size_t size);
Andreas Huber7a747b82010-06-07 15:19:40 -0700104 bool receiveLine(AString *line);
Andreas Huber0416da72010-08-26 11:17:32 -0700105 sp<ABuffer> receiveBinaryData();
Andreas Huber7a747b82010-06-07 15:19:40 -0700106 bool notifyResponseListener(const sp<ARTSPResponse> &response);
107
Andreas Hubera0b442e2010-10-20 15:00:34 -0700108 bool parseAuthMethod(const sp<ARTSPResponse> &response);
109 void addAuthentication(AString *request);
110
Andreas Huberb0e73812011-03-29 13:34:28 -0700111 void addUserAgent(AString *request) const;
112
Andreas Hubera0b442e2010-10-20 15:00:34 -0700113 status_t findPendingRequest(
114 const sp<ARTSPResponse> &response, ssize_t *index) const;
Andreas Huber7a747b82010-06-07 15:19:40 -0700115
Andreas Hubere0666162011-02-16 13:20:02 -0800116 bool handleServerRequest(const sp<ARTSPResponse> &request);
117
Andreas Huber7a747b82010-06-07 15:19:40 -0700118 static bool ParseSingleUnsignedLong(
119 const char *from, unsigned long *x);
120
Andreas Huberb0e73812011-03-29 13:34:28 -0700121 static void MakeUserAgent(AString *userAgent);
122
Andreas Huber7a747b82010-06-07 15:19:40 -0700123 DISALLOW_EVIL_CONSTRUCTORS(ARTSPConnection);
124};
125
126} // namespace android
127
128#endif // A_RTSP_CONNECTION_H_