commit
edf39faeee
@ -0,0 +1,117 @@
|
||||
/*
|
||||
* Copyright (C) 2018 The Android Open Source Project
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
//#define LOG_NDEBUG 0
|
||||
#define LOG_TAG "ClearDataSourceFactory"
|
||||
|
||||
#include "include/HTTPBase.h"
|
||||
#include "include/NuCachedSource2.h"
|
||||
|
||||
#include <media/MediaHTTPConnection.h>
|
||||
#include <media/MediaHTTPService.h>
|
||||
#include <media/stagefright/ClearFileSource.h>
|
||||
#include <media/stagefright/ClearMediaHTTP.h>
|
||||
#include <media/stagefright/ClearDataSourceFactory.h>
|
||||
#include <media/stagefright/DataURISource.h>
|
||||
#include <utils/String8.h>
|
||||
|
||||
namespace android {
|
||||
|
||||
// static
|
||||
sp<DataSource> ClearDataSourceFactory::CreateFromURI(
|
||||
const sp<MediaHTTPService> &httpService,
|
||||
const char *uri,
|
||||
const KeyedVector<String8, String8> *headers,
|
||||
String8 *contentType,
|
||||
HTTPBase *httpSource) {
|
||||
if (contentType != NULL) {
|
||||
*contentType = "";
|
||||
}
|
||||
|
||||
sp<DataSource> source;
|
||||
if (!strncasecmp("file://", uri, 7)) {
|
||||
source = new ClearFileSource(uri + 7);
|
||||
} else if (!strncasecmp("http://", uri, 7) || !strncasecmp("https://", uri, 8)) {
|
||||
if (httpService == NULL) {
|
||||
ALOGE("Invalid http service!");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
if (httpSource == NULL) {
|
||||
sp<MediaHTTPConnection> conn = httpService->makeHTTPConnection();
|
||||
if (conn == NULL) {
|
||||
ALOGE("Failed to make http connection from http service!");
|
||||
return NULL;
|
||||
}
|
||||
httpSource = new ClearMediaHTTP(conn);
|
||||
}
|
||||
|
||||
String8 cacheConfig;
|
||||
bool disconnectAtHighwatermark = false;
|
||||
KeyedVector<String8, String8> nonCacheSpecificHeaders;
|
||||
if (headers != NULL) {
|
||||
nonCacheSpecificHeaders = *headers;
|
||||
NuCachedSource2::RemoveCacheSpecificHeaders(
|
||||
&nonCacheSpecificHeaders,
|
||||
&cacheConfig,
|
||||
&disconnectAtHighwatermark);
|
||||
}
|
||||
|
||||
if (httpSource->connect(uri, &nonCacheSpecificHeaders) != OK) {
|
||||
ALOGE("Failed to connect http source!");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
if (contentType != NULL) {
|
||||
*contentType = httpSource->getMIMEType();
|
||||
}
|
||||
|
||||
source = NuCachedSource2::Create(
|
||||
httpSource,
|
||||
cacheConfig.isEmpty() ? NULL : cacheConfig.string(),
|
||||
disconnectAtHighwatermark);
|
||||
} else if (!strncasecmp("data:", uri, 5)) {
|
||||
source = DataURISource::Create(uri);
|
||||
} else {
|
||||
// Assume it's a filename.
|
||||
source = new ClearFileSource(uri);
|
||||
}
|
||||
|
||||
if (source == NULL || source->initCheck() != OK) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
return source;
|
||||
}
|
||||
|
||||
sp<DataSource> ClearDataSourceFactory::CreateFromFd(int fd, int64_t offset, int64_t length) {
|
||||
sp<ClearFileSource> source = new ClearFileSource(fd, offset, length);
|
||||
return source->initCheck() != OK ? nullptr : source;
|
||||
}
|
||||
|
||||
sp<DataSource> ClearDataSourceFactory::CreateMediaHTTP(const sp<MediaHTTPService> &httpService) {
|
||||
if (httpService == NULL) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
sp<MediaHTTPConnection> conn = httpService->makeHTTPConnection();
|
||||
if (conn == NULL) {
|
||||
return NULL;
|
||||
} else {
|
||||
return new ClearMediaHTTP(conn);
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace android
|
@ -0,0 +1,143 @@
|
||||
/*
|
||||
* Copyright (C) 2018 The Android Open Source Project
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
//#define LOG_NDEBUG 0
|
||||
#define LOG_TAG "ClearFileSource"
|
||||
#include <utils/Log.h>
|
||||
|
||||
#include <media/stagefright/foundation/ADebug.h>
|
||||
#include <media/stagefright/ClearFileSource.h>
|
||||
#include <media/stagefright/Utils.h>
|
||||
#include <sys/types.h>
|
||||
#include <unistd.h>
|
||||
#include <sys/types.h>
|
||||
#include <sys/stat.h>
|
||||
#include <fcntl.h>
|
||||
|
||||
namespace android {
|
||||
|
||||
ClearFileSource::ClearFileSource(const char *filename)
|
||||
: mFd(-1),
|
||||
mOffset(0),
|
||||
mLength(-1),
|
||||
mName("<null>") {
|
||||
|
||||
if (filename) {
|
||||
mName = String8::format("FileSource(%s)", filename);
|
||||
}
|
||||
ALOGV("%s", filename);
|
||||
mFd = open(filename, O_LARGEFILE | O_RDONLY);
|
||||
|
||||
if (mFd >= 0) {
|
||||
mLength = lseek64(mFd, 0, SEEK_END);
|
||||
} else {
|
||||
ALOGE("Failed to open file '%s'. (%s)", filename, strerror(errno));
|
||||
}
|
||||
}
|
||||
|
||||
ClearFileSource::ClearFileSource(int fd, int64_t offset, int64_t length)
|
||||
: mFd(fd),
|
||||
mOffset(offset),
|
||||
mLength(length),
|
||||
mName("<null>") {
|
||||
ALOGV("fd=%d (%s), offset=%lld, length=%lld",
|
||||
fd, nameForFd(fd).c_str(), (long long) offset, (long long) length);
|
||||
|
||||
if (mOffset < 0) {
|
||||
mOffset = 0;
|
||||
}
|
||||
if (mLength < 0) {
|
||||
mLength = 0;
|
||||
}
|
||||
if (mLength > INT64_MAX - mOffset) {
|
||||
mLength = INT64_MAX - mOffset;
|
||||
}
|
||||
struct stat s;
|
||||
if (fstat(fd, &s) == 0) {
|
||||
if (mOffset > s.st_size) {
|
||||
mOffset = s.st_size;
|
||||
mLength = 0;
|
||||
}
|
||||
if (mOffset + mLength > s.st_size) {
|
||||
mLength = s.st_size - mOffset;
|
||||
}
|
||||
}
|
||||
if (mOffset != offset || mLength != length) {
|
||||
ALOGW("offset/length adjusted from %lld/%lld to %lld/%lld",
|
||||
(long long) offset, (long long) length,
|
||||
(long long) mOffset, (long long) mLength);
|
||||
}
|
||||
|
||||
mName = String8::format(
|
||||
"FileSource(fd(%s), %lld, %lld)",
|
||||
nameForFd(fd).c_str(),
|
||||
(long long) mOffset,
|
||||
(long long) mLength);
|
||||
|
||||
}
|
||||
|
||||
ClearFileSource::~ClearFileSource() {
|
||||
if (mFd >= 0) {
|
||||
::close(mFd);
|
||||
mFd = -1;
|
||||
}
|
||||
}
|
||||
|
||||
status_t ClearFileSource::initCheck() const {
|
||||
return mFd >= 0 ? OK : NO_INIT;
|
||||
}
|
||||
|
||||
ssize_t ClearFileSource::readAt(off64_t offset, void *data, size_t size) {
|
||||
if (mFd < 0) {
|
||||
return NO_INIT;
|
||||
}
|
||||
|
||||
Mutex::Autolock autoLock(mLock);
|
||||
if (mLength >= 0) {
|
||||
if (offset >= mLength) {
|
||||
return 0; // read beyond EOF.
|
||||
}
|
||||
uint64_t numAvailable = mLength - offset;
|
||||
if ((uint64_t)size > numAvailable) {
|
||||
size = numAvailable;
|
||||
}
|
||||
}
|
||||
return readAt_l(offset, data, size);
|
||||
}
|
||||
|
||||
ssize_t ClearFileSource::readAt_l(off64_t offset, void *data, size_t size) {
|
||||
off64_t result = lseek64(mFd, offset + mOffset, SEEK_SET);
|
||||
if (result == -1) {
|
||||
ALOGE("seek to %lld failed", (long long)(offset + mOffset));
|
||||
return UNKNOWN_ERROR;
|
||||
}
|
||||
|
||||
return ::read(mFd, data, size);
|
||||
}
|
||||
|
||||
status_t ClearFileSource::getSize(off64_t *size) {
|
||||
Mutex::Autolock autoLock(mLock);
|
||||
|
||||
if (mFd < 0) {
|
||||
return NO_INIT;
|
||||
}
|
||||
|
||||
*size = mLength;
|
||||
|
||||
return OK;
|
||||
}
|
||||
|
||||
} // namespace android
|
@ -0,0 +1,180 @@
|
||||
/*
|
||||
* Copyright (C) 2018 The Android Open Source Project
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
//#define LOG_NDEBUG 0
|
||||
#define LOG_TAG "ClearMediaHTTP"
|
||||
#include <utils/Log.h>
|
||||
|
||||
#include <media/stagefright/ClearMediaHTTP.h>
|
||||
|
||||
#include <media/stagefright/foundation/ADebug.h>
|
||||
#include <media/stagefright/foundation/ALooper.h>
|
||||
#include <media/stagefright/Utils.h>
|
||||
|
||||
#include <media/MediaHTTPConnection.h>
|
||||
|
||||
namespace android {
|
||||
|
||||
ClearMediaHTTP::ClearMediaHTTP(const sp<MediaHTTPConnection> &conn)
|
||||
: mInitCheck((conn != NULL) ? OK : NO_INIT),
|
||||
mHTTPConnection(conn),
|
||||
mCachedSizeValid(false),
|
||||
mCachedSize(0ll) {
|
||||
}
|
||||
|
||||
ClearMediaHTTP::~ClearMediaHTTP() {
|
||||
}
|
||||
|
||||
status_t ClearMediaHTTP::connect(
|
||||
const char *uri,
|
||||
const KeyedVector<String8, String8> *headers,
|
||||
off64_t /* offset */) {
|
||||
if (mInitCheck != OK) {
|
||||
return mInitCheck;
|
||||
}
|
||||
|
||||
KeyedVector<String8, String8> extHeaders;
|
||||
if (headers != NULL) {
|
||||
extHeaders = *headers;
|
||||
}
|
||||
|
||||
if (extHeaders.indexOfKey(String8("User-Agent")) < 0) {
|
||||
extHeaders.add(String8("User-Agent"), String8(MakeUserAgent().c_str()));
|
||||
}
|
||||
|
||||
mLastURI = uri;
|
||||
// reconnect() calls with uri == old mLastURI.c_str(), which gets zapped
|
||||
// as part of the above assignment. Ensure no accidental later use.
|
||||
uri = NULL;
|
||||
|
||||
bool success = mHTTPConnection->connect(mLastURI.c_str(), &extHeaders);
|
||||
|
||||
mLastHeaders = extHeaders;
|
||||
|
||||
mCachedSizeValid = false;
|
||||
|
||||
if (success) {
|
||||
AString sanitized = uriDebugString(mLastURI);
|
||||
mName = String8::format("ClearMediaHTTP(%s)", sanitized.c_str());
|
||||
}
|
||||
|
||||
return success ? OK : UNKNOWN_ERROR;
|
||||
}
|
||||
|
||||
void ClearMediaHTTP::disconnect() {
|
||||
mName = String8("ClearMediaHTTP(<disconnected>)");
|
||||
if (mInitCheck != OK) {
|
||||
return;
|
||||
}
|
||||
|
||||
mHTTPConnection->disconnect();
|
||||
}
|
||||
|
||||
status_t ClearMediaHTTP::initCheck() const {
|
||||
return mInitCheck;
|
||||
}
|
||||
|
||||
ssize_t ClearMediaHTTP::readAt(off64_t offset, void *data, size_t size) {
|
||||
if (mInitCheck != OK) {
|
||||
return mInitCheck;
|
||||
}
|
||||
|
||||
int64_t startTimeUs = ALooper::GetNowUs();
|
||||
|
||||
size_t numBytesRead = 0;
|
||||
while (numBytesRead < size) {
|
||||
size_t copy = size - numBytesRead;
|
||||
|
||||
if (copy > 64 * 1024) {
|
||||
// limit the buffer sizes transferred across binder boundaries
|
||||
// to avoid spurious transaction failures.
|
||||
copy = 64 * 1024;
|
||||
}
|
||||
|
||||
ssize_t n = mHTTPConnection->readAt(
|
||||
offset + numBytesRead, (uint8_t *)data + numBytesRead, copy);
|
||||
|
||||
if (n < 0) {
|
||||
return n;
|
||||
} else if (n == 0) {
|
||||
break;
|
||||
}
|
||||
|
||||
numBytesRead += n;
|
||||
}
|
||||
|
||||
int64_t delayUs = ALooper::GetNowUs() - startTimeUs;
|
||||
|
||||
addBandwidthMeasurement(numBytesRead, delayUs);
|
||||
|
||||
return numBytesRead;
|
||||
}
|
||||
|
||||
status_t ClearMediaHTTP::getSize(off64_t *size) {
|
||||
if (mInitCheck != OK) {
|
||||
return mInitCheck;
|
||||
}
|
||||
|
||||
// Caching the returned size so that it stays valid even after a
|
||||
// disconnect. NuCachedSource2 relies on this.
|
||||
|
||||
if (!mCachedSizeValid) {
|
||||
mCachedSize = mHTTPConnection->getSize();
|
||||
mCachedSizeValid = true;
|
||||
}
|
||||
|
||||
*size = mCachedSize;
|
||||
|
||||
return *size < 0 ? *size : static_cast<status_t>(OK);
|
||||
}
|
||||
|
||||
uint32_t ClearMediaHTTP::flags() {
|
||||
return kWantsPrefetching | kIsHTTPBasedSource;
|
||||
}
|
||||
|
||||
status_t ClearMediaHTTP::reconnectAtOffset(off64_t offset) {
|
||||
return connect(mLastURI.c_str(), &mLastHeaders, offset);
|
||||
}
|
||||
|
||||
|
||||
String8 ClearMediaHTTP::getUri() {
|
||||
if (mInitCheck != OK) {
|
||||
return String8::empty();
|
||||
}
|
||||
|
||||
String8 uri;
|
||||
if (OK == mHTTPConnection->getUri(&uri)) {
|
||||
return uri;
|
||||
}
|
||||
return String8(mLastURI.c_str());
|
||||
}
|
||||
|
||||
String8 ClearMediaHTTP::getMIMEType() const {
|
||||
if (mInitCheck != OK) {
|
||||
return String8("application/octet-stream");
|
||||
}
|
||||
|
||||
String8 mimeType;
|
||||
status_t err = mHTTPConnection->getMIMEType(&mimeType);
|
||||
|
||||
if (err != OK) {
|
||||
return String8("application/octet-stream");
|
||||
}
|
||||
|
||||
return mimeType;
|
||||
}
|
||||
|
||||
} // namespace android
|
@ -0,0 +1,45 @@
|
||||
/*
|
||||
* Copyright (C) 2018 The Android Open Source Project
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
#ifndef DATA_SOURCE_FACTORY2_H_
|
||||
|
||||
#define DATA_SOURCE_FACTORY2_H_
|
||||
|
||||
#include <sys/types.h>
|
||||
#include <utils/RefBase.h>
|
||||
|
||||
namespace android {
|
||||
|
||||
struct MediaHTTPService;
|
||||
class String8;
|
||||
struct HTTPBase;
|
||||
|
||||
class ClearDataSourceFactory {
|
||||
public:
|
||||
static sp<DataSource> CreateFromURI(
|
||||
const sp<MediaHTTPService> &httpService,
|
||||
const char *uri,
|
||||
const KeyedVector<String8, String8> *headers = NULL,
|
||||
String8 *contentType = NULL,
|
||||
HTTPBase *httpSource = NULL);
|
||||
|
||||
static sp<DataSource> CreateMediaHTTP(const sp<MediaHTTPService> &httpService);
|
||||
static sp<DataSource> CreateFromFd(int fd, int64_t offset, int64_t length);
|
||||
};
|
||||
|
||||
} // namespace android
|
||||
|
||||
#endif // DATA_SOURCE_FACTORY2_H_
|
@ -0,0 +1,68 @@
|
||||
/*
|
||||
* Copyright (C) 2018 The Android Open Source Project
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
#ifndef CLEAR_FILE_SOURCE_H_
|
||||
|
||||
#define CLEAR_FILE_SOURCE_H_
|
||||
|
||||
#include <stdio.h>
|
||||
|
||||
#include <media/DataSource.h>
|
||||
#include <media/stagefright/MediaErrors.h>
|
||||
#include <utils/threads.h>
|
||||
|
||||
namespace android {
|
||||
|
||||
class ClearFileSource : public DataSource {
|
||||
public:
|
||||
ClearFileSource(const char *filename);
|
||||
// ClearFileSource takes ownership and will close the fd
|
||||
ClearFileSource(int fd, int64_t offset, int64_t length);
|
||||
|
||||
virtual status_t initCheck() const;
|
||||
|
||||
virtual ssize_t readAt(off64_t offset, void *data, size_t size);
|
||||
|
||||
virtual status_t getSize(off64_t *size);
|
||||
|
||||
virtual uint32_t flags() {
|
||||
return kIsLocalFileSource;
|
||||
}
|
||||
|
||||
virtual String8 toString() {
|
||||
return mName;
|
||||
}
|
||||
|
||||
protected:
|
||||
virtual ~ClearFileSource();
|
||||
virtual ssize_t readAt_l(off64_t offset, void *data, size_t size);
|
||||
|
||||
int mFd;
|
||||
int64_t mOffset;
|
||||
int64_t mLength;
|
||||
Mutex mLock;
|
||||
|
||||
private:
|
||||
String8 mName;
|
||||
|
||||
ClearFileSource(const ClearFileSource &);
|
||||
ClearFileSource &operator=(const ClearFileSource &);
|
||||
};
|
||||
|
||||
} // namespace android
|
||||
|
||||
#endif // CLEAR_FILE_SOURCE_H_
|
||||
|
@ -0,0 +1,71 @@
|
||||
/*
|
||||
* Copyright (C) 2018 The Android Open Source Project
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
#ifndef CLEAR_MEDIA_HTTP_H_
|
||||
|
||||
#define CLEAR_MEDIA_HTTP_H_
|
||||
|
||||
#include <media/stagefright/foundation/AString.h>
|
||||
|
||||
#include "include/HTTPBase.h"
|
||||
|
||||
namespace android {
|
||||
|
||||
struct MediaHTTPConnection;
|
||||
|
||||
struct ClearMediaHTTP : public HTTPBase {
|
||||
ClearMediaHTTP(const sp<MediaHTTPConnection> &conn);
|
||||
|
||||
virtual status_t connect(
|
||||
const char *uri,
|
||||
const KeyedVector<String8, String8> *headers,
|
||||
off64_t offset);
|
||||
|
||||
virtual void disconnect();
|
||||
|
||||
virtual status_t initCheck() const;
|
||||
|
||||
virtual ssize_t readAt(off64_t offset, void *data, size_t size);
|
||||
|
||||
virtual status_t getSize(off64_t *size);
|
||||
|
||||
virtual uint32_t flags();
|
||||
|
||||
virtual status_t reconnectAtOffset(off64_t offset);
|
||||
|
||||
protected:
|
||||
virtual ~ClearMediaHTTP();
|
||||
|
||||
virtual String8 getUri();
|
||||
virtual String8 getMIMEType() const;
|
||||
|
||||
AString mLastURI;
|
||||
|
||||
private:
|
||||
status_t mInitCheck;
|
||||
sp<MediaHTTPConnection> mHTTPConnection;
|
||||
|
||||
KeyedVector<String8, String8> mLastHeaders;
|
||||
|
||||
bool mCachedSizeValid;
|
||||
off64_t mCachedSize;
|
||||
|
||||
DISALLOW_EVIL_CONSTRUCTORS(ClearMediaHTTP);
|
||||
};
|
||||
|
||||
} // namespace android
|
||||
|
||||
#endif // CLEAR_MEDIA_HTTP_H_
|
Loading…
Reference in new issue