1a556c4822fc205db0d27834ba5b637c351d73ffaAndreas Huber/*
2a556c4822fc205db0d27834ba5b637c351d73ffaAndreas Huber * Copyright 2013, The Android Open Source Project
3a556c4822fc205db0d27834ba5b637c351d73ffaAndreas Huber *
4a556c4822fc205db0d27834ba5b637c351d73ffaAndreas Huber * Licensed under the Apache License, Version 2.0 (the "License");
5a556c4822fc205db0d27834ba5b637c351d73ffaAndreas Huber * you may not use this file except in compliance with the License.
6a556c4822fc205db0d27834ba5b637c351d73ffaAndreas Huber * You may obtain a copy of the License at
7a556c4822fc205db0d27834ba5b637c351d73ffaAndreas Huber *
8a556c4822fc205db0d27834ba5b637c351d73ffaAndreas Huber *     http://www.apache.org/licenses/LICENSE-2.0
9a556c4822fc205db0d27834ba5b637c351d73ffaAndreas Huber *
10a556c4822fc205db0d27834ba5b637c351d73ffaAndreas Huber * Unless required by applicable law or agreed to in writing, software
11a556c4822fc205db0d27834ba5b637c351d73ffaAndreas Huber * distributed under the License is distributed on an "AS IS" BASIS,
12a556c4822fc205db0d27834ba5b637c351d73ffaAndreas Huber * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13a556c4822fc205db0d27834ba5b637c351d73ffaAndreas Huber * See the License for the specific language governing permissions and
14a556c4822fc205db0d27834ba5b637c351d73ffaAndreas Huber * limitations under the License.
15a556c4822fc205db0d27834ba5b637c351d73ffaAndreas Huber */
16a556c4822fc205db0d27834ba5b637c351d73ffaAndreas Huber
17a556c4822fc205db0d27834ba5b637c351d73ffaAndreas Huber#ifndef RTP_SENDER_H_
18a556c4822fc205db0d27834ba5b637c351d73ffaAndreas Huber
19a556c4822fc205db0d27834ba5b637c351d73ffaAndreas Huber#define RTP_SENDER_H_
20a556c4822fc205db0d27834ba5b637c351d73ffaAndreas Huber
21a556c4822fc205db0d27834ba5b637c351d73ffaAndreas Huber#include "RTPBase.h"
22a556c4822fc205db0d27834ba5b637c351d73ffaAndreas Huber
23a556c4822fc205db0d27834ba5b637c351d73ffaAndreas Huber#include <media/stagefright/foundation/AHandler.h>
24a556c4822fc205db0d27834ba5b637c351d73ffaAndreas Huber
25a556c4822fc205db0d27834ba5b637c351d73ffaAndreas Hubernamespace android {
26a556c4822fc205db0d27834ba5b637c351d73ffaAndreas Huber
27a556c4822fc205db0d27834ba5b637c351d73ffaAndreas Huberstruct ABuffer;
28a556c4822fc205db0d27834ba5b637c351d73ffaAndreas Huberstruct ANetworkSession;
29a556c4822fc205db0d27834ba5b637c351d73ffaAndreas Huber
30a556c4822fc205db0d27834ba5b637c351d73ffaAndreas Huber// An object of this class facilitates sending of media data over an RTP
31a556c4822fc205db0d27834ba5b637c351d73ffaAndreas Huber// channel. The channel is established over a UDP or TCP connection depending
32a556c4822fc205db0d27834ba5b637c351d73ffaAndreas Huber// on which "TransportMode" was chosen. In addition different RTP packetization
33a556c4822fc205db0d27834ba5b637c351d73ffaAndreas Huber// schemes are supported such as "Transport Stream Packets over RTP",
34a556c4822fc205db0d27834ba5b637c351d73ffaAndreas Huber// or "AVC/H.264 encapsulation as specified in RFC 3984 (non-interleaved mode)"
35a556c4822fc205db0d27834ba5b637c351d73ffaAndreas Huberstruct RTPSender : public RTPBase, public AHandler {
36a556c4822fc205db0d27834ba5b637c351d73ffaAndreas Huber    enum {
37a556c4822fc205db0d27834ba5b637c351d73ffaAndreas Huber        kWhatInitDone,
38a556c4822fc205db0d27834ba5b637c351d73ffaAndreas Huber        kWhatError,
39126568c7aeeb5570789e70a310477f44dbdbd885Andreas Huber        kWhatNetworkStall,
40c86ef45279185b474bd6af0a7ae407f8ab577f13Andreas Huber        kWhatInformSender,
41a556c4822fc205db0d27834ba5b637c351d73ffaAndreas Huber    };
42a556c4822fc205db0d27834ba5b637c351d73ffaAndreas Huber    RTPSender(
43a556c4822fc205db0d27834ba5b637c351d73ffaAndreas Huber            const sp<ANetworkSession> &netSession,
44a556c4822fc205db0d27834ba5b637c351d73ffaAndreas Huber            const sp<AMessage> &notify);
45a556c4822fc205db0d27834ba5b637c351d73ffaAndreas Huber
46a556c4822fc205db0d27834ba5b637c351d73ffaAndreas Huber    status_t initAsync(
47a556c4822fc205db0d27834ba5b637c351d73ffaAndreas Huber              const char *remoteHost,
48a556c4822fc205db0d27834ba5b637c351d73ffaAndreas Huber              int32_t remoteRTPPort,
496e98aba4d23d00cab236d993d895f57ea76ea0e5Andreas Huber              TransportMode rtpMode,
50a556c4822fc205db0d27834ba5b637c351d73ffaAndreas Huber              int32_t remoteRTCPPort,
516e98aba4d23d00cab236d993d895f57ea76ea0e5Andreas Huber              TransportMode rtcpMode,
52a556c4822fc205db0d27834ba5b637c351d73ffaAndreas Huber              int32_t *outLocalRTPPort);
53a556c4822fc205db0d27834ba5b637c351d73ffaAndreas Huber
54a556c4822fc205db0d27834ba5b637c351d73ffaAndreas Huber    status_t queueBuffer(
55a556c4822fc205db0d27834ba5b637c351d73ffaAndreas Huber            const sp<ABuffer> &buffer,
56a556c4822fc205db0d27834ba5b637c351d73ffaAndreas Huber            uint8_t packetType,
57a556c4822fc205db0d27834ba5b637c351d73ffaAndreas Huber            PacketizationMode mode);
58a556c4822fc205db0d27834ba5b637c351d73ffaAndreas Huber
59a556c4822fc205db0d27834ba5b637c351d73ffaAndreas Huberprotected:
60a556c4822fc205db0d27834ba5b637c351d73ffaAndreas Huber    virtual ~RTPSender();
61a556c4822fc205db0d27834ba5b637c351d73ffaAndreas Huber    virtual void onMessageReceived(const sp<AMessage> &msg);
62a556c4822fc205db0d27834ba5b637c351d73ffaAndreas Huber
63a556c4822fc205db0d27834ba5b637c351d73ffaAndreas Huberprivate:
64a556c4822fc205db0d27834ba5b637c351d73ffaAndreas Huber    enum {
65a556c4822fc205db0d27834ba5b637c351d73ffaAndreas Huber        kWhatRTPNotify,
66a556c4822fc205db0d27834ba5b637c351d73ffaAndreas Huber        kWhatRTCPNotify,
67a556c4822fc205db0d27834ba5b637c351d73ffaAndreas Huber    };
68a556c4822fc205db0d27834ba5b637c351d73ffaAndreas Huber
69a556c4822fc205db0d27834ba5b637c351d73ffaAndreas Huber    enum {
70a556c4822fc205db0d27834ba5b637c351d73ffaAndreas Huber        kMaxNumTSPacketsPerRTPPacket = (kMaxUDPPacketSize - 12) / 188,
71a556c4822fc205db0d27834ba5b637c351d73ffaAndreas Huber        kMaxHistorySize              = 1024,
72a556c4822fc205db0d27834ba5b637c351d73ffaAndreas Huber        kSourceID                    = 0xdeadbeef,
73a556c4822fc205db0d27834ba5b637c351d73ffaAndreas Huber    };
74a556c4822fc205db0d27834ba5b637c351d73ffaAndreas Huber
75a556c4822fc205db0d27834ba5b637c351d73ffaAndreas Huber    sp<ANetworkSession> mNetSession;
76a556c4822fc205db0d27834ba5b637c351d73ffaAndreas Huber    sp<AMessage> mNotify;
776e98aba4d23d00cab236d993d895f57ea76ea0e5Andreas Huber    TransportMode mRTPMode;
786e98aba4d23d00cab236d993d895f57ea76ea0e5Andreas Huber    TransportMode mRTCPMode;
79a556c4822fc205db0d27834ba5b637c351d73ffaAndreas Huber    int32_t mRTPSessionID;
80a556c4822fc205db0d27834ba5b637c351d73ffaAndreas Huber    int32_t mRTCPSessionID;
81a556c4822fc205db0d27834ba5b637c351d73ffaAndreas Huber    bool mRTPConnected;
82a556c4822fc205db0d27834ba5b637c351d73ffaAndreas Huber    bool mRTCPConnected;
83a556c4822fc205db0d27834ba5b637c351d73ffaAndreas Huber
84a556c4822fc205db0d27834ba5b637c351d73ffaAndreas Huber    uint64_t mLastNTPTime;
85a556c4822fc205db0d27834ba5b637c351d73ffaAndreas Huber    uint32_t mLastRTPTime;
86a556c4822fc205db0d27834ba5b637c351d73ffaAndreas Huber    uint32_t mNumRTPSent;
87a556c4822fc205db0d27834ba5b637c351d73ffaAndreas Huber    uint32_t mNumRTPOctetsSent;
88a556c4822fc205db0d27834ba5b637c351d73ffaAndreas Huber    uint32_t mNumSRsSent;
89a556c4822fc205db0d27834ba5b637c351d73ffaAndreas Huber
90a556c4822fc205db0d27834ba5b637c351d73ffaAndreas Huber    uint32_t mRTPSeqNo;
91a556c4822fc205db0d27834ba5b637c351d73ffaAndreas Huber
92a556c4822fc205db0d27834ba5b637c351d73ffaAndreas Huber    List<sp<ABuffer> > mHistory;
93a556c4822fc205db0d27834ba5b637c351d73ffaAndreas Huber    size_t mHistorySize;
94a556c4822fc205db0d27834ba5b637c351d73ffaAndreas Huber
95a556c4822fc205db0d27834ba5b637c351d73ffaAndreas Huber    static uint64_t GetNowNTP();
96a556c4822fc205db0d27834ba5b637c351d73ffaAndreas Huber
978f1f6a4814403dd78539250c845f8326f6137a61Andreas Huber    status_t queueRawPacket(const sp<ABuffer> &tsPackets, uint8_t packetType);
98a556c4822fc205db0d27834ba5b637c351d73ffaAndreas Huber    status_t queueTSPackets(const sp<ABuffer> &tsPackets, uint8_t packetType);
99a556c4822fc205db0d27834ba5b637c351d73ffaAndreas Huber    status_t queueAVCBuffer(const sp<ABuffer> &accessUnit, uint8_t packetType);
100a556c4822fc205db0d27834ba5b637c351d73ffaAndreas Huber
101a239dd722e760fe4fd7379b454d7722e1f312928Andreas Huber    status_t sendRTPPacket(
102a239dd722e760fe4fd7379b454d7722e1f312928Andreas Huber            const sp<ABuffer> &packet, bool storeInHistory,
103a239dd722e760fe4fd7379b454d7722e1f312928Andreas Huber            bool timeValid = false, int64_t timeUs = -1ll);
104a556c4822fc205db0d27834ba5b637c351d73ffaAndreas Huber
105a556c4822fc205db0d27834ba5b637c351d73ffaAndreas Huber    void onNetNotify(bool isRTP, const sp<AMessage> &msg);
106a556c4822fc205db0d27834ba5b637c351d73ffaAndreas Huber
107a556c4822fc205db0d27834ba5b637c351d73ffaAndreas Huber    status_t onRTCPData(const sp<ABuffer> &data);
108a556c4822fc205db0d27834ba5b637c351d73ffaAndreas Huber    status_t parseReceiverReport(const uint8_t *data, size_t size);
109a556c4822fc205db0d27834ba5b637c351d73ffaAndreas Huber    status_t parseTSFB(const uint8_t *data, size_t size);
1102aea9552aeba92bbaf9e56c666049ea2d14057b5Andreas Huber    status_t parseAPP(const uint8_t *data, size_t size);
111a556c4822fc205db0d27834ba5b637c351d73ffaAndreas Huber
112a556c4822fc205db0d27834ba5b637c351d73ffaAndreas Huber    void notifyInitDone(status_t err);
113a556c4822fc205db0d27834ba5b637c351d73ffaAndreas Huber    void notifyError(status_t err);
114126568c7aeeb5570789e70a310477f44dbdbd885Andreas Huber    void notifyNetworkStall(size_t numBytesQueued);
115a556c4822fc205db0d27834ba5b637c351d73ffaAndreas Huber
116a556c4822fc205db0d27834ba5b637c351d73ffaAndreas Huber    DISALLOW_EVIL_CONSTRUCTORS(RTPSender);
117a556c4822fc205db0d27834ba5b637c351d73ffaAndreas Huber};
118a556c4822fc205db0d27834ba5b637c351d73ffaAndreas Huber
119a556c4822fc205db0d27834ba5b637c351d73ffaAndreas Huber}  // namespace android
120a556c4822fc205db0d27834ba5b637c351d73ffaAndreas Huber
121a556c4822fc205db0d27834ba5b637c351d73ffaAndreas Huber#endif  // RTP_SENDER_H_
122