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