Andreas Huber | b559084 | 2010-12-03 16:12:25 -0800 | [diff] [blame] | 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 LIVE_SESSION_H_ |
| 18 | |
| 19 | #define LIVE_SESSION_H_ |
| 20 | |
| 21 | #include <media/stagefright/foundation/AHandler.h> |
| 22 | |
Andreas Huber | 5087494 | 2011-04-19 11:50:27 -0700 | [diff] [blame] | 23 | #include <utils/String8.h> |
| 24 | |
Andreas Huber | b559084 | 2010-12-03 16:12:25 -0800 | [diff] [blame] | 25 | namespace android { |
| 26 | |
| 27 | struct ABuffer; |
| 28 | struct DataSource; |
| 29 | struct LiveDataSource; |
| 30 | struct M3UParser; |
Andreas Huber | 5f5719e | 2011-03-08 15:59:28 -0800 | [diff] [blame] | 31 | struct HTTPBase; |
Andreas Huber | b559084 | 2010-12-03 16:12:25 -0800 | [diff] [blame] | 32 | |
| 33 | struct LiveSession : public AHandler { |
Andreas Huber | 53182c4 | 2011-02-24 14:42:48 -0800 | [diff] [blame] | 34 | enum Flags { |
| 35 | // Don't log any URLs. |
| 36 | kFlagIncognito = 1, |
| 37 | }; |
| 38 | LiveSession(uint32_t flags = 0); |
Andreas Huber | b559084 | 2010-12-03 16:12:25 -0800 | [diff] [blame] | 39 | |
| 40 | sp<DataSource> getDataSource(); |
| 41 | |
Andreas Huber | 5087494 | 2011-04-19 11:50:27 -0700 | [diff] [blame] | 42 | void connect( |
| 43 | const char *url, |
| 44 | const KeyedVector<String8, String8> *headers = NULL); |
| 45 | |
Andreas Huber | b559084 | 2010-12-03 16:12:25 -0800 | [diff] [blame] | 46 | void disconnect(); |
| 47 | |
| 48 | // Blocks until seek is complete. |
| 49 | void seekTo(int64_t timeUs); |
| 50 | |
| 51 | status_t getDuration(int64_t *durationUs); |
| 52 | bool isSeekable(); |
| 53 | |
| 54 | protected: |
| 55 | virtual ~LiveSession(); |
| 56 | |
| 57 | virtual void onMessageReceived(const sp<AMessage> &msg); |
| 58 | |
| 59 | private: |
| 60 | enum { |
Andreas Huber | 08e10cb | 2011-01-05 12:17:08 -0800 | [diff] [blame] | 61 | kMaxNumQueuedFragments = 3, |
Andreas Huber | 54e6649 | 2010-12-23 10:27:40 -0800 | [diff] [blame] | 62 | kMaxNumRetries = 5, |
Andreas Huber | b559084 | 2010-12-03 16:12:25 -0800 | [diff] [blame] | 63 | }; |
| 64 | |
| 65 | static const int64_t kMaxPlaylistAgeUs; |
| 66 | |
| 67 | enum { |
| 68 | kWhatConnect = 'conn', |
| 69 | kWhatDisconnect = 'disc', |
| 70 | kWhatMonitorQueue = 'moni', |
| 71 | kWhatSeek = 'seek', |
| 72 | }; |
| 73 | |
| 74 | struct BandwidthItem { |
| 75 | AString mURI; |
| 76 | unsigned long mBandwidth; |
| 77 | }; |
| 78 | |
Andreas Huber | 53182c4 | 2011-02-24 14:42:48 -0800 | [diff] [blame] | 79 | uint32_t mFlags; |
| 80 | |
Andreas Huber | b559084 | 2010-12-03 16:12:25 -0800 | [diff] [blame] | 81 | sp<LiveDataSource> mDataSource; |
| 82 | |
Andreas Huber | 5f5719e | 2011-03-08 15:59:28 -0800 | [diff] [blame] | 83 | sp<HTTPBase> mHTTPDataSource; |
Andreas Huber | b559084 | 2010-12-03 16:12:25 -0800 | [diff] [blame] | 84 | |
| 85 | AString mMasterURL; |
Andreas Huber | 5087494 | 2011-04-19 11:50:27 -0700 | [diff] [blame] | 86 | KeyedVector<String8, String8> mExtraHeaders; |
| 87 | |
Andreas Huber | b559084 | 2010-12-03 16:12:25 -0800 | [diff] [blame] | 88 | Vector<BandwidthItem> mBandwidthItems; |
| 89 | |
| 90 | KeyedVector<AString, sp<ABuffer> > mAESKeyForURI; |
| 91 | |
| 92 | ssize_t mPrevBandwidthIndex; |
| 93 | int64_t mLastPlaylistFetchTimeUs; |
| 94 | sp<M3UParser> mPlaylist; |
| 95 | int32_t mSeqNumber; |
| 96 | int64_t mSeekTimeUs; |
| 97 | int32_t mNumRetries; |
| 98 | |
| 99 | Mutex mLock; |
| 100 | Condition mCondition; |
| 101 | int64_t mDurationUs; |
| 102 | bool mSeekDone; |
Andreas Huber | 17ab86c | 2011-01-31 16:18:49 -0800 | [diff] [blame] | 103 | bool mDisconnectPending; |
Andreas Huber | b559084 | 2010-12-03 16:12:25 -0800 | [diff] [blame] | 104 | |
| 105 | int32_t mMonitorQueueGeneration; |
| 106 | |
| 107 | void onConnect(const sp<AMessage> &msg); |
| 108 | void onDisconnect(); |
| 109 | void onDownloadNext(); |
| 110 | void onMonitorQueue(); |
| 111 | void onSeek(const sp<AMessage> &msg); |
| 112 | |
| 113 | status_t fetchFile(const char *url, sp<ABuffer> *out); |
| 114 | sp<M3UParser> fetchPlaylist(const char *url); |
| 115 | size_t getBandwidthIndex(); |
| 116 | |
| 117 | status_t decryptBuffer( |
| 118 | size_t playlistIndex, const sp<ABuffer> &buffer); |
| 119 | |
| 120 | void postMonitorQueue(int64_t delayUs = 0); |
| 121 | |
| 122 | static int SortByBandwidth(const BandwidthItem *, const BandwidthItem *); |
| 123 | |
| 124 | DISALLOW_EVIL_CONSTRUCTORS(LiveSession); |
| 125 | }; |
| 126 | |
| 127 | } // namespace android |
| 128 | |
| 129 | #endif // LIVE_SESSION_H_ |