ARTPConnection.h revision 0792ce7e0924ebb0dbe7b7cfcd79d12cbdb03ed2
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
54protected:
55    virtual ~ARTPConnection();
56    virtual void onMessageReceived(const sp<AMessage> &msg);
57
58private:
59    enum {
60        kWhatAddStream,
61        kWhatRemoveStream,
62        kWhatPollStreams,
63        kWhatInjectPacket,
64    };
65
66    static const int64_t kSelectTimeoutUs;
67
68    uint32_t mFlags;
69
70    struct StreamInfo;
71    List<StreamInfo> mStreams;
72
73    bool mPollEventPending;
74    int64_t mLastReceiverReportTimeUs;
75
76    void onAddStream(const sp<AMessage> &msg);
77    void onRemoveStream(const sp<AMessage> &msg);
78    void onPollStreams();
79    void onInjectPacket(const sp<AMessage> &msg);
80    void onSendReceiverReports();
81
82    status_t receive(StreamInfo *info, bool receiveRTP);
83
84    status_t parseRTP(StreamInfo *info, const sp<ABuffer> &buffer);
85    status_t parseRTCP(StreamInfo *info, const sp<ABuffer> &buffer);
86    status_t parseSR(StreamInfo *info, const uint8_t *data, size_t size);
87    status_t parseBYE(StreamInfo *info, const uint8_t *data, size_t size);
88
89    sp<ARTPSource> findSource(StreamInfo *info, uint32_t id);
90
91    void postPollEvent();
92
93    DISALLOW_EVIL_CONSTRUCTORS(ARTPConnection);
94};
95
96}  // namespace android
97
98#endif  // A_RTP_CONNECTION_H_
99