ANetworkSession.h revision bb197f84c4119651e5face418285688ddaf08ea3
1/*
2 * Copyright 2012, 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_NETWORK_SESSION_H_
18
19#define A_NETWORK_SESSION_H_
20
21#include <media/stagefright/foundation/ABase.h>
22#include <utils/KeyedVector.h>
23#include <utils/RefBase.h>
24#include <utils/Thread.h>
25
26namespace android {
27
28struct AMessage;
29
30struct ANetworkSession : public RefBase {
31    ANetworkSession();
32
33    status_t start();
34    status_t stop();
35
36    status_t createRTSPClient(
37            const char *host, unsigned port, const sp<AMessage> &notify,
38            int32_t *sessionID);
39
40    status_t createRTSPServer(
41            unsigned port, const sp<AMessage> &notify, int32_t *sessionID);
42
43    status_t createUDPSession(
44            unsigned localPort, const sp<AMessage> &notify, int32_t *sessionID);
45
46    status_t createUDPSession(
47            unsigned localPort,
48            const char *remoteHost,
49            unsigned remotePort,
50            const sp<AMessage> &notify,
51            int32_t *sessionID);
52
53    status_t connectUDPSession(
54            int32_t sessionID, const char *remoteHost, unsigned remotePort);
55
56    status_t destroySession(int32_t sessionID);
57
58    status_t sendRequest(
59            int32_t sessionID, const void *data, ssize_t size = -1);
60
61    enum NotificationReason {
62        kWhatError,
63        kWhatConnected,
64        kWhatClientConnected,
65        kWhatData,
66        kWhatDatagram,
67        kWhatBinaryData,
68    };
69
70protected:
71    virtual ~ANetworkSession();
72
73private:
74    struct NetworkThread;
75    struct Session;
76
77    Mutex mLock;
78    sp<Thread> mThread;
79
80    int32_t mNextSessionID;
81
82    int mPipeFd[2];
83
84    KeyedVector<int32_t, sp<Session> > mSessions;
85
86    enum Mode {
87        kModeCreateUDPSession,
88        kModeCreateRTSPServer,
89        kModeCreateRTSPClient,
90    };
91    status_t createClientOrServer(
92            Mode mode,
93            unsigned port,
94            const char *remoteHost,
95            unsigned remotePort,
96            const sp<AMessage> &notify,
97            int32_t *sessionID);
98
99    void threadLoop();
100    void interrupt();
101
102    static status_t MakeSocketNonBlocking(int s);
103
104    DISALLOW_EVIL_CONSTRUCTORS(ANetworkSession);
105};
106
107}  // namespace android
108
109#endif  // A_NETWORK_SESSION_H_
110