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       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 
ABuffer(void * data,size_t capacity)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
CreateAsCopy(const void * data,size_t capacity)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 
~ABuffer()62 ABuffer::~ABuffer() {
63     if (mOwnsData) {
64         if (mData != NULL) {
65             free(mData);
66             mData = NULL;
67         }
68     }
69 
70     if (mFarewell != NULL) {
71         mFarewell->post();
72     }
73 
74     setMediaBufferBase(NULL);
75 }
76 
setRange(size_t offset,size_t size)77 void ABuffer::setRange(size_t offset, size_t size) {
78     CHECK_LE(offset, mCapacity);
79     CHECK_LE(offset + size, mCapacity);
80 
81     mRangeOffset = offset;
82     mRangeLength = size;
83 }
84 
setFarewellMessage(const sp<AMessage> msg)85 void ABuffer::setFarewellMessage(const sp<AMessage> msg) {
86     mFarewell = msg;
87 }
88 
meta()89 sp<AMessage> ABuffer::meta() {
90     if (mMeta == NULL) {
91         mMeta = new AMessage;
92     }
93     return mMeta;
94 }
95 
getMediaBufferBase()96 MediaBufferBase *ABuffer::getMediaBufferBase() {
97     if (mMediaBufferBase != NULL) {
98         mMediaBufferBase->add_ref();
99     }
100     return mMediaBufferBase;
101 }
102 
setMediaBufferBase(MediaBufferBase * mediaBuffer)103 void ABuffer::setMediaBufferBase(MediaBufferBase *mediaBuffer) {
104     if (mMediaBufferBase != NULL) {
105         mMediaBufferBase->release();
106     }
107     mMediaBufferBase = mediaBuffer;
108 }
109 
110 }  // namespace android
111 
112