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