2021-06-29 19:58:29 +00:00
|
|
|
// Copyright 2021 Signal Messenger, LLC
|
|
|
|
// SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
2023-02-28 13:07:40 +00:00
|
|
|
import React, { useEffect } from 'react';
|
|
|
|
import { useSelector } from 'react-redux';
|
|
|
|
import type { VoiceNotesPlaybackProps } from '../../components/VoiceNotesPlaybackContext';
|
2023-02-24 23:18:57 +00:00
|
|
|
import { VoiceNotesPlaybackProvider } from '../../components/VoiceNotesPlaybackContext';
|
2023-02-28 13:07:40 +00:00
|
|
|
import { selectAudioPlayerActive } from '../selectors/audioPlayer';
|
2023-03-02 20:55:40 +00:00
|
|
|
import {
|
|
|
|
AudioPlayerContent,
|
|
|
|
useAudioPlayerActions,
|
|
|
|
} from '../ducks/audioPlayer';
|
2023-02-28 13:07:40 +00:00
|
|
|
import { globalMessageAudio } from '../../services/globalMessageAudio';
|
|
|
|
import { strictAssert } from '../../util/assert';
|
2023-09-06 18:06:30 +00:00
|
|
|
import { drop } from '../../util/drop';
|
2023-02-28 13:07:40 +00:00
|
|
|
import * as log from '../../logging/log';
|
2023-05-08 19:59:36 +00:00
|
|
|
import { Sound, SoundType } from '../../util/Sound';
|
2023-02-28 13:07:40 +00:00
|
|
|
import { getConversations } from '../selectors/conversations';
|
|
|
|
import { SeenStatus } from '../../MessageSeenStatus';
|
|
|
|
import { markViewed } from '../ducks/conversations';
|
|
|
|
import * as Errors from '../../types/errors';
|
|
|
|
import { usePrevious } from '../../hooks/usePrevious';
|
|
|
|
|
2023-09-06 18:06:30 +00:00
|
|
|
const stateChangeConfirmDownSound = new Sound({
|
2023-05-08 19:59:36 +00:00
|
|
|
soundType: SoundType.VoiceNoteStart,
|
2023-02-28 13:07:40 +00:00
|
|
|
});
|
2023-09-06 18:06:30 +00:00
|
|
|
const stateChangeConfirmUpSound = new Sound({
|
2023-05-08 19:59:36 +00:00
|
|
|
soundType: SoundType.VoiceNoteEnd,
|
2023-02-28 13:07:40 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Synchronizes the audioPlayer redux state with globalMessageAudio
|
|
|
|
*/
|
|
|
|
export function SmartVoiceNotesPlaybackProvider(
|
|
|
|
props: VoiceNotesPlaybackProps
|
|
|
|
): JSX.Element | null {
|
|
|
|
const active = useSelector(selectAudioPlayerActive);
|
|
|
|
const conversations = useSelector(getConversations);
|
|
|
|
|
|
|
|
const previousStartPosition = usePrevious(undefined, active?.startPosition);
|
|
|
|
|
|
|
|
const content = active?.content;
|
2023-03-02 20:55:40 +00:00
|
|
|
let url: undefined | string;
|
|
|
|
let messageId: undefined | string;
|
|
|
|
let messageIdForLogging: undefined | string;
|
|
|
|
let playNextConsecutiveSound = false;
|
|
|
|
let playFinishConsecutiveSound = false;
|
|
|
|
|
|
|
|
if (content && AudioPlayerContent.isVoiceNote(content)) {
|
|
|
|
({ url, id: messageId } = content.current);
|
|
|
|
messageIdForLogging = content.current.messageIdForLogging;
|
|
|
|
playNextConsecutiveSound = content.isConsecutive;
|
|
|
|
playFinishConsecutiveSound =
|
|
|
|
content.isConsecutive && content.queue.length === 0;
|
|
|
|
}
|
|
|
|
if (content && AudioPlayerContent.isDraft(content)) {
|
|
|
|
url = content.url;
|
|
|
|
}
|
2023-02-28 13:07:40 +00:00
|
|
|
|
|
|
|
const {
|
|
|
|
messageAudioEnded,
|
|
|
|
currentTimeUpdated,
|
|
|
|
durationChanged,
|
|
|
|
unloadMessageAudio,
|
|
|
|
} = useAudioPlayerActions();
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
// if we don't have a new audio source
|
|
|
|
// just control playback
|
2023-03-02 20:55:40 +00:00
|
|
|
if (!content || !url || url === globalMessageAudio.url) {
|
2023-02-28 13:07:40 +00:00
|
|
|
if (!active?.playing && globalMessageAudio.playing) {
|
|
|
|
globalMessageAudio.pause();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (active?.playing && !globalMessageAudio.playing) {
|
|
|
|
globalMessageAudio.play();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (active && active.playbackRate !== globalMessageAudio.playbackRate) {
|
|
|
|
globalMessageAudio.playbackRate = active.playbackRate;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (
|
|
|
|
active &&
|
|
|
|
active.startPosition !== undefined &&
|
2023-03-02 20:55:40 +00:00
|
|
|
active.startPosition !== previousStartPosition &&
|
|
|
|
globalMessageAudio.duration !== undefined
|
2023-02-28 13:07:40 +00:00
|
|
|
) {
|
|
|
|
globalMessageAudio.currentTime =
|
|
|
|
active.startPosition * globalMessageAudio.duration;
|
|
|
|
}
|
2023-03-02 20:55:40 +00:00
|
|
|
|
|
|
|
if (!active?.playing && globalMessageAudio.playing) {
|
|
|
|
globalMessageAudio.pause();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (active?.playing && !globalMessageAudio.playing) {
|
|
|
|
globalMessageAudio.play();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (active && active.playbackRate !== globalMessageAudio.playbackRate) {
|
|
|
|
globalMessageAudio.playbackRate = active.playbackRate;
|
|
|
|
}
|
|
|
|
|
|
|
|
// if user requested a new position
|
|
|
|
if (
|
|
|
|
active &&
|
|
|
|
active.startPosition !== undefined &&
|
|
|
|
active.startPosition !== previousStartPosition &&
|
|
|
|
active.duration
|
|
|
|
) {
|
|
|
|
globalMessageAudio.currentTime = active.startPosition * active.duration;
|
|
|
|
}
|
2023-02-28 13:07:40 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-03-02 20:55:40 +00:00
|
|
|
// if we have a new audio source
|
|
|
|
loadAudio({
|
2023-02-28 13:07:40 +00:00
|
|
|
url,
|
|
|
|
playbackRate: active.playbackRate,
|
2023-03-02 20:55:40 +00:00
|
|
|
messageId,
|
|
|
|
messageIdForLogging,
|
|
|
|
startPosition: active.startPosition,
|
|
|
|
playFinishConsecutiveSound,
|
|
|
|
durationChanged,
|
|
|
|
unloadMessageAudio,
|
|
|
|
currentTimeUpdated,
|
|
|
|
messageAudioEnded,
|
2023-02-28 13:07:40 +00:00
|
|
|
});
|
|
|
|
|
2023-03-02 20:55:40 +00:00
|
|
|
if (playNextConsecutiveSound) {
|
2023-09-06 18:06:30 +00:00
|
|
|
drop(
|
|
|
|
(async () => {
|
|
|
|
await stateChangeConfirmDownSound.play();
|
|
|
|
globalMessageAudio.play();
|
|
|
|
})()
|
|
|
|
);
|
2023-02-28 13:07:40 +00:00
|
|
|
} else {
|
|
|
|
globalMessageAudio.play();
|
|
|
|
}
|
|
|
|
|
2023-03-02 20:55:40 +00:00
|
|
|
if (AudioPlayerContent.isVoiceNote(content)) {
|
|
|
|
if (!content.current.isPlayed) {
|
|
|
|
const message = conversations.messagesLookup[content.current.id];
|
|
|
|
if (message && message.seenStatus !== SeenStatus.Unseen) {
|
|
|
|
markViewed(content.current.id);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
log.info('SmartVoiceNotesPlaybackProvider: message already played', {
|
|
|
|
message: content.current.messageIdForLogging,
|
|
|
|
});
|
2023-02-28 13:07:40 +00:00
|
|
|
}
|
|
|
|
}
|
2023-03-02 20:55:40 +00:00
|
|
|
}, [
|
|
|
|
active,
|
|
|
|
content,
|
|
|
|
conversations.messagesLookup,
|
|
|
|
currentTimeUpdated,
|
|
|
|
durationChanged,
|
|
|
|
messageAudioEnded,
|
|
|
|
messageId,
|
|
|
|
messageIdForLogging,
|
|
|
|
playFinishConsecutiveSound,
|
|
|
|
playNextConsecutiveSound,
|
|
|
|
previousStartPosition,
|
|
|
|
unloadMessageAudio,
|
|
|
|
url,
|
|
|
|
]);
|
2023-02-28 13:07:40 +00:00
|
|
|
|
|
|
|
return <VoiceNotesPlaybackProvider {...props} />;
|
|
|
|
}
|
2023-03-02 20:55:40 +00:00
|
|
|
|
|
|
|
function loadAudio({
|
|
|
|
url,
|
|
|
|
playbackRate,
|
|
|
|
messageId,
|
|
|
|
messageIdForLogging,
|
|
|
|
startPosition,
|
|
|
|
playFinishConsecutiveSound,
|
|
|
|
durationChanged,
|
|
|
|
currentTimeUpdated,
|
|
|
|
messageAudioEnded,
|
|
|
|
unloadMessageAudio,
|
|
|
|
}: {
|
|
|
|
url: string;
|
|
|
|
playbackRate: number;
|
|
|
|
messageId: string | undefined;
|
|
|
|
messageIdForLogging: string | undefined;
|
|
|
|
startPosition: number;
|
|
|
|
playFinishConsecutiveSound: boolean;
|
|
|
|
durationChanged: (value: number | undefined) => void;
|
|
|
|
currentTimeUpdated: (value: number) => void;
|
|
|
|
messageAudioEnded: () => void;
|
|
|
|
unloadMessageAudio: () => void;
|
|
|
|
}) {
|
|
|
|
globalMessageAudio.load({
|
|
|
|
url,
|
|
|
|
playbackRate,
|
|
|
|
onLoadedMetadata() {
|
|
|
|
strictAssert(
|
|
|
|
globalMessageAudio.duration !== undefined,
|
|
|
|
'Audio should have definite duration on `loadedmetadata` event'
|
|
|
|
);
|
|
|
|
log.info(
|
|
|
|
'SmartVoiceNotesPlaybackProvider: `loadedmetadata` event',
|
|
|
|
messageId
|
|
|
|
);
|
|
|
|
if (startPosition !== 0) {
|
|
|
|
globalMessageAudio.currentTime =
|
|
|
|
startPosition * globalMessageAudio.duration;
|
|
|
|
}
|
|
|
|
durationChanged(globalMessageAudio.duration);
|
|
|
|
},
|
|
|
|
onDurationChange() {
|
|
|
|
log.info(
|
|
|
|
'SmartVoiceNotesPlaybackProvider: `durationchange` event',
|
|
|
|
messageId
|
|
|
|
);
|
|
|
|
durationChanged(globalMessageAudio.duration);
|
|
|
|
},
|
|
|
|
onTimeUpdate() {
|
|
|
|
currentTimeUpdated(globalMessageAudio.currentTime);
|
|
|
|
},
|
|
|
|
onEnded() {
|
|
|
|
if (playFinishConsecutiveSound) {
|
2023-09-06 18:06:30 +00:00
|
|
|
drop(stateChangeConfirmUpSound.play());
|
2023-03-02 20:55:40 +00:00
|
|
|
}
|
|
|
|
messageAudioEnded();
|
|
|
|
},
|
|
|
|
onError(error) {
|
|
|
|
log.error(
|
|
|
|
'SmartVoiceNotesPlaybackProvider: playback error',
|
|
|
|
messageIdForLogging,
|
|
|
|
Errors.toLogFormat(error)
|
|
|
|
);
|
|
|
|
unloadMessageAudio();
|
|
|
|
},
|
|
|
|
});
|
|
|
|
}
|