Skip to content

Commit 89f4ee8

Browse files
[SES-383] - Fix voice message duration issue (#1200)
1 parent 7e62bac commit 89f4ee8

26 files changed

+480
-661
lines changed

app/src/main/java/org/session/libsession/database/StorageProtocol.kt

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -114,10 +114,6 @@ interface StorageProtocol {
114114
fun getReceivedMessageTimestamps(): Set<Long>
115115
fun addReceivedMessageTimestamp(timestamp: Long)
116116
fun removeReceivedMessageTimestamps(timestamps: Set<Long>)
117-
/**
118-
* Returns the IDs of the saved attachments.
119-
*/
120-
fun persistAttachments(messageID: Long, attachments: List<Attachment>): List<Long>
121117
fun getAttachmentsForMessage(mmsMessageId: Long): List<DatabaseAttachment>
122118
fun getMessageBy(timestamp: Long, author: String): MessageRecord?
123119
fun updateSentTimestamp(messageId: MessageId, openGroupSentTimestamp: Long, threadId: Long)

app/src/main/java/org/session/libsession/messaging/jobs/AttachmentUploadJob.kt

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -116,15 +116,13 @@ class AttachmentUploadJob(val attachmentID: Long, val threadID: String, val mess
116116
val messageDataProvider = MessagingModuleConfiguration.shared.messageDataProvider
117117
messageDataProvider.handleSuccessfulAttachmentUpload(attachmentID, attachment, attachmentKey, uploadResult)
118118

119-
// Outgoing voice messages do not have their final duration set because older Android versions (API 28 and below)
120-
// can have bugs where the media duration is calculated incorrectly. In such cases we leave the correct "interim"
121-
// voice message duration as the final duration as we know that it'll be correct..
119+
// We don't need to calculate the duration for voice notes, as they will have it set already.
122120
if (attachment.contentType.startsWith("audio/") && !attachment.voiceNote) {
123-
// ..but for outgoing audio files we do process the duration to the best of our ability.
124121
try {
125122
val inputStream = messageDataProvider.getAttachmentStream(attachmentID)!!.inputStream!!
126123
InputStreamMediaDataSource(inputStream).use { mediaDataSource ->
127124
val durationMS = (DecodedAudio.create(mediaDataSource).totalDurationMicroseconds / 1000.0).toLong()
125+
Log.d(TAG, "Audio attachment duration calculated as: $durationMS ms")
128126
messageDataProvider.getDatabaseAttachment(attachmentID)?.attachmentId?.let { attachmentId ->
129127
messageDataProvider.updateAudioAttachmentDuration(attachmentId, durationMS, threadID.toLong())
130128
}

app/src/main/java/org/session/libsession/messaging/sending_receiving/attachments/Attachment.java

Lines changed: 88 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -6,43 +6,53 @@
66

77
public abstract class Attachment {
88

9-
@NonNull private final String contentType;
9+
@NonNull
10+
private final String contentType;
1011
private final int transferState;
1112
private final long size;
1213
private final String filename;
1314

14-
@Nullable private final String location;
15-
@Nullable private final String key;
16-
@Nullable private final String relay;
17-
@Nullable private final byte[] digest;
18-
@Nullable private final String fastPreflightId;
15+
@Nullable
16+
private final String location;
17+
@Nullable
18+
private final String key;
19+
@Nullable
20+
private final String relay;
21+
@Nullable
22+
private final byte[] digest;
23+
@Nullable
24+
private final String fastPreflightId;
1925
private final boolean voiceNote;
2026
private final int width;
2127
private final int height;
2228
private final boolean quote;
23-
@Nullable private final String caption;
29+
@Nullable
30+
private final String caption;
2431
private final String url;
2532

33+
private final long audioDurationMs;
34+
2635
public Attachment(@NonNull String contentType, int transferState, long size, String filename,
2736
@Nullable String location, @Nullable String key, @Nullable String relay,
2837
@Nullable byte[] digest, @Nullable String fastPreflightId, boolean voiceNote,
29-
int width, int height, boolean quote, @Nullable String caption, String url)
30-
{
31-
this.contentType = contentType;
32-
this.transferState = transferState;
33-
this.size = size;
34-
this.filename = filename;
35-
this.location = location;
36-
this.key = key;
37-
this.relay = relay;
38-
this.digest = digest;
38+
int width, int height, boolean quote, @Nullable String caption, String url,
39+
long audioDurationMs) {
40+
this.contentType = contentType;
41+
this.transferState = transferState;
42+
this.size = size;
43+
this.filename = filename;
44+
this.location = location;
45+
this.key = key;
46+
this.relay = relay;
47+
this.digest = digest;
3948
this.fastPreflightId = fastPreflightId;
40-
this.voiceNote = voiceNote;
41-
this.width = width;
42-
this.height = height;
43-
this.quote = quote;
44-
this.caption = caption;
45-
this.url = url;
49+
this.voiceNote = voiceNote;
50+
this.width = width;
51+
this.height = height;
52+
this.quote = quote;
53+
this.caption = caption;
54+
this.url = url;
55+
this.audioDurationMs = audioDurationMs;
4656
}
4757

4858
@Nullable
@@ -51,7 +61,9 @@ public Attachment(@NonNull String contentType, int transferState, long size, Str
5161
@Nullable
5262
public abstract Uri getThumbnailUri();
5363

54-
public int getTransferState() { return transferState; }
64+
public int getTransferState() {
65+
return transferState;
66+
}
5567

5668
public boolean isInProgress() {
5769
return transferState == AttachmentState.DOWNLOADING.getValue();
@@ -65,37 +77,75 @@ public boolean isFailed() {
6577
return transferState == AttachmentState.FAILED.getValue();
6678
}
6779

68-
public long getSize() { return size; }
80+
public long getSize() {
81+
return size;
82+
}
6983

70-
public String getFilename() { return filename; }
84+
public String getFilename() {
85+
return filename;
86+
}
7187

7288
@NonNull
73-
public String getContentType() { return contentType; }
89+
public String getContentType() {
90+
return contentType;
91+
}
7492

7593
@Nullable
76-
public String getLocation() { return location; }
94+
public String getLocation() {
95+
return location;
96+
}
7797

7898
@Nullable
79-
public String getKey() { return key; }
99+
public String getKey() {
100+
return key;
101+
}
80102

81103
@Nullable
82-
public String getRelay() { return relay; }
104+
public String getRelay() {
105+
return relay;
106+
}
83107

84108
@Nullable
85-
public byte[] getDigest() { return digest; }
109+
public byte[] getDigest() {
110+
return digest;
111+
}
86112

87113
@Nullable
88-
public String getFastPreflightId() { return fastPreflightId; }
114+
public String getFastPreflightId() {
115+
return fastPreflightId;
116+
}
117+
118+
public boolean isVoiceNote() {
119+
return voiceNote;
120+
}
89121

90-
public boolean isVoiceNote() { return voiceNote; }
122+
public int getWidth() {
123+
return width;
124+
}
91125

92-
public int getWidth() { return width; }
126+
public int getHeight() {
127+
return height;
128+
}
93129

94-
public int getHeight() { return height; }
130+
public boolean isQuote() {
131+
return quote;
132+
}
95133

96-
public boolean isQuote() { return quote; }
134+
public @Nullable String getCaption() {
135+
return caption;
136+
}
97137

98-
public @Nullable String getCaption() { return caption; }
138+
public String getUrl() {
139+
return url;
140+
}
99141

100-
public String getUrl() { return url; }
142+
/**
143+
* Returns the duration of the audio in milliseconds.
144+
* This is only relevant for audio attachments.
145+
*
146+
* Returns -1 if the information is not available.
147+
*/
148+
public long getAudioDurationMs() {
149+
return audioDurationMs;
150+
}
101151
}

app/src/main/java/org/session/libsession/messaging/sending_receiving/attachments/DatabaseAttachment.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
package org.session.libsession.messaging.sending_receiving.attachments;
22

33
import android.net.Uri;
4+
45
import androidx.annotation.Nullable;
6+
57
import org.session.libsession.messaging.MessagingModuleConfiguration;
68

79
public class DatabaseAttachment extends Attachment {
@@ -18,9 +20,9 @@ public DatabaseAttachment(AttachmentId attachmentId, long mmsId,
1820
String filename, String location, String key, String relay,
1921
byte[] digest, String fastPreflightId, boolean voiceNote,
2022
int width, int height, boolean quote, @Nullable String caption,
21-
String url
23+
String url, long audioDurationMs
2224
) {
23-
super(contentType, transferProgress, size, filename, location, key, relay, digest, fastPreflightId, voiceNote, width, height, quote, caption, url);
25+
super(contentType, transferProgress, size, filename, location, key, relay, digest, fastPreflightId, voiceNote, width, height, quote, caption, url, audioDurationMs);
2426
this.attachmentId = attachmentId;
2527
this.hasData = hasData;
2628
this.hasThumbnail = hasThumbnail;

app/src/main/java/org/session/libsession/messaging/sending_receiving/attachments/PointerAttachment.java

Lines changed: 1 addition & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77

88
import org.session.libsignal.utilities.guava.Optional;
99
import org.session.libsignal.messages.SignalServiceAttachment;
10-
import org.session.libsignal.messages.SignalServiceDataMessage;
1110
import org.session.libsignal.utilities.Base64;
1211
import org.session.libsignal.protos.SignalServiceProtos;
1312

@@ -22,7 +21,7 @@ private PointerAttachment(@NonNull String contentType, int transferState, long s
2221
@Nullable byte[] digest, @Nullable String fastPreflightId, boolean voiceNote,
2322
int width, int height, @Nullable String caption, String url)
2423
{
25-
super(contentType, transferState, size, fileName, location, key, relay, digest, fastPreflightId, voiceNote, width, height, false, caption, url);
24+
super(contentType, transferState, size, fileName, location, key, relay, digest, fastPreflightId, voiceNote, width, height, false, caption, url, -1L);
2625
}
2726

2827
@Nullable
@@ -54,22 +53,6 @@ public static List<Attachment> forPointers(Optional<List<SignalServiceAttachment
5453
return results;
5554
}
5655

57-
public static List<Attachment> forPointersOfDataMessage(List<SignalServiceDataMessage.Quote.QuotedAttachment> pointers) {
58-
List<Attachment> results = new LinkedList<>();
59-
60-
if (pointers != null) {
61-
for (SignalServiceDataMessage.Quote.QuotedAttachment pointer : pointers) {
62-
Optional<Attachment> result = forPointer(pointer);
63-
64-
if (result.isPresent()) {
65-
results.add(result.get());
66-
}
67-
}
68-
}
69-
70-
return results;
71-
}
72-
7356
public static List<Attachment> forPointers(List<SignalServiceProtos.DataMessage.Quote.QuotedAttachment> pointers) {
7457
List<Attachment> results = new LinkedList<>();
7558

@@ -151,25 +134,6 @@ public static Optional<Attachment> forPointer(SignalServiceProtos.DataMessage.Qu
151134
thumbnail != null ? thumbnail.getUrl() : ""));
152135
}
153136

154-
public static Optional<Attachment> forPointer(SignalServiceDataMessage.Quote.QuotedAttachment pointer) {
155-
SignalServiceAttachment thumbnail = pointer.getThumbnail();
156-
157-
return Optional.of(new PointerAttachment(pointer.getContentType(),
158-
AttachmentState.PENDING.getValue(),
159-
thumbnail != null ? thumbnail.asPointer().getSize().or(0) : 0,
160-
pointer.getFileName(),
161-
String.valueOf(thumbnail != null ? thumbnail.asPointer().getId() : 0),
162-
thumbnail != null && thumbnail.asPointer().getKey() != null ? Base64.encodeBytes(thumbnail.asPointer().getKey()) : null,
163-
null,
164-
thumbnail != null ? thumbnail.asPointer().getDigest().orNull() : null,
165-
null,
166-
false,
167-
thumbnail != null ? thumbnail.asPointer().getWidth() : 0,
168-
thumbnail != null ? thumbnail.asPointer().getHeight() : 0,
169-
thumbnail != null ? thumbnail.asPointer().getCaption().orNull() : null,
170-
thumbnail != null ? thumbnail.asPointer().getUrl() : ""));
171-
}
172-
173137
/**
174138
* Converts a Session Attachment to a Signal Attachment
175139
* @param attachment Session Attachment

app/src/main/java/org/session/libsession/messaging/sending_receiving/attachments/UriAttachment.java

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,23 @@ public class UriAttachment extends Attachment {
1313
public UriAttachment(@NonNull Uri uri, @NonNull String contentType, int transferState, long size,
1414
@Nullable String fileName, boolean voiceNote, boolean quote, @Nullable String caption)
1515
{
16-
this(uri, uri, contentType, transferState, size, 0, 0, fileName, null, voiceNote, quote, caption);
16+
this(uri, uri, contentType, transferState, size, 0, 0, fileName, null, voiceNote, quote, caption, -1);
1717
}
1818

1919
public UriAttachment(@NonNull Uri dataUri, @Nullable Uri thumbnailUri,
2020
@NonNull String contentType, int transferState, long size, int width, int height,
2121
@Nullable String fileName, @Nullable String fastPreflightId,
22-
boolean voiceNote, boolean quote, @Nullable String caption)
22+
boolean voiceNote, boolean quote, @Nullable String caption) {
23+
this(dataUri, thumbnailUri, contentType, transferState, size, width, height, fileName, fastPreflightId,
24+
voiceNote, quote, caption, -1);
25+
}
26+
27+
public UriAttachment(@NonNull Uri dataUri, @Nullable Uri thumbnailUri,
28+
@NonNull String contentType, int transferState, long size, int width, int height,
29+
@Nullable String fileName, @Nullable String fastPreflightId,
30+
boolean voiceNote, boolean quote, @Nullable String caption, long audioDurationMs)
2331
{
24-
super(contentType, transferState, size, fileName, null, null, null, null, fastPreflightId, voiceNote, width, height, quote, caption, "");
32+
super(contentType, transferState, size, fileName, null, null, null, null, fastPreflightId, voiceNote, width, height, quote, caption, "", audioDurationMs);
2533
this.dataUri = dataUri;
2634
this.thumbnailUri = thumbnailUri;
2735
}

app/src/main/java/org/thoughtcrime/securesms/attachments/MmsNotificationAttachment.java

Lines changed: 0 additions & 36 deletions
This file was deleted.

0 commit comments

Comments
 (0)