Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RecorderThread: Open output files with O_TRUNC #146

Merged
merged 1 commit into from
Oct 7, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 8 additions & 6 deletions app/src/main/java/com/chiller3/bcr/RecorderThread.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import android.media.MediaRecorder
import android.net.Uri
import android.os.Build
import android.os.ParcelFileDescriptor
import android.system.Os
import android.telecom.Call
import android.telecom.PhoneAccount
import android.util.Log
Expand All @@ -20,6 +19,7 @@ import com.chiller3.bcr.format.SampleRate
import java.io.FileInputStream
import java.io.FileOutputStream
import java.io.IOException
import java.lang.Process
import java.nio.ByteBuffer
import java.time.Duration
import java.time.Instant
Expand Down Expand Up @@ -172,7 +172,7 @@ class RecorderThread(
resultUri = outputFile.uri

try {
openFile(outputFile).use {
openFile(outputFile, true).use {
recordUntilCancelled(it)
}
} finally {
Expand Down Expand Up @@ -343,9 +343,9 @@ class RecorderThread(
val targetFile = createFileInDir(targetDir, sourceFile.name!!, sourceFile.type!!)

try {
openFile(sourceFile).use { sourcePfd ->
openFile(sourceFile, false).use { sourcePfd ->
FileInputStream(sourcePfd.fileDescriptor).use { sourceStream ->
openFile(targetFile).use { targetPfd ->
openFile(targetFile, true).use { targetPfd ->
FileOutputStream(targetPfd.fileDescriptor).use { targetStream ->
val sourceChannel = sourceStream.channel
val targetChannel = targetStream.channel
Expand Down Expand Up @@ -404,9 +404,11 @@ class RecorderThread(
*
* @throws IOException if [file] cannot be opened
*/
private fun openFile(file: DocumentFile): ParcelFileDescriptor =
context.contentResolver.openFileDescriptor(file.uri, "rw")
private fun openFile(file: DocumentFile, truncate: Boolean): ParcelFileDescriptor {
val truncParam = if (truncate) { "t" } else { "" }
return context.contentResolver.openFileDescriptor(file.uri, "rw$truncParam")
?: throw IOException("Failed to open file at ${file.uri}")
}

/**
* Record from [MediaRecorder.AudioSource.VOICE_CALL] until [cancel] is called or an audio
Expand Down