blob: 99abe64c2fead0f9f77b9f71b09edceae9634ef8 [file] [log] [blame]
Andreas Huberb5590842010-12-03 16:12:25 -08001/*
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 Huber50874942011-04-19 11:50:27 -070023#include <utils/String8.h>
24
Andreas Huberb5590842010-12-03 16:12:25 -080025namespace android {
26
27struct ABuffer;
28struct DataSource;
29struct LiveDataSource;
30struct M3UParser;
Andreas Huber5f5719e2011-03-08 15:59:28 -080031struct HTTPBase;
Andreas Huberb5590842010-12-03 16:12:25 -080032
33struct LiveSession : public AHandler {
Andreas Huber53182c42011-02-24 14:42:48 -080034 enum Flags {
35 // Don't log any URLs.
36 kFlagIncognito = 1,
37 };
38 LiveSession(uint32_t flags = 0);
Andreas Huberb5590842010-12-03 16:12:25 -080039
40 sp<DataSource> getDataSource();
41
Andreas Huber50874942011-04-19 11:50:27 -070042 void connect(
43 const char *url,
44 const KeyedVector<String8, String8> *headers = NULL);
45
Andreas Huberb5590842010-12-03 16:12:25 -080046 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
54protected:
55 virtual ~LiveSession();
56
57 virtual void onMessageReceived(const sp<AMessage> &msg);
58
59private:
60 enum {
Andreas Huber08e10cb2011-01-05 12:17:08 -080061 kMaxNumQueuedFragments = 3,
Andreas Huber54e66492010-12-23 10:27:40 -080062 kMaxNumRetries = 5,
Andreas Huberb5590842010-12-03 16:12:25 -080063 };
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 Huber53182c42011-02-24 14:42:48 -080079 uint32_t mFlags;
80
Andreas Huberb5590842010-12-03 16:12:25 -080081 sp<LiveDataSource> mDataSource;
82
Andreas Huber5f5719e2011-03-08 15:59:28 -080083 sp<HTTPBase> mHTTPDataSource;
Andreas Huberb5590842010-12-03 16:12:25 -080084
85 AString mMasterURL;
Andreas Huber50874942011-04-19 11:50:27 -070086 KeyedVector<String8, String8> mExtraHeaders;
87
Andreas Huberb5590842010-12-03 16:12:25 -080088 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 Huber17ab86c2011-01-31 16:18:49 -0800103 bool mDisconnectPending;
Andreas Huberb5590842010-12-03 16:12:25 -0800104
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_