我(差不多)想明白了!我找到了ImageWriter
API,这正是我要从头开始重建的东西--一条从图像到曲面的直通管道.
因此,现在我将摄像机帧传输到ImageReader
,调用具有Image
的帧处理器,然后使用ImageWriter
作为中间人将Image
传递到MediaRecorder
:)
val size = config.getOutputSizes(ImageFormat.PRIVATE).max()
// Video Recorder Surface. We need to stream Frames here if we are recording.
val surface = recordingSession.surface
val imageWriter = ImageWriter.newInstance(surface,
VIDEO_OUTPUT_BUFFER_SIZE)
// Image Reader Surface. We stream Frames here for Frame Processor or Recording.
val flags = HardwareBuffer.USAGE_GPU_SAMPLED_IMAGE or HardwareBuffer.USAGE_VIDEO_ENCODE
val imageReader = ImageReader.newInstance(size.width,
size.height,
ImageFormat.PRIVATE,
VIDEO_OUTPUT_BUFFER_SIZE,
flags)
imageReader.setOnImageAvailableListener({ reader ->
val image = reader.acquireNextImage() ?: return
image.timestamp = System.nanoTime()
// Call JS Frame Processor
frameProcessor?.call(image)
// If recording, write to Video File
if (isRecording) {
imageWriter.queueInputImage(image)
}
image.close()
}, CameraQueues.videoQueue)
// Camera only streams frames into one single Surface
cameraSession.configure(.., imageReader.surface)
我现在唯一的问题是,所产生的视频记录有时会在大约3秒的记录后出现~1秒的长仰卧起坐,我不知道为什么.也许我应该用MediaCodec
而不是MediaRecorder
.也许我应该用一辆不同的ImageFormat
.也许我应该调查一下生成的.mp4
文件,看看出了什么问题.也许我应该修复图像时间戳.我不知道.
此外,Logcat还会收到以下垃圾邮件:
2023-08-17 11:38:17.977 3780-3899 GraphicBufferSource com.mrousavy.camera.example W released unpopulated slots: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63]
2023-08-17 11:38:18.021 3780-3899 GraphicBufferSource com.mrousavy.camera.example W released unpopulated slots: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63]
2023-08-17 11:38:18.050 3780-3899 GraphicBufferSource com.mrousavy.camera.example W released unpopulated slots: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63]
2023-08-17 11:38:18.082 3780-3899 GraphicBufferSource com.mrousavy.camera.example W released unpopulated slots: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63]
2023-08-17 11:38:18.113 3780-3899 GraphicBufferSource com.mrousavy.camera.example W released unpopulated slots: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63]
2023-08-17 11:38:18.146 3780-3899 GraphicBufferSource com.mrousavy.camera.example W released unpopulated slots: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63]
2023-08-17 11:38:18.179 3780-3899 GraphicBufferSource com.mrousavy.camera.example W released unpopulated slots: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63]
但是,嘿--它会录下一段视频.这是一个良好的开端.