MediaResourceGetter.java revision a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7
1// Copyright 2013 The Chromium Authors. 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 5package org.chromium.content.browser; 6 7import android.content.Context; 8import android.content.pm.PackageManager; 9import android.media.MediaMetadataRetriever; 10import android.net.ConnectivityManager; 11import android.net.NetworkInfo; 12import android.net.Uri; 13import android.text.TextUtils; 14import android.util.Log; 15 16import org.chromium.base.CalledByNative; 17import org.chromium.base.JNINamespace; 18import org.chromium.base.PathUtils; 19 20import java.io.File; 21import java.util.HashMap; 22 23/** 24 * Java counterpart of android MediaResourceGetter. 25 */ 26@JNINamespace("content") 27class MediaResourceGetter { 28 29 private static final String TAG = "MediaResourceGetter"; 30 31 private static class MediaMetadata { 32 private final int mDurationInMilliseconds; 33 private final int mWidth; 34 private final int mHeight; 35 private final boolean mSuccess; 36 37 private MediaMetadata(int durationInMilliseconds, int width, int height, boolean success) { 38 mDurationInMilliseconds = durationInMilliseconds; 39 mWidth = width; 40 mHeight = height; 41 mSuccess = success; 42 } 43 44 @CalledByNative("MediaMetadata") 45 private int getDurationInMilliseconds() { return mDurationInMilliseconds; } 46 47 @CalledByNative("MediaMetadata") 48 private int getWidth() { return mWidth; } 49 50 @CalledByNative("MediaMetadata") 51 private int getHeight() { return mHeight; } 52 53 @CalledByNative("MediaMetadata") 54 private boolean isSuccess() { return mSuccess; } 55 } 56 57 @CalledByNative 58 private static MediaMetadata extractMediaMetadata(Context context, String url, String cookies) { 59 int durationInMilliseconds = 0; 60 int width = 0; 61 int height = 0; 62 boolean success = false; 63 // TODO(qinmin): use ConnectionTypeObserver to listen to the network type change. 64 ConnectivityManager mConnectivityManager = 65 (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); 66 if (mConnectivityManager != null) { 67 if (context.checkCallingOrSelfPermission( 68 android.Manifest.permission.ACCESS_NETWORK_STATE) != 69 PackageManager.PERMISSION_GRANTED) { 70 return new MediaMetadata(0, 0, 0, false); 71 } 72 73 NetworkInfo info = mConnectivityManager.getActiveNetworkInfo(); 74 if (info == null) { 75 return new MediaMetadata(durationInMilliseconds, width, height, success); 76 } 77 switch (info.getType()) { 78 case ConnectivityManager.TYPE_ETHERNET: 79 case ConnectivityManager.TYPE_WIFI: 80 break; 81 case ConnectivityManager.TYPE_WIMAX: 82 case ConnectivityManager.TYPE_MOBILE: 83 default: 84 return new MediaMetadata(durationInMilliseconds, width, height, success); 85 } 86 } 87 88 MediaMetadataRetriever retriever = new MediaMetadataRetriever(); 89 try { 90 Uri uri = Uri.parse(url); 91 String scheme = uri.getScheme(); 92 if (scheme == null || scheme.equals("file")) { 93 File file = new File(uri.getPath()); 94 String path = file.getAbsolutePath(); 95 if (file.exists() && (path.startsWith("/mnt/sdcard/") || 96 path.startsWith("/sdcard/") || 97 path.startsWith(PathUtils.getExternalStorageDirectory()))) { 98 retriever.setDataSource(path); 99 } else { 100 Log.e(TAG, "Unable to read file: " + url); 101 return new MediaMetadata(durationInMilliseconds, width, height, success); 102 } 103 } else { 104 HashMap<String, String> headersMap = new HashMap<String, String>(); 105 if (!TextUtils.isEmpty(cookies)) { 106 headersMap.put("Cookie", cookies); 107 } 108 retriever.setDataSource(url, headersMap); 109 } 110 String duration = retriever.extractMetadata( 111 MediaMetadataRetriever.METADATA_KEY_DURATION); 112 String videoWidth = retriever.extractMetadata( 113 MediaMetadataRetriever.METADATA_KEY_VIDEO_WIDTH); 114 String videoHeight = retriever.extractMetadata( 115 MediaMetadataRetriever.METADATA_KEY_VIDEO_HEIGHT); 116 if (duration == null || videoWidth == null || videoHeight == null) { 117 return new MediaMetadata(durationInMilliseconds, width, height, success); 118 } 119 durationInMilliseconds = Integer.parseInt(duration); 120 width = Integer.parseInt(videoWidth); 121 height = Integer.parseInt(videoHeight); 122 success = true; 123 } catch (IllegalArgumentException e) { 124 Log.e(TAG, "Invalid url: " + e); 125 } catch (RuntimeException e) { 126 Log.e(TAG, "Invalid url: " + e); 127 } 128 return new MediaMetadata(durationInMilliseconds, width, height, success); 129 } 130} 131