blob: 5085a7eb8e623576b8f1256f693978cd833862e0 [file] [log] [blame]
Byeongjo Parkd157b792019-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>
Lajos Molnar19829332020-03-17 08:13:10 -070025#include <media/stagefright/MediaSource.h>
Byeongjo Parkd157b792019-01-24 20:56:37 +090026#include <media/stagefright/Utils.h>
27#include <media/BufferingSettings.h>
Byeongjo Parkd157b792019-01-24 20:56:37 +090028
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 Parkb0225aa2018-04-20 14:00:15 +090054 enum {
55 RTCP_TSFB = 205,
56 RTCP_PSFB = 206,
57 RTP_CVO = 300,
Kim Sungyeond194b8c2019-03-17 22:04:14 +090058 RTP_AUTODOWN = 400,
Byeongjo Parkb0225aa2018-04-20 14:00:15 +090059 };
60
Byeongjo Parkd157b792019-01-24 20:56:37 +090061 virtual status_t getBufferingSettings(
62 BufferingSettings* buffering /* nonnull */) override;
63 virtual status_t setBufferingSettings(const BufferingSettings& buffering) override;
64
65 virtual void prepareAsync();
66 virtual void start();
67 virtual void stop();
68 virtual void pause();
69 virtual void resume();
70
71 virtual status_t feedMoreTSData();
72
73 virtual status_t dequeueAccessUnit(bool audio, sp<ABuffer> *accessUnit);
74
75 virtual status_t getDuration(int64_t *durationUs);
76 virtual status_t seekTo(
77 int64_t seekTimeUs,
78 MediaPlayerSeekMode mode = MediaPlayerSeekMode::SEEK_PREVIOUS_SYNC) override;
79
80 void onMessageReceived(const sp<AMessage> &msg);
81
82protected:
83 virtual ~RTPSource();
84
85 virtual sp<MetaData> getFormatMeta(bool audio);
86
87private:
88 enum {
89 kWhatAccessUnit = 'accU',
90 kWhatAccessUnitComplete = 'accu',
91 kWhatDisconnect = 'disc',
92 kWhatEOS = 'eos!',
93 kWhatPollBuffering = 'poll',
94 kWhatSetBufferingSettings = 'sBuS',
95 };
96
Lajos Molnar19829332020-03-17 08:13:10 -070097 const int64_t kBufferingPollIntervalUs = 1000000ll;
98 const int32_t kMinVideoBitrate = 192000; /* bps */
99
Byeongjo Parkd157b792019-01-24 20:56:37 +0900100 enum State {
101 DISCONNECTED,
102 CONNECTING,
103 CONNECTED,
104 PAUSED,
105 };
106
107 struct TrackInfo {
108
109 /* SDP of track */
110 bool mIsAudio;
111 int32_t mPayloadType;
112 String8 mMimeType;
113 String8 mCodecName;
114 int32_t mCodecProfile;
115 int32_t mCodecLevel;
116 int32_t mWidth;
117 int32_t mHeight;
118 String8 mLocalIp;
119 String8 mRemoteIp;
120 int32_t mLocalPort;
121 int32_t mRemotePort;
Byeongjo Park88068dc2019-03-29 15:04:10 +0900122 int64_t mSocketNetwork;
Byeongjo Parkd157b792019-01-24 20:56:37 +0900123 int32_t mTimeScale;
124 int32_t mAS;
125
Kim Sungyeond2875e92018-03-20 16:51:41 +0900126 /* Unique ID indicates itself */
127 uint32_t mSelfID;
Byeongjo Parkb0225aa2018-04-20 14:00:15 +0900128 /* extmap:<value> for CVO will be set to here */
129 int32_t mCVOExtMap;
Kim Sungyeond2875e92018-03-20 16:51:41 +0900130
Byeongjo Parkd157b792019-01-24 20:56:37 +0900131 /* a copy of TrackInfo in RTSPSource */
132 sp<AnotherPacketSource> mSource;
133 uint32_t mRTPTime;
134 int64_t mNormalPlaytimeUs;
135 bool mNPTMappingValid;
136
137 /* a copy of TrackInfo in MyHandler.h */
138 int mRTPSocket;
139 int mRTCPSocket;
140 uint32_t mFirstSeqNumInSegment;
141 bool mNewSegment;
142 int32_t mAllowedStaleAccessUnits;
143 uint32_t mRTPAnchor;
144 int64_t mNTPAnchorUs;
145 bool mEOSReceived;
146 uint32_t mNormalPlayTimeRTP;
147 int64_t mNormalPlayTimeUs;
148 sp<APacketSource> mPacketSource;
149 List<sp<ABuffer>> mPackets;
150 };
151
152 const String8 mRTPParams;
153 uint32_t mFlags;
154 State mState;
155 status_t mFinalResult;
156
157 // below 3 parameters need to be checked whether it needed or not.
158 Mutex mBufferingLock;
159 bool mBuffering;
160 bool mInPreparationPhase;
161 Mutex mBufferingSettingsLock;
162 BufferingSettings mBufferingSettings;
163
164 sp<ALooper> mLooper;
165
166 sp<ARTPConnection> mRTPConn;
167
168 Vector<TrackInfo> mTracks;
169 sp<AnotherPacketSource> mAudioTrack;
170 sp<AnotherPacketSource> mVideoTrack;
171
172 int64_t mEOSTimeoutAudio;
173 int64_t mEOSTimeoutVideo;
174
175 /* MyHandler.h */
176 bool mFirstAccessUnit;
177 bool mAllTracksHaveTime;
178 int64_t mNTPAnchorUs;
179 int64_t mMediaAnchorUs;
180 int64_t mLastMediaTimeUs;
181 int64_t mNumAccessUnitsReceived;
Byeongjo Parkb0225aa2018-04-20 14:00:15 +0900182 int32_t mLastCVOUpdated;
Byeongjo Parkd157b792019-01-24 20:56:37 +0900183 bool mReceivedFirstRTCPPacket;
184 bool mReceivedFirstRTPPacket;
185 bool mPausing;
186 int32_t mPauseGeneration;
187
188 sp<AnotherPacketSource> getSource(bool audio);
189
190 /* MyHandler.h */
191 void onTimeUpdate(int32_t trackIndex, uint32_t rtpTime, uint64_t ntpTime);
192 bool addMediaTimestamp(int32_t trackIndex, const TrackInfo *track,
193 const sp<ABuffer> &accessUnit);
194 bool dataReceivedOnAllChannels();
195 void postQueueAccessUnit(size_t trackIndex, const sp<ABuffer> &accessUnit);
196 void postQueueEOS(size_t trackIndex, status_t finalResult);
197 sp<MetaData> getTrackFormat(size_t index, int32_t *timeScale);
198 void onConnected();
199 void onDisconnected(const sp<AMessage> &msg);
200
201 void schedulePollBuffering();
202 void onPollBuffering();
203
204 bool haveSufficientDataOnAllTracks();
205
206 void setEOSTimeout(bool audio, int64_t timeout);
207
208 status_t setParameters(const String8 &params);
209 status_t setParameter(const String8 &key, const String8 &value);
Byeongjo Park88068dc2019-03-29 15:04:10 +0900210 void setSocketNetwork(int64_t networkHandle);
Byeongjo Parkd157b792019-01-24 20:56:37 +0900211 static void TrimString(String8 *s);
212
213 DISALLOW_EVIL_CONSTRUCTORS(RTPSource);
214};
215
216} // namespace android
217
218#endif // RTP_SOURCE_H_