Support for generating video thumbnails

// FREEBIE
This commit is contained in:
Moxie Marlinspike
2016-12-11 13:37:27 -08:00
parent 6308e6491a
commit a4c17e5325
30 changed files with 268 additions and 108 deletions

View File

@@ -0,0 +1,64 @@
package org.thoughtcrime.securesms.video;
import android.media.MediaDataSource;
import android.os.Build;
import android.support.annotation.RequiresApi;
import org.thoughtcrime.securesms.crypto.DecryptingPartInputStream;
import org.thoughtcrime.securesms.crypto.MasterSecret;
import org.thoughtcrime.securesms.util.Util;
import java.io.File;
import java.io.IOException;
@RequiresApi(api = Build.VERSION_CODES.M)
public class EncryptedMediaDataSource extends MediaDataSource {
private final File mediaFile;
private final MasterSecret masterSecret;
public EncryptedMediaDataSource(MasterSecret masterSecret, File mediaFile) {
this.mediaFile = mediaFile;
this.masterSecret = masterSecret;
}
@Override
public int readAt(long position, byte[] bytes, int offset, int length) throws IOException {
DecryptingPartInputStream inputStream = new DecryptingPartInputStream(mediaFile, masterSecret);
byte[] buffer = new byte[4096];
long headerRemaining = position;
while (headerRemaining > 0) {
int read = inputStream.read(buffer, 0, Util.toIntExact(Math.min((long)buffer.length, headerRemaining)));
if (read == -1) return -1;
headerRemaining -= read;
}
int returnValue = inputStream.read(bytes, offset, length);
inputStream.close();
return returnValue;
}
@Override
public long getSize() throws IOException {
DecryptingPartInputStream inputStream = new DecryptingPartInputStream(mediaFile, masterSecret);
byte[] buffer = new byte[4096];
long size = 0;
int read;
while ((read = inputStream.read(buffer)) != -1) {
size += read;
}
return size;
}
@Override
public void close() throws IOException {
}
}