blob: 6e40185b0e9b92ead9df023d392a15d9200b832b [file] [log] [blame]
Andreas Huber8a61c222012-08-31 14:05:27 -07001/*
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
Andreas Huberbf049b92013-01-29 09:22:16 -080027#define USE_TUNNEL_RENDERER 0
28
Andreas Huber8a61c222012-08-31 14:05:27 -070029namespace android {
30
31struct ABuffer;
32struct ANetworkSession;
Andreas Huberbf049b92013-01-29 09:22:16 -080033
34#if USE_TUNNEL_RENDERER
Andreas Huber8a61c222012-08-31 14:05:27 -070035struct TunnelRenderer;
Andreas Huberbf049b92013-01-29 09:22:16 -080036#else
37struct DirectRenderer;
38#endif
Andreas Huber8a61c222012-08-31 14:05:27 -070039
40// Creates a pair of sockets for RTP/RTCP traffic, instantiates a renderer
41// for incoming transport stream data and occasionally sends statistics over
42// the RTCP channel.
43struct RTPSink : public AHandler {
44 RTPSink(const sp<ANetworkSession> &netSession,
Andreas Huberbf049b92013-01-29 09:22:16 -080045 const sp<IGraphicBufferProducer> &bufferProducer,
46 const sp<AMessage> &notify);
Andreas Huber8a61c222012-08-31 14:05:27 -070047
48 // If TCP interleaving is used, no UDP sockets are created, instead
49 // incoming RTP/RTCP packets (arriving on the RTSP control connection)
50 // are manually injected by WifiDisplaySink.
51 status_t init(bool useTCPInterleaving);
52
53 status_t connect(
54 const char *host, int32_t remoteRtpPort, int32_t remoteRtcpPort);
55
56 int32_t getRTPPort() const;
57
58 status_t injectPacket(bool isRTP, const sp<ABuffer> &buffer);
59
60protected:
61 virtual void onMessageReceived(const sp<AMessage> &msg);
62 virtual ~RTPSink();
63
64private:
65 enum {
66 kWhatRTPNotify,
67 kWhatRTCPNotify,
68 kWhatSendRR,
69 kWhatPacketLost,
70 kWhatInject,
71 };
72
73 struct Source;
74 struct StreamSource;
75
76 sp<ANetworkSession> mNetSession;
Andy McFadden484566c2012-12-18 09:46:54 -080077 sp<IGraphicBufferProducer> mSurfaceTex;
Andreas Huberbf049b92013-01-29 09:22:16 -080078 sp<AMessage> mNotify;
Andreas Huber8a61c222012-08-31 14:05:27 -070079 KeyedVector<uint32_t, sp<Source> > mSources;
80
81 int32_t mRTPPort;
82 int32_t mRTPSessionID;
83 int32_t mRTCPSessionID;
84
85 int64_t mFirstArrivalTimeUs;
86 int64_t mNumPacketsReceived;
87 LinearRegression mRegression;
88 int64_t mMaxDelayMs;
89
Andreas Huberbf049b92013-01-29 09:22:16 -080090#if USE_TUNNEL_RENDERER
Andreas Huber8a61c222012-08-31 14:05:27 -070091 sp<TunnelRenderer> mRenderer;
Andreas Huberbf049b92013-01-29 09:22:16 -080092#else
93 sp<DirectRenderer> mRenderer;
94#endif
Andreas Huber8a61c222012-08-31 14:05:27 -070095
96 status_t parseRTP(const sp<ABuffer> &buffer);
97 status_t parseRTCP(const sp<ABuffer> &buffer);
98 status_t parseBYE(const uint8_t *data, size_t size);
99 status_t parseSR(const uint8_t *data, size_t size);
100
101 void addSDES(const sp<ABuffer> &buffer);
102 void onSendRR();
103 void onPacketLost(const sp<AMessage> &msg);
104 void scheduleSendRR();
105
106 DISALLOW_EVIL_CONSTRUCTORS(RTPSink);
107};
108
109} // namespace android
110
111#endif // RTP_SINK_H_