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