1427e38084a16ec063983346347decf3ec461eed1James Dong/*
2427e38084a16ec063983346347decf3ec461eed1James Dong * Copyright (C) 2012 The Android Open Source Project
3427e38084a16ec063983346347decf3ec461eed1James Dong *
4427e38084a16ec063983346347decf3ec461eed1James Dong * Licensed under the Apache License, Version 2.0 (the "License");
5427e38084a16ec063983346347decf3ec461eed1James Dong * you may not use this file except in compliance with the License.
6427e38084a16ec063983346347decf3ec461eed1James Dong * You may obtain a copy of the License at
7427e38084a16ec063983346347decf3ec461eed1James Dong *
8427e38084a16ec063983346347decf3ec461eed1James Dong *      http://www.apache.org/licenses/LICENSE-2.0
9427e38084a16ec063983346347decf3ec461eed1James Dong *
10427e38084a16ec063983346347decf3ec461eed1James Dong * Unless required by applicable law or agreed to in writing, software
11427e38084a16ec063983346347decf3ec461eed1James Dong * distributed under the License is distributed on an "AS IS" BASIS,
12427e38084a16ec063983346347decf3ec461eed1James Dong * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13427e38084a16ec063983346347decf3ec461eed1James Dong * See the License for the specific language governing permissions and
14427e38084a16ec063983346347decf3ec461eed1James Dong * limitations under the License.
15427e38084a16ec063983346347decf3ec461eed1James Dong */
16427e38084a16ec063983346347decf3ec461eed1James Dong
17427e38084a16ec063983346347decf3ec461eed1James Dong#ifndef _MEDIA_BUFFER_PULLER_H
18427e38084a16ec063983346347decf3ec461eed1James Dong#define _MEDIA_BUFFER_PULLER_H
19427e38084a16ec063983346347decf3ec461eed1James Dong
20427e38084a16ec063983346347decf3ec461eed1James Dong#include <utils/threads.h>
21427e38084a16ec063983346347decf3ec461eed1James Dong#include <utils/Vector.h>
22427e38084a16ec063983346347decf3ec461eed1James Dong
23427e38084a16ec063983346347decf3ec461eed1James Dong
24427e38084a16ec063983346347decf3ec461eed1James Dongnamespace android {
25427e38084a16ec063983346347decf3ec461eed1James Dong
26427e38084a16ec063983346347decf3ec461eed1James Dongstruct MediaSource;
27427e38084a16ec063983346347decf3ec461eed1James Dongstruct MediaBuffer;
28427e38084a16ec063983346347decf3ec461eed1James Dong
29427e38084a16ec063983346347decf3ec461eed1James Dong/*
30427e38084a16ec063983346347decf3ec461eed1James Dong * An object of this class can pull a list of media buffers
31427e38084a16ec063983346347decf3ec461eed1James Dong * from a MediaSource repeatedly. The user can then get the
32427e38084a16ec063983346347decf3ec461eed1James Dong * buffers from that list.
33427e38084a16ec063983346347decf3ec461eed1James Dong */
34427e38084a16ec063983346347decf3ec461eed1James Dongstruct MediaBufferPuller {
35427e38084a16ec063983346347decf3ec461eed1James Dongpublic:
36427e38084a16ec063983346347decf3ec461eed1James Dong    MediaBufferPuller(const sp<MediaSource>& source);
37427e38084a16ec063983346347decf3ec461eed1James Dong    ~MediaBufferPuller();
38427e38084a16ec063983346347decf3ec461eed1James Dong
39427e38084a16ec063983346347decf3ec461eed1James Dong    // Start to build up the list of the buffers.
40427e38084a16ec063983346347decf3ec461eed1James Dong    void start();
41427e38084a16ec063983346347decf3ec461eed1James Dong
42427e38084a16ec063983346347decf3ec461eed1James Dong    // Release the list of the available buffers, and stop
43427e38084a16ec063983346347decf3ec461eed1James Dong    // pulling buffers from the MediaSource.
44427e38084a16ec063983346347decf3ec461eed1James Dong    void stop();
45427e38084a16ec063983346347decf3ec461eed1James Dong
46427e38084a16ec063983346347decf3ec461eed1James Dong    // Get a buffer from the list. If there is no buffer available
47427e38084a16ec063983346347decf3ec461eed1James Dong    // at the time this method is called, NULL is returned.
48427e38084a16ec063983346347decf3ec461eed1James Dong    MediaBuffer* getBufferBlocking();
49427e38084a16ec063983346347decf3ec461eed1James Dong
50427e38084a16ec063983346347decf3ec461eed1James Dong    // Get a buffer from the list. If there is no buffer available
51427e38084a16ec063983346347decf3ec461eed1James Dong    // at the time this method is called, it blocks waiting for
52427e38084a16ec063983346347decf3ec461eed1James Dong    // a buffer to become available or until stop() is called.
53427e38084a16ec063983346347decf3ec461eed1James Dong    MediaBuffer* getBufferNonBlocking();
54427e38084a16ec063983346347decf3ec461eed1James Dong
55427e38084a16ec063983346347decf3ec461eed1James Dong    // Add a buffer to the end of the list available media buffers
56427e38084a16ec063983346347decf3ec461eed1James Dong    void putBuffer(MediaBuffer* buffer);
57427e38084a16ec063983346347decf3ec461eed1James Dong
58427e38084a16ec063983346347decf3ec461eed1James Dong    // Check whether the source returned an error or not.
59427e38084a16ec063983346347decf3ec461eed1James Dong    bool hasMediaSourceReturnedError() const;
60427e38084a16ec063983346347decf3ec461eed1James Dong
61427e38084a16ec063983346347decf3ec461eed1James Dongprivate:
62427e38084a16ec063983346347decf3ec461eed1James Dong    static int acquireThreadStart(void* arg);
63427e38084a16ec063983346347decf3ec461eed1James Dong    void acquireThreadFunc();
64427e38084a16ec063983346347decf3ec461eed1James Dong
65427e38084a16ec063983346347decf3ec461eed1James Dong    static int releaseThreadStart(void* arg);
66427e38084a16ec063983346347decf3ec461eed1James Dong    void releaseThreadFunc();
67427e38084a16ec063983346347decf3ec461eed1James Dong
68427e38084a16ec063983346347decf3ec461eed1James Dong    sp<MediaSource> mSource;
69427e38084a16ec063983346347decf3ec461eed1James Dong    Vector<MediaBuffer*> mBuffers;
70427e38084a16ec063983346347decf3ec461eed1James Dong    Vector<MediaBuffer*> mReleaseBuffers;
71427e38084a16ec063983346347decf3ec461eed1James Dong
72427e38084a16ec063983346347decf3ec461eed1James Dong    mutable Mutex mLock;
73427e38084a16ec063983346347decf3ec461eed1James Dong    Condition mUserCond;     // for the user of this class
74427e38084a16ec063983346347decf3ec461eed1James Dong    Condition mAcquireCond;  // for the acquire thread
75427e38084a16ec063983346347decf3ec461eed1James Dong    Condition mReleaseCond;  // for the release thread
76427e38084a16ec063983346347decf3ec461eed1James Dong
77427e38084a16ec063983346347decf3ec461eed1James Dong    bool mAskToStart;      // Asks the threads to start
78427e38084a16ec063983346347decf3ec461eed1James Dong    bool mAskToStop;       // Asks the threads to stop
79427e38084a16ec063983346347decf3ec461eed1James Dong    bool mAcquireStopped;  // The acquire thread has stopped
80427e38084a16ec063983346347decf3ec461eed1James Dong    bool mReleaseStopped;  // The release thread has stopped
81427e38084a16ec063983346347decf3ec461eed1James Dong    status_t mSourceError; // Error returned by MediaSource read
82427e38084a16ec063983346347decf3ec461eed1James Dong
83427e38084a16ec063983346347decf3ec461eed1James Dong    // Don't call me!
84427e38084a16ec063983346347decf3ec461eed1James Dong    MediaBufferPuller(const MediaBufferPuller&);
85427e38084a16ec063983346347decf3ec461eed1James Dong    MediaBufferPuller& operator=(const MediaBufferPuller&);
86427e38084a16ec063983346347decf3ec461eed1James Dong};
87427e38084a16ec063983346347decf3ec461eed1James Dong
88427e38084a16ec063983346347decf3ec461eed1James Dong}  // namespace android
89427e38084a16ec063983346347decf3ec461eed1James Dong
90427e38084a16ec063983346347decf3ec461eed1James Dong#endif  // _MEDIA_BUFFER_PULLER_H
91