blob: 288104cff3edb9165f67b8226a68611a1ddc8b22 [file] [log] [blame]
Byeongjo Park870c9d62019-01-24 20:56:37 +09001/*
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 RTP_SOURCE_H_
18
19#define RTP_SOURCE_H_
20
21#include <media/stagefright/foundation/ABase.h>
22#include <media/stagefright/foundation/ABuffer.h>
23#include <media/stagefright/foundation/ADebug.h>
24#include <media/stagefright/foundation/AMessage.h>
25#include <media/stagefright/MediaSource.h>
26#include <media/stagefright/Utils.h>
27#include <media/BufferingSettings.h>
28
29#include <utils/KeyedVector.h>
30#include <utils/Vector.h>
31#include <utils/RefBase.h>
32
33#include "AnotherPacketSource.h"
34#include "APacketSource.h"
35#include "ARTPConnection.h"
36#include "ASessionDescription.h"
37#include "NuPlayerSource.h"
38
39
40
41
42
43
44namespace android {
45
46struct ALooper;
47struct AnotherPacketSource;
48
49struct NuPlayer::RTPSource : public NuPlayer::Source {
50 RTPSource(
51 const sp<AMessage> &notify,
52 const String8& rtpParams);
53
Byeongjo Park8f935262018-04-20 14:00:15 +090054 enum {
Kim Sungyeona6d72e52019-07-18 17:48:32 +090055 RTP_FIRST_PACKET = 100,
56 RTCP_FIRST_PACKET = 101,
57 RTP_QUALITY = 102,
Byeongjo Park8f935262018-04-20 14:00:15 +090058 RTCP_TSFB = 205,
59 RTCP_PSFB = 206,
60 RTP_CVO = 300,
Kim Sungyeond42c50d2019-03-17 22:04:14 +090061 RTP_AUTODOWN = 400,
Byeongjo Park8f935262018-04-20 14:00:15 +090062 };
63
Byeongjo Park870c9d62019-01-24 20:56:37 +090064 virtual status_t getBufferingSettings(
65 BufferingSettings* buffering /* nonnull */) override;
66 virtual status_t setBufferingSettings(const BufferingSettings& buffering) override;
67
68 virtual void prepareAsync();
69 virtual void start();
70 virtual void stop();
71 virtual void pause();
72 virtual void resume();
73
74 virtual status_t feedMoreTSData();
75
76 virtual status_t dequeueAccessUnit(bool audio, sp<ABuffer> *accessUnit);
77
78 virtual status_t getDuration(int64_t *durationUs);
79 virtual status_t seekTo(
80 int64_t seekTimeUs,
81 MediaPlayerSeekMode mode = MediaPlayerSeekMode::SEEK_PREVIOUS_SYNC) override;
82
83 void onMessageReceived(const sp<AMessage> &msg);
84
Kim Sungyeona6d72e52019-07-18 17:48:32 +090085 virtual void setTargetBitrate(int32_t bitrate) override;
86
Byeongjo Park870c9d62019-01-24 20:56:37 +090087protected:
88 virtual ~RTPSource();
89
90 virtual sp<MetaData> getFormatMeta(bool audio);
91
92private:
93 enum {
94 kWhatAccessUnit = 'accU',
95 kWhatAccessUnitComplete = 'accu',
96 kWhatDisconnect = 'disc',
97 kWhatEOS = 'eos!',
98 kWhatPollBuffering = 'poll',
99 kWhatSetBufferingSettings = 'sBuS',
100 };
101
Lajos Molnar638df3a2020-03-17 08:13:10 -0700102 const int64_t kBufferingPollIntervalUs = 1000000ll;
Lajos Molnar638df3a2020-03-17 08:13:10 -0700103
Byeongjo Park870c9d62019-01-24 20:56:37 +0900104 enum State {
105 DISCONNECTED,
106 CONNECTING,
107 CONNECTED,
108 PAUSED,
109 };
110
111 struct TrackInfo {
112
113 /* SDP of track */
114 bool mIsAudio;
115 int32_t mPayloadType;
116 String8 mMimeType;
117 String8 mCodecName;
118 int32_t mCodecProfile;
119 int32_t mCodecLevel;
120 int32_t mWidth;
121 int32_t mHeight;
122 String8 mLocalIp;
123 String8 mRemoteIp;
124 int32_t mLocalPort;
125 int32_t mRemotePort;
Byeongjo Parkd1587172019-03-29 15:04:10 +0900126 int64_t mSocketNetwork;
Byeongjo Park870c9d62019-01-24 20:56:37 +0900127 int32_t mTimeScale;
128 int32_t mAS;
129
Byeongjo Park75adcb72019-09-23 13:31:37 +0900130 /* RTP jitter buffer time in millsecond */
131 uint32_t mJbTime;
Kim Sungyeonfc88be82018-03-20 16:51:41 +0900132 /* Unique ID indicates itself */
133 uint32_t mSelfID;
Byeongjo Park8f935262018-04-20 14:00:15 +0900134 /* extmap:<value> for CVO will be set to here */
135 int32_t mCVOExtMap;
Kim Sungyeonfc88be82018-03-20 16:51:41 +0900136
Byeongjo Park870c9d62019-01-24 20:56:37 +0900137 /* a copy of TrackInfo in RTSPSource */
138 sp<AnotherPacketSource> mSource;
139 uint32_t mRTPTime;
140 int64_t mNormalPlaytimeUs;
141 bool mNPTMappingValid;
142
143 /* a copy of TrackInfo in MyHandler.h */
144 int mRTPSocket;
145 int mRTCPSocket;
146 uint32_t mFirstSeqNumInSegment;
147 bool mNewSegment;
148 int32_t mAllowedStaleAccessUnits;
149 uint32_t mRTPAnchor;
150 int64_t mNTPAnchorUs;
151 bool mEOSReceived;
152 uint32_t mNormalPlayTimeRTP;
153 int64_t mNormalPlayTimeUs;
154 sp<APacketSource> mPacketSource;
155 List<sp<ABuffer>> mPackets;
156 };
157
158 const String8 mRTPParams;
159 uint32_t mFlags;
160 State mState;
161 status_t mFinalResult;
162
163 // below 3 parameters need to be checked whether it needed or not.
164 Mutex mBufferingLock;
165 bool mBuffering;
166 bool mInPreparationPhase;
167 Mutex mBufferingSettingsLock;
168 BufferingSettings mBufferingSettings;
169
170 sp<ALooper> mLooper;
171
172 sp<ARTPConnection> mRTPConn;
173
174 Vector<TrackInfo> mTracks;
175 sp<AnotherPacketSource> mAudioTrack;
176 sp<AnotherPacketSource> mVideoTrack;
177
178 int64_t mEOSTimeoutAudio;
179 int64_t mEOSTimeoutVideo;
180
181 /* MyHandler.h */
182 bool mFirstAccessUnit;
183 bool mAllTracksHaveTime;
184 int64_t mNTPAnchorUs;
185 int64_t mMediaAnchorUs;
186 int64_t mLastMediaTimeUs;
187 int64_t mNumAccessUnitsReceived;
Byeongjo Park8f935262018-04-20 14:00:15 +0900188 int32_t mLastCVOUpdated;
Byeongjo Park870c9d62019-01-24 20:56:37 +0900189 bool mReceivedFirstRTCPPacket;
190 bool mReceivedFirstRTPPacket;
191 bool mPausing;
192 int32_t mPauseGeneration;
193
194 sp<AnotherPacketSource> getSource(bool audio);
195
196 /* MyHandler.h */
197 void onTimeUpdate(int32_t trackIndex, uint32_t rtpTime, uint64_t ntpTime);
198 bool addMediaTimestamp(int32_t trackIndex, const TrackInfo *track,
199 const sp<ABuffer> &accessUnit);
200 bool dataReceivedOnAllChannels();
201 void postQueueAccessUnit(size_t trackIndex, const sp<ABuffer> &accessUnit);
202 void postQueueEOS(size_t trackIndex, status_t finalResult);
203 sp<MetaData> getTrackFormat(size_t index, int32_t *timeScale);
204 void onConnected();
205 void onDisconnected(const sp<AMessage> &msg);
206
207 void schedulePollBuffering();
208 void onPollBuffering();
209
210 bool haveSufficientDataOnAllTracks();
211
212 void setEOSTimeout(bool audio, int64_t timeout);
213
214 status_t setParameters(const String8 &params);
215 status_t setParameter(const String8 &key, const String8 &value);
Byeongjo Parkd1587172019-03-29 15:04:10 +0900216 void setSocketNetwork(int64_t networkHandle);
Byeongjo Park870c9d62019-01-24 20:56:37 +0900217 static void TrimString(String8 *s);
218
219 DISALLOW_EVIL_CONSTRUCTORS(RTPSource);
220};
221
222} // namespace android
223
224#endif // RTP_SOURCE_H_