Andreas Huber | 14f7672 | 2013-01-15 09:04:18 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 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 PLAYLIST_FETCHER_H_ |
| 18 | |
| 19 | #define PLAYLIST_FETCHER_H_ |
| 20 | |
| 21 | #include <media/stagefright/foundation/AHandler.h> |
| 22 | |
| 23 | #include "mpeg2ts/ATSParser.h" |
| 24 | #include "LiveSession.h" |
| 25 | |
| 26 | namespace android { |
| 27 | |
| 28 | struct ABuffer; |
| 29 | struct AnotherPacketSource; |
| 30 | struct DataSource; |
| 31 | struct HTTPBase; |
| 32 | struct LiveDataSource; |
| 33 | struct M3UParser; |
| 34 | struct String8; |
| 35 | |
| 36 | struct PlaylistFetcher : public AHandler { |
| 37 | enum { |
| 38 | kWhatStarted, |
| 39 | kWhatPaused, |
| 40 | kWhatStopped, |
| 41 | kWhatError, |
| 42 | kWhatDurationUpdate, |
| 43 | kWhatTemporarilyDoneFetching, |
| 44 | kWhatPrepared, |
| 45 | kWhatPreparationFailed, |
| 46 | }; |
| 47 | |
| 48 | PlaylistFetcher( |
| 49 | const sp<AMessage> ¬ify, |
| 50 | const sp<LiveSession> &session, |
| 51 | const char *uri); |
| 52 | |
| 53 | sp<DataSource> getDataSource(); |
| 54 | |
| 55 | void startAsync( |
| 56 | const sp<AnotherPacketSource> &audioSource, |
| 57 | const sp<AnotherPacketSource> &videoSource, |
| 58 | const sp<AnotherPacketSource> &subtitleSource, |
| 59 | int64_t startTimeUs = -1ll); |
| 60 | |
| 61 | void pauseAsync(); |
| 62 | |
| 63 | void stopAsync(); |
| 64 | |
| 65 | protected: |
| 66 | virtual ~PlaylistFetcher(); |
| 67 | virtual void onMessageReceived(const sp<AMessage> &msg); |
| 68 | |
| 69 | private: |
| 70 | enum { |
| 71 | kMaxNumRetries = 5, |
| 72 | }; |
| 73 | |
| 74 | enum { |
| 75 | kWhatStart = 'strt', |
| 76 | kWhatPause = 'paus', |
| 77 | kWhatStop = 'stop', |
| 78 | kWhatMonitorQueue = 'moni', |
| 79 | }; |
| 80 | |
| 81 | static const int64_t kMinBufferedDurationUs; |
| 82 | |
| 83 | sp<AMessage> mNotify; |
| 84 | sp<LiveSession> mSession; |
| 85 | AString mURI; |
| 86 | |
| 87 | uint32_t mStreamTypeMask; |
| 88 | int64_t mStartTimeUs; |
| 89 | |
| 90 | KeyedVector<LiveSession::StreamType, sp<AnotherPacketSource> > |
| 91 | mPacketSources; |
| 92 | |
| 93 | KeyedVector<AString, sp<ABuffer> > mAESKeyForURI; |
| 94 | |
| 95 | int64_t mLastPlaylistFetchTimeUs; |
| 96 | sp<M3UParser> mPlaylist; |
| 97 | int32_t mSeqNumber; |
| 98 | int32_t mNumRetries; |
| 99 | bool mStartup; |
| 100 | int64_t mNextPTSTimeUs; |
| 101 | |
| 102 | int32_t mMonitorQueueGeneration; |
| 103 | |
| 104 | enum RefreshState { |
| 105 | INITIAL_MINIMUM_RELOAD_DELAY, |
| 106 | FIRST_UNCHANGED_RELOAD_ATTEMPT, |
| 107 | SECOND_UNCHANGED_RELOAD_ATTEMPT, |
| 108 | THIRD_UNCHANGED_RELOAD_ATTEMPT |
| 109 | }; |
| 110 | RefreshState mRefreshState; |
| 111 | |
| 112 | uint8_t mPlaylistHash[16]; |
| 113 | |
| 114 | sp<ATSParser> mTSParser; |
| 115 | |
| 116 | bool mFirstPTSValid; |
| 117 | uint64_t mFirstPTS; |
| 118 | int64_t mAbsoluteTimeAnchorUs; |
| 119 | |
| 120 | status_t decryptBuffer( |
| 121 | size_t playlistIndex, const sp<ABuffer> &buffer); |
| 122 | |
| 123 | void postMonitorQueue(int64_t delayUs = 0); |
| 124 | void cancelMonitorQueue(); |
| 125 | |
| 126 | bool timeToRefreshPlaylist(int64_t nowUs) const; |
| 127 | |
| 128 | // Returns the media time in us of the segment specified by seqNumber. |
| 129 | // This is computed by summing the durations of all segments before it. |
| 130 | int64_t getSegmentStartTimeUs(int32_t seqNumber) const; |
| 131 | |
| 132 | status_t onStart(const sp<AMessage> &msg); |
| 133 | void onPause(); |
| 134 | void onStop(); |
| 135 | void onMonitorQueue(); |
| 136 | void onDownloadNext(); |
| 137 | |
Chong Zhang | dcb89b3 | 2013-08-06 09:44:47 -0700 | [diff] [blame^] | 138 | status_t extractAndQueueAccessUnits( |
| 139 | const sp<ABuffer> &buffer, const sp<AMessage> &itemMeta); |
Andreas Huber | 14f7672 | 2013-01-15 09:04:18 -0800 | [diff] [blame] | 140 | |
| 141 | void notifyError(status_t err); |
| 142 | |
| 143 | void queueDiscontinuity( |
| 144 | ATSParser::DiscontinuityType type, const sp<AMessage> &extra); |
| 145 | |
| 146 | int32_t getSeqNumberForTime(int64_t timeUs) const; |
| 147 | |
| 148 | void updateDuration(); |
| 149 | |
| 150 | DISALLOW_EVIL_CONSTRUCTORS(PlaylistFetcher); |
| 151 | }; |
| 152 | |
| 153 | } // namespace android |
| 154 | |
| 155 | #endif // PLAYLIST_FETCHER_H_ |
| 156 | |