blob: 739bd539aa71eff04af96e6400a5bca89e3bd06c [file] [log] [blame]
Wei Jia53692fa2017-12-11 10:33:46 -08001/*
2 * Copyright 2017 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
Wei Jia2409c872018-02-02 10:34:33 -080017#ifndef RTSP_SOURCE2_H_
Wei Jia53692fa2017-12-11 10:33:46 -080018
Wei Jia2409c872018-02-02 10:34:33 -080019#define RTSP_SOURCE2_H_
Wei Jia53692fa2017-12-11 10:33:46 -080020
21#include "NuPlayer2Source.h"
22
23#include "ATSParser.h"
24
25namespace android {
26
27struct ALooper;
28struct AReplyToken;
29struct AnotherPacketSource;
30struct MyHandler;
31struct SDPLoader;
32
Wei Jia2409c872018-02-02 10:34:33 -080033struct NuPlayer2::RTSPSource2 : public NuPlayer2::Source {
34 RTSPSource2(
Wei Jia53692fa2017-12-11 10:33:46 -080035 const sp<AMessage> &notify,
36 const sp<MediaHTTPService> &httpService,
37 const char *url,
38 const KeyedVector<String8, String8> *headers,
39 bool uidValid = false,
40 uid_t uid = 0,
41 bool isSDP = false);
42
43 virtual status_t getBufferingSettings(
44 BufferingSettings* buffering /* nonnull */) override;
45 virtual status_t setBufferingSettings(const BufferingSettings& buffering) override;
46
47 virtual void prepareAsync();
48 virtual void start();
49 virtual void stop();
50
51 virtual status_t feedMoreTSData();
52
53 virtual status_t dequeueAccessUnit(bool audio, sp<ABuffer> *accessUnit);
54
55 virtual status_t getDuration(int64_t *durationUs);
56 virtual status_t seekTo(
57 int64_t seekTimeUs,
58 MediaPlayer2SeekMode mode = MediaPlayer2SeekMode::SEEK_PREVIOUS_SYNC) override;
59
60 void onMessageReceived(const sp<AMessage> &msg);
61
62protected:
Wei Jia2409c872018-02-02 10:34:33 -080063 virtual ~RTSPSource2();
Wei Jia53692fa2017-12-11 10:33:46 -080064
65 virtual sp<MetaData> getFormatMeta(bool audio);
66
67private:
68 enum {
69 kWhatNotify = 'noti',
70 kWhatDisconnect = 'disc',
71 kWhatPerformSeek = 'seek',
72 kWhatPollBuffering = 'poll',
73 kWhatSignalEOS = 'eos ',
74 };
75
76 enum State {
77 DISCONNECTED,
78 CONNECTING,
79 CONNECTED,
80 SEEKING,
81 };
82
83 enum Flags {
84 // Don't log any URLs.
85 kFlagIncognito = 1,
86 };
87
88 struct TrackInfo {
89 sp<AnotherPacketSource> mSource;
90
91 int32_t mTimeScale;
92 uint32_t mRTPTime;
93 int64_t mNormalPlaytimeUs;
94 bool mNPTMappingValid;
95 };
96
97 sp<MediaHTTPService> mHTTPService;
98 AString mURL;
99 KeyedVector<String8, String8> mExtraHeaders;
100 bool mUIDValid;
101 uid_t mUID;
102 uint32_t mFlags;
103 bool mIsSDP;
104 State mState;
105 status_t mFinalResult;
106 sp<AReplyToken> mDisconnectReplyID;
107 Mutex mBufferingLock;
108 bool mBuffering;
109 bool mInPreparationPhase;
110 bool mEOSPending;
111
112 Mutex mBufferingSettingsLock;
113 BufferingSettings mBufferingSettings;
114
115 sp<ALooper> mLooper;
116 sp<MyHandler> mHandler;
117 sp<SDPLoader> mSDPLoader;
118
119 Vector<TrackInfo> mTracks;
120 sp<AnotherPacketSource> mAudioTrack;
121 sp<AnotherPacketSource> mVideoTrack;
122
123 sp<ATSParser> mTSParser;
124
125 int32_t mSeekGeneration;
126
127 int64_t mEOSTimeoutAudio;
128 int64_t mEOSTimeoutVideo;
129
130 sp<AReplyToken> mSeekReplyID;
131
132 sp<AnotherPacketSource> getSource(bool audio);
133
134 void onConnected();
135 void onSDPLoaded(const sp<AMessage> &msg);
136 void onDisconnected(const sp<AMessage> &msg);
137 void finishDisconnectIfPossible();
138
139 void performSeek(int64_t seekTimeUs);
140 void schedulePollBuffering();
141 void checkBuffering(
142 bool *prepared,
143 bool *underflow,
144 bool *overflow,
145 bool *startServer,
146 bool *finished);
147 void onPollBuffering();
148
149 bool haveSufficientDataOnAllTracks();
150
151 void setEOSTimeout(bool audio, int64_t timeout);
152 void setError(status_t err);
153 void startBufferingIfNecessary();
154 bool stopBufferingIfNecessary();
155 void finishSeek(status_t err);
156
157 void postSourceEOSIfNecessary();
158 void signalSourceEOS(status_t result);
159 void onSignalEOS(const sp<AMessage> &msg);
160
161 bool sourceNearEOS(bool audio);
162 bool sourceReachedEOS(bool audio);
163
Wei Jia2409c872018-02-02 10:34:33 -0800164 DISALLOW_EVIL_CONSTRUCTORS(RTSPSource2);
Wei Jia53692fa2017-12-11 10:33:46 -0800165};
166
167} // namespace android
168
Wei Jia2409c872018-02-02 10:34:33 -0800169#endif // RTSP_SOURCE2_H_