def get_server_info(self):
        p.pa_threaded_mainloop_lock(self.main_loop)

        o = p.pa_context_get_server_info(self.ctx, self.server_info_cb, None)

        while p.pa_operation_get_state(o) == p.PA_OPERATION_RUNNING:
            p.pa_threaded_mainloop_wait(self.main_loop)

        p.pa_operation_unref(o)

        p.pa_threaded_mainloop_unlock(self.main_loop)
    def find_sources(self):
        p.pa_threaded_mainloop_lock(self.main_loop)

        o = p.pa_context_get_source_info_list(self.ctx, self.source_info_cb,
                                              1)  # 1 for new

        while p.pa_operation_get_state(o) == p.PA_OPERATION_RUNNING:
            p.pa_threaded_mainloop_wait(self.main_loop)

        p.pa_operation_unref(o)

        p.pa_threaded_mainloop_unlock(self.main_loop)
    def load_source_info(self, name):
        p.pa_threaded_mainloop_lock(self.main_loop)

        o = p.pa_context_get_source_info_by_name(self.ctx, name.encode(),
                                                 self.source_info_cb, None)

        while p.pa_operation_get_state(o) == p.PA_OPERATION_RUNNING:
            p.pa_threaded_mainloop_wait(self.main_loop)

        p.pa_operation_unref(o)

        p.pa_threaded_mainloop_unlock(self.main_loop)
    def unload_module(self, module):
        user_data = p.ctx_success_cb_data()

        user_data.operation = 'unload_module'.encode('utf-8')

        p.pa_threaded_mainloop_lock(self.main_loop)

        o = p.pa_context_unload_module(self.ctx, module, self.ctx_success_cb,
                                       p.get_ref(user_data))

        while p.pa_operation_get_state(o) == p.PA_OPERATION_RUNNING:
            p.pa_threaded_mainloop_wait(self.main_loop)

        p.pa_operation_unref(o)

        p.pa_threaded_mainloop_unlock(self.main_loop)
    def drain_ctx(self):
        o = p.pa_context_drain(self.ctx, self.ctx_drain_notify_cb, None)

        if o:
            p.pa_threaded_mainloop_lock(self.main_loop)

            while p.pa_operation_get_state(o) == p.PA_OPERATION_RUNNING:
                p.pa_threaded_mainloop_wait(self.main_loop)

            p.pa_threaded_mainloop_unlock(self.main_loop)

            self.log.debug(self.log_tag + 'drained pulseaudio context')

            p.pa_operation_unref(o)
        else:
            self.log.debug(self.log_tag + 'context is already drained')
    def load_sink(self, name, description, rate):
        self.sink_is_loaded = False

        self.load_sink_info(name)

        if not self.sink_is_loaded:
            args = []

            sink_properties = description
            sink_properties += 'device.class=\'sound\''

            args.append('sink_name=' + name)
            args.append('sink_properties=' + sink_properties)
            args.append('channels=2')
            args.append('rate=' + str(rate))

            argument = ' '.join(map(str, args)).encode('ascii')

            module = b'module-null-sink'

            p.pa_threaded_mainloop_lock(self.main_loop)

            o = p.pa_context_load_module(self.ctx, module, argument,
                                         self.module_idx_cb, None)

            while p.pa_operation_get_state(o) == p.PA_OPERATION_RUNNING:
                p.pa_threaded_mainloop_wait(self.main_loop)

            p.pa_operation_unref(o)

            p.pa_threaded_mainloop_unlock(self.main_loop)

            self.load_sink_info(name)  # checking if sink was loaded

            if self.sink_is_loaded:
                return True
            else:
                return False
        else:
            return True