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 */
16
17#ifndef MEDIA_BUFFER_GROUP_H_
18
19#define MEDIA_BUFFER_GROUP_H_
20
21#include <media/stagefright/MediaBuffer.h>
22#include <utils/Errors.h>
23#include <utils/threads.h>
24
25namespace android {
26
27class MediaBuffer;
28class MetaData;
29
30class MediaBufferGroup : public MediaBufferObserver {
31public:
32    MediaBufferGroup(size_t growthLimit = 0);
33
34    // create a media buffer group with preallocated buffers
35    MediaBufferGroup(size_t buffers, size_t buffer_size, size_t growthLimit = 0);
36
37    ~MediaBufferGroup();
38
39    void add_buffer(MediaBuffer *buffer);
40
41    bool has_buffers();
42
43    // If nonBlocking is false, it blocks until a buffer is available and
44    // passes it to the caller in *buffer, while returning OK.
45    // The returned buffer will have a reference count of 1.
46    // If nonBlocking is true and a buffer is not immediately available,
47    // buffer is set to NULL and it returns WOULD_BLOCK.
48    // If requestedSize is 0, any free MediaBuffer will be returned.
49    // If requestedSize is > 0, the returned MediaBuffer should have buffer
50    // size of at least requstedSize.
51    status_t acquire_buffer(
52            MediaBuffer **buffer, bool nonBlocking = false, size_t requestedSize = 0);
53
54    size_t buffers() const { return mBuffers.size(); }
55
56    // If buffer is nullptr, have acquire_buffer() check for remote release.
57    virtual void signalBufferReturned(MediaBuffer *buffer);
58
59private:
60    friend class MediaBuffer;
61
62    Mutex mLock;
63    Condition mCondition;
64    size_t mGrowthLimit;  // Do not automatically grow group larger than this.
65    std::list<MediaBuffer *> mBuffers;
66
67    MediaBufferGroup(const MediaBufferGroup &);
68    MediaBufferGroup &operator=(const MediaBufferGroup &);
69};
70
71}  // namespace android
72
73#endif  // MEDIA_BUFFER_GROUP_H_
74