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