Skip to content

Commit

Permalink
feat: Release source for Web, Linux, Windows (#1517)
Browse files Browse the repository at this point in the history
# Description

Release sources in `Release` mode, when source is `completed`.

## Related Issues

Closes #1537
  • Loading branch information
Gustl22 committed Nov 14, 2023
1 parent fd7fa46 commit c666217
Show file tree
Hide file tree
Showing 11 changed files with 46 additions and 28 deletions.
2 changes: 1 addition & 1 deletion feature_parity_table.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ Note: LLM means Low Latency Mode.
<tr><td>low latency mode</td><td>SDK >=21</td><td>no</td><td>no</td><td>no</td><td>no</td><td>no</td></tr>
<tr><td colspan="7"><strong>Audio Control Commands</strong></td></tr>
<tr><td>resume / pause / stop</td><td>yes</td><td>yes</td><td>yes</td><td>yes</td><td>yes</td><td>yes</td></tr>
<tr><td>release</td><td>yes</td><td>yes (no mode)</td><td>yes (no mode)</td><td>yes (no mode)</td><td>not yet</td><td>not yet</td></tr>
<tr><td>release</td><td>yes</td><td>yes (no mode)</td><td>yes (no mode)</td><td>yes (no mode)</td><td>yes (no mode)</td><td>yes (no mode)</td></tr>
<tr><td>loop</td><td>yes</td><td>yes</td><td>yes</td><td>yes</td><td>yes</td><td>yes</td></tr>
<tr><td>volume</td><td>yes</td><td>yes</td><td>yes</td><td>yes</td><td>yes</td><td>yes</td></tr>
<tr><td>seek</td><td>yes</td><td>yes</td><td>yes</td><td>yes</td><td>yes</td><td>yes</td></tr>
Expand Down
20 changes: 13 additions & 7 deletions packages/audioplayers_linux/linux/audio_player.cc
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,18 @@ void AudioPlayer::SetSourceUrl(std::string url) {
}
}

void AudioPlayer::ReleaseMediaSource() {
if(_isPlaying) _isPlaying = false;
if(_isInitialized) _isInitialized = false;
_url.clear();

GstState playbinState;
gst_element_get_state(playbin, &playbinState, NULL, GST_CLOCK_TIME_NONE);
if(playbinState > GST_STATE_NULL) {
gst_element_set_state(playbin, GST_STATE_NULL);
}
}

gboolean AudioPlayer::OnBusMessage(GstBus *bus, GstMessage *message,
AudioPlayer *data) {
switch (GST_MESSAGE_TYPE(message)) {
Expand Down Expand Up @@ -413,8 +425,7 @@ void AudioPlayer::Resume() {

void AudioPlayer::Dispose() {
if(!playbin) throw "Player was already disposed (Dispose)";
if(_isPlaying) _isPlaying = false;
if(_isInitialized) _isInitialized = false;
ReleaseMediaSource();

g_source_remove(_refreshId);

Expand All @@ -440,11 +451,6 @@ void AudioPlayer::Dispose() {
panorama = nullptr;
}

GstState playbinState;
gst_element_get_state(playbin, &playbinState, NULL, GST_CLOCK_TIME_NONE);
if(playbinState > GST_STATE_NULL) {
gst_element_set_state(playbin, GST_STATE_NULL);
}
gst_object_unref(GST_OBJECT(playbin));
// Do not dispose method channel as it is used by multiple players!
g_clear_object(&_eventChannel);
Expand Down
2 changes: 2 additions & 0 deletions packages/audioplayers_linux/linux/audio_player.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ class AudioPlayer {

void SetSourceUrl(std::string url);

void ReleaseMediaSource();

void OnError(const gchar *code, const gchar *message, FlValue *details,
GError **error);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -139,8 +139,7 @@ static void audioplayers_linux_plugin_handle_method_call(
player->Pause();
player->SetPosition(0);
} else if (strcmp(method, "release") == 0) {
player->Pause();
player->SetPosition(0);
player->ReleaseMediaSource();
} else if (strcmp(method, "seek") == 0) {
auto flPosition = fl_value_lookup_string(args, "position");
int position = flPosition == nullptr ? (int)(player->GetPosition().value_or(0))
Expand Down
22 changes: 9 additions & 13 deletions packages/audioplayers_web/lib/wrapped_player.dart
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ class WrappedPlayer {
}
_currentUrl = url;

stop();
release();
recreateNode();
if (_isPlaying) {
await resume();
Expand Down Expand Up @@ -138,8 +138,11 @@ class WrappedPlayer {
);
_playerEndedSubscription = p.onEnded.listen(
(_) {
_pausedAt = 0;
p.currentTime = 0;
if (_currentReleaseMode == ReleaseMode.release) {
release();
} else {
stop();
}
eventStreamController.add(
const AudioEvent(eventType: AudioEventType.complete),
);
Expand Down Expand Up @@ -167,10 +170,10 @@ class WrappedPlayer {
}

void release() {
stop();
// Release `AudioElement` correctly (#966)
player?.src = '';
player?.remove();
_cancel();
player = null;
_stereoPanner = null;

Expand Down Expand Up @@ -211,7 +214,7 @@ class WrappedPlayer {
}

void stop() {
_cancel();
pause();
_pausedAt = 0;
player?.currentTime = 0;
}
Expand All @@ -225,21 +228,14 @@ class WrappedPlayer {
}
}

void _cancel() {
_isPlaying = false;
player?.pause();
if (_currentReleaseMode == ReleaseMode.release) {
player = null;
}
}

void log(String message) {
eventStreamController.add(
AudioEvent(eventType: AudioEventType.log, logMessage: message),
);
}

Future<void> dispose() async {
release();
eventStreamController.close();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -106,4 +106,4 @@ void MediaEngineExtension::Shutdown()
}
}

} // namespace media
} // namespace media
5 changes: 5 additions & 0 deletions packages/audioplayers_windows/windows/MediaEngineWrapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -339,6 +339,11 @@ void MediaEngineWrapper::SetMediaSource(IMFMediaSource* mediaSource) {
THROW_IF_FAILED(mediaEngineEx->SetSource(source.get()));
}

void MediaEngineWrapper::ReleaseMediaSource() {
m_mediaEngineExtension->SetMediaSource(nullptr);
m_mediaEngine->SetSource(nullptr);
}

// Callback methods

void MediaEngineWrapper::OnLoaded()
Expand Down
3 changes: 3 additions & 0 deletions packages/audioplayers_windows/windows/MediaEngineWrapper.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@ class MediaEngineWrapper : public winrt::implements<MediaEngineWrapper, IUnknown
// Initialize with the provided media source
void SetMediaSource(IMFMediaSource* mediaSource);

// Release media resources
void ReleaseMediaSource();

// Stop playback and cleanup resources
void Pause();
void Shutdown();
Expand Down
10 changes: 8 additions & 2 deletions packages/audioplayers_windows/windows/audio_player.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -195,13 +195,19 @@ void AudioPlayer::SendInitialized() {
}
}

void AudioPlayer::Dispose() {
void AudioPlayer::ReleaseMediaSource() {
if (_isInitialized) {
m_mediaEngineWrapper->Pause();
}
m_mediaEngineWrapper->ReleaseMediaSource();
_isInitialized = false;
}

void AudioPlayer::Dispose() {
ReleaseMediaSource();
m_mediaEngineWrapper->Shutdown();
_methodChannel = nullptr;
_eventHandler = nullptr;
_isInitialized = false;
}

void AudioPlayer::SetLooping(bool isLooping) {
Expand Down
2 changes: 2 additions & 0 deletions packages/audioplayers_windows/windows/audio_player.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ class AudioPlayer {

void Dispose();

void ReleaseMediaSource();

void SetLooping(bool isLooping);

void SetVolume(double volume);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -164,8 +164,7 @@ void AudioplayersWindowsPlugin::HandleMethodCall(
player->Pause();
player->SeekTo(0);
} else if (method_call.method_name().compare("release") == 0) {
player->Pause();
player->SeekTo(0);
player->ReleaseMediaSource();
} else if (method_call.method_name().compare("seek") == 0) {
auto positionInMs = GetArgument<int>("position", args,
(int)ConvertSecondsToMs(player->GetPosition()));
Expand Down

0 comments on commit c666217

Please sign in to comment.