• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2018 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 package android.media;
18 
19 import android.annotation.NonNull;
20 import android.os.ParcelFileDescriptor;
21 import android.util.Log;
22 
23 import java.io.IOException;
24 
25 /**
26  * Structure of data source descriptor for sources using file descriptor.
27  *
28  * Used by {@link MediaPlayer2#setDataSource}, {@link MediaPlayer2#setNextDataSource} and
29  * {@link MediaPlayer2#setNextDataSources} to set data source for playback.
30  *
31  * <p>Users should use {@link Builder} to create {@link FileDataSourceDesc}.
32  * @hide
33  */
34 public class FileDataSourceDesc extends DataSourceDesc {
35     private static final String TAG = "FileDataSourceDesc";
36 
37     /**
38      * Used when the length of file descriptor is unknown.
39      *
40      * @see #getLength()
41      */
42     public static final long FD_LENGTH_UNKNOWN = LONG_MAX;
43 
44     private ParcelFileDescriptor mPFD;
45     private long mOffset = 0;
46     private long mLength = FD_LENGTH_UNKNOWN;
47     private int mCount = 0;
48     private boolean mClosed = false;
49 
FileDataSourceDesc(String mediaId, long startPositionMs, long endPositionMs, ParcelFileDescriptor pfd, long offset, long length)50     FileDataSourceDesc(String mediaId, long startPositionMs, long endPositionMs,
51             ParcelFileDescriptor pfd, long offset, long length) {
52         super(mediaId, startPositionMs, endPositionMs);
53         mPFD = pfd;
54         mOffset = offset;
55         mLength = length;
56     }
57 
58     /**
59      * Releases the resources held by this {@code FileDataSourceDesc} object.
60      */
61     @Override
close()62     void close() {
63         super.close();
64         decCount();
65     }
66 
67     /**
68      * Decrements usage count by {@link MediaPlayer2}.
69      * If this is the last usage, also releases the file descriptor held by this
70      * {@code FileDataSourceDesc} object.
71      */
decCount()72     void decCount() {
73         synchronized (this) {
74             --mCount;
75             if (mCount > 0) {
76                 return;
77             }
78 
79             try {
80                 mPFD.close();
81                 mClosed = true;
82             } catch (IOException e) {
83                 Log.e(TAG, "failed to close pfd: " + e);
84             }
85         }
86     }
87 
88     /**
89      * Increments usage count by {@link MediaPlayer2} if PFD has not been closed.
90      */
incCount()91     void incCount() {
92         synchronized (this) {
93             if (!mClosed) {
94                 ++mCount;
95             }
96         }
97     }
98 
99     /**
100      * Return the status of underline ParcelFileDescriptor
101      * @return true if underline ParcelFileDescriptor is closed, false otherwise.
102      */
isPFDClosed()103     boolean isPFDClosed() {
104         synchronized (this) {
105             return mClosed;
106         }
107     }
108 
109     /**
110      * Return the ParcelFileDescriptor of this data source.
111      * @return the ParcelFileDescriptor of this data source
112      */
getParcelFileDescriptor()113     public @NonNull ParcelFileDescriptor getParcelFileDescriptor() {
114         return mPFD;
115     }
116 
117     /**
118      * Return the offset associated with the ParcelFileDescriptor of this data source.
119      * It's meaningful only when it has been set by the {@link Builder}.
120      * @return the offset associated with the ParcelFileDescriptor of this data source
121      */
getOffset()122     public long getOffset() {
123         return mOffset;
124     }
125 
126     /**
127      * Return the content length associated with the ParcelFileDescriptor of this data source.
128      * {@link #FD_LENGTH_UNKNOWN} means same as the length of source content.
129      * @return the content length associated with the ParcelFileDescriptor of this data source
130      */
getLength()131     public long getLength() {
132         return mLength;
133     }
134 }
135