LiveSession.h revision ab8a0badb8fb1e294dacf2eb6a891439f348aff9
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
23namespace android {
24
25struct ABuffer;
26struct DataSource;
27struct LiveDataSource;
28struct M3UParser;
29struct NuHTTPDataSource;
30
31struct LiveSession : public AHandler {
32    LiveSession();
33
34    sp<DataSource> getDataSource();
35
36    void connect(const char *url);
37    void disconnect();
38
39    // Blocks until seek is complete.
40    void seekTo(int64_t timeUs);
41
42    status_t getDuration(int64_t *durationUs);
43    bool isSeekable();
44
45protected:
46    virtual ~LiveSession();
47
48    virtual void onMessageReceived(const sp<AMessage> &msg);
49
50private:
51    enum {
52        kMaxNumQueuedFragments = 3,
53        kMaxNumRetries         = 5,
54    };
55
56    static const int64_t kMaxPlaylistAgeUs;
57
58    enum {
59        kWhatConnect        = 'conn',
60        kWhatDisconnect     = 'disc',
61        kWhatMonitorQueue   = 'moni',
62        kWhatSeek           = 'seek',
63    };
64
65    struct BandwidthItem {
66        AString mURI;
67        unsigned long mBandwidth;
68    };
69
70    sp<LiveDataSource> mDataSource;
71
72    sp<NuHTTPDataSource> mHTTPDataSource;
73
74    AString mMasterURL;
75    Vector<BandwidthItem> mBandwidthItems;
76
77    KeyedVector<AString, sp<ABuffer> > mAESKeyForURI;
78
79    ssize_t mPrevBandwidthIndex;
80    int64_t mLastPlaylistFetchTimeUs;
81    sp<M3UParser> mPlaylist;
82    int32_t mSeqNumber;
83    int64_t mSeekTimeUs;
84    int32_t mNumRetries;
85
86    Mutex mLock;
87    Condition mCondition;
88    int64_t mDurationUs;
89    bool mSeekDone;
90    bool mDisconnectPending;
91
92    int32_t mMonitorQueueGeneration;
93
94    void onConnect(const sp<AMessage> &msg);
95    void onDisconnect();
96    void onDownloadNext();
97    void onMonitorQueue();
98    void onSeek(const sp<AMessage> &msg);
99
100    status_t fetchFile(const char *url, sp<ABuffer> *out);
101    sp<M3UParser> fetchPlaylist(const char *url);
102    size_t getBandwidthIndex();
103
104    status_t decryptBuffer(
105            size_t playlistIndex, const sp<ABuffer> &buffer);
106
107    void postMonitorQueue(int64_t delayUs = 0);
108
109    static int SortByBandwidth(const BandwidthItem *, const BandwidthItem *);
110
111    DISALLOW_EVIL_CONSTRUCTORS(LiveSession);
112};
113
114}  // namespace android
115
116#endif  // LIVE_SESSION_H_
117