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 17 package com.android.tv.tuner.tvinput; 18 19 import android.app.job.JobInfo; 20 import android.app.job.JobScheduler; 21 import android.content.ComponentName; 22 import android.content.Context; 23 import android.media.tv.TvContract; 24 import android.media.tv.TvInputService; 25 import android.util.Log; 26 27 import com.google.android.exoplayer.audio.AudioCapabilities; 28 import com.google.android.exoplayer.audio.AudioCapabilitiesReceiver; 29 import com.android.tv.TvApplication; 30 import com.android.tv.common.feature.CommonFeatures; 31 32 import java.util.Collections; 33 import java.util.Set; 34 import java.util.WeakHashMap; 35 import java.util.concurrent.TimeUnit; 36 37 /** 38 * {@link TunerTvInputService} serves TV channels coming from a tuner device. 39 */ 40 public class TunerTvInputService extends TvInputService 41 implements AudioCapabilitiesReceiver.Listener{ 42 private static final String TAG = "TunerTvInputService"; 43 private static final boolean DEBUG = false; 44 45 private static final int DVR_STORAGE_CLEANUP_JOB_ID = 100; 46 47 // WeakContainer for {@link TvInputSessionImpl} 48 private final Set<TunerSession> mTunerSessions = Collections.newSetFromMap(new WeakHashMap<>()); 49 private ChannelDataManager mChannelDataManager; 50 private AudioCapabilitiesReceiver mAudioCapabilitiesReceiver; 51 private AudioCapabilities mAudioCapabilities; 52 53 @Override onCreate()54 public void onCreate() { 55 if (!TvApplication.getSingletons(this).getTvInputManagerHelper().hasTvInputManager()) { 56 Log.wtf(TAG, "Stopping because device does not have a TvInputManager"); 57 this.stopSelf(); 58 return; 59 } 60 TvApplication.setCurrentRunningProcess(this, false); 61 super.onCreate(); 62 if (DEBUG) Log.d(TAG, "onCreate"); 63 mChannelDataManager = new ChannelDataManager(getApplicationContext()); 64 mAudioCapabilitiesReceiver = new AudioCapabilitiesReceiver(getApplicationContext(), this); 65 mAudioCapabilitiesReceiver.register(); 66 if (CommonFeatures.DVR.isEnabled(this)) { 67 JobScheduler jobScheduler = 68 (JobScheduler) getSystemService(Context.JOB_SCHEDULER_SERVICE); 69 JobInfo pendingJob = jobScheduler.getPendingJob(DVR_STORAGE_CLEANUP_JOB_ID); 70 if (pendingJob != null) { 71 // storage cleaning job is already scheduled. 72 } else { 73 JobInfo job = new JobInfo.Builder(DVR_STORAGE_CLEANUP_JOB_ID, 74 new ComponentName(this, TunerStorageCleanUpService.class)) 75 .setPersisted(true).setPeriodic(TimeUnit.DAYS.toMillis(1)).build(); 76 jobScheduler.schedule(job); 77 } 78 } 79 } 80 81 @Override onDestroy()82 public void onDestroy() { 83 if (DEBUG) Log.d(TAG, "onDestroy"); 84 super.onDestroy(); 85 mChannelDataManager.release(); 86 mAudioCapabilitiesReceiver.unregister(); 87 } 88 89 @Override onCreateRecordingSession(String inputId)90 public RecordingSession onCreateRecordingSession(String inputId) { 91 return new TunerRecordingSession(this, inputId, mChannelDataManager); 92 } 93 94 @Override onCreateSession(String inputId)95 public Session onCreateSession(String inputId) { 96 if (DEBUG) Log.d(TAG, "onCreateSession"); 97 try { 98 final TunerSession session = new TunerSession(this, mChannelDataManager); 99 mTunerSessions.add(session); 100 session.setAudioCapabilities(mAudioCapabilities); 101 session.setOverlayViewEnabled(true); 102 return session; 103 } catch (RuntimeException e) { 104 // There are no available DVB devices. 105 Log.e(TAG, "Creating a session for " + inputId + " failed.", e); 106 return null; 107 } 108 } 109 110 @Override onAudioCapabilitiesChanged(AudioCapabilities audioCapabilities)111 public void onAudioCapabilitiesChanged(AudioCapabilities audioCapabilities) { 112 mAudioCapabilities = audioCapabilities; 113 for (TunerSession session : mTunerSessions) { 114 if (!session.isReleased()) { 115 session.setAudioCapabilities(audioCapabilities); 116 } 117 } 118 } 119 getInputId(Context context)120 public static String getInputId(Context context) { 121 return TvContract.buildInputId(new ComponentName(context, TunerTvInputService.class)); 122 } 123 } 124