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