PlaybackSession.h revision e7bd24af08ef0722fb124a550662bcec48c56f86
1/* 2 * Copyright 2012, 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 PLAYBACK_SESSION_H_ 18 19#define PLAYBACK_SESSION_H_ 20 21#include "WifiDisplaySource.h" 22 23namespace android { 24 25struct ABuffer; 26struct BufferQueue; 27struct IHDCP; 28struct ISurfaceTexture; 29struct MediaPuller; 30struct MediaSource; 31struct TSPacketizer; 32 33#define LOG_TRANSPORT_STREAM 0 34#define ENABLE_RETRANSMISSION 0 35#define TRACK_BANDWIDTH 0 36 37// Encapsulates the state of an RTP/RTCP session in the context of wifi 38// display. 39struct WifiDisplaySource::PlaybackSession : public AHandler { 40 PlaybackSession( 41 const sp<ANetworkSession> &netSession, 42 const sp<AMessage> ¬ify, 43 const struct in_addr &interfaceAddr, 44 const sp<IHDCP> &hdcp); 45 46 enum TransportMode { 47 TRANSPORT_UDP, 48 TRANSPORT_TCP_INTERLEAVED, 49 TRANSPORT_TCP, 50 }; 51 status_t init( 52 const char *clientIP, int32_t clientRtp, int32_t clientRtcp, 53 TransportMode transportMode, 54 bool usePCMAudio); 55 56 void destroyAsync(); 57 58 int32_t getRTPPort() const; 59 60 int64_t getLastLifesignUs() const; 61 void updateLiveness(); 62 63 status_t play(); 64 status_t finishPlay(); 65 status_t pause(); 66 67 sp<ISurfaceTexture> getSurfaceTexture(); 68 int32_t width() const; 69 int32_t height() const; 70 71 void requestIDRFrame(); 72 73 enum { 74 kWhatSessionDead, 75 kWhatBinaryData, 76 kWhatSessionEstablished, 77 kWhatSessionDestroyed, 78 }; 79 80protected: 81 virtual void onMessageReceived(const sp<AMessage> &msg); 82 virtual ~PlaybackSession(); 83 84private: 85 struct Track; 86 87 enum { 88 kWhatSendSR, 89 kWhatRTPNotify, 90 kWhatRTCPNotify, 91#if ENABLE_RETRANSMISSION 92 kWhatRTPRetransmissionNotify, 93 kWhatRTCPRetransmissionNotify, 94#endif 95 kWhatMediaPullerNotify, 96 kWhatConverterNotify, 97 kWhatTrackNotify, 98 kWhatUpdateSurface, 99 kWhatFinishPlay, 100 }; 101 102 static const int64_t kSendSRIntervalUs = 10000000ll; 103 static const uint32_t kSourceID = 0xdeadbeef; 104 static const size_t kMaxHistoryLength = 128; 105 106#if ENABLE_RETRANSMISSION 107 static const size_t kRetransmissionPortOffset = 120; 108#endif 109 110 sp<ANetworkSession> mNetSession; 111 sp<AMessage> mNotify; 112 in_addr mInterfaceAddr; 113 sp<IHDCP> mHDCP; 114 bool mWeAreDead; 115 116 int64_t mLastLifesignUs; 117 118 sp<TSPacketizer> mPacketizer; 119 sp<BufferQueue> mBufferQueue; 120 121 KeyedVector<size_t, sp<Track> > mTracks; 122 ssize_t mVideoTrackIndex; 123 124 sp<ABuffer> mTSQueue; 125 int64_t mPrevTimeUs; 126 127 TransportMode mTransportMode; 128 129 AString mClientIP; 130 131 bool mAllTracksHavePacketizerIndex; 132 133 // in TCP mode 134 int32_t mRTPChannel; 135 int32_t mRTCPChannel; 136 137 // in UDP mode 138 int32_t mRTPPort; 139 int32_t mRTPSessionID; 140 int32_t mRTCPSessionID; 141 142#if ENABLE_RETRANSMISSION 143 int32_t mRTPRetransmissionSessionID; 144 int32_t mRTCPRetransmissionSessionID; 145#endif 146 147 int32_t mClientRTPPort; 148 int32_t mClientRTCPPort; 149 bool mRTPConnected; 150 bool mRTCPConnected; 151 152 uint32_t mRTPSeqNo; 153#if ENABLE_RETRANSMISSION 154 uint32_t mRTPRetransmissionSeqNo; 155#endif 156 157 uint64_t mLastNTPTime; 158 uint32_t mLastRTPTime; 159 uint32_t mNumRTPSent; 160 uint32_t mNumRTPOctetsSent; 161 uint32_t mNumSRsSent; 162 163 bool mSendSRPending; 164 165#if ENABLE_RETRANSMISSION 166 List<sp<ABuffer> > mHistory; 167 size_t mHistoryLength; 168#endif 169 170#if TRACK_BANDWIDTH 171 int64_t mFirstPacketTimeUs; 172 uint64_t mTotalBytesSent; 173#endif 174 175#if LOG_TRANSPORT_STREAM 176 FILE *mLogFile; 177#endif 178 179 void onSendSR(); 180 void addSR(const sp<ABuffer> &buffer); 181 void addSDES(const sp<ABuffer> &buffer); 182 static uint64_t GetNowNTP(); 183 184 status_t setupPacketizer(bool usePCMAudio); 185 186 status_t addSource( 187 bool isVideo, 188 const sp<MediaSource> &source, 189 bool isRepeaterSource, 190 bool usePCMAudio, 191 size_t *numInputBuffers); 192 193 status_t addVideoSource(); 194 status_t addAudioSource(bool usePCMAudio); 195 196 ssize_t appendTSData( 197 const void *data, size_t size, bool timeDiscontinuity, bool flush); 198 199 void scheduleSendSR(); 200 201 status_t parseRTCP(const sp<ABuffer> &buffer); 202 203#if ENABLE_RETRANSMISSION 204 status_t parseTSFB(const uint8_t *data, size_t size); 205#endif 206 207 status_t sendPacket(int32_t sessionID, const void *data, size_t size); 208 status_t onFinishPlay(); 209 status_t onFinishPlay2(); 210 211 bool allTracksHavePacketizerIndex(); 212 213 status_t packetizeAccessUnit( 214 size_t trackIndex, const sp<ABuffer> &accessUnit); 215 216 status_t packetizeQueuedAccessUnits(); 217 218 void notifySessionDead(); 219 220 DISALLOW_EVIL_CONSTRUCTORS(PlaybackSession); 221}; 222 223} // namespace android 224 225#endif // PLAYBACK_SESSION_H_ 226 227