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