1 /*
2 * Copyright (C) 2017 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 //#define LOG_NDEBUG 0
17 #define LOG_TAG "DataSource"
18
19 #include "include/HTTPBase.h"
20 #include "include/NuCachedSource2.h"
21
22 #include <media/MediaHTTPConnection.h>
23 #include <media/MediaHTTPService.h>
24 #include <media/stagefright/DataSourceFactory.h>
25 #include <media/stagefright/DataURISource.h>
26 #include <media/stagefright/FileSource.h>
27 #include <media/stagefright/MediaHTTP.h>
28 #include <utils/String8.h>
29
30 namespace android {
31
32 // static
CreateFromURI(const sp<MediaHTTPService> & httpService,const char * uri,const KeyedVector<String8,String8> * headers,String8 * contentType,HTTPBase * httpSource)33 sp<DataSource> DataSourceFactory::CreateFromURI(
34 const sp<MediaHTTPService> &httpService,
35 const char *uri,
36 const KeyedVector<String8, String8> *headers,
37 String8 *contentType,
38 HTTPBase *httpSource) {
39 if (contentType != NULL) {
40 *contentType = "";
41 }
42
43 sp<DataSource> source;
44 if (!strncasecmp("file://", uri, 7)) {
45 source = new FileSource(uri + 7);
46 } else if (!strncasecmp("http://", uri, 7) || !strncasecmp("https://", uri, 8)) {
47 if (httpService == NULL) {
48 ALOGE("Invalid http service!");
49 return NULL;
50 }
51
52 if (httpSource == NULL) {
53 sp<MediaHTTPConnection> conn = httpService->makeHTTPConnection();
54 if (conn == NULL) {
55 ALOGE("Failed to make http connection from http service!");
56 return NULL;
57 }
58 httpSource = new MediaHTTP(conn);
59 }
60
61 String8 cacheConfig;
62 bool disconnectAtHighwatermark = false;
63 KeyedVector<String8, String8> nonCacheSpecificHeaders;
64 if (headers != NULL) {
65 nonCacheSpecificHeaders = *headers;
66 NuCachedSource2::RemoveCacheSpecificHeaders(
67 &nonCacheSpecificHeaders,
68 &cacheConfig,
69 &disconnectAtHighwatermark);
70 }
71
72 if (httpSource->connect(uri, &nonCacheSpecificHeaders) != OK) {
73 ALOGE("Failed to connect http source!");
74 return NULL;
75 }
76
77 if (contentType != NULL) {
78 *contentType = httpSource->getMIMEType();
79 }
80
81 source = NuCachedSource2::Create(
82 httpSource,
83 cacheConfig.isEmpty() ? NULL : cacheConfig.string(),
84 disconnectAtHighwatermark);
85 } else if (!strncasecmp("data:", uri, 5)) {
86 source = DataURISource::Create(uri);
87 } else {
88 // Assume it's a filename.
89 source = new FileSource(uri);
90 }
91
92 if (source == NULL || source->initCheck() != OK) {
93 return NULL;
94 }
95
96 return source;
97 }
98
CreateFromFd(int fd,int64_t offset,int64_t length)99 sp<DataSource> DataSourceFactory::CreateFromFd(int fd, int64_t offset, int64_t length) {
100 sp<FileSource> source = new FileSource(fd, offset, length);
101 return source->initCheck() != OK ? nullptr : source;
102 }
103
CreateMediaHTTP(const sp<MediaHTTPService> & httpService)104 sp<DataSource> DataSourceFactory::CreateMediaHTTP(const sp<MediaHTTPService> &httpService) {
105 if (httpService == NULL) {
106 return NULL;
107 }
108
109 sp<MediaHTTPConnection> conn = httpService->makeHTTPConnection();
110 if (conn == NULL) {
111 return NULL;
112 } else {
113 return new MediaHTTP(conn);
114 }
115 }
116
117 } // namespace android
118