5c5e2cb597418270f4665a82b169ebd4f21580bf
[platform/framework/web/crosswalk.git] / src / xwalk / runtime / android / java / src / org / xwalk / runtime / extension / api / device_capabilities / XWalkMediaCodec.java
1 // Copyright (c) 2013 Intel Corporation. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 package org.xwalk.runtime.extension.api.device_capabilities;
6
7 import java.util.Set;
8
9 import org.json.JSONObject;
10
11 import android.os.Build;
12
13 abstract class XWalkMediaCodec {
14     private static final String TAG = "XWalkMediaCodec";
15
16     /** Android MediaCodec API reports codec with namespace instead of common codec name.
17      *  For example: "OMX.google.h263.decoder", what this API want to report is "h263".
18      *
19      *  The below static lists are used to parse out the common codec name. The codec names
20      *  are referenced by below links, it maybe adjusted on demand.
21      *
22      *  http://www.chromium.org/audio-video
23      *  https://developer.mozilla.org/en-US/docs/HTML/Supported_media_formats
24      */
25     protected static final String[] AUDIO_CODEC_NAMES =
26             {"ALAC", "MP3", "AMRNB", "AMRWB", "AAC", "G711", "VORBIS", "WMA", "FLAC", "OPUS"};
27     protected static final String[] VIDEO_CODEC_NAMES =
28             {"H263", "H264", "MPEG4", "AVC", "WMV", "VP8", "Theora"};
29
30     protected Set<AudioCodecElement> mAudioCodecsSet;
31     protected Set<VideoCodecElement> mVideoCodecsSet;
32
33     protected DeviceCapabilities mDeviceCapabilities;
34     private static XWalkMediaCodec sInstance;
35
36     protected class AudioCodecElement {
37         public String codecName;
38
39         public AudioCodecElement(String name) {
40             codecName = name;
41         }
42
43         // Objects of this class need to be added to a HashSet, so we implement equals()
44         // and hashCode() to support comparison operation.
45         public boolean equals(Object obj) {
46             return this.codecName.equals(((AudioCodecElement)obj).codecName);
47         }
48
49         public int hashCode() {
50             return codecName.hashCode();
51         }
52     }
53
54     protected class VideoCodecElement {
55         public String codecName;
56         public boolean isEncoder;
57         public boolean hwAccel;
58
59         public VideoCodecElement(String name, boolean encoder, boolean hardware) {
60             codecName = name;
61             isEncoder = encoder;
62             hwAccel = hardware;
63         }
64
65         // Objects of this class need to be added to a HashSet, so we implement equals()
66         // and hashCode() to support comparison operation.
67         public boolean equals(Object obj) {
68             return this.codecName.equals(((VideoCodecElement)obj).codecName)
69                     && this.isEncoder == ((VideoCodecElement)obj).isEncoder
70                             && this.hwAccel == ((VideoCodecElement)obj).hwAccel;
71         }
72
73         public int hashCode() {
74             return codecName.hashCode();
75         }
76     }
77
78     public static XWalkMediaCodec getInstance(DeviceCapabilities instance) {
79         if (sInstance == null) {
80             if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
81                 sInstance = new MediaCodec(instance);
82             } else {
83                 sInstance = new MediaCodecNull(instance);
84             }
85         }
86         return sInstance;
87     }
88
89     public abstract JSONObject getCodecsInfo();
90 }