37 gi.require_version(
'Gst',
'1.0')
38 from gi.repository
import GObject, Gst
39 GObject.threads_init()
43 from gstlal
import datasource
44 from gstlal
import pipeparts
45 from gstlal
import simplehandler
59 List-like object to hold CacheEntry objects, and run os.unlink() on
60 the .path of each as they are removed from the list or when the
61 list is garbage collected. All errors during file removal are
64 Note that there is no way to remove a CacheEntry from this list
65 without the file it represents being deleted. If, after adding a
66 CacheEntry to this list it is decided the file must not be deleted,
67 then instead of removing it from the list it must be replaced with
68 something else, e.g. None, and that item can then be removed from
73 >>> from lal.utils import CacheEntry
74 >>> # create a cache, and add an entry
75 >>> cache = tempcache()
76 >>> cache.append(CacheEntry("- - - - file://localhost/tmp/blah.txt"))
77 >>> # now remove it without the file being deleted
81 def __delitem__(self, i):
83 os.unlink(self[i].path)
86 super(tempcache, self).__delitem__(i)
88 def __delslice__(self, i, j):
90 for entry
in self[i:j]:
97 super(tempcache, self).__delslice__(i, j)
113 def __init__(self, *args, **kwargs):
114 super(Handler, self).__init__(*args, **kwargs)
117 def do_on_message(self, bus, message):
118 if message.type == Gst.MessageType.ELEMENT
and message.get_structure().get_name() ==
"GstMultiFileSink":
119 self.cache.append(pipeparts.framecpp_filesink_cache_entry_from_mfs_message(message))
133 def mkbasicsrc(pipeline, gw_data_source_info, instrument, verbose = False):
134 if gw_data_source_info.data_source ==
"frames":
135 if instrument ==
"V1":
137 src = pipeparts.mklalcachesrc(pipeline, blocksize = 1048576, use_mmap =
False, location = gw_data_source_info.frame_cache, cache_src_regex =
"V")
139 src = pipeparts.mklalcachesrc(pipeline, blocksize = 1048576, use_mmap =
False, location = gw_data_source_info.frame_cache, cache_src_regex = instrument[0], cache_dsc_regex = instrument)
140 demux = pipeparts.mkframecppchanneldemux(pipeline, src, do_file_checksum =
False, channel_list = list(map(
"%s:%s".__mod__, gw_data_source_info.channel_dict.items())))
141 pipeparts.framecpp_channeldemux_set_units(demux, dict.fromkeys(demux.get_property(
"channel-list"),
"strain"))
143 src = pipeparts.mkqueue(pipeline,
None, max_size_buffers = 0, max_size_bytes = 0, max_size_time = 8 * Gst.SECOND)
144 pipeparts.src_deferred_link(demux,
"%s:%s" % (instrument, gw_data_source_info.channel_dict[instrument]), src.get_static_pad(
"sink"))
146 src = pipeparts.mkaudiorate(pipeline, src, skip_to_first =
True, silent =
False)
148 raise ValueError(
"invalid data_source: %s" % gw_data_source_info.data_source)
151 src = pipeparts.mkaudioconvert(pipeline, src)
155 src = pipeparts.mkprogressreport(pipeline, src,
"progress_src_%s" % instrument)
158 if gw_data_source_info.injection_filename
is not None:
159 src = pipeparts.mkinjections(pipeline, src, gw_data_source_info.injection_filename)
161 src = pipeparts.mkqueue(pipeline, src, max_size_bytes = 0, max_size_buffers = 0, max_size_time = Gst.SECOND * 64)
176 def build_pipeline(pipeline, data_source_info, output_path = tempfile.gettempdir(), sample_rate =
None, description =
"TMPFILE_DELETE_ME_%s" % uuid.uuid4().hex, channel_comment =
None, frame_duration = 1, frames_per_file = 1024, verbose =
False):
182 channelmux_input_dict = {}
184 for instrument, channel_name
in data_source_info.channel_dict.items():
189 src = mkbasicsrc(pipeline, data_source_info, instrument, verbose = verbose)
195 if sample_rate
is not None:
197 src = pipeparts.mkcapsfilter(pipeline, src,
"audio/x-raw, rate=[%d,MAX]" % sample_rate)
198 src = pipeparts.mkresample(pipeline, src, quality = 9)
199 src = pipeparts.mkcapsfilter(pipeline, src,
"audio/x-raw, rate=%d" % sample_rate)
205 src = pipeparts.mkframecppchannelmux(pipeline, {
"%s:%s" % (instrument, channel_name): src}, frame_duration = frame_duration, frames_per_file = frames_per_file)
206 for pad
in src.sinkpads:
207 if channel_comment
is not None:
208 pad.set_property(
"comment", channel_comment)
209 pad.set_property(
"pad-type",
"FrProcData")
210 pipeparts.mkframecppfilesink(pipeline, src, frame_type =
"%s_%s" % (instrument, description), path = output_path)
222 def cache_hoft(data_source_info, channel_comment = "cached h(t)
for inspiral search
", verbose = False, **kwargs):
228 mainloop = GObject.MainLoop()
229 pipeline = Gst.Pipeline(name=
"pipeline")
230 handler =
Handler(mainloop, pipeline)
234 print(
"assembling pipeline ...", file=sys.stderr)
235 build_pipeline(pipeline, data_source_info, channel_comment = channel_comment, verbose = verbose, **kwargs)
237 print(
"done", file=sys.stderr)
244 if pipeline.set_state(Gst.State.READY) != Gst.StateChangeReturn.SUCCESS:
245 raise RuntimeError(
"pipeline did not enter ready state")
246 datasource.pipeline_seek_for_gps(pipeline, *data_source_info.seg)
248 print(
"setting pipeline state to playing ...", file=sys.stderr)
249 if pipeline.set_state(Gst.State.PLAYING) != Gst.StateChangeReturn.SUCCESS:
250 raise RuntimeError(
"pipeline did not enter playing state")
253 print(
"running pipeline ...", file=sys.stderr)