Only generate incremental mac for faststart videos.

This commit is contained in:
Nicholas
2023-11-28 09:36:32 -05:00
committed by Cody Henthorne
parent 1fd6aae3d9
commit 67ef831681
17 changed files with 96 additions and 49 deletions

View File

@@ -792,6 +792,7 @@ public class SignalServiceMessageSender {
PushAttachmentData attachmentData = new PushAttachmentData(attachment.getContentType(),
dataStream,
ciphertextLength,
attachment.isFaststart(),
new AttachmentCipherOutputStreamFactory(attachmentKey, attachmentIV),
attachment.getListener(),
attachment.getCancelationSignal(),
@@ -877,7 +878,7 @@ public class SignalServiceMessageSender {
attachment.getHeight(),
Optional.of(digest.getDigest()),
Optional.ofNullable(digest.getIncrementalDigest()),
digest.getIncrementalMacChunkSize(),
digest.getIncrementalDigest() != null ? digest.getIncrementalMacChunkSize() : 0,
attachment.getFileName(),
attachment.getVoiceNote(),
attachment.isBorderless(),

View File

@@ -41,7 +41,7 @@ public abstract class SignalServiceAttachment {
}
public static SignalServiceAttachmentStream emptyStream(String contentType) {
return new SignalServiceAttachmentStream(new ByteArrayInputStream(new byte[0]), contentType, 0, Optional.empty(), false, false, false, null, null);
return new SignalServiceAttachmentStream(new ByteArrayInputStream(new byte[0]), contentType, 0, Optional.empty(), false, false, false, false, null, null);
}
public static class Builder {
@@ -55,6 +55,7 @@ public abstract class SignalServiceAttachment {
private boolean voiceNote;
private boolean borderless;
private boolean gif;
private boolean faststart;
private int width;
private int height;
private String caption;
@@ -109,6 +110,11 @@ public abstract class SignalServiceAttachment {
return this;
}
public Builder withFaststart(boolean faststart) {
this.faststart = faststart;
return this;
}
public Builder withWidth(int width) {
this.width = width;
return this;
@@ -151,6 +157,7 @@ public abstract class SignalServiceAttachment {
voiceNote,
borderless,
gif,
faststart,
Optional.empty(),
width,
height,

View File

@@ -29,6 +29,7 @@ public class SignalServiceAttachmentStream extends SignalServiceAttachment imple
private final boolean voiceNote;
private final boolean borderless;
private final boolean gif;
private final boolean faststart;
private final int width;
private final int height;
private final long uploadTimestamp;
@@ -43,10 +44,11 @@ public class SignalServiceAttachmentStream extends SignalServiceAttachment imple
boolean voiceNote,
boolean borderless,
boolean gif,
boolean faststart,
ProgressListener listener,
CancelationSignal cancelationSignal)
{
this(inputStream, contentType, length, fileName, voiceNote, borderless, gif, Optional.empty(), 0, 0, System.currentTimeMillis(), Optional.empty(), Optional.empty(), listener, cancelationSignal, Optional.empty());
this(inputStream, contentType, length, fileName, voiceNote, borderless, gif, faststart, Optional.empty(), 0, 0, System.currentTimeMillis(), Optional.empty(), Optional.empty(), listener, cancelationSignal, Optional.empty());
}
public SignalServiceAttachmentStream(InputStream inputStream,
@@ -56,6 +58,7 @@ public class SignalServiceAttachmentStream extends SignalServiceAttachment imple
boolean voiceNote,
boolean borderless,
boolean gif,
boolean faststart,
Optional<byte[]> preview,
int width,
int height,
@@ -67,21 +70,22 @@ public class SignalServiceAttachmentStream extends SignalServiceAttachment imple
Optional<ResumableUploadSpec> resumableUploadSpec)
{
super(contentType);
this.inputStream = inputStream;
this.length = length;
this.fileName = fileName;
this.listener = listener;
this.voiceNote = voiceNote;
this.borderless = borderless;
this.gif = gif;
this.preview = preview;
this.width = width;
this.height = height;
this.uploadTimestamp = uploadTimestamp;
this.caption = caption;
this.blurHash = blurHash;
this.cancelationSignal = cancelationSignal;
this.resumableUploadSpec = resumableUploadSpec;
this.inputStream = inputStream;
this.length = length;
this.fileName = fileName;
this.listener = listener;
this.voiceNote = voiceNote;
this.borderless = borderless;
this.gif = gif;
this.preview = preview;
this.faststart = faststart;
this.width = width;
this.height = height;
this.uploadTimestamp = uploadTimestamp;
this.caption = caption;
this.blurHash = blurHash;
this.cancelationSignal = cancelationSignal;
this.resumableUploadSpec = resumableUploadSpec;
}
@Override
@@ -130,6 +134,10 @@ public class SignalServiceAttachmentStream extends SignalServiceAttachment imple
return gif;
}
public boolean isFaststart() {
return faststart;
}
public int getWidth() {
return width;
}

View File

@@ -61,7 +61,7 @@ public class DeviceContactsInputStream extends ChunkedInputStream {
InputStream avatarStream = new LimitedInputStream(in, avatarLength);
String avatarContentType = details.avatar.contentType;
avatar = Optional.of(new SignalServiceAttachmentStream(avatarStream, avatarContentType, avatarLength, Optional.empty(), false, false, false, null, null));
avatar = Optional.of(new SignalServiceAttachmentStream(avatarStream, avatarContentType, avatarLength, Optional.empty(), false, false, false, false, null, null));
}
if (details.verified != null) {

View File

@@ -18,19 +18,21 @@ public class PushAttachmentData {
private final String contentType;
private final InputStream data;
private final long dataSize;
private final boolean incremental;
private final OutputStreamFactory outputStreamFactory;
private final ProgressListener listener;
private final CancelationSignal cancelationSignal;
private final ResumableUploadSpec resumableUploadSpec;
public PushAttachmentData(String contentType, InputStream data, long dataSize,
OutputStreamFactory outputStreamFactory,
boolean incremental, OutputStreamFactory outputStreamFactory,
ProgressListener listener, CancelationSignal cancelationSignal,
ResumableUploadSpec resumableUploadSpec)
{
this.contentType = contentType;
this.data = data;
this.dataSize = dataSize;
this.incremental = incremental;
this.outputStreamFactory = outputStreamFactory;
this.resumableUploadSpec = resumableUploadSpec;
this.listener = listener;
@@ -49,6 +51,10 @@ public class PushAttachmentData {
return dataSize;
}
public boolean getIncremental() {
return incremental;
}
public OutputStreamFactory getOutputStreamFactory() {
return outputStreamFactory;
}

View File

@@ -946,7 +946,7 @@ public class PushServiceSocket {
formAttributes.getPolicy(), formAttributes.getAlgorithm(),
formAttributes.getCredential(), formAttributes.getDate(),
formAttributes.getSignature(), profileAvatar.getData(),
profileAvatar.getContentType(), profileAvatar.getDataLength(),
profileAvatar.getContentType(), profileAvatar.getDataLength(), false,
profileAvatar.getOutputStreamFactory(), null, null);
return Optional.of(formAttributes.getKey());
@@ -1393,7 +1393,7 @@ public class PushServiceSocket {
uploadAttributes.credential, uploadAttributes.date,
uploadAttributes.signature,
new ByteArrayInputStream(avatarCipherText),
"application/octet-stream", avatarCipherText.length,
"application/octet-stream", avatarCipherText.length, false,
new NoCipherOutputStreamFactory(),
null, null);
}
@@ -1407,8 +1407,8 @@ public class PushServiceSocket {
uploadAttributes.getCredential(), uploadAttributes.getDate(),
uploadAttributes.getSignature(), attachment.getData(),
"application/octet-stream", attachment.getDataSize(),
attachment.getOutputStreamFactory(), attachment.getListener(),
attachment.getCancelationSignal());
attachment.getIncremental(), attachment.getOutputStreamFactory(),
attachment.getListener(), attachment.getCancelationSignal());
return new Pair<>(id, digest);
}
@@ -1434,6 +1434,7 @@ public class PushServiceSocket {
attachment.getData(),
"application/octet-stream",
attachment.getDataSize(),
attachment.getIncremental(),
attachment.getOutputStreamFactory(),
attachment.getListener(),
attachment.getCancelationSignal());
@@ -1442,6 +1443,7 @@ public class PushServiceSocket {
attachment.getData(),
"application/offset+octet-stream",
attachment.getDataSize(),
attachment.getIncremental(),
attachment.getOutputStreamFactory(),
attachment.getListener(),
attachment.getCancelationSignal(),
@@ -1529,7 +1531,7 @@ public class PushServiceSocket {
private AttachmentDigest uploadToCdn0(String path, String acl, String key, String policy, String algorithm,
String credential, String date, String signature,
InputStream data, String contentType, long length,
InputStream data, String contentType, long length, boolean incremental,
OutputStreamFactory outputStreamFactory, ProgressListener progressListener,
CancelationSignal cancelationSignal)
throws PushNetworkException, NonSuccessfulResponseCodeException
@@ -1541,7 +1543,7 @@ public class PushServiceSocket {
.readTimeout(soTimeoutMillis, TimeUnit.MILLISECONDS)
.build();
DigestingRequestBody file = new DigestingRequestBody(data, outputStreamFactory, contentType, length, progressListener, cancelationSignal, 0);
DigestingRequestBody file = new DigestingRequestBody(data, outputStreamFactory, contentType, length, incremental, progressListener, cancelationSignal, 0);
RequestBody requestBody = new MultipartBody.Builder()
.setType(MultipartBody.FORM)
@@ -1639,7 +1641,7 @@ public class PushServiceSocket {
}
}
private AttachmentDigest uploadToCdn2(String resumableUrl, InputStream data, String contentType, long length, OutputStreamFactory outputStreamFactory, ProgressListener progressListener, CancelationSignal cancelationSignal) throws IOException {
private AttachmentDigest uploadToCdn2(String resumableUrl, InputStream data, String contentType, long length, boolean incremental, OutputStreamFactory outputStreamFactory, ProgressListener progressListener, CancelationSignal cancelationSignal) throws IOException {
ConnectionHolder connectionHolder = getRandom(cdnClientsMap.get(2), random);
OkHttpClient okHttpClient = connectionHolder.getClient()
.newBuilder()
@@ -1648,7 +1650,7 @@ public class PushServiceSocket {
.build();
ResumeInfo resumeInfo = getResumeInfoCdn2(resumableUrl, length);
DigestingRequestBody file = new DigestingRequestBody(data, outputStreamFactory, contentType, length, progressListener, cancelationSignal, resumeInfo.contentStart);
DigestingRequestBody file = new DigestingRequestBody(data, outputStreamFactory, contentType, length, incremental, progressListener, cancelationSignal, resumeInfo.contentStart);
if (resumeInfo.contentStart == length) {
Log.w(TAG, "Resume start point == content length");
@@ -1690,6 +1692,7 @@ public class PushServiceSocket {
InputStream data,
String contentType,
long length,
boolean incremental,
OutputStreamFactory outputStreamFactory,
ProgressListener progressListener,
CancelationSignal cancelationSignal,
@@ -1704,7 +1707,7 @@ public class PushServiceSocket {
.build();
ResumeInfo resumeInfo = getResumeInfoCdn3(resumableUrl, headers);
DigestingRequestBody file = new DigestingRequestBody(data, outputStreamFactory, contentType, length, progressListener, cancelationSignal, resumeInfo.contentStart);
DigestingRequestBody file = new DigestingRequestBody(data, outputStreamFactory, contentType, length, incremental, progressListener, cancelationSignal, resumeInfo.contentStart);
if (resumeInfo.contentStart == length) {
Log.w(TAG, "Resume start point == content length");

View File

@@ -22,6 +22,7 @@ class DigestingRequestBody(
private val outputStreamFactory: OutputStreamFactory,
private val contentType: String,
private val contentLength: Long,
private val incremental: Boolean,
private val progressListener: SignalServiceAttachment.ProgressListener?,
private val cancelationSignal: CancelationSignal?,
private val contentStart: Long
@@ -41,7 +42,7 @@ class DigestingRequestBody(
override fun writeTo(sink: BufferedSink) {
val digestStream = ByteArrayOutputStream()
val inner = SkippingOutputStream(contentStart, sink.outputStream())
val isIncremental = outputStreamFactory is AttachmentCipherOutputStreamFactory
val isIncremental = incremental && outputStreamFactory is AttachmentCipherOutputStreamFactory
val sizeChoice: ChunkSizeChoice = ChunkSizeChoice.inferChunkSize(contentLength.toInt())
val outputStream: DigestingOutputStream = if (isIncremental) {
(outputStreamFactory as AttachmentCipherOutputStreamFactory).createIncrementalFor(inner, contentLength, sizeChoice, digestStream)