1/*
2 * Copyright 2012, 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_SINK_H_
18
19#define RTP_SINK_H_
20
21#include <media/stagefright/foundation/AHandler.h>
22
23#include "LinearRegression.h"
24
25#include <gui/Surface.h>
26
27namespace android {
28
29struct ABuffer;
30struct ANetworkSession;
31struct TunnelRenderer;
32
33// Creates a pair of sockets for RTP/RTCP traffic, instantiates a renderer
34// for incoming transport stream data and occasionally sends statistics over
35// the RTCP channel.
36struct RTPSink : public AHandler {
37    RTPSink(const sp<ANetworkSession> &netSession,
38            const sp<ISurfaceTexture> &surfaceTex);
39
40    // If TCP interleaving is used, no UDP sockets are created, instead
41    // incoming RTP/RTCP packets (arriving on the RTSP control connection)
42    // are manually injected by WifiDisplaySink.
43    status_t init(bool useTCPInterleaving);
44
45    status_t connect(
46            const char *host, int32_t remoteRtpPort, int32_t remoteRtcpPort);
47
48    int32_t getRTPPort() const;
49
50    status_t injectPacket(bool isRTP, const sp<ABuffer> &buffer);
51
52protected:
53    virtual void onMessageReceived(const sp<AMessage> &msg);
54    virtual ~RTPSink();
55
56private:
57    enum {
58        kWhatRTPNotify,
59        kWhatRTCPNotify,
60        kWhatSendRR,
61        kWhatPacketLost,
62        kWhatInject,
63    };
64
65    struct Source;
66    struct StreamSource;
67
68    sp<ANetworkSession> mNetSession;
69    sp<ISurfaceTexture> mSurfaceTex;
70    KeyedVector<uint32_t, sp<Source> > mSources;
71
72    int32_t mRTPPort;
73    int32_t mRTPSessionID;
74    int32_t mRTCPSessionID;
75
76    int64_t mFirstArrivalTimeUs;
77    int64_t mNumPacketsReceived;
78    LinearRegression mRegression;
79    int64_t mMaxDelayMs;
80
81    sp<TunnelRenderer> mRenderer;
82
83    status_t parseRTP(const sp<ABuffer> &buffer);
84    status_t parseRTCP(const sp<ABuffer> &buffer);
85    status_t parseBYE(const uint8_t *data, size_t size);
86    status_t parseSR(const uint8_t *data, size_t size);
87
88    void addSDES(const sp<ABuffer> &buffer);
89    void onSendRR();
90    void onPacketLost(const sp<AMessage> &msg);
91    void scheduleSendRR();
92
93    DISALLOW_EVIL_CONSTRUCTORS(RTPSink);
94};
95
96}  // namespace android
97
98#endif  // RTP_SINK_H_
99