|
| 1 | +package org.droidplanner.services.android.utils.video; |
| 2 | + |
| 3 | +import android.content.Context; |
| 4 | +import android.media.MediaScannerConnection; |
| 5 | +import android.net.Uri; |
| 6 | +import android.os.Environment; |
| 7 | +import android.text.TextUtils; |
| 8 | +import android.util.Log; |
| 9 | + |
| 10 | +import com.coremedia.iso.boxes.Container; |
| 11 | +import com.googlecode.mp4parser.FileDataSourceImpl; |
| 12 | +import com.googlecode.mp4parser.authoring.Movie; |
| 13 | +import com.googlecode.mp4parser.authoring.builder.DefaultMp4Builder; |
| 14 | +import com.googlecode.mp4parser.authoring.tracks.h264.H264TrackImpl; |
| 15 | + |
| 16 | +import java.io.BufferedOutputStream; |
| 17 | +import java.io.File; |
| 18 | +import java.io.FileNotFoundException; |
| 19 | +import java.io.FileOutputStream; |
| 20 | +import java.io.IOException; |
| 21 | +import java.nio.channels.FileChannel; |
| 22 | +import java.util.concurrent.ExecutorService; |
| 23 | +import java.util.concurrent.Executors; |
| 24 | +import java.util.concurrent.atomic.AtomicReference; |
| 25 | + |
| 26 | +import timber.log.Timber; |
| 27 | + |
| 28 | +/** |
| 29 | + * Created by Fredia Huya-Kouadio on 11/22/15. |
| 30 | + */ |
| 31 | +class StreamRecorder { |
| 32 | + |
| 33 | + private final AtomicReference<String> recordingFilename = new AtomicReference<>(); |
| 34 | + |
| 35 | + private final File mediaRootDir; |
| 36 | + private final Context context; |
| 37 | + |
| 38 | + private final MediaScannerConnection.OnScanCompletedListener scanCompletedListener = new MediaScannerConnection.OnScanCompletedListener() { |
| 39 | + @Override |
| 40 | + public void onScanCompleted(String path, Uri uri) { |
| 41 | + Timber.i("Media file %s was scanned successfully: %s", path, uri); |
| 42 | + } |
| 43 | + }; |
| 44 | + |
| 45 | + private ExecutorService asyncExecutor; |
| 46 | + |
| 47 | + private BufferedOutputStream h264Writer; |
| 48 | + |
| 49 | + StreamRecorder(Context context) { |
| 50 | + this.context = context; |
| 51 | + this.mediaRootDir = new File(context.getExternalFilesDir(Environment.DIRECTORY_MOVIES), "stream"); |
| 52 | + if (!this.mediaRootDir.exists()) { |
| 53 | + this.mediaRootDir.mkdirs(); |
| 54 | + } |
| 55 | + } |
| 56 | + |
| 57 | + String getRecordingFilename(){ |
| 58 | + return recordingFilename.get(); |
| 59 | + } |
| 60 | + |
| 61 | + void startConverterThread() { |
| 62 | + if (asyncExecutor == null || asyncExecutor.isShutdown()) { |
| 63 | + asyncExecutor = Executors.newSingleThreadExecutor(); |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + void stopConverterThread() { |
| 68 | + if (asyncExecutor != null) |
| 69 | + asyncExecutor.shutdown(); |
| 70 | + } |
| 71 | + |
| 72 | + boolean isRecordingEnabled() { |
| 73 | + return !TextUtils.isEmpty(recordingFilename.get()); |
| 74 | + } |
| 75 | + |
| 76 | + boolean enableRecording(String mediaFilename) { |
| 77 | + if (!isRecordingEnabled()) { |
| 78 | + recordingFilename.set(mediaFilename); |
| 79 | + |
| 80 | + Timber.i("Enabling local recording to %s", mediaFilename); |
| 81 | + File h264File = new File(mediaRootDir, mediaFilename); |
| 82 | + if (h264File.exists()) |
| 83 | + h264File.delete(); |
| 84 | + |
| 85 | + try { |
| 86 | + h264Writer = new BufferedOutputStream(new FileOutputStream(h264File)); |
| 87 | + return true; |
| 88 | + } catch (FileNotFoundException e) { |
| 89 | + Timber.e(e, e.getMessage()); |
| 90 | + recordingFilename.set(null); |
| 91 | + return false; |
| 92 | + } |
| 93 | + } else { |
| 94 | + Timber.w("Video stream recording is already enabled"); |
| 95 | + return false; |
| 96 | + } |
| 97 | + } |
| 98 | + |
| 99 | + boolean disableRecording() { |
| 100 | + if (isRecordingEnabled()) { |
| 101 | + Timber.i("Disabling local recording"); |
| 102 | + |
| 103 | + //Close the Buffered output stream |
| 104 | + if (h264Writer != null) { |
| 105 | + try { |
| 106 | + h264Writer.close(); |
| 107 | + } catch (IOException e) { |
| 108 | + Timber.e(e, e.getMessage()); |
| 109 | + } finally { |
| 110 | + h264Writer = null; |
| 111 | + |
| 112 | + //Kickstart conversion of the h264 file to mp4. |
| 113 | + convertToMp4(recordingFilename.get()); |
| 114 | + |
| 115 | + recordingFilename.set(null); |
| 116 | + } |
| 117 | + } |
| 118 | + } |
| 119 | + |
| 120 | + return true; |
| 121 | + } |
| 122 | + |
| 123 | + //TODO: Maybe put this on a background thread to avoid blocking on the write to file. |
| 124 | + void onNaluChunkUpdated(byte[] payload, int index, int payloadLength) { |
| 125 | + if (isRecordingEnabled() && h264Writer != null) { |
| 126 | + try { |
| 127 | + h264Writer.write(payload, index, payloadLength); |
| 128 | + } catch (IOException e) { |
| 129 | + Timber.e(e, e.getMessage()); |
| 130 | + } |
| 131 | + } |
| 132 | + } |
| 133 | + |
| 134 | + void convertToMp4(final String filename) { |
| 135 | + if (TextUtils.isEmpty(filename)) { |
| 136 | + Timber.w("Invalid media filename."); |
| 137 | + return; |
| 138 | + } |
| 139 | + |
| 140 | + final File rawMedia = new File(mediaRootDir, filename); |
| 141 | + if (!rawMedia.exists()) { |
| 142 | + Timber.w("Media file doesn't exists."); |
| 143 | + return; |
| 144 | + } |
| 145 | + |
| 146 | + asyncExecutor.execute(new Runnable() { |
| 147 | + @Override |
| 148 | + public void run() { |
| 149 | + Timber.i("Starting h264 conversion process for media file %s.", filename); |
| 150 | + |
| 151 | + try { |
| 152 | + H264TrackImpl h264Track = new H264TrackImpl(new FileDataSourceImpl(rawMedia)); |
| 153 | + Movie movie = new Movie(); |
| 154 | + movie.addTrack(h264Track); |
| 155 | + Container mp4File = new DefaultMp4Builder().build(movie); |
| 156 | + |
| 157 | + File dstDir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_MOVIES); |
| 158 | + File mp4Media = new File(dstDir, filename + ".mp4"); |
| 159 | + Timber.i("Generating the mp4 file @ %s", mp4Media.getAbsolutePath()); |
| 160 | + FileChannel fc = new FileOutputStream(mp4Media).getChannel(); |
| 161 | + mp4File.writeContainer(fc); |
| 162 | + fc.close(); |
| 163 | + |
| 164 | + //Delete the h264 file. |
| 165 | + Timber.i("Deleting raw h264 media file."); |
| 166 | + rawMedia.delete(); |
| 167 | + |
| 168 | + //Add the generated file to the mediastore |
| 169 | + Timber.i("Adding the generated mp4 file to the media store."); |
| 170 | + MediaScannerConnection.scanFile(context, |
| 171 | + new String[]{mp4Media.getAbsolutePath()}, null, scanCompletedListener); |
| 172 | + |
| 173 | + } catch (IOException e) { |
| 174 | + Timber.e(e, e.getMessage()); |
| 175 | + } |
| 176 | + } |
| 177 | + }); |
| 178 | + } |
| 179 | +} |
0 commit comments