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