blob: 5d70142b94ed29236df3204b002afb50aaab3f52 [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
Byeongjo Parkc0a4d1a2019-10-16 16:38:24 +090083 virtual bool isRealTime() const;
84
Byeongjo Park870c9d62019-01-24 20:56:37 +090085 void onMessageReceived(const sp<AMessage> &msg);
86
Kim Sungyeona6d72e52019-07-18 17:48:32 +090087 virtual void setTargetBitrate(int32_t bitrate) override;
88
Byeongjo Park870c9d62019-01-24 20:56:37 +090089protected:
90 virtual ~RTPSource();
91
92 virtual sp<MetaData> getFormatMeta(bool audio);
93
94private:
95 enum {
96 kWhatAccessUnit = 'accU',
97 kWhatAccessUnitComplete = 'accu',
98 kWhatDisconnect = 'disc',
99 kWhatEOS = 'eos!',
100 kWhatPollBuffering = 'poll',
101 kWhatSetBufferingSettings = 'sBuS',
102 };
103
Lajos Molnar638df3a2020-03-17 08:13:10 -0700104 const int64_t kBufferingPollIntervalUs = 1000000ll;
Lajos Molnar638df3a2020-03-17 08:13:10 -0700105
Byeongjo Park870c9d62019-01-24 20:56:37 +0900106 enum State {
107 DISCONNECTED,
108 CONNECTING,
109 CONNECTED,
110 PAUSED,
111 };
112
113 struct TrackInfo {
114
115 /* SDP of track */
116 bool mIsAudio;
117 int32_t mPayloadType;
118 String8 mMimeType;
119 String8 mCodecName;
120 int32_t mCodecProfile;
121 int32_t mCodecLevel;
122 int32_t mWidth;
123 int32_t mHeight;
124 String8 mLocalIp;
125 String8 mRemoteIp;
126 int32_t mLocalPort;
127 int32_t mRemotePort;
Byeongjo Parkd1587172019-03-29 15:04:10 +0900128 int64_t mSocketNetwork;
Byeongjo Park870c9d62019-01-24 20:56:37 +0900129 int32_t mTimeScale;
130 int32_t mAS;
131
Byeongjo Park75adcb72019-09-23 13:31:37 +0900132 /* RTP jitter buffer time in millsecond */
133 uint32_t mJbTime;
Kim Sungyeonfc88be82018-03-20 16:51:41 +0900134 /* Unique ID indicates itself */
135 uint32_t mSelfID;
Byeongjo Park8f935262018-04-20 14:00:15 +0900136 /* extmap:<value> for CVO will be set to here */
137 int32_t mCVOExtMap;
Kim Sungyeonfc88be82018-03-20 16:51:41 +0900138
Byeongjo Park870c9d62019-01-24 20:56:37 +0900139 /* a copy of TrackInfo in RTSPSource */
140 sp<AnotherPacketSource> mSource;
141 uint32_t mRTPTime;
142 int64_t mNormalPlaytimeUs;
143 bool mNPTMappingValid;
144
145 /* a copy of TrackInfo in MyHandler.h */
146 int mRTPSocket;
147 int mRTCPSocket;
148 uint32_t mFirstSeqNumInSegment;
149 bool mNewSegment;
150 int32_t mAllowedStaleAccessUnits;
151 uint32_t mRTPAnchor;
152 int64_t mNTPAnchorUs;
153 bool mEOSReceived;
154 uint32_t mNormalPlayTimeRTP;
155 int64_t mNormalPlayTimeUs;
156 sp<APacketSource> mPacketSource;
157 List<sp<ABuffer>> mPackets;
158 };
159
160 const String8 mRTPParams;
161 uint32_t mFlags;
162 State mState;
163 status_t mFinalResult;
164
165 // below 3 parameters need to be checked whether it needed or not.
166 Mutex mBufferingLock;
167 bool mBuffering;
168 bool mInPreparationPhase;
169 Mutex mBufferingSettingsLock;
170 BufferingSettings mBufferingSettings;
171
172 sp<ALooper> mLooper;
173
174 sp<ARTPConnection> mRTPConn;
175
176 Vector<TrackInfo> mTracks;
177 sp<AnotherPacketSource> mAudioTrack;
178 sp<AnotherPacketSource> mVideoTrack;
179
180 int64_t mEOSTimeoutAudio;
181 int64_t mEOSTimeoutVideo;
182
183 /* MyHandler.h */
184 bool mFirstAccessUnit;
185 bool mAllTracksHaveTime;
186 int64_t mNTPAnchorUs;
187 int64_t mMediaAnchorUs;
188 int64_t mLastMediaTimeUs;
189 int64_t mNumAccessUnitsReceived;
Byeongjo Park8f935262018-04-20 14:00:15 +0900190 int32_t mLastCVOUpdated;
Byeongjo Park870c9d62019-01-24 20:56:37 +0900191 bool mReceivedFirstRTCPPacket;
192 bool mReceivedFirstRTPPacket;
193 bool mPausing;
194 int32_t mPauseGeneration;
195
196 sp<AnotherPacketSource> getSource(bool audio);
197
198 /* MyHandler.h */
199 void onTimeUpdate(int32_t trackIndex, uint32_t rtpTime, uint64_t ntpTime);
200 bool addMediaTimestamp(int32_t trackIndex, const TrackInfo *track,
201 const sp<ABuffer> &accessUnit);
202 bool dataReceivedOnAllChannels();
203 void postQueueAccessUnit(size_t trackIndex, const sp<ABuffer> &accessUnit);
204 void postQueueEOS(size_t trackIndex, status_t finalResult);
205 sp<MetaData> getTrackFormat(size_t index, int32_t *timeScale);
206 void onConnected();
207 void onDisconnected(const sp<AMessage> &msg);
208
209 void schedulePollBuffering();
210 void onPollBuffering();
211
212 bool haveSufficientDataOnAllTracks();
213
214 void setEOSTimeout(bool audio, int64_t timeout);
215
216 status_t setParameters(const String8 &params);
217 status_t setParameter(const String8 &key, const String8 &value);
Byeongjo Parkd1587172019-03-29 15:04:10 +0900218 void setSocketNetwork(int64_t networkHandle);
Byeongjo Park870c9d62019-01-24 20:56:37 +0900219 static void TrimString(String8 *s);
220
221 DISALLOW_EVIL_CONSTRUCTORS(RTPSource);
222};
223
224} // namespace android
225
226#endif // RTP_SOURCE_H_