blob: 72959c6f6114494379ecd7270774f95eaa6bb114 [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>
Dongwon Kang60761282017-10-09 11:16:48 -070020#include <media/stagefright/foundation/ByteUtils.h>
Andreas Huber20111aa2009-07-14 16:56:47 -070021#include <media/stagefright/MediaErrors.h>
Andreas Huber20111aa2009-07-14 16:56:47 -070022#include <utils/String8.h>
23
24namespace android {
25
James Dongc7fc37a2010-11-16 14:04:54 -080026bool DataSource::getUInt16(off64_t offset, uint16_t *x) {
Andreas Huber693d2712009-08-14 14:37:10 -070027 *x = 0;
28
29 uint8_t byte[2];
Andreas Huber34769bc2009-10-23 10:22:30 -070030 if (readAt(offset, byte, 2) != 2) {
Andreas Huber693d2712009-08-14 14:37:10 -070031 return false;
32 }
33
34 *x = (byte[0] << 8) | byte[1];
35
36 return true;
37}
38
Marco Nelissenec771222013-04-08 14:30:57 -070039bool DataSource::getUInt24(off64_t offset, uint32_t *x) {
40 *x = 0;
41
42 uint8_t byte[3];
43 if (readAt(offset, byte, 3) != 3) {
44 return false;
45 }
46
47 *x = (byte[0] << 16) | (byte[1] << 8) | byte[2];
48
49 return true;
50}
51
Marco Nelissen05f625c2013-02-13 09:27:28 -080052bool DataSource::getUInt32(off64_t offset, uint32_t *x) {
53 *x = 0;
54
55 uint32_t tmp;
56 if (readAt(offset, &tmp, 4) != 4) {
57 return false;
58 }
59
60 *x = ntohl(tmp);
61
62 return true;
63}
64
65bool DataSource::getUInt64(off64_t offset, uint64_t *x) {
66 *x = 0;
67
68 uint64_t tmp;
69 if (readAt(offset, &tmp, 8) != 8) {
70 return false;
71 }
72
73 *x = ntoh64(tmp);
74
75 return true;
76}
77
Chong Zhangb51ca282017-07-26 16:25:28 -070078bool DataSource::getUInt16Var(off64_t offset, uint16_t *x, size_t size) {
79 if (size == 2) {
80 return getUInt16(offset, x);
81 }
82 if (size == 1) {
83 uint8_t tmp;
84 if (readAt(offset, &tmp, 1) == 1) {
85 *x = tmp;
86 return true;
87 }
88 }
89 return false;
90}
91
92bool DataSource::getUInt32Var(off64_t offset, uint32_t *x, size_t size) {
93 if (size == 4) {
94 return getUInt32(offset, x);
95 }
96 if (size == 2) {
97 uint16_t tmp;
98 if (getUInt16(offset, &tmp)) {
99 *x = tmp;
100 return true;
101 }
102 }
103 return false;
104}
105
106bool DataSource::getUInt64Var(off64_t offset, uint64_t *x, size_t size) {
107 if (size == 8) {
108 return getUInt64(offset, x);
109 }
110 if (size == 4) {
111 uint32_t tmp;
112 if (getUInt32(offset, &tmp)) {
113 *x = tmp;
114 return true;
115 }
116 }
117 return false;
118}
119
James Dongc7fc37a2010-11-16 14:04:54 -0800120status_t DataSource::getSize(off64_t *size) {
Andreas Huber20111aa2009-07-14 16:56:47 -0700121 *size = 0;
122
123 return ERROR_UNSUPPORTED;
124}
125
Andreas Huber6511c972011-03-30 11:15:27 -0700126String8 DataSource::getMIMEType() const {
127 return String8("application/octet-stream");
128}
129
Andreas Huber20111aa2009-07-14 16:56:47 -0700130} // namespace android