Skip to content

Commit

Permalink
Fix sending voice messages
Browse files Browse the repository at this point in the history
  • Loading branch information
tulir committed Dec 26, 2023
1 parent dd77f9d commit da87980
Showing 1 changed file with 12 additions and 28 deletions.
40 changes: 12 additions & 28 deletions portal.go
Original file line number Diff line number Diff line change
Expand Up @@ -615,28 +615,6 @@ func convertVideo(ctx context.Context, mimeType string, video []byte) (string, [
return outMimeType, outVideo, nil
}

func convertAudio(ctx context.Context, mimeType string, audio []byte) (string, []byte, error) {
var outMimeType string
var outAudio []byte
var err error
switch mimeType {
case "audio/aac", "audio/mp4", "audio/amr", "audio/mpeg", "audio/ogg; codecs=opus":
// Allowed
outMimeType = mimeType
outAudio = audio
case "audio/ogg":
// Hopefully it's opus already
outMimeType = "audio/ogg; codecs=opus"
outAudio = audio
default:
return "", nil, fmt.Errorf("%w %q in audio message", errMediaUnsupportedType, mimeType)
}
if err != nil {
return "", nil, fmt.Errorf("%w (%s to %s)", errMediaConvertFailed, mimeType, "video/mp4")
}
return outMimeType, outAudio, nil
}

func (portal *Portal) convertMatrixMessage(ctx context.Context, sender *User, evt *event.Event) (*signalmeow.SignalContent, error) {
content, ok := evt.Content.Parsed.(*event.MessageEventContent)
if !ok {
Expand Down Expand Up @@ -755,19 +733,25 @@ func (portal *Portal) convertMatrixMessage(ctx context.Context, sender *User, ev
fileName = content.FileName
caption, ranges = matrixfmt.Parse(matrixFormatParams, content)
}
image, err := portal.downloadAndDecryptMatrixMedia(ctx, content)
data, err := portal.downloadAndDecryptMatrixMedia(ctx, content)
if err != nil {
return nil, err
}
newMimeType, convertedAudio, err := convertAudio(ctx, content.GetInfo().MimeType, image)
if err != nil {
return nil, err
_, isVoice := evt.Content.Raw["org.matrix.msc3245.voice"]
mime := content.GetInfo().MimeType
if isVoice {
data, err = ffmpeg.ConvertBytes(ctx, data, ".m4a", []string{}, []string{"-c:a", "aac"}, mime)
if err != nil {
return nil, err
}
mime = "audio/aac"
fileName += ".m4a"
}
attachmentPointer, err := signalmeow.UploadAttachment(sender.SignalDevice, convertedAudio, newMimeType, fileName)
attachmentPointer, err := signalmeow.UploadAttachment(sender.SignalDevice, data, mime, fileName)
if err != nil {
return nil, err
}
if _, isVoice := evt.Content.Raw["org.matrix.msc3245.voice"]; isVoice {
if isVoice {
attachmentPointer.Flags = proto.Uint32(uint32(signalpb.AttachmentPointer_VOICE_MESSAGE))
}
outgoingMessage = signalmeow.DataMessageForAttachment(attachmentPointer, caption, ranges)
Expand Down

0 comments on commit da87980

Please sign in to comment.