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