Skip to content

Commit

Permalink
#849: make it easier to simulate sound packet jitter without affectin…
Browse files Browse the repository at this point in the history
…g anything else, adds XPRA_SOUND_SOURCE_JITTER env var

git-svn-id: https://xpra.org/svn/Xpra/trunk@10231 3bb7dfac-3a0b-4e04-842a-767bc560f471
  • Loading branch information
totaam committed Aug 7, 2015
1 parent a07d8c9 commit 466a553
Showing 1 changed file with 27 additions and 4 deletions.
31 changes: 27 additions & 4 deletions src/xpra/sound/src.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import sys
import time

from xpra.os_util import SIGNAMES
from xpra.os_util import SIGNAMES, Queue
from xpra.sound.sound_pipeline import SoundPipeline, gobject
from xpra.gtk_common.gobject_util import n_arg_signal
from xpra.sound.gstreamer_util import plugin_str, get_encoder_formatter, get_source_plugins, normv, \
Expand All @@ -17,6 +17,7 @@
log = Logger("sound")

APPSINK = os.environ.get("XPRA_SOURCE_APPSINK", "appsink name=sink emit-signals=true max-buffers=10 drop=true sync=false async=false qos=false")
JITTER = int(os.environ.get("XPRA_SOUND_SOURCE_JITTER", "0"))


class SoundSource(SoundPipeline):
Expand Down Expand Up @@ -63,6 +64,8 @@ def __init__(self, src_type=None, src_options={}, codecs=CODECS, codec_options={
self.setup_pipeline_and_bus(pipeline_els)
self.volume = self.pipeline.get_by_name("volume")
self.sink = self.pipeline.get_by_name("sink")
if JITTER>0:
self.jitter_queue = Queue()
try:
#Gst 1.0:
self.sink.connect("new-sample", self.on_new_sample)
Expand Down Expand Up @@ -96,7 +99,7 @@ def emit_buffer1(self, sample):
#info = sample.get_info()
size = buf.get_size()
data = buf.extract_dup(0, size)
return self.do_emit_buffer(data, {"timestamp" : normv(buf.pts),
return self.emit_buffer(data, {"timestamp" : normv(buf.pts),
"duration" : normv(buf.duration),
})

Expand All @@ -122,12 +125,31 @@ def emit_buffer0(self, buf, metadata={}):
# "offset" : buf.offset,
# "offset_end": buf.offset_end}
log("emit buffer: %s bytes, timestamp=%s", len(buf.data), buf.timestamp//MS_TO_NS)
return self.do_emit_buffer(buf.data, {
return self.emit_buffer(buf.data, {
"caps" : buf.get_caps().to_string(),
"timestamp" : normv(buf.timestamp),
"duration" : normv(buf.duration)
})

def emit_buffer(self, data, metadata={}):
if JITTER>0:
#will actually emit the buffer after a random delay
if self.jitter_queue.empty():
#queue was empty, schedule a timer to flush it
from random import randint
jitter = randint(1, JITTER)
from xpra.gtk_common.gobject_compat import import_glib
glib = import_glib()
glib.timeout_add(jitter, self.flush_jitter_queue)
log("emit_buffer: will flush jitter queue in %ims", jitter)
self.jitter_queue.put((data, metadata))
return 0
return self.do_emit_buffer(data, metadata)

def flush_jitter_queue(self):
while not self.jitter_queue.empty():
d,m = self.jitter_queue.get(False)
self.do_emit_buffer(d, m)

def do_emit_buffer(self, data, metadata={}):
self.buffer_count += 1
Expand All @@ -142,7 +164,6 @@ def do_emit_buffer(self, data, metadata={}):


def main():
import glib
from xpra.platform import init, clean
init("Xpra-Sound-Source")
try:
Expand Down Expand Up @@ -203,6 +224,8 @@ def new_buffer(ss, data, metadata):
f.write(data)
f.flush()

from xpra.gtk_common.gobject_compat import import_glib
glib = import_glib()
glib_mainloop = glib.MainLoop()

ss.connect("new-buffer", new_buffer)
Expand Down

0 comments on commit 466a553

Please sign in to comment.