Andreas Huber | 20111aa | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 1 | /* |
| 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 Zhang | 3de157d | 2014-08-05 20:54:44 -0700 | [diff] [blame] | 16 | //#define LOG_NDEBUG 0 |
Marco Nelissen | 2a243f0 | 2018-01-30 08:29:57 -0800 | [diff] [blame] | 17 | #define LOG_TAG "DataSourceBase" |
Andreas Huber | 20111aa | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 18 | |
Marco Nelissen | 2a243f0 | 2018-01-30 08:29:57 -0800 | [diff] [blame] | 19 | #include <media/DataSourceBase.h> |
Dongwon Kang | 6076128 | 2017-10-09 11:16:48 -0700 | [diff] [blame] | 20 | #include <media/stagefright/foundation/ByteUtils.h> |
Andreas Huber | 20111aa | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 21 | #include <media/stagefright/MediaErrors.h> |
Andreas Huber | 20111aa | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 22 | #include <utils/String8.h> |
| 23 | |
| 24 | namespace android { |
| 25 | |
Marco Nelissen | 2a243f0 | 2018-01-30 08:29:57 -0800 | [diff] [blame] | 26 | bool DataSourceBase::getUInt16(off64_t offset, uint16_t *x) { |
Andreas Huber | 693d271 | 2009-08-14 14:37:10 -0700 | [diff] [blame] | 27 | *x = 0; |
| 28 | |
| 29 | uint8_t byte[2]; |
Andreas Huber | 34769bc | 2009-10-23 10:22:30 -0700 | [diff] [blame] | 30 | if (readAt(offset, byte, 2) != 2) { |
Andreas Huber | 693d271 | 2009-08-14 14:37:10 -0700 | [diff] [blame] | 31 | return false; |
| 32 | } |
| 33 | |
| 34 | *x = (byte[0] << 8) | byte[1]; |
| 35 | |
| 36 | return true; |
| 37 | } |
| 38 | |
Marco Nelissen | 2a243f0 | 2018-01-30 08:29:57 -0800 | [diff] [blame] | 39 | bool DataSourceBase::getUInt24(off64_t offset, uint32_t *x) { |
Marco Nelissen | ec77122 | 2013-04-08 14:30:57 -0700 | [diff] [blame] | 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 Nelissen | 2a243f0 | 2018-01-30 08:29:57 -0800 | [diff] [blame] | 52 | bool DataSourceBase::getUInt32(off64_t offset, uint32_t *x) { |
Marco Nelissen | 05f625c | 2013-02-13 09:27:28 -0800 | [diff] [blame] | 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 | |
Marco Nelissen | 2a243f0 | 2018-01-30 08:29:57 -0800 | [diff] [blame] | 65 | bool DataSourceBase::getUInt64(off64_t offset, uint64_t *x) { |
Marco Nelissen | 05f625c | 2013-02-13 09:27:28 -0800 | [diff] [blame] | 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 | |
Marco Nelissen | 2a243f0 | 2018-01-30 08:29:57 -0800 | [diff] [blame] | 78 | bool DataSourceBase::getUInt16Var(off64_t offset, uint16_t *x, size_t size) { |
Chong Zhang | b51ca28 | 2017-07-26 16:25:28 -0700 | [diff] [blame] | 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 | |
Marco Nelissen | 2a243f0 | 2018-01-30 08:29:57 -0800 | [diff] [blame] | 92 | bool DataSourceBase::getUInt32Var(off64_t offset, uint32_t *x, size_t size) { |
Chong Zhang | b51ca28 | 2017-07-26 16:25:28 -0700 | [diff] [blame] | 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 | |
Marco Nelissen | 2a243f0 | 2018-01-30 08:29:57 -0800 | [diff] [blame] | 106 | bool DataSourceBase::getUInt64Var(off64_t offset, uint64_t *x, size_t size) { |
Chong Zhang | b51ca28 | 2017-07-26 16:25:28 -0700 | [diff] [blame] | 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 | |
Marco Nelissen | 2a243f0 | 2018-01-30 08:29:57 -0800 | [diff] [blame] | 120 | status_t DataSourceBase::getSize(off64_t *size) { |
Andreas Huber | 20111aa | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 121 | *size = 0; |
| 122 | |
| 123 | return ERROR_UNSUPPORTED; |
| 124 | } |
| 125 | |
Dongwon Kang | b486b52 | 2018-02-16 15:46:53 -0800 | [diff] [blame^] | 126 | bool DataSourceBase::getUri(char *uriString __unused, size_t bufferSize __unused) { |
| 127 | return false; |
Andreas Huber | 6511c97 | 2011-03-30 11:15:27 -0700 | [diff] [blame] | 128 | } |
| 129 | |
Andreas Huber | 20111aa | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 130 | } // namespace android |