Gstreamer python:在管道运行时添加和删除音频源

我正在研究一个示例python脚本,最初在这里找到:Adding and removing audio sources to/from GStreamer pipeline on-the-go.
目的是制作一个如上所述的脚本,能够在管道运行时插入和删除音频源,但在源和加法器之间有一个audioconvert元素.这是因为在更一般的情况下,Adder希望传入的流具有相同的格式.

所以这是代码;我们创造了2个发电机(蜂鸣器).第一个发出1000Hz音调并等待返回键.第二个是500Hz音调,按键后总和为第一个音调.再次,通过按下返回键,只能听到第二个发生器.

#!/usr/bin/python

import gobject;
gobject.threads_init()
import gst

# THE FOLLOWING FUNCTION IS A REWORK OF THE ORIGINAL, STILL DOING THE JOB

def create_raw_audiotest_signal(pipe, freq, adder):
  # create buzzer of a given freq
  buzzer = gst.element_factory_make("audiotestsrc","buzzer%d" % freq)
  buzzer.set_property("freq",freq)
  pipe.add(buzzer)
  buzzersrc=buzzer.get_pad("src")
  # Gather a request sink pad on the mixer
  sinkpad=adder.get_request_pad("sink%d")
  # .. and connect it to the buzzer
  buzzersrc.link(sinkpad)
  return buzzer, buzzersrc, sinkpad

# THIS IS A MODIFIED VERSION, NOT WORKING, THAT JUST PUTS AN AUDIOCONVERT
# ELEMENT BETWEEN THE GENERATOR AND THE ADDER.

def create_audiotest_signal_with_converter(pipe, freq, adder):
    # create buzzer of a given freq
    buzzer = gst.element_factory_make("audiotestsrc","buzzer%d" % freq)
    buzzer.set_property("freq",freq)
    # add a converter because adder wants inputs with the same format.
    ac = gst.element_factory_make("audioconvert", "ac%d" % freq)
    pipe.add(buzzer, ac)
    # link the buzzer with the converter ...
    buzzer.link(ac)
    buzzersrc=buzzer.get_pad("src")
    # Gather a request sink pad on the mixer
    sinkpad=adder.get_request_pad("sink%d")
    # and then the converter to the adder
    ac.get_pad('src').link(sinkpad)
    return buzzer, buzzersrc, sinkpad

if __name__ == "__main__":
  # First create our pipeline
  pipe = gst.Pipeline("mypipe")

  # Create a software mixer with "Adder"
  adder = gst.element_factory_make("adder","audiomixer")
  pipe.add(adder)

  # Create the first buzzer..
  #buzzer1, buzzersrc1, sinkpad1 = create_raw_audiotest_signal(pipe, 1000, adder)
  buzzer1, buzzersrc1, sinkpad1 = create_audiotest_signal_with_converter(pipe, 1000, adder)

  # Add some output
  output = gst.element_factory_make("autoaudiosink", "audio_out")
  pipe.add(output)
  adder.link(output)

  # Start the playback
  pipe.set_state(gst.STATE_PLAYING)

  raw_input("1kHz test sound. Press <ENTER> to continue.")

  # Get another generator
  #buzzer2, buzzersrc2, sinkpad2 = create_raw_audiotest_signal(pipe, 500, adder)
  buzzer2, buzzersrc2, sinkpad2 = create_audiotest_signal_with_converter(pipe, 500, adder)

  # Start the second buzzer (other ways streaming stops because of starvation)
  buzzer2.set_state(gst.STATE_PLAYING)

  raw_input("1kHz + 500Hz test sound playing simoultenously. Press <ENTER> to continue.")

  # Before removing a source, we must use pad blocking to prevent state changes
  buzzersrc1.set_blocked(True)
  # Stop the first buzzer
  buzzer1.set_state(gst.STATE_NULL)
  # Unlink from the mixer
  buzzersrc1.unlink(sinkpad2)
  # Release the mixers first sink pad
  adder.release_request_pad(sinkpad1)
  # Because here none of the Adder's sink pads block, streaming continues

  raw_input("Only 500Hz test sound. Press <ENTER> to stop.")

如果在两个调用中使用create_raw_audiotest_signal代替create_audiotest_signal_with_converter,它当然可以工作.如果你使用两者的混合物,它可以工作,但中间有一个不必要的额外延迟.最有趣的情况是在两个调用中使用audioconvert,但在第一个返回键时使用gtk块.

有人有什么建议吗?我究竟做错了什么?
先感谢您.

解决方法:

我自己找到了答案,确实很简单……
我添加了其他组件,但它们存在于管道中并保持独立的播放状态.所以解决方案是将所有管道设置为播放,然后将状态设置为所有子节点.

pipe.set_state(gst.STATE_PLAYING)

代替:

buzzer2.set_state(gst.STATE_PLAYING)

它再次有效.

上一篇:linux – gstreamer和带音频的mp4或avi输出


下一篇:使用Python管理多平台视频流的最佳方法是什么?