blob: a6a76445e88ee3e18e4f8f5c2fee81432971f668 [file] [log] [blame]
Andreas Huber2bfdd422011-10-11 15:24:07 -07001/*
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 RTSP_SOURCE_H_
18
19#define RTSP_SOURCE_H_
20
21#include "NuPlayerSource.h"
22
Andreas Hubercfaeeec2012-08-31 10:27:46 -070023#include "ATSParser.h"
24
Andreas Huber2bfdd422011-10-11 15:24:07 -070025namespace android {
26
27struct ALooper;
Lajos Molnar3f274362015-03-05 14:35:41 -080028struct AReplyToken;
Andreas Huber2bfdd422011-10-11 15:24:07 -070029struct AnotherPacketSource;
30struct MyHandler;
Oscar Rydhé7a33b772012-02-20 10:15:48 +010031struct SDPLoader;
Andreas Huber2bfdd422011-10-11 15:24:07 -070032
33struct NuPlayer::RTSPSource : public NuPlayer::Source {
34 RTSPSource(
Andreas Huberb5f25f02013-02-05 10:14:26 -080035 const sp<AMessage> &notify,
Andreas Huber1b86fe02014-01-29 11:13:26 -080036 const sp<IMediaHTTPService> &httpService,
Andreas Huber2bfdd422011-10-11 15:24:07 -070037 const char *url,
38 const KeyedVector<String8, String8> *headers,
39 bool uidValid = false,
Oscar Rydhé7a33b772012-02-20 10:15:48 +010040 uid_t uid = 0,
41 bool isSDP = false);
Andreas Huber2bfdd422011-10-11 15:24:07 -070042
Andreas Huber9575c962013-02-05 13:59:56 -080043 virtual void prepareAsync();
Andreas Huber2bfdd422011-10-11 15:24:07 -070044 virtual void start();
45 virtual void stop();
46
47 virtual status_t feedMoreTSData();
48
Andreas Huber2bfdd422011-10-11 15:24:07 -070049 virtual status_t dequeueAccessUnit(bool audio, sp<ABuffer> *accessUnit);
50
51 virtual status_t getDuration(int64_t *durationUs);
52 virtual status_t seekTo(int64_t seekTimeUs);
Andreas Huberb7c8e912012-11-27 15:02:53 -080053
Andreas Huber2bfdd422011-10-11 15:24:07 -070054 void onMessageReceived(const sp<AMessage> &msg);
55
56protected:
57 virtual ~RTSPSource();
58
Andreas Huber84066782011-08-16 09:34:26 -070059 virtual sp<MetaData> getFormatMeta(bool audio);
60
Andreas Huber2bfdd422011-10-11 15:24:07 -070061private:
62 enum {
63 kWhatNotify = 'noti',
64 kWhatDisconnect = 'disc',
Andreas Huberee736e92011-12-08 13:04:50 -080065 kWhatPerformSeek = 'seek',
Robert Shih641e0c72016-02-22 11:37:20 -080066 kWhatPollBuffering = 'poll',
Andreas Huber2bfdd422011-10-11 15:24:07 -070067 };
68
69 enum State {
70 DISCONNECTED,
71 CONNECTING,
72 CONNECTED,
73 SEEKING,
74 };
75
76 enum Flags {
77 // Don't log any URLs.
78 kFlagIncognito = 1,
79 };
80
Robert Shih641e0c72016-02-22 11:37:20 -080081 // Buffer Prepare/Underflow/Overflow/Resume Marks
82 static const int64_t kPrepareMarkUs;
83 static const int64_t kUnderflowMarkUs;
84 static const int64_t kOverflowMarkUs;
85 static const int64_t kStartServerMarkUs;
86
Andreas Huber2bfdd422011-10-11 15:24:07 -070087 struct TrackInfo {
88 sp<AnotherPacketSource> mSource;
89
90 int32_t mTimeScale;
91 uint32_t mRTPTime;
92 int64_t mNormalPlaytimeUs;
Andreas Huber1906e5c2011-12-08 12:27:47 -080093 bool mNPTMappingValid;
Andreas Huber2bfdd422011-10-11 15:24:07 -070094 };
95
Andreas Huber1b86fe02014-01-29 11:13:26 -080096 sp<IMediaHTTPService> mHTTPService;
Andreas Huber2bfdd422011-10-11 15:24:07 -070097 AString mURL;
98 KeyedVector<String8, String8> mExtraHeaders;
99 bool mUIDValid;
100 uid_t mUID;
101 uint32_t mFlags;
Oscar Rydhé7a33b772012-02-20 10:15:48 +0100102 bool mIsSDP;
Andreas Huber2bfdd422011-10-11 15:24:07 -0700103 State mState;
104 status_t mFinalResult;
Lajos Molnar3f274362015-03-05 14:35:41 -0800105 sp<AReplyToken> mDisconnectReplyID;
Chong Zhang180d1b92014-12-02 18:35:35 -0800106 Mutex mBufferingLock;
Roger Jönssonb50e83e2013-01-21 16:26:41 +0100107 bool mBuffering;
Robert Shih641e0c72016-02-22 11:37:20 -0800108 bool mInPreparationPhase;
Andreas Huber2bfdd422011-10-11 15:24:07 -0700109
110 sp<ALooper> mLooper;
Andreas Huber2bfdd422011-10-11 15:24:07 -0700111 sp<MyHandler> mHandler;
Oscar Rydhé7a33b772012-02-20 10:15:48 +0100112 sp<SDPLoader> mSDPLoader;
Andreas Huber2bfdd422011-10-11 15:24:07 -0700113
114 Vector<TrackInfo> mTracks;
115 sp<AnotherPacketSource> mAudioTrack;
116 sp<AnotherPacketSource> mVideoTrack;
117
Andreas Hubercfaeeec2012-08-31 10:27:46 -0700118 sp<ATSParser> mTSParser;
119
Andreas Huberee736e92011-12-08 13:04:50 -0800120 int32_t mSeekGeneration;
121
Roger Jönssonb50e83e2013-01-21 16:26:41 +0100122 int64_t mEOSTimeoutAudio;
123 int64_t mEOSTimeoutVideo;
124
Robert Shih8d237a52015-07-13 17:59:36 -0700125 sp<AReplyToken> mSeekReplyID;
126
Andreas Huber2bfdd422011-10-11 15:24:07 -0700127 sp<AnotherPacketSource> getSource(bool audio);
128
129 void onConnected();
Oscar Rydhé7a33b772012-02-20 10:15:48 +0100130 void onSDPLoaded(const sp<AMessage> &msg);
Andreas Huber2bfdd422011-10-11 15:24:07 -0700131 void onDisconnected(const sp<AMessage> &msg);
132 void finishDisconnectIfPossible();
133
Andreas Huberee736e92011-12-08 13:04:50 -0800134 void performSeek(int64_t seekTimeUs);
Robert Shih641e0c72016-02-22 11:37:20 -0800135 void schedulePollBuffering();
136 void checkBuffering(bool *prepared, bool *underflow, bool *overflow, bool *startServer);
137 void onPollBuffering();
Andreas Huberee736e92011-12-08 13:04:50 -0800138
Andreas Huberbfd4d0d2012-05-17 14:18:50 -0700139 bool haveSufficientDataOnAllTracks();
140
Roger Jönssonb50e83e2013-01-21 16:26:41 +0100141 void setEOSTimeout(bool audio, int64_t timeout);
Chong Zhang180d1b92014-12-02 18:35:35 -0800142 void setError(status_t err);
143 void startBufferingIfNecessary();
144 bool stopBufferingIfNecessary();
Robert Shih8d237a52015-07-13 17:59:36 -0700145 void finishSeek(status_t err);
Roger Jönssonb50e83e2013-01-21 16:26:41 +0100146
Andreas Huber2bfdd422011-10-11 15:24:07 -0700147 DISALLOW_EVIL_CONSTRUCTORS(RTSPSource);
148};
149
150} // namespace android
151
152#endif // RTSP_SOURCE_H_