From 64a58e0662898c6e3d408fdfd9882ed61c98f21b Mon Sep 17 00:00:00 2001 From: Thomas Adamcik Date: Fri, 25 Mar 2016 13:09:44 +0100 Subject: [PATCH] audio: Move sample to data conversion to a helper. Also add check for None buffer being returned. --- mopidy/audio/tags.py | 23 ++++++++++++++++------- 1 file changed, 16 insertions(+), 7 deletions(-) diff --git a/mopidy/audio/tags.py b/mopidy/audio/tags.py index 1625ccc4..e29f37d8 100644 --- a/mopidy/audio/tags.py +++ b/mopidy/audio/tags.py @@ -54,13 +54,9 @@ gstreamer-GstTagList.html elif isinstance(value, (compat.text_type, bool, numbers.Number)): result[tag].append(value) elif isinstance(value, Gst.Sample): - buf = value.get_buffer() - (found, mapinfo) = buf.map(Gst.MapFlags.READ) - if found: - try: - result[tag].append(bytes(mapinfo.data)) - finally: - buf.unmap(mapinfo) + data = _extract_sample_data(value) + if data: + result[tag].append(data) else: logger.log( log.TRACE_LOG_LEVEL, @@ -70,6 +66,19 @@ gstreamer-GstTagList.html return result +def _extract_sample_data(sample): + buf = sample.get_buffer() + if not buf: + return None + found, mapinfo = buf.map(Gst.MapFlags.READ) + if not found: + return None + try: + return bytes(mapinfo.data) + finally: + buf.unmap(mapinfo) + + # TODO: split based on "stream" and "track" based conversion? i.e. handle data # from radios in it's own helper instead? def convert_tags_to_track(tags):