summaryrefslogtreecommitdiff
path: root/android/media/DataSourceDesc.java
blob: 73fad7ad4bf378098cd5b43218aeb1c71dcfa336 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
/*
 * Copyright 2018 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package android.media;

import android.annotation.IntDef;
import android.annotation.NonNull;
import android.annotation.Nullable;
import android.content.Context;
import android.content.res.AssetFileDescriptor;
import android.net.Uri;
import android.os.Parcel;
import android.os.Parcelable;

import com.android.internal.util.Preconditions;

import java.io.FileDescriptor;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.net.HttpCookie;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Structure for data source descriptor.
 *
 * Used by {@link MediaPlayer2#setDataSource(DataSourceDesc)}
 * to set data source for playback.
 *
 * <p>Users should use {@link Builder} to change {@link DataSourceDesc}.
 *
 */
public final class DataSourceDesc {
    /* No data source has been set yet */
    public static final int TYPE_NONE     = 0;
    /* data source is type of MediaDataSource */
    public static final int TYPE_CALLBACK = 1;
    /* data source is type of FileDescriptor */
    public static final int TYPE_FD       = 2;
    /* data source is type of Uri */
    public static final int TYPE_URI      = 3;

    // intentionally less than long.MAX_VALUE
    public static final long LONG_MAX = 0x7ffffffffffffffL;

    private int mType = TYPE_NONE;

    private Media2DataSource mMedia2DataSource;

    private FileDescriptor mFD;
    private long mFDOffset = 0;
    private long mFDLength = LONG_MAX;

    private Uri mUri;
    private Map<String, String> mUriHeader;
    private List<HttpCookie> mUriCookies;
    private Context mUriContext;

    private long mId = 0;
    private long mStartPositionMs = 0;
    private long mEndPositionMs = LONG_MAX;

    private DataSourceDesc() {
    }

    /**
     * Return the Id of data source.
     * @return the Id of data source
     */
    public long getId() {
        return mId;
    }

    /**
     * Return the position in milliseconds at which the playback will start.
     * @return the position in milliseconds at which the playback will start
     */
    public long getStartPosition() {
        return mStartPositionMs;
    }

    /**
     * Return the position in milliseconds at which the playback will end.
     * -1 means ending at the end of source content.
     * @return the position in milliseconds at which the playback will end
     */
    public long getEndPosition() {
        return mEndPositionMs;
    }

    /**
     * Return the type of data source.
     * @return the type of data source
     */
    public int getType() {
        return mType;
    }

    /**
     * Return the Media2DataSource of this data source.
     * It's meaningful only when {@code getType} returns {@link #TYPE_CALLBACK}.
     * @return the Media2DataSource of this data source
     */
    public Media2DataSource getMedia2DataSource() {
        return mMedia2DataSource;
    }

    /**
     * Return the FileDescriptor of this data source.
     * It's meaningful only when {@code getType} returns {@link #TYPE_FD}.
     * @return the FileDescriptor of this data source
     */
    public FileDescriptor getFileDescriptor() {
        return mFD;
    }

    /**
     * Return the offset associated with the FileDescriptor of this data source.
     * It's meaningful only when {@code getType} returns {@link #TYPE_FD} and it has
     * been set by the {@link Builder}.
     * @return the offset associated with the FileDescriptor of this data source
     */
    public long getFileDescriptorOffset() {
        return mFDOffset;
    }

    /**
     * Return the content length associated with the FileDescriptor of this data source.
     * It's meaningful only when {@code getType} returns {@link #TYPE_FD}.
     * -1 means same as the length of source content.
     * @return the content length associated with the FileDescriptor of this data source
     */
    public long getFileDescriptorLength() {
        return mFDLength;
    }

    /**
     * Return the Uri of this data source.
     * It's meaningful only when {@code getType} returns {@link #TYPE_URI}.
     * @return the Uri of this data source
     */
    public Uri getUri() {
        return mUri;
    }

    /**
     * Return the Uri headers of this data source.
     * It's meaningful only when {@code getType} returns {@link #TYPE_URI}.
     * @return the Uri headers of this data source
     */
    public Map<String, String> getUriHeaders() {
        if (mUriHeader == null) {
            return null;
        }
        return new HashMap<String, String>(mUriHeader);
    }

    /**
     * Return the Uri cookies of this data source.
     * It's meaningful only when {@code getType} returns {@link #TYPE_URI}.
     * @return the Uri cookies of this data source
     */
    public List<HttpCookie> getUriCookies() {
        if (mUriCookies == null) {
            return null;
        }
        return new ArrayList<HttpCookie>(mUriCookies);
    }

    /**
     * Return the Context used for resolving the Uri of this data source.
     * It's meaningful only when {@code getType} returns {@link #TYPE_URI}.
     * @return the Context used for resolving the Uri of this data source
     */
    public Context getUriContext() {
        return mUriContext;
    }

    /**
     * Builder class for {@link DataSourceDesc} objects.
     * <p> Here is an example where <code>Builder</code> is used to define the
     * {@link DataSourceDesc} to be used by a {@link MediaPlayer2} instance:
     *
     * <pre class="prettyprint">
     * DataSourceDesc oldDSD = mediaplayer2.getDataSourceDesc();
     * DataSourceDesc newDSD = new DataSourceDesc.Builder(oldDSD)
     *         .setStartPosition(1000)
     *         .setEndPosition(15000)
     *         .build();
     * mediaplayer2.setDataSourceDesc(newDSD);
     * </pre>
     */
    public static class Builder {
        private int mType = TYPE_NONE;

        private Media2DataSource mMedia2DataSource;

        private FileDescriptor mFD;
        private long mFDOffset = 0;
        private long mFDLength = LONG_MAX;

        private Uri mUri;
        private Map<String, String> mUriHeader;
        private List<HttpCookie> mUriCookies;
        private Context mUriContext;

        private long mId = 0;
        private long mStartPositionMs = 0;
        private long mEndPositionMs = LONG_MAX;

        /**
         * Constructs a new Builder with the defaults.
         */
        public Builder() {
        }

        /**
         * Constructs a new Builder from a given {@link DataSourceDesc} instance
         * @param dsd the {@link DataSourceDesc} object whose data will be reused
         * in the new Builder.
         */
        public Builder(DataSourceDesc dsd) {
            mType = dsd.mType;
            mMedia2DataSource = dsd.mMedia2DataSource;
            mFD = dsd.mFD;
            mFDOffset = dsd.mFDOffset;
            mFDLength = dsd.mFDLength;
            mUri = dsd.mUri;
            mUriHeader = dsd.mUriHeader;
            mUriCookies = dsd.mUriCookies;
            mUriContext = dsd.mUriContext;

            mId = dsd.mId;
            mStartPositionMs = dsd.mStartPositionMs;
            mEndPositionMs = dsd.mEndPositionMs;
        }

        /**
         * Combines all of the fields that have been set and return a new
         * {@link DataSourceDesc} object. <code>IllegalStateException</code> will be
         * thrown if there is conflict between fields.
         *
         * @return a new {@link DataSourceDesc} object
         */
        public DataSourceDesc build() {
            if (mType != TYPE_CALLBACK
                && mType != TYPE_FD
                && mType != TYPE_URI) {
                throw new IllegalStateException("Illegal type: " + mType);
            }
            if (mStartPositionMs > mEndPositionMs) {
                throw new IllegalStateException("Illegal start/end position: "
                    + mStartPositionMs + " : " + mEndPositionMs);
            }

            DataSourceDesc dsd = new DataSourceDesc();
            dsd.mType = mType;
            dsd.mMedia2DataSource = mMedia2DataSource;
            dsd.mFD = mFD;
            dsd.mFDOffset = mFDOffset;
            dsd.mFDLength = mFDLength;
            dsd.mUri = mUri;
            dsd.mUriHeader = mUriHeader;
            dsd.mUriCookies = mUriCookies;
            dsd.mUriContext = mUriContext;

            dsd.mId = mId;
            dsd.mStartPositionMs = mStartPositionMs;
            dsd.mEndPositionMs = mEndPositionMs;

            return dsd;
        }

        /**
         * Sets the Id of this data source.
         *
         * @param id the Id of this data source
         * @return the same Builder instance.
         */
        public Builder setId(long id) {
            mId = id;
            return this;
        }

        /**
         * Sets the start position in milliseconds at which the playback will start.
         * Any negative number is treated as 0.
         *
         * @param position the start position in milliseconds at which the playback will start
         * @return the same Builder instance.
         *
         */
        public Builder setStartPosition(long position) {
            if (position < 0) {
                position = 0;
            }
            mStartPositionMs = position;
            return this;
        }

        /**
         * Sets the end position in milliseconds at which the playback will end.
         * Any negative number is treated as maximum length of the data source.
         *
         * @param position the end position in milliseconds at which the playback will end
         * @return the same Builder instance.
         */
        public Builder setEndPosition(long position) {
            if (position < 0) {
                position = LONG_MAX;
            }
            mEndPositionMs = position;
            return this;
        }

        /**
         * Sets the data source (Media2DataSource) to use.
         *
         * @param m2ds the Media2DataSource for the media you want to play
         * @return the same Builder instance.
         * @throws NullPointerException if m2ds is null.
         */
        public Builder setDataSource(Media2DataSource m2ds) {
            Preconditions.checkNotNull(m2ds);
            resetDataSource();
            mType = TYPE_CALLBACK;
            mMedia2DataSource = m2ds;
            return this;
        }

        /**
         * Sets the data source (FileDescriptor) to use. The FileDescriptor must be
         * seekable (N.B. a LocalSocket is not seekable). It is the caller's responsibility
         * to close the file descriptor after the source has been used.
         *
         * @param fd the FileDescriptor for the file you want to play
         * @return the same Builder instance.
         * @throws NullPointerException if fd is null.
         */
        public Builder setDataSource(FileDescriptor fd) {
            Preconditions.checkNotNull(fd);
            resetDataSource();
            mType = TYPE_FD;
            mFD = fd;
            return this;
        }

        /**
         * Sets the data source (FileDescriptor) to use. The FileDescriptor must be
         * seekable (N.B. a LocalSocket is not seekable). It is the caller's responsibility
         * to close the file descriptor after the source has been used.
         *
         * Any negative number for offset is treated as 0.
         * Any negative number for length is treated as maximum length of the data source.
         *
         * @param fd the FileDescriptor for the file you want to play
         * @param offset the offset into the file where the data to be played starts, in bytes
         * @param length the length in bytes of the data to be played
         * @return the same Builder instance.
         * @throws NullPointerException if fd is null.
         */
        public Builder setDataSource(FileDescriptor fd, long offset, long length) {
            Preconditions.checkNotNull(fd);
            if (offset < 0) {
                offset = 0;
            }
            if (length < 0) {
                length = LONG_MAX;
            }
            resetDataSource();
            mType = TYPE_FD;
            mFD = fd;
            mFDOffset = offset;
            mFDLength = length;
            return this;
        }

        /**
         * Sets the data source as a content Uri.
         *
         * @param context the Context to use when resolving the Uri
         * @param uri the Content URI of the data you want to play
         * @return the same Builder instance.
         * @throws NullPointerException if context or uri is null.
         */
        public Builder setDataSource(@NonNull Context context, @NonNull Uri uri) {
            Preconditions.checkNotNull(context, "context cannot be null");
            Preconditions.checkNotNull(uri, "uri cannot be null");
            resetDataSource();
            mType = TYPE_URI;
            mUri = uri;
            mUriContext = context;
            return this;
        }

        /**
         * Sets the data source as a content Uri.
         *
         * To provide cookies for the subsequent HTTP requests, you can install your own default
         * cookie handler and use other variants of setDataSource APIs instead. Alternatively, you
         * can use this API to pass the cookies as a list of HttpCookie. If the app has not
         * installed a CookieHandler already, {@link MediaPlayer2} will create a CookieManager
         * and populates its CookieStore with the provided cookies when this data source is passed
         * to {@link MediaPlayer2}. If the app has installed its own handler already, the handler
         * is required to be of CookieManager type such that {@link MediaPlayer2} can update the
         * manager’s CookieStore.
         *
         *  <p><strong>Note</strong> that the cross domain redirection is allowed by default,
         * but that can be changed with key/value pairs through the headers parameter with
         * "android-allow-cross-domain-redirect" as the key and "0" or "1" as the value to
         * disallow or allow cross domain redirection.
         *
         * @param context the Context to use when resolving the Uri
         * @param uri the Content URI of the data you want to play
         * @param headers the headers to be sent together with the request for the data
         *                The headers must not include cookies. Instead, use the cookies param.
         * @param cookies the cookies to be sent together with the request
         * @return the same Builder instance.
         * @throws NullPointerException if context or uri is null.
         */
        public Builder setDataSource(@NonNull Context context, @NonNull Uri uri,
                @Nullable Map<String, String> headers, @Nullable List<HttpCookie> cookies) {
            Preconditions.checkNotNull(uri);
            resetDataSource();
            mType = TYPE_URI;
            mUri = uri;
            if (headers != null) {
                mUriHeader = new HashMap<String, String>(headers);
            }
            if (cookies != null) {
                mUriCookies = new ArrayList<HttpCookie>(cookies);
            }
            mUriContext = context;
            return this;
        }

        private void resetDataSource() {
            mType = TYPE_NONE;
            mMedia2DataSource = null;
            mFD = null;
            mFDOffset = 0;
            mFDLength = LONG_MAX;
            mUri = null;
            mUriHeader = null;
            mUriCookies = null;
            mUriContext = null;
        }
    }
}