<commit_before>import pygst
pygst.require('0.10')
import gst
import gobject

import logging

from pykka.actor import ThreadingActor
from pykka.registry import ActorRegistry

from mopidy import settings, utils
from mopidy.backends.base import Backend
from mopidy.utils import process

# Trigger install of gst mixer plugins
from mopidy import mixers

logger = logging.getLogger('mopidy.gstreamer')


class GStreamer(ThreadingActor):
    """
    Audio output through `GStreamer <http://gstreamer.freedesktop.org/>`_.

    **Settings:**

    - :attr:`mopidy.settings.OUTPUT`
    - :attr:`mopidy.settings.MIXER`
    - :attr:`mopidy.settings.MIXER_TRACK`

    """

    def __init__(self):
        super(GStreamer, self).__init__()

        self._default_caps = gst.Caps("""
            audio/x-raw-int,
            endianness=(int)1234,
            channels=(int)2,
            width=(int)16,
            depth=(int)16,
            signed=(boolean)true,
            rate=(int)44100""")

        self._pipeline = None
        self._source = None
        self._uridecodebin = None
        self._output = None
        self._mixer = None

        self._message_processor_set_up = False

    def on_start(self):
        try:
            self._setup_pipeline()
            self._setup_output()
            self._setup_mixer()
            self._setup_message_processor()
        except gobject.GError as ex:
            logger.exception(ex)
            process.exit_process()

    def on_stop(self):
        self._teardown_message_processor()
        self._teardown_mixer()
        self._teardown_pipeline()

    def _setup_pipeline(self):
        # TODO: replace with and input bin so we simply have an input bin we
        # connect to an output bin with a mixer on the side. set_uri on bin?
        description = ' ! '.join([
            'uridecodebin name=uri',
            'audioconvert name=convert',
            'audioresample name=resample',
            'queue name=queue'])

        logger.debug(u'Setting up base GStreamer pipeline: %s', description)

        self._pipeline = gst.parse_launch(description)
        self._uridecodebin = self._pipeline.get_by_name('uri')

        self._uridecodebin.connect('notify::source', self._on_new_source)
        self._uridecodebin.connect('pad-added', self._on_new_pad,
            self._pipeline.get_by_name('queue').get_pad('sink'))

    def _teardown_pipeline(self):
        self._pipeline.set_state(gst.STATE_NULL)

    def _setup_output(self):
        try:
            self._output = gst.parse_bin_from_description(
                settings.OUTPUT, ghost_unconnected_pads=True)
        except gobject.GError as ex:
            logger.error('Failed to create output "%s": %s',
                settings.OUTPUT, ex)
            process.exit_process()
            return

        self._pipeline.add(self._output)
        gst.element_link_many(self._pipeline.get_by_name('queue'),
                              self._output)
        logger.info('Output set to %s', settings.OUTPUT)

    def _setup_mixer(self):
        if not settings.MIXER:
            logger.info('Not setting up mixer.')
            return

        try:
            mixerbin = gst.parse_bin_from_description(settings.MIXER, False)
        except gobject.GError as ex:
            logger.warning('Failed to create mixer "%s": %s',
                settings.MIXER, ex)
            return

        # We assume that the bin will contain a single mixer.
        mixer = mixerbin.get_by_interface('GstMixer')
        if not mixer:
            logger.warning('Did not find any mixers in %r', settings.MIXER)
            return

        if mixerbin.set_state(gst.STATE_READY) != gst.STATE_CHANGE_SUCCESS:
            logger.warning('Setting mixer %r to READY failed.', settings.MIXER)
            return

        track = self._select_mixer_track(mixer, settings.MIXER_TRACK)
        if not track:
            logger.warning('Could not find usable mixer track.')
            return

        self._mixer = (mixer, track)
        logger.info('Mixer set to %s using track called %s',<commit_msg>Use kwarg to make meaning obvious<commit_after>import pygst
pygst.require('0.10')
import gst
import gobject

import logging

from pykka.actor import ThreadingActor
from pykka.registry import ActorRegistry

from mopidy import settings, utils
from mopidy.backends.base import Backend
from mopidy.utils import process

# Trigger install of gst mixer plugins
from mopidy import mixers

logger = logging.getLogger('mopidy.gstreamer')


class GStreamer(ThreadingActor):
    """
    Audio output through `GStreamer <http://gstreamer.freedesktop.org/>`_.

    **Settings:**

    - :attr:`mopidy.settings.OUTPUT`
    - :attr:`mopidy.settings.MIXER`
    - :attr:`mopidy.settings.MIXER_TRACK`

    """

    def __init__(self):
        super(GStreamer, self).__init__()

        self._default_caps = gst.Caps("""
            audio/x-raw-int,
            endianness=(int)1234,
            channels=(int)2,
            width=(int)16,
            depth=(int)16,
            signed=(boolean)true,
            rate=(int)44100""")

        self._pipeline = None
        self._source = None
        self._uridecodebin = None
        self._output = None
        self._mixer = None

        self._message_processor_set_up = False

    def on_start(self):
        try:
            self._setup_pipeline()
            self._setup_output()
            self._setup_mixer()
            self._setup_message_processor()
        except gobject.GError as ex:
            logger.exception(ex)
            process.exit_process()

    def on_stop(self):
        self._teardown_message_processor()
        self._teardown_mixer()
        self._teardown_pipeline()

    def _setup_pipeline(self):
        # TODO: replace with and input bin so we simply have an input bin we
        # connect to an output bin with a mixer on the side. set_uri on bin?
        description = ' ! '.join([
            'uridecodebin name=uri',
            'audioconvert name=convert',
            'audioresample name=resample',
            'queue name=queue'])

        logger.debug(u'Setting up base GStreamer pipeline: %s', description)

        self._pipeline = gst.parse_launch(description)
        self._uridecodebin = self._pipeline.get_by_name('uri')

        self._uridecodebin.connect('notify::source', self._on_new_source)
        self._uridecodebin.connect('pad-added', self._on_new_pad,
            self._pipeline.get_by_name('queue').get_pad('sink'))

    def _teardown_pipeline(self):
        self._pipeline.set_state(gst.STATE_NULL)

    def _setup_output(self):
        try:
            self._output = gst.parse_bin_from_description(
                settings.OUTPUT, ghost_unconnected_pads=True)
        except gobject.GError as ex:
            logger.error('Failed to create output "%s": %s',
                settings.OUTPUT, ex)
            process.exit_process()
            return

        self._pipeline.add(self._output)
        gst.element_link_many(self._pipeline.get_by_name('queue'),
                              self._output)
        logger.info('Output set to %s', settings.OUTPUT)

    def _setup_mixer(self):
        if not settings.MIXER:
            logger.info('Not setting up mixer.')
            return

        try:
            mixerbin = gst.parse_bin_from_description(settings.MIXER,
                ghost_unconnected_pads=False)
        except gobject.GError as ex:
            logger.warning('Failed to create mixer "%s": %s',
                settings.MIXER, ex)
            return

        # We assume that the bin will contain a single mixer.
        mixer = mixerbin.get_by_interface('GstMixer')
        if not mixer:
            logger.warning('Did not find any mixers in %r', settings.MIXER)
            return

        if mixerbin.set_state(gst.STATE_READY) != gst.STATE_CHANGE_SUCCESS:
            logger.warning('Setting mixer %r to READY failed.', settings.MIXER)
            return

        track = self._select_mixer_track(mixer, settings.MIXER_TRACK)
        if not track:
            logger.warning('Could not find usable mixer track.')
            return

        self._mixer = (mixer, track)
        logger.info('Mixer set to %s using track called %s',
{'fraction_non_alphanumeric': 0.09768232010678315, 'fraction_numerical': 0.004125712898920034, 'mean_word_length': 2.224569640062598, 'pattern_counts': {'":': 4, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7206202', 'n_tokens_mistral': 2480, 'n_tokens_neox': 2407, 'n_words': 544}