Home | History | Annotate | Download | only in src
      1 /*
      2  * Copyright (C) 2012 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_PULLER_H
     18 #define _MEDIA_BUFFER_PULLER_H
     19 
     20 #include <utils/threads.h>
     21 #include <utils/Vector.h>
     22 
     23 
     24 namespace android {
     25 
     26 struct MediaSource;
     27 struct MediaBuffer;
     28 
     29 /*
     30  * An object of this class can pull a list of media buffers
     31  * from a MediaSource repeatedly. The user can then get the
     32  * buffers from that list.
     33  */
     34 struct MediaBufferPuller {
     35 public:
     36     MediaBufferPuller(const sp<MediaSource>& source);
     37     ~MediaBufferPuller();
     38 
     39     // Start to build up the list of the buffers.
     40     void start();
     41 
     42     // Release the list of the available buffers, and stop
     43     // pulling buffers from the MediaSource.
     44     void stop();
     45 
     46     // Get a buffer from the list. If there is no buffer available
     47     // at the time this method is called, NULL is returned.
     48     MediaBuffer* getBufferBlocking();
     49 
     50     // Get a buffer from the list. If there is no buffer available
     51     // at the time this method is called, it blocks waiting for
     52     // a buffer to become available or until stop() is called.
     53     MediaBuffer* getBufferNonBlocking();
     54 
     55     // Add a buffer to the end of the list available media buffers
     56     void putBuffer(MediaBuffer* buffer);
     57 
     58     // Check whether the source returned an error or not.
     59     bool hasMediaSourceReturnedError() const;
     60 
     61 private:
     62     static int acquireThreadStart(void* arg);
     63     void acquireThreadFunc();
     64 
     65     static int releaseThreadStart(void* arg);
     66     void releaseThreadFunc();
     67 
     68     sp<MediaSource> mSource;
     69     Vector<MediaBuffer*> mBuffers;
     70     Vector<MediaBuffer*> mReleaseBuffers;
     71 
     72     mutable Mutex mLock;
     73     Condition mUserCond;     // for the user of this class
     74     Condition mAcquireCond;  // for the acquire thread
     75     Condition mReleaseCond;  // for the release thread
     76 
     77     bool mAskToStart;      // Asks the threads to start
     78     bool mAskToStop;       // Asks the threads to stop
     79     bool mAcquireStopped;  // The acquire thread has stopped
     80     bool mReleaseStopped;  // The release thread has stopped
     81     status_t mSourceError; // Error returned by MediaSource read
     82 
     83     // Don't call me!
     84     MediaBufferPuller(const MediaBufferPuller&);
     85     MediaBufferPuller& operator=(const MediaBufferPuller&);
     86 };
     87 
     88 }  // namespace android
     89 
     90 #endif  // _MEDIA_BUFFER_PULLER_H
     91