Buenas.
Tengo un desarrollo, desde hace tiempo, que reproduce videos en un Raspberrry. Después de mucho buscar y probar me decidí a partir de unos fuentes que tiran directo de la GPU del RPY, porque otras soluciones que probé no reproducían al instante el video.
El problema es que ese fuente, es muy críptico, casi indescifrable en cuanto a la manera de atacar a la GPU. Después de muchas pruebas, metí parte de mi código en el bucle principal que alimenta de datos a la GPU. Funciona bastante bien, pero no puedo terminar la reproducción del video al instante o se queda colgado, me toca dejarle al menos un par de segundos y ese tiempo no siempre es igual para todos los videos, depende del tamaño del video.
Hoy, mirando cosas por Google, me he encontrado unos fuentes en Python que usan otra táctica, que me ha parecido interesante. El programa que reproduce el video es independiente y no está modificado, lo que hacen es llamar a ese programa externo desde el programa principal, y cuando lo quieren cancelar lanzan un "kill -9" más el PID del programa para matar el proceso.
Mi duda es de donde sacan el PID del proceso, a mi me vendría muy bien usar la misma técnica, si funciona, pero lo haría desde C++. Lanzo el video con una llamada a ese programa y cuando lo quiera parar (normalmente porque quiero reproducir otro video antes de que ese acabe), le hago un Kill al proceso.
Estos son parte de los fuentes en Python. ¿ Sabeis de donde está saliendo el PID del proceso al que luego se le aplica el KILL -9 ?.
Bucle del programa principal:
def run(self):
"""Main program loop. Will never return!"""
# Get playlist of movies to play from file reader.
playlist = self._build_playlist()
self._prepare_to_run_playlist(playlist)
# Main loop to play videos in the playlist and listen for file changes.
while self._running:
# Load and play a new movie if nothing is playing.
if not self._player.is_playing():
movie = playlist.get_next()
if movie is not None:
# Start playing the first available movie.
self._print('Playing movie: {0}'.format(movie))
self._player.play(movie, loop=playlist.length() == 1, vol = self._sound_vol)
# Check for changes in the file search path (like USB drives added)
# and rebuild the playlist.
if self._reader.is_changed():
self._player.stop(3) # Up to 3 second delay waiting for old
# player to stop.
# Rebuild playlist and show countdown again (if OSD enabled).
playlist = self._build_playlist()
self._prepare_to_run_playlist(playlist)
# Give the CPU some time to do other tasks.
time.sleep(0.002)
Rutina, en la que se mata el proceso con un Kill -9:
def stop(self, block_timeout_sec=None):
"""Stop the video player. block_timeout_sec is how many seconds to
block waiting for the player to stop before moving on.
"""
# Stop the player if it's running.
if self._process is not None and self._process.returncode is None:
# process.kill() doesn't seem to work reliably if USB drive is
# removed, instead just run a kill -9 on it.
subprocess.call(['kill', '-9', str(self._process.pid)])
# If a blocking timeout was specified, wait up to that amount of time
# for the process to stop.
start = time.time()
while self._process is not None and self._process.returncode is None:
if (time.time() - start) >= block_timeout_sec:
break
time.sleep(0)
# Let the process be garbage collected.
self._process = None