1bc8f53b8c1c220d9ce01526b6e3834f14af98ed5Dongwon Kang/*
2bc8f53b8c1c220d9ce01526b6e3834f14af98ed5Dongwon Kang * Copyright (C) 2009 The Android Open Source Project
3bc8f53b8c1c220d9ce01526b6e3834f14af98ed5Dongwon Kang *
4bc8f53b8c1c220d9ce01526b6e3834f14af98ed5Dongwon Kang * Licensed under the Apache License, Version 2.0 (the "License");
5bc8f53b8c1c220d9ce01526b6e3834f14af98ed5Dongwon Kang * you may not use this file except in compliance with the License.
6bc8f53b8c1c220d9ce01526b6e3834f14af98ed5Dongwon Kang * You may obtain a copy of the License at
7bc8f53b8c1c220d9ce01526b6e3834f14af98ed5Dongwon Kang *
8bc8f53b8c1c220d9ce01526b6e3834f14af98ed5Dongwon Kang *      http://www.apache.org/licenses/LICENSE-2.0
9bc8f53b8c1c220d9ce01526b6e3834f14af98ed5Dongwon Kang *
10bc8f53b8c1c220d9ce01526b6e3834f14af98ed5Dongwon Kang * Unless required by applicable law or agreed to in writing, software
11bc8f53b8c1c220d9ce01526b6e3834f14af98ed5Dongwon Kang * distributed under the License is distributed on an "AS IS" BASIS,
12bc8f53b8c1c220d9ce01526b6e3834f14af98ed5Dongwon Kang * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13bc8f53b8c1c220d9ce01526b6e3834f14af98ed5Dongwon Kang * See the License for the specific language governing permissions and
14bc8f53b8c1c220d9ce01526b6e3834f14af98ed5Dongwon Kang * limitations under the License.
15bc8f53b8c1c220d9ce01526b6e3834f14af98ed5Dongwon Kang */
16bc8f53b8c1c220d9ce01526b6e3834f14af98ed5Dongwon Kang
17bc8f53b8c1c220d9ce01526b6e3834f14af98ed5Dongwon Kang#ifndef MEDIA_BUFFER_GROUP_H_
18bc8f53b8c1c220d9ce01526b6e3834f14af98ed5Dongwon Kang
19bc8f53b8c1c220d9ce01526b6e3834f14af98ed5Dongwon Kang#define MEDIA_BUFFER_GROUP_H_
20bc8f53b8c1c220d9ce01526b6e3834f14af98ed5Dongwon Kang
211889c3edad32995c0cf26ae2248fe7c957b7ec84Dongwon Kang#include <list>
221889c3edad32995c0cf26ae2248fe7c957b7ec84Dongwon Kang
231889c3edad32995c0cf26ae2248fe7c957b7ec84Dongwon Kang#include <media/stagefright/MediaBufferBase.h>
241889c3edad32995c0cf26ae2248fe7c957b7ec84Dongwon Kang#include <utils/Errors.h>
251889c3edad32995c0cf26ae2248fe7c957b7ec84Dongwon Kang#include <utils/threads.h>
26bc8f53b8c1c220d9ce01526b6e3834f14af98ed5Dongwon Kang
27bc8f53b8c1c220d9ce01526b6e3834f14af98ed5Dongwon Kangnamespace android {
28bc8f53b8c1c220d9ce01526b6e3834f14af98ed5Dongwon Kang
291889c3edad32995c0cf26ae2248fe7c957b7ec84Dongwon Kangclass MediaBufferBase;
30bc8f53b8c1c220d9ce01526b6e3834f14af98ed5Dongwon Kang
31bc8f53b8c1c220d9ce01526b6e3834f14af98ed5Dongwon Kangclass MediaBufferGroup : public MediaBufferObserver {
32bc8f53b8c1c220d9ce01526b6e3834f14af98ed5Dongwon Kangpublic:
33bc8f53b8c1c220d9ce01526b6e3834f14af98ed5Dongwon Kang    MediaBufferGroup(size_t growthLimit = 0);
34bc8f53b8c1c220d9ce01526b6e3834f14af98ed5Dongwon Kang
35bc8f53b8c1c220d9ce01526b6e3834f14af98ed5Dongwon Kang    // create a media buffer group with preallocated buffers
36bc8f53b8c1c220d9ce01526b6e3834f14af98ed5Dongwon Kang    MediaBufferGroup(size_t buffers, size_t buffer_size, size_t growthLimit = 0);
37bc8f53b8c1c220d9ce01526b6e3834f14af98ed5Dongwon Kang
38bc8f53b8c1c220d9ce01526b6e3834f14af98ed5Dongwon Kang    ~MediaBufferGroup();
39bc8f53b8c1c220d9ce01526b6e3834f14af98ed5Dongwon Kang
401889c3edad32995c0cf26ae2248fe7c957b7ec84Dongwon Kang    void add_buffer(MediaBufferBase *buffer);
41bc8f53b8c1c220d9ce01526b6e3834f14af98ed5Dongwon Kang
42bc8f53b8c1c220d9ce01526b6e3834f14af98ed5Dongwon Kang    bool has_buffers();
43bc8f53b8c1c220d9ce01526b6e3834f14af98ed5Dongwon Kang
44bc8f53b8c1c220d9ce01526b6e3834f14af98ed5Dongwon Kang    // If nonBlocking is false, it blocks until a buffer is available and
45bc8f53b8c1c220d9ce01526b6e3834f14af98ed5Dongwon Kang    // passes it to the caller in *buffer, while returning OK.
46bc8f53b8c1c220d9ce01526b6e3834f14af98ed5Dongwon Kang    // The returned buffer will have a reference count of 1.
47bc8f53b8c1c220d9ce01526b6e3834f14af98ed5Dongwon Kang    // If nonBlocking is true and a buffer is not immediately available,
48bc8f53b8c1c220d9ce01526b6e3834f14af98ed5Dongwon Kang    // buffer is set to NULL and it returns WOULD_BLOCK.
49bc8f53b8c1c220d9ce01526b6e3834f14af98ed5Dongwon Kang    // If requestedSize is 0, any free MediaBuffer will be returned.
50bc8f53b8c1c220d9ce01526b6e3834f14af98ed5Dongwon Kang    // If requestedSize is > 0, the returned MediaBuffer should have buffer
51bc8f53b8c1c220d9ce01526b6e3834f14af98ed5Dongwon Kang    // size of at least requstedSize.
52bc8f53b8c1c220d9ce01526b6e3834f14af98ed5Dongwon Kang    status_t acquire_buffer(
531889c3edad32995c0cf26ae2248fe7c957b7ec84Dongwon Kang            MediaBufferBase **buffer, bool nonBlocking = false, size_t requestedSize = 0);
54bc8f53b8c1c220d9ce01526b6e3834f14af98ed5Dongwon Kang
554d0c0a8942de184f07fdd6c918c6355e5771bdffDongwon Kang    size_t buffers() const;
56bc8f53b8c1c220d9ce01526b6e3834f14af98ed5Dongwon Kang
57bc8f53b8c1c220d9ce01526b6e3834f14af98ed5Dongwon Kang    // If buffer is nullptr, have acquire_buffer() check for remote release.
581889c3edad32995c0cf26ae2248fe7c957b7ec84Dongwon Kang    virtual void signalBufferReturned(MediaBufferBase *buffer);
59bc8f53b8c1c220d9ce01526b6e3834f14af98ed5Dongwon Kang
60bc8f53b8c1c220d9ce01526b6e3834f14af98ed5Dongwon Kangprivate:
614d0c0a8942de184f07fdd6c918c6355e5771bdffDongwon Kang    struct InternalData;
624d0c0a8942de184f07fdd6c918c6355e5771bdffDongwon Kang    InternalData *mInternal;
63bc8f53b8c1c220d9ce01526b6e3834f14af98ed5Dongwon Kang
64bc8f53b8c1c220d9ce01526b6e3834f14af98ed5Dongwon Kang    MediaBufferGroup(const MediaBufferGroup &);
65bc8f53b8c1c220d9ce01526b6e3834f14af98ed5Dongwon Kang    MediaBufferGroup &operator=(const MediaBufferGroup &);
66bc8f53b8c1c220d9ce01526b6e3834f14af98ed5Dongwon Kang};
67bc8f53b8c1c220d9ce01526b6e3834f14af98ed5Dongwon Kang
68bc8f53b8c1c220d9ce01526b6e3834f14af98ed5Dongwon Kang}  // namespace android
69bc8f53b8c1c220d9ce01526b6e3834f14af98ed5Dongwon Kang
70bc8f53b8c1c220d9ce01526b6e3834f14af98ed5Dongwon Kang#endif  // MEDIA_BUFFER_GROUP_H_
71