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