Upstream version 8.36.161.0
[platform/framework/web/crosswalk.git] / src / xwalk / runtime / android / core_internal / src / org / xwalk / core / internal / 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.core.internal.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
35     protected class AudioCodecElement {
36         public String codecName;
37
38         public AudioCodecElement(String name) {
39             codecName = name;
40         }
41
42         // Objects of this class need to be added to a HashSet, so we implement equals()
43         // and hashCode() to support comparison operation.
44         public boolean equals(Object obj) {
45             return this.codecName.equals(((AudioCodecElement)obj).codecName);
46         }
47
48         public int hashCode() {
49             return codecName.hashCode();
50         }
51     }
52
53     protected class VideoCodecElement {
54         public String codecName;
55         public boolean isEncoder;
56         public boolean hwAccel;
57
58         public VideoCodecElement(String name, boolean encoder, boolean hardware) {
59             codecName = name;
60             isEncoder = encoder;
61             hwAccel = hardware;
62         }
63
64         // Objects of this class need to be added to a HashSet, so we implement equals()
65         // and hashCode() to support comparison operation.
66         public boolean equals(Object obj) {
67             return this.codecName.equals(((VideoCodecElement)obj).codecName)
68                     && this.isEncoder == ((VideoCodecElement)obj).isEncoder
69                             && this.hwAccel == ((VideoCodecElement)obj).hwAccel;
70         }
71
72         public int hashCode() {
73             return codecName.hashCode();
74         }
75     }
76
77     public static XWalkMediaCodec Create(DeviceCapabilities instance) {
78         XWalkMediaCodec codec;
79         if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
80             codec = new MediaCodec(instance);
81         } else {
82             codec = new MediaCodecNull(instance);
83         }
84         return codec;
85     }
86
87     public abstract JSONObject getCodecsInfo();
88 }