Skip to content

Commit b808af7

Browse files
narayankAndroid (Google) Code Review
authored andcommitted
Merge "Allow data uris to be data sources"
2 parents bb60ee9 + 6b67215 commit b808af7

File tree

4 files changed

+156
-2
lines changed

4 files changed

+156
-2
lines changed

media/libstagefright/DataSource.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,11 @@
1616

1717
#include "include/AMRExtractor.h"
1818
#include "include/AVIExtractor.h"
19+
20+
#if CHROMIUM_AVAILABLE
21+
#include "include/DataUriSource.h"
22+
#endif
23+
1924
#include "include/MP3Extractor.h"
2025
#include "include/MPEG4Extractor.h"
2126
#include "include/WAVExtractor.h"
@@ -136,6 +141,10 @@ sp<DataSource> DataSource::CreateFromURI(
136141
return NULL;
137142
}
138143
source = new NuCachedSource2(httpSource);
144+
# if CHROMIUM_AVAILABLE
145+
} else if (!strncasecmp("data:", uri, 5)) {
146+
source = new DataUriSource(uri);
147+
#endif
139148
} else {
140149
// Assume it's a filename.
141150
source = new FileSource(uri);

media/libstagefright/chromium_http/Android.mk

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,9 @@ LOCAL_PATH:= $(call my-dir)
33
include $(CLEAR_VARS)
44

55
LOCAL_SRC_FILES:= \
6-
ChromiumHTTPDataSource.cpp \
7-
support.cpp \
6+
DataUriSource.cpp \
7+
ChromiumHTTPDataSource.cpp \
8+
support.cpp
89

910
LOCAL_C_INCLUDES:= \
1011
$(JNI_H_INCLUDE) \
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
/*
2+
* Copyright (C) 2011 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+
#include <include/DataUriSource.h>
18+
19+
#include <net/base/data_url.h>
20+
#include <googleurl/src/gurl.h>
21+
22+
23+
namespace android {
24+
25+
DataUriSource::DataUriSource(const char *uri) :
26+
mDataUri(uri),
27+
mInited(NO_INIT) {
28+
29+
// Copy1: const char *uri -> String8 mDataUri.
30+
std::string mimeTypeStr, unusedCharsetStr, dataStr;
31+
// Copy2: String8 mDataUri -> std::string
32+
const bool ret = net::DataURL::Parse(
33+
GURL(std::string(mDataUri.string())),
34+
&mimeTypeStr, &unusedCharsetStr, &dataStr);
35+
// Copy3: std::string dataStr -> AString mData
36+
mData.setTo(dataStr.data(), dataStr.length());
37+
mInited = ret ? OK : UNKNOWN_ERROR;
38+
39+
// The chromium data url implementation defaults to using "text/plain"
40+
// if no mime type is specified. We prefer to leave this unspecified
41+
// instead, since the mime type is sniffed in most cases.
42+
if (mimeTypeStr != "text/plain") {
43+
mMimeType = mimeTypeStr.c_str();
44+
}
45+
}
46+
47+
ssize_t DataUriSource::readAt(off64_t offset, void *out, size_t size) {
48+
if (mInited != OK) {
49+
return mInited;
50+
}
51+
52+
const off64_t length = mData.size();
53+
if (offset >= length) {
54+
return UNKNOWN_ERROR;
55+
}
56+
57+
const char *dataBuf = mData.c_str();
58+
const size_t bytesToCopy =
59+
offset + size >= length ? (length - offset) : size;
60+
61+
if (bytesToCopy > 0) {
62+
memcpy(out, dataBuf + offset, bytesToCopy);
63+
}
64+
65+
return bytesToCopy;
66+
}
67+
68+
} // namespace android
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
/*
2+
* Copyright (C) 2011 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 DATA_URI_SOURCE_H_
18+
19+
#define DATA_URI_SOURCE_H_
20+
21+
#include <stdio.h>
22+
23+
#include <media/stagefright/DataSource.h>
24+
#include <media/stagefright/MediaErrors.h>
25+
#include <media/stagefright/foundation/AString.h>
26+
27+
namespace android {
28+
29+
class DataUriSource : public DataSource {
30+
public:
31+
DataUriSource(const char *uri);
32+
33+
virtual status_t initCheck() const {
34+
return mInited;
35+
}
36+
37+
virtual ssize_t readAt(off64_t offset, void *data, size_t size);
38+
39+
virtual status_t getSize(off64_t *size) {
40+
if (mInited != OK) {
41+
return mInited;
42+
}
43+
44+
*size = mData.size();
45+
return OK;
46+
}
47+
48+
virtual String8 getUri() {
49+
return mDataUri;
50+
}
51+
52+
virtual String8 getMIMEType() const {
53+
return mMimeType;
54+
}
55+
56+
protected:
57+
virtual ~DataUriSource() {
58+
// Nothing to delete.
59+
}
60+
61+
private:
62+
const String8 mDataUri;
63+
64+
String8 mMimeType;
65+
// Use AString because individual bytes may not be valid UTF8 chars.
66+
AString mData;
67+
status_t mInited;
68+
69+
// Disallow copy and assign.
70+
DataUriSource(const DataUriSource &);
71+
DataUriSource &operator=(const DataUriSource &);
72+
};
73+
74+
} // namespace android
75+
76+
#endif // DATA_URI_SOURCE_H_

0 commit comments

Comments
 (0)