feat: use MediaStore pending API for video recording
This commit is contained in:
1 parent
491f075278
commit
cdaa10d952
3 files changed
+97
-17
No files matched your search
@@ -317,6 +317,8 @@ object GalleryManager {
|
||||
hasAudio = retriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_HAS_AUDIO)?.let {
|
||||
it == "yes" || it == "true" || it == "1"
|
||||
}
|
||||
} catch (e: Exception) {
|
||||
PLog.w(TAG, "Video retriever metadata unavailable for $uri: ${e.message}")
|
||||
} finally {
|
||||
retriever.release()
|
||||
}
|
||||
@@ -2310,6 +2312,7 @@ object GalleryManager {
|
||||
return@withContext null
|
||||
}
|
||||
dir.setLastModified(info.dateTaken)
|
||||
notifyPhotoLibraryChanged()
|
||||
photoId
|
||||
}
|
||||
|
||||
|
||||
@@ -4,6 +4,7 @@ import android.content.ContentValues
|
||||
import android.content.Context
|
||||
import android.net.Uri
|
||||
import android.os.Environment
|
||||
import android.os.ParcelFileDescriptor
|
||||
import android.provider.MediaStore
|
||||
import com.hinnka.mycamera.utils.PLog
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
@@ -17,6 +18,60 @@ object VideoMediaStoreWriter {
|
||||
|
||||
private const val TAG = "VideoMediaStoreWriter"
|
||||
|
||||
data class PendingVideo(
|
||||
val uri: Uri,
|
||||
val descriptor: ParcelFileDescriptor
|
||||
)
|
||||
|
||||
fun createPendingVideo(
|
||||
context: Context,
|
||||
dateTakenMs: Long = System.currentTimeMillis()
|
||||
): PendingVideo? {
|
||||
val fileName = buildFileName(dateTakenMs)
|
||||
val values = ContentValues().apply {
|
||||
put(MediaStore.MediaColumns.DISPLAY_NAME, fileName)
|
||||
put(MediaStore.MediaColumns.MIME_TYPE, "video/mp4")
|
||||
put(MediaStore.MediaColumns.RELATIVE_PATH, Environment.DIRECTORY_DCIM + "/PhotonCamera")
|
||||
put(MediaStore.MediaColumns.DATE_ADDED, dateTakenMs / 1000)
|
||||
put(MediaStore.MediaColumns.DATE_MODIFIED, dateTakenMs / 1000)
|
||||
put(MediaStore.MediaColumns.IS_PENDING, 1)
|
||||
}
|
||||
|
||||
val uri = context.contentResolver.insert(MediaStore.Video.Media.EXTERNAL_CONTENT_URI, values)
|
||||
?: return null
|
||||
return try {
|
||||
val descriptor = context.contentResolver.openFileDescriptor(uri, "rw")
|
||||
?: throw IllegalStateException("Cannot open output descriptor")
|
||||
PendingVideo(uri, descriptor)
|
||||
} catch (e: Exception) {
|
||||
PLog.e(TAG, "Failed to create pending video output", e)
|
||||
context.contentResolver.delete(uri, null, null)
|
||||
null
|
||||
}
|
||||
}
|
||||
|
||||
fun publishPendingVideo(context: Context, uri: Uri): Uri? {
|
||||
return try {
|
||||
val values = ContentValues().apply {
|
||||
put(MediaStore.MediaColumns.IS_PENDING, 0)
|
||||
put(MediaStore.MediaColumns.DATE_MODIFIED, System.currentTimeMillis() / 1000)
|
||||
}
|
||||
context.contentResolver.update(uri, values, null, null)
|
||||
uri
|
||||
} catch (e: Exception) {
|
||||
PLog.e(TAG, "Failed to publish pending video", e)
|
||||
null
|
||||
}
|
||||
}
|
||||
|
||||
fun discardPendingVideo(context: Context, uri: Uri) {
|
||||
try {
|
||||
context.contentResolver.delete(uri, null, null)
|
||||
} catch (e: Exception) {
|
||||
PLog.w(TAG, "Failed to discard pending video: ${e.message}")
|
||||
}
|
||||
}
|
||||
|
||||
suspend fun publishVideo(
|
||||
context: Context,
|
||||
sourceFile: File,
|
||||
@@ -26,9 +81,7 @@ object VideoMediaStoreWriter {
|
||||
return@withContext null
|
||||
}
|
||||
|
||||
val fileName = "PhotonCamera_${
|
||||
SimpleDateFormat("yyyyMMdd_HHmmss", Locale.US).format(Date(dateTakenMs))
|
||||
}.mp4"
|
||||
val fileName = buildFileName(dateTakenMs)
|
||||
val values = ContentValues().apply {
|
||||
put(MediaStore.MediaColumns.DISPLAY_NAME, fileName)
|
||||
put(MediaStore.MediaColumns.MIME_TYPE, "video/mp4")
|
||||
@@ -53,4 +106,10 @@ object VideoMediaStoreWriter {
|
||||
null
|
||||
}
|
||||
}
|
||||
|
||||
private fun buildFileName(dateTakenMs: Long): String {
|
||||
return "PhotonCamera_${
|
||||
SimpleDateFormat("yyyyMMdd_HHmmss", Locale.US).format(Date(dateTakenMs))
|
||||
}.mp4"
|
||||
}
|
||||
}
|
||||
@@ -29,7 +29,6 @@ import kotlinx.coroutines.cancel
|
||||
import kotlinx.coroutines.delay
|
||||
import kotlinx.coroutines.launch
|
||||
import kotlinx.coroutines.withContext
|
||||
import java.io.File
|
||||
import java.nio.ByteBuffer
|
||||
import java.util.concurrent.Executors
|
||||
|
||||
@@ -93,7 +92,8 @@ class VideoRecorder(
|
||||
|
||||
private var requestedSize = android.util.Size(1080, 1920)
|
||||
private var requestedFps = 30
|
||||
private var tempOutputFile: File? = null
|
||||
private var outputDateTakenMs: Long = 0L
|
||||
private var pendingVideoOutput: VideoMediaStoreWriter.PendingVideo? = null
|
||||
private var muxer: MediaMuxer? = null
|
||||
private var videoTrackIndex = -1
|
||||
private var audioTrackIndex = -1
|
||||
@@ -148,7 +148,7 @@ class VideoRecorder(
|
||||
requestedCodecMime = codecMime
|
||||
requestedColorConfig = colorConfig
|
||||
requestedOrientationHintDegrees = normalizeOrientationHint(orientationHintDegrees)
|
||||
tempOutputFile = File(context.cacheDir, "video_${System.currentTimeMillis()}.mp4")
|
||||
outputDateTakenMs = System.currentTimeMillis()
|
||||
finishCallback = onFinished
|
||||
resetMuxerState()
|
||||
frameSelectionStartTimestampUs = Long.MIN_VALUE
|
||||
@@ -396,8 +396,12 @@ class VideoRecorder(
|
||||
}
|
||||
|
||||
private fun createMuxer() {
|
||||
val file = tempOutputFile ?: return
|
||||
muxer = MediaMuxer(file.absolutePath, MediaMuxer.OutputFormat.MUXER_OUTPUT_MPEG_4).apply {
|
||||
val output = VideoMediaStoreWriter.createPendingVideo(
|
||||
context = context,
|
||||
dateTakenMs = outputDateTakenMs
|
||||
) ?: throw IllegalStateException("Failed to create video output")
|
||||
pendingVideoOutput = output
|
||||
muxer = MediaMuxer(output.descriptor.fileDescriptor, MediaMuxer.OutputFormat.MUXER_OUTPUT_MPEG_4).apply {
|
||||
setOrientationHint(requestedOrientationHintDegrees)
|
||||
}
|
||||
}
|
||||
@@ -765,7 +769,8 @@ class VideoRecorder(
|
||||
}
|
||||
|
||||
private suspend fun finalizeOutput(): Uri? {
|
||||
val file = tempOutputFile ?: return null
|
||||
val output = pendingVideoOutput ?: return null
|
||||
var muxerStopSucceeded = false
|
||||
synchronized(muxerLock) {
|
||||
if (!muxerStarted && videoFormat != null) {
|
||||
maybeStartMuxerLocked()
|
||||
@@ -783,6 +788,7 @@ class VideoRecorder(
|
||||
try {
|
||||
if (muxerStarted) {
|
||||
muxer?.stop()
|
||||
muxerStopSucceeded = true
|
||||
}
|
||||
} catch (e: Exception) {
|
||||
PLog.w(TAG, "Failed to stop muxer cleanly: ${e.message}")
|
||||
@@ -795,15 +801,21 @@ class VideoRecorder(
|
||||
}
|
||||
}
|
||||
|
||||
if (!file.exists() || file.length() <= 0L) {
|
||||
try {
|
||||
output.descriptor.close()
|
||||
} catch (_: Exception) {
|
||||
}
|
||||
pendingVideoOutput = null
|
||||
|
||||
if (!muxerStopSucceeded) {
|
||||
VideoMediaStoreWriter.discardPendingVideo(context, output.uri)
|
||||
return null
|
||||
}
|
||||
|
||||
val uri = VideoMediaStoreWriter.publishVideo(
|
||||
context = context,
|
||||
sourceFile = file
|
||||
)
|
||||
file.delete()
|
||||
val uri = VideoMediaStoreWriter.publishPendingVideo(context, output.uri)
|
||||
if (uri == null) {
|
||||
VideoMediaStoreWriter.discardPendingVideo(context, output.uri)
|
||||
}
|
||||
return uri
|
||||
}
|
||||
|
||||
@@ -857,8 +869,14 @@ class VideoRecorder(
|
||||
finishCallback = null
|
||||
lastSharedContext = EGL14.EGL_NO_CONTEXT
|
||||
lastSharedDisplay = EGL14.EGL_NO_DISPLAY
|
||||
tempOutputFile?.delete()
|
||||
tempOutputFile = null
|
||||
pendingVideoOutput?.let { output ->
|
||||
try {
|
||||
output.descriptor.close()
|
||||
} catch (_: Exception) {
|
||||
}
|
||||
VideoMediaStoreWriter.discardPendingVideo(context, output.uri)
|
||||
}
|
||||
pendingVideoOutput = null
|
||||
}
|
||||
|
||||
private fun resetMuxerState() {
|
||||
|
||||
Reference in new issue
Block a user