|
14 | 14 |
|
15 | 15 | /** |
16 | 16 | * A {@link DataSource} backed by a file absolute path. |
| 17 | + * |
| 18 | + * This class actually wraps a {@link FileDescriptorDataSource} for the apply() operations. |
| 19 | + * We could pass the path directly to MediaExtractor and MediaMetadataRetriever, but that is |
| 20 | + * discouraged since they could not be able to open the file from another process. |
| 21 | + * |
| 22 | + * See {@link MediaExtractor#setDataSource(String)} documentation. |
17 | 23 | */ |
18 | 24 | public class FilePathDataSource extends DefaultDataSource { |
19 | 25 | private static final String TAG = FilePathDataSource.class.getSimpleName(); |
20 | 26 | private static final Logger LOG = new Logger(TAG); |
21 | 27 |
|
22 | | - private final FileDescriptorDataSource descriptor; |
23 | | - private FileInputStream stream; |
| 28 | + private FileDescriptorDataSource mDescriptorSource; |
| 29 | + private FileInputStream mStream; |
| 30 | + private final String mPath; |
24 | 31 |
|
25 | 32 | public FilePathDataSource(@NonNull String path) { |
26 | | - FileDescriptor fileDescriptor; |
27 | | - try { |
28 | | - stream = new FileInputStream(path); |
29 | | - fileDescriptor = stream.getFD(); |
30 | | - } catch (IOException e) { |
31 | | - release(); |
32 | | - throw new RuntimeException(e); |
| 33 | + mPath = path; |
| 34 | + } |
| 35 | + |
| 36 | + private void ensureDescriptorSource() { |
| 37 | + if (mDescriptorSource == null) { |
| 38 | + try { |
| 39 | + mStream = new FileInputStream(mPath); |
| 40 | + mDescriptorSource = new FileDescriptorDataSource(mStream.getFD()); |
| 41 | + } catch (IOException e) { |
| 42 | + release(); |
| 43 | + throw new RuntimeException(e); |
| 44 | + } |
33 | 45 | } |
34 | | - descriptor = new FileDescriptorDataSource(fileDescriptor); |
35 | 46 | } |
36 | 47 |
|
37 | 48 | @Override |
38 | 49 | public void applyExtractor(@NonNull MediaExtractor extractor) throws IOException { |
39 | | - descriptor.applyExtractor(extractor); |
| 50 | + ensureDescriptorSource(); |
| 51 | + mDescriptorSource.applyExtractor(extractor); |
40 | 52 | } |
41 | 53 |
|
42 | 54 | @Override |
43 | 55 | public void applyRetriever(@NonNull MediaMetadataRetriever retriever) { |
44 | | - descriptor.applyRetriever(retriever); |
| 56 | + ensureDescriptorSource(); |
| 57 | + mDescriptorSource.applyRetriever(retriever); |
45 | 58 | } |
46 | 59 |
|
47 | 60 | @Override |
48 | 61 | protected void release() { |
49 | 62 | super.release(); |
50 | | - descriptor.release(); |
51 | | - if (stream != null) { |
| 63 | + if (mDescriptorSource != null) { |
| 64 | + mDescriptorSource.release(); |
| 65 | + } |
| 66 | + if (mStream != null) { |
52 | 67 | try { |
53 | | - stream.close(); |
| 68 | + mStream.close(); |
54 | 69 | } catch (IOException e) { |
55 | 70 | LOG.e("Can't close input stream: ", e); |
56 | 71 | } |
57 | 72 | } |
58 | 73 | } |
| 74 | + |
| 75 | + @Override |
| 76 | + public void rewind() { |
| 77 | + super.rewind(); |
| 78 | + // I think we must recreate the stream to restart reading from the very first bytes. |
| 79 | + // This means that we must also recreate the underlying source. |
| 80 | + if (mDescriptorSource != null) { |
| 81 | + mDescriptorSource.release(); |
| 82 | + } |
| 83 | + if (mStream != null) { |
| 84 | + try { |
| 85 | + mStream.close(); |
| 86 | + } catch (IOException ignore) { } |
| 87 | + } |
| 88 | + mDescriptorSource = null; |
| 89 | + mStream = null; |
| 90 | + } |
59 | 91 | } |
0 commit comments