|
| 1 | +import sys |
| 2 | +import gi |
| 3 | +import logging |
| 4 | +import os |
| 5 | +gi.require_version("GLib", "2.0") |
| 6 | +gi.require_version("GObject", "2.0") |
| 7 | +gi.require_version("Gst", "1.0") |
| 8 | +from gi.repository import Gst, GLib, GObject |
| 9 | +from numpy import ndarray |
| 10 | + |
| 11 | + |
| 12 | +#Playback sample code |
| 13 | +def gst_sample_playback(content): |
| 14 | + # Create the empty pipeline |
| 15 | + pipeline = Gst.Pipeline.new("audio playback") |
| 16 | + filesrc = Gst.ElementFactory.make("filesrc", "file_src") |
| 17 | + wavparse = Gst.ElementFactory.make("wavparse", "wav_parse"); |
| 18 | + alsasink = Gst.ElementFactory.make("alsasink", "alsa_sink"); |
| 19 | + |
| 20 | + if not pipeline or not filesrc or not wavparse or not alsasink: |
| 21 | + print("Create pipeline/filesrc/wavparse/alsasink element failed.") |
| 22 | + sys.exit(1) |
| 23 | + |
| 24 | + #set properties to the elements |
| 25 | + filesrc.set_property('location',content) |
| 26 | + alsasink.set_property('device',"hw:0,0") |
| 27 | + |
| 28 | + #Build pipeline |
| 29 | + #Add elements to the pipeline and link them |
| 30 | + pipeline.add(filesrc) |
| 31 | + pipeline.add(wavparse) |
| 32 | + pipeline.add(alsasink) |
| 33 | + |
| 34 | + if not filesrc.link(wavparse): |
| 35 | + print("ERROR: Could not link filesrc to wavparse") |
| 36 | + sys.exit(1) |
| 37 | + if not wavparse.link(alsasink): |
| 38 | + print("ERROR: Could not link wavparse to alsasink") |
| 39 | + sys.exit(1) |
| 40 | + |
| 41 | + pipeline.set_state(Gst.State.PLAYING) |
| 42 | + print("Start"); |
| 43 | + bus = pipeline.get_bus() |
| 44 | + bus_id = bus.add_signal_watch() |
| 45 | + msg = bus.timed_pop_filtered(Gst.CLOCK_TIME_NONE,Gst.MessageType.ERROR | Gst.MessageType.EOS) |
| 46 | + |
| 47 | + if msg: |
| 48 | + t = msg.type |
| 49 | + if t == Gst.MessageType.ERROR: |
| 50 | + err, dbg = msg.parse_error() |
| 51 | + print("ERROR:", msg.src.get_name(), ":", err.message) |
| 52 | + if dbg: |
| 53 | + print("debugging info:", dbg) |
| 54 | + elif t == Gst.MessageType.EOS: |
| 55 | + print("Stop end-Of-Stream reached") |
| 56 | + else: |
| 57 | + print("ERROR: Unexpected message received.") |
| 58 | + |
| 59 | + pipeline.set_state(Gst.State.NULL) |
| 60 | + Gst.Object.unref(bus) |
| 61 | + Gst.deinit() |
| 62 | + sys.exit(1) |
| 63 | + |
| 64 | + |
| 65 | +#Capture sample code |
| 66 | +def gst_sample_capture(content): |
| 67 | + #create the empty pipeline |
| 68 | + alsasrc = Gst.ElementFactory.make("alsasrc", "alsa_src"); |
| 69 | + capsfilter = Gst.ElementFactory.make("capsfilter", "caps_filter"); |
| 70 | + wavenc = Gst.ElementFactory.make("wavenc", "wav_enc"); |
| 71 | + filesink = Gst.ElementFactory.make("filesink", "file_sink"); |
| 72 | + pipeline = Gst.Pipeline.new("audio_capture_stream"); |
| 73 | + |
| 74 | + if not pipeline or not alsasrc or not capsfilter or not wavenc or not filesink: |
| 75 | + print("Create pipeline/alsasrc/capsfilter/wavenc/filesink element failed.") |
| 76 | + sys.exit(1) |
| 77 | + |
| 78 | + #set properties to the elements |
| 79 | + alsasrc.set_property("device","hw:0,0") |
| 80 | + alsasrc.set_property("num-buffers",1000) |
| 81 | + alsasrc.set_property("buffer-time",10000) |
| 82 | + caps=Gst.Caps.new_empty_simple("audio/x-raw") |
| 83 | + capsfilter.set_property("caps",caps) |
| 84 | + filesink.set_property("location",content) |
| 85 | + |
| 86 | + #Build the pipeline. Add elements to the pipeline and link them |
| 87 | + pipeline.add(alsasrc) |
| 88 | + pipeline.add(capsfilter) |
| 89 | + pipeline.add(wavenc) |
| 90 | + pipeline.add(filesink) |
| 91 | + |
| 92 | + if not alsasrc.link(capsfilter): |
| 93 | + print("ERROR: Could not link alsasrc to capsfilter") |
| 94 | + sys.exit(1) |
| 95 | + if not capsfilter.link(wavenc): |
| 96 | + print("ERROR: Could not link capsfilter to wavenc") |
| 97 | + sys.exit(1) |
| 98 | + if not wavenc.link(filesink): |
| 99 | + print("ERROR: Could not link wavenc to filesink") |
| 100 | + sys.exit(1) |
| 101 | + |
| 102 | + pipeline.set_state(Gst.State.PLAYING) |
| 103 | + print("Start"); |
| 104 | + |
| 105 | + bus = pipeline.get_bus() |
| 106 | + bus_id = bus.add_signal_watch() |
| 107 | + msg = bus.timed_pop_filtered(Gst.CLOCK_TIME_NONE,Gst.MessageType.ERROR | Gst.MessageType.EOS) |
| 108 | + if msg: |
| 109 | + t = msg.type |
| 110 | + if t == Gst.MessageType.ERROR: |
| 111 | + err, dbg = msg.parse_error() |
| 112 | + print("ERROR:", msg.src.get_name(), ":", err.message) |
| 113 | + if dbg: |
| 114 | + print("debugging info:", dbg) |
| 115 | + elif t == Gst.MessageType.EOS: |
| 116 | + print("Stop end-Of-Stream reached") |
| 117 | + else: |
| 118 | + print("ERROR: Unexpected message received.") |
| 119 | + |
| 120 | + pipeline.set_state(Gst.State.NULL) |
| 121 | + Gst.Object.unref(bus) |
| 122 | + Gst.deinit() |
| 123 | + sys.exit(1) |
| 124 | + |
| 125 | + |
| 126 | +def main(): |
| 127 | + Gst.init(sys.argv) |
| 128 | + if len(sys.argv) < 2: |
| 129 | + print("Missing <output-location> parameter") |
| 130 | + sys.exit(1) |
| 131 | + else: |
| 132 | + if (sys.argv[1]=="playback"): |
| 133 | + gst_sample_playback(sys.argv[2]); |
| 134 | + elif(sys.argv[1]=="capture"): |
| 135 | + gst_sample_capture(sys.argv[2]); |
| 136 | + else: |
| 137 | + print("Unexpected operation %s", sys.argv[1]); |
| 138 | + |
| 139 | + |
| 140 | +if __name__ == "__main__": |
| 141 | + main() |
0 commit comments