Home | History | Annotate | Download | only in simplecamera
      1 /*
      2  * Copyright (C) 2013 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.filterfw.samples.simplecamera;
     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 import android.util.Log;
     27 
     28 import androidx.media.filterfw.Filter;
     29 import androidx.media.filterfw.FrameType;
     30 import androidx.media.filterfw.FrameValue;
     31 import androidx.media.filterfw.FrameValues;
     32 import androidx.media.filterfw.MffContext;
     33 import androidx.media.filterfw.OutputPort;
     34 import androidx.media.filterfw.Signature;
     35 
     36 public final class MotionSensorWTime extends Filter implements SensorEventListener {
     37 
     38     private SensorManager mSensorManager = null;
     39     private Sensor mSensor = null;
     40 
     41     private float[] mValues = new float[3];
     42     private float[][] mTemp = new float[3][3];
     43     private float[] mAvgValues = new float[3];
     44     private int mCounter = 0;
     45 
     46     public MotionSensorWTime(MffContext context, String name) {
     47         super(context, name);
     48     }
     49 
     50     @Override
     51     public Signature getSignature() {
     52         return new Signature()
     53             .addOutputPort("values", Signature.PORT_REQUIRED, FrameType.array(float.class))
     54             .addOutputPort("timestamp", Signature.PORT_OPTIONAL, FrameType.single(long.class))
     55             .disallowOtherPorts();
     56     }
     57 
     58     @Override
     59     protected void onPrepare() {
     60         mSensorManager = (SensorManager)getContext().getApplicationContext()
     61                             .getSystemService(Context.SENSOR_SERVICE);
     62         mSensor = mSensorManager.getDefaultSensor(Sensor.TYPE_LINEAR_ACCELERATION);
     63         // TODO: currently, the type of sensor is hardcoded. Should be able to set the sensor
     64         //  type as filter input!
     65         mSensorManager.registerListener(this, mSensor, SensorManager.SENSOR_DELAY_UI);
     66     }
     67 
     68     @Override
     69     protected void onTearDown() {
     70         mSensorManager.unregisterListener(this);
     71     }
     72 
     73     @Override
     74     public final void onAccuracyChanged(Sensor sensor, int accuracy) {
     75         // (Do we need to do something when sensor accuracy changes?)
     76     }
     77 
     78     @Override
     79     public final void onSensorChanged(SensorEvent event) {
     80         synchronized(mValues) {
     81             mValues[0] = event.values[0];
     82             mValues[1] = event.values[1];
     83             mValues[2] = event.values[2];
     84         }
     85     }
     86 
     87     @Override
     88     protected void onProcess() {
     89         OutputPort outPort = getConnectedOutputPort("values");
     90         FrameValues outFrame = outPort.fetchAvailableFrame(null).asFrameValues();
     91         synchronized(mValues) {
     92             if (mCounter < 3 && mCounter >= 0) {
     93                 mTemp[0][mCounter] = mValues[0];
     94                 mTemp[1][mCounter] = mValues[1];
     95                 mTemp[2][mCounter] = mValues[2];
     96             }
     97 
     98             mCounter = (mCounter + 1) % 3;
     99 
    100             mAvgValues[0] = (mTemp[0][0] + mTemp[0][1] + mTemp[0][2]) / 3;
    101             mAvgValues[1] = (mTemp[1][0] + mTemp[1][1] + mTemp[1][2]) / 3;
    102             mAvgValues[2] = (mTemp[2][0] + mTemp[2][1] + mTemp[2][2]) / 3;
    103             outFrame.setValues(mAvgValues);
    104         }
    105         outFrame.setTimestamp(System.currentTimeMillis() * 1000000L);
    106         outPort.pushFrame(outFrame);
    107 
    108         OutputPort timeOutPort = getConnectedOutputPort("timestamp");
    109         if (timeOutPort != null) {
    110             long timestamp = System.nanoTime();
    111             Log.v("MotionSensor", "Timestamp is: " + timestamp);
    112             FrameValue timeStampFrame = timeOutPort.fetchAvailableFrame(null).asFrameValue();
    113             timeStampFrame.setValue(timestamp);
    114             timeOutPort.pushFrame(timeStampFrame);
    115         }
    116     }
    117 }
    118 
    119