ARTSPConnection.h revision cf7b9c7aae758ac0b99833915053c63c2ac46e09
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
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
43protected:
44    virtual ~ARTSPConnection();
45    virtual void onMessageReceived(const sp<AMessage> &msg);
46
47private:
48    enum State {
49        DISCONNECTED,
50        CONNECTING,
51        CONNECTED,
52    };
53
54    enum {
55        kWhatConnect            = 'conn',
56        kWhatDisconnect         = 'disc',
57        kWhatCompleteConnection = 'comc',
58        kWhatSendRequest        = 'sreq',
59        kWhatReceiveResponse    = 'rres',
60    };
61
62    static const int64_t kSelectTimeoutUs;
63
64    State mState;
65    int mSocket;
66    int32_t mConnectionID;
67    int32_t mNextCSeq;
68    bool mReceiveResponseEventPending;
69
70    KeyedVector<int32_t, sp<AMessage> > mPendingRequests;
71
72    void onConnect(const sp<AMessage> &msg);
73    void onDisconnect(const sp<AMessage> &msg);
74    void onCompleteConnection(const sp<AMessage> &msg);
75    void onSendRequest(const sp<AMessage> &msg);
76    void onReceiveResponse();
77
78    void flushPendingRequests();
79    void postReceiveReponseEvent();
80
81    // Return false iff something went unrecoverably wrong.
82    bool receiveRTSPReponse();
83    bool receiveLine(AString *line);
84    bool notifyResponseListener(const sp<ARTSPResponse> &response);
85
86    static bool ParseURL(
87            const char *url, AString *host, unsigned *port, AString *path);
88
89    static bool ParseSingleUnsignedLong(
90            const char *from, unsigned long *x);
91
92    DISALLOW_EVIL_CONSTRUCTORS(ARTSPConnection);
93};
94
95}  // namespace android
96
97#endif  // A_RTSP_CONNECTION_H_
98