Byeongjo Park | 870c9d6 | 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> |
| 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 | |
| 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 | 8f93526 | 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 | d42c50d | 2019-03-17 22:04:14 +0900 | [diff] [blame] | 58 | RTP_AUTODOWN = 400, |
Byeongjo Park | 8f93526 | 2018-04-20 14:00:15 +0900 | [diff] [blame] | 59 | }; |
| 60 | |
Byeongjo Park | 870c9d6 | 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 | 638df3a | 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 | 870c9d6 | 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 | d158717 | 2019-03-29 15:04:10 +0900 | [diff] [blame] | 122 | int64_t mSocketNetwork; |
Byeongjo Park | 870c9d6 | 2019-01-24 20:56:37 +0900 | [diff] [blame] | 123 | int32_t mTimeScale; |
| 124 | int32_t mAS; |
| 125 | |
Byeongjo Park | 75adcb7 | 2019-09-23 13:31:37 +0900 | [diff] [blame^] | 126 | /* RTP jitter buffer time in millsecond */ |
| 127 | uint32_t mJbTime; |
Kim Sungyeon | fc88be8 | 2018-03-20 16:51:41 +0900 | [diff] [blame] | 128 | /* Unique ID indicates itself */ |
| 129 | uint32_t mSelfID; |
Byeongjo Park | 8f93526 | 2018-04-20 14:00:15 +0900 | [diff] [blame] | 130 | /* extmap:<value> for CVO will be set to here */ |
| 131 | int32_t mCVOExtMap; |
Kim Sungyeon | fc88be8 | 2018-03-20 16:51:41 +0900 | [diff] [blame] | 132 | |
Byeongjo Park | 870c9d6 | 2019-01-24 20:56:37 +0900 | [diff] [blame] | 133 | /* a copy of TrackInfo in RTSPSource */ |
| 134 | sp<AnotherPacketSource> mSource; |
| 135 | uint32_t mRTPTime; |
| 136 | int64_t mNormalPlaytimeUs; |
| 137 | bool mNPTMappingValid; |
| 138 | |
| 139 | /* a copy of TrackInfo in MyHandler.h */ |
| 140 | int mRTPSocket; |
| 141 | int mRTCPSocket; |
| 142 | uint32_t mFirstSeqNumInSegment; |
| 143 | bool mNewSegment; |
| 144 | int32_t mAllowedStaleAccessUnits; |
| 145 | uint32_t mRTPAnchor; |
| 146 | int64_t mNTPAnchorUs; |
| 147 | bool mEOSReceived; |
| 148 | uint32_t mNormalPlayTimeRTP; |
| 149 | int64_t mNormalPlayTimeUs; |
| 150 | sp<APacketSource> mPacketSource; |
| 151 | List<sp<ABuffer>> mPackets; |
| 152 | }; |
| 153 | |
| 154 | const String8 mRTPParams; |
| 155 | uint32_t mFlags; |
| 156 | State mState; |
| 157 | status_t mFinalResult; |
| 158 | |
| 159 | // below 3 parameters need to be checked whether it needed or not. |
| 160 | Mutex mBufferingLock; |
| 161 | bool mBuffering; |
| 162 | bool mInPreparationPhase; |
| 163 | Mutex mBufferingSettingsLock; |
| 164 | BufferingSettings mBufferingSettings; |
| 165 | |
| 166 | sp<ALooper> mLooper; |
| 167 | |
| 168 | sp<ARTPConnection> mRTPConn; |
| 169 | |
| 170 | Vector<TrackInfo> mTracks; |
| 171 | sp<AnotherPacketSource> mAudioTrack; |
| 172 | sp<AnotherPacketSource> mVideoTrack; |
| 173 | |
| 174 | int64_t mEOSTimeoutAudio; |
| 175 | int64_t mEOSTimeoutVideo; |
| 176 | |
| 177 | /* MyHandler.h */ |
| 178 | bool mFirstAccessUnit; |
| 179 | bool mAllTracksHaveTime; |
| 180 | int64_t mNTPAnchorUs; |
| 181 | int64_t mMediaAnchorUs; |
| 182 | int64_t mLastMediaTimeUs; |
| 183 | int64_t mNumAccessUnitsReceived; |
Byeongjo Park | 8f93526 | 2018-04-20 14:00:15 +0900 | [diff] [blame] | 184 | int32_t mLastCVOUpdated; |
Byeongjo Park | 870c9d6 | 2019-01-24 20:56:37 +0900 | [diff] [blame] | 185 | bool mReceivedFirstRTCPPacket; |
| 186 | bool mReceivedFirstRTPPacket; |
| 187 | bool mPausing; |
| 188 | int32_t mPauseGeneration; |
| 189 | |
| 190 | sp<AnotherPacketSource> getSource(bool audio); |
| 191 | |
| 192 | /* MyHandler.h */ |
| 193 | void onTimeUpdate(int32_t trackIndex, uint32_t rtpTime, uint64_t ntpTime); |
| 194 | bool addMediaTimestamp(int32_t trackIndex, const TrackInfo *track, |
| 195 | const sp<ABuffer> &accessUnit); |
| 196 | bool dataReceivedOnAllChannels(); |
| 197 | void postQueueAccessUnit(size_t trackIndex, const sp<ABuffer> &accessUnit); |
| 198 | void postQueueEOS(size_t trackIndex, status_t finalResult); |
| 199 | sp<MetaData> getTrackFormat(size_t index, int32_t *timeScale); |
| 200 | void onConnected(); |
| 201 | void onDisconnected(const sp<AMessage> &msg); |
| 202 | |
| 203 | void schedulePollBuffering(); |
| 204 | void onPollBuffering(); |
| 205 | |
| 206 | bool haveSufficientDataOnAllTracks(); |
| 207 | |
| 208 | void setEOSTimeout(bool audio, int64_t timeout); |
| 209 | |
| 210 | status_t setParameters(const String8 ¶ms); |
| 211 | status_t setParameter(const String8 &key, const String8 &value); |
Byeongjo Park | d158717 | 2019-03-29 15:04:10 +0900 | [diff] [blame] | 212 | void setSocketNetwork(int64_t networkHandle); |
Byeongjo Park | 870c9d6 | 2019-01-24 20:56:37 +0900 | [diff] [blame] | 213 | static void TrimString(String8 *s); |
| 214 | |
| 215 | DISALLOW_EVIL_CONSTRUCTORS(RTPSource); |
| 216 | }; |
| 217 | |
| 218 | } // namespace android |
| 219 | |
| 220 | #endif // RTP_SOURCE_H_ |