Compare commits
4 Commits
loewy/frag
...
de5e15b347
| Author | SHA1 | Date | |
|---|---|---|---|
| de5e15b347 | |||
| c0b352dc1d | |||
| a307f9b7e8 | |||
| b79f876114 |
@@ -428,19 +428,21 @@ class CameraSession(private val context: Context, private val cameraManager: Cam
|
|||||||
|
|
||||||
// Get actual device rotation from WindowManager since the React Native orientation hook
|
// Get actual device rotation from WindowManager since the React Native orientation hook
|
||||||
// doesn't update when rotating between landscape-left and landscape-right on Android.
|
// doesn't update when rotating between landscape-left and landscape-right on Android.
|
||||||
// Map device rotation to the correct orientationHint for video recording:
|
// Map device rotation to the correct orientation for video recording.
|
||||||
// - Counter-clockwise (ROTATION_90) → 270° hint
|
// Surface.ROTATION_90 = device rotated 90° CCW = phone top on LEFT = LANDSCAPE_LEFT
|
||||||
// - Clockwise (ROTATION_270) → 90° hint
|
// Surface.ROTATION_270 = device rotated 90° CW = phone top on RIGHT = LANDSCAPE_RIGHT
|
||||||
val windowManager = context.getSystemService(Context.WINDOW_SERVICE) as WindowManager
|
val windowManager = context.getSystemService(Context.WINDOW_SERVICE) as WindowManager
|
||||||
val deviceRotation = windowManager.defaultDisplay.rotation
|
val deviceRotation = windowManager.defaultDisplay.rotation
|
||||||
val recordingOrientation = when (deviceRotation) {
|
val recordingOrientation = when (deviceRotation) {
|
||||||
Surface.ROTATION_0 -> Orientation.PORTRAIT
|
Surface.ROTATION_0 -> Orientation.PORTRAIT
|
||||||
Surface.ROTATION_90 -> Orientation.LANDSCAPE_RIGHT
|
Surface.ROTATION_90 -> Orientation.LANDSCAPE_LEFT
|
||||||
Surface.ROTATION_180 -> Orientation.PORTRAIT_UPSIDE_DOWN
|
Surface.ROTATION_180 -> Orientation.PORTRAIT_UPSIDE_DOWN
|
||||||
Surface.ROTATION_270 -> Orientation.LANDSCAPE_LEFT
|
Surface.ROTATION_270 -> Orientation.LANDSCAPE_RIGHT
|
||||||
else -> Orientation.PORTRAIT
|
else -> Orientation.PORTRAIT
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Log.i(TAG, "startRecording: orientation=${recordingOrientation.toDegrees()}° (deviceRotation=$deviceRotation)")
|
||||||
|
|
||||||
val recording = RecordingSession(
|
val recording = RecordingSession(
|
||||||
context,
|
context,
|
||||||
cameraId,
|
cameraId,
|
||||||
@@ -448,7 +450,7 @@ class CameraSession(private val context: Context, private val cameraManager: Cam
|
|||||||
enableAudio,
|
enableAudio,
|
||||||
fps,
|
fps,
|
||||||
videoOutput.enableHdr,
|
videoOutput.enableHdr,
|
||||||
orientation,
|
recordingOrientation,
|
||||||
options,
|
options,
|
||||||
filePath,
|
filePath,
|
||||||
callback,
|
callback,
|
||||||
|
|||||||
@@ -39,17 +39,18 @@ class FragmentedRecordingManager(
|
|||||||
segmentDurationSeconds: Int = DEFAULT_SEGMENT_DURATION_SECONDS
|
segmentDurationSeconds: Int = DEFAULT_SEGMENT_DURATION_SECONDS
|
||||||
): FragmentedRecordingManager {
|
): FragmentedRecordingManager {
|
||||||
val mimeType = options.videoCodec.toMimeType()
|
val mimeType = options.videoCodec.toMimeType()
|
||||||
val cameraOrientationDegrees = cameraOrientation.toDegrees()
|
// Use cameraOrientation (from WindowManager) for rotation metadata
|
||||||
val recordingOrientationDegrees = (options.orientation ?: Orientation.PORTRAIT).toDegrees()
|
// The options.orientation from JavaScript is unreliable on Android when rotating between landscape modes
|
||||||
|
val orientationDegrees = cameraOrientation.toDegrees()
|
||||||
|
|
||||||
// Use size dimensions directly - the encoder output format will have the actual dimensions
|
// Swap dimensions based on camera orientation, same as ChunkedRecordingManager
|
||||||
// Don't swap based on orientation here; the camera pipeline handles that
|
val (width, height) = if (cameraOrientation.isLandscape()) {
|
||||||
val width = size.width
|
size.height to size.width
|
||||||
val height = size.height
|
} else {
|
||||||
|
size.width to size.height
|
||||||
|
}
|
||||||
|
|
||||||
Log.d(TAG, "Input size: ${size.width}x${size.height}, " +
|
Log.d(TAG, "Recording: ${width}x${height}, orientation=$orientationDegrees°")
|
||||||
"cameraOrientation: $cameraOrientation ($cameraOrientationDegrees°), " +
|
|
||||||
"recordingOrientation: $recordingOrientationDegrees°")
|
|
||||||
|
|
||||||
val format = MediaFormat.createVideoFormat(mimeType, width, height)
|
val format = MediaFormat.createVideoFormat(mimeType, width, height)
|
||||||
val codec = MediaCodec.createEncoderByType(mimeType)
|
val codec = MediaCodec.createEncoderByType(mimeType)
|
||||||
@@ -59,16 +60,18 @@ class FragmentedRecordingManager(
|
|||||||
MediaCodecInfo.CodecCapabilities.COLOR_FormatSurface
|
MediaCodecInfo.CodecCapabilities.COLOR_FormatSurface
|
||||||
)
|
)
|
||||||
|
|
||||||
val effectiveFps = fps ?: 30
|
// Use 30fps as conservative default since many Android devices can't sustain
|
||||||
|
// higher frame rates at high resolutions. This affects:
|
||||||
|
// - Encoder: bitrate allocation and I-frame interval calculation
|
||||||
|
// - HlsMuxer: timescale for accurate sample durations
|
||||||
|
// The actual frame timing comes from camera timestamps regardless of this setting.
|
||||||
|
val effectiveFps = 30
|
||||||
format.setInteger(MediaFormat.KEY_FRAME_RATE, effectiveFps)
|
format.setInteger(MediaFormat.KEY_FRAME_RATE, effectiveFps)
|
||||||
format.setInteger(MediaFormat.KEY_I_FRAME_INTERVAL, segmentDurationSeconds)
|
format.setInteger(MediaFormat.KEY_I_FRAME_INTERVAL, segmentDurationSeconds)
|
||||||
format.setInteger(MediaFormat.KEY_BIT_RATE, bitRate)
|
format.setInteger(MediaFormat.KEY_BIT_RATE, bitRate)
|
||||||
|
|
||||||
Log.d(TAG, "Video Format: $format, orientation: $recordingOrientationDegrees")
|
|
||||||
|
|
||||||
codec.configure(format, null, null, MediaCodec.CONFIGURE_FLAG_ENCODE)
|
codec.configure(format, null, null, MediaCodec.CONFIGURE_FLAG_ENCODE)
|
||||||
|
|
||||||
// Create muxer with callbacks and orientation
|
|
||||||
val muxer = HlsMuxer(
|
val muxer = HlsMuxer(
|
||||||
outputDirectory = outputDirectory,
|
outputDirectory = outputDirectory,
|
||||||
callback = object : HlsMuxer.Callback {
|
callback = object : HlsMuxer.Callback {
|
||||||
@@ -80,12 +83,11 @@ class FragmentedRecordingManager(
|
|||||||
callbacks.onVideoChunkReady(file, index, durationUs)
|
callbacks.onVideoChunkReady(file, index, durationUs)
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
orientationDegrees = recordingOrientationDegrees
|
orientationDegrees = orientationDegrees,
|
||||||
|
fps = effectiveFps
|
||||||
)
|
)
|
||||||
muxer.setSegmentDuration(segmentDurationSeconds * 1_000_000L)
|
muxer.setSegmentDuration(segmentDurationSeconds * 1_000_000L)
|
||||||
|
|
||||||
Log.d(TAG, "Created HlsMuxer with orientation: $recordingOrientationDegrees degrees")
|
|
||||||
|
|
||||||
return FragmentedRecordingManager(codec, muxer)
|
return FragmentedRecordingManager(codec, muxer)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -27,7 +27,8 @@ import java.nio.ByteBuffer
|
|||||||
class HlsMuxer(
|
class HlsMuxer(
|
||||||
private val outputDirectory: File,
|
private val outputDirectory: File,
|
||||||
private val callback: Callback,
|
private val callback: Callback,
|
||||||
private val orientationDegrees: Int = 0
|
private val orientationDegrees: Int = 0,
|
||||||
|
private val fps: Int = 30
|
||||||
) {
|
) {
|
||||||
companion object {
|
companion object {
|
||||||
private const val TAG = "HlsMuxer"
|
private const val TAG = "HlsMuxer"
|
||||||
@@ -41,7 +42,7 @@ class HlsMuxer(
|
|||||||
|
|
||||||
// Configuration
|
// Configuration
|
||||||
private var targetSegmentDurationUs: Long = DEFAULT_SEGMENT_DURATION_US
|
private var targetSegmentDurationUs: Long = DEFAULT_SEGMENT_DURATION_US
|
||||||
private var timescale: Int = 30000 // Default, updated from format
|
private var timescale: Int = 30000 // Default, updated in addTrack() to fps * 1000
|
||||||
|
|
||||||
// State
|
// State
|
||||||
private var state = State.UNINITIALIZED
|
private var state = State.UNINITIALIZED
|
||||||
@@ -54,6 +55,14 @@ class HlsMuxer(
|
|||||||
private var segmentStartTimeUs = -1L
|
private var segmentStartTimeUs = -1L
|
||||||
private var lastPresentationTimeUs = 0L
|
private var lastPresentationTimeUs = 0L
|
||||||
|
|
||||||
|
// Timestamp normalization - MediaCodec timestamps are device uptime, not starting from 0
|
||||||
|
private var firstPresentationTimeUs = -1L
|
||||||
|
|
||||||
|
// Actual fps detection from frame timestamps
|
||||||
|
private var detectedFps: Int? = null
|
||||||
|
private var fpsDetectionSamples = mutableListOf<Long>()
|
||||||
|
private val FPS_DETECTION_SAMPLE_COUNT = 30
|
||||||
|
|
||||||
private enum class State {
|
private enum class State {
|
||||||
UNINITIALIZED,
|
UNINITIALIZED,
|
||||||
INITIALIZED,
|
INITIALIZED,
|
||||||
@@ -69,6 +78,29 @@ class HlsMuxer(
|
|||||||
val isKeyFrame: Boolean
|
val isKeyFrame: Boolean
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// ==================== Timestamp Normalization ====================
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Normalizes a presentation timestamp to start from 0.
|
||||||
|
* The first timestamp received becomes time 0, and all subsequent
|
||||||
|
* timestamps are relative to that.
|
||||||
|
*
|
||||||
|
* This is necessary because MediaCodec timestamps are based on device uptime,
|
||||||
|
* not starting from 0. HLS players expect timestamps to start at or near 0.
|
||||||
|
*/
|
||||||
|
private fun normalizeTimestamp(rawPresentationTimeUs: Long): Long {
|
||||||
|
if (firstPresentationTimeUs < 0) {
|
||||||
|
firstPresentationTimeUs = rawPresentationTimeUs
|
||||||
|
Log.d(TAG, "First timestamp captured: ${rawPresentationTimeUs}us (${rawPresentationTimeUs / 1_000_000.0}s), normalizing to 0")
|
||||||
|
}
|
||||||
|
val normalized = rawPresentationTimeUs - firstPresentationTimeUs
|
||||||
|
// Log first few normalizations to debug
|
||||||
|
if (normalized < 1_000_000) { // First second
|
||||||
|
Log.d(TAG, "Timestamp: raw=${rawPresentationTimeUs}us -> normalized=${normalized}us")
|
||||||
|
}
|
||||||
|
return normalized
|
||||||
|
}
|
||||||
|
|
||||||
// ==================== Annex-B to AVCC Conversion ====================
|
// ==================== Annex-B to AVCC Conversion ====================
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -201,13 +233,11 @@ class HlsMuxer(
|
|||||||
|
|
||||||
trackFormat = format
|
trackFormat = format
|
||||||
|
|
||||||
// Extract timescale from frame rate
|
// Use fps * 1000 as timescale for good precision (1000 timescale units per frame).
|
||||||
val fps = try {
|
// This ensures accurate sample durations without integer truncation issues.
|
||||||
format.getInteger(MediaFormat.KEY_FRAME_RATE)
|
// Note: ffprobe may report r_frame_rate based on timescale, so the backend
|
||||||
} catch (e: Exception) {
|
// should use the explicit framesPerSecond from the API mutation, not ffprobe.
|
||||||
30
|
timescale = fps * 1000
|
||||||
}
|
|
||||||
timescale = fps * 1000 // Use fps * 1000 for good precision
|
|
||||||
|
|
||||||
state = State.INITIALIZED
|
state = State.INITIALIZED
|
||||||
|
|
||||||
@@ -215,7 +245,7 @@ class HlsMuxer(
|
|||||||
val formatHeight = try { format.getInteger(MediaFormat.KEY_HEIGHT) } catch (e: Exception) { -1 }
|
val formatHeight = try { format.getInteger(MediaFormat.KEY_HEIGHT) } catch (e: Exception) { -1 }
|
||||||
Log.d(TAG, "Added track: ${format.getString(MediaFormat.KEY_MIME)}, " +
|
Log.d(TAG, "Added track: ${format.getString(MediaFormat.KEY_MIME)}, " +
|
||||||
"encoder output: ${formatWidth}x${formatHeight}, " +
|
"encoder output: ${formatWidth}x${formatHeight}, " +
|
||||||
"timescale=$timescale, orientation=$orientationDegrees°")
|
"fps=$fps, timescale=$timescale, orientation=$orientationDegrees°")
|
||||||
|
|
||||||
return 0 // Single track, index 0
|
return 0 // Single track, index 0
|
||||||
}
|
}
|
||||||
@@ -227,16 +257,30 @@ class HlsMuxer(
|
|||||||
check(state == State.INITIALIZED) { "Must call addTrack() before start()" }
|
check(state == State.INITIALIZED) { "Must call addTrack() before start()" }
|
||||||
val format = trackFormat ?: throw IllegalStateException("No track format")
|
val format = trackFormat ?: throw IllegalStateException("No track format")
|
||||||
|
|
||||||
// Create output directory if needed
|
// Create output directory if needed, with proper error handling
|
||||||
if (!outputDirectory.exists()) {
|
if (!outputDirectory.exists()) {
|
||||||
outputDirectory.mkdirs()
|
val created = outputDirectory.mkdirs()
|
||||||
|
if (!created && !outputDirectory.exists()) {
|
||||||
|
throw IllegalStateException(
|
||||||
|
"Failed to create output directory: ${outputDirectory.absolutePath}. " +
|
||||||
|
"Parent exists: ${outputDirectory.parentFile?.exists()}, " +
|
||||||
|
"Parent path: ${outputDirectory.parentFile?.absolutePath}"
|
||||||
|
)
|
||||||
|
}
|
||||||
|
Log.d(TAG, "Created output directory: ${outputDirectory.absolutePath}")
|
||||||
}
|
}
|
||||||
|
|
||||||
// Write init segment
|
// Write init segment
|
||||||
val initBytes = buildInitSegment(format)
|
val initBytes = buildInitSegment(format)
|
||||||
val initFile = File(outputDirectory, "init.mp4")
|
val initFile = File(outputDirectory, "init.mp4")
|
||||||
FileOutputStream(initFile).use { it.write(initBytes) }
|
FileOutputStream(initFile).use { it.write(initBytes) }
|
||||||
|
|
||||||
|
// Log frame rate metadata for debugging
|
||||||
|
val defaultSampleDuration = timescale / fps
|
||||||
Log.d(TAG, "Created init segment: ${initFile.absolutePath} (${initBytes.size} bytes)")
|
Log.d(TAG, "Created init segment: ${initFile.absolutePath} (${initBytes.size} bytes)")
|
||||||
|
Log.d(TAG, "Frame rate metadata: timescale=$timescale, fps=$fps, " +
|
||||||
|
"default_sample_duration=$defaultSampleDuration (ffprobe should calculate ${timescale}/${defaultSampleDuration}=${fps}fps)")
|
||||||
|
|
||||||
callback.onInitSegmentReady(initFile)
|
callback.onInitSegmentReady(initFile)
|
||||||
|
|
||||||
state = State.STARTED
|
state = State.STARTED
|
||||||
@@ -259,13 +303,40 @@ class HlsMuxer(
|
|||||||
}
|
}
|
||||||
|
|
||||||
val isKeyFrame = (bufferInfo.flags and MediaCodec.BUFFER_FLAG_KEY_FRAME) != 0
|
val isKeyFrame = (bufferInfo.flags and MediaCodec.BUFFER_FLAG_KEY_FRAME) != 0
|
||||||
val presentationTimeUs = bufferInfo.presentationTimeUs
|
// Normalize timestamp to start from 0 (MediaCodec uses device uptime)
|
||||||
|
val presentationTimeUs = normalizeTimestamp(bufferInfo.presentationTimeUs)
|
||||||
|
|
||||||
|
// Detect actual fps from first N samples
|
||||||
|
if (detectedFps == null) {
|
||||||
|
fpsDetectionSamples.add(presentationTimeUs)
|
||||||
|
if (fpsDetectionSamples.size >= FPS_DETECTION_SAMPLE_COUNT) {
|
||||||
|
val elapsed = fpsDetectionSamples.last() - fpsDetectionSamples.first()
|
||||||
|
if (elapsed > 0) {
|
||||||
|
val actualFps = ((FPS_DETECTION_SAMPLE_COUNT - 1) * 1_000_000.0 / elapsed).toInt()
|
||||||
|
detectedFps = actualFps
|
||||||
|
if (kotlin.math.abs(actualFps - fps) > 5) {
|
||||||
|
Log.w(TAG, "Actual fps ($actualFps) differs significantly from configured fps ($fps)! " +
|
||||||
|
"This may cause processing issues if backend uses configured fps.")
|
||||||
|
} else {
|
||||||
|
Log.d(TAG, "Detected actual fps: $actualFps (configured: $fps)")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
fpsDetectionSamples.clear() // Free memory
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Initialize segment start time
|
// Initialize segment start time
|
||||||
if (segmentStartTimeUs < 0) {
|
if (segmentStartTimeUs < 0) {
|
||||||
segmentStartTimeUs = presentationTimeUs
|
segmentStartTimeUs = presentationTimeUs
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Update duration of previous sample BEFORE finalization check
|
||||||
|
// This ensures the last sample has correct duration when segment is finalized
|
||||||
|
if (pendingSamples.isNotEmpty()) {
|
||||||
|
val lastSample = pendingSamples.last()
|
||||||
|
lastSample.durationUs = presentationTimeUs - lastSample.presentationTimeUs
|
||||||
|
}
|
||||||
|
|
||||||
// Check if we should finalize current segment (at keyframe boundaries)
|
// Check if we should finalize current segment (at keyframe boundaries)
|
||||||
if (isKeyFrame && pendingSamples.isNotEmpty()) {
|
if (isKeyFrame && pendingSamples.isNotEmpty()) {
|
||||||
val segmentDurationUs = presentationTimeUs - segmentStartTimeUs
|
val segmentDurationUs = presentationTimeUs - segmentStartTimeUs
|
||||||
@@ -284,12 +355,6 @@ class HlsMuxer(
|
|||||||
// Convert Annex-B (start codes) to AVCC (length prefixes)
|
// Convert Annex-B (start codes) to AVCC (length prefixes)
|
||||||
val data = convertAnnexBToAvcc(rawData)
|
val data = convertAnnexBToAvcc(rawData)
|
||||||
|
|
||||||
// Update duration of previous sample
|
|
||||||
if (pendingSamples.isNotEmpty()) {
|
|
||||||
val lastSample = pendingSamples.last()
|
|
||||||
lastSample.durationUs = presentationTimeUs - lastSample.presentationTimeUs
|
|
||||||
}
|
|
||||||
|
|
||||||
// Estimate duration (will be corrected by next sample)
|
// Estimate duration (will be corrected by next sample)
|
||||||
val estimatedDurationUs = if (lastPresentationTimeUs > 0) {
|
val estimatedDurationUs = if (lastPresentationTimeUs > 0) {
|
||||||
presentationTimeUs - lastPresentationTimeUs
|
presentationTimeUs - lastPresentationTimeUs
|
||||||
@@ -351,6 +416,7 @@ class HlsMuxer(
|
|||||||
val durationUs = (lastSample.presentationTimeUs - firstPts) + lastSample.durationUs
|
val durationUs = (lastSample.presentationTimeUs - firstPts) + lastSample.durationUs
|
||||||
|
|
||||||
Log.d(TAG, "Created segment $segmentIndex: samples=${pendingSamples.size}, " +
|
Log.d(TAG, "Created segment $segmentIndex: samples=${pendingSamples.size}, " +
|
||||||
|
"baseDecodeTime=${baseDecodeTimeUs}us (${baseDecodeTimeUs / 1_000_000.0}s), " +
|
||||||
"duration=${durationUs / 1000}ms, size=${fragmentBytes.size} bytes")
|
"duration=${durationUs / 1000}ms, size=${fragmentBytes.size} bytes")
|
||||||
|
|
||||||
callback.onMediaSegmentReady(segmentFile, segmentIndex, durationUs)
|
callback.onMediaSegmentReady(segmentFile, segmentIndex, durationUs)
|
||||||
@@ -478,31 +544,80 @@ class HlsMuxer(
|
|||||||
dos.writeShort(0) // volume (0 for video)
|
dos.writeShort(0) // volume (0 for video)
|
||||||
dos.writeShort(0) // reserved
|
dos.writeShort(0) // reserved
|
||||||
|
|
||||||
// Rotation matrix - use identity and rely on correct dimensions from encoder
|
// Rotation matrix based on orientationDegrees
|
||||||
// The encoder output format already has the correct dimensions for the content
|
writeRotationMatrix(dos, width, height)
|
||||||
writeRotationMatrix(dos)
|
|
||||||
|
|
||||||
// Use dimensions as-is from encoder output format
|
// For 90° and 270° rotations, the display dimensions are swapped
|
||||||
dos.writeInt(width shl 16) // width (16.16 fixed point)
|
// The tkhd width/height represent the final display size after rotation
|
||||||
dos.writeInt(height shl 16) // height (16.16 fixed point)
|
val (displayWidth, displayHeight) = when (orientationDegrees) {
|
||||||
|
90, 270 -> Pair(height, width)
|
||||||
|
else -> Pair(width, height)
|
||||||
|
}
|
||||||
|
dos.writeInt(displayWidth shl 16) // width (16.16 fixed point)
|
||||||
|
dos.writeInt(displayHeight shl 16) // height (16.16 fixed point)
|
||||||
|
|
||||||
Log.d(TAG, "tkhd: ${width}x${height}, rotation=$orientationDegrees")
|
Log.d(TAG, "tkhd: encoder=${width}x${height}, display=${displayWidth}x${displayHeight}, rotation=$orientationDegrees")
|
||||||
|
|
||||||
return wrapBox("tkhd", output.toByteArray())
|
return wrapBox("tkhd", output.toByteArray())
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Writes the 3x3 transformation matrix for video rotation.
|
* Writes the 3x3 transformation matrix for video rotation.
|
||||||
* Uses simple rotation values - the encoder already outputs correctly oriented frames.
|
* The matrix is applied to rotate the video content for correct display.
|
||||||
|
*
|
||||||
|
* Matrix format in tkhd box (all values in fixed-point):
|
||||||
|
* | a b u | where a,b,c,d are 16.16 fixed-point
|
||||||
|
* | c d v | and u,v are 2.30 fixed-point (always 0)
|
||||||
|
* | x y w | x,y are 16.16, w is 2.30 (always 1.0)
|
||||||
|
*
|
||||||
|
* For rotation by θ: a=cos(θ), b=sin(θ), c=-sin(θ), d=cos(θ)
|
||||||
|
* Translation (x,y) keeps the rotated video in the visible area.
|
||||||
*/
|
*/
|
||||||
private fun writeRotationMatrix(dos: DataOutputStream) {
|
private fun writeRotationMatrix(dos: DataOutputStream, width: Int, height: Int) {
|
||||||
// Fixed-point constants
|
// Fixed-point constants
|
||||||
val one = 0x00010000 // 1.0 in 16.16
|
val one = 0x00010000 // 1.0 in 16.16
|
||||||
|
val negOne = -0x00010000 // -1.0 in 16.16 (will be written as unsigned)
|
||||||
val w = 0x40000000 // 1.0 in 2.30
|
val w = 0x40000000 // 1.0 in 2.30
|
||||||
|
|
||||||
// Identity matrix - no transformation
|
when (orientationDegrees) {
|
||||||
// Most HLS players handle rotation via the dimensions themselves
|
90 -> {
|
||||||
// or we can add rotation metadata separately if needed
|
// 90° rotation: x' = y, y' = -x + width
|
||||||
|
dos.writeInt(0) // a = 0
|
||||||
|
dos.writeInt(negOne) // b = -1
|
||||||
|
dos.writeInt(0) // u = 0
|
||||||
|
dos.writeInt(one) // c = 1
|
||||||
|
dos.writeInt(0) // d = 0
|
||||||
|
dos.writeInt(0) // v = 0
|
||||||
|
dos.writeInt(0) // x = 0
|
||||||
|
dos.writeInt(width shl 16) // y = width (translation)
|
||||||
|
dos.writeInt(w) // w = 1
|
||||||
|
}
|
||||||
|
180 -> {
|
||||||
|
// 180° rotation
|
||||||
|
dos.writeInt(negOne) // a = -1
|
||||||
|
dos.writeInt(0) // b = 0
|
||||||
|
dos.writeInt(0) // u = 0
|
||||||
|
dos.writeInt(0) // c = 0
|
||||||
|
dos.writeInt(negOne) // d = -1
|
||||||
|
dos.writeInt(0) // v = 0
|
||||||
|
dos.writeInt(width shl 16) // x = width (translation)
|
||||||
|
dos.writeInt(height shl 16) // y = height (translation)
|
||||||
|
dos.writeInt(w) // w = 1
|
||||||
|
}
|
||||||
|
270 -> {
|
||||||
|
// 270° rotation: x' = -y + height, y' = x
|
||||||
|
dos.writeInt(0) // a = 0
|
||||||
|
dos.writeInt(one) // b = 1
|
||||||
|
dos.writeInt(0) // u = 0
|
||||||
|
dos.writeInt(negOne) // c = -1
|
||||||
|
dos.writeInt(0) // d = 0
|
||||||
|
dos.writeInt(0) // v = 0
|
||||||
|
dos.writeInt(height shl 16) // x = height (translation)
|
||||||
|
dos.writeInt(0) // y = 0
|
||||||
|
dos.writeInt(w) // w = 1
|
||||||
|
}
|
||||||
|
else -> {
|
||||||
|
// 0° or unknown: identity matrix
|
||||||
dos.writeInt(one) // a = 1
|
dos.writeInt(one) // a = 1
|
||||||
dos.writeInt(0) // b = 0
|
dos.writeInt(0) // b = 0
|
||||||
dos.writeInt(0) // u = 0
|
dos.writeInt(0) // u = 0
|
||||||
@@ -513,6 +628,8 @@ class HlsMuxer(
|
|||||||
dos.writeInt(0) // y = 0
|
dos.writeInt(0) // y = 0
|
||||||
dos.writeInt(w) // w = 1
|
dos.writeInt(w) // w = 1
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private fun buildMdiaBox(width: Int, height: Int, sps: ByteArray, pps: ByteArray): ByteArray {
|
private fun buildMdiaBox(width: Int, height: Int, sps: ByteArray, pps: ByteArray): ByteArray {
|
||||||
val content = ByteArrayOutputStream()
|
val content = ByteArrayOutputStream()
|
||||||
@@ -598,7 +715,7 @@ class HlsMuxer(
|
|||||||
private fun buildStblBox(width: Int, height: Int, sps: ByteArray, pps: ByteArray): ByteArray {
|
private fun buildStblBox(width: Int, height: Int, sps: ByteArray, pps: ByteArray): ByteArray {
|
||||||
val content = ByteArrayOutputStream()
|
val content = ByteArrayOutputStream()
|
||||||
content.write(buildStsdBox(width, height, sps, pps))
|
content.write(buildStsdBox(width, height, sps, pps))
|
||||||
content.write(buildEmptySttsBox())
|
content.write(buildSttsBox()) // Contains default timing for ffprobe frame rate detection
|
||||||
content.write(buildEmptyStscBox())
|
content.write(buildEmptyStscBox())
|
||||||
content.write(buildEmptyStszBox())
|
content.write(buildEmptyStszBox())
|
||||||
content.write(buildEmptyStcoBox())
|
content.write(buildEmptyStcoBox())
|
||||||
@@ -665,11 +782,21 @@ class HlsMuxer(
|
|||||||
return wrapBox("avcC", output.toByteArray())
|
return wrapBox("avcC", output.toByteArray())
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun buildEmptySttsBox(): ByteArray {
|
private fun buildSttsBox(): ByteArray {
|
||||||
val output = ByteArrayOutputStream()
|
val output = ByteArrayOutputStream()
|
||||||
val dos = DataOutputStream(output)
|
val dos = DataOutputStream(output)
|
||||||
|
|
||||||
|
// For fragmented MP4, stts is normally empty as timing is in trun boxes.
|
||||||
|
// However, ffprobe uses stts to calculate r_frame_rate when present.
|
||||||
|
// We add a single entry with the default sample duration so ffprobe
|
||||||
|
// can derive: r_frame_rate = timescale / sample_delta = 30000/1000 = 30
|
||||||
|
val defaultSampleDuration = timescale / fps
|
||||||
|
|
||||||
dos.writeInt(0) // version & flags
|
dos.writeInt(0) // version & flags
|
||||||
dos.writeInt(0) // entry count
|
dos.writeInt(1) // entry count (1 entry for default timing)
|
||||||
|
dos.writeInt(1) // sample_count (indicates this is the default duration)
|
||||||
|
dos.writeInt(defaultSampleDuration) // sample_delta in timescale units
|
||||||
|
|
||||||
return wrapBox("stts", output.toByteArray())
|
return wrapBox("stts", output.toByteArray())
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -706,10 +833,15 @@ class HlsMuxer(
|
|||||||
val output = ByteArrayOutputStream()
|
val output = ByteArrayOutputStream()
|
||||||
val dos = DataOutputStream(output)
|
val dos = DataOutputStream(output)
|
||||||
|
|
||||||
|
// Calculate default sample duration so ffprobe can derive correct fps
|
||||||
|
// fps = timescale / default_sample_duration
|
||||||
|
// At 30fps with timescale=30000: duration=1000, ffprobe calculates 30000/1000=30
|
||||||
|
val defaultSampleDuration = timescale / fps
|
||||||
|
|
||||||
dos.writeInt(0) // version & flags
|
dos.writeInt(0) // version & flags
|
||||||
dos.writeInt(1) // track ID
|
dos.writeInt(1) // track ID
|
||||||
dos.writeInt(1) // default sample description index
|
dos.writeInt(1) // default sample description index
|
||||||
dos.writeInt(0) // default sample duration
|
dos.writeInt(defaultSampleDuration) // default sample duration
|
||||||
dos.writeInt(0) // default sample size
|
dos.writeInt(0) // default sample size
|
||||||
dos.writeInt(0) // default sample flags
|
dos.writeInt(0) // default sample flags
|
||||||
|
|
||||||
@@ -753,9 +885,11 @@ class HlsMuxer(
|
|||||||
): ByteArray {
|
): ByteArray {
|
||||||
// Calculate sizes to determine data offset
|
// Calculate sizes to determine data offset
|
||||||
val mfhdBox = buildMfhdBox(sequenceNumber)
|
val mfhdBox = buildMfhdBox(sequenceNumber)
|
||||||
val tfhdSize = 8 + 8 // box header + content (version/flags + track_id)
|
// tfhd: 8 header + 4 version/flags + 4 track_id + 4 duration + 4 size + 4 flags = 28 bytes
|
||||||
|
val tfhdSize = 8 + 20
|
||||||
val tfdtSize = 8 + 12 // box header + version 1 content
|
val tfdtSize = 8 + 12 // box header + version 1 content
|
||||||
val trunSize = 8 + 12 + (samples.size * 12) // header + fixed + per-sample (no composition offset)
|
// trun: 8 header + 12 fixed + per-sample (size + flags only, no duration)
|
||||||
|
val trunSize = 8 + 12 + (samples.size * 8)
|
||||||
val trafSize = 8 + tfhdSize + tfdtSize + trunSize
|
val trafSize = 8 + tfhdSize + tfdtSize + trunSize
|
||||||
val moofSize = 8 + mfhdBox.size + trafSize
|
val moofSize = 8 + mfhdBox.size + trafSize
|
||||||
|
|
||||||
@@ -790,9 +924,21 @@ class HlsMuxer(
|
|||||||
val output = ByteArrayOutputStream()
|
val output = ByteArrayOutputStream()
|
||||||
val dos = DataOutputStream(output)
|
val dos = DataOutputStream(output)
|
||||||
|
|
||||||
// Flags: default-base-is-moof (0x020000)
|
// Calculate default sample duration for this fragment
|
||||||
dos.writeInt(0x00020000)
|
// This helps ffprobe calculate correct frame rate when reading via HLS
|
||||||
|
val defaultSampleDuration = timescale / fps // e.g., 30000/30 = 1000
|
||||||
|
|
||||||
|
// Match iOS AVFoundation's tfhd structure (28 bytes total)
|
||||||
|
// Flags: default-base-is-moof (0x020000) + default-sample-duration (0x000008)
|
||||||
|
// + default-sample-size (0x000010) + default-sample-flags (0x000020)
|
||||||
|
val flags = 0x00020000 or 0x000008 or 0x000010 or 0x000020
|
||||||
|
dos.writeInt(flags)
|
||||||
dos.writeInt(1) // track ID
|
dos.writeInt(1) // track ID
|
||||||
|
dos.writeInt(defaultSampleDuration) // default sample duration in timescale units
|
||||||
|
dos.writeInt(0) // default sample size (0 = variable, specified in trun)
|
||||||
|
dos.writeInt(0x01010000) // default sample flags (non-keyframe, depends on others)
|
||||||
|
|
||||||
|
Log.d(TAG, "tfhd: default_sample_duration=$defaultSampleDuration (timescale=$timescale, fps=$fps)")
|
||||||
|
|
||||||
return wrapBox("tfhd", output.toByteArray())
|
return wrapBox("tfhd", output.toByteArray())
|
||||||
}
|
}
|
||||||
@@ -815,16 +961,17 @@ class HlsMuxer(
|
|||||||
val output = ByteArrayOutputStream()
|
val output = ByteArrayOutputStream()
|
||||||
val dos = DataOutputStream(output)
|
val dos = DataOutputStream(output)
|
||||||
|
|
||||||
// Flags: data-offset + sample-duration + sample-size + sample-flags
|
// Flags: data-offset + sample-size + sample-flags
|
||||||
val flags = 0x000001 or 0x000100 or 0x000200 or 0x000400
|
// NOTE: We intentionally OMIT sample-duration (0x000100) so ffprobe uses
|
||||||
|
// the default_sample_duration from tfhd instead of per-sample durations.
|
||||||
|
// This ensures consistent frame rate calculation via HLS.
|
||||||
|
val flags = 0x000001 or 0x000200 or 0x000400
|
||||||
dos.writeInt(flags)
|
dos.writeInt(flags)
|
||||||
dos.writeInt(samples.size)
|
dos.writeInt(samples.size)
|
||||||
dos.writeInt(dataOffset)
|
dos.writeInt(dataOffset)
|
||||||
|
|
||||||
for (sample in samples) {
|
for (sample in samples) {
|
||||||
// Convert duration to timescale units
|
// No duration - using default from tfhd
|
||||||
val durationInTimescale = ((sample.durationUs * timescale) / 1_000_000).toInt()
|
|
||||||
dos.writeInt(durationInTimescale)
|
|
||||||
dos.writeInt(sample.data.size)
|
dos.writeInt(sample.data.size)
|
||||||
dos.writeInt(buildSampleFlags(sample.isKeyFrame))
|
dos.writeInt(buildSampleFlags(sample.isKeyFrame))
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -44,7 +44,14 @@ class RecordingSession(
|
|||||||
|
|
||||||
data class Video(val path: String, val durationMs: Long, val size: Size)
|
data class Video(val path: String, val durationMs: Long, val size: Size)
|
||||||
|
|
||||||
private val outputPath: File = File(filePath)
|
// Strip file:// prefix if present (expo-file-system returns URIs with this prefix)
|
||||||
|
private val outputPath: File = File(
|
||||||
|
if (filePath.startsWith("file://")) {
|
||||||
|
filePath.removePrefix("file://")
|
||||||
|
} else {
|
||||||
|
filePath
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
private val bitRate = getBitRate()
|
private val bitRate = getBitRate()
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user