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