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