Home | History | Annotate | Download | only in media
      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 
     18 package android.media;
     19 
     20 import java.io.Closeable;
     21 import java.io.IOException;
     22 
     23 /**
     24  * For supplying media data to the framework. Implement this if your app has
     25  * special requirements for the way media data is obtained.
     26  *
     27  * <p class="note">Methods of this interface may be called on multiple different
     28  * threads. There will be a thread synchronization point between each call to ensure that
     29  * modifications to the state of your MediaDataSource are visible to future calls. This means
     30  * you don't need to do your own synchronization unless you're modifying the
     31  * MediaDataSource from another thread while it's being used by the framework.</p>
     32  */
     33 public abstract class MediaDataSource implements Closeable {
     34     /**
     35      * Called to request data from the given position.
     36      *
     37      * Implementations should fill {@code buffer} with up to {@code size}
     38      * bytes of data, and return the number of valid bytes in the buffer.
     39      *
     40      * Return {@code 0} if size is zero (thus no bytes are read).
     41      *
     42      * Return {@code -1} to indicate that end of stream is reached.
     43      *
     44      * @param position the position in the data source to read from.
     45      * @param buffer the buffer to read the data into.
     46      * @param offset the offset within buffer to read the data into.
     47      * @param size the number of bytes to read.
     48      * @throws IOException on fatal errors.
     49      * @return the number of bytes read, or -1 if there was an error.
     50      */
     51     public abstract int readAt(long position, byte[] buffer, int offset, int size)
     52             throws IOException;
     53 
     54     /**
     55      * Called to get the size of the data source.
     56      *
     57      * @throws IOException on fatal errors
     58      * @return the size of data source in bytes, or -1 if the size is unknown.
     59      */
     60     public abstract long getSize() throws IOException;
     61 }
     62