1 /* GStreamer
2 * Copyright (C) 2010 Sebastian Dröge <sebastian.droege@collabora.co.uk>
3 *
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Library General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
8 *
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Library General Public License for more details.
13 *
14 * You should have received a copy of the GNU Library General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
17 * Boston, MA 02110-1301, USA.
18 */
19
20 #ifdef HAVE_CONFIG_H
21 #include "config.h"
22 #endif
23
24 #include <gst/gst.h>
25 #include <gst/audio/audio.h>
26
27 #include "gstaudiosegmentclip.h"
28
29 static GstStaticPadTemplate sink_pad_template =
30 GST_STATIC_PAD_TEMPLATE ("sink", GST_PAD_SINK, GST_PAD_ALWAYS,
31 GST_STATIC_CAPS (GST_AUDIO_CAPS_MAKE (GST_AUDIO_FORMATS_ALL)));
32
33 static GstStaticPadTemplate src_pad_template =
34 GST_STATIC_PAD_TEMPLATE ("src", GST_PAD_SRC, GST_PAD_ALWAYS,
35 GST_STATIC_CAPS (GST_AUDIO_CAPS_MAKE (GST_AUDIO_FORMATS_ALL)));
36
37 static void gst_audio_segment_clip_reset (GstSegmentClip * self);
38 static GstFlowReturn gst_audio_segment_clip_clip_buffer (GstSegmentClip * self,
39 GstBuffer * buffer, GstBuffer ** outbuf);
40 static gboolean gst_audio_segment_clip_set_caps (GstSegmentClip * self,
41 GstCaps * caps);
42
43 GST_DEBUG_CATEGORY_STATIC (gst_audio_segment_clip_debug);
44 #define GST_CAT_DEFAULT gst_audio_segment_clip_debug
45
46 G_DEFINE_TYPE (GstAudioSegmentClip, gst_audio_segment_clip,
47 GST_TYPE_SEGMENT_CLIP);
48
49 static void
gst_audio_segment_clip_class_init(GstAudioSegmentClipClass * klass)50 gst_audio_segment_clip_class_init (GstAudioSegmentClipClass * klass)
51 {
52 GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
53 GstSegmentClipClass *segment_clip_klass = GST_SEGMENT_CLIP_CLASS (klass);
54
55 GST_DEBUG_CATEGORY_INIT (gst_audio_segment_clip_debug, "audiosegmentclip", 0,
56 "audiosegmentclip element");
57
58 segment_clip_klass->reset = GST_DEBUG_FUNCPTR (gst_audio_segment_clip_reset);
59 segment_clip_klass->set_caps =
60 GST_DEBUG_FUNCPTR (gst_audio_segment_clip_set_caps);
61 segment_clip_klass->clip_buffer =
62 GST_DEBUG_FUNCPTR (gst_audio_segment_clip_clip_buffer);
63
64 gst_element_class_set_static_metadata (element_class,
65 "Audio buffer segment clipper",
66 "Filter/Audio",
67 "Clips audio buffers to the configured segment",
68 "Sebastian Dröge <sebastian.droege@collabora.co.uk>");
69
70 gst_element_class_add_static_pad_template (element_class, &sink_pad_template);
71 gst_element_class_add_static_pad_template (element_class, &src_pad_template);
72 }
73
74 static void
gst_audio_segment_clip_init(GstAudioSegmentClip * self)75 gst_audio_segment_clip_init (GstAudioSegmentClip * self)
76 {
77 }
78
79 static void
gst_audio_segment_clip_reset(GstSegmentClip * base)80 gst_audio_segment_clip_reset (GstSegmentClip * base)
81 {
82 GstAudioSegmentClip *self = GST_AUDIO_SEGMENT_CLIP (base);
83
84 GST_DEBUG_OBJECT (self, "Resetting internal state");
85
86 self->rate = self->framesize = 0;
87 }
88
89
90 static GstFlowReturn
gst_audio_segment_clip_clip_buffer(GstSegmentClip * base,GstBuffer * buffer,GstBuffer ** outbuf)91 gst_audio_segment_clip_clip_buffer (GstSegmentClip * base, GstBuffer * buffer,
92 GstBuffer ** outbuf)
93 {
94 GstAudioSegmentClip *self = GST_AUDIO_SEGMENT_CLIP (base);
95 GstSegment *segment = &base->segment;
96 GstClockTime timestamp = GST_BUFFER_TIMESTAMP (buffer);
97 GstClockTime duration = GST_BUFFER_DURATION (buffer);
98 guint64 offset = GST_BUFFER_OFFSET (buffer);
99 guint64 offset_end = GST_BUFFER_OFFSET_END (buffer);
100 guint size = gst_buffer_get_size (buffer);
101
102 if (!self->rate || !self->framesize) {
103 GST_ERROR_OBJECT (self, "Not negotiated yet");
104 gst_buffer_unref (buffer);
105 return GST_FLOW_NOT_NEGOTIATED;
106 }
107
108 if (segment->format != GST_FORMAT_DEFAULT &&
109 segment->format != GST_FORMAT_TIME) {
110 GST_DEBUG_OBJECT (self, "Unsupported segment format %s",
111 gst_format_get_name (segment->format));
112 *outbuf = buffer;
113 return GST_FLOW_OK;
114 }
115
116 if (!GST_BUFFER_TIMESTAMP_IS_VALID (buffer)) {
117 GST_WARNING_OBJECT (self, "Buffer without valid timestamp");
118 *outbuf = buffer;
119 return GST_FLOW_OK;
120 }
121
122 *outbuf =
123 gst_audio_buffer_clip (buffer, segment, self->rate, self->framesize);
124
125 if (!*outbuf) {
126 GST_DEBUG_OBJECT (self, "Buffer outside the configured segment");
127
128 /* Now return unexpected if we're before/after the end */
129 if (segment->format == GST_FORMAT_TIME) {
130 if (segment->rate >= 0) {
131 if (segment->stop != -1 && timestamp >= segment->stop)
132 return GST_FLOW_EOS;
133 } else {
134 if (!GST_CLOCK_TIME_IS_VALID (duration))
135 duration =
136 gst_util_uint64_scale_int (size, GST_SECOND,
137 self->framesize * self->rate);
138
139 if (segment->start != -1 && timestamp + duration <= segment->start)
140 return GST_FLOW_EOS;
141 }
142 } else {
143 if (segment->rate >= 0) {
144 if (segment->stop != -1 && offset != -1 && offset >= segment->stop)
145 return GST_FLOW_EOS;
146 } else if (offset != -1 || offset_end != -1) {
147 if (offset_end == -1)
148 offset_end = offset + size / self->framesize;
149
150 if (segment->start != -1 && offset_end <= segment->start)
151 return GST_FLOW_EOS;
152 }
153 }
154 }
155
156 return GST_FLOW_OK;
157 }
158
159 static gboolean
gst_audio_segment_clip_set_caps(GstSegmentClip * base,GstCaps * caps)160 gst_audio_segment_clip_set_caps (GstSegmentClip * base, GstCaps * caps)
161 {
162 GstAudioSegmentClip *self = GST_AUDIO_SEGMENT_CLIP (base);
163 gboolean ret;
164 GstAudioInfo info;
165 gint rate, channels, width;
166
167 gst_audio_info_init (&info);
168 ret = gst_audio_info_from_caps (&info, caps);
169
170 if (ret) {
171 rate = GST_AUDIO_INFO_RATE (&info);
172 channels = GST_AUDIO_INFO_CHANNELS (&info);
173 width = GST_AUDIO_INFO_WIDTH (&info);
174
175 GST_DEBUG_OBJECT (self, "Configured: rate %d channels %d width %d",
176 rate, channels, width);
177 self->rate = rate;
178 self->framesize = (width / 8) * channels;
179 }
180
181 return ret;
182 }
183