blob: efbf6d2496e1319af04a4663cc4ae9955f74a7ca [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;
121 int32_t mTimeScale;
122 int32_t mAS;
123
Kim Sungyeonfc88be82018-03-20 16:51:41 +0900124 /* Unique ID indicates itself */
125 uint32_t mSelfID;
Byeongjo Park8f935262018-04-20 14:00:15 +0900126 /* extmap:<value> for CVO will be set to here */
127 int32_t mCVOExtMap;
Kim Sungyeonfc88be82018-03-20 16:51:41 +0900128
Byeongjo Park870c9d62019-01-24 20:56:37 +0900129 /* a copy of TrackInfo in RTSPSource */
130 sp<AnotherPacketSource> mSource;
131 uint32_t mRTPTime;
132 int64_t mNormalPlaytimeUs;
133 bool mNPTMappingValid;
134
135 /* a copy of TrackInfo in MyHandler.h */
136 int mRTPSocket;
137 int mRTCPSocket;
138 uint32_t mFirstSeqNumInSegment;
139 bool mNewSegment;
140 int32_t mAllowedStaleAccessUnits;
141 uint32_t mRTPAnchor;
142 int64_t mNTPAnchorUs;
143 bool mEOSReceived;
144 uint32_t mNormalPlayTimeRTP;
145 int64_t mNormalPlayTimeUs;
146 sp<APacketSource> mPacketSource;
147 List<sp<ABuffer>> mPackets;
148 };
149
150 const String8 mRTPParams;
151 uint32_t mFlags;
152 State mState;
153 status_t mFinalResult;
154
155 // below 3 parameters need to be checked whether it needed or not.
156 Mutex mBufferingLock;
157 bool mBuffering;
158 bool mInPreparationPhase;
159 Mutex mBufferingSettingsLock;
160 BufferingSettings mBufferingSettings;
161
162 sp<ALooper> mLooper;
163
164 sp<ARTPConnection> mRTPConn;
165
166 Vector<TrackInfo> mTracks;
167 sp<AnotherPacketSource> mAudioTrack;
168 sp<AnotherPacketSource> mVideoTrack;
169
170 int64_t mEOSTimeoutAudio;
171 int64_t mEOSTimeoutVideo;
172
173 /* MyHandler.h */
174 bool mFirstAccessUnit;
175 bool mAllTracksHaveTime;
176 int64_t mNTPAnchorUs;
177 int64_t mMediaAnchorUs;
178 int64_t mLastMediaTimeUs;
179 int64_t mNumAccessUnitsReceived;
Byeongjo Park8f935262018-04-20 14:00:15 +0900180 int32_t mLastCVOUpdated;
Byeongjo Park870c9d62019-01-24 20:56:37 +0900181 bool mReceivedFirstRTCPPacket;
182 bool mReceivedFirstRTPPacket;
183 bool mPausing;
184 int32_t mPauseGeneration;
185
186 sp<AnotherPacketSource> getSource(bool audio);
187
188 /* MyHandler.h */
189 void onTimeUpdate(int32_t trackIndex, uint32_t rtpTime, uint64_t ntpTime);
190 bool addMediaTimestamp(int32_t trackIndex, const TrackInfo *track,
191 const sp<ABuffer> &accessUnit);
192 bool dataReceivedOnAllChannels();
193 void postQueueAccessUnit(size_t trackIndex, const sp<ABuffer> &accessUnit);
194 void postQueueEOS(size_t trackIndex, status_t finalResult);
195 sp<MetaData> getTrackFormat(size_t index, int32_t *timeScale);
196 void onConnected();
197 void onDisconnected(const sp<AMessage> &msg);
198
199 void schedulePollBuffering();
200 void onPollBuffering();
201
202 bool haveSufficientDataOnAllTracks();
203
204 void setEOSTimeout(bool audio, int64_t timeout);
205
206 status_t setParameters(const String8 &params);
207 status_t setParameter(const String8 &key, const String8 &value);
208 static void TrimString(String8 *s);
209
210 DISALLOW_EVIL_CONSTRUCTORS(RTPSource);
211};
212
213} // namespace android
214
215#endif // RTP_SOURCE_H_