1 /*
2  * Copyright (C) 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 package com.google.android.exoplayer2.metadata;
17 
18 import com.google.android.exoplayer2.Format;
19 import com.google.android.exoplayer2.metadata.emsg.EventMessageDecoder;
20 import com.google.android.exoplayer2.metadata.id3.Id3Decoder;
21 import com.google.android.exoplayer2.metadata.scte35.SpliceInfoDecoder;
22 import com.google.android.exoplayer2.util.MimeTypes;
23 
24 /**
25  * A factory for {@link MetadataDecoder} instances.
26  */
27 public interface MetadataDecoderFactory {
28 
29   /**
30    * Returns whether the factory is able to instantiate a {@link MetadataDecoder} for the given
31    * {@link Format}.
32    *
33    * @param format The {@link Format}.
34    * @return Whether the factory can instantiate a suitable {@link MetadataDecoder}.
35    */
supportsFormat(Format format)36   boolean supportsFormat(Format format);
37 
38   /**
39    * Creates a {@link MetadataDecoder} for the given {@link Format}.
40    *
41    * @param format The {@link Format}.
42    * @return A new {@link MetadataDecoder}.
43    * @throws IllegalArgumentException If the {@link Format} is not supported.
44    */
createDecoder(Format format)45   MetadataDecoder createDecoder(Format format);
46 
47   /**
48    * Default {@link MetadataDecoder} implementation.
49    * <p>
50    * The formats supported by this factory are:
51    * <ul>
52    * <li>ID3 ({@link Id3Decoder})</li>
53    * <li>EMSG ({@link EventMessageDecoder})</li>
54    * <li>SCTE-35 ({@link SpliceInfoDecoder})</li>
55    * </ul>
56    */
57   MetadataDecoderFactory DEFAULT = new MetadataDecoderFactory() {
58 
59     @Override
60     public boolean supportsFormat(Format format) {
61       String mimeType = format.sampleMimeType;
62       return MimeTypes.APPLICATION_ID3.equals(mimeType)
63           || MimeTypes.APPLICATION_EMSG.equals(mimeType)
64           || MimeTypes.APPLICATION_SCTE35.equals(mimeType);
65     }
66 
67     @Override
68     public MetadataDecoder createDecoder(Format format) {
69       switch (format.sampleMimeType) {
70         case MimeTypes.APPLICATION_ID3:
71           return new Id3Decoder();
72         case MimeTypes.APPLICATION_EMSG:
73           return new EventMessageDecoder();
74         case MimeTypes.APPLICATION_SCTE35:
75           return new SpliceInfoDecoder();
76         default:
77           throw new IllegalArgumentException("Attempted to create decoder for unsupported format");
78       }
79     }
80 
81   };
82 
83 }
84