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