1 /*
2  * Copyright 2015 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 HTTP_DOWNLOADER_H_
18 
19 #define HTTP_DOWNLOADER_H_
20 
21 #include <media/stagefright/foundation/ADebug.h>
22 #include <utils/KeyedVector.h>
23 #include <utils/Mutex.h>
24 #include <utils/RefBase.h>
25 
26 namespace android {
27 
28 struct ABuffer;
29 class DataSource;
30 struct HTTPBase;
31 struct MediaHTTPService;
32 struct M3UParser;
33 
34 struct HTTPDownloader : public RefBase {
35     HTTPDownloader(
36             const sp<MediaHTTPService> &httpService,
37             const KeyedVector<String8, String8> &headers);
38 
39     void reconnect();
40     void disconnect();
41     bool isDisconnecting();
42     // If given a non-zero block_size (default 0), it is used to cap the number of
43     // bytes read in from the DataSource. If given a non-NULL buffer, new content
44     // is read into the end.
45     //
46     // The DataSource we read from is responsible for signaling error or EOF to help us
47     // break out of the read loop. The DataSource can be returned to the caller, so
48     // that the caller can reuse it for subsequent fetches (within the initially
49     // requested range).
50     //
51     // For reused HTTP sources, the caller must download a file sequentially without
52     // any overlaps or gaps to prevent reconnection.
53     ssize_t fetchBlock(
54             const char *url,
55             sp<ABuffer> *out,
56             int64_t range_offset, /* open file at range_offset */
57             int64_t range_length, /* open file for range_length (-1: entire file) */
58             uint32_t block_size,  /* download block size (0: entire range) */
59             String8 *actualUrl,   /* returns actual URL */
60             bool reconnect        /* force connect http */
61             );
62 
63     // simplified version to fetch a single file
64     ssize_t fetchFile(
65             const char *url,
66             sp<ABuffer> *out,
67             String8 *actualUrl = NULL);
68 
69     // fetch a playlist file
70     sp<M3UParser> fetchPlaylist(
71             const char *url, uint8_t *curPlaylistHash, bool *unchanged);
72 
73 private:
74     sp<HTTPBase> mHTTPDataSource;
75     sp<DataSource> mDataSource;
76     KeyedVector<String8, String8> mExtraHeaders;
77 
78     Mutex mLock;
79     bool mDisconnecting;
80 
81     DISALLOW_EVIL_CONSTRUCTORS(HTTPDownloader);
82 };
83 
84 }  // namespace android
85 
86 #endif  // HTTP_DOWNLOADER_H_
87