summaryrefslogtreecommitdiffstats
path: root/src/com/android/fmradio/FmRecorder.java
diff options
context:
space:
mode:
authorKhalid Zubair <kzubair@cyngn.com>2016-06-27 14:29:45 -0700
committerKhalid Zubair <kzubair@cyngn.com>2016-06-28 11:12:53 -0700
commit3c0c276699c9ba975e370c3c7711677b71772854 (patch)
tree2aba79805447209c0f94e23baddf60e8a5680708 /src/com/android/fmradio/FmRecorder.java
parenta7db4dc6a01e5f756257907e3000538134497b7c (diff)
downloadandroid_packages_apps_FMRadio-3c0c276699c9ba975e370c3c7711677b71772854.tar.gz
android_packages_apps_FMRadio-3c0c276699c9ba975e370c3c7711677b71772854.tar.bz2
android_packages_apps_FMRadio-3c0c276699c9ba975e370c3c7711677b71772854.zip
Handle recording without using MediaRecorder
MediaRecorder creates a new AudioRecord for the tuner internally and causes two audio input devices to be in-use during recording. Replace MediaRecorder with a custom recorder that gets fed by the same AudioRecord instance used for playback when SW rendering is in affect. This change helps workaround some bugs in the Audio HAL during fm recording (two tuner input devices) and concurrent mic recording. Force SW rendering when recording starts so that the recorder works and attempt to start the audio patch when recording ends. In onAudioPatchListUpdate there is no need to call initAudioRecordSink() before calling startRender() because startRender() will call initAudioRecordSink(). CYNGNOS-2819, KIPPER-687, FEIJ-1372 Change-Id: Iddd9f325892ca4482c3977dcadc627352e6f5bb2
Diffstat (limited to 'src/com/android/fmradio/FmRecorder.java')
-rw-r--r--src/com/android/fmradio/FmRecorder.java92
1 files changed, 35 insertions, 57 deletions
diff --git a/src/com/android/fmradio/FmRecorder.java b/src/com/android/fmradio/FmRecorder.java
index 18a9d00..389fbc2 100644
--- a/src/com/android/fmradio/FmRecorder.java
+++ b/src/com/android/fmradio/FmRecorder.java
@@ -21,8 +21,8 @@ import android.content.ContentUris;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
+import android.media.AudioFormat;
import android.media.MediaPlayer;
-import android.media.MediaRecorder;
import android.media.MediaScannerConnection;
import android.net.Uri;
import android.os.Environment;
@@ -41,7 +41,7 @@ import java.util.Locale;
* This class provider interface to recording, stop recording, save recording
* file, play recording file
*/
-public class FmRecorder implements MediaRecorder.OnErrorListener, MediaRecorder.OnInfoListener {
+public class FmRecorder implements AudioRecorder.Callback {
private static final String TAG = "FmRecorder";
// file prefix
public static final String RECORDING_FILE_PREFIX = "FM";
@@ -82,7 +82,15 @@ public class FmRecorder implements MediaRecorder.OnErrorListener, MediaRecorder.
// listener use for notify service the record state or error state
private OnRecorderStateChangedListener mStateListener = null;
// recorder use for record file
- private MediaRecorder mRecorder = null;
+ private AudioRecorder mRecorder = null;
+ // take this lock before manipulating mRecorder
+ private final Object mRecorderLock = new Object();
+ // format of input audio
+ private AudioFormat mInputFormat = null;
+
+ FmRecorder(AudioFormat in) {
+ mInputFormat = in;
+ }
/**
* Start recording the voice of FM, also check the pre-conditions, if not
@@ -146,31 +154,20 @@ public class FmRecorder implements MediaRecorder.OnErrorListener, MediaRecorder.
}
// set record parameter and start recording
try {
- mRecorder = new MediaRecorder();
- mRecorder.setOnErrorListener(this);
- mRecorder.setOnInfoListener(this);
- mRecorder.setAudioSource(MediaRecorder.AudioSource.RADIO_TUNER);
- mRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
- mRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AAC);
- final int samplingRate = 44100;
- mRecorder.setAudioSamplingRate(samplingRate);
- final int bitRate = 128000;
- mRecorder.setAudioEncodingBitRate(bitRate);
- final int audiochannels = 2;
- mRecorder.setAudioChannels(audiochannels);
- mRecorder.setOutputFile(mRecordFile.getAbsolutePath());
- mRecorder.prepare();
- mRecordStartTime = SystemClock.elapsedRealtime();
- mRecorder.start();
- mIsRecordingFileSaved = false;
+ synchronized(mRecorderLock) {
+ if (mRecorder != null) {
+ mRecorder.stopRecording();
+ }
+
+ mRecorder = new AudioRecorder(mInputFormat, mRecordFile);
+ mRecorder.setCallback(this);
+ mRecordStartTime = SystemClock.elapsedRealtime();
+ mIsRecordingFileSaved = false;
+ }
} catch (IllegalStateException e) {
Log.e(TAG, "startRecording, IllegalStateException while starting recording!", e);
setError(ERROR_RECORDER_INTERNAL);
return;
- } catch (IOException e) {
- Log.e(TAG, "startRecording, IOException while starting recording!", e);
- setError(ERROR_RECORDER_INTERNAL);
- return;
}
setState(STATE_RECORDING);
}
@@ -297,17 +294,9 @@ public class FmRecorder implements MediaRecorder.OnErrorListener, MediaRecorder.
void onRecorderError(int error);
}
- /**
- * When recorder occur error, release player, notify error message, and
- * update FM recorder state to idle
- *
- * @param mr The current recorder
- * @param what The error message type
- * @param extra The error message extra
- */
@Override
- public void onError(MediaRecorder mr, int what, int extra) {
- Log.e(TAG, "onError, what = " + what + ", extra = " + extra);
+ public void onError(int what) {
+ Log.e(TAG, "onError, what = " + what);
stopRecorder();
setError(ERROR_RECORDER_INTERNAL);
if (STATE_RECORDING == mInternalState) {
@@ -315,23 +304,11 @@ public class FmRecorder implements MediaRecorder.OnErrorListener, MediaRecorder.
}
}
- @Override
- public void onInfo(MediaRecorder mr, int what, int extra) {
- Log.d(TAG, "onInfo: what=" + what + ", extra=" + extra);
- if (what == MediaRecorder.MEDIA_RECORDER_INFO_MAX_DURATION_REACHED ||
- what == MediaRecorder.MEDIA_RECORDER_INFO_MAX_FILESIZE_REACHED) {
- onError(mr, what, extra);
- }
- }
-
/**
* Reset FM recorder
*/
public void resetRecorder() {
- if (mRecorder != null) {
- mRecorder.release();
- mRecorder = null;
- }
+ stopRecorder();
mRecordFile = null;
mRecordStartTime = 0;
mRecordTime = 0;
@@ -523,17 +500,18 @@ public class FmRecorder implements MediaRecorder.OnErrorListener, MediaRecorder.
}
private void stopRecorder() {
- synchronized (this) {
+ synchronized (mRecorderLock) {
if (mRecorder != null) {
- try {
- mRecorder.stop();
- } catch (IllegalStateException ex) {
- Log.e(TAG, "stopRecorder, IllegalStateException ocurr " + ex);
- setError(ERROR_RECORDER_INTERNAL);
- } finally {
- mRecorder.release();
- mRecorder = null;
- }
+ mRecorder.stopRecording();
+ mRecorder = null;
+ }
+ }
+ }
+
+ public void encode(byte bytes[]) {
+ synchronized (mRecorderLock) {
+ if (mRecorder != null) {
+ mRecorder.encode(bytes);
}
}
}