<html><head></head><body>Nit: I don't think it was " racy". A live loop is not a race condition - the result does not vary based on a race.<br><br><div class="gmail_quote">Le 20 mai 2019 18:35:11 GMT+03:00, Romain Vimont <rom1v@videolabs.io> a écrit :<blockquote class="gmail_quote" style="margin: 0pt 0pt 0pt 0.8ex; border-left: 1px solid rgb(204, 204, 204); padding-left: 1ex;">
<pre class="k9mail">In order to wait for a specific state, libvlc player tests used live<br>loops to read the player state.<br><br>Listen to state changes instead.<hr> test/Makefile.am | 2 +-<br> test/libvlc/media_player.c | 85 ++++++++++++++++++++++++--------------<br> 2 files changed, 55 insertions(+), 32 deletions(-)<br><br>diff --git a/test/Makefile.am b/test/Makefile.am<br>index 8343bea501..625f8d3453 100644<br>--- a/test/Makefile.am<br>+++ b/test/Makefile.am<br>@@ -98,7 +98,7 @@ test_libvlc_media_list_player_LDADD = $(LIBVLC)<br> test_libvlc_media_list_SOURCES = libvlc/media_list.c<br> test_libvlc_media_list_LDADD = $(LIBVLC)<br> test_libvlc_media_player_SOURCES = libvlc/media_player.c<br>-test_libvlc_media_player_LDADD = $(LIBVLC)<br>+test_libvlc_media_player_LDADD = $(LIBVLCCORE) $(LIBVLC)<br> test_libvlc_media_discoverer_SOURCES = libvlc/media_discoverer.c<br> test_libvlc_media_discoverer_LDADD = $(LIBVLC)<br> test_libvlc_renderer_discoverer_SOURCES = libvlc/renderer_discoverer.c<br>diff --git a/test/libvlc/media_player.c b/test/libvlc/media_player.c<br>index 5417fd7561..2489e6f5fa 100644<br>--- a/test/libvlc/media_player.c<br>+++ b/test/libvlc/media_player.c<br>@@ -21,30 +21,61 @@<br> **********************************************************************/<br> <br> #include "test.h"<br>+#include <vlc_common.h><br> <br>-static void wait_playing(libvlc_media_player_t *mp)<br>+static void on_event(const struct libvlc_event_t *event, void *data)<br> {<br>- libvlc_state_t state;<br>- do {<br>- state = libvlc_media_player_get_state (mp);<br>- } while(state != libvlc_Playing &&<br>- state != libvlc_Error &&<br>- state != libvlc_Ended );<br>-<br>- state = libvlc_media_player_get_state (mp);<br>- assert(state == libvlc_Playing || state == libvlc_Ended);<br>+ (void) event;<br>+ vlc_sem_t *sem = data;<br>+ vlc_sem_post(sem);<br> }<br> <br>-static void wait_paused(libvlc_media_player_t *mp)<br>+static void play_and_wait(libvlc_media_player_t *mp)<br> {<br>- libvlc_state_t state;<br>- do {<br>- state = libvlc_media_player_get_state (mp);<br>- } while(state != libvlc_Paused &&<br>- state != libvlc_Ended );<br>-<br>- state = libvlc_media_player_get_state (mp);<br>- assert(state == libvlc_Paused || state == libvlc_Ended);<br>+ libvlc_event_manager_t *em = libvlc_media_player_event_manager(mp);<br>+<br>+ vlc_sem_t sem;<br>+ vlc_sem_init(&sem, 0);<br>+<br>+ int res;<br>+ res = libvlc_event_attach(em, libvlc_MediaPlayerPlaying, on_event, &sem);<br>+ assert(!res);<br>+<br>+ libvlc_media_player_play(mp);<br>+<br>+ test_log("Waiting for playing\n");<br>+ vlc_sem_wait(&sem);<br>+<br>+ libvlc_event_detach(em, libvlc_MediaPlayerPlaying, on_event, &sem);<br>+<br>+ vlc_sem_destroy(&sem);<br>+}<br>+<br>+static void pause_and_wait(libvlc_media_player_t *mp)<br>+{<br>+ libvlc_event_manager_t *em = libvlc_media_player_event_manager(mp);<br>+<br>+ vlc_sem_t sem;<br>+ vlc_sem_init(&sem, 0);<br>+<br>+ int res;<br>+ res = libvlc_event_attach(em, libvlc_MediaPlayerPaused, on_event, &sem);<br>+ assert(!res);<br>+ res = libvlc_event_attach(em, libvlc_MediaPlayerEndReached, on_event, &sem);<br>+ assert(!res);<br>+<br>+ libvlc_media_player_set_pause(mp, true);<br>+<br>+ /* the end may have been already reached before attaching the event */<br>+ if (libvlc_media_player_get_state(mp) == libvlc_Playing)<br>+ {<br>+ test_log("Waiting for pause\n");<br>+ vlc_sem_wait(&sem);<br>+ }<br>+<br>+ vlc_sem_destroy(&sem);<br>+ libvlc_event_detach(em, libvlc_MediaPlayerPaused, on_event, &sem);<br>+ libvlc_event_detach(em, libvlc_MediaPlayerEndReached, on_event, &sem);<br> }<br> <br> /* Test a bunch of A/V properties. This most does nothing since the current<br>@@ -111,9 +142,7 @@ static void test_media_player_set_media(const char** argv, int argc)<br> <br> libvlc_media_release (md);<br> <br>- libvlc_media_player_play (mp);<br>-<br>- wait_playing (mp);<br>+ play_and_wait(mp);<br> <br> libvlc_media_player_stop (mp);<br> libvlc_media_player_release (mp);<br>@@ -140,9 +169,7 @@ static void test_media_player_play_stop(const char** argv, int argc)<br> <br> libvlc_media_release (md);<br> <br>- libvlc_media_player_play (mi);<br>-<br>- wait_playing (mi);<br>+ play_and_wait(mi);<br> <br> libvlc_media_player_stop (mi);<br> libvlc_media_player_release (mi);<br>@@ -172,14 +199,10 @@ static void test_media_player_pause_stop(const char** argv, int argc)<br> test_audio_video(mi);<br> test_role(mi);<br> <br>- libvlc_media_player_play (mi);<br>- test_log ("Waiting for playing\n");<br>- wait_playing (mi);<br>+ play_and_wait(mi);<br> test_audio_video(mi);<br> <br>- libvlc_media_player_set_pause (mi, true);<br>- test_log ("Waiting for pause\n");<br>- wait_paused (mi);<br>+ pause_and_wait(mi);<br> test_audio_video(mi);<br> <br> libvlc_media_player_stop (mi);</pre></blockquote></div><br>-- <br>Envoyé de mon appareil Android avec Courriel K-9 Mail. Veuillez excuser ma brièveté.</body></html>