blob: 3b4f9e9732ee614113c9e2f9da3e275f0a7c4239 [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"
Kim Sungyeon89d5bc62020-03-17 21:39:33 +090036#include "ARTPSource.h"
Byeongjo Park870c9d62019-01-24 20:56:37 +090037#include "ASessionDescription.h"
38#include "NuPlayerSource.h"
39
40
41
42
43
44
45namespace android {
46
47struct ALooper;
48struct AnotherPacketSource;
49
50struct NuPlayer::RTPSource : public NuPlayer::Source {
51 RTPSource(
52 const sp<AMessage> &notify,
53 const String8& rtpParams);
54
55 virtual status_t getBufferingSettings(
56 BufferingSettings* buffering /* nonnull */) override;
57 virtual status_t setBufferingSettings(const BufferingSettings& buffering) override;
58
59 virtual void prepareAsync();
60 virtual void start();
61 virtual void stop();
62 virtual void pause();
63 virtual void resume();
64
65 virtual status_t feedMoreTSData();
66
67 virtual status_t dequeueAccessUnit(bool audio, sp<ABuffer> *accessUnit);
68
69 virtual status_t getDuration(int64_t *durationUs);
70 virtual status_t seekTo(
71 int64_t seekTimeUs,
72 MediaPlayerSeekMode mode = MediaPlayerSeekMode::SEEK_PREVIOUS_SYNC) override;
73
Byeongjo Parkc0a4d1a2019-10-16 16:38:24 +090074 virtual bool isRealTime() const;
75
Byeongjo Park870c9d62019-01-24 20:56:37 +090076 void onMessageReceived(const sp<AMessage> &msg);
77
Kim Sungyeona6d72e52019-07-18 17:48:32 +090078 virtual void setTargetBitrate(int32_t bitrate) override;
79
Byeongjo Park870c9d62019-01-24 20:56:37 +090080protected:
81 virtual ~RTPSource();
82
83 virtual sp<MetaData> getFormatMeta(bool audio);
84
85private:
86 enum {
87 kWhatAccessUnit = 'accU',
88 kWhatAccessUnitComplete = 'accu',
89 kWhatDisconnect = 'disc',
90 kWhatEOS = 'eos!',
91 kWhatPollBuffering = 'poll',
92 kWhatSetBufferingSettings = 'sBuS',
93 };
94
Lajos Molnar638df3a2020-03-17 08:13:10 -070095 const int64_t kBufferingPollIntervalUs = 1000000ll;
Lajos Molnar638df3a2020-03-17 08:13:10 -070096
Byeongjo Park870c9d62019-01-24 20:56:37 +090097 enum State {
98 DISCONNECTED,
99 CONNECTING,
100 CONNECTED,
101 PAUSED,
102 };
103
104 struct TrackInfo {
105
106 /* SDP of track */
107 bool mIsAudio;
108 int32_t mPayloadType;
109 String8 mMimeType;
110 String8 mCodecName;
111 int32_t mCodecProfile;
112 int32_t mCodecLevel;
113 int32_t mWidth;
114 int32_t mHeight;
115 String8 mLocalIp;
116 String8 mRemoteIp;
117 int32_t mLocalPort;
118 int32_t mRemotePort;
Byeongjo Parkd1587172019-03-29 15:04:10 +0900119 int64_t mSocketNetwork;
Byeongjo Park870c9d62019-01-24 20:56:37 +0900120 int32_t mTimeScale;
121 int32_t mAS;
122
Lajos Molnard9b59572020-08-17 16:53:02 -0700123 /* RTP jitter buffer time in milliseconds */
124 uint32_t mJbTimeMs;
Kim Sungyeonfc88be82018-03-20 16:51:41 +0900125 /* Unique ID indicates itself */
126 uint32_t mSelfID;
Byeongjo Park8f935262018-04-20 14:00:15 +0900127 /* extmap:<value> for CVO will be set to here */
128 int32_t mCVOExtMap;
Kim Sungyeonfc88be82018-03-20 16:51:41 +0900129
Byeongjo Park870c9d62019-01-24 20:56:37 +0900130 /* a copy of TrackInfo in RTSPSource */
131 sp<AnotherPacketSource> mSource;
132 uint32_t mRTPTime;
133 int64_t mNormalPlaytimeUs;
134 bool mNPTMappingValid;
135
136 /* a copy of TrackInfo in MyHandler.h */
137 int mRTPSocket;
138 int mRTCPSocket;
139 uint32_t mFirstSeqNumInSegment;
140 bool mNewSegment;
141 int32_t mAllowedStaleAccessUnits;
142 uint32_t mRTPAnchor;
143 int64_t mNTPAnchorUs;
144 bool mEOSReceived;
145 uint32_t mNormalPlayTimeRTP;
146 int64_t mNormalPlayTimeUs;
147 sp<APacketSource> mPacketSource;
148 List<sp<ABuffer>> mPackets;
149 };
150
151 const String8 mRTPParams;
152 uint32_t mFlags;
153 State mState;
154 status_t mFinalResult;
155
156 // below 3 parameters need to be checked whether it needed or not.
157 Mutex mBufferingLock;
158 bool mBuffering;
159 bool mInPreparationPhase;
160 Mutex mBufferingSettingsLock;
161 BufferingSettings mBufferingSettings;
162
163 sp<ALooper> mLooper;
164
165 sp<ARTPConnection> mRTPConn;
166
167 Vector<TrackInfo> mTracks;
168 sp<AnotherPacketSource> mAudioTrack;
169 sp<AnotherPacketSource> mVideoTrack;
170
171 int64_t mEOSTimeoutAudio;
172 int64_t mEOSTimeoutVideo;
173
174 /* MyHandler.h */
175 bool mFirstAccessUnit;
176 bool mAllTracksHaveTime;
177 int64_t mNTPAnchorUs;
178 int64_t mMediaAnchorUs;
179 int64_t mLastMediaTimeUs;
180 int64_t mNumAccessUnitsReceived;
Byeongjo Park8f935262018-04-20 14:00:15 +0900181 int32_t mLastCVOUpdated;
Byeongjo Park870c9d62019-01-24 20:56:37 +0900182 bool mReceivedFirstRTCPPacket;
183 bool mReceivedFirstRTPPacket;
184 bool mPausing;
185 int32_t mPauseGeneration;
186
187 sp<AnotherPacketSource> getSource(bool audio);
188
189 /* MyHandler.h */
190 void onTimeUpdate(int32_t trackIndex, uint32_t rtpTime, uint64_t ntpTime);
191 bool addMediaTimestamp(int32_t trackIndex, const TrackInfo *track,
192 const sp<ABuffer> &accessUnit);
193 bool dataReceivedOnAllChannels();
194 void postQueueAccessUnit(size_t trackIndex, const sp<ABuffer> &accessUnit);
195 void postQueueEOS(size_t trackIndex, status_t finalResult);
196 sp<MetaData> getTrackFormat(size_t index, int32_t *timeScale);
197 void onConnected();
198 void onDisconnected(const sp<AMessage> &msg);
199
200 void schedulePollBuffering();
201 void onPollBuffering();
202
203 bool haveSufficientDataOnAllTracks();
204
205 void setEOSTimeout(bool audio, int64_t timeout);
206
207 status_t setParameters(const String8 &params);
208 status_t setParameter(const String8 &key, const String8 &value);
Byeongjo Parkd1587172019-03-29 15:04:10 +0900209 void setSocketNetwork(int64_t networkHandle);
Byeongjo Park870c9d62019-01-24 20:56:37 +0900210 static void TrimString(String8 *s);
211
212 DISALLOW_EVIL_CONSTRUCTORS(RTPSource);
213};
214
215} // namespace android
216
217#endif // RTP_SOURCE_H_