| 1 |
/* GStreamer |
| 2 |
* Copyright (C) 2008 Nokia Corporation. All rights reserved. |
| 3 |
* Contact: Stefan Kost <[email protected]> |
| 4 |
* Copyright (C) 2008 Sebastian Dröge <[email protected]>. |
| 5 |
* Copyright (C) 2011, Hewlett-Packard Development Company, L.P. |
| 6 |
* Author: Sebastian Dröge <[email protected]>, Collabora Ltd. |
| 7 |
* |
| 8 |
* This library is free software; you can redistribute it and/or |
| 9 |
* modify it under the terms of the GNU Library General Public |
| 10 |
* License as published by the Free Software Foundation; either |
| 11 |
* version 2 of the License, or (at your option) any later version. |
| 12 |
* |
| 13 |
* This library is distributed in the hope that it will be useful, |
| 14 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 16 |
* Library General Public License for more details. |
| 17 |
* |
| 18 |
* You should have received a copy of the GNU Library General Public |
| 19 |
* License along with this library; if not, write to the |
| 20 |
* Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, |
| 21 |
* Boston, MA 02110-1301, USA. |
| 22 |
*/ |
| 23 |
|
| 24 |
/** |
| 25 |
* SECTION:gstbaseparse |
| 26 |
* @title: GstBaseParse |
| 27 |
* @short_description: Base class for stream parsers |
| 28 |
* @see_also: #GstBaseTransform |
| 29 |
* |
| 30 |
* This base class is for parser elements that process data and splits it |
| 31 |
* into separate audio/video/whatever frames. |
| 32 |
* |
| 33 |
* It provides for: |
| 34 |
* |
| 35 |
* * provides one sink pad and one source pad |
| 36 |
* * handles state changes |
| 37 |
* * can operate in pull mode or push mode |
| 38 |
* * handles seeking in both modes |
| 39 |
* * handles events (SEGMENT/EOS/FLUSH) |
| 40 |
* * handles queries (POSITION/DURATION/SEEKING/FORMAT/CONVERT) |
| 41 |
* * handles flushing |
| 42 |
* |
| 43 |
* The purpose of this base class is to provide the basic functionality of |
| 44 |
* a parser and share a lot of rather complex code. |
| 45 |
* |
| 46 |
* # Description of the parsing mechanism: |
| 47 |
* |
| 48 |
* ## Set-up phase |
| 49 |
* |
| 50 |
* * #GstBaseParse calls #GstBaseParseClass::start to inform subclass |
| 51 |
* that data processing is about to start now. |
| 52 |
* |
| 53 |
* * #GstBaseParse class calls #GstBaseParseClass::set_sink_caps to |
| 54 |
* inform the subclass about incoming sinkpad caps. Subclass could |
| 55 |
* already set the srcpad caps accordingly, but this might be delayed |
| 56 |
* until calling gst_base_parse_finish_frame() with a non-queued frame. |
| 57 |
* |
| 58 |
* * At least at this point subclass needs to tell the #GstBaseParse class |
| 59 |
* how big data chunks it wants to receive (minimum frame size ). It can |
| 60 |
* do this with gst_base_parse_set_min_frame_size(). |
| 61 |
* |
| 62 |
* * #GstBaseParse class sets up appropriate data passing mode (pull/push) |
| 63 |
* and starts to process the data. |
| 64 |
* |
| 65 |
* ## Parsing phase |
| 66 |
* |
| 67 |
* * #GstBaseParse gathers at least min_frame_size bytes of data either |
| 68 |
* by pulling it from upstream or collecting buffers in an internal |
| 69 |
* #GstAdapter. |
| 70 |
* |
| 71 |
* * A buffer of (at least) min_frame_size bytes is passed to subclass |
| 72 |
* with #GstBaseParseClass::handle_frame. Subclass checks the contents |
| 73 |
* and can optionally return #GST_FLOW_OK along with an amount of data |
| 74 |
* to be skipped to find a valid frame (which will result in a |
| 75 |
* subsequent DISCONT). If, otherwise, the buffer does not hold a |
| 76 |
* complete frame, #GstBaseParseClass::handle_frame can merely return |
| 77 |
* and will be called again when additional data is available. In push |
| 78 |
* mode this amounts to an additional input buffer (thus minimal |
| 79 |
* additional latency), in pull mode this amounts to some arbitrary |
| 80 |
* reasonable buffer size increase. |
| 81 |
* |
| 82 |
* Of course, gst_base_parse_set_min_frame_size() could also be used if |
| 83 |
* a very specific known amount of additional data is required. If, |
| 84 |
* however, the buffer holds a complete valid frame, it can pass the |
| 85 |
* size of this frame to gst_base_parse_finish_frame(). |
| 86 |
* |
| 87 |
* If acting as a converter, it can also merely indicate consumed input |
| 88 |
* data while simultaneously providing custom output data. Note that |
| 89 |
* baseclass performs some processing (such as tracking overall consumed |
| 90 |
* data rate versus duration) for each finished frame, but other state |
| 91 |
* is only updated upon each call to #GstBaseParseClass::handle_frame |
| 92 |
* (such as tracking upstream input timestamp). |
| 93 |
* |
| 94 |
* Subclass is also responsible for setting the buffer metadata |
| 95 |
* (e.g. buffer timestamp and duration, or keyframe if applicable). |
| 96 |
* (although the latter can also be done by #GstBaseParse if it is |
| 97 |
* appropriately configured, see below). Frame is provided with |
| 98 |
* timestamp derived from upstream (as much as generally possible), |
| 99 |
* duration obtained from configuration (see below), and offset |
| 100 |
* if meaningful (in pull mode). |
| 101 |
* |
| 102 |
* Note that #GstBaseParseClass::handle_frame might receive any small |
| 103 |
* amount of input data when leftover data is being drained (e.g. at |
| 104 |
* EOS). |
| 105 |
* |
| 106 |
* * As part of finish frame processing, just prior to actually pushing |
| 107 |
* the buffer in question, it is passed to |
| 108 |
* #GstBaseParseClass::pre_push_frame which gives subclass yet one last |
| 109 |
* chance to examine buffer metadata, or to send some custom (tag) |
| 110 |
* events, or to perform custom (segment) filtering. |
| 111 |
* |
| 112 |
* * During the parsing process #GstBaseParseClass will handle both srcpad |
| 113 |
* and sinkpad events. They will be passed to subclass if |
| 114 |
* #GstBaseParseClass::sink_event or #GstBaseParseClass::src_event |
| 115 |
* implementations have been provided. |
| 116 |
* |
| 117 |
* ## Shutdown phase |
| 118 |
* |
| 119 |
* * #GstBaseParse class calls #GstBaseParseClass::stop to inform the |
| 120 |
* subclass that data parsing will be stopped. |
| 121 |
* |
| 122 |
* Subclass is responsible for providing pad template caps for source and |
| 123 |
* sink pads. The pads need to be named "sink" and "src". It also needs to |
| 124 |
* set the fixed caps on srcpad, when the format is ensured (e.g. when |
| 125 |
* base class calls subclass' #GstBaseParseClass::set_sink_caps function). |
| 126 |
* |
| 127 |
* This base class uses %GST_FORMAT_DEFAULT as a meaning of frames. So, |
| 128 |
* subclass conversion routine needs to know that conversion from |
| 129 |
* %GST_FORMAT_TIME to %GST_FORMAT_DEFAULT must return the |
| 130 |
* frame number that can be found from the given byte position. |
| 131 |
* |
| 132 |
* #GstBaseParse uses subclasses conversion methods also for seeking (or |
| 133 |
* otherwise uses its own default one, see also below). |
| 134 |
* |
| 135 |
* Subclass @start and @stop functions will be called to inform the beginning |
| 136 |
* and end of data processing. |
| 137 |
* |
| 138 |
* Things that subclass need to take care of: |
| 139 |
* |
| 140 |
* * Provide pad templates |
| 141 |
* * Fixate the source pad caps when appropriate |
| 142 |
* * Inform base class how big data chunks should be retrieved. This is |
| 143 |
* done with gst_base_parse_set_min_frame_size() function. |
| 144 |
* * Examine data chunks passed to subclass with |
| 145 |
* #GstBaseParseClass::handle_frame and pass proper frame(s) to |
| 146 |
* gst_base_parse_finish_frame(), and setting src pad caps and timestamps |
| 147 |
* on frame. |
| 148 |
* * Provide conversion functions |
| 149 |
* * Update the duration information with gst_base_parse_set_duration() |
| 150 |
* * Optionally passthrough using gst_base_parse_set_passthrough() |
| 151 |
* * Configure various baseparse parameters using |
| 152 |
* gst_base_parse_set_average_bitrate(), gst_base_parse_set_syncable() |
| 153 |
* and gst_base_parse_set_frame_rate(). |
| 154 |
* |
| 155 |
* * In particular, if subclass is unable to determine a duration, but |
| 156 |
* parsing (or specs) yields a frames per seconds rate, then this can be |
| 157 |
* provided to #GstBaseParse to enable it to cater for buffer time |
| 158 |
* metadata (which will be taken from upstream as much as |
| 159 |
* possible). Internally keeping track of frame durations and respective |
| 160 |
* sizes that have been pushed provides #GstBaseParse with an estimated |
| 161 |
* bitrate. A default #GstBaseParseClass::convert (used if not |
| 162 |
* overridden) will then use these rates to perform obvious conversions. |
| 163 |
* These rates are also used to update (estimated) duration at regular |
| 164 |
* frame intervals. |
| 165 |
* |
| 166 |
*/ |
| 167 |
|
| 168 |
/* TODO: |
| 169 |
* - In push mode provide a queue of adapter-"queued" buffers for upstream |
| 170 |
* buffer metadata |
| 171 |
* - Queue buffers/events until caps are set |
| 172 |
*/ |
| 173 |
|
| 174 |
#ifdef HAVE_CONFIG_H |
| 175 |
# include "config.h" |
| 176 |
#endif |
| 177 |
|
| 178 |
#include <stdlib.h> |
| 179 |
#include <string.h> |
| 180 |
|
| 181 |
#include <gst/base/gstadapter.h> |
| 182 |
|
| 183 |
#include "gstbaseparse.h" |
| 184 |
|
| 185 |
/* FIXME: get rid of old GstIndex code */ |
| 186 |
#include "gstindex.h" |
| 187 |
#include "gstindex.c" |
| 188 |
#include "gstmemindex.c" |
| 189 |
|
| 190 |
#define GST_BASE_PARSE_FRAME_PRIVATE_FLAG_NOALLOC (1 << 0) |
| 191 |
|
| 192 |
#define MIN_FRAMES_TO_POST_BITRATE 10 |
| 193 |
#define TARGET_DIFFERENCE (20 * GST_SECOND) |
| 194 |
#define MAX_INDEX_ENTRIES 4096 |
| 195 |
#define UPDATE_THRESHOLD 2 |
| 196 |
|
| 197 |
#define ABSDIFF(a,b) (((a) > (b)) ? ((a) - (b)) : ((b) - (a))) |
| 198 |
|
| 199 |
GST_DEBUG_CATEGORY_STATIC (gst_base_parse_debug); |
| 200 |
#define GST_CAT_DEFAULT gst_base_parse_debug |
| 201 |
|
| 202 |
/* Supported formats */ |
| 203 |
static const GstFormat fmtlist[] = { |
| 204 |
GST_FORMAT_DEFAULT, |
| 205 |
GST_FORMAT_BYTES, |
| 206 |
GST_FORMAT_TIME, |
| 207 |
GST_FORMAT_UNDEFINED |
| 208 |
}; |
| 209 |
|
| 210 |
struct _GstBaseParsePrivate |
| 211 |
{ |
| 212 |
GstPadMode pad_mode; |
| 213 |
|
| 214 |
GstAdapter *adapter; |
| 215 |
|
| 216 |
gint64 duration; |
| 217 |
GstFormat duration_fmt; |
| 218 |
gint64 estimated_duration; |
| 219 |
gint64 estimated_drift; |
| 220 |
|
| 221 |
guint min_frame_size; |
| 222 |
gboolean disable_passthrough; |
| 223 |
gboolean disable_clip; |
| 224 |
gboolean passthrough; |
| 225 |
gboolean pts_interpolate; |
| 226 |
gboolean infer_ts; |
| 227 |
gboolean syncable; |
| 228 |
gboolean has_timing_info; |
| 229 |
guint fps_num, fps_den; |
| 230 |
gint update_interval; |
| 231 |
guint bitrate; |
| 232 |
guint lead_in, lead_out; |
| 233 |
GstClockTime lead_in_ts, lead_out_ts; |
| 234 |
GstClockTime min_latency, max_latency; |
| 235 |
/* Tracks whether the latency message was posted at least once */ |
| 236 |
gboolean posted_latency_msg; |
| 237 |
|
| 238 |
gboolean discont; |
| 239 |
gboolean flushing; |
| 240 |
gboolean drain; |
| 241 |
gboolean saw_gaps; |
| 242 |
|
| 243 |
gint64 offset; |
| 244 |
gint64 sync_offset; |
| 245 |
GstClockTime next_pts; |
| 246 |
GstClockTime next_dts; |
| 247 |
GstClockTime prev_pts; |
| 248 |
GstClockTime prev_dts; |
| 249 |
gboolean prev_dts_from_pts; |
| 250 |
GstClockTime frame_duration; |
| 251 |
gboolean seen_keyframe; |
| 252 |
gboolean is_video; |
| 253 |
gint flushed; |
| 254 |
|
| 255 |
guint64 framecount; |
| 256 |
guint64 bytecount; |
| 257 |
guint64 data_bytecount; |
| 258 |
guint64 acc_duration; |
| 259 |
GstClockTime first_frame_pts; |
| 260 |
GstClockTime first_frame_dts; |
| 261 |
gint64 first_frame_offset; |
| 262 |
|
| 263 |
gboolean post_min_bitrate; |
| 264 |
gboolean post_avg_bitrate; |
| 265 |
gboolean post_max_bitrate; |
| 266 |
|
| 267 |
guint min_bitrate; |
| 268 |
guint avg_bitrate; |
| 269 |
guint max_bitrate; |
| 270 |
guint posted_avg_bitrate; |
| 271 |
|
| 272 |
/* frames/buffers that are queued and ready to go on OK */ |
| 273 |
GQueue queued_frames; |
| 274 |
|
| 275 |
GstBuffer *cache; |
| 276 |
|
| 277 |
/* index entry storage, either ours or provided */ |
| 278 |
GstIndex *index; |
| 279 |
gint index_id; |
| 280 |
gboolean own_index; |
| 281 |
GMutex index_lock; |
| 282 |
|
| 283 |
/* seek table entries only maintained if upstream is BYTE seekable */ |
| 284 |
gboolean upstream_seekable; |
| 285 |
gboolean upstream_has_duration; |
| 286 |
gint64 upstream_size; |
| 287 |
GstFormat upstream_format; |
| 288 |
/* minimum distance between two index entries */ |
| 289 |
GstClockTimeDiff idx_interval; |
| 290 |
guint64 idx_byte_interval; |
| 291 |
/* ts and offset of last entry added */ |
| 292 |
GstClockTime index_last_ts; |
| 293 |
gint64 index_last_offset; |
| 294 |
gboolean index_last_valid; |
| 295 |
|
| 296 |
/* timestamps currently produced are accurate, e.g. started from 0 onwards */ |
| 297 |
gboolean exact_position; |
| 298 |
/* seek events are temporarily kept to match them with newsegments */ |
| 299 |
GSList *pending_seeks; |
| 300 |
|
| 301 |
/* reverse playback */ |
| 302 |
GSList *buffers_pending; |
| 303 |
GSList *buffers_head; |
| 304 |
GSList *buffers_queued; |
| 305 |
GSList *buffers_send; |
| 306 |
GstClockTime last_pts; |
| 307 |
GstClockTime last_dts; |
| 308 |
gint64 last_offset; |
| 309 |
|
| 310 |
/* Pending serialized events */ |
| 311 |
GList *pending_events; |
| 312 |
|
| 313 |
/* If baseparse has checked the caps to identify if it is |
| 314 |
* handling video or audio */ |
| 315 |
gboolean checked_media; |
| 316 |
|
| 317 |
/* offset of last parsed frame/data */ |
| 318 |
gint64 prev_offset; |
| 319 |
/* force a new frame, regardless of offset */ |
| 320 |
gboolean new_frame; |
| 321 |
/* whether we are merely scanning for a frame */ |
| 322 |
gboolean scanning; |
| 323 |
/* ... and resulting frame, if any */ |
| 324 |
GstBaseParseFrame *scanned_frame; |
| 325 |
|
| 326 |
/* TRUE if we're still detecting the format, i.e. |
| 327 |
* if ::detect() is still called for future buffers */ |
| 328 |
gboolean detecting; |
| 329 |
GList *detect_buffers; |
| 330 |
guint detect_buffers_size; |
| 331 |
|
| 332 |
/* True when no buffers have been received yet */ |
| 333 |
gboolean first_buffer; |
| 334 |
|
| 335 |
/* if TRUE, a STREAM_START event needs to be pushed */ |
| 336 |
gboolean push_stream_start; |
| 337 |
|
| 338 |
/* When we need to skip more data than we have currently */ |
| 339 |
guint skip; |
| 340 |
|
| 341 |
/* Tag handling (stream tags only, global tags are passed through as-is) */ |
| 342 |
GstTagList *upstream_tags; |
| 343 |
GstTagList *parser_tags; |
| 344 |
GstTagMergeMode parser_tags_merge_mode; |
| 345 |
gboolean tags_changed; |
| 346 |
|
| 347 |
/* Current segment seqnum */ |
| 348 |
guint32 segment_seqnum; |
| 349 |
}; |
| 350 |
|
| 351 |
typedef struct _GstBaseParseSeek |
| 352 |
{ |
| 353 |
GstSegment segment; |
| 354 |
gboolean accurate; |
| 355 |
gint64 offset; |
| 356 |
GstClockTime start_ts; |
| 357 |
} GstBaseParseSeek; |
| 358 |
|
| 359 |
#define DEFAULT_DISABLE_PASSTHROUGH FALSE |
| 360 |
#define DEFAULT_DISABLE_CLIP TRUE |
| 361 |
|
| 362 |
enum |
| 363 |
{ |
| 364 |
PROP_0, |
| 365 |
PROP_DISABLE_PASSTHROUGH, |
| 366 |
PROP_DISABLE_CLIP, |
| 367 |
PROP_LAST |
| 368 |
}; |
| 369 |
|
| 370 |
#define GST_BASE_PARSE_INDEX_LOCK(parse) \ |
| 371 |
g_mutex_lock (&parse->priv->index_lock); |
| 372 |
#define GST_BASE_PARSE_INDEX_UNLOCK(parse) \ |
| 373 |
g_mutex_unlock (&parse->priv->index_lock); |
| 374 |
|
| 375 |
static GstElementClass *parent_class = NULL; |
| 376 |
static gint base_parse_private_offset = 0; |
| 377 |
|
| 378 |
static void gst_base_parse_class_init (GstBaseParseClass * klass); |
| 379 |
static void gst_base_parse_init (GstBaseParse * parse, |
| 380 |
GstBaseParseClass * klass); |
| 381 |
|
| 382 |
GType |
| 383 |
gst_base_parse_get_type (void) |
| 384 |
{ |
| 385 |
static gsize base_parse_type = 0; |
| 386 |
|
| 387 |
if (g_once_init_enter (&base_parse_type)) { |
| 388 |
static const GTypeInfo base_parse_info = { |
| 389 |
sizeof (GstBaseParseClass), |
| 390 |
(GBaseInitFunc) NULL, |
| 391 |
(GBaseFinalizeFunc) NULL, |
| 392 |
(GClassInitFunc) gst_base_parse_class_init, |
| 393 |
NULL, |
| 394 |
NULL, |
| 395 |
sizeof (GstBaseParse), |
| 396 |
0, |
| 397 |
(GInstanceInitFunc) gst_base_parse_init, |
| 398 |
}; |
| 399 |
GType _type; |
| 400 |
|
| 401 |
_type = g_type_register_static (GST_TYPE_ELEMENT, |
| 402 |
"GstBaseParse", &base_parse_info, G_TYPE_FLAG_ABSTRACT); |
| 403 |
|
| 404 |
base_parse_private_offset = |
| 405 |
g_type_add_instance_private (_type, sizeof (GstBaseParsePrivate)); |
| 406 |
|
| 407 |
g_once_init_leave (&base_parse_type, _type); |
| 408 |
} |
| 409 |
return (GType) base_parse_type; |
| 410 |
} |
| 411 |
|
| 412 |
static inline GstBaseParsePrivate * |
| 413 |
gst_base_parse_get_instance_private (GstBaseParse * self) |
| 414 |
{ |
| 415 |
return (G_STRUCT_MEMBER_P (self, base_parse_private_offset)); |
| 416 |
} |
| 417 |
|
| 418 |
static void gst_base_parse_finalize (GObject * object); |
| 419 |
|
| 420 |
static GstStateChangeReturn gst_base_parse_change_state (GstElement * element, |
| 421 |
GstStateChange transition); |
| 422 |
static void gst_base_parse_reset (GstBaseParse * parse); |
| 423 |
|
| 424 |
#if 0 |
| 425 |
static void gst_base_parse_set_index (GstElement * element, GstIndex * index); |
| 426 |
static GstIndex *gst_base_parse_get_index (GstElement * element); |
| 427 |
#endif |
| 428 |
|
| 429 |
static gboolean gst_base_parse_sink_activate (GstPad * sinkpad, |
| 430 |
GstObject * parent); |
| 431 |
static gboolean gst_base_parse_sink_activate_mode (GstPad * pad, |
| 432 |
GstObject * parent, GstPadMode mode, gboolean active); |
| 433 |
static gboolean gst_base_parse_handle_seek (GstBaseParse * parse, |
| 434 |
GstEvent * event); |
| 435 |
static void gst_base_parse_set_upstream_tags (GstBaseParse * parse, |
| 436 |
GstTagList * taglist); |
| 437 |
|
| 438 |
static void gst_base_parse_set_property (GObject * object, guint prop_id, |
| 439 |
const GValue * value, GParamSpec * pspec); |
| 440 |
static void gst_base_parse_get_property (GObject * object, guint prop_id, |
| 441 |
GValue * value, GParamSpec * pspec); |
| 442 |
|
| 443 |
static gboolean gst_base_parse_src_event (GstPad * pad, GstObject * parent, |
| 444 |
GstEvent * event); |
| 445 |
static gboolean gst_base_parse_src_query (GstPad * pad, GstObject * parent, |
| 446 |
GstQuery * query); |
| 447 |
|
| 448 |
static gboolean gst_base_parse_sink_event (GstPad * pad, GstObject * parent, |
| 449 |
GstEvent * event); |
| 450 |
static gboolean gst_base_parse_sink_query (GstPad * pad, GstObject * parent, |
| 451 |
GstQuery * query); |
| 452 |
|
| 453 |
static GstFlowReturn gst_base_parse_chain (GstPad * pad, GstObject * parent, |
| 454 |
GstBuffer * buffer); |
| 455 |
static void gst_base_parse_loop (GstPad * pad); |
| 456 |
|
| 457 |
static GstFlowReturn gst_base_parse_parse_frame (GstBaseParse * parse, |
| 458 |
GstBaseParseFrame * frame); |
| 459 |
|
| 460 |
static gboolean gst_base_parse_sink_event_default (GstBaseParse * parse, |
| 461 |
GstEvent * event); |
| 462 |
|
| 463 |
static gboolean gst_base_parse_src_event_default (GstBaseParse * parse, |
| 464 |
GstEvent * event); |
| 465 |
|
| 466 |
static gboolean gst_base_parse_sink_query_default (GstBaseParse * parse, |
| 467 |
GstQuery * query); |
| 468 |
static gboolean gst_base_parse_src_query_default (GstBaseParse * parse, |
| 469 |
GstQuery * query); |
| 470 |
|
| 471 |
static gint64 gst_base_parse_find_offset (GstBaseParse * parse, |
| 472 |
GstClockTime time, gboolean before, GstClockTime * _ts); |
| 473 |
static GstFlowReturn gst_base_parse_locate_time (GstBaseParse * parse, |
| 474 |
GstClockTime * _time, gint64 * _offset); |
| 475 |
|
| 476 |
static GstFlowReturn gst_base_parse_start_fragment (GstBaseParse * parse); |
| 477 |
static GstFlowReturn gst_base_parse_finish_fragment (GstBaseParse * parse, |
| 478 |
gboolean prev_head); |
| 479 |
static GstFlowReturn gst_base_parse_send_buffers (GstBaseParse * parse); |
| 480 |
|
| 481 |
static inline GstFlowReturn gst_base_parse_check_sync (GstBaseParse * parse); |
| 482 |
|
| 483 |
static gboolean gst_base_parse_is_seekable (GstBaseParse * parse); |
| 484 |
|
| 485 |
static void gst_base_parse_push_pending_events (GstBaseParse * parse); |
| 486 |
|
| 487 |
static void |
| 488 |
gst_base_parse_clear_queues (GstBaseParse * parse, gboolean clear_sticky_events) |
| 489 |
{ |
| 490 |
g_slist_foreach (parse->priv->buffers_queued, (GFunc) gst_buffer_unref, NULL); |
| 491 |
g_slist_free (parse->priv->buffers_queued); |
| 492 |
parse->priv->buffers_queued = NULL; |
| 493 |
g_slist_foreach (parse->priv->buffers_pending, (GFunc) gst_buffer_unref, |
| 494 |
NULL); |
| 495 |
g_slist_free (parse->priv->buffers_pending); |
| 496 |
parse->priv->buffers_pending = NULL; |
| 497 |
g_slist_foreach (parse->priv->buffers_head, (GFunc) gst_buffer_unref, NULL); |
| 498 |
g_slist_free (parse->priv->buffers_head); |
| 499 |
parse->priv->buffers_head = NULL; |
| 500 |
g_slist_foreach (parse->priv->buffers_send, (GFunc) gst_buffer_unref, NULL); |
| 501 |
g_slist_free (parse->priv->buffers_send); |
| 502 |
parse->priv->buffers_send = NULL; |
| 503 |
|
| 504 |
g_list_foreach (parse->priv->detect_buffers, (GFunc) gst_buffer_unref, NULL); |
| 505 |
g_list_free (parse->priv->detect_buffers); |
| 506 |
parse->priv->detect_buffers = NULL; |
| 507 |
parse->priv->detect_buffers_size = 0; |
| 508 |
|
| 509 |
g_queue_foreach (&parse->priv->queued_frames, |
| 510 |
(GFunc) gst_base_parse_frame_free, NULL); |
| 511 |
g_queue_clear (&parse->priv->queued_frames); |
| 512 |
|
| 513 |
gst_buffer_replace (&parse->priv->cache, NULL); |
| 514 |
|
| 515 |
if (clear_sticky_events) { |
| 516 |
g_list_foreach (parse->priv->pending_events, (GFunc) gst_event_unref, NULL); |
| 517 |
g_list_free (parse->priv->pending_events); |
| 518 |
parse->priv->pending_events = NULL; |
| 519 |
} else { |
| 520 |
GList *cur = g_list_first (parse->priv->pending_events); |
| 521 |
GList *next; |
| 522 |
|
| 523 |
while (cur != NULL) { |
| 524 |
GstEvent *event = GST_EVENT (cur->data); |
| 525 |
GstEventType type = GST_EVENT_TYPE (event); |
| 526 |
|
| 527 |
next = g_list_next (cur); |
| 528 |
|
| 529 |
if (!GST_EVENT_IS_STICKY (event) || type == GST_EVENT_EOS |
| 530 |
|| type == GST_EVENT_STREAM_GROUP_DONE || type == GST_EVENT_SEGMENT) { |
| 531 |
gst_event_unref (event); |
| 532 |
parse->priv->pending_events = |
| 533 |
g_list_delete_link (parse->priv->pending_events, cur); |
| 534 |
} |
| 535 |
|
| 536 |
cur = next; |
| 537 |
} |
| 538 |
} |
| 539 |
|
| 540 |
parse->priv->checked_media = FALSE; |
| 541 |
} |
| 542 |
|
| 543 |
static void |
| 544 |
gst_base_parse_finalize (GObject * object) |
| 545 |
{ |
| 546 |
GstBaseParse *parse = GST_BASE_PARSE (object); |
| 547 |
|
| 548 |
g_object_unref (parse->priv->adapter); |
| 549 |
|
| 550 |
if (parse->priv->index) { |
| 551 |
gst_object_unref (parse->priv->index); |
| 552 |
parse->priv->index = NULL; |
| 553 |
} |
| 554 |
g_mutex_clear (&parse->priv->index_lock); |
| 555 |
|
| 556 |
gst_base_parse_clear_queues (parse, TRUE); |
| 557 |
|
| 558 |
G_OBJECT_CLASS (parent_class)->finalize (object); |
| 559 |
} |
| 560 |
|
| 561 |
static void |
| 562 |
gst_base_parse_class_init (GstBaseParseClass * klass) |
| 563 |
{ |
| 564 |
GObjectClass *gobject_class; |
| 565 |
GstElementClass *gstelement_class; |
| 566 |
|
| 567 |
gobject_class = G_OBJECT_CLASS (klass); |
| 568 |
|
| 569 |
if (base_parse_private_offset != 0) |
| 570 |
g_type_class_adjust_private_offset (klass, &base_parse_private_offset); |
| 571 |
|
| 572 |
parent_class = g_type_class_peek_parent (klass); |
| 573 |
|
| 574 |
gobject_class->finalize = GST_DEBUG_FUNCPTR (gst_base_parse_finalize); |
| 575 |
gobject_class->set_property = GST_DEBUG_FUNCPTR (gst_base_parse_set_property); |
| 576 |
gobject_class->get_property = GST_DEBUG_FUNCPTR (gst_base_parse_get_property); |
| 577 |
|
| 578 |
/** |
| 579 |
* GstBaseParse:disable-passthrough: |
| 580 |
* |
| 581 |
* If set to %TRUE, baseparse will unconditionally force parsing of the |
| 582 |
* incoming data. This can be required in the rare cases where the incoming |
| 583 |
* side-data (caps, pts, dts, ...) is not trusted by the user and wants to |
| 584 |
* force validation and parsing of the incoming data. |
| 585 |
* If set to %FALSE, decision of whether to parse the data or not is up to |
| 586 |
* the implementation (standard behaviour). |
| 587 |
*/ |
| 588 |
g_object_class_install_property (gobject_class, PROP_DISABLE_PASSTHROUGH, |
| 589 |
g_param_spec_boolean ("disable-passthrough", "Disable passthrough", |
| 590 |
"Force processing (disables passthrough)", |
| 591 |
DEFAULT_DISABLE_PASSTHROUGH, |
| 592 |
G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS)); |
| 593 |
|
| 594 |
/** |
| 595 |
* GstBaseParse:disable-clip: |
| 596 |
* |
| 597 |
* Disable dropping buffers that are out of segment |
| 598 |
* |
| 599 |
* Since: 1.28 |
| 600 |
*/ |
| 601 |
g_object_class_install_property (gobject_class, PROP_DISABLE_CLIP, |
| 602 |
g_param_spec_boolean ("disable-clip", "Disable Clip", |
| 603 |
"Disable buffer dropping that are out of segment", |
| 604 |
DEFAULT_DISABLE_CLIP, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS)); |
| 605 |
|
| 606 |
gstelement_class = (GstElementClass *) klass; |
| 607 |
gstelement_class->change_state = |
| 608 |
GST_DEBUG_FUNCPTR (gst_base_parse_change_state); |
| 609 |
|
| 610 |
#if 0 |
| 611 |
gstelement_class->set_index = GST_DEBUG_FUNCPTR (gst_base_parse_set_index); |
| 612 |
gstelement_class->get_index = GST_DEBUG_FUNCPTR (gst_base_parse_get_index); |
| 613 |
#endif |
| 614 |
|
| 615 |
/* Default handlers */ |
| 616 |
klass->sink_event = gst_base_parse_sink_event_default; |
| 617 |
klass->src_event = gst_base_parse_src_event_default; |
| 618 |
klass->sink_query = gst_base_parse_sink_query_default; |
| 619 |
klass->src_query = gst_base_parse_src_query_default; |
| 620 |
klass->convert = gst_base_parse_convert_default; |
| 621 |
|
| 622 |
GST_DEBUG_CATEGORY_INIT (gst_base_parse_debug, "baseparse", 0, |
| 623 |
"baseparse element"); |
| 624 |
} |
| 625 |
|
| 626 |
static void |
| 627 |
gst_base_parse_init (GstBaseParse * parse, GstBaseParseClass * bclass) |
| 628 |
{ |
| 629 |
GstPadTemplate *pad_template; |
| 630 |
|
| 631 |
GST_DEBUG_OBJECT (parse, "gst_base_parse_init"); |
| 632 |
|
| 633 |
parse->priv = gst_base_parse_get_instance_private (parse); |
| 634 |
|
| 635 |
pad_template = |
| 636 |
gst_element_class_get_pad_template (GST_ELEMENT_CLASS (bclass), "sink"); |
| 637 |
g_return_if_fail (pad_template != NULL); |
| 638 |
parse->sinkpad = gst_pad_new_from_template (pad_template, "sink"); |
| 639 |
gst_pad_set_event_function (parse->sinkpad, |
| 640 |
GST_DEBUG_FUNCPTR (gst_base_parse_sink_event)); |
| 641 |
gst_pad_set_query_function (parse->sinkpad, |
| 642 |
GST_DEBUG_FUNCPTR (gst_base_parse_sink_query)); |
| 643 |
gst_pad_set_chain_function (parse->sinkpad, |
| 644 |
GST_DEBUG_FUNCPTR (gst_base_parse_chain)); |
| 645 |
gst_pad_set_activate_function (parse->sinkpad, |
| 646 |
GST_DEBUG_FUNCPTR (gst_base_parse_sink_activate)); |
| 647 |
gst_pad_set_activatemode_function (parse->sinkpad, |
| 648 |
GST_DEBUG_FUNCPTR (gst_base_parse_sink_activate_mode)); |
| 649 |
GST_PAD_SET_PROXY_ALLOCATION (parse->sinkpad); |
| 650 |
gst_element_add_pad (GST_ELEMENT (parse), parse->sinkpad); |
| 651 |
|
| 652 |
GST_DEBUG_OBJECT (parse, "sinkpad created"); |
| 653 |
|
| 654 |
pad_template = |
| 655 |
gst_element_class_get_pad_template (GST_ELEMENT_CLASS (bclass), "src"); |
| 656 |
g_return_if_fail (pad_template != NULL); |
| 657 |
parse->srcpad = gst_pad_new_from_template (pad_template, "src"); |
| 658 |
gst_pad_set_event_function (parse->srcpad, |
| 659 |
GST_DEBUG_FUNCPTR (gst_base_parse_src_event)); |
| 660 |
gst_pad_set_query_function (parse->srcpad, |
| 661 |
GST_DEBUG_FUNCPTR (gst_base_parse_src_query)); |
| 662 |
gst_pad_use_fixed_caps (parse->srcpad); |
| 663 |
gst_element_add_pad (GST_ELEMENT (parse), parse->srcpad); |
| 664 |
GST_DEBUG_OBJECT (parse, "src created"); |
| 665 |
|
| 666 |
g_queue_init (&parse->priv->queued_frames); |
| 667 |
|
| 668 |
parse->priv->adapter = gst_adapter_new (); |
| 669 |
|
| 670 |
parse->priv->pad_mode = GST_PAD_MODE_NONE; |
| 671 |
|
| 672 |
g_mutex_init (&parse->priv->index_lock); |
| 673 |
|
| 674 |
/* init state */ |
| 675 |
gst_base_parse_reset (parse); |
| 676 |
GST_DEBUG_OBJECT (parse, "init ok"); |
| 677 |
|
| 678 |
GST_OBJECT_FLAG_SET (parse, GST_ELEMENT_FLAG_INDEXABLE); |
| 679 |
|
| 680 |
parse->priv->upstream_tags = NULL; |
| 681 |
parse->priv->parser_tags = NULL; |
| 682 |
parse->priv->parser_tags_merge_mode = GST_TAG_MERGE_APPEND; |
| 683 |
parse->priv->disable_passthrough = DEFAULT_DISABLE_PASSTHROUGH; |
| 684 |
parse->priv->disable_clip = DEFAULT_DISABLE_CLIP; |
| 685 |
} |
| 686 |
|
| 687 |
static void |
| 688 |
gst_base_parse_set_property (GObject * object, guint prop_id, |
| 689 |
const GValue * value, GParamSpec * pspec) |
| 690 |
{ |
| 691 |
GstBaseParse *parse = GST_BASE_PARSE (object); |
| 692 |
|
| 693 |
switch (prop_id) { |
| 694 |
case PROP_DISABLE_PASSTHROUGH: |
| 695 |
parse->priv->disable_passthrough = g_value_get_boolean (value); |
| 696 |
break; |
| 697 |
case PROP_DISABLE_CLIP: |
| 698 |
parse->priv->disable_clip = g_value_get_boolean (value); |
| 699 |
break; |
| 700 |
default: |
| 701 |
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec); |
| 702 |
break; |
| 703 |
} |
| 704 |
} |
| 705 |
|
| 706 |
static void |
| 707 |
gst_base_parse_get_property (GObject * object, guint prop_id, GValue * value, |
| 708 |
GParamSpec * pspec) |
| 709 |
{ |
| 710 |
GstBaseParse *parse = GST_BASE_PARSE (object); |
| 711 |
|
| 712 |
switch (prop_id) { |
| 713 |
case PROP_DISABLE_PASSTHROUGH: |
| 714 |
g_value_set_boolean (value, parse->priv->disable_passthrough); |
| 715 |
break; |
| 716 |
case PROP_DISABLE_CLIP: |
| 717 |
g_value_set_boolean (value, parse->priv->disable_clip); |
| 718 |
break; |
| 719 |
default: |
| 720 |
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec); |
| 721 |
break; |
| 722 |
} |
| 723 |
} |
| 724 |
|
| 725 |
/** |
| 726 |
* gst_base_parse_frame_copy: |
| 727 |
* @frame: a #GstBaseParseFrame |
| 728 |
* |
| 729 |
* Copies a #GstBaseParseFrame. |
| 730 |
* |
| 731 |
* Returns: A copy of @frame |
| 732 |
* |
| 733 |
* Since: 1.12.1 |
| 734 |
*/ |
| 735 |
GstBaseParseFrame * |
| 736 |
gst_base_parse_frame_copy (GstBaseParseFrame * frame) |
| 737 |
{ |
| 738 |
GstBaseParseFrame *copy; |
| 739 |
|
| 740 |
copy = g_memdup2 (frame, sizeof (GstBaseParseFrame)); |
| 741 |
copy->buffer = gst_buffer_ref (frame->buffer); |
| 742 |
if (copy->out_buffer) |
| 743 |
gst_buffer_ref (copy->out_buffer); |
| 744 |
copy->_private_flags &= ~GST_BASE_PARSE_FRAME_PRIVATE_FLAG_NOALLOC; |
| 745 |
|
| 746 |
GST_TRACE ("copied frame %p -> %p", frame, copy); |
| 747 |
|
| 748 |
return copy; |
| 749 |
} |
| 750 |
|
| 751 |
/** |
| 752 |
* gst_base_parse_frame_free: |
| 753 |
* @frame: A #GstBaseParseFrame |
| 754 |
* |
| 755 |
* Frees the provided @frame. |
| 756 |
*/ |
| 757 |
void |
| 758 |
gst_base_parse_frame_free (GstBaseParseFrame * frame) |
| 759 |
{ |
| 760 |
GST_TRACE ("freeing frame %p", frame); |
| 761 |
|
| 762 |
if (frame->buffer) { |
| 763 |
gst_buffer_unref (frame->buffer); |
| 764 |
frame->buffer = NULL; |
| 765 |
} |
| 766 |
|
| 767 |
if (frame->out_buffer) { |
| 768 |
gst_buffer_unref (frame->out_buffer); |
| 769 |
frame->out_buffer = NULL; |
| 770 |
} |
| 771 |
|
| 772 |
if (!(frame->_private_flags & GST_BASE_PARSE_FRAME_PRIVATE_FLAG_NOALLOC)) { |
| 773 |
g_free (frame); |
| 774 |
} else { |
| 775 |
memset (frame, 0, sizeof (*frame)); |
| 776 |
} |
| 777 |
} |
| 778 |
|
| 779 |
G_DEFINE_BOXED_TYPE (GstBaseParseFrame, gst_base_parse_frame, |
| 780 |
(GBoxedCopyFunc) gst_base_parse_frame_copy, |
| 781 |
(GBoxedFreeFunc) gst_base_parse_frame_free); |
| 782 |
|
| 783 |
/** |
| 784 |
* gst_base_parse_frame_init: |
| 785 |
* @frame: #GstBaseParseFrame. |
| 786 |
* |
| 787 |
* Sets a #GstBaseParseFrame to initial state. Currently this means |
| 788 |
* all public fields are zero-ed and a private flag is set to make |
| 789 |
* sure gst_base_parse_frame_free() only frees the contents but not |
| 790 |
* the actual frame. Use this function to initialise a #GstBaseParseFrame |
| 791 |
* allocated on the stack. |
| 792 |
*/ |
| 793 |
void |
| 794 |
gst_base_parse_frame_init (GstBaseParseFrame * frame) |
| 795 |
{ |
| 796 |
memset (frame, 0, sizeof (GstBaseParseFrame)); |
| 797 |
frame->_private_flags = GST_BASE_PARSE_FRAME_PRIVATE_FLAG_NOALLOC; |
| 798 |
GST_TRACE ("inited frame %p", frame); |
| 799 |
} |
| 800 |
|
| 801 |
/** |
| 802 |
* gst_base_parse_frame_new: |
| 803 |
* @buffer: (transfer none): a #GstBuffer |
| 804 |
* @flags: the flags |
| 805 |
* @overhead: number of bytes in this frame which should be counted as |
| 806 |
* metadata overhead, ie. not used to calculate the average bitrate. |
| 807 |
* Set to -1 to mark the entire frame as metadata. If in doubt, set to 0. |
| 808 |
* |
| 809 |
* Allocates a new #GstBaseParseFrame. This function is mainly for bindings, |
| 810 |
* elements written in C should usually allocate the frame on the stack and |
| 811 |
* then use gst_base_parse_frame_init() to initialise it. |
| 812 |
* |
| 813 |
* Returns: a newly-allocated #GstBaseParseFrame. Free with |
| 814 |
* gst_base_parse_frame_free() when no longer needed. |
| 815 |
*/ |
| 816 |
GstBaseParseFrame * |
| 817 |
gst_base_parse_frame_new (GstBuffer * buffer, GstBaseParseFrameFlags flags, |
| 818 |
gint overhead) |
| 819 |
{ |
| 820 |
GstBaseParseFrame *frame; |
| 821 |
|
| 822 |
frame = g_new0 (GstBaseParseFrame, 1); |
| 823 |
frame->buffer = gst_buffer_ref (buffer); |
| 824 |
|
| 825 |
GST_TRACE ("created frame %p", frame); |
| 826 |
return frame; |
| 827 |
} |
| 828 |
|
| 829 |
static inline void |
| 830 |
gst_base_parse_update_flags (GstBaseParse * parse) |
| 831 |
{ |
| 832 |
parse->flags = 0; |
| 833 |
|
| 834 |
/* set flags one by one for clarity */ |
| 835 |
if (G_UNLIKELY (parse->priv->drain)) |
| 836 |
parse->flags |= GST_BASE_PARSE_FLAG_DRAINING; |
| 837 |
|
| 838 |
/* losing sync is pretty much a discont (and vice versa), no ? */ |
| 839 |
if (G_UNLIKELY (parse->priv->discont)) |
| 840 |
parse->flags |= GST_BASE_PARSE_FLAG_LOST_SYNC; |
| 841 |
} |
| 842 |
|
| 843 |
static inline void |
| 844 |
gst_base_parse_update_frame (GstBaseParse * parse, GstBaseParseFrame * frame) |
| 845 |
{ |
| 846 |
if (G_UNLIKELY (parse->priv->discont)) { |
| 847 |
GST_DEBUG_OBJECT (parse, "marking DISCONT"); |
| 848 |
GST_BUFFER_FLAG_SET (frame->buffer, GST_BUFFER_FLAG_DISCONT); |
| 849 |
} else { |
| 850 |
GST_BUFFER_FLAG_UNSET (frame->buffer, GST_BUFFER_FLAG_DISCONT); |
| 851 |
} |
| 852 |
|
| 853 |
if (parse->priv->prev_offset != parse->priv->offset || parse->priv->new_frame) { |
| 854 |
GST_LOG_OBJECT (parse, "marking as new frame"); |
| 855 |
frame->flags |= GST_BASE_PARSE_FRAME_FLAG_NEW_FRAME; |
| 856 |
} |
| 857 |
|
| 858 |
frame->offset = parse->priv->prev_offset = parse->priv->offset; |
| 859 |
} |
| 860 |
|
| 861 |
static void |
| 862 |
gst_base_parse_reset (GstBaseParse * parse) |
| 863 |
{ |
| 864 |
GST_OBJECT_LOCK (parse); |
| 865 |
gst_segment_init (&parse->segment, GST_FORMAT_TIME); |
| 866 |
parse->priv->duration = -1; |
| 867 |
parse->priv->min_frame_size = 1; |
| 868 |
parse->priv->discont = TRUE; |
| 869 |
parse->priv->flushing = FALSE; |
| 870 |
parse->priv->saw_gaps = FALSE; |
| 871 |
parse->priv->offset = 0; |
| 872 |
parse->priv->sync_offset = 0; |
| 873 |
parse->priv->update_interval = -1; |
| 874 |
parse->priv->fps_num = parse->priv->fps_den = 0; |
| 875 |
parse->priv->frame_duration = GST_CLOCK_TIME_NONE; |
| 876 |
parse->priv->lead_in = parse->priv->lead_out = 0; |
| 877 |
parse->priv->lead_in_ts = parse->priv->lead_out_ts = 0; |
| 878 |
parse->priv->bitrate = 0; |
| 879 |
parse->priv->framecount = 0; |
| 880 |
parse->priv->bytecount = 0; |
| 881 |
parse->priv->data_bytecount = 0; |
| 882 |
parse->priv->acc_duration = 0; |
| 883 |
parse->priv->first_frame_pts = GST_CLOCK_TIME_NONE; |
| 884 |
parse->priv->first_frame_dts = GST_CLOCK_TIME_NONE; |
| 885 |
parse->priv->first_frame_offset = -1; |
| 886 |
parse->priv->estimated_duration = -1; |
| 887 |
parse->priv->estimated_drift = 0; |
| 888 |
parse->priv->next_pts = GST_CLOCK_TIME_NONE; |
| 889 |
parse->priv->next_dts = 0; |
| 890 |
parse->priv->syncable = TRUE; |
| 891 |
parse->priv->passthrough = FALSE; |
| 892 |
parse->priv->pts_interpolate = TRUE; |
| 893 |
parse->priv->infer_ts = TRUE; |
| 894 |
parse->priv->has_timing_info = FALSE; |
| 895 |
parse->priv->min_bitrate = G_MAXUINT; |
| 896 |
parse->priv->max_bitrate = 0; |
| 897 |
parse->priv->avg_bitrate = 0; |
| 898 |
parse->priv->posted_avg_bitrate = 0; |
| 899 |
|
| 900 |
parse->priv->index_last_ts = GST_CLOCK_TIME_NONE; |
| 901 |
parse->priv->index_last_offset = -1; |
| 902 |
parse->priv->index_last_valid = TRUE; |
| 903 |
parse->priv->upstream_seekable = FALSE; |
| 904 |
parse->priv->upstream_size = 0; |
| 905 |
parse->priv->upstream_has_duration = FALSE; |
| 906 |
parse->priv->upstream_format = GST_FORMAT_UNDEFINED; |
| 907 |
parse->priv->idx_interval = 0; |
| 908 |
parse->priv->idx_byte_interval = 0; |
| 909 |
parse->priv->exact_position = TRUE; |
| 910 |
parse->priv->seen_keyframe = FALSE; |
| 911 |
parse->priv->checked_media = FALSE; |
| 912 |
|
| 913 |
parse->priv->last_dts = GST_CLOCK_TIME_NONE; |
| 914 |
parse->priv->last_pts = GST_CLOCK_TIME_NONE; |
| 915 |
parse->priv->last_offset = 0; |
| 916 |
|
| 917 |
parse->priv->skip = 0; |
| 918 |
|
| 919 |
g_list_foreach (parse->priv->pending_events, (GFunc) gst_mini_object_unref, |
| 920 |
NULL); |
| 921 |
g_list_free (parse->priv->pending_events); |
| 922 |
parse->priv->pending_events = NULL; |
| 923 |
|
| 924 |
if (parse->priv->cache) { |
| 925 |
gst_buffer_unref (parse->priv->cache); |
| 926 |
parse->priv->cache = NULL; |
| 927 |
} |
| 928 |
|
| 929 |
g_slist_foreach (parse->priv->pending_seeks, (GFunc) g_free, NULL); |
| 930 |
g_slist_free (parse->priv->pending_seeks); |
| 931 |
parse->priv->pending_seeks = NULL; |
| 932 |
|
| 933 |
if (parse->priv->adapter) |
| 934 |
gst_adapter_clear (parse->priv->adapter); |
| 935 |
|
| 936 |
gst_base_parse_set_upstream_tags (parse, NULL); |
| 937 |
|
| 938 |
if (parse->priv->parser_tags) { |
| 939 |
gst_tag_list_unref (parse->priv->parser_tags); |
| 940 |
parse->priv->parser_tags = NULL; |
| 941 |
} |
| 942 |
parse->priv->parser_tags_merge_mode = GST_TAG_MERGE_APPEND; |
| 943 |
|
| 944 |
parse->priv->new_frame = TRUE; |
| 945 |
|
| 946 |
parse->priv->first_buffer = TRUE; |
| 947 |
|
| 948 |
g_list_foreach (parse->priv->detect_buffers, (GFunc) gst_buffer_unref, NULL); |
| 949 |
g_list_free (parse->priv->detect_buffers); |
| 950 |
parse->priv->detect_buffers = NULL; |
| 951 |
parse->priv->detect_buffers_size = 0; |
| 952 |
|
| 953 |
parse->priv->segment_seqnum = GST_SEQNUM_INVALID; |
| 954 |
parse->priv->posted_latency_msg = FALSE; |
| 955 |
GST_OBJECT_UNLOCK (parse); |
| 956 |
} |
| 957 |
|
| 958 |
static gboolean |
| 959 |
gst_base_parse_check_bitrate_tag (GstBaseParse * parse, const gchar * tag) |
| 960 |
{ |
| 961 |
gboolean got_tag = FALSE; |
| 962 |
guint n = 0; |
| 963 |
|
| 964 |
if (parse->priv->upstream_tags != NULL) |
| 965 |
got_tag = gst_tag_list_get_uint (parse->priv->upstream_tags, tag, &n); |
| 966 |
|
| 967 |
if (!got_tag && parse->priv->parser_tags != NULL) |
| 968 |
got_tag = gst_tag_list_get_uint (parse->priv->parser_tags, tag, &n); |
| 969 |
|
| 970 |
return got_tag; |
| 971 |
} |
| 972 |
|
| 973 |
/* check if upstream or subclass tags contain bitrates already */ |
| 974 |
static void |
| 975 |
gst_base_parse_check_bitrate_tags (GstBaseParse * parse) |
| 976 |
{ |
| 977 |
parse->priv->post_min_bitrate = |
| 978 |
!gst_base_parse_check_bitrate_tag (parse, GST_TAG_MINIMUM_BITRATE); |
| 979 |
parse->priv->post_avg_bitrate = |
| 980 |
!gst_base_parse_check_bitrate_tag (parse, GST_TAG_BITRATE); |
| 981 |
parse->priv->post_max_bitrate = |
| 982 |
!gst_base_parse_check_bitrate_tag (parse, GST_TAG_MAXIMUM_BITRATE); |
| 983 |
} |
| 984 |
|
| 985 |
/* Queues new tag event with the current combined state of the stream tags |
| 986 |
* (i.e. upstream tags merged with subclass tags and current baseparse tags) */ |
| 987 |
static void |
| 988 |
gst_base_parse_queue_tag_event_update (GstBaseParse * parse) |
| 989 |
{ |
| 990 |
GstTagList *merged_tags; |
| 991 |
|
| 992 |
GST_LOG_OBJECT (parse, "upstream : %" GST_PTR_FORMAT, |
| 993 |
parse->priv->upstream_tags); |
| 994 |
GST_LOG_OBJECT (parse, "parser : %" GST_PTR_FORMAT, |
| 995 |
parse->priv->parser_tags); |
| 996 |
GST_LOG_OBJECT (parse, "mode : %d", parse->priv->parser_tags_merge_mode); |
| 997 |
|
| 998 |
merged_tags = |
| 999 |
gst_tag_list_merge (parse->priv->upstream_tags, parse->priv->parser_tags, |
| 1000 |
parse->priv->parser_tags_merge_mode); |
| 1001 |
|
| 1002 |
GST_DEBUG_OBJECT (parse, "merged : %" GST_PTR_FORMAT, merged_tags); |
| 1003 |
|
| 1004 |
if (merged_tags == NULL) |
| 1005 |
merged_tags = gst_tag_list_new_empty (); |
| 1006 |
|
| 1007 |
if (parse->priv->framecount >= MIN_FRAMES_TO_POST_BITRATE) { |
| 1008 |
/* only add bitrate tags if neither upstream tags nor the subclass sets the |
| 1009 |
* bitrate tag in question already */ |
| 1010 |
if (parse->priv->min_bitrate != G_MAXUINT && parse->priv->post_min_bitrate) { |
| 1011 |
GST_LOG_OBJECT (parse, "adding min bitrate %u", parse->priv->min_bitrate); |
| 1012 |
gst_tag_list_add (merged_tags, GST_TAG_MERGE_KEEP, |
| 1013 |
GST_TAG_MINIMUM_BITRATE, parse->priv->min_bitrate, NULL); |
| 1014 |
} |
| 1015 |
if (parse->priv->max_bitrate != 0 && parse->priv->post_max_bitrate) { |
| 1016 |
GST_LOG_OBJECT (parse, "adding max bitrate %u", parse->priv->max_bitrate); |
| 1017 |
gst_tag_list_add (merged_tags, GST_TAG_MERGE_KEEP, |
| 1018 |
GST_TAG_MAXIMUM_BITRATE, parse->priv->max_bitrate, NULL); |
| 1019 |
} |
| 1020 |
if (parse->priv->avg_bitrate != 0 && parse->priv->post_avg_bitrate) { |
| 1021 |
parse->priv->posted_avg_bitrate = parse->priv->avg_bitrate; |
| 1022 |
GST_LOG_OBJECT (parse, "adding avg bitrate %u", parse->priv->avg_bitrate); |
| 1023 |
gst_tag_list_add (merged_tags, GST_TAG_MERGE_KEEP, |
| 1024 |
GST_TAG_BITRATE, parse->priv->avg_bitrate, NULL); |
| 1025 |
} |
| 1026 |
} |
| 1027 |
|
| 1028 |
if (gst_tag_list_is_empty (merged_tags)) { |
| 1029 |
gst_tag_list_unref (merged_tags); |
| 1030 |
return; |
| 1031 |
} |
| 1032 |
|
| 1033 |
parse->priv->pending_events = |
| 1034 |
g_list_prepend (parse->priv->pending_events, |
| 1035 |
gst_event_new_tag (merged_tags)); |
| 1036 |
} |
| 1037 |
|
| 1038 |
/* gst_base_parse_parse_frame: |
| 1039 |
* @parse: #GstBaseParse. |
| 1040 |
* @buffer: #GstBuffer. |
| 1041 |
* |
| 1042 |
* Default callback for parse_frame. |
| 1043 |
*/ |
| 1044 |
static GstFlowReturn |
| 1045 |
gst_base_parse_parse_frame (GstBaseParse * parse, GstBaseParseFrame * frame) |
| 1046 |
{ |
| 1047 |
GstBuffer *buffer = frame->buffer; |
| 1048 |
gboolean must_approximate_pts = !GST_BUFFER_PTS_IS_VALID (buffer) |
| 1049 |
&& GST_CLOCK_TIME_IS_VALID (parse->priv->next_pts); |
| 1050 |
gboolean must_approximate_dts = !GST_BUFFER_DTS_IS_VALID (buffer) |
| 1051 |
&& GST_CLOCK_TIME_IS_VALID (parse->priv->next_dts); |
| 1052 |
|
| 1053 |
if (must_approximate_pts) { |
| 1054 |
GST_BUFFER_PTS (buffer) = parse->priv->next_pts; |
| 1055 |
if (!must_approximate_dts |
| 1056 |
&& GST_BUFFER_DTS (buffer) > parse->priv->next_pts |
| 1057 |
&& GST_CLOCK_TIME_IS_VALID (GST_BUFFER_DTS (buffer))) { |
| 1058 |
/* Can't present a frame before it's decoded: change the pts! This can |
| 1059 |
* happen, for example, when accumulating rounding errors from the |
| 1060 |
* buffer durations. Assume DTS is correct because only PTS is |
| 1061 |
* approximated here */ |
| 1062 |
GST_LOG_OBJECT (parse, |
| 1063 |
"Found DTS (%" GST_TIME_FORMAT ") > PTS (%" GST_TIME_FORMAT |
| 1064 |
"), set PTS = DTS", GST_TIME_ARGS (GST_BUFFER_DTS (buffer)), |
| 1065 |
GST_TIME_ARGS (GST_BUFFER_PTS (buffer))); |
| 1066 |
GST_BUFFER_PTS (buffer) = GST_BUFFER_DTS (buffer); |
| 1067 |
} |
| 1068 |
} |
| 1069 |
|
| 1070 |
if (must_approximate_dts) { |
| 1071 |
if (!must_approximate_pts |
| 1072 |
&& GST_BUFFER_PTS (buffer) < parse->priv->next_dts) { |
| 1073 |
/* Can't present a frame before it's decoded: change the dts! This can |
| 1074 |
* happen, for example, when accumulating rounding errors from the |
| 1075 |
* buffer durations. Assume PTS is correct because only DTS is |
| 1076 |
* approximated here */ |
| 1077 |
GST_LOG_OBJECT (parse, |
| 1078 |
"Found DTS (%" GST_TIME_FORMAT ") > PTS (%" GST_TIME_FORMAT |
| 1079 |
"), set DTS = PTS", GST_TIME_ARGS (GST_BUFFER_DTS (buffer)), |
| 1080 |
GST_TIME_ARGS (GST_BUFFER_PTS (buffer))); |
| 1081 |
GST_BUFFER_DTS (buffer) = GST_BUFFER_PTS (buffer); |
| 1082 |
} else { |
| 1083 |
GST_BUFFER_DTS (buffer) = parse->priv->next_dts; |
| 1084 |
} |
| 1085 |
} |
| 1086 |
|
| 1087 |
if (GST_CLOCK_TIME_IS_VALID (GST_BUFFER_PTS (buffer)) |
| 1088 |
&& GST_CLOCK_TIME_IS_VALID (GST_BUFFER_DTS (buffer)) |
| 1089 |
&& GST_BUFFER_PTS (buffer) < GST_BUFFER_DTS (buffer)) { |
| 1090 |
/* Can't present a frame before it's decoded: change the pts! This can |
| 1091 |
* happen, for example, when accumulating rounding errors from the buffer |
| 1092 |
* durations. PTS and DTS are either both approximated or both from the |
| 1093 |
* original buffer timestamp. Set PTS = DTS because the opposite has been |
| 1094 |
* observed to cause DTS going backwards */ |
| 1095 |
GST_LOG_OBJECT (parse, |
| 1096 |
"Found DTS (%" GST_TIME_FORMAT ") > PTS (%" GST_TIME_FORMAT |
| 1097 |
"), set PTS = DTS", GST_TIME_ARGS (GST_BUFFER_DTS (buffer)), |
| 1098 |
GST_TIME_ARGS (GST_BUFFER_PTS (buffer))); |
| 1099 |
GST_BUFFER_PTS (buffer) = GST_BUFFER_DTS (buffer); |
| 1100 |
} |
| 1101 |
|
| 1102 |
if (!GST_BUFFER_DURATION_IS_VALID (buffer) && |
| 1103 |
GST_CLOCK_TIME_IS_VALID (parse->priv->frame_duration)) { |
| 1104 |
GST_BUFFER_DURATION (buffer) = parse->priv->frame_duration; |
| 1105 |
} |
| 1106 |
return GST_FLOW_OK; |
| 1107 |
} |
| 1108 |
|
| 1109 |
/* gst_base_parse_convert: |
| 1110 |
* @parse: #GstBaseParse. |
| 1111 |
* @src_format: #GstFormat describing the source format. |
| 1112 |
* @src_value: Source value to be converted. |
| 1113 |
* @dest_format: #GstFormat defining the converted format. |
| 1114 |
* @dest_value: (out): Pointer where the conversion result will be put. |
| 1115 |
* |
| 1116 |
* Converts using configured "convert" vmethod in #GstBaseParse class. |
| 1117 |
* |
| 1118 |
* Returns: %TRUE if conversion was successful. |
| 1119 |
*/ |
| 1120 |
static gboolean |
| 1121 |
gst_base_parse_convert (GstBaseParse * parse, |
| 1122 |
GstFormat src_format, |
| 1123 |
gint64 src_value, GstFormat dest_format, gint64 * dest_value) |
| 1124 |
{ |
| 1125 |
GstBaseParseClass *klass = GST_BASE_PARSE_GET_CLASS (parse); |
| 1126 |
gboolean ret; |
| 1127 |
|
| 1128 |
g_return_val_if_fail (dest_value != NULL, FALSE); |
| 1129 |
|
| 1130 |
if (!klass->convert) |
| 1131 |
return FALSE; |
| 1132 |
|
| 1133 |
ret = klass->convert (parse, src_format, src_value, dest_format, dest_value); |
| 1134 |
|
| 1135 |
#ifndef GST_DISABLE_GST_DEBUG |
| 1136 |
{ |
| 1137 |
if (ret) { |
| 1138 |
if (src_format == GST_FORMAT_TIME && dest_format == GST_FORMAT_BYTES) { |
| 1139 |
GST_LOG_OBJECT (parse, |
| 1140 |
"TIME -> BYTES: %" GST_TIME_FORMAT " -> %" G_GINT64_FORMAT, |
| 1141 |
GST_TIME_ARGS (src_value), *dest_value); |
| 1142 |
} else if (dest_format == GST_FORMAT_TIME && |
| 1143 |
src_format == GST_FORMAT_BYTES) { |
| 1144 |
GST_LOG_OBJECT (parse, |
| 1145 |
"BYTES -> TIME: %" G_GINT64_FORMAT " -> %" GST_TIME_FORMAT, |
| 1146 |
src_value, GST_TIME_ARGS (*dest_value)); |
| 1147 |
} else { |
| 1148 |
GST_LOG_OBJECT (parse, |
| 1149 |
"%s -> %s: %" G_GINT64_FORMAT " -> %" G_GINT64_FORMAT, |
| 1150 |
GST_STR_NULL (gst_format_get_name (src_format)), |
| 1151 |
GST_STR_NULL (gst_format_get_name (dest_format)), |
| 1152 |
src_value, *dest_value); |
| 1153 |
} |
| 1154 |
} else { |
| 1155 |
GST_DEBUG_OBJECT (parse, "conversion failed"); |
| 1156 |
} |
| 1157 |
} |
| 1158 |
#endif |
| 1159 |
|
| 1160 |
return ret; |
| 1161 |
} |
| 1162 |
|
| 1163 |
static gboolean |
| 1164 |
update_upstream_provided (const GstIdStr * field, const GValue * value, |
| 1165 |
gpointer user_data) |
| 1166 |
{ |
| 1167 |
GstCaps *default_caps = user_data; |
| 1168 |
gint i; |
| 1169 |
gint caps_size; |
| 1170 |
|
| 1171 |
caps_size = gst_caps_get_size (default_caps); |
| 1172 |
for (i = 0; i < caps_size; i++) { |
| 1173 |
GstStructure *structure = gst_caps_get_structure (default_caps, i); |
| 1174 |
if (!gst_structure_has_field (structure, gst_id_str_as_str (field))) { |
| 1175 |
gst_structure_id_str_set_value (structure, field, value); |
| 1176 |
} else { |
| 1177 |
const GValue *v = gst_structure_id_str_get_value (structure, field); |
| 1178 |
|
| 1179 |
// If a downstream caps field is not fixed and the upstream value is a |
| 1180 |
// subset, take over the value from the upstream caps. |
| 1181 |
// Otherwise let gst_caps_fixate() take care of it later. |
| 1182 |
if (!gst_value_is_fixed (v) && gst_value_is_subset (value, v)) { |
| 1183 |
gst_structure_id_str_set_value (structure, field, value); |
| 1184 |
} |
| 1185 |
} |
| 1186 |
} |
| 1187 |
|
| 1188 |
return TRUE; |
| 1189 |
} |
| 1190 |
|
| 1191 |
static GstCaps * |
| 1192 |
gst_base_parse_negotiate_default_caps (GstBaseParse * parse) |
| 1193 |
{ |
| 1194 |
GstCaps *caps, *templcaps; |
| 1195 |
GstCaps *sinkcaps = NULL; |
| 1196 |
GstCaps *default_caps = NULL; |
| 1197 |
GstStructure *structure; |
| 1198 |
|
| 1199 |
templcaps = gst_pad_get_pad_template_caps (GST_BASE_PARSE_SRC_PAD (parse)); |
| 1200 |
caps = gst_pad_peer_query_caps (GST_BASE_PARSE_SRC_PAD (parse), templcaps); |
| 1201 |
if (caps) |
| 1202 |
gst_caps_unref (templcaps); |
| 1203 |
else |
| 1204 |
caps = templcaps; |
| 1205 |
templcaps = NULL; |
| 1206 |
|
| 1207 |
if (!caps || gst_caps_is_empty (caps) || gst_caps_is_any (caps)) { |
| 1208 |
goto caps_error; |
| 1209 |
} |
| 1210 |
|
| 1211 |
GST_LOG_OBJECT (parse, "peer caps %" GST_PTR_FORMAT, caps); |
| 1212 |
|
| 1213 |
/* before fixating, try to use whatever upstream provided */ |
| 1214 |
default_caps = gst_caps_copy (caps); |
| 1215 |
sinkcaps = gst_pad_get_current_caps (GST_BASE_PARSE_SINK_PAD (parse)); |
| 1216 |
|
| 1217 |
GST_LOG_OBJECT (parse, "current caps %" GST_PTR_FORMAT " for sinkpad", |
| 1218 |
sinkcaps); |
| 1219 |
|
| 1220 |
if (sinkcaps) { |
| 1221 |
structure = gst_caps_get_structure (sinkcaps, 0); |
| 1222 |
gst_structure_foreach_id_str (structure, update_upstream_provided, |
| 1223 |
default_caps); |
| 1224 |
} |
| 1225 |
|
| 1226 |
default_caps = gst_caps_fixate (default_caps); |
| 1227 |
|
| 1228 |
if (!default_caps) { |
| 1229 |
GST_WARNING_OBJECT (parse, "Failed to create default caps !"); |
| 1230 |
goto caps_error; |
| 1231 |
} |
| 1232 |
|
| 1233 |
GST_INFO_OBJECT (parse, |
| 1234 |
"Chose default caps %" GST_PTR_FORMAT " for initial gap", default_caps); |
| 1235 |
|
| 1236 |
if (sinkcaps) |
| 1237 |
gst_caps_unref (sinkcaps); |
| 1238 |
gst_caps_unref (caps); |
| 1239 |
|
| 1240 |
return default_caps; |
| 1241 |
|
| 1242 |
caps_error: |
| 1243 |
{ |
| 1244 |
if (caps) |
| 1245 |
gst_caps_unref (caps); |
| 1246 |
if (sinkcaps) |
| 1247 |
gst_caps_unref (sinkcaps); |
| 1248 |
return NULL; |
| 1249 |
} |
| 1250 |
} |
| 1251 |
|
| 1252 |
/* gst_base_parse_sink_event: |
| 1253 |
* @pad: #GstPad that received the event. |
| 1254 |
* @event: #GstEvent to be handled. |
| 1255 |
* |
| 1256 |
* Handler for sink pad events. |
| 1257 |
* |
| 1258 |
* Returns: %TRUE if the event was handled. |
| 1259 |
*/ |
| 1260 |
static gboolean |
| 1261 |
gst_base_parse_sink_event (GstPad * pad, GstObject * parent, GstEvent * event) |
| 1262 |
{ |
| 1263 |
GstBaseParse *parse = GST_BASE_PARSE (parent); |
| 1264 |
GstBaseParseClass *bclass = GST_BASE_PARSE_GET_CLASS (parse); |
| 1265 |
gboolean ret; |
| 1266 |
|
| 1267 |
ret = bclass->sink_event (parse, event); |
| 1268 |
|
| 1269 |
return ret; |
| 1270 |
} |
| 1271 |
|
| 1272 |
/* gst_base_parse_sink_event_default: |
| 1273 |
* @parse: #GstBaseParse. |
| 1274 |
* @event: #GstEvent to be handled. |
| 1275 |
* |
| 1276 |
* Element-level event handler function. |
| 1277 |
* |
| 1278 |
* The event will be unreffed only if it has been handled and this |
| 1279 |
* function returns %TRUE |
| 1280 |
* |
| 1281 |
* Returns: %TRUE if the event was handled and not need forwarding. |
| 1282 |
*/ |
| 1283 |
static gboolean |
| 1284 |
gst_base_parse_sink_event_default (GstBaseParse * parse, GstEvent * event) |
| 1285 |
{ |
| 1286 |
GstBaseParseClass *klass = GST_BASE_PARSE_GET_CLASS (parse); |
| 1287 |
gboolean ret = FALSE; |
| 1288 |
gboolean forward_immediate = FALSE; |
| 1289 |
|
| 1290 |
GST_DEBUG_OBJECT (parse, "handling event %d, %s", GST_EVENT_TYPE (event), |
| 1291 |
GST_EVENT_TYPE_NAME (event)); |
| 1292 |
|
| 1293 |
switch (GST_EVENT_TYPE (event)) { |
| 1294 |
case GST_EVENT_CAPS: |
| 1295 |
{ |
| 1296 |
GstCaps *caps; |
| 1297 |
|
| 1298 |
gst_event_parse_caps (event, &caps); |
| 1299 |
GST_DEBUG_OBJECT (parse, "caps: %" GST_PTR_FORMAT, caps); |
| 1300 |
|
| 1301 |
if (klass->set_sink_caps) |
| 1302 |
ret = klass->set_sink_caps (parse, caps); |
| 1303 |
else |
| 1304 |
ret = TRUE; |
| 1305 |
|
| 1306 |
/* will send our own caps downstream */ |
| 1307 |
gst_event_unref (event); |
| 1308 |
event = NULL; |
| 1309 |
break; |
| 1310 |
} |
| 1311 |
case GST_EVENT_SEGMENT: |
| 1312 |
{ |
| 1313 |
const GstSegment *in_segment; |
| 1314 |
GstSegment out_segment; |
| 1315 |
gint64 offset = 0, next_dts; |
| 1316 |
|
| 1317 |
parse->priv->segment_seqnum = gst_event_get_seqnum (event); |
| 1318 |
gst_event_parse_segment (event, &in_segment); |
| 1319 |
gst_segment_init (&out_segment, GST_FORMAT_TIME); |
| 1320 |
out_segment.rate = in_segment->rate; |
| 1321 |
out_segment.applied_rate = in_segment->applied_rate; |
| 1322 |
|
| 1323 |
GST_DEBUG_OBJECT (parse, "New segment %" GST_SEGMENT_FORMAT, in_segment); |
| 1324 |
GST_DEBUG_OBJECT (parse, "Current segment %" GST_SEGMENT_FORMAT, |
| 1325 |
&parse->segment); |
| 1326 |
|
| 1327 |
parse->priv->upstream_format = in_segment->format; |
| 1328 |
if (in_segment->format == GST_FORMAT_BYTES) { |
| 1329 |
GstBaseParseSeek *seek = NULL; |
| 1330 |
GSList *node; |
| 1331 |
|
| 1332 |
/* stop time is allowed to be open-ended, but not start & pos */ |
| 1333 |
offset = in_segment->time; |
| 1334 |
|
| 1335 |
GST_OBJECT_LOCK (parse); |
| 1336 |
for (node = parse->priv->pending_seeks; node; node = node->next) { |
| 1337 |
GstBaseParseSeek *tmp = node->data; |
| 1338 |
|
| 1339 |
if (tmp->offset == offset) { |
| 1340 |
seek = tmp; |
| 1341 |
break; |
| 1342 |
} |
| 1343 |
} |
| 1344 |
parse->priv->pending_seeks = |
| 1345 |
g_slist_remove (parse->priv->pending_seeks, seek); |
| 1346 |
GST_OBJECT_UNLOCK (parse); |
| 1347 |
|
| 1348 |
if (seek) { |
| 1349 |
GST_DEBUG_OBJECT (parse, |
| 1350 |
"Matched newsegment to%s seek: %" GST_SEGMENT_FORMAT, |
| 1351 |
seek->accurate ? " accurate" : "", &seek->segment); |
| 1352 |
|
| 1353 |
out_segment.start = seek->segment.start; |
| 1354 |
out_segment.stop = seek->segment.stop; |
| 1355 |
out_segment.time = seek->segment.start; |
| 1356 |
|
| 1357 |
next_dts = seek->start_ts; |
| 1358 |
parse->priv->exact_position = seek->accurate; |
| 1359 |
g_free (seek); |
| 1360 |
} else { |
| 1361 |
/* best attempt convert */ |
| 1362 |
/* as these are only estimates, stop is kept open-ended to avoid |
| 1363 |
* premature cutting */ |
| 1364 |
gst_base_parse_convert (parse, GST_FORMAT_BYTES, in_segment->start, |
| 1365 |
GST_FORMAT_TIME, (gint64 *) & next_dts); |
| 1366 |
|
| 1367 |
out_segment.start = next_dts; |
| 1368 |
out_segment.stop = GST_CLOCK_TIME_NONE; |
| 1369 |
out_segment.time = next_dts; |
| 1370 |
|
| 1371 |
parse->priv->exact_position = (in_segment->start == 0); |
| 1372 |
} |
| 1373 |
|
| 1374 |
gst_event_unref (event); |
| 1375 |
|
| 1376 |
event = gst_event_new_segment (&out_segment); |
| 1377 |
gst_event_set_seqnum (event, parse->priv->segment_seqnum); |
| 1378 |
|
| 1379 |
GST_DEBUG_OBJECT (parse, "Converted incoming segment to TIME. %" |
| 1380 |
GST_SEGMENT_FORMAT, in_segment); |
| 1381 |
|
| 1382 |
} else if (in_segment->format != GST_FORMAT_TIME) { |
| 1383 |
/* Unknown incoming segment format. Output a default open-ended |
| 1384 |
* TIME segment */ |
| 1385 |
gst_event_unref (event); |
| 1386 |
|
| 1387 |
out_segment.start = 0; |
| 1388 |
out_segment.stop = GST_CLOCK_TIME_NONE; |
| 1389 |
out_segment.time = 0; |
| 1390 |
|
| 1391 |
event = gst_event_new_segment (&out_segment); |
| 1392 |
gst_event_set_seqnum (event, parse->priv->segment_seqnum); |
| 1393 |
|
| 1394 |
next_dts = 0; |
| 1395 |
} else { |
| 1396 |
/* not considered BYTE seekable if it is talking to us in TIME, |
| 1397 |
* whatever else it might claim */ |
| 1398 |
parse->priv->upstream_seekable = FALSE; |
| 1399 |
next_dts = GST_CLOCK_TIME_NONE; |
| 1400 |
gst_event_copy_segment (event, &out_segment); |
| 1401 |
} |
| 1402 |
|
| 1403 |
GST_DEBUG_OBJECT (parse, "OUT segment %" GST_SEGMENT_FORMAT, |
| 1404 |
&out_segment); |
| 1405 |
memcpy (&parse->segment, &out_segment, sizeof (GstSegment)); |
| 1406 |
|
| 1407 |
/* |
| 1408 |
gst_segment_set_newsegment (&parse->segment, update, rate, |
| 1409 |
applied_rate, format, start, stop, start); |
| 1410 |
*/ |
| 1411 |
|
| 1412 |
ret = TRUE; |
| 1413 |
|
| 1414 |
/* save the segment for later, right before we push a new buffer so that |
| 1415 |
* the caps are fixed and the next linked element can receive |
| 1416 |
* the segment but finish the current segment */ |
| 1417 |
GST_DEBUG_OBJECT (parse, "draining current segment"); |
| 1418 |
if (in_segment->rate > 0.0) |
| 1419 |
gst_base_parse_drain (parse); |
| 1420 |
else |
| 1421 |
gst_base_parse_finish_fragment (parse, FALSE); |
| 1422 |
gst_adapter_clear (parse->priv->adapter); |
| 1423 |
|
| 1424 |
parse->priv->offset = offset; |
| 1425 |
parse->priv->sync_offset = offset; |
| 1426 |
parse->priv->next_dts = next_dts; |
| 1427 |
parse->priv->next_pts = GST_CLOCK_TIME_NONE; |
| 1428 |
parse->priv->last_pts = GST_CLOCK_TIME_NONE; |
| 1429 |
parse->priv->last_dts = GST_CLOCK_TIME_NONE; |
| 1430 |
parse->priv->prev_pts = GST_CLOCK_TIME_NONE; |
| 1431 |
parse->priv->prev_dts = GST_CLOCK_TIME_NONE; |
| 1432 |
parse->priv->prev_dts_from_pts = FALSE; |
| 1433 |
parse->priv->discont = TRUE; |
| 1434 |
parse->priv->seen_keyframe = FALSE; |
| 1435 |
parse->priv->skip = 0; |
| 1436 |
break; |
| 1437 |
} |
| 1438 |
|
| 1439 |
case GST_EVENT_SEGMENT_DONE: |
| 1440 |
/* need to drain now, rather than upon a new segment, |
| 1441 |
* since that would have SEGMENT_DONE come before potential |
| 1442 |
* delayed last part of the current segment */ |
| 1443 |
GST_DEBUG_OBJECT (parse, "draining current segment"); |
| 1444 |
if (parse->segment.rate > 0.0) |
| 1445 |
gst_base_parse_drain (parse); |
| 1446 |
else |
| 1447 |
gst_base_parse_finish_fragment (parse, FALSE); |
| 1448 |
/* Also forward event immediately, there might be no new data |
| 1449 |
* coming afterwards that would allow us to forward it later */ |
| 1450 |
forward_immediate = TRUE; |
| 1451 |
break; |
| 1452 |
|
| 1453 |
case GST_EVENT_FLUSH_START: |
| 1454 |
GST_OBJECT_LOCK (parse); |
| 1455 |
parse->priv->flushing = TRUE; |
| 1456 |
GST_OBJECT_UNLOCK (parse); |
| 1457 |
break; |
| 1458 |
|
| 1459 |
case GST_EVENT_FLUSH_STOP: |
| 1460 |
gst_adapter_clear (parse->priv->adapter); |
| 1461 |
gst_base_parse_clear_queues (parse, FALSE); |
| 1462 |
parse->priv->flushing = FALSE; |
| 1463 |
parse->priv->discont = TRUE; |
| 1464 |
parse->priv->last_pts = GST_CLOCK_TIME_NONE; |
| 1465 |
parse->priv->last_dts = GST_CLOCK_TIME_NONE; |
| 1466 |
parse->priv->new_frame = TRUE; |
| 1467 |
parse->priv->skip = 0; |
| 1468 |
|
| 1469 |
forward_immediate = TRUE; |
| 1470 |
break; |
| 1471 |
|
| 1472 |
case GST_EVENT_EOS: |
| 1473 |
if (parse->segment.rate > 0.0) |
| 1474 |
gst_base_parse_drain (parse); |
| 1475 |
else |
| 1476 |
gst_base_parse_finish_fragment (parse, TRUE); |
| 1477 |
|
| 1478 |
/* If we STILL have zero frames processed, fire an error */ |
| 1479 |
if (parse->priv->framecount == 0 && !parse->priv->saw_gaps && |
| 1480 |
!parse->priv->first_buffer) { |
| 1481 |
GST_ELEMENT_ERROR (parse, STREAM, WRONG_TYPE, |
| 1482 |
("No valid frames found before end of stream"), (NULL)); |
| 1483 |
} |
| 1484 |
|
| 1485 |
if (!parse->priv->saw_gaps |
| 1486 |
&& parse->priv->framecount < MIN_FRAMES_TO_POST_BITRATE) { |
| 1487 |
/* We've not posted bitrate tags yet - do so now */ |
| 1488 |
gst_base_parse_queue_tag_event_update (parse); |
| 1489 |
} |
| 1490 |
|
| 1491 |
/* newsegment and other serialized events before eos */ |
| 1492 |
gst_base_parse_push_pending_events (parse); |
| 1493 |
|
| 1494 |
forward_immediate = TRUE; |
| 1495 |
break; |
| 1496 |
case GST_EVENT_CUSTOM_DOWNSTREAM:{ |
| 1497 |
/* FIXME: Code duplicated from libgstvideo because core can't depend on -base */ |
| 1498 |
#ifndef GST_VIDEO_EVENT_STILL_STATE_NAME |
| 1499 |
#define GST_VIDEO_EVENT_STILL_STATE_NAME "GstEventStillFrame" |
| 1500 |
#endif |
| 1501 |
|
| 1502 |
const GstStructure *s; |
| 1503 |
gboolean ev_still_state; |
| 1504 |
|
| 1505 |
s = gst_event_get_structure (event); |
| 1506 |
if (s != NULL && |
| 1507 |
gst_structure_has_name (s, GST_VIDEO_EVENT_STILL_STATE_NAME) && |
| 1508 |
gst_structure_get_boolean (s, "still-state", &ev_still_state)) { |
| 1509 |
if (ev_still_state) { |
| 1510 |
GST_DEBUG_OBJECT (parse, "draining current data for still-frame"); |
| 1511 |
if (parse->segment.rate > 0.0) |
| 1512 |
gst_base_parse_drain (parse); |
| 1513 |
else |
| 1514 |
gst_base_parse_finish_fragment (parse, TRUE); |
| 1515 |
} |
| 1516 |
forward_immediate = TRUE; |
| 1517 |
} |
| 1518 |
break; |
| 1519 |
} |
| 1520 |
case GST_EVENT_GAP: |
| 1521 |
{ |
| 1522 |
GST_DEBUG_OBJECT (parse, "draining current data due to gap event"); |
| 1523 |
|
| 1524 |
/* Ensure we have caps before forwarding the event */ |
| 1525 |
if (!gst_pad_has_current_caps (GST_BASE_PARSE_SRC_PAD (parse))) { |
| 1526 |
GstCaps *default_caps = NULL; |
| 1527 |
if ((default_caps = gst_base_parse_negotiate_default_caps (parse))) { |
| 1528 |
GList *l; |
| 1529 |
GstEvent *caps_event = gst_event_new_caps (default_caps); |
| 1530 |
|
| 1531 |
GST_DEBUG_OBJECT (parse, |
| 1532 |
"Store caps event to pending list for initial pre-rolling"); |
| 1533 |
|
| 1534 |
/* Events are in decreasing order. Go down the list until we |
| 1535 |
* find the first pre-CAPS event and insert our CAPS event there. |
| 1536 |
* |
| 1537 |
* There should be a SEGMENT event already, which is > CAPS */ |
| 1538 |
for (l = parse->priv->pending_events; l; l = l->next) { |
| 1539 |
GstEvent *e = l->data; |
| 1540 |
|
| 1541 |
if (GST_EVENT_TYPE (e) < GST_EVENT_CAPS) { |
| 1542 |
parse->priv->pending_events = |
| 1543 |
g_list_insert_before (parse->priv->pending_events, l, |
| 1544 |
caps_event); |
| 1545 |
break; |
| 1546 |
} |
| 1547 |
} |
| 1548 |
/* No pending event that is < CAPS, so we have to add it at the very |
| 1549 |
* end of the list */ |
| 1550 |
if (!l) { |
| 1551 |
parse->priv->pending_events = |
| 1552 |
g_list_append (parse->priv->pending_events, caps_event); |
| 1553 |
} |
| 1554 |
gst_caps_unref (default_caps); |
| 1555 |
} else { |
| 1556 |
gst_event_unref (event); |
| 1557 |
event = NULL; |
| 1558 |
ret = FALSE; |
| 1559 |
GST_ELEMENT_ERROR (parse, STREAM, FORMAT, (NULL), |
| 1560 |
("Parser output not negotiated before GAP event.")); |
| 1561 |
break; |
| 1562 |
} |
| 1563 |
} |
| 1564 |
|
| 1565 |
gst_base_parse_push_pending_events (parse); |
| 1566 |
|
| 1567 |
if (parse->segment.rate > 0.0) |
| 1568 |
gst_base_parse_drain (parse); |
| 1569 |
else |
| 1570 |
gst_base_parse_finish_fragment (parse, TRUE); |
| 1571 |
forward_immediate = TRUE; |
| 1572 |
parse->priv->saw_gaps = TRUE; |
| 1573 |
break; |
| 1574 |
} |
| 1575 |
case GST_EVENT_TAG: |
| 1576 |
{ |
| 1577 |
GstTagList *tags = NULL; |
| 1578 |
|
| 1579 |
gst_event_parse_tag (event, &tags); |
| 1580 |
|
| 1581 |
/* We only care about stream tags here, global tags we just forward */ |
| 1582 |
if (gst_tag_list_get_scope (tags) != GST_TAG_SCOPE_STREAM) |
| 1583 |
break; |
| 1584 |
|
| 1585 |
gst_base_parse_set_upstream_tags (parse, tags); |
| 1586 |
gst_base_parse_queue_tag_event_update (parse); |
| 1587 |
parse->priv->tags_changed = FALSE; |
| 1588 |
gst_event_unref (event); |
| 1589 |
event = NULL; |
| 1590 |
ret = TRUE; |
| 1591 |
break; |
| 1592 |
} |
| 1593 |
case GST_EVENT_STREAM_START: |
| 1594 |
{ |
| 1595 |
if (parse->priv->pad_mode != GST_PAD_MODE_PULL) |
| 1596 |
forward_immediate = TRUE; |
| 1597 |
|
| 1598 |
gst_base_parse_set_upstream_tags (parse, NULL); |
| 1599 |
parse->priv->tags_changed = TRUE; |
| 1600 |
break; |
| 1601 |
} |
| 1602 |
default: |
| 1603 |
break; |
| 1604 |
} |
| 1605 |
|
| 1606 |
/* Forward non-serialized events and EOS/FLUSH_STOP immediately. |
| 1607 |
* For EOS this is required because no buffer or serialized event |
| 1608 |
* will come after EOS and nothing could trigger another |
| 1609 |
* _finish_frame() call. * |
| 1610 |
* If the subclass handles sending of EOS manually it can return |
| 1611 |
* _DROPPED from ::finish() and all other subclasses should have |
| 1612 |
* decoded/flushed all remaining data before this |
| 1613 |
* |
| 1614 |
* For FLUSH_STOP this is required because it is expected |
| 1615 |
* to be forwarded immediately and no buffers are queued anyway. |
| 1616 |
*/ |
| 1617 |
if (event) { |
| 1618 |
if (!GST_EVENT_IS_SERIALIZED (event) || forward_immediate) { |
| 1619 |
ret = gst_pad_push_event (parse->srcpad, event); |
| 1620 |
} else { |
| 1621 |
parse->priv->pending_events = |
| 1622 |
g_list_prepend (parse->priv->pending_events, event); |
| 1623 |
ret = TRUE; |
| 1624 |
} |
| 1625 |
} |
| 1626 |
|
| 1627 |
GST_DEBUG_OBJECT (parse, "event handled"); |
| 1628 |
|
| 1629 |
return ret; |
| 1630 |
} |
| 1631 |
|
| 1632 |
static gboolean |
| 1633 |
gst_base_parse_sink_query_default (GstBaseParse * parse, GstQuery * query) |
| 1634 |
{ |
| 1635 |
GstPad *pad; |
| 1636 |
gboolean res; |
| 1637 |
|
| 1638 |
pad = GST_BASE_PARSE_SINK_PAD (parse); |
| 1639 |
|
| 1640 |
switch (GST_QUERY_TYPE (query)) { |
| 1641 |
case GST_QUERY_BITRATE: |
| 1642 |
{ |
| 1643 |
if (parse->priv->avg_bitrate) { |
| 1644 |
gst_query_set_bitrate (query, parse->priv->avg_bitrate); |
| 1645 |
res = TRUE; |
| 1646 |
} else { |
| 1647 |
res = gst_pad_query_default (pad, GST_OBJECT_CAST (parse), query); |
| 1648 |
} |
| 1649 |
break; |
| 1650 |
} |
| 1651 |
case GST_QUERY_CAPS: |
| 1652 |
{ |
| 1653 |
GstBaseParseClass *bclass; |
| 1654 |
|
| 1655 |
bclass = GST_BASE_PARSE_GET_CLASS (parse); |
| 1656 |
|
| 1657 |
if (bclass->get_sink_caps) { |
| 1658 |
GstCaps *caps, *filter; |
| 1659 |
|
| 1660 |
gst_query_parse_caps (query, &filter); |
| 1661 |
caps = bclass->get_sink_caps (parse, filter); |
| 1662 |
GST_LOG_OBJECT (parse, "sink getcaps returning caps %" GST_PTR_FORMAT, |
| 1663 |
caps); |
| 1664 |
gst_query_set_caps_result (query, caps); |
| 1665 |
gst_caps_unref (caps); |
| 1666 |
|
| 1667 |
res = TRUE; |
| 1668 |
} else { |
| 1669 |
GstCaps *caps, *template_caps, *filter; |
| 1670 |
|
| 1671 |
gst_query_parse_caps (query, &filter); |
| 1672 |
template_caps = gst_pad_get_pad_template_caps (pad); |
| 1673 |
if (filter != NULL) { |
| 1674 |
caps = |
| 1675 |
gst_caps_intersect_full (filter, template_caps, |
| 1676 |
GST_CAPS_INTERSECT_FIRST); |
| 1677 |
gst_caps_unref (template_caps); |
| 1678 |
} else { |
| 1679 |
caps = template_caps; |
| 1680 |
} |
| 1681 |
gst_query_set_caps_result (query, caps); |
| 1682 |
gst_caps_unref (caps); |
| 1683 |
|
| 1684 |
res = TRUE; |
| 1685 |
} |
| 1686 |
break; |
| 1687 |
} |
| 1688 |
default: |
| 1689 |
{ |
| 1690 |
res = gst_pad_query_default (pad, GST_OBJECT_CAST (parse), query); |
| 1691 |
break; |
| 1692 |
} |
| 1693 |
} |
| 1694 |
|
| 1695 |
return res; |
| 1696 |
} |
| 1697 |
|
| 1698 |
static gboolean |
| 1699 |
gst_base_parse_sink_query (GstPad * pad, GstObject * parent, GstQuery * query) |
| 1700 |
{ |
| 1701 |
GstBaseParseClass *bclass; |
| 1702 |
GstBaseParse *parse; |
| 1703 |
gboolean ret; |
| 1704 |
|
| 1705 |
parse = GST_BASE_PARSE (parent); |
| 1706 |
bclass = GST_BASE_PARSE_GET_CLASS (parse); |
| 1707 |
|
| 1708 |
GST_DEBUG_OBJECT (parse, "%s query", GST_QUERY_TYPE_NAME (query)); |
| 1709 |
|
| 1710 |
if (bclass->sink_query) |
| 1711 |
ret = bclass->sink_query (parse, query); |
| 1712 |
else |
| 1713 |
ret = FALSE; |
| 1714 |
|
| 1715 |
GST_LOG_OBJECT (parse, "%s query result: %d %" GST_PTR_FORMAT, |
| 1716 |
GST_QUERY_TYPE_NAME (query), ret, query); |
| 1717 |
|
| 1718 |
return ret; |
| 1719 |
} |
| 1720 |
|
| 1721 |
static gboolean |
| 1722 |
gst_base_parse_src_query (GstPad * pad, GstObject * parent, GstQuery * query) |
| 1723 |
{ |
| 1724 |
GstBaseParseClass *bclass; |
| 1725 |
GstBaseParse *parse; |
| 1726 |
gboolean ret; |
| 1727 |
|
| 1728 |
parse = GST_BASE_PARSE (parent); |
| 1729 |
bclass = GST_BASE_PARSE_GET_CLASS (parse); |
| 1730 |
|
| 1731 |
GST_DEBUG_OBJECT (parse, "%s query: %" GST_PTR_FORMAT, |
| 1732 |
GST_QUERY_TYPE_NAME (query), query); |
| 1733 |
|
| 1734 |
if (bclass->src_query) |
| 1735 |
ret = bclass->src_query (parse, query); |
| 1736 |
else |
| 1737 |
ret = FALSE; |
| 1738 |
|
| 1739 |
GST_LOG_OBJECT (parse, "%s query result: %d %" GST_PTR_FORMAT, |
| 1740 |
GST_QUERY_TYPE_NAME (query), ret, query); |
| 1741 |
|
| 1742 |
return ret; |
| 1743 |
} |
| 1744 |
|
| 1745 |
/* gst_base_parse_src_event: |
| 1746 |
* @pad: #GstPad that received the event. |
| 1747 |
* @event: #GstEvent that was received. |
| 1748 |
* |
| 1749 |
* Handler for source pad events. |
| 1750 |
* |
| 1751 |
* Returns: %TRUE if the event was handled. |
| 1752 |
*/ |
| 1753 |
static gboolean |
| 1754 |
gst_base_parse_src_event (GstPad * pad, GstObject * parent, GstEvent * event) |
| 1755 |
{ |
| 1756 |
GstBaseParse *parse; |
| 1757 |
GstBaseParseClass *bclass; |
| 1758 |
gboolean ret = TRUE; |
| 1759 |
|
| 1760 |
parse = GST_BASE_PARSE (parent); |
| 1761 |
bclass = GST_BASE_PARSE_GET_CLASS (parse); |
| 1762 |
|
| 1763 |
GST_DEBUG_OBJECT (parse, "event %d, %s", GST_EVENT_TYPE (event), |
| 1764 |
GST_EVENT_TYPE_NAME (event)); |
| 1765 |
|
| 1766 |
if (bclass->src_event) |
| 1767 |
ret = bclass->src_event (parse, event); |
| 1768 |
else |
| 1769 |
gst_event_unref (event); |
| 1770 |
|
| 1771 |
return ret; |
| 1772 |
} |
| 1773 |
|
| 1774 |
static gboolean |
| 1775 |
gst_base_parse_is_seekable (GstBaseParse * parse) |
| 1776 |
{ |
| 1777 |
/* FIXME: could do more here, e.g. check index or just send data from 0 |
| 1778 |
* in pull mode and let decoder/sink clip */ |
| 1779 |
return parse->priv->syncable; |
| 1780 |
} |
| 1781 |
|
| 1782 |
/* gst_base_parse_src_event_default: |
| 1783 |
* @parse: #GstBaseParse. |
| 1784 |
* @event: #GstEvent that was received. |
| 1785 |
* |
| 1786 |
* Default srcpad event handler. |
| 1787 |
* |
| 1788 |
* Returns: %TRUE if the event was handled and can be dropped. |
| 1789 |
*/ |
| 1790 |
static gboolean |
| 1791 |
gst_base_parse_src_event_default (GstBaseParse * parse, GstEvent * event) |
| 1792 |
{ |
| 1793 |
gboolean res = FALSE; |
| 1794 |
|
| 1795 |
switch (GST_EVENT_TYPE (event)) { |
| 1796 |
case GST_EVENT_SEEK: |
| 1797 |
if (gst_base_parse_is_seekable (parse)) |
| 1798 |
res = gst_base_parse_handle_seek (parse, event); |
| 1799 |
break; |
| 1800 |
default: |
| 1801 |
res = gst_pad_event_default (parse->srcpad, GST_OBJECT_CAST (parse), |
| 1802 |
event); |
| 1803 |
break; |
| 1804 |
} |
| 1805 |
return res; |
| 1806 |
} |
| 1807 |
|
| 1808 |
|
| 1809 |
/** |
| 1810 |
* gst_base_parse_convert_default: |
| 1811 |
* @parse: #GstBaseParse. |
| 1812 |
* @src_format: #GstFormat describing the source format. |
| 1813 |
* @src_value: Source value to be converted. |
| 1814 |
* @dest_format: #GstFormat defining the converted format. |
| 1815 |
* @dest_value: (out): Pointer where the conversion result will be put. |
| 1816 |
* |
| 1817 |
* Default implementation of #GstBaseParseClass::convert. |
| 1818 |
* |
| 1819 |
* Returns: %TRUE if conversion was successful. |
| 1820 |
*/ |
| 1821 |
gboolean |
| 1822 |
gst_base_parse_convert_default (GstBaseParse * parse, |
| 1823 |
GstFormat src_format, |
| 1824 |
gint64 src_value, GstFormat dest_format, gint64 * dest_value) |
| 1825 |
{ |
| 1826 |
gboolean ret = FALSE; |
| 1827 |
guint64 bytes, duration; |
| 1828 |
|
| 1829 |
if (G_UNLIKELY (src_format == dest_format)) { |
| 1830 |
*dest_value = src_value; |
| 1831 |
return TRUE; |
| 1832 |
} |
| 1833 |
|
| 1834 |
if (G_UNLIKELY (src_value == -1)) { |
| 1835 |
*dest_value = -1; |
| 1836 |
return TRUE; |
| 1837 |
} |
| 1838 |
|
| 1839 |
if (G_UNLIKELY (src_value == 0)) { |
| 1840 |
*dest_value = 0; |
| 1841 |
return TRUE; |
| 1842 |
} |
| 1843 |
|
| 1844 |
if (parse->priv->upstream_format != GST_FORMAT_BYTES) { |
| 1845 |
/* don't do byte format conversions if we're not really parsing |
| 1846 |
* a raw elementary stream, since we don't really have BYTES |
| 1847 |
* position / duration info */ |
| 1848 |
if (src_format == GST_FORMAT_BYTES || dest_format == GST_FORMAT_BYTES) |
| 1849 |
goto no_slaved_conversions; |
| 1850 |
} |
| 1851 |
|
| 1852 |
/* need at least some frames */ |
| 1853 |
if (!parse->priv->framecount) |
| 1854 |
goto no_framecount; |
| 1855 |
|
| 1856 |
duration = parse->priv->acc_duration; |
| 1857 |
bytes = parse->priv->bytecount; |
| 1858 |
|
| 1859 |
if (G_UNLIKELY (!duration || !bytes)) |
| 1860 |
goto no_duration_bytes; |
| 1861 |
|
| 1862 |
if (src_format == GST_FORMAT_BYTES) { |
| 1863 |
if (dest_format == GST_FORMAT_TIME) { |
| 1864 |
/* BYTES -> TIME conversion */ |
| 1865 |
GST_DEBUG_OBJECT (parse, "converting bytes -> time"); |
| 1866 |
*dest_value = gst_util_uint64_scale (src_value, duration, bytes); |
| 1867 |
GST_DEBUG_OBJECT (parse, |
| 1868 |
"converted %" G_GINT64_FORMAT " bytes to %" GST_TIME_FORMAT, |
| 1869 |
src_value, GST_TIME_ARGS (*dest_value)); |
| 1870 |
ret = TRUE; |
| 1871 |
} else { |
| 1872 |
GST_DEBUG_OBJECT (parse, "converting bytes -> other not implemented"); |
| 1873 |
} |
| 1874 |
} else if (src_format == GST_FORMAT_TIME) { |
| 1875 |
if (dest_format == GST_FORMAT_BYTES) { |
| 1876 |
GST_DEBUG_OBJECT (parse, "converting time -> bytes"); |
| 1877 |
*dest_value = gst_util_uint64_scale (src_value, bytes, duration); |
| 1878 |
GST_DEBUG_OBJECT (parse, |
| 1879 |
"converted %" GST_TIME_FORMAT " to %" G_GINT64_FORMAT " bytes", |
| 1880 |
GST_TIME_ARGS (src_value), *dest_value); |
| 1881 |
ret = TRUE; |
| 1882 |
} else { |
| 1883 |
GST_DEBUG_OBJECT (parse, "converting time -> other not implemented"); |
| 1884 |
} |
| 1885 |
} else if (src_format == GST_FORMAT_DEFAULT) { |
| 1886 |
/* DEFAULT == frame-based */ |
| 1887 |
if (dest_format == GST_FORMAT_TIME) { |
| 1888 |
GST_DEBUG_OBJECT (parse, "converting default -> time"); |
| 1889 |
if (parse->priv->fps_den) { |
| 1890 |
*dest_value = gst_util_uint64_scale (src_value, |
| 1891 |
GST_SECOND * parse->priv->fps_den, parse->priv->fps_num); |
| 1892 |
ret = TRUE; |
| 1893 |
} |
| 1894 |
} else { |
| 1895 |
GST_DEBUG_OBJECT (parse, "converting default -> other not implemented"); |
| 1896 |
} |
| 1897 |
} else { |
| 1898 |
GST_DEBUG_OBJECT (parse, "conversion not implemented"); |
| 1899 |
} |
| 1900 |
return ret; |
| 1901 |
|
| 1902 |
/* ERRORS */ |
| 1903 |
no_framecount: |
| 1904 |
{ |
| 1905 |
GST_DEBUG_OBJECT (parse, "no framecount"); |
| 1906 |
return FALSE; |
| 1907 |
} |
| 1908 |
no_duration_bytes: |
| 1909 |
{ |
| 1910 |
GST_DEBUG_OBJECT (parse, "no duration %" G_GUINT64_FORMAT ", bytes %" |
| 1911 |
G_GUINT64_FORMAT, duration, bytes); |
| 1912 |
return FALSE; |
| 1913 |
} |
| 1914 |
no_slaved_conversions: |
| 1915 |
{ |
| 1916 |
GST_DEBUG_OBJECT (parse, |
| 1917 |
"Can't do format conversions when upstream format is not BYTES"); |
| 1918 |
return FALSE; |
| 1919 |
} |
| 1920 |
} |
| 1921 |
|
| 1922 |
static void |
| 1923 |
gst_base_parse_update_duration (GstBaseParse * parse) |
| 1924 |
{ |
| 1925 |
gint64 ptot, dest_value; |
| 1926 |
|
| 1927 |
if (!gst_pad_peer_query_duration (parse->sinkpad, GST_FORMAT_BYTES, &ptot)) |
| 1928 |
return; |
| 1929 |
|
| 1930 |
if (!gst_base_parse_convert (parse, GST_FORMAT_BYTES, ptot, |
| 1931 |
GST_FORMAT_TIME, &dest_value)) |
| 1932 |
return; |
| 1933 |
|
| 1934 |
/* inform if duration changed, but try to avoid spamming */ |
| 1935 |
parse->priv->estimated_drift += dest_value - parse->priv->estimated_duration; |
| 1936 |
|
| 1937 |
parse->priv->estimated_duration = dest_value; |
| 1938 |
GST_LOG_OBJECT (parse, |
| 1939 |
"updated estimated duration to %" GST_TIME_FORMAT, |
| 1940 |
GST_TIME_ARGS (dest_value)); |
| 1941 |
|
| 1942 |
if (parse->priv->estimated_drift > GST_SECOND || |
| 1943 |
parse->priv->estimated_drift < -GST_SECOND) { |
| 1944 |
gst_element_post_message (GST_ELEMENT (parse), |
| 1945 |
gst_message_new_duration_changed (GST_OBJECT (parse))); |
| 1946 |
parse->priv->estimated_drift = 0; |
| 1947 |
} |
| 1948 |
} |
| 1949 |
|
| 1950 |
/* gst_base_parse_update_bitrates: |
| 1951 |
* @parse: #GstBaseParse. |
| 1952 |
* @buffer: Current frame as a #GstBuffer |
| 1953 |
* |
| 1954 |
* Keeps track of the minimum and maximum bitrates, and also maintains a |
| 1955 |
* running average bitrate of the stream so far. |
| 1956 |
*/ |
| 1957 |
static void |
| 1958 |
gst_base_parse_update_bitrates (GstBaseParse * parse, GstBaseParseFrame * frame) |
| 1959 |
{ |
| 1960 |
guint64 data_len, frame_dur; |
| 1961 |
gint overhead; |
| 1962 |
guint frame_bitrate; |
| 1963 |
guint64 frame_bitrate64; |
| 1964 |
GstBuffer *buffer = frame->buffer; |
| 1965 |
|
| 1966 |
overhead = frame->overhead; |
| 1967 |
if (overhead == -1) |
| 1968 |
return; |
| 1969 |
|
| 1970 |
data_len = gst_buffer_get_size (buffer) - overhead; |
| 1971 |
parse->priv->data_bytecount += data_len; |
| 1972 |
|
| 1973 |
/* duration should be valid by now, |
| 1974 |
* either set by subclass or maybe based on fps settings */ |
| 1975 |
if (GST_BUFFER_DURATION_IS_VALID (buffer) && parse->priv->acc_duration != 0) { |
| 1976 |
guint64 avg_bitrate; |
| 1977 |
|
| 1978 |
/* Calculate duration of a frame from buffer properties */ |
| 1979 |
frame_dur = GST_BUFFER_DURATION (buffer); |
| 1980 |
avg_bitrate = gst_util_uint64_scale (GST_SECOND, |
| 1981 |
8 * parse->priv->data_bytecount, parse->priv->acc_duration); |
| 1982 |
|
| 1983 |
if (avg_bitrate > G_MAXUINT) |
| 1984 |
return; |
| 1985 |
|
| 1986 |
parse->priv->avg_bitrate = (guint) avg_bitrate; |
| 1987 |
} else { |
| 1988 |
/* No way to figure out frame duration (is this even possible?) */ |
| 1989 |
return; |
| 1990 |
} |
| 1991 |
|
| 1992 |
/* override if subclass provided bitrate, e.g. metadata based */ |
| 1993 |
if (parse->priv->bitrate) { |
| 1994 |
parse->priv->avg_bitrate = parse->priv->bitrate; |
| 1995 |
/* spread this (confirmed) info ASAP */ |
| 1996 |
if (parse->priv->post_avg_bitrate && |
| 1997 |
parse->priv->posted_avg_bitrate != parse->priv->avg_bitrate) |
| 1998 |
parse->priv->tags_changed = TRUE; |
| 1999 |
} |
| 2000 |
|
| 2001 |
if (!frame_dur) |
| 2002 |
return; |
| 2003 |
|
| 2004 |
frame_bitrate64 = gst_util_uint64_scale (GST_SECOND, 8 * data_len, frame_dur); |
| 2005 |
|
| 2006 |
if (frame_bitrate64 > G_MAXUINT) |
| 2007 |
return; |
| 2008 |
|
| 2009 |
frame_bitrate = (guint) frame_bitrate64; |
| 2010 |
|
| 2011 |
GST_LOG_OBJECT (parse, "frame bitrate %u, avg bitrate %u", frame_bitrate, |
| 2012 |
parse->priv->avg_bitrate); |
| 2013 |
|
| 2014 |
if (parse->priv->framecount < MIN_FRAMES_TO_POST_BITRATE) |
| 2015 |
return; |
| 2016 |
|
| 2017 |
if (parse->priv->framecount == MIN_FRAMES_TO_POST_BITRATE && |
| 2018 |
(parse->priv->post_min_bitrate || parse->priv->post_avg_bitrate |
| 2019 |
|| parse->priv->post_max_bitrate)) |
| 2020 |
parse->priv->tags_changed = TRUE; |
| 2021 |
|
| 2022 |
if (G_LIKELY (parse->priv->framecount >= MIN_FRAMES_TO_POST_BITRATE)) { |
| 2023 |
if (frame_bitrate < parse->priv->min_bitrate) { |
| 2024 |
parse->priv->min_bitrate = frame_bitrate; |
| 2025 |
if (parse->priv->post_min_bitrate) |
| 2026 |
parse->priv->tags_changed = TRUE; |
| 2027 |
} |
| 2028 |
|
| 2029 |
if (frame_bitrate > parse->priv->max_bitrate) { |
| 2030 |
parse->priv->max_bitrate = frame_bitrate; |
| 2031 |
if (parse->priv->post_max_bitrate) |
| 2032 |
parse->priv->tags_changed = TRUE; |
| 2033 |
} |
| 2034 |
|
| 2035 |
/* Only update the tag on a 2% change */ |
| 2036 |
if (parse->priv->post_avg_bitrate && parse->priv->avg_bitrate) { |
| 2037 |
guint64 diffprev = gst_util_uint64_scale (100, |
| 2038 |
ABSDIFF (parse->priv->avg_bitrate, parse->priv->posted_avg_bitrate), |
| 2039 |
parse->priv->avg_bitrate); |
| 2040 |
if (diffprev >= UPDATE_THRESHOLD) |
| 2041 |
parse->priv->tags_changed = TRUE; |
| 2042 |
} |
| 2043 |
} |
| 2044 |
} |
| 2045 |
|
| 2046 |
/** |
| 2047 |
* gst_base_parse_add_index_entry: |
| 2048 |
* @parse: #GstBaseParse. |
| 2049 |
* @offset: offset of entry |
| 2050 |
* @ts: timestamp associated with offset |
| 2051 |
* @key: whether entry refers to keyframe |
| 2052 |
* @force: add entry disregarding sanity checks |
| 2053 |
* |
| 2054 |
* Adds an entry to the index associating @offset to @ts. It is recommended |
| 2055 |
* to only add keyframe entries. @force allows to bypass checks, such as |
| 2056 |
* whether the stream is (upstream) seekable, another entry is already "close" |
| 2057 |
* to the new entry, etc. |
| 2058 |
* |
| 2059 |
* Returns: #gboolean indicating whether entry was added |
| 2060 |
*/ |
| 2061 |
gboolean |
| 2062 |
gst_base_parse_add_index_entry (GstBaseParse * parse, guint64 offset, |
| 2063 |
GstClockTime ts, gboolean key, gboolean force) |
| 2064 |
{ |
| 2065 |
gboolean ret = FALSE; |
| 2066 |
GstIndexAssociation associations[2]; |
| 2067 |
|
| 2068 |
g_return_val_if_fail (GST_CLOCK_TIME_IS_VALID (ts), FALSE); |
| 2069 |
|
| 2070 |
GST_LOG_OBJECT (parse, "Adding key=%d index entry %" GST_TIME_FORMAT |
| 2071 |
" @ offset 0x%08" G_GINT64_MODIFIER "x", key, GST_TIME_ARGS (ts), offset); |
| 2072 |
|
| 2073 |
if (G_LIKELY (!force)) { |
| 2074 |
|
| 2075 |
if (!parse->priv->upstream_seekable) { |
| 2076 |
GST_DEBUG_OBJECT (parse, "upstream not seekable; discarding"); |
| 2077 |
goto exit; |
| 2078 |
} |
| 2079 |
|
| 2080 |
/* FIXME need better helper data structure that handles these issues |
| 2081 |
* related to ongoing collecting of index entries */ |
| 2082 |
if (parse->priv->index_last_offset + parse->priv->idx_byte_interval >= |
| 2083 |
(gint64) offset) { |
| 2084 |
GST_LOG_OBJECT (parse, |
| 2085 |
"already have entries up to offset 0x%08" G_GINT64_MODIFIER "x", |
| 2086 |
parse->priv->index_last_offset + parse->priv->idx_byte_interval); |
| 2087 |
goto exit; |
| 2088 |
} |
| 2089 |
|
| 2090 |
if (GST_CLOCK_TIME_IS_VALID (parse->priv->index_last_ts) && |
| 2091 |
GST_CLOCK_DIFF (parse->priv->index_last_ts, ts) < |
| 2092 |
parse->priv->idx_interval) { |
| 2093 |
GST_LOG_OBJECT (parse, "entry too close to last time %" GST_TIME_FORMAT, |
| 2094 |
GST_TIME_ARGS (parse->priv->index_last_ts)); |
| 2095 |
goto exit; |
| 2096 |
} |
| 2097 |
|
| 2098 |
/* if last is not really the last one */ |
| 2099 |
if (!parse->priv->index_last_valid) { |
| 2100 |
GstClockTime prev_ts; |
| 2101 |
|
| 2102 |
gst_base_parse_find_offset (parse, ts, TRUE, &prev_ts); |
| 2103 |
if (GST_CLOCK_DIFF (prev_ts, ts) < parse->priv->idx_interval) { |
| 2104 |
GST_LOG_OBJECT (parse, |
| 2105 |
"entry too close to existing entry %" GST_TIME_FORMAT, |
| 2106 |
GST_TIME_ARGS (prev_ts)); |
| 2107 |
parse->priv->index_last_offset = offset; |
| 2108 |
parse->priv->index_last_ts = ts; |
| 2109 |
goto exit; |
| 2110 |
} |
| 2111 |
} |
| 2112 |
} |
| 2113 |
|
| 2114 |
associations[0].format = GST_FORMAT_TIME; |
| 2115 |
associations[0].value = ts; |
| 2116 |
associations[1].format = GST_FORMAT_BYTES; |
| 2117 |
associations[1].value = offset; |
| 2118 |
|
| 2119 |
/* index might change on-the-fly, although that would be nutty app ... */ |
| 2120 |
GST_BASE_PARSE_INDEX_LOCK (parse); |
| 2121 |
gst_index_add_associationv (parse->priv->index, parse->priv->index_id, |
| 2122 |
(key) ? GST_INDEX_ASSOCIATION_FLAG_KEY_UNIT : |
| 2123 |
GST_INDEX_ASSOCIATION_FLAG_DELTA_UNIT, 2, |
| 2124 |
(const GstIndexAssociation *) &associations); |
| 2125 |
GST_BASE_PARSE_INDEX_UNLOCK (parse); |
| 2126 |
|
| 2127 |
if (key) { |
| 2128 |
parse->priv->index_last_offset = offset; |
| 2129 |
parse->priv->index_last_ts = ts; |
| 2130 |
} |
| 2131 |
|
| 2132 |
ret = TRUE; |
| 2133 |
|
| 2134 |
exit: |
| 2135 |
return ret; |
| 2136 |
} |
| 2137 |
|
| 2138 |
/* check for seekable upstream, above and beyond a mere query */ |
| 2139 |
static void |
| 2140 |
gst_base_parse_check_seekability (GstBaseParse * parse) |
| 2141 |
{ |
| 2142 |
GstQuery *query; |
| 2143 |
gboolean seekable = FALSE; |
| 2144 |
gint64 start = -1, stop = -1; |
| 2145 |
guint idx_interval = 0; |
| 2146 |
guint64 idx_byte_interval = 0; |
| 2147 |
|
| 2148 |
query = gst_query_new_seeking (GST_FORMAT_BYTES); |
| 2149 |
if (!gst_pad_peer_query (parse->sinkpad, query)) { |
| 2150 |
GST_DEBUG_OBJECT (parse, "seeking query failed"); |
| 2151 |
goto done; |
| 2152 |
} |
| 2153 |
|
| 2154 |
gst_query_parse_seeking (query, NULL, &seekable, &start, &stop); |
| 2155 |
|
| 2156 |
/* try harder to query upstream size if we didn't get it the first time */ |
| 2157 |
if (seekable && stop == -1) { |
| 2158 |
GST_DEBUG_OBJECT (parse, "doing duration query to fix up unset stop"); |
| 2159 |
gst_pad_peer_query_duration (parse->sinkpad, GST_FORMAT_BYTES, &stop); |
| 2160 |
} |
| 2161 |
|
| 2162 |
/* if upstream doesn't know the size, it's likely that it's not seekable in |
| 2163 |
* practice even if it technically may be seekable */ |
| 2164 |
if (seekable && (start != 0 || stop <= start)) { |
| 2165 |
GST_DEBUG_OBJECT (parse, "seekable but unknown start/stop -> disable"); |
| 2166 |
seekable = FALSE; |
| 2167 |
} |
| 2168 |
|
| 2169 |
/* let's not put every single frame into our index */ |
| 2170 |
if (seekable) { |
| 2171 |
if (stop < 10 * 1024 * 1024) |
| 2172 |
idx_interval = 100; |
| 2173 |
else if (stop < 100 * 1024 * 1024) |
| 2174 |
idx_interval = 500; |
| 2175 |
else |
| 2176 |
idx_interval = 1000; |
| 2177 |
|
| 2178 |
/* ensure that even for large files (e.g. very long audio files), the index |
| 2179 |
* stays reasonably-size, with some arbitrary limit to the total number of |
| 2180 |
* index entries */ |
| 2181 |
idx_byte_interval = (stop - start) / MAX_INDEX_ENTRIES; |
| 2182 |
GST_DEBUG_OBJECT (parse, |
| 2183 |
"Limiting index entries to %d, indexing byte interval %" |
| 2184 |
G_GUINT64_FORMAT " bytes", MAX_INDEX_ENTRIES, idx_byte_interval); |
| 2185 |
} |
| 2186 |
|
| 2187 |
done: |
| 2188 |
gst_query_unref (query); |
| 2189 |
|
| 2190 |
GST_DEBUG_OBJECT (parse, "seekable: %d (%" G_GUINT64_FORMAT " - %" |
| 2191 |
G_GUINT64_FORMAT ")", seekable, start, stop); |
| 2192 |
parse->priv->upstream_seekable = seekable; |
| 2193 |
parse->priv->upstream_size = seekable ? stop : 0; |
| 2194 |
|
| 2195 |
GST_DEBUG_OBJECT (parse, "idx_interval: %ums", idx_interval); |
| 2196 |
parse->priv->idx_interval = idx_interval * GST_MSECOND; |
| 2197 |
parse->priv->idx_byte_interval = idx_byte_interval; |
| 2198 |
} |
| 2199 |
|
| 2200 |
/* some misc checks on upstream */ |
| 2201 |
static void |
| 2202 |
gst_base_parse_check_upstream (GstBaseParse * parse) |
| 2203 |
{ |
| 2204 |
gint64 stop; |
| 2205 |
|
| 2206 |
if (gst_pad_peer_query_duration (parse->sinkpad, GST_FORMAT_TIME, &stop)) |
| 2207 |
if (GST_CLOCK_TIME_IS_VALID (stop) && stop) { |
| 2208 |
/* upstream has one, accept it also, and no further updates */ |
| 2209 |
gst_base_parse_set_duration (parse, GST_FORMAT_TIME, stop, 0); |
| 2210 |
parse->priv->upstream_has_duration = TRUE; |
| 2211 |
} |
| 2212 |
|
| 2213 |
GST_DEBUG_OBJECT (parse, "upstream_has_duration: %d", |
| 2214 |
parse->priv->upstream_has_duration); |
| 2215 |
} |
| 2216 |
|
| 2217 |
/* checks src caps to determine if dealing with audio or video */ |
| 2218 |
/* TODO maybe forego automagic stuff and let subclass configure it ? */ |
| 2219 |
static void |
| 2220 |
gst_base_parse_check_media (GstBaseParse * parse) |
| 2221 |
{ |
| 2222 |
GstCaps *caps; |
| 2223 |
GstStructure *s; |
| 2224 |
|
| 2225 |
caps = gst_pad_get_current_caps (parse->srcpad); |
| 2226 |
if (G_LIKELY (caps) && (s = gst_caps_get_structure (caps, 0))) { |
| 2227 |
parse->priv->is_video = |
| 2228 |
g_str_has_prefix (gst_structure_get_name (s), "video"); |
| 2229 |
} else { |
| 2230 |
/* historical default */ |
| 2231 |
parse->priv->is_video = FALSE; |
| 2232 |
} |
| 2233 |
if (caps) |
| 2234 |
gst_caps_unref (caps); |
| 2235 |
|
| 2236 |
parse->priv->checked_media = TRUE; |
| 2237 |
GST_DEBUG_OBJECT (parse, "media is video: %d", parse->priv->is_video); |
| 2238 |
} |
| 2239 |
|
| 2240 |
/* takes ownership of frame */ |
| 2241 |
static void |
| 2242 |
gst_base_parse_queue_frame (GstBaseParse * parse, GstBaseParseFrame * frame) |
| 2243 |
{ |
| 2244 |
if (!(frame->_private_flags & GST_BASE_PARSE_FRAME_PRIVATE_FLAG_NOALLOC)) { |
| 2245 |
/* frame allocated on the heap, we can just take ownership */ |
| 2246 |
g_queue_push_tail (&parse->priv->queued_frames, frame); |
| 2247 |
GST_TRACE ("queued frame %p", frame); |
| 2248 |
} else { |
| 2249 |
GstBaseParseFrame *copy; |
| 2250 |
|
| 2251 |
/* probably allocated on the stack, must make a proper copy */ |
| 2252 |
copy = gst_base_parse_frame_copy (frame); |
| 2253 |
g_queue_push_tail (&parse->priv->queued_frames, copy); |
| 2254 |
GST_TRACE ("queued frame %p (copy of %p)", copy, frame); |
| 2255 |
gst_base_parse_frame_free (frame); |
| 2256 |
} |
| 2257 |
} |
| 2258 |
|
| 2259 |
/* makes sure that @buf is properly prepared and decorated for passing |
| 2260 |
* to baseclass, and an equally setup frame is returned setup with @buf. |
| 2261 |
* Takes ownership of @buf. */ |
| 2262 |
static GstBaseParseFrame * |
| 2263 |
gst_base_parse_prepare_frame (GstBaseParse * parse, GstBuffer * buffer) |
| 2264 |
{ |
| 2265 |
GstBaseParseFrame *frame = NULL; |
| 2266 |
|
| 2267 |
buffer = gst_buffer_make_writable (buffer); |
| 2268 |
|
| 2269 |
GST_LOG_OBJECT (parse, |
| 2270 |
"preparing frame at offset %" G_GUINT64_FORMAT |
| 2271 |
" (%#" G_GINT64_MODIFIER "x) of size %" G_GSIZE_FORMAT, |
| 2272 |
GST_BUFFER_OFFSET (buffer), GST_BUFFER_OFFSET (buffer), |
| 2273 |
gst_buffer_get_size (buffer)); |
| 2274 |
|
| 2275 |
GST_BUFFER_OFFSET (buffer) = parse->priv->offset; |
| 2276 |
|
| 2277 |
gst_base_parse_update_flags (parse); |
| 2278 |
|
| 2279 |
frame = gst_base_parse_frame_new (buffer, 0, 0); |
| 2280 |
gst_buffer_unref (buffer); |
| 2281 |
gst_base_parse_update_frame (parse, frame); |
| 2282 |
|
| 2283 |
/* clear flags for next frame */ |
| 2284 |
parse->priv->discont = FALSE; |
| 2285 |
parse->priv->new_frame = FALSE; |
| 2286 |
|
| 2287 |
/* use default handler to provide initial (upstream) metadata */ |
| 2288 |
gst_base_parse_parse_frame (parse, frame); |
| 2289 |
|
| 2290 |
return frame; |
| 2291 |
} |
| 2292 |
|
| 2293 |
/* Wraps buffer in a frame and dispatches to subclass. |
| 2294 |
* Also manages data skipping and offset handling (including adapter flushing). |
| 2295 |
* Takes ownership of @buffer */ |
| 2296 |
static GstFlowReturn |
| 2297 |
gst_base_parse_handle_buffer (GstBaseParse * parse, GstBuffer * buffer, |
| 2298 |
gint * skip, gint * flushed) |
| 2299 |
{ |
| 2300 |
GstBaseParseClass *klass = GST_BASE_PARSE_GET_CLASS (parse); |
| 2301 |
GstBaseParseFrame *frame; |
| 2302 |
GstFlowReturn ret; |
| 2303 |
|
| 2304 |
g_return_val_if_fail (skip != NULL || flushed != NULL, GST_FLOW_ERROR); |
| 2305 |
|
| 2306 |
GST_LOG_OBJECT (parse, |
| 2307 |
"handling buffer of size %" G_GSIZE_FORMAT " with dts %" GST_TIME_FORMAT |
| 2308 |
", pts %" GST_TIME_FORMAT ", duration %" GST_TIME_FORMAT, |
| 2309 |
gst_buffer_get_size (buffer), GST_TIME_ARGS (GST_BUFFER_DTS (buffer)), |
| 2310 |
GST_TIME_ARGS (GST_BUFFER_PTS (buffer)), |
| 2311 |
GST_TIME_ARGS (GST_BUFFER_DURATION (buffer))); |
| 2312 |
|
| 2313 |
/* track what is being flushed during this single round of frame processing */ |
| 2314 |
parse->priv->flushed = 0; |
| 2315 |
*skip = 0; |
| 2316 |
|
| 2317 |
/* make it easy for _finish_frame to pick up input data */ |
| 2318 |
if (parse->priv->pad_mode == GST_PAD_MODE_PULL) { |
| 2319 |
gst_buffer_ref (buffer); |
| 2320 |
gst_adapter_push (parse->priv->adapter, buffer); |
| 2321 |
} |
| 2322 |
|
| 2323 |
frame = gst_base_parse_prepare_frame (parse, buffer); |
| 2324 |
ret = klass->handle_frame (parse, frame, skip); |
| 2325 |
|
| 2326 |
*flushed = parse->priv->flushed; |
| 2327 |
|
| 2328 |
GST_LOG_OBJECT (parse, "handle_frame skipped %d, flushed %d", |
| 2329 |
*skip, *flushed); |
| 2330 |
|
| 2331 |
/* subclass can only do one of these, or semantics are too unclear */ |
| 2332 |
g_assert (*skip == 0 || *flushed == 0); |
| 2333 |
|
| 2334 |
/* track skipping */ |
| 2335 |
if (*skip > 0) { |
| 2336 |
GstClockTime pts, dts; |
| 2337 |
GstBuffer *outbuf; |
| 2338 |
|
| 2339 |
GST_LOG_OBJECT (parse, "finding sync, skipping %d bytes", *skip); |
| 2340 |
if (parse->segment.rate < 0.0 && !parse->priv->buffers_queued) { |
| 2341 |
/* reverse playback, and no frames found yet, so we are skipping |
| 2342 |
* the leading part of a fragment, which may form the tail of |
| 2343 |
* fragment coming later, hopefully subclass skips efficiently ... */ |
| 2344 |
pts = gst_adapter_prev_pts (parse->priv->adapter, NULL); |
| 2345 |
dts = gst_adapter_prev_dts (parse->priv->adapter, NULL); |
| 2346 |
outbuf = gst_adapter_take_buffer (parse->priv->adapter, *skip); |
| 2347 |
outbuf = gst_buffer_make_writable (outbuf); |
| 2348 |
GST_BUFFER_PTS (outbuf) = pts; |
| 2349 |
GST_BUFFER_DTS (outbuf) = dts; |
| 2350 |
GST_BUFFER_OFFSET (outbuf) = GST_BUFFER_OFFSET_NONE; |
| 2351 |
GST_BUFFER_DURATION (outbuf) = GST_CLOCK_TIME_NONE; |
| 2352 |
GST_BUFFER_OFFSET_END (outbuf) = GST_BUFFER_OFFSET_NONE; |
| 2353 |
GST_BUFFER_FLAGS (outbuf) = 0; |
| 2354 |
parse->priv->buffers_head = |
| 2355 |
g_slist_prepend (parse->priv->buffers_head, outbuf); |
| 2356 |
outbuf = NULL; |
| 2357 |
} else { |
| 2358 |
/* If we're asked to skip more than is available in the adapter, |
| 2359 |
we need to remember what we need to skip for next iteration */ |
| 2360 |
gsize av = gst_adapter_available (parse->priv->adapter); |
| 2361 |
GST_DEBUG ("Asked to skip %u (%" G_GSIZE_FORMAT " available)", *skip, av); |
| 2362 |
if (av >= *skip) { |
| 2363 |
gst_adapter_flush (parse->priv->adapter, *skip); |
| 2364 |
} else { |
| 2365 |
GST_DEBUG |
| 2366 |
("This is more than available, flushing %" G_GSIZE_FORMAT |
| 2367 |
", storing %u to skip", av, (guint) (*skip - av)); |
| 2368 |
parse->priv->skip = *skip - av; |
| 2369 |
gst_adapter_flush (parse->priv->adapter, av); |
| 2370 |
*skip = av; |
| 2371 |
} |
| 2372 |
} |
| 2373 |
if (!parse->priv->discont) |
| 2374 |
parse->priv->sync_offset = parse->priv->offset; |
| 2375 |
parse->priv->offset += *skip; |
| 2376 |
parse->priv->discont = TRUE; |
| 2377 |
/* check for indefinite skipping */ |
| 2378 |
if (ret == GST_FLOW_OK) |
| 2379 |
ret = gst_base_parse_check_sync (parse); |
| 2380 |
} |
| 2381 |
|
| 2382 |
parse->priv->offset += *flushed; |
| 2383 |
|
| 2384 |
if (parse->priv->pad_mode == GST_PAD_MODE_PULL) { |
| 2385 |
gst_adapter_clear (parse->priv->adapter); |
| 2386 |
} |
| 2387 |
|
| 2388 |
if (*skip == 0 && *flushed == 0) { |
| 2389 |
/* Carry over discont if we need more data */ |
| 2390 |
if (GST_BUFFER_IS_DISCONT (frame->buffer)) |
| 2391 |
parse->priv->discont = TRUE; |
| 2392 |
} |
| 2393 |
|
| 2394 |
gst_base_parse_frame_free (frame); |
| 2395 |
|
| 2396 |
return ret; |
| 2397 |
} |
| 2398 |
|
| 2399 |
/* gst_base_parse_push_pending_events: |
| 2400 |
* @parse: #GstBaseParse |
| 2401 |
* |
| 2402 |
* Pushes the pending events |
| 2403 |
*/ |
| 2404 |
static void |
| 2405 |
gst_base_parse_push_pending_events (GstBaseParse * parse) |
| 2406 |
{ |
| 2407 |
if (G_UNLIKELY (parse->priv->pending_events)) { |
| 2408 |
GList *r = g_list_reverse (parse->priv->pending_events); |
| 2409 |
GList *l; |
| 2410 |
|
| 2411 |
parse->priv->pending_events = NULL; |
| 2412 |
for (l = r; l != NULL; l = l->next) { |
| 2413 |
gst_pad_push_event (parse->srcpad, GST_EVENT_CAST (l->data)); |
| 2414 |
} |
| 2415 |
g_list_free (r); |
| 2416 |
} |
| 2417 |
} |
| 2418 |
|
| 2419 |
/* gst_base_parse_handle_and_push_frame: |
| 2420 |
* @parse: #GstBaseParse. |
| 2421 |
* @klass: #GstBaseParseClass. |
| 2422 |
* @frame: (transfer full): a #GstBaseParseFrame |
| 2423 |
* |
| 2424 |
* Parses the frame from given buffer and pushes it forward. Also performs |
| 2425 |
* timestamp handling and checks the segment limits. |
| 2426 |
* |
| 2427 |
* This is called with srcpad STREAM_LOCK held. |
| 2428 |
* |
| 2429 |
* Returns: #GstFlowReturn |
| 2430 |
*/ |
| 2431 |
static GstFlowReturn |
| 2432 |
gst_base_parse_handle_and_push_frame (GstBaseParse * parse, |
| 2433 |
GstBaseParseFrame * frame) |
| 2434 |
{ |
| 2435 |
gint64 offset; |
| 2436 |
GstBuffer *buffer; |
| 2437 |
|
| 2438 |
g_return_val_if_fail (frame != NULL, GST_FLOW_ERROR); |
| 2439 |
|
| 2440 |
buffer = frame->buffer; |
| 2441 |
offset = frame->offset; |
| 2442 |
|
| 2443 |
/* check if subclass/format can provide ts. |
| 2444 |
* If so, that allows and enables extra seek and duration determining options */ |
| 2445 |
if (G_UNLIKELY (parse->priv->first_frame_offset < 0)) { |
| 2446 |
if (GST_BUFFER_PTS_IS_VALID (buffer) && parse->priv->has_timing_info |
| 2447 |
&& parse->priv->pad_mode == GST_PAD_MODE_PULL) { |
| 2448 |
parse->priv->first_frame_offset = offset; |
| 2449 |
parse->priv->first_frame_pts = GST_BUFFER_PTS (buffer); |
| 2450 |
parse->priv->first_frame_dts = GST_BUFFER_DTS (buffer); |
| 2451 |
GST_DEBUG_OBJECT (parse, "subclass provided dts %" GST_TIME_FORMAT |
| 2452 |
", pts %" GST_TIME_FORMAT " for first frame at offset %" |
| 2453 |
G_GINT64_FORMAT, GST_TIME_ARGS (parse->priv->first_frame_dts), |
| 2454 |
GST_TIME_ARGS (parse->priv->first_frame_pts), |
| 2455 |
parse->priv->first_frame_offset); |
| 2456 |
if (!GST_CLOCK_TIME_IS_VALID (parse->priv->duration)) { |
| 2457 |
gint64 off; |
| 2458 |
GstClockTime last_ts = G_MAXINT64; |
| 2459 |
|
| 2460 |
GST_DEBUG_OBJECT (parse, "no duration; trying scan to determine"); |
| 2461 |
gst_base_parse_locate_time (parse, &last_ts, &off); |
| 2462 |
if (GST_CLOCK_TIME_IS_VALID (last_ts)) |
| 2463 |
gst_base_parse_set_duration (parse, GST_FORMAT_TIME, last_ts, 0); |
| 2464 |
} |
| 2465 |
} else { |
| 2466 |
/* disable further checks */ |
| 2467 |
parse->priv->first_frame_offset = 0; |
| 2468 |
} |
| 2469 |
} |
| 2470 |
|
| 2471 |
/* track upstream time if provided, not subclass' internal notion of it */ |
| 2472 |
if (parse->priv->upstream_format == GST_FORMAT_TIME) { |
| 2473 |
GST_BUFFER_PTS (frame->buffer) = GST_CLOCK_TIME_NONE; |
| 2474 |
GST_BUFFER_DTS (frame->buffer) = GST_CLOCK_TIME_NONE; |
| 2475 |
} |
| 2476 |
|
| 2477 |
/* interpolating and no valid pts yet, |
| 2478 |
* start with dts and carry on from there */ |
| 2479 |
if (parse->priv->infer_ts && parse->priv->pts_interpolate |
| 2480 |
&& !GST_CLOCK_TIME_IS_VALID (parse->priv->next_pts)) |
| 2481 |
parse->priv->next_pts = parse->priv->next_dts; |
| 2482 |
|
| 2483 |
/* again use default handler to add missing metadata; |
| 2484 |
* we may have new information on frame properties */ |
| 2485 |
gst_base_parse_parse_frame (parse, frame); |
| 2486 |
|
| 2487 |
parse->priv->next_pts = GST_CLOCK_TIME_NONE; |
| 2488 |
if (GST_BUFFER_DTS_IS_VALID (buffer) && GST_BUFFER_DURATION_IS_VALID (buffer)) { |
| 2489 |
parse->priv->next_dts = |
| 2490 |
GST_BUFFER_DTS (buffer) + GST_BUFFER_DURATION (buffer); |
| 2491 |
if (parse->priv->pts_interpolate && GST_BUFFER_PTS_IS_VALID (buffer)) { |
| 2492 |
GstClockTime next_pts = |
| 2493 |
GST_BUFFER_PTS (buffer) + GST_BUFFER_DURATION (buffer); |
| 2494 |
if (next_pts >= parse->priv->next_dts) |
| 2495 |
parse->priv->next_pts = next_pts; |
| 2496 |
} |
| 2497 |
} else { |
| 2498 |
/* we lost track, do not produce bogus time next time around |
| 2499 |
* (probably means parser subclass has given up on parsing as well) */ |
| 2500 |
GST_DEBUG_OBJECT (parse, "no next fallback timestamp"); |
| 2501 |
parse->priv->next_dts = GST_CLOCK_TIME_NONE; |
| 2502 |
} |
| 2503 |
|
| 2504 |
if (parse->priv->upstream_seekable && parse->priv->exact_position && |
| 2505 |
GST_BUFFER_PTS_IS_VALID (buffer)) |
| 2506 |
gst_base_parse_add_index_entry (parse, offset, |
| 2507 |
GST_BUFFER_PTS (buffer), |
| 2508 |
!GST_BUFFER_FLAG_IS_SET (buffer, GST_BUFFER_FLAG_DELTA_UNIT), FALSE); |
| 2509 |
|
| 2510 |
/* All OK, push queued frames if there are any */ |
| 2511 |
if (G_UNLIKELY (!g_queue_is_empty (&parse->priv->queued_frames))) { |
| 2512 |
GstBaseParseFrame *queued_frame; |
| 2513 |
|
| 2514 |
while ((queued_frame = g_queue_pop_head (&parse->priv->queued_frames))) { |
| 2515 |
gst_base_parse_push_frame (parse, queued_frame); |
| 2516 |
gst_base_parse_frame_free (queued_frame); |
| 2517 |
} |
| 2518 |
} |
| 2519 |
|
| 2520 |
return gst_base_parse_push_frame (parse, frame); |
| 2521 |
} |
| 2522 |
|
| 2523 |
/** |
| 2524 |
* gst_base_parse_push_frame: |
| 2525 |
* @parse: #GstBaseParse. |
| 2526 |
* @frame: (transfer none): a #GstBaseParseFrame |
| 2527 |
* |
| 2528 |
* Pushes the frame's buffer downstream, sends any pending events and |
| 2529 |
* does some timestamp and segment handling. Takes ownership of |
| 2530 |
* frame's buffer, though caller retains ownership of @frame. |
| 2531 |
* |
| 2532 |
* This must be called with sinkpad STREAM_LOCK held. |
| 2533 |
* |
| 2534 |
* Returns: #GstFlowReturn |
| 2535 |
*/ |
| 2536 |
GstFlowReturn |
| 2537 |
gst_base_parse_push_frame (GstBaseParse * parse, GstBaseParseFrame * frame) |
| 2538 |
{ |
| 2539 |
GstFlowReturn ret = GST_FLOW_OK; |
| 2540 |
GstClockTime last_start = GST_CLOCK_TIME_NONE; |
| 2541 |
GstClockTime last_stop = GST_CLOCK_TIME_NONE; |
| 2542 |
GstBaseParseClass *klass = GST_BASE_PARSE_GET_CLASS (parse); |
| 2543 |
GstBuffer *buffer; |
| 2544 |
gsize size; |
| 2545 |
|
| 2546 |
g_return_val_if_fail (frame != NULL, GST_FLOW_ERROR); |
| 2547 |
g_return_val_if_fail (frame->buffer != NULL, GST_FLOW_ERROR); |
| 2548 |
|
| 2549 |
GST_TRACE_OBJECT (parse, "pushing frame %p", frame); |
| 2550 |
|
| 2551 |
buffer = frame->buffer; |
| 2552 |
|
| 2553 |
GST_LOG_OBJECT (parse, |
| 2554 |
"processing buffer of size %" G_GSIZE_FORMAT " with dts %" GST_TIME_FORMAT |
| 2555 |
", pts %" GST_TIME_FORMAT ", duration %" GST_TIME_FORMAT, |
| 2556 |
gst_buffer_get_size (buffer), |
| 2557 |
GST_TIME_ARGS (GST_BUFFER_DTS (buffer)), |
| 2558 |
GST_TIME_ARGS (GST_BUFFER_PTS (buffer)), |
| 2559 |
GST_TIME_ARGS (GST_BUFFER_DURATION (buffer))); |
| 2560 |
|
| 2561 |
/* update stats */ |
| 2562 |
parse->priv->bytecount += frame->size; |
| 2563 |
if (G_LIKELY (!(frame->flags & GST_BASE_PARSE_FRAME_FLAG_NO_FRAME))) { |
| 2564 |
parse->priv->framecount++; |
| 2565 |
if (GST_BUFFER_DURATION_IS_VALID (buffer)) { |
| 2566 |
parse->priv->acc_duration += GST_BUFFER_DURATION (buffer); |
| 2567 |
} |
| 2568 |
} |
| 2569 |
/* 0 means disabled */ |
| 2570 |
if (parse->priv->update_interval < 0) |
| 2571 |
parse->priv->update_interval = 50; |
| 2572 |
else if (parse->priv->update_interval > 0 && |
| 2573 |
(parse->priv->framecount % parse->priv->update_interval) == 0) |
| 2574 |
gst_base_parse_update_duration (parse); |
| 2575 |
|
| 2576 |
if (GST_BUFFER_PTS_IS_VALID (buffer)) |
| 2577 |
last_start = last_stop = GST_BUFFER_PTS (buffer); |
| 2578 |
if (last_start != GST_CLOCK_TIME_NONE |
| 2579 |
&& GST_BUFFER_DURATION_IS_VALID (buffer)) |
| 2580 |
last_stop = last_start + GST_BUFFER_DURATION (buffer); |
| 2581 |
|
| 2582 |
/* should have caps by now */ |
| 2583 |
if (!gst_pad_has_current_caps (parse->srcpad)) |
| 2584 |
goto no_caps; |
| 2585 |
|
| 2586 |
if (G_UNLIKELY (!parse->priv->checked_media)) { |
| 2587 |
/* have caps; check identity */ |
| 2588 |
gst_base_parse_check_media (parse); |
| 2589 |
} |
| 2590 |
|
| 2591 |
if (parse->priv->tags_changed) { |
| 2592 |
gst_base_parse_queue_tag_event_update (parse); |
| 2593 |
parse->priv->tags_changed = FALSE; |
| 2594 |
} |
| 2595 |
|
| 2596 |
/* Push pending events, including SEGMENT events */ |
| 2597 |
gst_base_parse_push_pending_events (parse); |
| 2598 |
|
| 2599 |
/* update bitrates and optionally post corresponding tags |
| 2600 |
* (following newsegment) */ |
| 2601 |
gst_base_parse_update_bitrates (parse, frame); |
| 2602 |
|
| 2603 |
if (klass->pre_push_frame) { |
| 2604 |
ret = klass->pre_push_frame (parse, frame); |
| 2605 |
} else { |
| 2606 |
frame->flags |= GST_BASE_PARSE_FRAME_FLAG_CLIP; |
| 2607 |
} |
| 2608 |
|
| 2609 |
/* Push pending events, if there are any new ones |
| 2610 |
* like tags added by pre_push_frame */ |
| 2611 |
if (parse->priv->tags_changed) { |
| 2612 |
gst_base_parse_queue_tag_event_update (parse); |
| 2613 |
parse->priv->tags_changed = FALSE; |
| 2614 |
} |
| 2615 |
gst_base_parse_push_pending_events (parse); |
| 2616 |
|
| 2617 |
/* take final ownership of frame buffer */ |
| 2618 |
if (frame->out_buffer) { |
| 2619 |
buffer = frame->out_buffer; |
| 2620 |
frame->out_buffer = NULL; |
| 2621 |
gst_buffer_replace (&frame->buffer, NULL); |
| 2622 |
} else { |
| 2623 |
buffer = frame->buffer; |
| 2624 |
frame->buffer = NULL; |
| 2625 |
} |
| 2626 |
|
| 2627 |
/* subclass must play nice */ |
| 2628 |
g_return_val_if_fail (buffer != NULL, GST_FLOW_ERROR); |
| 2629 |
|
| 2630 |
size = gst_buffer_get_size (buffer); |
| 2631 |
|
| 2632 |
parse->priv->seen_keyframe |= parse->priv->is_video && |
| 2633 |
!GST_BUFFER_FLAG_IS_SET (buffer, GST_BUFFER_FLAG_DELTA_UNIT); |
| 2634 |
|
| 2635 |
if (frame->flags & GST_BASE_PARSE_FRAME_FLAG_CLIP) { |
| 2636 |
if (GST_BUFFER_TIMESTAMP_IS_VALID (buffer) && |
| 2637 |
GST_CLOCK_TIME_IS_VALID (parse->segment.stop) && |
| 2638 |
GST_BUFFER_TIMESTAMP (buffer) > |
| 2639 |
parse->segment.stop + parse->priv->lead_out_ts) { |
| 2640 |
GST_LOG_OBJECT (parse, "Dropped frame, after segment"); |
| 2641 |
ret = GST_FLOW_EOS; |
| 2642 |
} else if (!parse->priv->disable_clip && |
| 2643 |
GST_BUFFER_TIMESTAMP_IS_VALID (buffer) && |
| 2644 |
GST_BUFFER_DURATION_IS_VALID (buffer) && |
| 2645 |
GST_CLOCK_TIME_IS_VALID (parse->segment.start) && |
| 2646 |
GST_BUFFER_TIMESTAMP (buffer) + GST_BUFFER_DURATION (buffer) + |
| 2647 |
parse->priv->lead_in_ts < parse->segment.start) { |
| 2648 |
if (parse->priv->seen_keyframe) { |
| 2649 |
GST_LOG_OBJECT (parse, "Frame before segment, after keyframe"); |
| 2650 |
ret = GST_FLOW_OK; |
| 2651 |
} else { |
| 2652 |
GST_LOG_OBJECT (parse, "Dropped frame, before segment"); |
| 2653 |
ret = GST_BASE_PARSE_FLOW_DROPPED; |
| 2654 |
} |
| 2655 |
} else { |
| 2656 |
ret = GST_FLOW_OK; |
| 2657 |
} |
| 2658 |
} |
| 2659 |
|
| 2660 |
if (ret == GST_BASE_PARSE_FLOW_DROPPED) { |
| 2661 |
GST_LOG_OBJECT (parse, "frame (%" G_GSIZE_FORMAT " bytes) dropped", size); |
| 2662 |
if (GST_BUFFER_FLAG_IS_SET (buffer, GST_BUFFER_FLAG_DISCONT)) |
| 2663 |
parse->priv->discont = TRUE; |
| 2664 |
gst_buffer_unref (buffer); |
| 2665 |
ret = GST_FLOW_OK; |
| 2666 |
} else if (ret == GST_FLOW_OK) { |
| 2667 |
if (parse->segment.rate > 0.0) { |
| 2668 |
GST_LOG_OBJECT (parse, "pushing frame (%" G_GSIZE_FORMAT " bytes) now..", |
| 2669 |
size); |
| 2670 |
ret = gst_pad_push (parse->srcpad, buffer); |
| 2671 |
GST_LOG_OBJECT (parse, "frame pushed, flow %s", gst_flow_get_name (ret)); |
| 2672 |
} else if (!parse->priv->disable_passthrough && parse->priv->passthrough) { |
| 2673 |
|
| 2674 |
/* in backwards playback mode, if on passthrough we need to push buffers |
| 2675 |
* directly without accumulating them into the buffers_queued as baseparse |
| 2676 |
* will never check for a DISCONT while on passthrough and those buffers |
| 2677 |
* will never be pushed. |
| 2678 |
* |
| 2679 |
* also, as we are on reverse playback, it might be possible that |
| 2680 |
* passthrough might have just been enabled, so make sure to drain the |
| 2681 |
* buffers_queued list */ |
| 2682 |
if (G_UNLIKELY (parse->priv->buffers_queued != NULL)) { |
| 2683 |
gst_base_parse_finish_fragment (parse, TRUE); |
| 2684 |
ret = gst_base_parse_send_buffers (parse); |
| 2685 |
} |
| 2686 |
|
| 2687 |
if (ret == GST_FLOW_OK) { |
| 2688 |
GST_LOG_OBJECT (parse, |
| 2689 |
"pushing frame (%" G_GSIZE_FORMAT " bytes) now..", size); |
| 2690 |
ret = gst_pad_push (parse->srcpad, buffer); |
| 2691 |
GST_LOG_OBJECT (parse, "frame pushed, flow %s", |
| 2692 |
gst_flow_get_name (ret)); |
| 2693 |
} else { |
| 2694 |
GST_LOG_OBJECT (parse, |
| 2695 |
"frame (%" G_GSIZE_FORMAT " bytes) not pushed: %s", size, |
| 2696 |
gst_flow_get_name (ret)); |
| 2697 |
gst_buffer_unref (buffer); |
| 2698 |
} |
| 2699 |
|
| 2700 |
} else { |
| 2701 |
GST_LOG_OBJECT (parse, "frame (%" G_GSIZE_FORMAT " bytes) queued for now", |
| 2702 |
size); |
| 2703 |
parse->priv->buffers_queued = |
| 2704 |
g_slist_prepend (parse->priv->buffers_queued, buffer); |
| 2705 |
ret = GST_FLOW_OK; |
| 2706 |
} |
| 2707 |
} else { |
| 2708 |
GST_LOG_OBJECT (parse, "frame (%" G_GSIZE_FORMAT " bytes) not pushed: %s", |
| 2709 |
size, gst_flow_get_name (ret)); |
| 2710 |
gst_buffer_unref (buffer); |
| 2711 |
/* if we are not sufficiently in control, let upstream decide on EOS */ |
| 2712 |
if (ret == GST_FLOW_EOS && !parse->priv->disable_passthrough && |
| 2713 |
(parse->priv->passthrough || |
| 2714 |
(parse->priv->pad_mode == GST_PAD_MODE_PUSH && |
| 2715 |
!parse->priv->upstream_seekable))) |
| 2716 |
ret = GST_FLOW_OK; |
| 2717 |
} |
| 2718 |
|
| 2719 |
/* Update current running segment position */ |
| 2720 |
if ((ret == GST_FLOW_OK || ret == GST_FLOW_NOT_LINKED) |
| 2721 |
&& last_stop != GST_CLOCK_TIME_NONE |
| 2722 |
&& parse->segment.position < last_stop) |
| 2723 |
parse->segment.position = last_stop; |
| 2724 |
|
| 2725 |
return ret; |
| 2726 |
|
| 2727 |
/* ERRORS */ |
| 2728 |
no_caps: |
| 2729 |
{ |
| 2730 |
if (GST_PAD_IS_FLUSHING (parse->srcpad)) |
| 2731 |
return GST_FLOW_FLUSHING; |
| 2732 |
|
| 2733 |
GST_ELEMENT_ERROR (parse, STREAM, DECODE, ("No caps set"), (NULL)); |
| 2734 |
return GST_FLOW_ERROR; |
| 2735 |
} |
| 2736 |
} |
| 2737 |
|
| 2738 |
/** |
| 2739 |
* gst_base_parse_finish_frame: |
| 2740 |
* @parse: a #GstBaseParse |
| 2741 |
* @frame: a #GstBaseParseFrame |
| 2742 |
* @size: consumed input data represented by frame |
| 2743 |
* |
| 2744 |
* Collects parsed data and pushes it downstream. |
| 2745 |
* Source pad caps must be set when this is called. |
| 2746 |
* |
| 2747 |
* If @frame's out_buffer is set, that will be used as subsequent frame data, |
| 2748 |
* and @size amount will be flushed from the input data. The output_buffer size |
| 2749 |
* can differ from the consumed size indicated by @size. |
| 2750 |
* |
| 2751 |
* Otherwise, @size samples will be taken from the input and used for output, |
| 2752 |
* and the output's metadata (timestamps etc) will be taken as (optionally) |
| 2753 |
* set by the subclass on @frame's (input) buffer (which is otherwise |
| 2754 |
* ignored for any but the above purpose/information). |
| 2755 |
* |
| 2756 |
* Note that the latter buffer is invalidated by this call, whereas the |
| 2757 |
* caller retains ownership of @frame. |
| 2758 |
* |
| 2759 |
* Returns: a #GstFlowReturn that should be escalated to caller (of caller) |
| 2760 |
*/ |
| 2761 |
GstFlowReturn |
| 2762 |
gst_base_parse_finish_frame (GstBaseParse * parse, GstBaseParseFrame * frame, |
| 2763 |
gint size) |
| 2764 |
{ |
| 2765 |
GstFlowReturn ret = GST_FLOW_OK; |
| 2766 |
|
| 2767 |
g_return_val_if_fail (frame != NULL, GST_FLOW_ERROR); |
| 2768 |
g_return_val_if_fail (frame->buffer != NULL, GST_FLOW_ERROR); |
| 2769 |
g_return_val_if_fail (size > 0 || frame->out_buffer, GST_FLOW_ERROR); |
| 2770 |
g_return_val_if_fail (gst_adapter_available (parse->priv->adapter) >= size, |
| 2771 |
GST_FLOW_ERROR); |
| 2772 |
|
| 2773 |
GST_LOG_OBJECT (parse, "finished frame at offset %" G_GUINT64_FORMAT ", " |
| 2774 |
"flushing size %d", frame->offset, size); |
| 2775 |
|
| 2776 |
/* some one-time start-up */ |
| 2777 |
if (G_UNLIKELY (parse->priv->framecount == 0)) { |
| 2778 |
gst_base_parse_check_seekability (parse); |
| 2779 |
gst_base_parse_check_upstream (parse); |
| 2780 |
} |
| 2781 |
|
| 2782 |
parse->priv->flushed += size; |
| 2783 |
|
| 2784 |
if (parse->priv->scanning && frame->buffer) { |
| 2785 |
if (!parse->priv->scanned_frame) { |
| 2786 |
parse->priv->scanned_frame = gst_base_parse_frame_copy (frame); |
| 2787 |
} |
| 2788 |
goto exit; |
| 2789 |
} |
| 2790 |
|
| 2791 |
/* either PUSH or PULL mode arranges for adapter data */ |
| 2792 |
/* ensure output buffer */ |
| 2793 |
if (!frame->out_buffer) { |
| 2794 |
GstBuffer *src, *dest; |
| 2795 |
|
| 2796 |
frame->out_buffer = gst_adapter_take_buffer (parse->priv->adapter, size); |
| 2797 |
frame->out_buffer = gst_buffer_make_writable (frame->out_buffer); |
| 2798 |
dest = frame->out_buffer; |
| 2799 |
src = frame->buffer; |
| 2800 |
GST_BUFFER_PTS (dest) = GST_BUFFER_PTS (src); |
| 2801 |
GST_BUFFER_DTS (dest) = GST_BUFFER_DTS (src); |
| 2802 |
GST_BUFFER_OFFSET (dest) = GST_BUFFER_OFFSET (src); |
| 2803 |
GST_BUFFER_DURATION (dest) = GST_BUFFER_DURATION (src); |
| 2804 |
GST_BUFFER_OFFSET_END (dest) = GST_BUFFER_OFFSET_END (src); |
| 2805 |
GST_BUFFER_FLAGS (dest) = GST_BUFFER_FLAGS (src); |
| 2806 |
} else { |
| 2807 |
gst_adapter_flush (parse->priv->adapter, size); |
| 2808 |
} |
| 2809 |
|
| 2810 |
/* use as input for subsequent processing */ |
| 2811 |
gst_buffer_replace (&frame->buffer, frame->out_buffer); |
| 2812 |
gst_buffer_unref (frame->out_buffer); |
| 2813 |
frame->out_buffer = NULL; |
| 2814 |
|
| 2815 |
/* mark input size consumed */ |
| 2816 |
frame->size = size; |
| 2817 |
|
| 2818 |
/* subclass might queue frames/data internally if it needs more |
| 2819 |
* frames to decide on the format, or might request us to queue here. */ |
| 2820 |
if (frame->flags & GST_BASE_PARSE_FRAME_FLAG_DROP) { |
| 2821 |
gst_buffer_replace (&frame->buffer, NULL); |
| 2822 |
goto exit; |
| 2823 |
} else if (frame->flags & GST_BASE_PARSE_FRAME_FLAG_QUEUE) { |
| 2824 |
GstBaseParseFrame *copy; |
| 2825 |
|
| 2826 |
copy = gst_base_parse_frame_copy (frame); |
| 2827 |
copy->flags &= ~GST_BASE_PARSE_FRAME_FLAG_QUEUE; |
| 2828 |
gst_base_parse_queue_frame (parse, copy); |
| 2829 |
goto exit; |
| 2830 |
} |
| 2831 |
|
| 2832 |
ret = gst_base_parse_handle_and_push_frame (parse, frame); |
| 2833 |
|
| 2834 |
exit: |
| 2835 |
return ret; |
| 2836 |
} |
| 2837 |
|
| 2838 |
/** |
| 2839 |
* gst_base_parse_drain: |
| 2840 |
* @parse: a #GstBaseParse |
| 2841 |
* |
| 2842 |
* Drains the adapter until it is empty. It decreases the min_frame_size to |
| 2843 |
* match the current adapter size and calls chain method until the adapter |
| 2844 |
* is emptied or chain returns with error. |
| 2845 |
* |
| 2846 |
* Since: 1.12 |
| 2847 |
*/ |
| 2848 |
void |
| 2849 |
gst_base_parse_drain (GstBaseParse * parse) |
| 2850 |
{ |
| 2851 |
guint avail; |
| 2852 |
|
| 2853 |
GST_DEBUG_OBJECT (parse, "draining"); |
| 2854 |
parse->priv->drain = TRUE; |
| 2855 |
|
| 2856 |
for (;;) { |
| 2857 |
avail = gst_adapter_available (parse->priv->adapter); |
| 2858 |
if (!avail) |
| 2859 |
break; |
| 2860 |
|
| 2861 |
if (gst_base_parse_chain (parse->sinkpad, GST_OBJECT_CAST (parse), |
| 2862 |
NULL) != GST_FLOW_OK) { |
| 2863 |
break; |
| 2864 |
} |
| 2865 |
|
| 2866 |
/* nothing changed, maybe due to truncated frame; break infinite loop */ |
| 2867 |
if (avail == gst_adapter_available (parse->priv->adapter)) { |
| 2868 |
GST_DEBUG_OBJECT (parse, "no change during draining; flushing"); |
| 2869 |
gst_adapter_clear (parse->priv->adapter); |
| 2870 |
} |
| 2871 |
} |
| 2872 |
|
| 2873 |
parse->priv->drain = FALSE; |
| 2874 |
} |
| 2875 |
|
| 2876 |
/* gst_base_parse_send_buffers |
| 2877 |
* |
| 2878 |
* Sends buffers collected in send_buffers downstream, and ensures that list |
| 2879 |
* is empty at the end (errors or not). |
| 2880 |
*/ |
| 2881 |
static GstFlowReturn |
| 2882 |
gst_base_parse_send_buffers (GstBaseParse * parse) |
| 2883 |
{ |
| 2884 |
GSList *send = NULL; |
| 2885 |
GstBuffer *buf; |
| 2886 |
GstFlowReturn ret = GST_FLOW_OK; |
| 2887 |
gboolean first = TRUE; |
| 2888 |
|
| 2889 |
send = parse->priv->buffers_send; |
| 2890 |
|
| 2891 |
/* send buffers */ |
| 2892 |
while (send) { |
| 2893 |
buf = GST_BUFFER_CAST (send->data); |
| 2894 |
GST_LOG_OBJECT (parse, "pushing buffer %p, dts %" |
| 2895 |
GST_TIME_FORMAT ", pts %" GST_TIME_FORMAT ", duration %" GST_TIME_FORMAT |
| 2896 |
", offset %" G_GINT64_FORMAT, buf, |
| 2897 |
GST_TIME_ARGS (GST_BUFFER_DTS (buf)), |
| 2898 |
GST_TIME_ARGS (GST_BUFFER_PTS (buf)), |
| 2899 |
GST_TIME_ARGS (GST_BUFFER_DURATION (buf)), GST_BUFFER_OFFSET (buf)); |
| 2900 |
|
| 2901 |
/* Make sure the first buffer is always DISCONT. If we split |
| 2902 |
* GOPs inside the parser this is otherwise not guaranteed */ |
| 2903 |
if (first) { |
| 2904 |
GST_BUFFER_FLAG_SET (buf, GST_BUFFER_FLAG_DISCONT); |
| 2905 |
first = FALSE; |
| 2906 |
} else { |
| 2907 |
/* likewise, subsequent buffers should never have DISCONT |
| 2908 |
* according to the "reverse fragment protocol", or such would |
| 2909 |
* confuse a downstream decoder |
| 2910 |
* (could be DISCONT due to aggregating upstream fragments by parsing) */ |
| 2911 |
GST_BUFFER_FLAG_UNSET (buf, GST_BUFFER_FLAG_DISCONT); |
| 2912 |
} |
| 2913 |
|
| 2914 |
/* iterate output queue an push downstream */ |
| 2915 |
ret = gst_pad_push (parse->srcpad, buf); |
| 2916 |
send = g_slist_delete_link (send, send); |
| 2917 |
|
| 2918 |
/* clear any leftover if error */ |
| 2919 |
if (G_UNLIKELY (ret != GST_FLOW_OK)) { |
| 2920 |
while (send) { |
| 2921 |
buf = GST_BUFFER_CAST (send->data); |
| 2922 |
gst_buffer_unref (buf); |
| 2923 |
send = g_slist_delete_link (send, send); |
| 2924 |
} |
| 2925 |
} |
| 2926 |
} |
| 2927 |
|
| 2928 |
parse->priv->buffers_send = send; |
| 2929 |
|
| 2930 |
return ret; |
| 2931 |
} |
| 2932 |
|
| 2933 |
/* gst_base_parse_start_fragment: |
| 2934 |
* |
| 2935 |
* Prepares for processing a reverse playback (forward) fragment |
| 2936 |
* by (re)setting proper state variables. |
| 2937 |
*/ |
| 2938 |
static GstFlowReturn |
| 2939 |
gst_base_parse_start_fragment (GstBaseParse * parse) |
| 2940 |
{ |
| 2941 |
GST_LOG_OBJECT (parse, "starting fragment"); |
| 2942 |
|
| 2943 |
/* invalidate so no fall-back timestamping is performed; |
| 2944 |
* ok if taken from subclass or upstream */ |
| 2945 |
parse->priv->next_pts = GST_CLOCK_TIME_NONE; |
| 2946 |
parse->priv->prev_pts = GST_CLOCK_TIME_NONE; |
| 2947 |
parse->priv->next_dts = GST_CLOCK_TIME_NONE; |
| 2948 |
parse->priv->prev_dts = GST_CLOCK_TIME_NONE; |
| 2949 |
parse->priv->prev_dts_from_pts = FALSE; |
| 2950 |
/* prevent it hanging around stop all the time */ |
| 2951 |
parse->segment.position = GST_CLOCK_TIME_NONE; |
| 2952 |
/* mark next run */ |
| 2953 |
parse->priv->discont = TRUE; |
| 2954 |
|
| 2955 |
/* head of previous fragment is now pending tail of current fragment */ |
| 2956 |
parse->priv->buffers_pending = parse->priv->buffers_head; |
| 2957 |
parse->priv->buffers_head = NULL; |
| 2958 |
|
| 2959 |
return GST_FLOW_OK; |
| 2960 |
} |
| 2961 |
|
| 2962 |
|
| 2963 |
/* gst_base_parse_finish_fragment: |
| 2964 |
* |
| 2965 |
* Processes a reverse playback (forward) fragment: |
| 2966 |
* - append head of last fragment that was skipped to current fragment data |
| 2967 |
* - drain the resulting current fragment data (i.e. repeated chain) |
| 2968 |
* - add time/duration (if needed) to frames queued by chain |
| 2969 |
* - push queued data |
| 2970 |
*/ |
| 2971 |
static GstFlowReturn |
| 2972 |
gst_base_parse_finish_fragment (GstBaseParse * parse, gboolean prev_head) |
| 2973 |
{ |
| 2974 |
GstBuffer *buf; |
| 2975 |
GstFlowReturn ret = GST_FLOW_OK; |
| 2976 |
gboolean seen_key = FALSE, seen_delta = FALSE; |
| 2977 |
|
| 2978 |
GST_LOG_OBJECT (parse, "finishing fragment"); |
| 2979 |
|
| 2980 |
/* restore order */ |
| 2981 |
parse->priv->buffers_pending = g_slist_reverse (parse->priv->buffers_pending); |
| 2982 |
while (parse->priv->buffers_pending) { |
| 2983 |
buf = GST_BUFFER_CAST (parse->priv->buffers_pending->data); |
| 2984 |
if (prev_head) { |
| 2985 |
GST_LOG_OBJECT (parse, "adding pending buffer (size %" G_GSIZE_FORMAT ")", |
| 2986 |
gst_buffer_get_size (buf)); |
| 2987 |
gst_adapter_push (parse->priv->adapter, buf); |
| 2988 |
} else { |
| 2989 |
GST_LOG_OBJECT (parse, "discarding head buffer"); |
| 2990 |
gst_buffer_unref (buf); |
| 2991 |
} |
| 2992 |
parse->priv->buffers_pending = |
| 2993 |
g_slist_delete_link (parse->priv->buffers_pending, |
| 2994 |
parse->priv->buffers_pending); |
| 2995 |
} |
| 2996 |
|
| 2997 |
/* chain looks for frames and queues resulting ones (in stead of pushing) */ |
| 2998 |
/* initial skipped data is added to buffers_pending */ |
| 2999 |
gst_base_parse_drain (parse); |
| 3000 |
|
| 3001 |
if (parse->priv->buffers_send) { |
| 3002 |
buf = GST_BUFFER_CAST (parse->priv->buffers_send->data); |
| 3003 |
seen_key |= !GST_BUFFER_FLAG_IS_SET (buf, GST_BUFFER_FLAG_DELTA_UNIT); |
| 3004 |
} |
| 3005 |
|
| 3006 |
/* add metadata (if needed to queued buffers */ |
| 3007 |
GST_LOG_OBJECT (parse, "last timestamp: %" GST_TIME_FORMAT, |
| 3008 |
GST_TIME_ARGS (parse->priv->last_pts)); |
| 3009 |
while (parse->priv->buffers_queued) { |
| 3010 |
buf = GST_BUFFER_CAST (parse->priv->buffers_queued->data); |
| 3011 |
|
| 3012 |
/* no touching if upstream or parsing provided time */ |
| 3013 |
if (GST_BUFFER_PTS_IS_VALID (buf)) { |
| 3014 |
GST_LOG_OBJECT (parse, "buffer has time %" GST_TIME_FORMAT, |
| 3015 |
GST_TIME_ARGS (GST_BUFFER_PTS (buf))); |
| 3016 |
} else if (GST_BUFFER_DURATION_IS_VALID (buf)) { |
| 3017 |
if (GST_CLOCK_TIME_IS_VALID (parse->priv->last_pts)) { |
| 3018 |
if (G_LIKELY (GST_BUFFER_DURATION (buf) <= parse->priv->last_pts)) |
| 3019 |
parse->priv->last_pts -= GST_BUFFER_DURATION (buf); |
| 3020 |
else |
| 3021 |
parse->priv->last_pts = 0; |
| 3022 |
GST_BUFFER_PTS (buf) = parse->priv->last_pts; |
| 3023 |
GST_LOG_OBJECT (parse, "applied time %" GST_TIME_FORMAT, |
| 3024 |
GST_TIME_ARGS (GST_BUFFER_PTS (buf))); |
| 3025 |
} |
| 3026 |
if (GST_CLOCK_TIME_IS_VALID (parse->priv->last_dts)) { |
| 3027 |
if (G_LIKELY (GST_BUFFER_DURATION (buf) <= parse->priv->last_dts)) |
| 3028 |
parse->priv->last_dts -= GST_BUFFER_DURATION (buf); |
| 3029 |
else |
| 3030 |
parse->priv->last_dts = 0; |
| 3031 |
GST_BUFFER_DTS (buf) = parse->priv->last_dts; |
| 3032 |
GST_LOG_OBJECT (parse, "applied dts %" GST_TIME_FORMAT, |
| 3033 |
GST_TIME_ARGS (GST_BUFFER_DTS (buf))); |
| 3034 |
} |
| 3035 |
} else { |
| 3036 |
/* no idea, very bad */ |
| 3037 |
GST_WARNING_OBJECT (parse, "could not determine time for buffer"); |
| 3038 |
} |
| 3039 |
|
| 3040 |
parse->priv->last_pts = GST_BUFFER_PTS (buf); |
| 3041 |
parse->priv->last_dts = GST_BUFFER_DTS (buf); |
| 3042 |
|
| 3043 |
/* reverse order for ascending sending */ |
| 3044 |
/* send downstream at keyframe not preceded by a keyframe |
| 3045 |
* (e.g. that should identify start of collection of IDR nals) */ |
| 3046 |
if (GST_BUFFER_FLAG_IS_SET (buf, GST_BUFFER_FLAG_DELTA_UNIT)) { |
| 3047 |
if (seen_key) { |
| 3048 |
ret = gst_base_parse_send_buffers (parse); |
| 3049 |
/* if a problem, throw all to sending */ |
| 3050 |
if (ret != GST_FLOW_OK) { |
| 3051 |
parse->priv->buffers_send = |
| 3052 |
g_slist_reverse (parse->priv->buffers_queued); |
| 3053 |
parse->priv->buffers_queued = NULL; |
| 3054 |
break; |
| 3055 |
} |
| 3056 |
seen_key = FALSE; |
| 3057 |
} |
| 3058 |
seen_delta = TRUE; |
| 3059 |
} else { |
| 3060 |
seen_key = TRUE; |
| 3061 |
} |
| 3062 |
|
| 3063 |
parse->priv->buffers_send = |
| 3064 |
g_slist_prepend (parse->priv->buffers_send, buf); |
| 3065 |
parse->priv->buffers_queued = |
| 3066 |
g_slist_delete_link (parse->priv->buffers_queued, |
| 3067 |
parse->priv->buffers_queued); |
| 3068 |
} |
| 3069 |
|
| 3070 |
/* audio may have all marked as keyframe, so arrange to send here. Also |
| 3071 |
* we might have ended the loop above on a keyframe, in which case we |
| 3072 |
* should */ |
| 3073 |
if (!seen_delta || seen_key) |
| 3074 |
ret = gst_base_parse_send_buffers (parse); |
| 3075 |
|
| 3076 |
/* any trailing unused no longer usable (ideally none) */ |
| 3077 |
if (G_UNLIKELY (gst_adapter_available (parse->priv->adapter))) { |
| 3078 |
GST_DEBUG_OBJECT (parse, "discarding %" G_GSIZE_FORMAT " trailing bytes", |
| 3079 |
gst_adapter_available (parse->priv->adapter)); |
| 3080 |
gst_adapter_clear (parse->priv->adapter); |
| 3081 |
} |
| 3082 |
|
| 3083 |
return ret; |
| 3084 |
} |
| 3085 |
|
| 3086 |
/* small helper that checks whether we have been trying to resync too long */ |
| 3087 |
static inline GstFlowReturn |
| 3088 |
gst_base_parse_check_sync (GstBaseParse * parse) |
| 3089 |
{ |
| 3090 |
if (G_UNLIKELY (parse->priv->discont && |
| 3091 |
parse->priv->offset - parse->priv->sync_offset > 2 * 1024 * 1024)) { |
| 3092 |
GST_ELEMENT_ERROR (parse, STREAM, DECODE, |
| 3093 |
("Failed to parse stream"), (NULL)); |
| 3094 |
return GST_FLOW_ERROR; |
| 3095 |
} |
| 3096 |
|
| 3097 |
return GST_FLOW_OK; |
| 3098 |
} |
| 3099 |
|
| 3100 |
static GstFlowReturn |
| 3101 |
gst_base_parse_process_streamheader (GstBaseParse * parse) |
| 3102 |
{ |
| 3103 |
GstCaps *caps; |
| 3104 |
GstStructure *str; |
| 3105 |
const GValue *value; |
| 3106 |
GstFlowReturn ret = GST_FLOW_OK; |
| 3107 |
|
| 3108 |
caps = gst_pad_get_current_caps (GST_BASE_PARSE_SINK_PAD (parse)); |
| 3109 |
if (caps == NULL) |
| 3110 |
goto notfound; |
| 3111 |
|
| 3112 |
str = gst_caps_get_structure (caps, 0); |
| 3113 |
value = gst_structure_get_value (str, "streamheader"); |
| 3114 |
if (value == NULL) |
| 3115 |
goto notfound; |
| 3116 |
|
| 3117 |
GST_DEBUG_OBJECT (parse, "Found streamheader field on input caps"); |
| 3118 |
|
| 3119 |
if (GST_VALUE_HOLDS_ARRAY (value)) { |
| 3120 |
gint i; |
| 3121 |
gsize len = gst_value_array_get_size (value); |
| 3122 |
|
| 3123 |
for (i = 0; i < len; i++) { |
| 3124 |
GstBuffer *buffer = |
| 3125 |
gst_value_get_buffer (gst_value_array_get_value (value, i)); |
| 3126 |
ret = |
| 3127 |
gst_base_parse_chain (GST_BASE_PARSE_SINK_PAD (parse), |
| 3128 |
GST_OBJECT_CAST (parse), gst_buffer_ref (buffer)); |
| 3129 |
} |
| 3130 |
|
| 3131 |
} else if (GST_VALUE_HOLDS_BUFFER (value)) { |
| 3132 |
GstBuffer *buffer = gst_value_get_buffer (value); |
| 3133 |
ret = |
| 3134 |
gst_base_parse_chain (GST_BASE_PARSE_SINK_PAD (parse), |
| 3135 |
GST_OBJECT_CAST (parse), gst_buffer_ref (buffer)); |
| 3136 |
} |
| 3137 |
|
| 3138 |
gst_caps_unref (caps); |
| 3139 |
|
| 3140 |
return ret; |
| 3141 |
|
| 3142 |
notfound: |
| 3143 |
{ |
| 3144 |
if (caps) { |
| 3145 |
gst_caps_unref (caps); |
| 3146 |
} |
| 3147 |
|
| 3148 |
GST_DEBUG_OBJECT (parse, "No streamheader on caps"); |
| 3149 |
return GST_FLOW_OK; |
| 3150 |
} |
| 3151 |
} |
| 3152 |
|
| 3153 |
static GstFlowReturn |
| 3154 |
gst_base_parse_chain (GstPad * pad, GstObject * parent, GstBuffer * buffer) |
| 3155 |
{ |
| 3156 |
GstBaseParseClass *bclass; |
| 3157 |
GstBaseParse *parse; |
| 3158 |
GstFlowReturn ret = GST_FLOW_OK; |
| 3159 |
GstFlowReturn old_ret = GST_FLOW_OK; |
| 3160 |
GstBuffer *tmpbuf = NULL; |
| 3161 |
guint fsize = 1; |
| 3162 |
gint skip = -1; |
| 3163 |
guint min_size, av; |
| 3164 |
GstClockTime pts, dts; |
| 3165 |
|
| 3166 |
parse = GST_BASE_PARSE (parent); |
| 3167 |
bclass = GST_BASE_PARSE_GET_CLASS (parse); |
| 3168 |
GST_DEBUG_OBJECT (parent, "chain"); |
| 3169 |
|
| 3170 |
/* early out for speed, if we need to skip */ |
| 3171 |
if (buffer && GST_BUFFER_IS_DISCONT (buffer)) |
| 3172 |
parse->priv->skip = 0; |
| 3173 |
if (parse->priv->skip > 0) { |
| 3174 |
gsize bsize = gst_buffer_get_size (buffer); |
| 3175 |
GST_DEBUG ("Got %" G_GSIZE_FORMAT " buffer, need to skip %u", bsize, |
| 3176 |
parse->priv->skip); |
| 3177 |
if (parse->priv->skip >= bsize) { |
| 3178 |
parse->priv->skip -= bsize; |
| 3179 |
GST_DEBUG ("All the buffer is skipped"); |
| 3180 |
parse->priv->offset += bsize; |
| 3181 |
parse->priv->sync_offset = parse->priv->offset; |
| 3182 |
gst_buffer_unref (buffer); |
| 3183 |
return GST_FLOW_OK; |
| 3184 |
} |
| 3185 |
buffer = gst_buffer_make_writable (buffer); |
| 3186 |
gst_buffer_resize (buffer, parse->priv->skip, bsize - parse->priv->skip); |
| 3187 |
parse->priv->offset += parse->priv->skip; |
| 3188 |
GST_DEBUG ("Done skipping, we have %u left on this buffer", |
| 3189 |
(unsigned) (bsize - parse->priv->skip)); |
| 3190 |
parse->priv->skip = 0; |
| 3191 |
parse->priv->discont = TRUE; |
| 3192 |
} |
| 3193 |
|
| 3194 |
if (G_UNLIKELY (parse->priv->first_buffer)) { |
| 3195 |
parse->priv->first_buffer = FALSE; |
| 3196 |
if (!GST_BUFFER_FLAG_IS_SET (buffer, GST_BUFFER_FLAG_HEADER)) { |
| 3197 |
/* this stream has no header buffers, check if we just prepend the |
| 3198 |
* streamheader from caps to the stream */ |
| 3199 |
GST_DEBUG_OBJECT (parse, "Looking for streamheader field on caps to " |
| 3200 |
"prepend to the stream"); |
| 3201 |
gst_base_parse_process_streamheader (parse); |
| 3202 |
} else { |
| 3203 |
GST_DEBUG_OBJECT (parse, "Stream has header buffers, not prepending " |
| 3204 |
"streamheader from caps"); |
| 3205 |
} |
| 3206 |
} |
| 3207 |
|
| 3208 |
if (parse->priv->detecting) { |
| 3209 |
GstBuffer *detect_buf; |
| 3210 |
|
| 3211 |
if (parse->priv->detect_buffers_size == 0) { |
| 3212 |
detect_buf = gst_buffer_ref (buffer); |
| 3213 |
} else { |
| 3214 |
GList *l; |
| 3215 |
guint offset = 0; |
| 3216 |
|
| 3217 |
detect_buf = gst_buffer_new (); |
| 3218 |
|
| 3219 |
for (l = parse->priv->detect_buffers; l; l = l->next) { |
| 3220 |
gsize tmpsize = gst_buffer_get_size (l->data); |
| 3221 |
|
| 3222 |
gst_buffer_copy_into (detect_buf, GST_BUFFER_CAST (l->data), |
| 3223 |
GST_BUFFER_COPY_MEMORY, offset, tmpsize); |
| 3224 |
offset += tmpsize; |
| 3225 |
} |
| 3226 |
if (buffer) |
| 3227 |
gst_buffer_copy_into (detect_buf, buffer, GST_BUFFER_COPY_MEMORY, |
| 3228 |
offset, gst_buffer_get_size (buffer)); |
| 3229 |
} |
| 3230 |
|
| 3231 |
ret = bclass->detect (parse, detect_buf); |
| 3232 |
gst_buffer_unref (detect_buf); |
| 3233 |
|
| 3234 |
if (ret == GST_FLOW_OK) { |
| 3235 |
GList *l; |
| 3236 |
|
| 3237 |
/* Detected something */ |
| 3238 |
parse->priv->detecting = FALSE; |
| 3239 |
|
| 3240 |
for (l = parse->priv->detect_buffers; l; l = l->next) { |
| 3241 |
if (ret == GST_FLOW_OK && !parse->priv->flushing) |
| 3242 |
ret = |
| 3243 |
gst_base_parse_chain (GST_BASE_PARSE_SINK_PAD (parse), |
| 3244 |
parent, GST_BUFFER_CAST (l->data)); |
| 3245 |
else |
| 3246 |
gst_buffer_unref (GST_BUFFER_CAST (l->data)); |
| 3247 |
} |
| 3248 |
g_list_free (parse->priv->detect_buffers); |
| 3249 |
parse->priv->detect_buffers = NULL; |
| 3250 |
parse->priv->detect_buffers_size = 0; |
| 3251 |
|
| 3252 |
if (ret != GST_FLOW_OK) { |
| 3253 |
gst_buffer_unref (buffer); |
| 3254 |
return ret; |
| 3255 |
} |
| 3256 |
|
| 3257 |
/* Handle the current buffer */ |
| 3258 |
} else if (ret == GST_FLOW_NOT_NEGOTIATED) { |
| 3259 |
/* Still detecting, append buffer or error out if draining */ |
| 3260 |
|
| 3261 |
if (parse->priv->drain) { |
| 3262 |
GST_DEBUG_OBJECT (parse, "Draining but did not detect format yet"); |
| 3263 |
gst_buffer_unref (buffer); |
| 3264 |
return GST_FLOW_ERROR; |
| 3265 |
} else if (parse->priv->flushing) { |
| 3266 |
g_list_foreach (parse->priv->detect_buffers, (GFunc) gst_buffer_unref, |
| 3267 |
NULL); |
| 3268 |
g_list_free (parse->priv->detect_buffers); |
| 3269 |
parse->priv->detect_buffers = NULL; |
| 3270 |
parse->priv->detect_buffers_size = 0; |
| 3271 |
} else { |
| 3272 |
parse->priv->detect_buffers = |
| 3273 |
g_list_append (parse->priv->detect_buffers, buffer); |
| 3274 |
parse->priv->detect_buffers_size += gst_buffer_get_size (buffer); |
| 3275 |
return GST_FLOW_OK; |
| 3276 |
} |
| 3277 |
} else { |
| 3278 |
/* Something went wrong, subclass responsible for error reporting */ |
| 3279 |
gst_buffer_unref (buffer); |
| 3280 |
g_list_foreach (parse->priv->detect_buffers, (GFunc) gst_buffer_unref, |
| 3281 |
NULL); |
| 3282 |
g_list_free (parse->priv->detect_buffers); |
| 3283 |
parse->priv->detect_buffers = NULL; |
| 3284 |
parse->priv->detect_buffers_size = 0; |
| 3285 |
return ret; |
| 3286 |
} |
| 3287 |
|
| 3288 |
/* And now handle the current buffer if detection worked */ |
| 3289 |
} |
| 3290 |
|
| 3291 |
if (G_LIKELY (buffer)) { |
| 3292 |
GST_LOG_OBJECT (parse, |
| 3293 |
"buffer size: %" G_GSIZE_FORMAT ", offset = %" G_GINT64_FORMAT |
| 3294 |
", dts %" GST_TIME_FORMAT ", pts %" GST_TIME_FORMAT, |
| 3295 |
gst_buffer_get_size (buffer), GST_BUFFER_OFFSET (buffer), |
| 3296 |
GST_TIME_ARGS (GST_BUFFER_DTS (buffer)), |
| 3297 |
GST_TIME_ARGS (GST_BUFFER_PTS (buffer))); |
| 3298 |
|
| 3299 |
if (G_UNLIKELY (!parse->priv->disable_passthrough |
| 3300 |
&& parse->priv->passthrough)) { |
| 3301 |
GstBaseParseFrame frame; |
| 3302 |
|
| 3303 |
gst_base_parse_frame_init (&frame); |
| 3304 |
frame.buffer = gst_buffer_make_writable (buffer); |
| 3305 |
ret = gst_base_parse_push_frame (parse, &frame); |
| 3306 |
gst_base_parse_frame_free (&frame); |
| 3307 |
return ret; |
| 3308 |
} |
| 3309 |
if (G_UNLIKELY (GST_BUFFER_FLAG_IS_SET (buffer, GST_BUFFER_FLAG_DISCONT))) { |
| 3310 |
/* upstream feeding us in reverse playback; |
| 3311 |
* finish previous fragment and start new upon DISCONT */ |
| 3312 |
if (parse->segment.rate < 0.0) { |
| 3313 |
GST_DEBUG_OBJECT (parse, "buffer starts new reverse playback fragment"); |
| 3314 |
ret = gst_base_parse_finish_fragment (parse, TRUE); |
| 3315 |
gst_base_parse_start_fragment (parse); |
| 3316 |
} else { |
| 3317 |
/* discont in the stream, drain and mark discont for next output */ |
| 3318 |
gst_base_parse_drain (parse); |
| 3319 |
parse->priv->discont = TRUE; |
| 3320 |
} |
| 3321 |
} |
| 3322 |
gst_adapter_push (parse->priv->adapter, buffer); |
| 3323 |
} |
| 3324 |
|
| 3325 |
/* Parse and push as many frames as possible */ |
| 3326 |
/* Stop either when adapter is empty or we are flushing */ |
| 3327 |
while (!parse->priv->flushing) { |
| 3328 |
gint flush = 0; |
| 3329 |
gboolean updated_prev_pts = FALSE; |
| 3330 |
|
| 3331 |
/* note: if subclass indicates MAX fsize, |
| 3332 |
* this will not likely be available anyway ... */ |
| 3333 |
min_size = MAX (parse->priv->min_frame_size, fsize); |
| 3334 |
av = gst_adapter_available (parse->priv->adapter); |
| 3335 |
|
| 3336 |
if (G_UNLIKELY (parse->priv->drain)) { |
| 3337 |
min_size = av; |
| 3338 |
GST_DEBUG_OBJECT (parse, "draining, data left: %d", min_size); |
| 3339 |
if (G_UNLIKELY (!min_size)) { |
| 3340 |
goto done; |
| 3341 |
} |
| 3342 |
} |
| 3343 |
|
| 3344 |
/* Collect at least min_frame_size bytes */ |
| 3345 |
if (av < min_size) { |
| 3346 |
GST_DEBUG_OBJECT (parse, "not enough data available (only %d bytes)", av); |
| 3347 |
goto done; |
| 3348 |
} |
| 3349 |
|
| 3350 |
/* move along with upstream timestamp (if any), |
| 3351 |
* but interpolate in between */ |
| 3352 |
pts = gst_adapter_prev_pts (parse->priv->adapter, NULL); |
| 3353 |
dts = gst_adapter_prev_dts (parse->priv->adapter, NULL); |
| 3354 |
if (GST_CLOCK_TIME_IS_VALID (pts) && (parse->priv->prev_pts != pts)) { |
| 3355 |
parse->priv->prev_pts = parse->priv->next_pts = pts; |
| 3356 |
updated_prev_pts = TRUE; |
| 3357 |
} |
| 3358 |
|
| 3359 |
if (GST_CLOCK_TIME_IS_VALID (dts) && (parse->priv->prev_dts != dts)) { |
| 3360 |
parse->priv->prev_dts = parse->priv->next_dts = dts; |
| 3361 |
parse->priv->prev_dts_from_pts = FALSE; |
| 3362 |
} |
| 3363 |
|
| 3364 |
/* we can mess with, erm interpolate, timestamps, |
| 3365 |
* and incoming stuff has PTS but no DTS seen so far, |
| 3366 |
* then pick up DTS from PTS and hope for the best ... */ |
| 3367 |
if (parse->priv->infer_ts && |
| 3368 |
parse->priv->pts_interpolate && |
| 3369 |
!GST_CLOCK_TIME_IS_VALID (dts) && |
| 3370 |
(!GST_CLOCK_TIME_IS_VALID (parse->priv->prev_dts) |
| 3371 |
|| (parse->priv->prev_dts_from_pts && updated_prev_pts)) |
| 3372 |
&& GST_CLOCK_TIME_IS_VALID (pts)) { |
| 3373 |
parse->priv->prev_dts = parse->priv->next_dts = pts; |
| 3374 |
parse->priv->prev_dts_from_pts = TRUE; |
| 3375 |
} |
| 3376 |
|
| 3377 |
/* always pass all available data */ |
| 3378 |
tmpbuf = gst_adapter_get_buffer (parse->priv->adapter, av); |
| 3379 |
|
| 3380 |
/* already inform subclass what timestamps we have planned, |
| 3381 |
* at least if provided by time-based upstream */ |
| 3382 |
if (parse->priv->upstream_format == GST_FORMAT_TIME) { |
| 3383 |
gboolean timestamp_updated = FALSE; |
| 3384 |
if (GST_BUFFER_PTS (tmpbuf) != parse->priv->next_pts || |
| 3385 |
GST_BUFFER_DTS (tmpbuf) != parse->priv->next_dts) { |
| 3386 |
timestamp_updated = TRUE; |
| 3387 |
} |
| 3388 |
|
| 3389 |
/* Preserve upstream buffer duration if timestamp is the same as expected |
| 3390 |
* ones already and subclass disabled pts interpolation/inferring */ |
| 3391 |
if (timestamp_updated || parse->priv->infer_ts || |
| 3392 |
parse->priv->pts_interpolate) { |
| 3393 |
tmpbuf = gst_buffer_make_writable (tmpbuf); |
| 3394 |
GST_BUFFER_PTS (tmpbuf) = parse->priv->next_pts; |
| 3395 |
GST_BUFFER_DTS (tmpbuf) = parse->priv->next_dts; |
| 3396 |
GST_BUFFER_DURATION (tmpbuf) = GST_CLOCK_TIME_NONE; |
| 3397 |
} |
| 3398 |
} |
| 3399 |
|
| 3400 |
/* keep the adapter mapped, so keep track of what has to be flushed */ |
| 3401 |
ret = gst_base_parse_handle_buffer (parse, tmpbuf, &skip, &flush); |
| 3402 |
tmpbuf = NULL; |
| 3403 |
|
| 3404 |
if (ret != GST_FLOW_OK && ret != GST_FLOW_NOT_LINKED) { |
| 3405 |
goto done; |
| 3406 |
} |
| 3407 |
if (skip == 0 && flush == 0) { |
| 3408 |
GST_LOG_OBJECT (parse, "nothing skipped and no frames finished, " |
| 3409 |
"breaking to get more data"); |
| 3410 |
/* ignore this return as it produced no data */ |
| 3411 |
ret = old_ret; |
| 3412 |
goto done; |
| 3413 |
} |
| 3414 |
if (old_ret == GST_FLOW_OK) |
| 3415 |
old_ret = ret; |
| 3416 |
} |
| 3417 |
|
| 3418 |
done: |
| 3419 |
GST_LOG_OBJECT (parse, "chain leaving"); |
| 3420 |
return ret; |
| 3421 |
} |
| 3422 |
|
| 3423 |
/* Return the number of bytes available in the cached |
| 3424 |
* read buffer, if any */ |
| 3425 |
static guint |
| 3426 |
gst_base_parse_get_cached_available (GstBaseParse * parse) |
| 3427 |
{ |
| 3428 |
if (parse->priv->cache != NULL) { |
| 3429 |
gint64 cache_offset = GST_BUFFER_OFFSET (parse->priv->cache); |
| 3430 |
gint cache_size = gst_buffer_get_size (parse->priv->cache); |
| 3431 |
|
| 3432 |
if (parse->priv->offset >= cache_offset |
| 3433 |
&& parse->priv->offset < cache_offset + cache_size) |
| 3434 |
return cache_size - (parse->priv->offset - cache_offset); /* Size of the cache minus consumed */ |
| 3435 |
} |
| 3436 |
return 0; |
| 3437 |
} |
| 3438 |
|
| 3439 |
/* pull @size bytes at current offset, |
| 3440 |
* i.e. at least try to and possibly return a shorter buffer if near the end */ |
| 3441 |
static GstFlowReturn |
| 3442 |
gst_base_parse_pull_range (GstBaseParse * parse, guint size, |
| 3443 |
GstBuffer ** buffer) |
| 3444 |
{ |
| 3445 |
GstFlowReturn ret = GST_FLOW_OK; |
| 3446 |
guint read_size; |
| 3447 |
|
| 3448 |
g_return_val_if_fail (buffer != NULL, GST_FLOW_ERROR); |
| 3449 |
|
| 3450 |
/* Caching here actually makes much less difference than one would expect. |
| 3451 |
* We do it mainly to avoid pulling buffers of 1 byte all the time */ |
| 3452 |
if (parse->priv->cache) { |
| 3453 |
gint64 cache_offset = GST_BUFFER_OFFSET (parse->priv->cache); |
| 3454 |
gint cache_size = gst_buffer_get_size (parse->priv->cache); |
| 3455 |
|
| 3456 |
if (cache_offset <= parse->priv->offset && |
| 3457 |
(parse->priv->offset + size) <= (cache_offset + cache_size)) { |
| 3458 |
*buffer = gst_buffer_copy_region (parse->priv->cache, GST_BUFFER_COPY_ALL, |
| 3459 |
parse->priv->offset - cache_offset, size); |
| 3460 |
GST_BUFFER_OFFSET (*buffer) = parse->priv->offset; |
| 3461 |
GST_LOG_OBJECT (parse, |
| 3462 |
"Satisfying read request of %u bytes from cached buffer with offset %" |
| 3463 |
G_GINT64_FORMAT, size, cache_offset); |
| 3464 |
return GST_FLOW_OK; |
| 3465 |
} |
| 3466 |
/* not enough data in the cache, free cache and get a new one */ |
| 3467 |
gst_buffer_unref (parse->priv->cache); |
| 3468 |
parse->priv->cache = NULL; |
| 3469 |
} |
| 3470 |
|
| 3471 |
/* refill the cache */ |
| 3472 |
read_size = MAX (64 * 1024, size); |
| 3473 |
GST_LOG_OBJECT (parse, |
| 3474 |
"Reading cache buffer of %u bytes from offset %" G_GINT64_FORMAT, |
| 3475 |
read_size, parse->priv->offset); |
| 3476 |
ret = |
| 3477 |
gst_pad_pull_range (parse->sinkpad, parse->priv->offset, read_size, |
| 3478 |
&parse->priv->cache); |
| 3479 |
if (ret != GST_FLOW_OK) { |
| 3480 |
parse->priv->cache = NULL; |
| 3481 |
return ret; |
| 3482 |
} |
| 3483 |
|
| 3484 |
if (gst_buffer_get_size (parse->priv->cache) < size) { |
| 3485 |
GST_DEBUG_OBJECT (parse, "Returning short buffer at offset %" |
| 3486 |
G_GUINT64_FORMAT ": wanted %u bytes, got %" G_GSIZE_FORMAT " bytes", |
| 3487 |
parse->priv->offset, size, gst_buffer_get_size (parse->priv->cache)); |
| 3488 |
|
| 3489 |
*buffer = parse->priv->cache; |
| 3490 |
parse->priv->cache = NULL; |
| 3491 |
|
| 3492 |
return GST_FLOW_OK; |
| 3493 |
} |
| 3494 |
|
| 3495 |
GST_BUFFER_OFFSET (parse->priv->cache) = parse->priv->offset; |
| 3496 |
|
| 3497 |
*buffer = |
| 3498 |
gst_buffer_copy_region (parse->priv->cache, GST_BUFFER_COPY_ALL, 0, size); |
| 3499 |
GST_BUFFER_OFFSET (*buffer) = parse->priv->offset; |
| 3500 |
|
| 3501 |
return GST_FLOW_OK; |
| 3502 |
} |
| 3503 |
|
| 3504 |
static GstFlowReturn |
| 3505 |
gst_base_parse_handle_previous_fragment (GstBaseParse * parse) |
| 3506 |
{ |
| 3507 |
gint64 offset = 0; |
| 3508 |
GstClockTime ts = 0; |
| 3509 |
GstBuffer *buffer; |
| 3510 |
GstFlowReturn ret; |
| 3511 |
|
| 3512 |
GST_DEBUG_OBJECT (parse, "fragment ended; last_ts = %" GST_TIME_FORMAT |
| 3513 |
", last_offset = %" G_GINT64_FORMAT, |
| 3514 |
GST_TIME_ARGS (parse->priv->last_pts), parse->priv->last_offset); |
| 3515 |
|
| 3516 |
if (!parse->priv->last_offset |
| 3517 |
|| parse->priv->last_pts <= parse->segment.start) { |
| 3518 |
GST_DEBUG_OBJECT (parse, "past start of segment %" GST_TIME_FORMAT, |
| 3519 |
GST_TIME_ARGS (parse->segment.start)); |
| 3520 |
ret = GST_FLOW_EOS; |
| 3521 |
goto exit; |
| 3522 |
} |
| 3523 |
|
| 3524 |
/* last fragment started at last_offset / last_ts; |
| 3525 |
* seek back 10s capped at 1MB */ |
| 3526 |
if (parse->priv->last_pts >= 10 * GST_SECOND) |
| 3527 |
ts = parse->priv->last_pts - 10 * GST_SECOND; |
| 3528 |
/* if we are exact now, we will be more so going backwards */ |
| 3529 |
if (parse->priv->exact_position) { |
| 3530 |
offset = gst_base_parse_find_offset (parse, ts, TRUE, NULL); |
| 3531 |
} else { |
| 3532 |
if (!gst_base_parse_convert (parse, GST_FORMAT_TIME, ts, |
| 3533 |
GST_FORMAT_BYTES, &offset)) { |
| 3534 |
GST_DEBUG_OBJECT (parse, "conversion failed, only BYTE based"); |
| 3535 |
} |
| 3536 |
} |
| 3537 |
offset = CLAMP (offset, parse->priv->last_offset - 1024 * 1024, |
| 3538 |
parse->priv->last_offset - 1024); |
| 3539 |
offset = MAX (0, offset); |
| 3540 |
|
| 3541 |
GST_DEBUG_OBJECT (parse, "next fragment from offset %" G_GINT64_FORMAT, |
| 3542 |
offset); |
| 3543 |
parse->priv->offset = offset; |
| 3544 |
|
| 3545 |
ret = gst_base_parse_pull_range (parse, parse->priv->last_offset - offset, |
| 3546 |
&buffer); |
| 3547 |
if (ret != GST_FLOW_OK) |
| 3548 |
goto exit; |
| 3549 |
|
| 3550 |
/* offset will increase again as fragment is processed/parsed */ |
| 3551 |
parse->priv->last_offset = offset; |
| 3552 |
|
| 3553 |
gst_base_parse_start_fragment (parse); |
| 3554 |
gst_adapter_push (parse->priv->adapter, buffer); |
| 3555 |
ret = gst_base_parse_finish_fragment (parse, TRUE); |
| 3556 |
if (ret != GST_FLOW_OK) |
| 3557 |
goto exit; |
| 3558 |
|
| 3559 |
/* force previous fragment */ |
| 3560 |
parse->priv->offset = -1; |
| 3561 |
|
| 3562 |
exit: |
| 3563 |
return ret; |
| 3564 |
} |
| 3565 |
|
| 3566 |
/* PULL mode: |
| 3567 |
* pull and scan for next frame starting from current offset |
| 3568 |
* adjusts sync, drain and offset going along */ |
| 3569 |
static GstFlowReturn |
| 3570 |
gst_base_parse_scan_frame (GstBaseParse * parse, GstBaseParseClass * klass) |
| 3571 |
{ |
| 3572 |
GstBuffer *buffer; |
| 3573 |
GstFlowReturn ret = GST_FLOW_OK; |
| 3574 |
guint fsize, min_size; |
| 3575 |
gint flushed = 0; |
| 3576 |
gint skip = 0; |
| 3577 |
|
| 3578 |
GST_LOG_OBJECT (parse, "scanning for frame at offset %" G_GUINT64_FORMAT |
| 3579 |
" (%#" G_GINT64_MODIFIER "x)", parse->priv->offset, parse->priv->offset); |
| 3580 |
|
| 3581 |
/* let's make this efficient for all subclass once and for all; |
| 3582 |
* maybe it does not need this much, but in the latter case, we know we are |
| 3583 |
* in pull mode here and might as well try to read and supply more anyway, |
| 3584 |
* so start with the cached buffer, or if that's shrunk below 1024 bytes, |
| 3585 |
* pull a new cache buffer */ |
| 3586 |
fsize = gst_base_parse_get_cached_available (parse); |
| 3587 |
if (fsize < 1024) |
| 3588 |
fsize = 64 * 1024; |
| 3589 |
|
| 3590 |
while (TRUE) { |
| 3591 |
min_size = MAX (parse->priv->min_frame_size, fsize); |
| 3592 |
|
| 3593 |
GST_LOG_OBJECT (parse, "reading buffer size %u", min_size); |
| 3594 |
|
| 3595 |
parse->priv->drain = FALSE; |
| 3596 |
ret = gst_base_parse_pull_range (parse, min_size, &buffer); |
| 3597 |
if (ret != GST_FLOW_OK) |
| 3598 |
goto done; |
| 3599 |
|
| 3600 |
/* if we got a short read, inform subclass we are draining leftover |
| 3601 |
* and no more is to be expected */ |
| 3602 |
if (gst_buffer_get_size (buffer) < min_size) { |
| 3603 |
GST_LOG_OBJECT (parse, "... but did not get that; marked draining"); |
| 3604 |
parse->priv->drain = TRUE; |
| 3605 |
} |
| 3606 |
|
| 3607 |
if (parse->priv->detecting) { |
| 3608 |
ret = klass->detect (parse, buffer); |
| 3609 |
if (ret == GST_FLOW_NOT_NEGOTIATED) { |
| 3610 |
/* If draining we error out, otherwise request a buffer |
| 3611 |
* with 64kb more */ |
| 3612 |
if (parse->priv->drain) { |
| 3613 |
gst_buffer_unref (buffer); |
| 3614 |
GST_ERROR_OBJECT (parse, "Failed to detect format but draining"); |
| 3615 |
return GST_FLOW_ERROR; |
| 3616 |
} else { |
| 3617 |
/* Double our frame size, or increment by at most 64KB */ |
| 3618 |
fsize += MIN (fsize, 64 * 1024); |
| 3619 |
gst_buffer_unref (buffer); |
| 3620 |
continue; |
| 3621 |
} |
| 3622 |
} else if (ret != GST_FLOW_OK) { |
| 3623 |
gst_buffer_unref (buffer); |
| 3624 |
GST_ERROR_OBJECT (parse, "detect() returned %s", |
| 3625 |
gst_flow_get_name (ret)); |
| 3626 |
return ret; |
| 3627 |
} |
| 3628 |
|
| 3629 |
/* Else handle this buffer normally */ |
| 3630 |
} |
| 3631 |
|
| 3632 |
ret = gst_base_parse_handle_buffer (parse, buffer, &skip, &flushed); |
| 3633 |
if (ret != GST_FLOW_OK) |
| 3634 |
break; |
| 3635 |
|
| 3636 |
/* If a large amount of data was requested to be skipped, _handle_buffer |
| 3637 |
might have set the priv->skip flag to an extra amount on top of skip. |
| 3638 |
In pull mode, we can just pull from the new offset directly. */ |
| 3639 |
parse->priv->offset += parse->priv->skip; |
| 3640 |
parse->priv->skip = 0; |
| 3641 |
|
| 3642 |
/* something flushed means something happened, |
| 3643 |
* and we should bail out of this loop so as not to occupy |
| 3644 |
* the task thread indefinitely */ |
| 3645 |
if (flushed) { |
| 3646 |
GST_LOG_OBJECT (parse, "frame finished, breaking loop"); |
| 3647 |
break; |
| 3648 |
} |
| 3649 |
if (!skip) { |
| 3650 |
if (parse->priv->drain) { |
| 3651 |
/* nothing flushed, no skip and draining, so nothing left to do */ |
| 3652 |
GST_LOG_OBJECT (parse, "no activity or result when draining; " |
| 3653 |
"breaking loop and marking EOS"); |
| 3654 |
ret = GST_FLOW_EOS; |
| 3655 |
break; |
| 3656 |
} |
| 3657 |
/* otherwise, get some more data |
| 3658 |
* note that is checked this does not happen indefinitely */ |
| 3659 |
GST_LOG_OBJECT (parse, "getting some more data"); |
| 3660 |
|
| 3661 |
/* Double our frame size, or increment by at most 64KB */ |
| 3662 |
fsize += MIN (fsize, 64 * 1024); |
| 3663 |
} |
| 3664 |
} |
| 3665 |
|
| 3666 |
done: |
| 3667 |
return ret; |
| 3668 |
} |
| 3669 |
|
| 3670 |
/* Loop that is used in pull mode to retrieve data from upstream */ |
| 3671 |
static void |
| 3672 |
gst_base_parse_loop (GstPad * pad) |
| 3673 |
{ |
| 3674 |
GstBaseParse *parse; |
| 3675 |
GstBaseParseClass *klass; |
| 3676 |
GstFlowReturn ret = GST_FLOW_OK; |
| 3677 |
|
| 3678 |
parse = GST_BASE_PARSE (gst_pad_get_parent (pad)); |
| 3679 |
klass = GST_BASE_PARSE_GET_CLASS (parse); |
| 3680 |
|
| 3681 |
GST_LOG_OBJECT (parse, "Entering parse loop"); |
| 3682 |
|
| 3683 |
if (G_UNLIKELY (parse->priv->push_stream_start)) { |
| 3684 |
gchar *stream_id; |
| 3685 |
GstEvent *event; |
| 3686 |
|
| 3687 |
stream_id = |
| 3688 |
gst_pad_create_stream_id (parse->srcpad, GST_ELEMENT_CAST (parse), |
| 3689 |
NULL); |
| 3690 |
|
| 3691 |
event = gst_event_new_stream_start (stream_id); |
| 3692 |
gst_event_set_group_id (event, gst_util_group_id_next ()); |
| 3693 |
|
| 3694 |
GST_DEBUG_OBJECT (parse, "Pushing STREAM_START"); |
| 3695 |
gst_pad_push_event (parse->srcpad, event); |
| 3696 |
parse->priv->push_stream_start = FALSE; |
| 3697 |
g_free (stream_id); |
| 3698 |
} |
| 3699 |
|
| 3700 |
/* reverse playback: |
| 3701 |
* first fragment (closest to stop time) is handled normally below, |
| 3702 |
* then we pull in fragments going backwards */ |
| 3703 |
if (parse->segment.rate < 0.0) { |
| 3704 |
/* check if we jumped back to a previous fragment, |
| 3705 |
* which is a post-first fragment */ |
| 3706 |
if (parse->priv->offset < 0) { |
| 3707 |
ret = gst_base_parse_handle_previous_fragment (parse); |
| 3708 |
goto done; |
| 3709 |
} |
| 3710 |
} |
| 3711 |
|
| 3712 |
ret = gst_base_parse_scan_frame (parse, klass); |
| 3713 |
|
| 3714 |
/* eat expected eos signalling past segment in reverse playback */ |
| 3715 |
if (parse->segment.rate < 0.0 && ret == GST_FLOW_EOS && |
| 3716 |
parse->segment.position >= parse->segment.stop) { |
| 3717 |
GST_DEBUG_OBJECT (parse, "downstream has reached end of segment"); |
| 3718 |
/* push what was accumulated during loop run */ |
| 3719 |
gst_base_parse_finish_fragment (parse, FALSE); |
| 3720 |
/* force previous fragment */ |
| 3721 |
parse->priv->offset = -1; |
| 3722 |
goto eos; |
| 3723 |
} |
| 3724 |
|
| 3725 |
if (ret != GST_FLOW_OK) |
| 3726 |
goto done; |
| 3727 |
|
| 3728 |
done: |
| 3729 |
if (ret == GST_FLOW_EOS) |
| 3730 |
goto eos; |
| 3731 |
else if (ret != GST_FLOW_OK) |
| 3732 |
goto pause; |
| 3733 |
|
| 3734 |
gst_object_unref (parse); |
| 3735 |
return; |
| 3736 |
|
| 3737 |
/* ERRORS */ |
| 3738 |
eos: |
| 3739 |
{ |
| 3740 |
ret = GST_FLOW_EOS; |
| 3741 |
GST_DEBUG_OBJECT (parse, "eos"); |
| 3742 |
/* fall-through */ |
| 3743 |
} |
| 3744 |
pause: |
| 3745 |
{ |
| 3746 |
gboolean push_eos = FALSE; |
| 3747 |
|
| 3748 |
GST_DEBUG_OBJECT (parse, "pausing task, reason %s", |
| 3749 |
gst_flow_get_name (ret)); |
| 3750 |
gst_pad_pause_task (parse->sinkpad); |
| 3751 |
|
| 3752 |
if (ret == GST_FLOW_EOS) { |
| 3753 |
/* handle end-of-stream/segment */ |
| 3754 |
if (parse->segment.flags & GST_SEGMENT_FLAG_SEGMENT) { |
| 3755 |
gint64 stop; |
| 3756 |
|
| 3757 |
if ((stop = parse->segment.stop) == -1) |
| 3758 |
stop = parse->segment.duration; |
| 3759 |
|
| 3760 |
GST_DEBUG_OBJECT (parse, "sending segment_done"); |
| 3761 |
|
| 3762 |
gst_element_post_message |
| 3763 |
(GST_ELEMENT_CAST (parse), |
| 3764 |
gst_message_new_segment_done (GST_OBJECT_CAST (parse), |
| 3765 |
GST_FORMAT_TIME, stop)); |
| 3766 |
gst_pad_push_event (parse->srcpad, |
| 3767 |
gst_event_new_segment_done (GST_FORMAT_TIME, stop)); |
| 3768 |
} else { |
| 3769 |
/* If we STILL have zero frames processed, fire an error */ |
| 3770 |
if (parse->priv->framecount == 0) { |
| 3771 |
GST_ELEMENT_ERROR (parse, STREAM, WRONG_TYPE, |
| 3772 |
("No valid frames found before end of stream"), (NULL)); |
| 3773 |
} |
| 3774 |
push_eos = TRUE; |
| 3775 |
} |
| 3776 |
} else if (ret == GST_FLOW_NOT_LINKED || ret < GST_FLOW_EOS) { |
| 3777 |
/* for fatal errors we post an error message, wrong-state is |
| 3778 |
* not fatal because it happens due to flushes and only means |
| 3779 |
* that we should stop now. */ |
| 3780 |
GST_ELEMENT_FLOW_ERROR (parse, ret); |
| 3781 |
push_eos = TRUE; |
| 3782 |
} |
| 3783 |
if (push_eos) { |
| 3784 |
GstEvent *topush; |
| 3785 |
if (parse->priv->estimated_duration <= 0) { |
| 3786 |
gst_base_parse_update_duration (parse); |
| 3787 |
} |
| 3788 |
/* Push pending events, including SEGMENT events */ |
| 3789 |
gst_base_parse_push_pending_events (parse); |
| 3790 |
|
| 3791 |
topush = gst_event_new_eos (); |
| 3792 |
GST_DEBUG_OBJECT (parse, "segment_seqnum:%" G_GUINT32_FORMAT, |
| 3793 |
parse->priv->segment_seqnum); |
| 3794 |
if (parse->priv->segment_seqnum != GST_SEQNUM_INVALID) |
| 3795 |
gst_event_set_seqnum (topush, parse->priv->segment_seqnum); |
| 3796 |
gst_pad_push_event (parse->srcpad, topush); |
| 3797 |
} |
| 3798 |
gst_object_unref (parse); |
| 3799 |
} |
| 3800 |
} |
| 3801 |
|
| 3802 |
static gboolean |
| 3803 |
gst_base_parse_sink_activate (GstPad * sinkpad, GstObject * parent) |
| 3804 |
{ |
| 3805 |
GstSchedulingFlags sched_flags; |
| 3806 |
GstBaseParse *parse; |
| 3807 |
GstQuery *query; |
| 3808 |
gboolean pull_mode; |
| 3809 |
|
| 3810 |
parse = GST_BASE_PARSE (parent); |
| 3811 |
|
| 3812 |
GST_DEBUG_OBJECT (parse, "sink activate"); |
| 3813 |
|
| 3814 |
query = gst_query_new_scheduling (); |
| 3815 |
if (!gst_pad_peer_query (sinkpad, query)) { |
| 3816 |
gst_query_unref (query); |
| 3817 |
goto baseparse_push; |
| 3818 |
} |
| 3819 |
|
| 3820 |
gst_query_parse_scheduling (query, &sched_flags, NULL, NULL, NULL); |
| 3821 |
|
| 3822 |
pull_mode = gst_query_has_scheduling_mode (query, GST_PAD_MODE_PULL) |
| 3823 |
&& ((sched_flags & GST_SCHEDULING_FLAG_SEEKABLE) != 0); |
| 3824 |
|
| 3825 |
gst_query_unref (query); |
| 3826 |
|
| 3827 |
if (!pull_mode) |
| 3828 |
goto baseparse_push; |
| 3829 |
|
| 3830 |
GST_DEBUG_OBJECT (parse, "trying to activate in pull mode"); |
| 3831 |
if (!gst_pad_activate_mode (sinkpad, GST_PAD_MODE_PULL, TRUE)) |
| 3832 |
goto baseparse_push; |
| 3833 |
|
| 3834 |
parse->priv->push_stream_start = TRUE; |
| 3835 |
/* In pull mode, upstream is BYTES */ |
| 3836 |
parse->priv->upstream_format = GST_FORMAT_BYTES; |
| 3837 |
|
| 3838 |
return gst_pad_start_task (sinkpad, (GstTaskFunction) gst_base_parse_loop, |
| 3839 |
sinkpad, NULL); |
| 3840 |
/* fallback */ |
| 3841 |
baseparse_push: |
| 3842 |
{ |
| 3843 |
GST_DEBUG_OBJECT (parse, "trying to activate in push mode"); |
| 3844 |
return gst_pad_activate_mode (sinkpad, GST_PAD_MODE_PUSH, TRUE); |
| 3845 |
} |
| 3846 |
} |
| 3847 |
|
| 3848 |
static gboolean |
| 3849 |
gst_base_parse_activate (GstBaseParse * parse, gboolean active) |
| 3850 |
{ |
| 3851 |
GstBaseParseClass *klass; |
| 3852 |
gboolean result = TRUE; |
| 3853 |
|
| 3854 |
GST_DEBUG_OBJECT (parse, "activate %d", active); |
| 3855 |
|
| 3856 |
klass = GST_BASE_PARSE_GET_CLASS (parse); |
| 3857 |
|
| 3858 |
if (active) { |
| 3859 |
if (parse->priv->pad_mode == GST_PAD_MODE_NONE && klass->start) |
| 3860 |
result = klass->start (parse); |
| 3861 |
|
| 3862 |
/* If the subclass implements ::detect we want to |
| 3863 |
* call it for the first buffers now */ |
| 3864 |
parse->priv->detecting = (klass->detect != NULL); |
| 3865 |
} else { |
| 3866 |
/* We must make sure streaming has finished before resetting things |
| 3867 |
* and calling the ::stop vfunc */ |
| 3868 |
GST_PAD_STREAM_LOCK (parse->sinkpad); |
| 3869 |
GST_PAD_STREAM_UNLOCK (parse->sinkpad); |
| 3870 |
|
| 3871 |
if (parse->priv->pad_mode != GST_PAD_MODE_NONE && klass->stop) |
| 3872 |
result = klass->stop (parse); |
| 3873 |
|
| 3874 |
parse->priv->pad_mode = GST_PAD_MODE_NONE; |
| 3875 |
parse->priv->upstream_format = GST_FORMAT_UNDEFINED; |
| 3876 |
} |
| 3877 |
GST_DEBUG_OBJECT (parse, "activate return: %d", result); |
| 3878 |
return result; |
| 3879 |
} |
| 3880 |
|
| 3881 |
static gboolean |
| 3882 |
gst_base_parse_sink_activate_mode (GstPad * pad, GstObject * parent, |
| 3883 |
GstPadMode mode, gboolean active) |
| 3884 |
{ |
| 3885 |
gboolean result; |
| 3886 |
GstBaseParse *parse; |
| 3887 |
|
| 3888 |
parse = GST_BASE_PARSE (parent); |
| 3889 |
|
| 3890 |
GST_DEBUG_OBJECT (parse, "sink %sactivate in %s mode", |
| 3891 |
(active) ? "" : "de", gst_pad_mode_get_name (mode)); |
| 3892 |
|
| 3893 |
if (!gst_base_parse_activate (parse, active)) |
| 3894 |
goto activate_failed; |
| 3895 |
|
| 3896 |
switch (mode) { |
| 3897 |
case GST_PAD_MODE_PULL: |
| 3898 |
if (active) { |
| 3899 |
GstEvent *ev = gst_event_new_segment (&parse->segment); |
| 3900 |
|
| 3901 |
if (parse->priv->segment_seqnum != GST_SEQNUM_INVALID) |
| 3902 |
gst_event_set_seqnum (ev, parse->priv->segment_seqnum); |
| 3903 |
else |
| 3904 |
parse->priv->segment_seqnum = gst_event_get_seqnum (ev); |
| 3905 |
|
| 3906 |
parse->priv->pending_events = |
| 3907 |
g_list_prepend (parse->priv->pending_events, ev); |
| 3908 |
result = TRUE; |
| 3909 |
} else { |
| 3910 |
result = gst_pad_stop_task (pad); |
| 3911 |
} |
| 3912 |
break; |
| 3913 |
default: |
| 3914 |
result = TRUE; |
| 3915 |
break; |
| 3916 |
} |
| 3917 |
if (result) |
| 3918 |
parse->priv->pad_mode = active ? mode : GST_PAD_MODE_NONE; |
| 3919 |
|
| 3920 |
GST_DEBUG_OBJECT (parse, "sink activate return: %d", result); |
| 3921 |
|
| 3922 |
return result; |
| 3923 |
|
| 3924 |
/* ERRORS */ |
| 3925 |
activate_failed: |
| 3926 |
{ |
| 3927 |
GST_DEBUG_OBJECT (parse, "activate failed"); |
| 3928 |
return FALSE; |
| 3929 |
} |
| 3930 |
} |
| 3931 |
|
| 3932 |
/** |
| 3933 |
* gst_base_parse_set_duration: |
| 3934 |
* @parse: #GstBaseParse. |
| 3935 |
* @fmt: #GstFormat. |
| 3936 |
* @duration: duration value. |
| 3937 |
* @interval: how often to update the duration estimate based on bitrate, or 0. |
| 3938 |
* |
| 3939 |
* Sets the duration of the currently playing media. Subclass can use this |
| 3940 |
* when it is able to determine duration and/or notices a change in the media |
| 3941 |
* duration. Alternatively, if @interval is non-zero (default), then stream |
| 3942 |
* duration is determined based on estimated bitrate, and updated every @interval |
| 3943 |
* frames. |
| 3944 |
*/ |
| 3945 |
void |
| 3946 |
gst_base_parse_set_duration (GstBaseParse * parse, |
| 3947 |
GstFormat fmt, gint64 duration, gint interval) |
| 3948 |
{ |
| 3949 |
gint64 old_duration; |
| 3950 |
|
| 3951 |
g_return_if_fail (parse != NULL); |
| 3952 |
|
| 3953 |
if (parse->priv->upstream_has_duration) { |
| 3954 |
GST_DEBUG_OBJECT (parse, "using upstream duration; discarding update"); |
| 3955 |
goto exit; |
| 3956 |
} |
| 3957 |
|
| 3958 |
old_duration = parse->priv->duration; |
| 3959 |
|
| 3960 |
parse->priv->duration = duration; |
| 3961 |
parse->priv->duration_fmt = fmt; |
| 3962 |
GST_DEBUG_OBJECT (parse, "set duration: %" G_GINT64_FORMAT, duration); |
| 3963 |
if (fmt == GST_FORMAT_TIME && GST_CLOCK_TIME_IS_VALID (duration)) { |
| 3964 |
if (interval != 0) { |
| 3965 |
GST_DEBUG_OBJECT (parse, "valid duration provided, disabling estimate"); |
| 3966 |
interval = 0; |
| 3967 |
} |
| 3968 |
} |
| 3969 |
GST_DEBUG_OBJECT (parse, "set update interval: %d", interval); |
| 3970 |
parse->priv->update_interval = interval; |
| 3971 |
if (duration != old_duration) { |
| 3972 |
GstMessage *m; |
| 3973 |
|
| 3974 |
m = gst_message_new_duration_changed (GST_OBJECT (parse)); |
| 3975 |
gst_element_post_message (GST_ELEMENT (parse), m); |
| 3976 |
|
| 3977 |
/* TODO: what about duration tag? */ |
| 3978 |
} |
| 3979 |
exit: |
| 3980 |
return; |
| 3981 |
} |
| 3982 |
|
| 3983 |
/** |
| 3984 |
* gst_base_parse_set_average_bitrate: |
| 3985 |
* @parse: #GstBaseParse. |
| 3986 |
* @bitrate: average bitrate in bits/second |
| 3987 |
* |
| 3988 |
* Optionally sets the average bitrate detected in media (if non-zero), |
| 3989 |
* e.g. based on metadata, as it will be posted to the application. |
| 3990 |
* |
| 3991 |
* By default, announced average bitrate is estimated. The average bitrate |
| 3992 |
* is used to estimate the total duration of the stream and to estimate |
| 3993 |
* a seek position, if there's no index and the format is syncable |
| 3994 |
* (see gst_base_parse_set_syncable()). |
| 3995 |
*/ |
| 3996 |
void |
| 3997 |
gst_base_parse_set_average_bitrate (GstBaseParse * parse, guint bitrate) |
| 3998 |
{ |
| 3999 |
parse->priv->bitrate = bitrate; |
| 4000 |
GST_DEBUG_OBJECT (parse, "bitrate %u", bitrate); |
| 4001 |
} |
| 4002 |
|
| 4003 |
/** |
| 4004 |
* gst_base_parse_set_min_frame_size: |
| 4005 |
* @parse: #GstBaseParse. |
| 4006 |
* @min_size: Minimum size in bytes of the data that this base class should |
| 4007 |
* give to subclass. |
| 4008 |
* |
| 4009 |
* Subclass can use this function to tell the base class that it needs to |
| 4010 |
* be given buffers of at least @min_size bytes. |
| 4011 |
*/ |
| 4012 |
void |
| 4013 |
gst_base_parse_set_min_frame_size (GstBaseParse * parse, guint min_size) |
| 4014 |
{ |
| 4015 |
g_return_if_fail (parse != NULL); |
| 4016 |
|
| 4017 |
parse->priv->min_frame_size = min_size; |
| 4018 |
GST_LOG_OBJECT (parse, "set frame_min_size: %d", min_size); |
| 4019 |
} |
| 4020 |
|
| 4021 |
/** |
| 4022 |
* gst_base_parse_set_frame_rate: |
| 4023 |
* @parse: the #GstBaseParse to set |
| 4024 |
* @fps_num: frames per second (numerator). |
| 4025 |
* @fps_den: frames per second (denominator). |
| 4026 |
* @lead_in: frames needed before a segment for subsequent decode |
| 4027 |
* @lead_out: frames needed after a segment |
| 4028 |
* |
| 4029 |
* If frames per second is configured, parser can take care of buffer duration |
| 4030 |
* and timestamping. When performing segment clipping, or seeking to a specific |
| 4031 |
* location, a corresponding decoder might need an initial @lead_in and a |
| 4032 |
* following @lead_out number of frames to ensure the desired segment is |
| 4033 |
* entirely filled upon decoding. |
| 4034 |
*/ |
| 4035 |
void |
| 4036 |
gst_base_parse_set_frame_rate (GstBaseParse * parse, guint fps_num, |
| 4037 |
guint fps_den, guint lead_in, guint lead_out) |
| 4038 |
{ |
| 4039 |
g_return_if_fail (parse != NULL); |
| 4040 |
|
| 4041 |
parse->priv->fps_num = fps_num; |
| 4042 |
parse->priv->fps_den = fps_den; |
| 4043 |
if (!fps_num || !fps_den) { |
| 4044 |
GST_DEBUG_OBJECT (parse, "invalid fps (%d/%d), ignoring parameters", |
| 4045 |
fps_num, fps_den); |
| 4046 |
fps_num = fps_den = 0; |
| 4047 |
parse->priv->frame_duration = GST_CLOCK_TIME_NONE; |
| 4048 |
parse->priv->lead_in = parse->priv->lead_out = 0; |
| 4049 |
parse->priv->lead_in_ts = parse->priv->lead_out_ts = 0; |
| 4050 |
} else { |
| 4051 |
parse->priv->frame_duration = |
| 4052 |
gst_util_uint64_scale (GST_SECOND, fps_den, fps_num); |
| 4053 |
parse->priv->lead_in = lead_in; |
| 4054 |
parse->priv->lead_out = lead_out; |
| 4055 |
parse->priv->lead_in_ts = |
| 4056 |
gst_util_uint64_scale (GST_SECOND, fps_den * lead_in, fps_num); |
| 4057 |
parse->priv->lead_out_ts = |
| 4058 |
gst_util_uint64_scale (GST_SECOND, fps_den * lead_out, fps_num); |
| 4059 |
/* aim for about 1.5s to estimate duration */ |
| 4060 |
if (parse->priv->update_interval < 0) { |
| 4061 |
guint64 interval = gst_util_uint64_scale (fps_num, 3, |
| 4062 |
G_GUINT64_CONSTANT (2) * fps_den); |
| 4063 |
|
| 4064 |
parse->priv->update_interval = MIN (interval, G_MAXINT); |
| 4065 |
|
| 4066 |
GST_LOG_OBJECT (parse, "estimated update interval to %d frames", |
| 4067 |
parse->priv->update_interval); |
| 4068 |
} |
| 4069 |
} |
| 4070 |
GST_LOG_OBJECT (parse, "set fps: %d/%d => duration: %" G_GINT64_FORMAT " ms", |
| 4071 |
fps_num, fps_den, parse->priv->frame_duration / GST_MSECOND); |
| 4072 |
GST_LOG_OBJECT (parse, "set lead in: %d frames = %" G_GUINT64_FORMAT " ms, " |
| 4073 |
"lead out: %d frames = %" G_GUINT64_FORMAT " ms", |
| 4074 |
lead_in, parse->priv->lead_in_ts / GST_MSECOND, |
| 4075 |
lead_out, parse->priv->lead_out_ts / GST_MSECOND); |
| 4076 |
} |
| 4077 |
|
| 4078 |
/** |
| 4079 |
* gst_base_parse_set_has_timing_info: |
| 4080 |
* @parse: a #GstBaseParse |
| 4081 |
* @has_timing: whether frames carry timing information |
| 4082 |
* |
| 4083 |
* Set if frames carry timing information which the subclass can (generally) |
| 4084 |
* parse and provide. In particular, intrinsic (rather than estimated) time |
| 4085 |
* can be obtained following a seek. |
| 4086 |
*/ |
| 4087 |
void |
| 4088 |
gst_base_parse_set_has_timing_info (GstBaseParse * parse, gboolean has_timing) |
| 4089 |
{ |
| 4090 |
parse->priv->has_timing_info = has_timing; |
| 4091 |
GST_INFO_OBJECT (parse, "has_timing: %s", (has_timing) ? "yes" : "no"); |
| 4092 |
} |
| 4093 |
|
| 4094 |
/** |
| 4095 |
* gst_base_parse_set_syncable: |
| 4096 |
* @parse: a #GstBaseParse |
| 4097 |
* @syncable: set if frame starts can be identified |
| 4098 |
* |
| 4099 |
* Set if frame starts can be identified. This is set by default and |
| 4100 |
* determines whether seeking based on bitrate averages |
| 4101 |
* is possible for a format/stream. |
| 4102 |
*/ |
| 4103 |
void |
| 4104 |
gst_base_parse_set_syncable (GstBaseParse * parse, gboolean syncable) |
| 4105 |
{ |
| 4106 |
parse->priv->syncable = syncable; |
| 4107 |
GST_INFO_OBJECT (parse, "syncable: %s", (syncable) ? "yes" : "no"); |
| 4108 |
} |
| 4109 |
|
| 4110 |
/** |
| 4111 |
* gst_base_parse_set_passthrough: |
| 4112 |
* @parse: a #GstBaseParse |
| 4113 |
* @passthrough: %TRUE if parser should run in passthrough mode |
| 4114 |
* |
| 4115 |
* Set if the nature of the format or configuration does not allow (much) |
| 4116 |
* parsing, and the parser should operate in passthrough mode (which only |
| 4117 |
* applies when operating in push mode). That is, incoming buffers are |
| 4118 |
* pushed through unmodified, i.e. no #GstBaseParseClass::handle_frame |
| 4119 |
* will be invoked, but #GstBaseParseClass::pre_push_frame will still be |
| 4120 |
* invoked, so subclass can perform as much or as little is appropriate for |
| 4121 |
* passthrough semantics in #GstBaseParseClass::pre_push_frame. |
| 4122 |
*/ |
| 4123 |
void |
| 4124 |
gst_base_parse_set_passthrough (GstBaseParse * parse, gboolean passthrough) |
| 4125 |
{ |
| 4126 |
parse->priv->passthrough = passthrough; |
| 4127 |
GST_INFO_OBJECT (parse, "passthrough: %s", (passthrough) ? "yes" : "no"); |
| 4128 |
} |
| 4129 |
|
| 4130 |
/** |
| 4131 |
* gst_base_parse_set_pts_interpolation: |
| 4132 |
* @parse: a #GstBaseParse |
| 4133 |
* @pts_interpolate: %TRUE if parser should interpolate PTS timestamps |
| 4134 |
* |
| 4135 |
* By default, the base class will guess PTS timestamps using a simple |
| 4136 |
* interpolation (previous timestamp + duration), which is incorrect for |
| 4137 |
* data streams with reordering, where PTS can go backward. Sub-classes |
| 4138 |
* implementing such formats should disable PTS interpolation. |
| 4139 |
*/ |
| 4140 |
void |
| 4141 |
gst_base_parse_set_pts_interpolation (GstBaseParse * parse, |
| 4142 |
gboolean pts_interpolate) |
| 4143 |
{ |
| 4144 |
parse->priv->pts_interpolate = pts_interpolate; |
| 4145 |
GST_INFO_OBJECT (parse, "PTS interpolation: %s", |
| 4146 |
(pts_interpolate) ? "yes" : "no"); |
| 4147 |
} |
| 4148 |
|
| 4149 |
/** |
| 4150 |
* gst_base_parse_set_infer_ts: |
| 4151 |
* @parse: a #GstBaseParse |
| 4152 |
* @infer_ts: %TRUE if parser should infer DTS/PTS from each other |
| 4153 |
* |
| 4154 |
* By default, the base class might try to infer PTS from DTS and vice |
| 4155 |
* versa. While this is generally correct for audio data, it may not |
| 4156 |
* be otherwise. Sub-classes implementing such formats should disable |
| 4157 |
* timestamp inferring. |
| 4158 |
*/ |
| 4159 |
void |
| 4160 |
gst_base_parse_set_infer_ts (GstBaseParse * parse, gboolean infer_ts) |
| 4161 |
{ |
| 4162 |
parse->priv->infer_ts = infer_ts; |
| 4163 |
GST_INFO_OBJECT (parse, "TS inferring: %s", (infer_ts) ? "yes" : "no"); |
| 4164 |
} |
| 4165 |
|
| 4166 |
/** |
| 4167 |
* gst_base_parse_set_latency: |
| 4168 |
* @parse: a #GstBaseParse |
| 4169 |
* @min_latency: minimum parse latency |
| 4170 |
* @max_latency: maximum parse latency |
| 4171 |
* |
| 4172 |
* Sets the minimum and maximum (which may likely be equal) latency introduced |
| 4173 |
* by the parsing process. If there is such a latency, which depends on the |
| 4174 |
* particular parsing of the format, it typically corresponds to 1 frame duration. |
| 4175 |
* |
| 4176 |
* If the provided values changed from previously provided ones, this will |
| 4177 |
* also post a LATENCY message on the bus so the pipeline can reconfigure its |
| 4178 |
* global latency. |
| 4179 |
*/ |
| 4180 |
void |
| 4181 |
gst_base_parse_set_latency (GstBaseParse * parse, GstClockTime min_latency, |
| 4182 |
GstClockTime max_latency) |
| 4183 |
{ |
| 4184 |
gboolean post_message = FALSE; |
| 4185 |
|
| 4186 |
g_return_if_fail (GST_CLOCK_TIME_IS_VALID (min_latency)); |
| 4187 |
g_return_if_fail (min_latency <= max_latency); |
| 4188 |
|
| 4189 |
GST_INFO_OBJECT (parse, "min/max latency %" GST_TIME_FORMAT ", %" |
| 4190 |
GST_TIME_FORMAT, GST_TIME_ARGS (min_latency), |
| 4191 |
GST_TIME_ARGS (max_latency)); |
| 4192 |
|
| 4193 |
GST_OBJECT_LOCK (parse); |
| 4194 |
if (parse->priv->min_latency != min_latency) { |
| 4195 |
parse->priv->min_latency = min_latency; |
| 4196 |
post_message = TRUE; |
| 4197 |
} |
| 4198 |
if (parse->priv->max_latency != max_latency) { |
| 4199 |
parse->priv->max_latency = max_latency; |
| 4200 |
post_message = TRUE; |
| 4201 |
} |
| 4202 |
if (!parse->priv->posted_latency_msg) { |
| 4203 |
parse->priv->posted_latency_msg = TRUE; |
| 4204 |
post_message = TRUE; |
| 4205 |
} |
| 4206 |
GST_OBJECT_UNLOCK (parse); |
| 4207 |
|
| 4208 |
if (post_message) |
| 4209 |
gst_element_post_message (GST_ELEMENT_CAST (parse), |
| 4210 |
gst_message_new_latency (GST_OBJECT_CAST (parse))); |
| 4211 |
} |
| 4212 |
|
| 4213 |
static gboolean |
| 4214 |
gst_base_parse_get_duration (GstBaseParse * parse, GstFormat format, |
| 4215 |
GstClockTime * duration) |
| 4216 |
{ |
| 4217 |
gboolean res = FALSE; |
| 4218 |
|
| 4219 |
g_return_val_if_fail (duration != NULL, FALSE); |
| 4220 |
|
| 4221 |
*duration = GST_CLOCK_TIME_NONE; |
| 4222 |
if (parse->priv->duration != -1 && format == parse->priv->duration_fmt) { |
| 4223 |
GST_LOG_OBJECT (parse, "using provided duration"); |
| 4224 |
*duration = parse->priv->duration; |
| 4225 |
res = TRUE; |
| 4226 |
} else if (parse->priv->duration != -1) { |
| 4227 |
GST_LOG_OBJECT (parse, "converting provided duration"); |
| 4228 |
res = gst_base_parse_convert (parse, parse->priv->duration_fmt, |
| 4229 |
parse->priv->duration, format, (gint64 *) duration); |
| 4230 |
} else if (format == GST_FORMAT_TIME && parse->priv->estimated_duration != -1) { |
| 4231 |
GST_LOG_OBJECT (parse, "using estimated duration"); |
| 4232 |
*duration = parse->priv->estimated_duration; |
| 4233 |
res = TRUE; |
| 4234 |
} else { |
| 4235 |
GST_LOG_OBJECT (parse, "cannot estimate duration"); |
| 4236 |
} |
| 4237 |
|
| 4238 |
GST_LOG_OBJECT (parse, "res: %d, duration %" GST_TIME_FORMAT, res, |
| 4239 |
GST_TIME_ARGS (*duration)); |
| 4240 |
return res; |
| 4241 |
} |
| 4242 |
|
| 4243 |
static gboolean |
| 4244 |
gst_base_parse_src_query_default (GstBaseParse * parse, GstQuery * query) |
| 4245 |
{ |
| 4246 |
gboolean res = FALSE; |
| 4247 |
GstPad *pad; |
| 4248 |
|
| 4249 |
pad = GST_BASE_PARSE_SRC_PAD (parse); |
| 4250 |
|
| 4251 |
switch (GST_QUERY_TYPE (query)) { |
| 4252 |
case GST_QUERY_POSITION: |
| 4253 |
{ |
| 4254 |
gint64 dest_value; |
| 4255 |
GstFormat format; |
| 4256 |
|
| 4257 |
GST_DEBUG_OBJECT (parse, "position query"); |
| 4258 |
gst_query_parse_position (query, &format, NULL); |
| 4259 |
|
| 4260 |
/* try upstream first */ |
| 4261 |
res = gst_pad_query_default (pad, GST_OBJECT_CAST (parse), query); |
| 4262 |
if (!res) { |
| 4263 |
/* Fall back on interpreting segment */ |
| 4264 |
GST_OBJECT_LOCK (parse); |
| 4265 |
/* Only reply BYTES if upstream is in BYTES already, otherwise |
| 4266 |
* we're not in charge */ |
| 4267 |
if (format == GST_FORMAT_BYTES |
| 4268 |
&& parse->priv->upstream_format == GST_FORMAT_BYTES) { |
| 4269 |
dest_value = parse->priv->offset; |
| 4270 |
res = TRUE; |
| 4271 |
} else if (format == parse->segment.format && |
| 4272 |
GST_CLOCK_TIME_IS_VALID (parse->segment.position)) { |
| 4273 |
dest_value = gst_segment_to_stream_time (&parse->segment, |
| 4274 |
parse->segment.format, parse->segment.position); |
| 4275 |
res = TRUE; |
| 4276 |
} |
| 4277 |
GST_OBJECT_UNLOCK (parse); |
| 4278 |
if (!res && parse->priv->upstream_format == GST_FORMAT_BYTES) { |
| 4279 |
/* no precise result, upstream no idea either, then best estimate */ |
| 4280 |
/* priv->offset is updated in both PUSH/PULL modes, *iff* we're |
| 4281 |
* in charge of things */ |
| 4282 |
res = gst_base_parse_convert (parse, |
| 4283 |
GST_FORMAT_BYTES, parse->priv->offset, format, &dest_value); |
| 4284 |
} |
| 4285 |
if (res) |
| 4286 |
gst_query_set_position (query, format, dest_value); |
| 4287 |
} |
| 4288 |
break; |
| 4289 |
} |
| 4290 |
case GST_QUERY_DURATION: |
| 4291 |
{ |
| 4292 |
GstFormat format; |
| 4293 |
GstClockTime duration; |
| 4294 |
|
| 4295 |
GST_DEBUG_OBJECT (parse, "duration query"); |
| 4296 |
gst_query_parse_duration (query, &format, NULL); |
| 4297 |
|
| 4298 |
/* consult upstream */ |
| 4299 |
res = gst_pad_query_default (pad, GST_OBJECT_CAST (parse), query); |
| 4300 |
|
| 4301 |
/* otherwise best estimate from us */ |
| 4302 |
if (!res) { |
| 4303 |
res = gst_base_parse_get_duration (parse, format, &duration); |
| 4304 |
if (res) |
| 4305 |
gst_query_set_duration (query, format, duration); |
| 4306 |
} |
| 4307 |
break; |
| 4308 |
} |
| 4309 |
case GST_QUERY_SEEKING: |
| 4310 |
{ |
| 4311 |
GstFormat fmt; |
| 4312 |
GstClockTime duration = GST_CLOCK_TIME_NONE; |
| 4313 |
gboolean seekable = FALSE; |
| 4314 |
|
| 4315 |
GST_DEBUG_OBJECT (parse, "seeking query"); |
| 4316 |
gst_query_parse_seeking (query, &fmt, NULL, NULL, NULL); |
| 4317 |
|
| 4318 |
/* consult upstream */ |
| 4319 |
res = gst_pad_query_default (pad, GST_OBJECT_CAST (parse), query); |
| 4320 |
|
| 4321 |
/* we may be able to help if in TIME */ |
| 4322 |
if (fmt == GST_FORMAT_TIME && gst_base_parse_is_seekable (parse)) { |
| 4323 |
gst_query_parse_seeking (query, &fmt, &seekable, NULL, NULL); |
| 4324 |
/* already OK if upstream takes care */ |
| 4325 |
GST_LOG_OBJECT (parse, "upstream handled %d, seekable %d", |
| 4326 |
res, seekable); |
| 4327 |
if (!(res && seekable)) { |
| 4328 |
if (!gst_base_parse_get_duration (parse, GST_FORMAT_TIME, &duration) |
| 4329 |
|| duration == -1) { |
| 4330 |
/* seekable if we still have a chance to get duration later on */ |
| 4331 |
seekable = parse->priv->upstream_seekable && |
| 4332 |
(parse->priv->update_interval > 0); |
| 4333 |
} else { |
| 4334 |
seekable = parse->priv->upstream_seekable; |
| 4335 |
GST_LOG_OBJECT (parse, "already determine upstream seekabled: %d", |
| 4336 |
seekable); |
| 4337 |
} |
| 4338 |
gst_query_set_seeking (query, GST_FORMAT_TIME, seekable, 0, duration); |
| 4339 |
res = TRUE; |
| 4340 |
} |
| 4341 |
} |
| 4342 |
break; |
| 4343 |
} |
| 4344 |
case GST_QUERY_FORMATS: |
| 4345 |
gst_query_set_formatsv (query, 3, fmtlist); |
| 4346 |
res = TRUE; |
| 4347 |
break; |
| 4348 |
case GST_QUERY_CONVERT: |
| 4349 |
{ |
| 4350 |
GstFormat src_format, dest_format; |
| 4351 |
gint64 src_value, dest_value; |
| 4352 |
|
| 4353 |
gst_query_parse_convert (query, &src_format, &src_value, |
| 4354 |
&dest_format, &dest_value); |
| 4355 |
|
| 4356 |
res = gst_base_parse_convert (parse, src_format, src_value, |
| 4357 |
dest_format, &dest_value); |
| 4358 |
if (res) { |
| 4359 |
gst_query_set_convert (query, src_format, src_value, |
| 4360 |
dest_format, dest_value); |
| 4361 |
} |
| 4362 |
break; |
| 4363 |
} |
| 4364 |
case GST_QUERY_LATENCY: |
| 4365 |
{ |
| 4366 |
if ((res = gst_pad_peer_query (parse->sinkpad, query))) { |
| 4367 |
gboolean live; |
| 4368 |
GstClockTime min_latency, max_latency; |
| 4369 |
|
| 4370 |
gst_query_parse_latency (query, &live, &min_latency, &max_latency); |
| 4371 |
GST_DEBUG_OBJECT (parse, "Peer latency: live %d, min %" |
| 4372 |
GST_TIME_FORMAT " max %" GST_TIME_FORMAT, live, |
| 4373 |
GST_TIME_ARGS (min_latency), GST_TIME_ARGS (max_latency)); |
| 4374 |
|
| 4375 |
GST_OBJECT_LOCK (parse); |
| 4376 |
/* add our latency */ |
| 4377 |
min_latency += parse->priv->min_latency; |
| 4378 |
if (max_latency == -1 || parse->priv->max_latency == -1) |
| 4379 |
max_latency = -1; |
| 4380 |
else |
| 4381 |
max_latency += parse->priv->max_latency; |
| 4382 |
GST_OBJECT_UNLOCK (parse); |
| 4383 |
|
| 4384 |
gst_query_set_latency (query, live, min_latency, max_latency); |
| 4385 |
} |
| 4386 |
break; |
| 4387 |
} |
| 4388 |
case GST_QUERY_SEGMENT: |
| 4389 |
{ |
| 4390 |
GstFormat format; |
| 4391 |
gint64 start, stop; |
| 4392 |
|
| 4393 |
format = parse->segment.format; |
| 4394 |
|
| 4395 |
start = |
| 4396 |
gst_segment_to_stream_time (&parse->segment, format, |
| 4397 |
parse->segment.start); |
| 4398 |
if ((stop = parse->segment.stop) == -1) |
| 4399 |
stop = parse->segment.duration; |
| 4400 |
else |
| 4401 |
stop = gst_segment_to_stream_time (&parse->segment, format, stop); |
| 4402 |
|
| 4403 |
gst_query_set_segment (query, parse->segment.rate, format, start, stop); |
| 4404 |
res = TRUE; |
| 4405 |
break; |
| 4406 |
} |
| 4407 |
default: |
| 4408 |
res = gst_pad_query_default (pad, GST_OBJECT_CAST (parse), query); |
| 4409 |
break; |
| 4410 |
} |
| 4411 |
return res; |
| 4412 |
} |
| 4413 |
|
| 4414 |
/* scans for a cluster start from @pos, |
| 4415 |
* return GST_FLOW_OK and frame position/time in @pos/@time if found */ |
| 4416 |
static GstFlowReturn |
| 4417 |
gst_base_parse_find_frame (GstBaseParse * parse, gint64 * pos, |
| 4418 |
GstClockTime * time, GstClockTime * duration) |
| 4419 |
{ |
| 4420 |
GstBaseParseClass *klass; |
| 4421 |
gint64 orig_offset; |
| 4422 |
gboolean orig_drain, orig_discont; |
| 4423 |
GstFlowReturn ret = GST_FLOW_OK; |
| 4424 |
GstBuffer *buf = NULL; |
| 4425 |
GstBaseParseFrame *sframe = NULL; |
| 4426 |
|
| 4427 |
g_return_val_if_fail (pos != NULL, GST_FLOW_ERROR); |
| 4428 |
g_return_val_if_fail (time != NULL, GST_FLOW_ERROR); |
| 4429 |
g_return_val_if_fail (duration != NULL, GST_FLOW_ERROR); |
| 4430 |
|
| 4431 |
klass = GST_BASE_PARSE_GET_CLASS (parse); |
| 4432 |
|
| 4433 |
*time = GST_CLOCK_TIME_NONE; |
| 4434 |
*duration = GST_CLOCK_TIME_NONE; |
| 4435 |
|
| 4436 |
/* save state */ |
| 4437 |
orig_offset = parse->priv->offset; |
| 4438 |
orig_discont = parse->priv->discont; |
| 4439 |
orig_drain = parse->priv->drain; |
| 4440 |
|
| 4441 |
GST_DEBUG_OBJECT (parse, "scanning for frame starting at %" G_GINT64_FORMAT |
| 4442 |
" (%#" G_GINT64_MODIFIER "x)", *pos, *pos); |
| 4443 |
|
| 4444 |
/* jump elsewhere and locate next frame */ |
| 4445 |
parse->priv->offset = *pos; |
| 4446 |
/* mark as scanning so frames don't get processed all the way */ |
| 4447 |
parse->priv->scanning = TRUE; |
| 4448 |
ret = gst_base_parse_scan_frame (parse, klass); |
| 4449 |
parse->priv->scanning = FALSE; |
| 4450 |
/* retrieve frame found during scan */ |
| 4451 |
sframe = parse->priv->scanned_frame; |
| 4452 |
parse->priv->scanned_frame = NULL; |
| 4453 |
|
| 4454 |
if (ret != GST_FLOW_OK || !sframe) |
| 4455 |
goto done; |
| 4456 |
|
| 4457 |
/* get offset first, subclass parsing might dump other stuff in there */ |
| 4458 |
*pos = sframe->offset; |
| 4459 |
buf = sframe->buffer; |
| 4460 |
g_assert (buf); |
| 4461 |
|
| 4462 |
/* but it should provide proper time */ |
| 4463 |
*time = GST_BUFFER_TIMESTAMP (buf); |
| 4464 |
*duration = GST_BUFFER_DURATION (buf); |
| 4465 |
|
| 4466 |
GST_LOG_OBJECT (parse, |
| 4467 |
"frame with time %" GST_TIME_FORMAT " at offset %" G_GINT64_FORMAT, |
| 4468 |
GST_TIME_ARGS (*time), *pos); |
| 4469 |
|
| 4470 |
done: |
| 4471 |
if (sframe) |
| 4472 |
gst_base_parse_frame_free (sframe); |
| 4473 |
|
| 4474 |
/* restore state */ |
| 4475 |
parse->priv->offset = orig_offset; |
| 4476 |
parse->priv->discont = orig_discont; |
| 4477 |
parse->priv->drain = orig_drain; |
| 4478 |
|
| 4479 |
return ret; |
| 4480 |
} |
| 4481 |
|
| 4482 |
/* bisect and scan through file for frame starting before @time, |
| 4483 |
* returns OK and @time/@offset if found, NONE and/or error otherwise |
| 4484 |
* If @time == G_MAXINT64, scan for duration ( == last frame) */ |
| 4485 |
static GstFlowReturn |
| 4486 |
gst_base_parse_locate_time (GstBaseParse * parse, GstClockTime * _time, |
| 4487 |
gint64 * _offset) |
| 4488 |
{ |
| 4489 |
GstFlowReturn ret = GST_FLOW_OK; |
| 4490 |
gint64 lpos, hpos, newpos; |
| 4491 |
GstClockTime time, ltime, htime, newtime, dur; |
| 4492 |
gboolean cont = TRUE; |
| 4493 |
const GstClockTime tolerance = TARGET_DIFFERENCE; |
| 4494 |
const guint chunk = 4 * 1024; |
| 4495 |
|
| 4496 |
g_return_val_if_fail (_time != NULL, GST_FLOW_ERROR); |
| 4497 |
g_return_val_if_fail (_offset != NULL, GST_FLOW_ERROR); |
| 4498 |
|
| 4499 |
GST_DEBUG_OBJECT (parse, "Bisecting for time %" GST_TIME_FORMAT, |
| 4500 |
GST_TIME_ARGS (*_time)); |
| 4501 |
|
| 4502 |
/* TODO also make keyframe aware if useful some day */ |
| 4503 |
|
| 4504 |
time = *_time; |
| 4505 |
|
| 4506 |
/* basic cases */ |
| 4507 |
if (time == 0) { |
| 4508 |
*_offset = 0; |
| 4509 |
return GST_FLOW_OK; |
| 4510 |
} |
| 4511 |
|
| 4512 |
if (time == -1) { |
| 4513 |
*_offset = -1; |
| 4514 |
return GST_FLOW_OK; |
| 4515 |
} |
| 4516 |
|
| 4517 |
/* do not know at first */ |
| 4518 |
*_offset = -1; |
| 4519 |
*_time = GST_CLOCK_TIME_NONE; |
| 4520 |
|
| 4521 |
/* need initial positions; start and end */ |
| 4522 |
lpos = parse->priv->first_frame_offset; |
| 4523 |
ltime = parse->priv->first_frame_pts; |
| 4524 |
/* try other one if no luck */ |
| 4525 |
if (!GST_CLOCK_TIME_IS_VALID (ltime)) |
| 4526 |
ltime = parse->priv->first_frame_dts; |
| 4527 |
if (!gst_base_parse_get_duration (parse, GST_FORMAT_TIME, &htime)) { |
| 4528 |
GST_DEBUG_OBJECT (parse, "Unknown time duration, cannot bisect"); |
| 4529 |
return GST_FLOW_ERROR; |
| 4530 |
} |
| 4531 |
hpos = parse->priv->upstream_size; |
| 4532 |
|
| 4533 |
GST_DEBUG_OBJECT (parse, |
| 4534 |
"Bisection initial bounds: bytes %" G_GINT64_FORMAT " %" G_GINT64_FORMAT |
| 4535 |
", times %" GST_TIME_FORMAT " %" GST_TIME_FORMAT, lpos, hpos, |
| 4536 |
GST_TIME_ARGS (ltime), GST_TIME_ARGS (htime)); |
| 4537 |
|
| 4538 |
/* check preconditions are satisfied; |
| 4539 |
* start and end are needed, except for special case where we scan for |
| 4540 |
* last frame to determine duration */ |
| 4541 |
if (parse->priv->pad_mode != GST_PAD_MODE_PULL || !hpos || |
| 4542 |
!GST_CLOCK_TIME_IS_VALID (ltime) || |
| 4543 |
(!GST_CLOCK_TIME_IS_VALID (htime) && time != G_MAXINT64)) { |
| 4544 |
return GST_FLOW_OK; |
| 4545 |
} |
| 4546 |
|
| 4547 |
/* shortcut cases */ |
| 4548 |
if (time < ltime) { |
| 4549 |
goto exit; |
| 4550 |
} else if (time < ltime + tolerance) { |
| 4551 |
*_offset = lpos; |
| 4552 |
*_time = ltime; |
| 4553 |
goto exit; |
| 4554 |
} else if (time >= htime) { |
| 4555 |
*_offset = hpos; |
| 4556 |
*_time = htime; |
| 4557 |
goto exit; |
| 4558 |
} |
| 4559 |
|
| 4560 |
while (htime > ltime && cont) { |
| 4561 |
GST_LOG_OBJECT (parse, |
| 4562 |
"lpos: %" G_GUINT64_FORMAT ", ltime: %" GST_TIME_FORMAT, lpos, |
| 4563 |
GST_TIME_ARGS (ltime)); |
| 4564 |
GST_LOG_OBJECT (parse, |
| 4565 |
"hpos: %" G_GUINT64_FORMAT ", htime: %" GST_TIME_FORMAT, hpos, |
| 4566 |
GST_TIME_ARGS (htime)); |
| 4567 |
if (G_UNLIKELY (time == G_MAXINT64)) { |
| 4568 |
newpos = hpos; |
| 4569 |
} else if (G_LIKELY (hpos > lpos)) { |
| 4570 |
newpos = |
| 4571 |
gst_util_uint64_scale (hpos - lpos, time - ltime, htime - ltime) + |
| 4572 |
lpos - chunk; |
| 4573 |
} else { |
| 4574 |
/* should mean lpos == hpos, since lpos <= hpos is invariant */ |
| 4575 |
newpos = lpos; |
| 4576 |
/* we check this case once, but not forever, so break loop */ |
| 4577 |
cont = FALSE; |
| 4578 |
} |
| 4579 |
|
| 4580 |
/* ensure */ |
| 4581 |
newpos = CLAMP (newpos, lpos, hpos); |
| 4582 |
GST_LOG_OBJECT (parse, |
| 4583 |
"estimated _offset for %" GST_TIME_FORMAT ": %" G_GINT64_FORMAT, |
| 4584 |
GST_TIME_ARGS (time), newpos); |
| 4585 |
|
| 4586 |
ret = gst_base_parse_find_frame (parse, &newpos, &newtime, &dur); |
| 4587 |
if (ret == GST_FLOW_EOS) { |
| 4588 |
/* heuristic HACK */ |
| 4589 |
hpos = MAX (lpos, hpos - chunk); |
| 4590 |
continue; |
| 4591 |
} else if (ret != GST_FLOW_OK) { |
| 4592 |
goto exit; |
| 4593 |
} |
| 4594 |
|
| 4595 |
if (newtime == -1 || newpos == -1) { |
| 4596 |
GST_DEBUG_OBJECT (parse, "subclass did not provide metadata; aborting"); |
| 4597 |
break; |
| 4598 |
} |
| 4599 |
|
| 4600 |
if (G_UNLIKELY (time == G_MAXINT64)) { |
| 4601 |
*_offset = newpos; |
| 4602 |
*_time = newtime; |
| 4603 |
if (GST_CLOCK_TIME_IS_VALID (dur)) |
| 4604 |
*_time += dur; |
| 4605 |
break; |
| 4606 |
} else if (newtime > time) { |
| 4607 |
/* overshoot */ |
| 4608 |
hpos = (newpos >= hpos) ? MAX (lpos, hpos - chunk) : MAX (lpos, newpos); |
| 4609 |
htime = newtime; |
| 4610 |
} else if (newtime + tolerance > time) { |
| 4611 |
/* close enough undershoot */ |
| 4612 |
*_offset = newpos; |
| 4613 |
*_time = newtime; |
| 4614 |
break; |
| 4615 |
} else if (newtime < ltime) { |
| 4616 |
/* so a position beyond lpos resulted in earlier time than ltime ... */ |
| 4617 |
GST_DEBUG_OBJECT (parse, "non-ascending time; aborting"); |
| 4618 |
break; |
| 4619 |
} else { |
| 4620 |
/* undershoot too far */ |
| 4621 |
newpos += newpos == lpos ? chunk : 0; |
| 4622 |
lpos = CLAMP (newpos, lpos, hpos); |
| 4623 |
ltime = newtime; |
| 4624 |
} |
| 4625 |
} |
| 4626 |
|
| 4627 |
exit: |
| 4628 |
GST_LOG_OBJECT (parse, "return offset %" G_GINT64_FORMAT ", time %" |
| 4629 |
GST_TIME_FORMAT, *_offset, GST_TIME_ARGS (*_time)); |
| 4630 |
return ret; |
| 4631 |
} |
| 4632 |
|
| 4633 |
static gint64 |
| 4634 |
gst_base_parse_find_offset (GstBaseParse * parse, GstClockTime time, |
| 4635 |
gboolean before, GstClockTime * _ts) |
| 4636 |
{ |
| 4637 |
gint64 bytes = 0, ts = 0; |
| 4638 |
GstIndexEntry *entry = NULL; |
| 4639 |
|
| 4640 |
if (time == GST_CLOCK_TIME_NONE) { |
| 4641 |
ts = time; |
| 4642 |
bytes = -1; |
| 4643 |
goto exit; |
| 4644 |
} |
| 4645 |
|
| 4646 |
GST_BASE_PARSE_INDEX_LOCK (parse); |
| 4647 |
if (parse->priv->index) { |
| 4648 |
/* Let's check if we have an index entry for that time */ |
| 4649 |
entry = gst_index_get_assoc_entry (parse->priv->index, |
| 4650 |
parse->priv->index_id, |
| 4651 |
before ? GST_INDEX_LOOKUP_BEFORE : GST_INDEX_LOOKUP_AFTER, |
| 4652 |
GST_INDEX_ASSOCIATION_FLAG_KEY_UNIT, GST_FORMAT_TIME, time); |
| 4653 |
} |
| 4654 |
|
| 4655 |
if (entry) { |
| 4656 |
gst_index_entry_assoc_map (entry, GST_FORMAT_BYTES, &bytes); |
| 4657 |
gst_index_entry_assoc_map (entry, GST_FORMAT_TIME, &ts); |
| 4658 |
|
| 4659 |
GST_DEBUG_OBJECT (parse, "found index entry for %" GST_TIME_FORMAT |
| 4660 |
" at %" GST_TIME_FORMAT ", offset %" G_GINT64_FORMAT, |
| 4661 |
GST_TIME_ARGS (time), GST_TIME_ARGS (ts), bytes); |
| 4662 |
} else { |
| 4663 |
GST_DEBUG_OBJECT (parse, "no index entry found for %" GST_TIME_FORMAT, |
| 4664 |
GST_TIME_ARGS (time)); |
| 4665 |
if (!before) { |
| 4666 |
bytes = -1; |
| 4667 |
ts = GST_CLOCK_TIME_NONE; |
| 4668 |
} |
| 4669 |
} |
| 4670 |
GST_BASE_PARSE_INDEX_UNLOCK (parse); |
| 4671 |
|
| 4672 |
exit: |
| 4673 |
if (_ts) |
| 4674 |
*_ts = ts; |
| 4675 |
|
| 4676 |
return bytes; |
| 4677 |
} |
| 4678 |
|
| 4679 |
/* returns TRUE if seek succeeded */ |
| 4680 |
static gboolean |
| 4681 |
gst_base_parse_handle_seek (GstBaseParse * parse, GstEvent * event) |
| 4682 |
{ |
| 4683 |
gdouble rate; |
| 4684 |
GstFormat format; |
| 4685 |
GstSeekFlags flags; |
| 4686 |
GstSeekType start_type = GST_SEEK_TYPE_NONE, stop_type; |
| 4687 |
gboolean flush, update, res = TRUE, accurate; |
| 4688 |
gint64 start, stop, seekpos, seekstop; |
| 4689 |
GstSegment seeksegment = { 0, }; |
| 4690 |
GstClockTime start_ts; |
| 4691 |
guint32 seqnum; |
| 4692 |
GstEvent *segment_event; |
| 4693 |
|
| 4694 |
/* try upstream first, unless we're driving the streaming thread ourselves */ |
| 4695 |
if (parse->priv->pad_mode != GST_PAD_MODE_PULL) { |
| 4696 |
res = gst_pad_push_event (parse->sinkpad, gst_event_ref (event)); |
| 4697 |
if (res) |
| 4698 |
goto done; |
| 4699 |
} |
| 4700 |
|
| 4701 |
gst_event_parse_seek (event, &rate, &format, &flags, |
| 4702 |
&start_type, &start, &stop_type, &stop); |
| 4703 |
seqnum = gst_event_get_seqnum (event); |
| 4704 |
parse->priv->segment_seqnum = seqnum; |
| 4705 |
|
| 4706 |
GST_DEBUG_OBJECT (parse, "seek to format %s, rate %f, " |
| 4707 |
"start type %d at %" GST_TIME_FORMAT ", end type %d at %" |
| 4708 |
GST_TIME_FORMAT, gst_format_get_name (format), rate, |
| 4709 |
start_type, GST_TIME_ARGS (start), stop_type, GST_TIME_ARGS (stop)); |
| 4710 |
|
| 4711 |
/* we can only handle TIME, so check if subclass can convert |
| 4712 |
* to TIME format if it's some other format (such as DEFAULT) */ |
| 4713 |
if (format != GST_FORMAT_TIME) { |
| 4714 |
if (!gst_base_parse_convert (parse, format, start, GST_FORMAT_TIME, &start) |
| 4715 |
|| !gst_base_parse_convert (parse, format, stop, GST_FORMAT_TIME, |
| 4716 |
&stop)) |
| 4717 |
goto no_convert_to_time; |
| 4718 |
|
| 4719 |
GST_INFO_OBJECT (parse, "converted %s format to start time " |
| 4720 |
"%" GST_TIME_FORMAT " and stop time %" GST_TIME_FORMAT, |
| 4721 |
gst_format_get_name (format), GST_TIME_ARGS (start), |
| 4722 |
GST_TIME_ARGS (stop)); |
| 4723 |
|
| 4724 |
format = GST_FORMAT_TIME; |
| 4725 |
} |
| 4726 |
|
| 4727 |
/* no negative rates in push mode (unless upstream takes care of that, but |
| 4728 |
* we've already tried upstream and it didn't handle the seek request) */ |
| 4729 |
if (rate < 0.0 && parse->priv->pad_mode == GST_PAD_MODE_PUSH) |
| 4730 |
goto negative_rate; |
| 4731 |
|
| 4732 |
if (start_type != GST_SEEK_TYPE_SET || |
| 4733 |
(stop_type != GST_SEEK_TYPE_SET && stop_type != GST_SEEK_TYPE_NONE)) |
| 4734 |
goto wrong_type; |
| 4735 |
|
| 4736 |
/* get flush flag */ |
| 4737 |
flush = flags & GST_SEEK_FLAG_FLUSH; |
| 4738 |
|
| 4739 |
/* copy segment, we need this because we still need the old |
| 4740 |
* segment when we close the current segment. */ |
| 4741 |
gst_segment_copy_into (&parse->segment, &seeksegment); |
| 4742 |
|
| 4743 |
GST_DEBUG_OBJECT (parse, "configuring seek"); |
| 4744 |
gst_segment_do_seek (&seeksegment, rate, format, flags, |
| 4745 |
start_type, start, stop_type, stop, &update); |
| 4746 |
|
| 4747 |
/* accurate seeking implies seek tables are used to obtain position, |
| 4748 |
* and the requested segment is maintained exactly, not adjusted any way */ |
| 4749 |
accurate = flags & GST_SEEK_FLAG_ACCURATE; |
| 4750 |
|
| 4751 |
/* maybe we can be accurate for (almost) free */ |
| 4752 |
gst_base_parse_find_offset (parse, seeksegment.position, TRUE, &start_ts); |
| 4753 |
if (seeksegment.position <= start_ts + TARGET_DIFFERENCE) { |
| 4754 |
GST_DEBUG_OBJECT (parse, "accurate seek possible"); |
| 4755 |
accurate = TRUE; |
| 4756 |
} |
| 4757 |
|
| 4758 |
if (accurate) { |
| 4759 |
GstClockTime startpos; |
| 4760 |
if (rate >= 0) |
| 4761 |
startpos = seeksegment.position; |
| 4762 |
else |
| 4763 |
startpos = start; |
| 4764 |
|
| 4765 |
/* accurate requested, so ... seek a bit before target */ |
| 4766 |
if (startpos < parse->priv->lead_in_ts) |
| 4767 |
startpos = 0; |
| 4768 |
else |
| 4769 |
startpos -= parse->priv->lead_in_ts; |
| 4770 |
|
| 4771 |
if (seeksegment.stop == -1 && seeksegment.duration != -1) |
| 4772 |
seeksegment.stop = seeksegment.start + seeksegment.duration; |
| 4773 |
|
| 4774 |
seekpos = gst_base_parse_find_offset (parse, startpos, TRUE, &start_ts); |
| 4775 |
seekstop = gst_base_parse_find_offset (parse, seeksegment.stop, FALSE, |
| 4776 |
NULL); |
| 4777 |
} else { |
| 4778 |
if (rate >= 0) |
| 4779 |
start_ts = seeksegment.position; |
| 4780 |
else |
| 4781 |
start_ts = start; |
| 4782 |
|
| 4783 |
if (seeksegment.stop == -1 && seeksegment.duration != -1) |
| 4784 |
seeksegment.stop = seeksegment.start + seeksegment.duration; |
| 4785 |
|
| 4786 |
if (!gst_base_parse_convert (parse, format, start_ts, |
| 4787 |
GST_FORMAT_BYTES, &seekpos)) |
| 4788 |
goto convert_failed; |
| 4789 |
if (!gst_base_parse_convert (parse, format, seeksegment.stop, |
| 4790 |
GST_FORMAT_BYTES, &seekstop)) |
| 4791 |
goto convert_failed; |
| 4792 |
} |
| 4793 |
|
| 4794 |
GST_DEBUG_OBJECT (parse, |
| 4795 |
"seek position %" G_GINT64_FORMAT " in bytes: %" G_GINT64_FORMAT, |
| 4796 |
start_ts, seekpos); |
| 4797 |
GST_DEBUG_OBJECT (parse, |
| 4798 |
"seek stop %" G_GINT64_FORMAT " in bytes: %" G_GINT64_FORMAT, |
| 4799 |
seeksegment.stop, seekstop); |
| 4800 |
|
| 4801 |
if (parse->priv->pad_mode == GST_PAD_MODE_PULL) { |
| 4802 |
gint64 last_stop; |
| 4803 |
|
| 4804 |
GST_DEBUG_OBJECT (parse, "seek in PULL mode"); |
| 4805 |
|
| 4806 |
if (flush) { |
| 4807 |
if (parse->srcpad) { |
| 4808 |
GstEvent *fevent = gst_event_new_flush_start (); |
| 4809 |
GST_DEBUG_OBJECT (parse, "sending flush start"); |
| 4810 |
|
| 4811 |
gst_event_set_seqnum (fevent, seqnum); |
| 4812 |
|
| 4813 |
gst_pad_push_event (parse->srcpad, gst_event_ref (fevent)); |
| 4814 |
/* unlock upstream pull_range */ |
| 4815 |
gst_pad_push_event (parse->sinkpad, fevent); |
| 4816 |
} |
| 4817 |
} else { |
| 4818 |
gst_pad_pause_task (parse->sinkpad); |
| 4819 |
} |
| 4820 |
|
| 4821 |
/* we should now be able to grab the streaming thread because we stopped it |
| 4822 |
* with the above flush/pause code */ |
| 4823 |
GST_PAD_STREAM_LOCK (parse->sinkpad); |
| 4824 |
|
| 4825 |
/* save current position */ |
| 4826 |
last_stop = parse->segment.position; |
| 4827 |
GST_DEBUG_OBJECT (parse, "stopped streaming at %" G_GINT64_FORMAT, |
| 4828 |
last_stop); |
| 4829 |
|
| 4830 |
/* now commit to new position */ |
| 4831 |
|
| 4832 |
/* prepare for streaming again */ |
| 4833 |
if (flush) { |
| 4834 |
GstEvent *fevent = gst_event_new_flush_stop (TRUE); |
| 4835 |
GST_DEBUG_OBJECT (parse, "sending flush stop"); |
| 4836 |
gst_event_set_seqnum (fevent, seqnum); |
| 4837 |
gst_pad_push_event (parse->srcpad, gst_event_ref (fevent)); |
| 4838 |
gst_pad_push_event (parse->sinkpad, fevent); |
| 4839 |
gst_base_parse_clear_queues (parse, FALSE); |
| 4840 |
} |
| 4841 |
|
| 4842 |
memcpy (&parse->segment, &seeksegment, sizeof (GstSegment)); |
| 4843 |
|
| 4844 |
/* store the newsegment event so it can be sent from the streaming thread. */ |
| 4845 |
/* This will be sent later in _loop() */ |
| 4846 |
segment_event = gst_event_new_segment (&parse->segment); |
| 4847 |
gst_event_set_seqnum (segment_event, seqnum); |
| 4848 |
parse->priv->pending_events = |
| 4849 |
g_list_prepend (parse->priv->pending_events, segment_event); |
| 4850 |
|
| 4851 |
GST_DEBUG_OBJECT (parse, "Created newseg format %d, " |
| 4852 |
"start = %" GST_TIME_FORMAT ", stop = %" GST_TIME_FORMAT |
| 4853 |
", time = %" GST_TIME_FORMAT, format, |
| 4854 |
GST_TIME_ARGS (parse->segment.start), |
| 4855 |
GST_TIME_ARGS (parse->segment.stop), |
| 4856 |
GST_TIME_ARGS (parse->segment.time)); |
| 4857 |
|
| 4858 |
/* one last chance in pull mode to stay accurate; |
| 4859 |
* maybe scan and subclass can find where to go */ |
| 4860 |
if (!accurate) { |
| 4861 |
gint64 scanpos; |
| 4862 |
GstClockTime ts = seeksegment.position; |
| 4863 |
|
| 4864 |
gst_base_parse_locate_time (parse, &ts, &scanpos); |
| 4865 |
if (scanpos >= 0) { |
| 4866 |
accurate = TRUE; |
| 4867 |
seekpos = scanpos; |
| 4868 |
/* running collected index now consists of several intervals, |
| 4869 |
* so optimized check no longer possible */ |
| 4870 |
parse->priv->index_last_valid = FALSE; |
| 4871 |
parse->priv->index_last_offset = 0; |
| 4872 |
parse->priv->index_last_ts = 0; |
| 4873 |
} |
| 4874 |
} |
| 4875 |
|
| 4876 |
/* mark discont if we are going to stream from another position. */ |
| 4877 |
if (seekpos != parse->priv->offset) { |
| 4878 |
GST_DEBUG_OBJECT (parse, |
| 4879 |
"mark DISCONT, we did a seek to another position"); |
| 4880 |
parse->priv->offset = seekpos; |
| 4881 |
parse->priv->last_offset = seekpos; |
| 4882 |
parse->priv->seen_keyframe = FALSE; |
| 4883 |
parse->priv->discont = TRUE; |
| 4884 |
parse->priv->next_dts = start_ts; |
| 4885 |
parse->priv->next_pts = GST_CLOCK_TIME_NONE; |
| 4886 |
parse->priv->last_dts = GST_CLOCK_TIME_NONE; |
| 4887 |
parse->priv->last_pts = GST_CLOCK_TIME_NONE; |
| 4888 |
parse->priv->sync_offset = seekpos; |
| 4889 |
parse->priv->exact_position = accurate; |
| 4890 |
} |
| 4891 |
|
| 4892 |
/* Start streaming thread if paused */ |
| 4893 |
gst_pad_start_task (parse->sinkpad, |
| 4894 |
(GstTaskFunction) gst_base_parse_loop, parse->sinkpad, NULL); |
| 4895 |
|
| 4896 |
GST_PAD_STREAM_UNLOCK (parse->sinkpad); |
| 4897 |
|
| 4898 |
/* handled seek */ |
| 4899 |
res = TRUE; |
| 4900 |
} else { |
| 4901 |
GstEvent *new_event; |
| 4902 |
GstBaseParseSeek *seek; |
| 4903 |
GstSeekFlags flags = (flush ? GST_SEEK_FLAG_FLUSH : GST_SEEK_FLAG_NONE); |
| 4904 |
|
| 4905 |
/* The only thing we need to do in PUSH-mode is to send the |
| 4906 |
seek event (in bytes) to upstream. Segment / flush handling happens |
| 4907 |
in corresponding src event handlers */ |
| 4908 |
GST_DEBUG_OBJECT (parse, "seek in PUSH mode"); |
| 4909 |
if (seekstop >= 0 && seekstop <= seekpos) |
| 4910 |
seekstop = seekpos; |
| 4911 |
new_event = gst_event_new_seek (rate, GST_FORMAT_BYTES, flags, |
| 4912 |
GST_SEEK_TYPE_SET, seekpos, stop_type, seekstop); |
| 4913 |
gst_event_set_seqnum (new_event, seqnum); |
| 4914 |
|
| 4915 |
/* store segment info so its precise details can be reconstructed when |
| 4916 |
* receiving newsegment; |
| 4917 |
* this matters for all details when accurate seeking, |
| 4918 |
* is most useful to preserve NONE stop time otherwise */ |
| 4919 |
seek = g_new0 (GstBaseParseSeek, 1); |
| 4920 |
seek->segment = seeksegment; |
| 4921 |
seek->accurate = accurate; |
| 4922 |
seek->offset = seekpos; |
| 4923 |
seek->start_ts = start_ts; |
| 4924 |
GST_OBJECT_LOCK (parse); |
| 4925 |
/* less optimal, but preserves order */ |
| 4926 |
parse->priv->pending_seeks = |
| 4927 |
g_slist_append (parse->priv->pending_seeks, seek); |
| 4928 |
GST_OBJECT_UNLOCK (parse); |
| 4929 |
|
| 4930 |
res = gst_pad_push_event (parse->sinkpad, new_event); |
| 4931 |
|
| 4932 |
if (!res) { |
| 4933 |
GST_OBJECT_LOCK (parse); |
| 4934 |
parse->priv->pending_seeks = |
| 4935 |
g_slist_remove (parse->priv->pending_seeks, seek); |
| 4936 |
GST_OBJECT_UNLOCK (parse); |
| 4937 |
g_free (seek); |
| 4938 |
} |
| 4939 |
} |
| 4940 |
|
| 4941 |
done: |
| 4942 |
gst_event_unref (event); |
| 4943 |
return res; |
| 4944 |
|
| 4945 |
/* ERRORS */ |
| 4946 |
negative_rate: |
| 4947 |
{ |
| 4948 |
GST_DEBUG_OBJECT (parse, "negative playback rates delegated upstream."); |
| 4949 |
res = FALSE; |
| 4950 |
goto done; |
| 4951 |
} |
| 4952 |
wrong_type: |
| 4953 |
{ |
| 4954 |
GST_DEBUG_OBJECT (parse, "unsupported seek type."); |
| 4955 |
res = FALSE; |
| 4956 |
goto done; |
| 4957 |
} |
| 4958 |
no_convert_to_time: |
| 4959 |
{ |
| 4960 |
GST_DEBUG_OBJECT (parse, "seek in %s format was requested, but subclass " |
| 4961 |
"couldn't convert that into TIME format", gst_format_get_name (format)); |
| 4962 |
res = FALSE; |
| 4963 |
goto done; |
| 4964 |
} |
| 4965 |
convert_failed: |
| 4966 |
{ |
| 4967 |
GST_DEBUG_OBJECT (parse, "conversion TIME to BYTES failed."); |
| 4968 |
res = FALSE; |
| 4969 |
goto done; |
| 4970 |
} |
| 4971 |
} |
| 4972 |
|
| 4973 |
static void |
| 4974 |
gst_base_parse_set_upstream_tags (GstBaseParse * parse, GstTagList * taglist) |
| 4975 |
{ |
| 4976 |
if (taglist == parse->priv->upstream_tags) |
| 4977 |
return; |
| 4978 |
|
| 4979 |
if (parse->priv->upstream_tags) { |
| 4980 |
gst_tag_list_unref (parse->priv->upstream_tags); |
| 4981 |
parse->priv->upstream_tags = NULL; |
| 4982 |
} |
| 4983 |
|
| 4984 |
GST_INFO_OBJECT (parse, "upstream tags: %" GST_PTR_FORMAT, taglist); |
| 4985 |
|
| 4986 |
if (taglist != NULL) |
| 4987 |
parse->priv->upstream_tags = gst_tag_list_ref (taglist); |
| 4988 |
|
| 4989 |
gst_base_parse_check_bitrate_tags (parse); |
| 4990 |
} |
| 4991 |
|
| 4992 |
#if 0 |
| 4993 |
static void |
| 4994 |
gst_base_parse_set_index (GstElement * element, GstIndex * index) |
| 4995 |
{ |
| 4996 |
GstBaseParse *parse = GST_BASE_PARSE (element); |
| 4997 |
|
| 4998 |
GST_BASE_PARSE_INDEX_LOCK (parse); |
| 4999 |
if (parse->priv->index) |
| 5000 |
gst_object_unref (parse->priv->index); |
| 5001 |
if (index) { |
| 5002 |
parse->priv->index = gst_object_ref (index); |
| 5003 |
gst_index_get_writer_id (index, GST_OBJECT_CAST (element), |
| 5004 |
&parse->priv->index_id); |
| 5005 |
parse->priv->own_index = FALSE; |
| 5006 |
} else { |
| 5007 |
parse->priv->index = NULL; |
| 5008 |
} |
| 5009 |
GST_BASE_PARSE_INDEX_UNLOCK (parse); |
| 5010 |
} |
| 5011 |
|
| 5012 |
static GstIndex * |
| 5013 |
gst_base_parse_get_index (GstElement * element) |
| 5014 |
{ |
| 5015 |
GstBaseParse *parse = GST_BASE_PARSE (element); |
| 5016 |
GstIndex *result = NULL; |
| 5017 |
|
| 5018 |
GST_BASE_PARSE_INDEX_LOCK (parse); |
| 5019 |
if (parse->priv->index) |
| 5020 |
result = gst_object_ref (parse->priv->index); |
| 5021 |
GST_BASE_PARSE_INDEX_UNLOCK (parse); |
| 5022 |
|
| 5023 |
return result; |
| 5024 |
} |
| 5025 |
#endif |
| 5026 |
|
| 5027 |
static GstStateChangeReturn |
| 5028 |
gst_base_parse_change_state (GstElement * element, GstStateChange transition) |
| 5029 |
{ |
| 5030 |
GstBaseParse *parse; |
| 5031 |
GstStateChangeReturn result; |
| 5032 |
|
| 5033 |
parse = GST_BASE_PARSE (element); |
| 5034 |
|
| 5035 |
switch (transition) { |
| 5036 |
case GST_STATE_CHANGE_READY_TO_PAUSED: |
| 5037 |
/* If this is our own index destroy it as the |
| 5038 |
* old entries might be wrong for the new stream */ |
| 5039 |
GST_BASE_PARSE_INDEX_LOCK (parse); |
| 5040 |
if (parse->priv->own_index) { |
| 5041 |
gst_object_unref (parse->priv->index); |
| 5042 |
parse->priv->index = NULL; |
| 5043 |
parse->priv->own_index = FALSE; |
| 5044 |
} |
| 5045 |
|
| 5046 |
/* If no index was created, generate one */ |
| 5047 |
if (G_UNLIKELY (!parse->priv->index)) { |
| 5048 |
GST_DEBUG_OBJECT (parse, "no index provided creating our own"); |
| 5049 |
|
| 5050 |
parse->priv->index = g_object_new (gst_mem_index_get_type (), NULL); |
| 5051 |
gst_index_get_writer_id (parse->priv->index, GST_OBJECT (parse), |
| 5052 |
&parse->priv->index_id); |
| 5053 |
parse->priv->own_index = TRUE; |
| 5054 |
} |
| 5055 |
GST_BASE_PARSE_INDEX_UNLOCK (parse); |
| 5056 |
break; |
| 5057 |
default: |
| 5058 |
break; |
| 5059 |
} |
| 5060 |
|
| 5061 |
result = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition); |
| 5062 |
|
| 5063 |
switch (transition) { |
| 5064 |
case GST_STATE_CHANGE_PAUSED_TO_READY: |
| 5065 |
gst_base_parse_reset (parse); |
| 5066 |
break; |
| 5067 |
default: |
| 5068 |
break; |
| 5069 |
} |
| 5070 |
|
| 5071 |
return result; |
| 5072 |
} |
| 5073 |
|
| 5074 |
/** |
| 5075 |
* gst_base_parse_set_ts_at_offset: |
| 5076 |
* @parse: a #GstBaseParse |
| 5077 |
* @offset: offset into current buffer |
| 5078 |
* |
| 5079 |
* This function should only be called from a @handle_frame implementation. |
| 5080 |
* |
| 5081 |
* #GstBaseParse creates initial timestamps for frames by using the last |
| 5082 |
* timestamp seen in the stream before the frame starts. In certain |
| 5083 |
* cases, the correct timestamps will occur in the stream after the |
| 5084 |
* start of the frame, but before the start of the actual picture data. |
| 5085 |
* This function can be used to set the timestamps based on the offset |
| 5086 |
* into the frame data that the picture starts. |
| 5087 |
* |
| 5088 |
* Since: 1.2 |
| 5089 |
*/ |
| 5090 |
void |
| 5091 |
gst_base_parse_set_ts_at_offset (GstBaseParse * parse, gsize offset) |
| 5092 |
{ |
| 5093 |
GstClockTime pts, dts; |
| 5094 |
|
| 5095 |
g_return_if_fail (GST_IS_BASE_PARSE (parse)); |
| 5096 |
|
| 5097 |
pts = gst_adapter_prev_pts_at_offset (parse->priv->adapter, offset, NULL); |
| 5098 |
dts = gst_adapter_prev_dts_at_offset (parse->priv->adapter, offset, NULL); |
| 5099 |
|
| 5100 |
if (!GST_CLOCK_TIME_IS_VALID (pts) || !GST_CLOCK_TIME_IS_VALID (dts)) { |
| 5101 |
GST_DEBUG_OBJECT (parse, |
| 5102 |
"offset adapter timestamps dts=%" GST_TIME_FORMAT " pts=%" |
| 5103 |
GST_TIME_FORMAT, GST_TIME_ARGS (dts), GST_TIME_ARGS (pts)); |
| 5104 |
} |
| 5105 |
if (GST_CLOCK_TIME_IS_VALID (pts) && (parse->priv->prev_pts != pts)) |
| 5106 |
parse->priv->prev_pts = parse->priv->next_pts = pts; |
| 5107 |
|
| 5108 |
if (GST_CLOCK_TIME_IS_VALID (dts) && (parse->priv->prev_dts != dts)) { |
| 5109 |
parse->priv->prev_dts = parse->priv->next_dts = dts; |
| 5110 |
parse->priv->prev_dts_from_pts = FALSE; |
| 5111 |
} |
| 5112 |
} |
| 5113 |
|
| 5114 |
/** |
| 5115 |
* gst_base_parse_merge_tags: |
| 5116 |
* @parse: a #GstBaseParse |
| 5117 |
* @tags: (allow-none): a #GstTagList to merge, or NULL to unset |
| 5118 |
* previously-set tags |
| 5119 |
* @mode: the #GstTagMergeMode to use, usually #GST_TAG_MERGE_REPLACE |
| 5120 |
* |
| 5121 |
* Sets the parser subclass's tags and how they should be merged with any |
| 5122 |
* upstream stream tags. This will override any tags previously-set |
| 5123 |
* with gst_base_parse_merge_tags(). |
| 5124 |
* |
| 5125 |
* Note that this is provided for convenience, and the subclass is |
| 5126 |
* not required to use this and can still do tag handling on its own. |
| 5127 |
* |
| 5128 |
* Since: 1.6 |
| 5129 |
*/ |
| 5130 |
void |
| 5131 |
gst_base_parse_merge_tags (GstBaseParse * parse, GstTagList * tags, |
| 5132 |
GstTagMergeMode mode) |
| 5133 |
{ |
| 5134 |
g_return_if_fail (GST_IS_BASE_PARSE (parse)); |
| 5135 |
g_return_if_fail (tags == NULL || GST_IS_TAG_LIST (tags)); |
| 5136 |
g_return_if_fail (tags == NULL || mode != GST_TAG_MERGE_UNDEFINED); |
| 5137 |
|
| 5138 |
GST_OBJECT_LOCK (parse); |
| 5139 |
|
| 5140 |
if (tags != parse->priv->parser_tags) { |
| 5141 |
if (parse->priv->parser_tags) { |
| 5142 |
gst_tag_list_unref (parse->priv->parser_tags); |
| 5143 |
parse->priv->parser_tags = NULL; |
| 5144 |
parse->priv->parser_tags_merge_mode = GST_TAG_MERGE_APPEND; |
| 5145 |
} |
| 5146 |
if (tags) { |
| 5147 |
parse->priv->parser_tags = gst_tag_list_ref (tags); |
| 5148 |
parse->priv->parser_tags_merge_mode = mode; |
| 5149 |
} |
| 5150 |
|
| 5151 |
GST_DEBUG_OBJECT (parse, "setting parser tags to %" GST_PTR_FORMAT |
| 5152 |
" (mode %d)", tags, parse->priv->parser_tags_merge_mode); |
| 5153 |
|
| 5154 |
gst_base_parse_check_bitrate_tags (parse); |
| 5155 |
parse->priv->tags_changed = TRUE; |
| 5156 |
} |
| 5157 |
|
| 5158 |
GST_OBJECT_UNLOCK (parse); |
| 5159 |
} |