:right-sidebar: True MediaStream =================================================================== .. currentmodule:: gi.repository.Gtk .. class:: MediaStream(**properties: ~typing.Any) :no-contents-entry: Superclasses: :class:`~gi.repository.GObject.Object` Subclasses: :class:`~gi.repository.Gtk.MediaFile` Implemented Interfaces: :class:`~gi.repository.Gdk.Paintable` ``GtkMediaStream`` is the integration point for media playback inside GTK. GTK provides an implementation of the ``GtkMediaStream`` interface that is called :obj:`~gi.repository.Gtk.MediaFile`. Apart from application-facing API for stream playback, ``GtkMediaStream`` has a number of APIs that are only useful for implementations and should not be used in applications: :obj:`~gi.repository.Gtk.MediaStream.prepared`, :obj:`~gi.repository.Gtk.MediaStream.unprepared`, :obj:`~gi.repository.Gtk.MediaStream.update`, :obj:`~gi.repository.Gtk.MediaStream.ended`, :obj:`~gi.repository.Gtk.MediaStream.seek_success`, :obj:`~gi.repository.Gtk.MediaStream.seek_failed`, :obj:`~gi.repository.Gtk.MediaStream.gerror`, :obj:`~gi.repository.Gtk.MediaStream.error`, :obj:`~gi.repository.Gtk.MediaStream.error_valist`. Methods ------- .. rst-class:: interim-class .. class:: MediaStream :no-index: .. method:: gerror(error: ~gi.repository.GLib.GError) -> None Sets ``self`` into an error state. This will pause the stream (you can check for an error via :obj:`~gi.repository.Gtk.MediaStream.get_error` in your GtkMediaStream.pause() implementation), abort pending seeks and mark the stream as prepared. if the stream is already in an error state, this call will be ignored and the existing error will be retained. To unset an error, the stream must be reset via a call to :obj:`~gi.repository.Gtk.MediaStream.unprepared`. :param error: the ``GError`` to set .. method:: get_duration() -> int Gets the duration of the stream. If the duration is not known, 0 will be returned. .. method:: get_ended() -> bool Returns whether the streams playback is finished. .. method:: get_error() -> ~gi.repository.GLib.GError | None If the stream is in an error state, returns the ``GError`` explaining that state. Any type of error can be reported here depending on the implementation of the media stream. A media stream in an error cannot be operated on, calls like :obj:`~gi.repository.Gtk.MediaStream.play` or :obj:`~gi.repository.Gtk.MediaStream.seek` will not have any effect. ``GtkMediaStream`` itself does not provide a way to unset an error, but implementations may provide options. For example, a :obj:`~gi.repository.Gtk.MediaFile` will unset errors when a new source is set, e.g. with :obj:`~gi.repository.Gtk.MediaFile.set_file`. .. method:: get_loop() -> bool Returns whether the stream is set to loop. See :obj:`~gi.repository.Gtk.MediaStream.set_loop` for details. .. method:: get_muted() -> bool Returns whether the audio for the stream is muted. See :obj:`~gi.repository.Gtk.MediaStream.set_muted` for details. .. method:: get_playing() -> bool Return whether the stream is currently playing. .. method:: get_timestamp() -> int Returns the current presentation timestamp in microseconds. .. method:: get_volume() -> float Returns the volume of the audio for the stream. See :obj:`~gi.repository.Gtk.MediaStream.set_volume` for details. .. method:: has_audio() -> bool Returns whether the stream has audio. .. method:: has_video() -> bool Returns whether the stream has video. .. method:: is_prepared() -> bool Returns whether the stream has finished initializing. At this point the existence of audio and video is known. .. method:: is_seekable() -> bool Checks if a stream may be seekable. This is meant to be a hint. Streams may not allow seeking even if this function returns :const:`True`. However, if this function returns :const:`False`, streams are guaranteed to not be seekable and user interfaces may hide controls that allow seeking. It is allowed to call :obj:`~gi.repository.Gtk.MediaStream.seek` on a non-seekable stream, though it will not do anything. .. method:: is_seeking() -> bool Checks if there is currently a seek operation going on. .. method:: pause() -> None Pauses playback of the stream. If the stream is not playing, do nothing. .. method:: play() -> None Starts playing the stream. If the stream is in error or already playing, do nothing. .. method:: realize(surface: ~gi.repository.Gdk.Surface) -> None Called by users to attach the media stream to a ``GdkSurface`` they manage. The stream can then access the resources of ``surface`` for its rendering purposes. In particular, media streams might want to create a ``GdkGLContext`` or sync to the ``GdkFrameClock``. Whoever calls this function is responsible for calling :obj:`~gi.repository.Gtk.MediaStream.unrealize` before either the stream or ``surface`` get destroyed. Multiple calls to this function may happen from different users of the video, even with the same ``surface``. Each of these calls must be followed by its own call to :obj:`~gi.repository.Gtk.MediaStream.unrealize`. It is not required to call this function to make a media stream work. :param surface: a ``GdkSurface`` .. method:: seek(timestamp: int) -> None Start a seek operation on ``self`` to ``timestamp``. If ``timestamp`` is out of range, it will be clamped. Seek operations may not finish instantly. While a seek operation is in process, the :obj:`~gi.repository.Gtk.MediaStream.props.seeking` property will be set. When calling :func:`~gi.repository.Gtk.MediaStream.seek` during an ongoing seek operation, the new seek will override any pending seek. :param timestamp: timestamp to seek to. .. method:: seek_failed() -> None Ends a seek operation started via GtkMediaStream.seek() as a failure. This will not cause an error on the stream and will assume that playback continues as if no seek had happened. See :obj:`~gi.repository.Gtk.MediaStream.seek_success` for the other way of ending a seek. .. method:: seek_success() -> None Ends a seek operation started via GtkMediaStream.seek() successfully. This function will unset the GtkMediaStream:ended property if it was set. See :obj:`~gi.repository.Gtk.MediaStream.seek_failed` for the other way of ending a seek. .. method:: set_loop(loop: bool) -> None Sets whether the stream should loop. In this case, it will attempt to restart playback from the beginning instead of stopping at the end. Not all streams may support looping, in particular non-seekable streams. Those streams will ignore the loop setting and just end. :param loop: :const:`True` if the stream should loop .. method:: set_muted(muted: bool) -> None Sets whether the audio stream should be muted. Muting a stream will cause no audio to be played, but it does not modify the volume. This means that muting and then unmuting the stream will restore the volume settings. If the stream has no audio, calling this function will still work but it will not have an audible effect. :param muted: :const:`True` if the stream should be muted .. method:: set_playing(playing: bool) -> None Starts or pauses playback of the stream. :param playing: whether to start or pause playback .. method:: set_volume(volume: float) -> None Sets the volume of the audio stream. This function call will work even if the stream is muted. The given ``volume`` should range from 0.0 for silence to 1.0 for as loud as possible. Values outside of this range will be clamped to the nearest value. If the stream has no audio or is muted, calling this function will still work but it will not have an immediate audible effect. When the stream is unmuted, the new volume setting will take effect. :param volume: New volume of the stream from 0.0 to 1.0 .. method:: stream_ended() -> None Pauses the media stream and marks it as ended. This is a hint only, calls to :obj:`~gi.repository.Gtk.MediaStream.play` may still happen. The media stream must be prepared when this function is called. .. versionadded:: 4.4 .. method:: stream_prepared(has_audio: bool, has_video: bool, seekable: bool, duration: int) -> None Called by ``GtkMediaStream`` implementations to advertise the stream being ready to play and providing details about the stream. Note that the arguments are hints. If the stream implementation cannot determine the correct values, it is better to err on the side of caution and return :const:`True`. User interfaces will use those values to determine what controls to show. This function may not be called again until the stream has been reset via :obj:`~gi.repository.Gtk.MediaStream.stream_unprepared`. .. versionadded:: 4.4 :param has_audio: :const:`True` if the stream should advertise audio support :param has_video: :const:`True` if the stream should advertise video support :param seekable: :const:`True` if the stream should advertise seekability :param duration: The duration of the stream or 0 if unknown .. method:: stream_unprepared() -> None Resets a given media stream implementation. :obj:`~gi.repository.Gtk.MediaStream.stream_prepared` can then be called again. This function will also reset any error state the stream was in. .. versionadded:: 4.4 .. method:: unrealize(surface: ~gi.repository.Gdk.Surface) -> None Undoes a previous call to :func:`~gi.repository.Gtk.MediaStream.realize`. This causes the stream to release all resources it had allocated from ``surface``. :param surface: the ``GdkSurface`` the stream was realized with .. method:: update(timestamp: int) -> None Media stream implementations should regularly call this function to update the timestamp reported by the stream. It is up to implementations to call this at the frequency they deem appropriate. The media stream must be prepared when this function is called. :param timestamp: the new timestamp Properties ---------- .. rst-class:: interim-class .. class:: MediaStream :no-index: .. attribute:: props.duration :type: int The stream's duration in microseconds or 0 if unknown. .. attribute:: props.ended :type: bool Set when playback has finished. .. attribute:: props.error :type: ~gi.repository.GLib.GError :const:`None` for a properly working stream or the ``GError`` that the stream is in. .. attribute:: props.has_audio :type: bool Whether the stream contains audio. .. attribute:: props.has_video :type: bool Whether the stream contains video. .. attribute:: props.loop :type: bool Try to restart the media from the beginning once it ended. .. attribute:: props.muted :type: bool Whether the audio stream should be muted. .. attribute:: props.playing :type: bool Whether the stream is currently playing. .. attribute:: props.prepared :type: bool Whether the stream has finished initializing and existence of audio and video is known. .. attribute:: props.seekable :type: bool Set unless the stream is known to not support seeking. .. attribute:: props.seeking :type: bool Set while a seek is in progress. .. attribute:: props.timestamp :type: int The current presentation timestamp in microseconds. .. attribute:: props.volume :type: float Volume of the audio stream. Virtual Methods --------------- .. rst-class:: interim-class .. class:: MediaStream :no-index: .. method:: do_pause() -> None Pauses playback of the stream. If the stream is not playing, do nothing. .. method:: do_play() -> bool .. method:: do_realize(surface: ~gi.repository.Gdk.Surface) -> None Called by users to attach the media stream to a ``GdkSurface`` they manage. The stream can then access the resources of ``surface`` for its rendering purposes. In particular, media streams might want to create a ``GdkGLContext`` or sync to the ``GdkFrameClock``. Whoever calls this function is responsible for calling :obj:`~gi.repository.Gtk.MediaStream.unrealize` before either the stream or ``surface`` get destroyed. Multiple calls to this function may happen from different users of the video, even with the same ``surface``. Each of these calls must be followed by its own call to :obj:`~gi.repository.Gtk.MediaStream.unrealize`. It is not required to call this function to make a media stream work. :param surface: a ``GdkSurface`` .. method:: do_seek(timestamp: int) -> None Start a seek operation on ``self`` to ``timestamp``. If ``timestamp`` is out of range, it will be clamped. Seek operations may not finish instantly. While a seek operation is in process, the :obj:`~gi.repository.Gtk.MediaStream.props.seeking` property will be set. When calling :func:`~gi.repository.Gtk.MediaStream.seek` during an ongoing seek operation, the new seek will override any pending seek. :param timestamp: timestamp to seek to. .. method:: do_unrealize(surface: ~gi.repository.Gdk.Surface) -> None Undoes a previous call to :func:`~gi.repository.Gtk.MediaStream.realize`. This causes the stream to release all resources it had allocated from ``surface``. :param surface: the ``GdkSurface`` the stream was realized with .. method:: do_update_audio(muted: bool, volume: float) -> None :param muted: :param volume: Fields ------ .. rst-class:: interim-class .. class:: MediaStream :no-index: .. attribute:: parent_instance