blob: 7666087afba305099e78fefd65ec9dbcd9896f89 [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
Kim Sungyeon7c5b5c02018-03-22 22:39:38 +090049const int32_t videoMinBitrate = 192000;
50
Byeongjo Park870c9d62019-01-24 20:56:37 +090051struct NuPlayer::RTPSource : public NuPlayer::Source {
52 RTPSource(
53 const sp<AMessage> &notify,
54 const String8& rtpParams);
55
Byeongjo Park8f935262018-04-20 14:00:15 +090056 enum {
57 RTCP_TSFB = 205,
58 RTCP_PSFB = 206,
59 RTP_CVO = 300,
Kim Sungyeond42c50d2019-03-17 22:04:14 +090060 RTP_AUTODOWN = 400,
Byeongjo Park8f935262018-04-20 14:00:15 +090061 };
62
Byeongjo Park870c9d62019-01-24 20:56:37 +090063 virtual status_t getBufferingSettings(
64 BufferingSettings* buffering /* nonnull */) override;
65 virtual status_t setBufferingSettings(const BufferingSettings& buffering) override;
66
67 virtual void prepareAsync();
68 virtual void start();
69 virtual void stop();
70 virtual void pause();
71 virtual void resume();
72
73 virtual status_t feedMoreTSData();
74
75 virtual status_t dequeueAccessUnit(bool audio, sp<ABuffer> *accessUnit);
76
77 virtual status_t getDuration(int64_t *durationUs);
78 virtual status_t seekTo(
79 int64_t seekTimeUs,
80 MediaPlayerSeekMode mode = MediaPlayerSeekMode::SEEK_PREVIOUS_SYNC) override;
81
82 void onMessageReceived(const sp<AMessage> &msg);
83
84protected:
85 virtual ~RTPSource();
86
87 virtual sp<MetaData> getFormatMeta(bool audio);
88
89private:
90 enum {
91 kWhatAccessUnit = 'accU',
92 kWhatAccessUnitComplete = 'accu',
93 kWhatDisconnect = 'disc',
94 kWhatEOS = 'eos!',
95 kWhatPollBuffering = 'poll',
96 kWhatSetBufferingSettings = 'sBuS',
97 };
98
99 enum State {
100 DISCONNECTED,
101 CONNECTING,
102 CONNECTED,
103 PAUSED,
104 };
105
106 struct TrackInfo {
107
108 /* SDP of track */
109 bool mIsAudio;
110 int32_t mPayloadType;
111 String8 mMimeType;
112 String8 mCodecName;
113 int32_t mCodecProfile;
114 int32_t mCodecLevel;
115 int32_t mWidth;
116 int32_t mHeight;
117 String8 mLocalIp;
118 String8 mRemoteIp;
119 int32_t mLocalPort;
120 int32_t mRemotePort;
Byeongjo Parkd1587172019-03-29 15:04:10 +0900121 int64_t mSocketNetwork;
Byeongjo Park870c9d62019-01-24 20:56:37 +0900122 int32_t mTimeScale;
123 int32_t mAS;
124
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_