1/* 2 * Copyright (C) 2015 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 17package com.android.tv.tuner; 18 19import android.content.Context; 20import android.os.ParcelFileDescriptor; 21import android.util.Log; 22import com.android.tv.tuner.DvbDeviceAccessor.DvbDeviceInfoWrapper; 23 24import java.util.List; 25import java.util.SortedSet; 26import java.util.TreeSet; 27 28/** 29 * A class to handle a hardware Linux DVB API supported tuner device. 30 */ 31public class DvbTunerHal extends TunerHal { 32 33 private static final Object sLock = new Object(); 34 // @GuardedBy("sLock") 35 private static final SortedSet<DvbDeviceInfoWrapper> sUsedDvbDevices = new TreeSet<>(); 36 37 private final DvbDeviceAccessor mDvbDeviceAccessor; 38 private DvbDeviceInfoWrapper mDvbDeviceInfo; 39 40 protected DvbTunerHal(Context context) { 41 super(context); 42 mDvbDeviceAccessor = new DvbDeviceAccessor(context); 43 } 44 45 @Override 46 protected boolean openFirstAvailable() { 47 List<DvbDeviceInfoWrapper> deviceInfoList = mDvbDeviceAccessor.getDvbDeviceList(); 48 if (deviceInfoList == null || deviceInfoList.isEmpty()) { 49 Log.e(TAG, "There's no dvb device attached"); 50 return false; 51 } 52 synchronized (sLock) { 53 for (DvbDeviceInfoWrapper deviceInfo : deviceInfoList) { 54 if (!sUsedDvbDevices.contains(deviceInfo)) { 55 if (DEBUG) Log.d(TAG, "Available device info: " + deviceInfo); 56 mDvbDeviceInfo = deviceInfo; 57 sUsedDvbDevices.add(deviceInfo); 58 getDeliverySystemTypeFromDevice(); 59 return true; 60 } 61 } 62 } 63 Log.e(TAG, "There's no available dvb devices"); 64 return false; 65 } 66 67 /** 68 * Acquires the tuner device. The requested device will be locked to the current instance if 69 * it's not acquired by others. 70 * 71 * @param deviceInfo a tuner device to open 72 * @return {@code true} if the operation was successful, {@code false} otherwise 73 */ 74 protected boolean open(DvbDeviceInfoWrapper deviceInfo) { 75 if (deviceInfo == null) { 76 Log.e(TAG, "Device info should not be null"); 77 return false; 78 } 79 if (mDvbDeviceInfo != null) { 80 Log.e(TAG, "Already acquired"); 81 return false; 82 } 83 List<DvbDeviceInfoWrapper> deviceInfoList = mDvbDeviceAccessor.getDvbDeviceList(); 84 if (deviceInfoList == null || deviceInfoList.isEmpty()) { 85 Log.e(TAG, "There's no dvb device attached"); 86 return false; 87 } 88 for (DvbDeviceInfoWrapper deviceInfoWrapper : deviceInfoList) { 89 if (deviceInfoWrapper.compareTo(deviceInfo) == 0) { 90 synchronized (sLock) { 91 if (sUsedDvbDevices.contains(deviceInfo)) { 92 Log.e(TAG, deviceInfo + " is already taken"); 93 return false; 94 } 95 sUsedDvbDevices.add(deviceInfo); 96 } 97 if (DEBUG) Log.d(TAG, "Available device info: " + deviceInfo); 98 mDvbDeviceInfo = deviceInfo; 99 return true; 100 } 101 } 102 Log.e(TAG, "There's no such dvb device attached"); 103 return false; 104 } 105 106 @Override 107 public void close() { 108 if (mDvbDeviceInfo != null) { 109 if (isStreaming()) { 110 stopTune(); 111 } 112 nativeFinalize(mDvbDeviceInfo.getId()); 113 synchronized (sLock) { 114 sUsedDvbDevices.remove(mDvbDeviceInfo); 115 } 116 mDvbDeviceInfo = null; 117 } 118 } 119 120 @Override 121 protected boolean isDeviceOpen() { 122 return (mDvbDeviceInfo != null); 123 } 124 125 @Override 126 protected long getDeviceId() { 127 if (mDvbDeviceInfo != null) { 128 return mDvbDeviceInfo.getId(); 129 } 130 return -1; 131 } 132 133 @Override 134 protected int openDvbFrontEndFd() { 135 if (mDvbDeviceInfo != null) { 136 ParcelFileDescriptor descriptor = mDvbDeviceAccessor.openDvbDevice( 137 mDvbDeviceInfo, DvbDeviceAccessor.DVB_DEVICE_FRONTEND); 138 if (descriptor != null) { 139 return descriptor.detachFd(); 140 } 141 } 142 return -1; 143 } 144 145 @Override 146 protected int openDvbDemuxFd() { 147 if (mDvbDeviceInfo != null) { 148 ParcelFileDescriptor descriptor = mDvbDeviceAccessor.openDvbDevice( 149 mDvbDeviceInfo, DvbDeviceAccessor.DVB_DEVICE_DEMUX); 150 if (descriptor != null) { 151 return descriptor.detachFd(); 152 } 153 } 154 return -1; 155 } 156 157 @Override 158 protected int openDvbDvrFd() { 159 if (mDvbDeviceInfo != null) { 160 ParcelFileDescriptor descriptor = mDvbDeviceAccessor.openDvbDevice( 161 mDvbDeviceInfo, DvbDeviceAccessor.DVB_DEVICE_DVR); 162 if (descriptor != null) { 163 return descriptor.detachFd(); 164 } 165 } 166 return -1; 167 } 168 169 /** 170 * Gets the number of USB tuner devices currently present. 171 */ 172 public static int getNumberOfDevices(Context context) { 173 try { 174 return (new DvbDeviceAccessor(context)).getNumOfDvbDevices(); 175 } catch (Exception e) { 176 return 0; 177 } 178 } 179} 180