ARTPConnection.h revision f88f84414ae7baead03497f1d650ad8ea2f87688
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_RTP_CONNECTION_H_
18
19#define A_RTP_CONNECTION_H_
20
21#include <media/stagefright/foundation/AHandler.h>
22#include <utils/List.h>
23
24namespace android {
25
26struct ABuffer;
27struct ARTPSource;
28struct ASessionDescription;
29
30struct ARTPConnection : public AHandler {
31    enum Flags {
32        kFakeTimestamps      = 1,
33        kRegularlyRequestFIR = 2,
34    };
35
36    ARTPConnection(uint32_t flags = 0);
37
38    void addStream(
39            int rtpSocket, int rtcpSocket,
40            const sp<ASessionDescription> &sessionDesc, size_t index,
41            const sp<AMessage> &notify);
42
43    void removeStream(int rtpSocket, int rtcpSocket);
44
45    // Creates a pair of UDP datagram sockets bound to adjacent ports
46    // (the rtpSocket is bound to an even port, the rtcpSocket to the
47    // next higher port).
48    static void MakePortPair(
49            int *rtpSocket, int *rtcpSocket, unsigned *rtpPort);
50
51protected:
52    virtual ~ARTPConnection();
53    virtual void onMessageReceived(const sp<AMessage> &msg);
54
55private:
56    enum {
57        kWhatAddStream,
58        kWhatRemoveStream,
59        kWhatPollStreams,
60    };
61
62    static const int64_t kSelectTimeoutUs;
63
64    uint32_t mFlags;
65
66    struct StreamInfo;
67    List<StreamInfo> mStreams;
68
69    bool mPollEventPending;
70    int64_t mLastReceiverReportTimeUs;
71
72    void onAddStream(const sp<AMessage> &msg);
73    void onRemoveStream(const sp<AMessage> &msg);
74    void onPollStreams();
75    void onSendReceiverReports();
76
77    status_t receive(StreamInfo *info, bool receiveRTP);
78
79    status_t parseRTP(StreamInfo *info, const sp<ABuffer> &buffer);
80    status_t parseRTCP(StreamInfo *info, const sp<ABuffer> &buffer);
81    status_t parseSR(StreamInfo *info, const uint8_t *data, size_t size);
82    status_t parseBYE(StreamInfo *info, const uint8_t *data, size_t size);
83
84    sp<ARTPSource> findSource(StreamInfo *info, uint32_t id);
85
86    void postPollEvent();
87
88    DISALLOW_EVIL_CONSTRUCTORS(ARTPConnection);
89};
90
91}  // namespace android
92
93#endif  // A_RTP_CONNECTION_H_
94