blob: 6c618ecd78d6a92e4b570728bc37bd46211baced [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
115 /* a copy of TrackInfo in RTSPSource */
116 sp<AnotherPacketSource> mSource;
117 uint32_t mRTPTime;
118 int64_t mNormalPlaytimeUs;
119 bool mNPTMappingValid;
120
121 /* a copy of TrackInfo in MyHandler.h */
122 int mRTPSocket;
123 int mRTCPSocket;
124 uint32_t mFirstSeqNumInSegment;
125 bool mNewSegment;
126 int32_t mAllowedStaleAccessUnits;
127 uint32_t mRTPAnchor;
128 int64_t mNTPAnchorUs;
129 bool mEOSReceived;
130 uint32_t mNormalPlayTimeRTP;
131 int64_t mNormalPlayTimeUs;
132 sp<APacketSource> mPacketSource;
133 List<sp<ABuffer>> mPackets;
134 };
135
136 const String8 mRTPParams;
137 uint32_t mFlags;
138 State mState;
139 status_t mFinalResult;
140
141 // below 3 parameters need to be checked whether it needed or not.
142 Mutex mBufferingLock;
143 bool mBuffering;
144 bool mInPreparationPhase;
145 Mutex mBufferingSettingsLock;
146 BufferingSettings mBufferingSettings;
147
148 sp<ALooper> mLooper;
149
150 sp<ARTPConnection> mRTPConn;
151
152 Vector<TrackInfo> mTracks;
153 sp<AnotherPacketSource> mAudioTrack;
154 sp<AnotherPacketSource> mVideoTrack;
155
156 int64_t mEOSTimeoutAudio;
157 int64_t mEOSTimeoutVideo;
158
159 /* MyHandler.h */
160 bool mFirstAccessUnit;
161 bool mAllTracksHaveTime;
162 int64_t mNTPAnchorUs;
163 int64_t mMediaAnchorUs;
164 int64_t mLastMediaTimeUs;
165 int64_t mNumAccessUnitsReceived;
166 bool mReceivedFirstRTCPPacket;
167 bool mReceivedFirstRTPPacket;
168 bool mPausing;
169 int32_t mPauseGeneration;
170
171 sp<AnotherPacketSource> getSource(bool audio);
172
173 /* MyHandler.h */
174 void onTimeUpdate(int32_t trackIndex, uint32_t rtpTime, uint64_t ntpTime);
175 bool addMediaTimestamp(int32_t trackIndex, const TrackInfo *track,
176 const sp<ABuffer> &accessUnit);
177 bool dataReceivedOnAllChannels();
178 void postQueueAccessUnit(size_t trackIndex, const sp<ABuffer> &accessUnit);
179 void postQueueEOS(size_t trackIndex, status_t finalResult);
180 sp<MetaData> getTrackFormat(size_t index, int32_t *timeScale);
181 void onConnected();
182 void onDisconnected(const sp<AMessage> &msg);
183
184 void schedulePollBuffering();
185 void onPollBuffering();
186
187 bool haveSufficientDataOnAllTracks();
188
189 void setEOSTimeout(bool audio, int64_t timeout);
190
191 status_t setParameters(const String8 &params);
192 status_t setParameter(const String8 &key, const String8 &value);
193 static void TrimString(String8 *s);
194
195 DISALLOW_EVIL_CONSTRUCTORS(RTPSource);
196};
197
198} // namespace android
199
200#endif // RTP_SOURCE_H_