Home | History | Annotate | Download | only in filterfw
      1 /*
      2  * Copyright (C) 2012 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 // Make values from a motion sensor (e.g., accelerometer) available as filter outputs.
     18 
     19 package androidx.media.filterpacks.sensors;
     20 
     21 import android.content.Context;
     22 import android.hardware.Sensor;
     23 import android.hardware.SensorEvent;
     24 import android.hardware.SensorEventListener;
     25 import android.hardware.SensorManager;
     26 
     27 import androidx.media.filterfw.Filter;
     28 import androidx.media.filterfw.FrameType;
     29 import androidx.media.filterfw.FrameValues;
     30 import androidx.media.filterfw.MffContext;
     31 import androidx.media.filterfw.OutputPort;
     32 import androidx.media.filterfw.Signature;
     33 
     34 public final class MotionSensor extends Filter implements SensorEventListener {
     35 
     36     private SensorManager mSensorManager = null;
     37     private Sensor mSensor = null;
     38 
     39     private float[] mValues = new float[3];
     40 
     41     public MotionSensor(MffContext context, String name) {
     42         super(context, name);
     43     }
     44 
     45     @Override
     46     public Signature getSignature() {
     47         return new Signature()
     48             .addOutputPort("values", Signature.PORT_REQUIRED, FrameType.array(float.class))
     49             .disallowOtherPorts();
     50     }
     51 
     52     @Override
     53     protected void onPrepare() {
     54         mSensorManager = (SensorManager)getContext().getApplicationContext()
     55                             .getSystemService(Context.SENSOR_SERVICE);
     56         mSensor = mSensorManager.getDefaultSensor(Sensor.TYPE_LINEAR_ACCELERATION);
     57         // TODO: currently, the type of sensor is hardcoded. Should be able to set the sensor
     58         //  type as filter input!
     59         mSensorManager.registerListener(this, mSensor, SensorManager.SENSOR_DELAY_UI);
     60     }
     61 
     62     @Override
     63     protected void onTearDown() {
     64         mSensorManager.unregisterListener(this);
     65     }
     66 
     67     @Override
     68     public final void onAccuracyChanged(Sensor sensor, int accuracy) {
     69         // (Do we need to do something when sensor accuracy changes?)
     70     }
     71 
     72     @Override
     73     public final void onSensorChanged(SensorEvent event) {
     74         synchronized(mValues) {
     75             mValues[0] = event.values[0];
     76             mValues[1] = event.values[1];
     77             mValues[2] = event.values[2];
     78         }
     79     }
     80 
     81     @Override
     82     protected void onProcess() {
     83         OutputPort outPort = getConnectedOutputPort("values");
     84         FrameValues outFrame = outPort.fetchAvailableFrame(null).asFrameValues();
     85         synchronized(mValues) {
     86             outFrame.setValues(mValues);
     87         }
     88         outFrame.setTimestamp(System.currentTimeMillis() * 1000000L);
     89         outPort.pushFrame(outFrame);
     90     }
     91 }
     92 
     93