androidtestingandroid-camera2android-mediacodecdev-null

Discard MediaCodec output after encoding


I am using MediaCodec in a Camera2 app and I have a use case of testing it by running recording for a day. I want the encode the video but I want it to be discarded just as soon as it is done so no storage is used. Something like routing the MediaCodec output to dev/null would be best if that is possible.

Any kind of hack also would do, just that the encoding of the video should be going on but not saved.


Solution

  • I found the solution. We can indeed redirect to dev/null even on Android.

    private fun create(): MediaMuxer {
        return  MediaMuxer(openFile("dev/null")!!, MediaMuxer.OutputFormat.MUXER_OUTPUT_MPEG_4)
    }
    
    private fun openFile(path: String): FileDescriptor? {
        val file = File(path)
        val fos = FileOutputStream(file)
        return fos.fd
    }
    

    The other solution is to make the muxer null so the encoding happens but not the saving. muxer?.start() This solution is better if you want to run encoding for days which has a problem of track index exceeding the max value.