summaryrefslogtreecommitdiffstats
path: root/libvideoeditor/vss/stagefrightshells/src/MediaBufferPuller.h
blob: ed72a532db98dd7394840097db7abdfc497cb941 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
/*
 * Copyright (C) 2012 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#ifndef _MEDIA_BUFFER_PULLER_H
#define _MEDIA_BUFFER_PULLER_H

#include <utils/threads.h>
#include <utils/Vector.h>


namespace android {

struct MediaSource;
struct MediaBuffer;

/*
 * An object of this class can pull a list of media buffers
 * from a MediaSource repeatedly. The user can then get the
 * buffers from that list.
 */
struct MediaBufferPuller {
public:
    MediaBufferPuller(const sp<MediaSource>& source);
    ~MediaBufferPuller();

    // Start to build up the list of the buffers.
    void start();

    // Release the list of the available buffers, and stop
    // pulling buffers from the MediaSource.
    void stop();

    // Get a buffer from the list. If there is no buffer available
    // at the time this method is called, NULL is returned.
    MediaBuffer* getBufferBlocking();

    // Get a buffer from the list. If there is no buffer available
    // at the time this method is called, it blocks waiting for
    // a buffer to become available or until stop() is called.
    MediaBuffer* getBufferNonBlocking();

    // Add a buffer to the end of the list available media buffers
    void putBuffer(MediaBuffer* buffer);

    // Check whether the source returned an error or not.
    bool hasMediaSourceReturnedError() const;

private:
    static int acquireThreadStart(void* arg);
    void acquireThreadFunc();

    static int releaseThreadStart(void* arg);
    void releaseThreadFunc();

    sp<MediaSource> mSource;
    Vector<MediaBuffer*> mBuffers;
    Vector<MediaBuffer*> mReleaseBuffers;

    mutable Mutex mLock;
    Condition mUserCond;     // for the user of this class
    Condition mAcquireCond;  // for the acquire thread
    Condition mReleaseCond;  // for the release thread

    bool mAskToStart;      // Asks the threads to start
    bool mAskToStop;       // Asks the threads to stop
    bool mAcquireStopped;  // The acquire thread has stopped
    bool mReleaseStopped;  // The release thread has stopped
    status_t mSourceError; // Error returned by MediaSource read

    // Don't call me!
    MediaBufferPuller(const MediaBufferPuller&);
    MediaBufferPuller& operator=(const MediaBufferPuller&);
};

}  // namespace android

#endif  // _MEDIA_BUFFER_PULLER_H