blob: c22e4cbc2b3e9868e36a5fe0285f58731935cd87 [file] [log] [blame]
Andreas Huber20111aa2009-07-14 16:56:47 -07001/*
2 * Copyright (C) 2009 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 */
Chong Zhang3de157d2014-08-05 20:54:44 -070016//#define LOG_NDEBUG 0
17#define LOG_TAG "DataSource"
Andreas Huber20111aa2009-07-14 16:56:47 -070018
Dongwon Kangd91dc5a2017-10-10 00:07:09 -070019#include <media/DataSource.h>
Andy Hungd49dbd62016-07-07 14:20:35 -070020#include <media/IDataSource.h>
Dongwon Kang60761282017-10-09 11:16:48 -070021#include <media/stagefright/foundation/ByteUtils.h>
Andreas Huber20111aa2009-07-14 16:56:47 -070022#include <media/stagefright/MediaErrors.h>
Andreas Huber20111aa2009-07-14 16:56:47 -070023#include <utils/String8.h>
24
25namespace android {
26
James Dongc7fc37a2010-11-16 14:04:54 -080027bool DataSource::getUInt16(off64_t offset, uint16_t *x) {
Andreas Huber693d2712009-08-14 14:37:10 -070028 *x = 0;
29
30 uint8_t byte[2];
Andreas Huber34769bc2009-10-23 10:22:30 -070031 if (readAt(offset, byte, 2) != 2) {
Andreas Huber693d2712009-08-14 14:37:10 -070032 return false;
33 }
34
35 *x = (byte[0] << 8) | byte[1];
36
37 return true;
38}
39
Marco Nelissenec771222013-04-08 14:30:57 -070040bool DataSource::getUInt24(off64_t offset, uint32_t *x) {
41 *x = 0;
42
43 uint8_t byte[3];
44 if (readAt(offset, byte, 3) != 3) {
45 return false;
46 }
47
48 *x = (byte[0] << 16) | (byte[1] << 8) | byte[2];
49
50 return true;
51}
52
Marco Nelissen05f625c2013-02-13 09:27:28 -080053bool DataSource::getUInt32(off64_t offset, uint32_t *x) {
54 *x = 0;
55
56 uint32_t tmp;
57 if (readAt(offset, &tmp, 4) != 4) {
58 return false;
59 }
60
61 *x = ntohl(tmp);
62
63 return true;
64}
65
66bool DataSource::getUInt64(off64_t offset, uint64_t *x) {
67 *x = 0;
68
69 uint64_t tmp;
70 if (readAt(offset, &tmp, 8) != 8) {
71 return false;
72 }
73
74 *x = ntoh64(tmp);
75
76 return true;
77}
78
Chong Zhangb51ca282017-07-26 16:25:28 -070079bool DataSource::getUInt16Var(off64_t offset, uint16_t *x, size_t size) {
80 if (size == 2) {
81 return getUInt16(offset, x);
82 }
83 if (size == 1) {
84 uint8_t tmp;
85 if (readAt(offset, &tmp, 1) == 1) {
86 *x = tmp;
87 return true;
88 }
89 }
90 return false;
91}
92
93bool DataSource::getUInt32Var(off64_t offset, uint32_t *x, size_t size) {
94 if (size == 4) {
95 return getUInt32(offset, x);
96 }
97 if (size == 2) {
98 uint16_t tmp;
99 if (getUInt16(offset, &tmp)) {
100 *x = tmp;
101 return true;
102 }
103 }
104 return false;
105}
106
107bool DataSource::getUInt64Var(off64_t offset, uint64_t *x, size_t size) {
108 if (size == 8) {
109 return getUInt64(offset, x);
110 }
111 if (size == 4) {
112 uint32_t tmp;
113 if (getUInt32(offset, &tmp)) {
114 *x = tmp;
115 return true;
116 }
117 }
118 return false;
119}
120
James Dongc7fc37a2010-11-16 14:04:54 -0800121status_t DataSource::getSize(off64_t *size) {
Andreas Huber20111aa2009-07-14 16:56:47 -0700122 *size = 0;
123
124 return ERROR_UNSUPPORTED;
125}
126
Andy Hungd49dbd62016-07-07 14:20:35 -0700127sp<IDataSource> DataSource::getIDataSource() const {
128 return nullptr;
129}
130
Andreas Huber6511c972011-03-30 11:15:27 -0700131String8 DataSource::getMIMEType() const {
132 return String8("application/octet-stream");
133}
134
Andreas Huber20111aa2009-07-14 16:56:47 -0700135} // namespace android