1/*
2 * Copyright (C) 2011 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
18package android.media.filterfw.samples;
19
20import android.app.Activity;
21import android.os.Bundle;
22import android.os.Environment;
23import android.view.View;
24import android.view.View.OnClickListener;
25import android.view.SurfaceView;
26import android.view.KeyEvent;
27import android.widget.Button;
28import android.filterfw.GraphEnvironment;
29import android.filterfw.core.GraphRunner;
30import android.filterpacks.videosink.MediaEncoderFilter;
31import android.graphics.Bitmap;
32import android.graphics.BitmapFactory;
33import android.content.Intent;
34
35public class CameraEffectsRecordingSample extends Activity {
36
37    private Button mRunButton;
38    private SurfaceView mCameraView;
39
40    private GraphRunner mRunner;
41    private int mCameraId = 0;
42    private String mOutFileName =  Environment.getExternalStorageDirectory().toString() +
43        "/CameraEffectsRecordingSample.mp4";
44
45    public void onCreate(Bundle savedInstanceState) {
46        super.onCreate(savedInstanceState);
47        setContentView(R.layout.main);
48        mRunButton = (Button) findViewById(R.id.runbutton);
49        mCameraView = (SurfaceView) findViewById(R.id.cameraview);
50        mRunButton.setOnClickListener(mRunButtonClick);
51
52        Intent intent = getIntent();
53        if (intent.hasExtra("OUTPUT_FILENAME")) {
54            mOutFileName = intent.getStringExtra("OUTPUT_FILENAME");
55        }
56        // Set up the references and load the filter graph
57        createGraph();
58    }
59
60    @Override
61    public boolean onKeyDown(int keyCode, KeyEvent event) {
62        switch (keyCode) {
63            case KeyEvent.KEYCODE_CAMERA:
64                mRunButton.performClick();
65                return true;
66        }
67        return super.onKeyDown(keyCode, event);
68    }
69
70    private void createGraph() {
71        Bitmap sourceBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.android);
72        GraphEnvironment graphEnvironment = new GraphEnvironment();
73        graphEnvironment.createGLEnvironment();
74        graphEnvironment.addReferences("cameraView", mCameraView);
75        graphEnvironment.addReferences("cameraId", mCameraId);
76        graphEnvironment.addReferences("outputFileName", mOutFileName);
77        int graphId = graphEnvironment.loadGraph(this, R.raw.cameraeffectsrecordingsample);
78        mRunner = graphEnvironment.getRunner(graphId, GraphEnvironment.MODE_ASYNCHRONOUS);
79    }
80
81    protected void onPause() {
82        super.onPause();
83        if (mRunner.isRunning()) {
84            mRunner.stop();
85            mRunButton.setText("Record");
86        }
87    }
88
89    private OnClickListener mRunButtonClick = new OnClickListener() {
90        @Override
91        public void onClick(View v) {
92            if (mRunner.isRunning()) {
93                mRunner.stop();
94                mRunButton.setText("Record");
95            } else {
96                mRunner.run();
97                mRunButton.setText("Stop");
98            }
99        }
100    };
101}
102