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.usbtuner; 18 19import android.content.Context; 20import android.os.ParcelFileDescriptor; 21import android.support.annotation.IntDef; 22import android.util.Log; 23import com.android.usbtuner.DvbDeviceAccessor.DvbDeviceInfoWrapper; 24 25import java.util.List; 26import java.util.SortedSet; 27import java.util.TreeSet; 28 29/** 30 * A class to handle a hardware USB tuner device. 31 */ 32public class UsbTunerHal extends TunerHal { 33 34 private static final Object sLock = new Object(); 35 // @GuardedBy("sLock") 36 private static final SortedSet<DvbDeviceInfoWrapper> sUsedDvbDevices = new TreeSet<>(); 37 38 private final DvbDeviceAccessor mDvbDeviceAccessor; 39 private DvbDeviceInfoWrapper mDvbDeviceInfo; 40 41 protected UsbTunerHal(Context context) { 42 super(context); 43 mDvbDeviceAccessor = new DvbDeviceAccessor(context); 44 } 45 46 @Override 47 protected boolean openFirstAvailable() { 48 List<DvbDeviceInfoWrapper> deviceInfoList = mDvbDeviceAccessor.getDvbDeviceList(); 49 if (deviceInfoList == null || deviceInfoList.isEmpty()) { 50 Log.e(TAG, "There's no dvb device attached"); 51 return false; 52 } 53 synchronized (sLock) { 54 for (DvbDeviceInfoWrapper deviceInfo : deviceInfoList) { 55 if (!sUsedDvbDevices.contains(deviceInfo)) { 56 if (DEBUG) Log.d(TAG, "Available device info: " + deviceInfo); 57 mDvbDeviceInfo = deviceInfo; 58 sUsedDvbDevices.add(deviceInfo); 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