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 GST_ELEMENT_REGISTER_DEFINE (audiosegmentclip, "audiosegmentclip",
49 GST_RANK_NONE, GST_TYPE_AUDIO_SEGMENT_CLIP);
50
51 static void
gst_audio_segment_clip_class_init(GstAudioSegmentClipClass * klass)52 gst_audio_segment_clip_class_init (GstAudioSegmentClipClass * klass)
53 {
54 GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
55 GstSegmentClipClass *segment_clip_klass = GST_SEGMENT_CLIP_CLASS (klass);
56
57 GST_DEBUG_CATEGORY_INIT (gst_audio_segment_clip_debug, "audiosegmentclip", 0,
58 "audiosegmentclip element");
59
60 segment_clip_klass->reset = GST_DEBUG_FUNCPTR (gst_audio_segment_clip_reset);
61 segment_clip_klass->set_caps =
62 GST_DEBUG_FUNCPTR (gst_audio_segment_clip_set_caps);
63 segment_clip_klass->clip_buffer =
64 GST_DEBUG_FUNCPTR (gst_audio_segment_clip_clip_buffer);
65
66 gst_element_class_set_static_metadata (element_class,
67 "Audio buffer segment clipper",
68 "Filter/Audio",
69 "Clips audio buffers to the configured segment",
70 "Sebastian Dröge <sebastian.droege@collabora.co.uk>");
71
72 gst_element_class_add_static_pad_template (element_class, &sink_pad_template);
73 gst_element_class_add_static_pad_template (element_class, &src_pad_template);
74 }
75
76 static void
gst_audio_segment_clip_init(GstAudioSegmentClip * self)77 gst_audio_segment_clip_init (GstAudioSegmentClip * self)
78 {
79 }
80
81 static void
gst_audio_segment_clip_reset(GstSegmentClip * base)82 gst_audio_segment_clip_reset (GstSegmentClip * base)
83 {
84 GstAudioSegmentClip *self = GST_AUDIO_SEGMENT_CLIP (base);
85
86 GST_DEBUG_OBJECT (self, "Resetting internal state");
87
88 self->rate = self->framesize = 0;
89 }
90
91
92 static GstFlowReturn
gst_audio_segment_clip_clip_buffer(GstSegmentClip * base,GstBuffer * buffer,GstBuffer ** outbuf)93 gst_audio_segment_clip_clip_buffer (GstSegmentClip * base, GstBuffer * buffer,
94 GstBuffer ** outbuf)
95 {
96 GstAudioSegmentClip *self = GST_AUDIO_SEGMENT_CLIP (base);
97 GstSegment *segment = &base->segment;
98 GstClockTime timestamp = GST_BUFFER_TIMESTAMP (buffer);
99 GstClockTime duration = GST_BUFFER_DURATION (buffer);
100 guint64 offset = GST_BUFFER_OFFSET (buffer);
101 guint64 offset_end = GST_BUFFER_OFFSET_END (buffer);
102 guint size = gst_buffer_get_size (buffer);
103
104 if (!self->rate || !self->framesize) {
105 GST_ERROR_OBJECT (self, "Not negotiated yet");
106 gst_buffer_unref (buffer);
107 return GST_FLOW_NOT_NEGOTIATED;
108 }
109
110 if (segment->format != GST_FORMAT_DEFAULT &&
111 segment->format != GST_FORMAT_TIME) {
112 GST_DEBUG_OBJECT (self, "Unsupported segment format %s",
113 gst_format_get_name (segment->format));
114 *outbuf = buffer;
115 return GST_FLOW_OK;
116 }
117
118 if (!GST_BUFFER_TIMESTAMP_IS_VALID (buffer)) {
119 GST_WARNING_OBJECT (self, "Buffer without valid timestamp");
120 *outbuf = buffer;
121 return GST_FLOW_OK;
122 }
123
124 *outbuf =
125 gst_audio_buffer_clip (buffer, segment, self->rate, self->framesize);
126
127 if (!*outbuf) {
128 GST_DEBUG_OBJECT (self, "Buffer outside the configured segment");
129
130 /* Now return unexpected if we're before/after the end */
131 if (segment->format == GST_FORMAT_TIME) {
132 if (segment->rate >= 0) {
133 if (segment->stop != -1 && timestamp >= segment->stop)
134 return GST_FLOW_EOS;
135 } else {
136 if (!GST_CLOCK_TIME_IS_VALID (duration))
137 duration =
138 gst_util_uint64_scale_int (size, GST_SECOND,
139 self->framesize * self->rate);
140
141 if (segment->start != -1 && timestamp + duration <= segment->start)
142 return GST_FLOW_EOS;
143 }
144 } else {
145 if (segment->rate >= 0) {
146 if (segment->stop != -1 && offset != -1 && offset >= segment->stop)
147 return GST_FLOW_EOS;
148 } else if (offset != -1 || offset_end != -1) {
149 if (offset_end == -1)
150 offset_end = offset + size / self->framesize;
151
152 if (segment->start != -1 && offset_end <= segment->start)
153 return GST_FLOW_EOS;
154 }
155 }
156 }
157
158 return GST_FLOW_OK;
159 }
160
161 static gboolean
gst_audio_segment_clip_set_caps(GstSegmentClip * base,GstCaps * caps)162 gst_audio_segment_clip_set_caps (GstSegmentClip * base, GstCaps * caps)
163 {
164 GstAudioSegmentClip *self = GST_AUDIO_SEGMENT_CLIP (base);
165 gboolean ret;
166 GstAudioInfo info;
167 gint rate, channels, width;
168
169 gst_audio_info_init (&info);
170 ret = gst_audio_info_from_caps (&info, caps);
171
172 if (ret) {
173 rate = GST_AUDIO_INFO_RATE (&info);
174 channels = GST_AUDIO_INFO_CHANNELS (&info);
175 width = GST_AUDIO_INFO_WIDTH (&info);
176
177 GST_DEBUG_OBJECT (self, "Configured: rate %d channels %d width %d",
178 rate, channels, width);
179 self->rate = rate;
180 self->framesize = (width / 8) * channels;
181 }
182
183 return ret;
184 }
185