ANetworkSession.h revision 8060060217ff16cd67c8f6a15c649f44c343acf0
10a490665a3a287cd3aee1e7327f2381222c387c4Liubov Dmitrieva/*
20a490665a3a287cd3aee1e7327f2381222c387c4Liubov Dmitrieva * Copyright 2012, The Android Open Source Project
30a490665a3a287cd3aee1e7327f2381222c387c4Liubov Dmitrieva *
40a490665a3a287cd3aee1e7327f2381222c387c4Liubov Dmitrieva * Licensed under the Apache License, Version 2.0 (the "License");
50a490665a3a287cd3aee1e7327f2381222c387c4Liubov Dmitrieva * you may not use this file except in compliance with the License.
60a490665a3a287cd3aee1e7327f2381222c387c4Liubov Dmitrieva * You may obtain a copy of the License at
70a490665a3a287cd3aee1e7327f2381222c387c4Liubov Dmitrieva *
80a490665a3a287cd3aee1e7327f2381222c387c4Liubov Dmitrieva *     http://www.apache.org/licenses/LICENSE-2.0
90a490665a3a287cd3aee1e7327f2381222c387c4Liubov Dmitrieva *
100a490665a3a287cd3aee1e7327f2381222c387c4Liubov Dmitrieva * Unless required by applicable law or agreed to in writing, software
110a490665a3a287cd3aee1e7327f2381222c387c4Liubov Dmitrieva * distributed under the License is distributed on an "AS IS" BASIS,
120a490665a3a287cd3aee1e7327f2381222c387c4Liubov Dmitrieva * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
130a490665a3a287cd3aee1e7327f2381222c387c4Liubov Dmitrieva * See the License for the specific language governing permissions and
140a490665a3a287cd3aee1e7327f2381222c387c4Liubov Dmitrieva * limitations under the License.
150a490665a3a287cd3aee1e7327f2381222c387c4Liubov Dmitrieva */
160a490665a3a287cd3aee1e7327f2381222c387c4Liubov Dmitrieva
170a490665a3a287cd3aee1e7327f2381222c387c4Liubov Dmitrieva#ifndef A_NETWORK_SESSION_H_
180a490665a3a287cd3aee1e7327f2381222c387c4Liubov Dmitrieva
190a490665a3a287cd3aee1e7327f2381222c387c4Liubov Dmitrieva#define A_NETWORK_SESSION_H_
200a490665a3a287cd3aee1e7327f2381222c387c4Liubov Dmitrieva
210a490665a3a287cd3aee1e7327f2381222c387c4Liubov Dmitrieva#include <media/stagefright/foundation/ABase.h>
220a490665a3a287cd3aee1e7327f2381222c387c4Liubov Dmitrieva#include <utils/KeyedVector.h>
230a490665a3a287cd3aee1e7327f2381222c387c4Liubov Dmitrieva#include <utils/RefBase.h>
240a490665a3a287cd3aee1e7327f2381222c387c4Liubov Dmitrieva#include <utils/Thread.h>
250a490665a3a287cd3aee1e7327f2381222c387c4Liubov Dmitrieva
260a490665a3a287cd3aee1e7327f2381222c387c4Liubov Dmitrieva#include <netinet/in.h>
270a490665a3a287cd3aee1e7327f2381222c387c4Liubov Dmitrieva
280a490665a3a287cd3aee1e7327f2381222c387c4Liubov Dmitrievanamespace android {
290a490665a3a287cd3aee1e7327f2381222c387c4Liubov Dmitrieva
300a490665a3a287cd3aee1e7327f2381222c387c4Liubov Dmitrievastruct AMessage;
310a490665a3a287cd3aee1e7327f2381222c387c4Liubov Dmitrieva
320a490665a3a287cd3aee1e7327f2381222c387c4Liubov Dmitrieva// Helper class to manage a number of live sockets (datagram and stream-based)
330a490665a3a287cd3aee1e7327f2381222c387c4Liubov Dmitrieva// on a single thread. Clients are notified about activity through AMessages.
340a490665a3a287cd3aee1e7327f2381222c387c4Liubov Dmitrievastruct ANetworkSession : public RefBase {
350a490665a3a287cd3aee1e7327f2381222c387c4Liubov Dmitrieva    ANetworkSession();
360a490665a3a287cd3aee1e7327f2381222c387c4Liubov Dmitrieva
370a490665a3a287cd3aee1e7327f2381222c387c4Liubov Dmitrieva    status_t start();
380a490665a3a287cd3aee1e7327f2381222c387c4Liubov Dmitrieva    status_t stop();
390a490665a3a287cd3aee1e7327f2381222c387c4Liubov Dmitrieva
400a490665a3a287cd3aee1e7327f2381222c387c4Liubov Dmitrieva    status_t createRTSPClient(
410a490665a3a287cd3aee1e7327f2381222c387c4Liubov Dmitrieva            const char *host, unsigned port, const sp<AMessage> &notify,
420a490665a3a287cd3aee1e7327f2381222c387c4Liubov Dmitrieva            int32_t *sessionID);
430a490665a3a287cd3aee1e7327f2381222c387c4Liubov Dmitrieva
440a490665a3a287cd3aee1e7327f2381222c387c4Liubov Dmitrieva    status_t createRTSPServer(
450a490665a3a287cd3aee1e7327f2381222c387c4Liubov Dmitrieva            const struct in_addr &addr, unsigned port,
460a490665a3a287cd3aee1e7327f2381222c387c4Liubov Dmitrieva            const sp<AMessage> &notify, int32_t *sessionID);
470a490665a3a287cd3aee1e7327f2381222c387c4Liubov Dmitrieva
480a490665a3a287cd3aee1e7327f2381222c387c4Liubov Dmitrieva    status_t createUDPSession(
490a490665a3a287cd3aee1e7327f2381222c387c4Liubov Dmitrieva            unsigned localPort, const sp<AMessage> &notify, int32_t *sessionID);
500a490665a3a287cd3aee1e7327f2381222c387c4Liubov Dmitrieva
510a490665a3a287cd3aee1e7327f2381222c387c4Liubov Dmitrieva    status_t createUDPSession(
520a490665a3a287cd3aee1e7327f2381222c387c4Liubov Dmitrieva            unsigned localPort,
530a490665a3a287cd3aee1e7327f2381222c387c4Liubov Dmitrieva            const char *remoteHost,
540a490665a3a287cd3aee1e7327f2381222c387c4Liubov Dmitrieva            unsigned remotePort,
550a490665a3a287cd3aee1e7327f2381222c387c4Liubov Dmitrieva            const sp<AMessage> &notify,
560a490665a3a287cd3aee1e7327f2381222c387c4Liubov Dmitrieva            int32_t *sessionID);
570a490665a3a287cd3aee1e7327f2381222c387c4Liubov Dmitrieva
580a490665a3a287cd3aee1e7327f2381222c387c4Liubov Dmitrieva    status_t connectUDPSession(
590a490665a3a287cd3aee1e7327f2381222c387c4Liubov Dmitrieva            int32_t sessionID, const char *remoteHost, unsigned remotePort);
600a490665a3a287cd3aee1e7327f2381222c387c4Liubov Dmitrieva
610a490665a3a287cd3aee1e7327f2381222c387c4Liubov Dmitrieva    // passive
620a490665a3a287cd3aee1e7327f2381222c387c4Liubov Dmitrieva    status_t createTCPDatagramSession(
630a490665a3a287cd3aee1e7327f2381222c387c4Liubov Dmitrieva            const struct in_addr &addr, unsigned port,
640a490665a3a287cd3aee1e7327f2381222c387c4Liubov Dmitrieva            const sp<AMessage> &notify, int32_t *sessionID);
650a490665a3a287cd3aee1e7327f2381222c387c4Liubov Dmitrieva
660a490665a3a287cd3aee1e7327f2381222c387c4Liubov Dmitrieva    // active
670a490665a3a287cd3aee1e7327f2381222c387c4Liubov Dmitrieva    status_t createTCPDatagramSession(
680a490665a3a287cd3aee1e7327f2381222c387c4Liubov Dmitrieva            unsigned localPort,
690a490665a3a287cd3aee1e7327f2381222c387c4Liubov Dmitrieva            const char *remoteHost,
700a490665a3a287cd3aee1e7327f2381222c387c4Liubov Dmitrieva            unsigned remotePort,
710a490665a3a287cd3aee1e7327f2381222c387c4Liubov Dmitrieva            const sp<AMessage> &notify,
720a490665a3a287cd3aee1e7327f2381222c387c4Liubov Dmitrieva            int32_t *sessionID);
730a490665a3a287cd3aee1e7327f2381222c387c4Liubov Dmitrieva
740a490665a3a287cd3aee1e7327f2381222c387c4Liubov Dmitrieva    status_t destroySession(int32_t sessionID);
750a490665a3a287cd3aee1e7327f2381222c387c4Liubov Dmitrieva
760a490665a3a287cd3aee1e7327f2381222c387c4Liubov Dmitrieva    status_t sendRequest(
770a490665a3a287cd3aee1e7327f2381222c387c4Liubov Dmitrieva            int32_t sessionID, const void *data, ssize_t size = -1,
780a490665a3a287cd3aee1e7327f2381222c387c4Liubov Dmitrieva            bool timeValid = false, int64_t timeUs = -1ll);
790a490665a3a287cd3aee1e7327f2381222c387c4Liubov Dmitrieva
800a490665a3a287cd3aee1e7327f2381222c387c4Liubov Dmitrieva    status_t switchToWebSocketMode(int32_t sessionID);
810a490665a3a287cd3aee1e7327f2381222c387c4Liubov Dmitrieva
820a490665a3a287cd3aee1e7327f2381222c387c4Liubov Dmitrieva    enum NotificationReason {
830a490665a3a287cd3aee1e7327f2381222c387c4Liubov Dmitrieva        kWhatError,
840a490665a3a287cd3aee1e7327f2381222c387c4Liubov Dmitrieva        kWhatConnected,
850a490665a3a287cd3aee1e7327f2381222c387c4Liubov Dmitrieva        kWhatClientConnected,
860a490665a3a287cd3aee1e7327f2381222c387c4Liubov Dmitrieva        kWhatData,
870a490665a3a287cd3aee1e7327f2381222c387c4Liubov Dmitrieva        kWhatDatagram,
880a490665a3a287cd3aee1e7327f2381222c387c4Liubov Dmitrieva        kWhatBinaryData,
890a490665a3a287cd3aee1e7327f2381222c387c4Liubov Dmitrieva        kWhatWebSocketMessage,
900a490665a3a287cd3aee1e7327f2381222c387c4Liubov Dmitrieva        kWhatNetworkStall,
910a490665a3a287cd3aee1e7327f2381222c387c4Liubov Dmitrieva    };
920a490665a3a287cd3aee1e7327f2381222c387c4Liubov Dmitrieva
930a490665a3a287cd3aee1e7327f2381222c387c4Liubov Dmitrievaprotected:
940a490665a3a287cd3aee1e7327f2381222c387c4Liubov Dmitrieva    virtual ~ANetworkSession();
950a490665a3a287cd3aee1e7327f2381222c387c4Liubov Dmitrieva
960a490665a3a287cd3aee1e7327f2381222c387c4Liubov Dmitrievaprivate:
970a490665a3a287cd3aee1e7327f2381222c387c4Liubov Dmitrieva    struct NetworkThread;
980a490665a3a287cd3aee1e7327f2381222c387c4Liubov Dmitrieva    struct Session;
990a490665a3a287cd3aee1e7327f2381222c387c4Liubov Dmitrieva
1000a490665a3a287cd3aee1e7327f2381222c387c4Liubov Dmitrieva    Mutex mLock;
1010a490665a3a287cd3aee1e7327f2381222c387c4Liubov Dmitrieva    sp<Thread> mThread;
1020a490665a3a287cd3aee1e7327f2381222c387c4Liubov Dmitrieva
1030a490665a3a287cd3aee1e7327f2381222c387c4Liubov Dmitrieva    int32_t mNextSessionID;
1040a490665a3a287cd3aee1e7327f2381222c387c4Liubov Dmitrieva
1050a490665a3a287cd3aee1e7327f2381222c387c4Liubov Dmitrieva    int mPipeFd[2];
1060a490665a3a287cd3aee1e7327f2381222c387c4Liubov Dmitrieva
1070a490665a3a287cd3aee1e7327f2381222c387c4Liubov Dmitrieva    KeyedVector<int32_t, sp<Session> > mSessions;
1080a490665a3a287cd3aee1e7327f2381222c387c4Liubov Dmitrieva
1090a490665a3a287cd3aee1e7327f2381222c387c4Liubov Dmitrieva    enum Mode {
1100a490665a3a287cd3aee1e7327f2381222c387c4Liubov Dmitrieva        kModeCreateUDPSession,
1110a490665a3a287cd3aee1e7327f2381222c387c4Liubov Dmitrieva        kModeCreateTCPDatagramSessionPassive,
1120a490665a3a287cd3aee1e7327f2381222c387c4Liubov Dmitrieva        kModeCreateTCPDatagramSessionActive,
1130a490665a3a287cd3aee1e7327f2381222c387c4Liubov Dmitrieva        kModeCreateRTSPServer,
1140a490665a3a287cd3aee1e7327f2381222c387c4Liubov Dmitrieva        kModeCreateRTSPClient,
1150a490665a3a287cd3aee1e7327f2381222c387c4Liubov Dmitrieva    };
1160a490665a3a287cd3aee1e7327f2381222c387c4Liubov Dmitrieva    status_t createClientOrServer(
1170a490665a3a287cd3aee1e7327f2381222c387c4Liubov Dmitrieva            Mode mode,
1180a490665a3a287cd3aee1e7327f2381222c387c4Liubov Dmitrieva            const struct in_addr *addr,
1190a490665a3a287cd3aee1e7327f2381222c387c4Liubov Dmitrieva            unsigned port,
1200a490665a3a287cd3aee1e7327f2381222c387c4Liubov Dmitrieva            const char *remoteHost,
1210a490665a3a287cd3aee1e7327f2381222c387c4Liubov Dmitrieva            unsigned remotePort,
1220a490665a3a287cd3aee1e7327f2381222c387c4Liubov Dmitrieva            const sp<AMessage> &notify,
1230a490665a3a287cd3aee1e7327f2381222c387c4Liubov Dmitrieva            int32_t *sessionID);
1240a490665a3a287cd3aee1e7327f2381222c387c4Liubov Dmitrieva
1250a490665a3a287cd3aee1e7327f2381222c387c4Liubov Dmitrieva    void threadLoop();
1260a490665a3a287cd3aee1e7327f2381222c387c4Liubov Dmitrieva    void interrupt();
1270a490665a3a287cd3aee1e7327f2381222c387c4Liubov Dmitrieva
1280a490665a3a287cd3aee1e7327f2381222c387c4Liubov Dmitrieva    static status_t MakeSocketNonBlocking(int s);
1290a490665a3a287cd3aee1e7327f2381222c387c4Liubov Dmitrieva
1300a490665a3a287cd3aee1e7327f2381222c387c4Liubov Dmitrieva    DISALLOW_EVIL_CONSTRUCTORS(ANetworkSession);
1310a490665a3a287cd3aee1e7327f2381222c387c4Liubov Dmitrieva};
1320a490665a3a287cd3aee1e7327f2381222c387c4Liubov Dmitrieva
1330a490665a3a287cd3aee1e7327f2381222c387c4Liubov Dmitrieva}  // namespace android
1340a490665a3a287cd3aee1e7327f2381222c387c4Liubov Dmitrieva
1350a490665a3a287cd3aee1e7327f2381222c387c4Liubov Dmitrieva#endif  // A_NETWORK_SESSION_H_
1360a490665a3a287cd3aee1e7327f2381222c387c4Liubov Dmitrieva