1/*
2 * Copyright 2013, 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 RTP_SENDER_H_
18
19#define RTP_SENDER_H_
20
21#include "RTPBase.h"
22
23#include <media/stagefright/foundation/AHandler.h>
24
25namespace android {
26
27struct ABuffer;
28struct ANetworkSession;
29
30// An object of this class facilitates sending of media data over an RTP
31// channel. The channel is established over a UDP or TCP connection depending
32// on which "TransportMode" was chosen. In addition different RTP packetization
33// schemes are supported such as "Transport Stream Packets over RTP",
34// or "AVC/H.264 encapsulation as specified in RFC 3984 (non-interleaved mode)"
35struct RTPSender : public RTPBase, public AHandler {
36    enum {
37        kWhatInitDone,
38        kWhatError,
39        kWhatNetworkStall,
40        kWhatInformSender,
41    };
42    RTPSender(
43            const sp<ANetworkSession> &netSession,
44            const sp<AMessage> &notify);
45
46    status_t initAsync(
47              const char *remoteHost,
48              int32_t remoteRTPPort,
49              TransportMode rtpMode,
50              int32_t remoteRTCPPort,
51              TransportMode rtcpMode,
52              int32_t *outLocalRTPPort);
53
54    status_t queueBuffer(
55            const sp<ABuffer> &buffer,
56            uint8_t packetType,
57            PacketizationMode mode);
58
59protected:
60    virtual ~RTPSender();
61    virtual void onMessageReceived(const sp<AMessage> &msg);
62
63private:
64    enum {
65        kWhatRTPNotify,
66        kWhatRTCPNotify,
67    };
68
69    enum {
70        kMaxNumTSPacketsPerRTPPacket = (kMaxUDPPacketSize - 12) / 188,
71        kMaxHistorySize              = 1024,
72        kSourceID                    = 0xdeadbeef,
73    };
74
75    sp<ANetworkSession> mNetSession;
76    sp<AMessage> mNotify;
77    TransportMode mRTPMode;
78    TransportMode mRTCPMode;
79    int32_t mRTPSessionID;
80    int32_t mRTCPSessionID;
81    bool mRTPConnected;
82    bool mRTCPConnected;
83
84    uint64_t mLastNTPTime;
85    uint32_t mLastRTPTime;
86    uint32_t mNumRTPSent;
87    uint32_t mNumRTPOctetsSent;
88    uint32_t mNumSRsSent;
89
90    uint32_t mRTPSeqNo;
91
92    List<sp<ABuffer> > mHistory;
93    size_t mHistorySize;
94
95    static uint64_t GetNowNTP();
96
97    status_t queueRawPacket(const sp<ABuffer> &tsPackets, uint8_t packetType);
98    status_t queueTSPackets(const sp<ABuffer> &tsPackets, uint8_t packetType);
99    status_t queueAVCBuffer(const sp<ABuffer> &accessUnit, uint8_t packetType);
100
101    status_t sendRTPPacket(
102            const sp<ABuffer> &packet, bool storeInHistory,
103            bool timeValid = false, int64_t timeUs = -1ll);
104
105    void onNetNotify(bool isRTP, const sp<AMessage> &msg);
106
107    status_t onRTCPData(const sp<ABuffer> &data);
108    status_t parseReceiverReport(const uint8_t *data, size_t size);
109    status_t parseTSFB(const uint8_t *data, size_t size);
110    status_t parseAPP(const uint8_t *data, size_t size);
111
112    void notifyInitDone(status_t err);
113    void notifyError(status_t err);
114    void notifyNetworkStall(size_t numBytesQueued);
115
116    DISALLOW_EVIL_CONSTRUCTORS(RTPSender);
117};
118
119}  // namespace android
120
121#endif  // RTP_SENDER_H_
122