1 /* 2 * Copyright (C) 2010 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 "ABuffer.h" 18 19 #include "ADebug.h" 20 #include "ALooper.h" 21 #include "AMessage.h" 22 #include "MediaBufferBase.h" 23 24 namespace android { 25 26 ABuffer::ABuffer(size_t capacity) 27 : mMediaBufferBase(NULL), 28 mRangeOffset(0), 29 mInt32Data(0), 30 mOwnsData(true) { 31 mData = malloc(capacity); 32 if (mData == NULL) { 33 mCapacity = 0; 34 mRangeLength = 0; 35 } else { 36 mCapacity = capacity; 37 mRangeLength = capacity; 38 } 39 } 40 41 ABuffer::ABuffer(void *data, size_t capacity) 42 : mMediaBufferBase(NULL), 43 mData(data), 44 mCapacity(capacity), 45 mRangeOffset(0), 46 mRangeLength(capacity), 47 mInt32Data(0), 48 mOwnsData(false) { 49 } 50 51 // static 52 sp<ABuffer> ABuffer::CreateAsCopy(const void *data, size_t capacity) 53 { 54 sp<ABuffer> res = new ABuffer(capacity); 55 if (res->base() == NULL) { 56 return NULL; 57 } 58 memcpy(res->data(), data, capacity); 59 return res; 60 } 61 62 ABuffer::~ABuffer() { 63 if (mOwnsData) { 64 if (mData != NULL) { 65 free(mData); 66 mData = NULL; 67 } 68 } 69 70 setMediaBufferBase(NULL); 71 } 72 73 void ABuffer::setRange(size_t offset, size_t size) { 74 CHECK_LE(offset, mCapacity); 75 CHECK_LE(offset + size, mCapacity); 76 77 mRangeOffset = offset; 78 mRangeLength = size; 79 } 80 81 sp<AMessage> ABuffer::meta() { 82 if (mMeta == NULL) { 83 mMeta = new AMessage; 84 } 85 return mMeta; 86 } 87 88 MediaBufferBase *ABuffer::getMediaBufferBase() { 89 if (mMediaBufferBase != NULL) { 90 mMediaBufferBase->add_ref(); 91 } 92 return mMediaBufferBase; 93 } 94 95 void ABuffer::setMediaBufferBase(MediaBufferBase *mediaBuffer) { 96 if (mMediaBufferBase != NULL) { 97 mMediaBufferBase->release(); 98 } 99 mMediaBufferBase = mediaBuffer; 100 } 101 102 } // namespace android 103 104