gstaudiobasesink.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. /* GStreamer
  2. * Copyright (C) 1999,2000 Erik Walthinsen <omega@cse.ogi.edu>
  3. * 2005 Wim Taymans <wim@fluendo.com>
  4. *
  5. * gstaudiobasesink.h:
  6. *
  7. * This library is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Library General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2 of the License, or (at your option) any later version.
  11. *
  12. * This library is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Library General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Library General Public
  18. * License along with this library; if not, write to the
  19. * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
  20. * Boston, MA 02110-1301, USA.
  21. */
  22. /* a base class for audio sinks.
  23. *
  24. * It uses a ringbuffer to schedule playback of samples. This makes
  25. * it very easy to drop or insert samples to align incoming
  26. * buffers to the exact playback timestamp.
  27. *
  28. * Subclasses must provide a ringbuffer pointing to either DMA
  29. * memory or regular memory. A subclass should also call a callback
  30. * function when it has played N segments in the buffer. The subclass
  31. * is free to use a thread to signal this callback, use EIO or any
  32. * other mechanism.
  33. *
  34. * The base class is able to operate in push or pull mode. The chain
  35. * mode will queue the samples in the ringbuffer as much as possible.
  36. * The available space is calculated in the callback function.
  37. *
  38. * The pull mode will pull_range() a new buffer of N samples with a
  39. * configurable latency. This allows for high-end real time
  40. * audio processing pipelines driven by the audiosink. The callback
  41. * function will be used to perform a pull_range() on the sinkpad.
  42. * The thread scheduling the callback can be a real-time thread.
  43. *
  44. * Subclasses must implement a GstAudioRingBuffer in addition to overriding
  45. * the methods in GstBaseSink and this class.
  46. */
  47. #ifndef __GST_AUDIO_AUDIO_H__
  48. #include <gst/audio/audio.h>
  49. #endif
  50. #ifndef __GST_AUDIO_BASE_SINK_H__
  51. #define __GST_AUDIO_BASE_SINK_H__
  52. #include <gst/base/gstbasesink.h>
  53. G_BEGIN_DECLS
  54. #define GST_TYPE_AUDIO_BASE_SINK (gst_audio_base_sink_get_type())
  55. #define GST_AUDIO_BASE_SINK(obj) (G_TYPE_CHECK_INSTANCE_CAST((obj),GST_TYPE_AUDIO_BASE_SINK,GstAudioBaseSink))
  56. #define GST_AUDIO_BASE_SINK_CAST(obj) ((GstAudioBaseSink*)obj)
  57. #define GST_AUDIO_BASE_SINK_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST((klass),GST_TYPE_AUDIO_BASE_SINK,GstAudioBaseSinkClass))
  58. #define GST_AUDIO_BASE_SINK_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), GST_TYPE_AUDIO_BASE_SINK, GstAudioBaseSinkClass))
  59. #define GST_IS_AUDIO_BASE_SINK(obj) (G_TYPE_CHECK_INSTANCE_TYPE((obj),GST_TYPE_AUDIO_BASE_SINK))
  60. #define GST_IS_AUDIO_BASE_SINK_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE((klass),GST_TYPE_AUDIO_BASE_SINK))
  61. /**
  62. * GST_AUDIO_BASE_SINK_CLOCK:
  63. * @obj: a #GstAudioBaseSink
  64. *
  65. * Get the #GstClock of @obj.
  66. */
  67. #define GST_AUDIO_BASE_SINK_CLOCK(obj) (GST_AUDIO_BASE_SINK (obj)->clock)
  68. /**
  69. * GST_AUDIO_BASE_SINK_PAD:
  70. * @obj: a #GstAudioBaseSink
  71. *
  72. * Get the sink #GstPad of @obj.
  73. */
  74. #define GST_AUDIO_BASE_SINK_PAD(obj) (GST_BASE_SINK (obj)->sinkpad)
  75. /**
  76. * GstAudioBaseSinkSlaveMethod:
  77. * @GST_AUDIO_BASE_SINK_SLAVE_RESAMPLE: Resample to match the master clock
  78. * @GST_AUDIO_BASE_SINK_SLAVE_SKEW: Adjust playout pointer when master clock
  79. * drifts too much.
  80. * @GST_AUDIO_BASE_SINK_SLAVE_NONE: No adjustment is done.
  81. * @GST_AUDIO_BASE_SINK_SLAVE_CUSTOM: Use custom clock slaving algorithm (Since: 1.6)
  82. *
  83. * Different possible clock slaving algorithms used when the internal audio
  84. * clock is not selected as the pipeline master clock.
  85. */
  86. typedef enum
  87. {
  88. GST_AUDIO_BASE_SINK_SLAVE_RESAMPLE,
  89. GST_AUDIO_BASE_SINK_SLAVE_SKEW,
  90. GST_AUDIO_BASE_SINK_SLAVE_NONE,
  91. GST_AUDIO_BASE_SINK_SLAVE_CUSTOM
  92. } GstAudioBaseSinkSlaveMethod;
  93. #define GST_TYPE_AUDIO_BASE_SINK_SLAVE_METHOD (gst_audio_base_sink_slave_method_get_type ())
  94. typedef struct _GstAudioBaseSink GstAudioBaseSink;
  95. typedef struct _GstAudioBaseSinkClass GstAudioBaseSinkClass;
  96. typedef struct _GstAudioBaseSinkPrivate GstAudioBaseSinkPrivate;
  97. /**
  98. * GstAudioBaseSinkDiscontReason:
  99. * @GST_AUDIO_BASE_SINK_DISCONT_REASON_NO_DISCONT: No discontinuity occurred
  100. * @GST_AUDIO_BASE_SINK_DISCONT_REASON_NEW_CAPS: New caps are set, causing renegotiotion
  101. * @GST_AUDIO_BASE_SINK_DISCONT_REASON_FLUSH: Samples have been flushed
  102. * @GST_AUDIO_BASE_SINK_DISCONT_REASON_SYNC_LATENCY: Sink was synchronized to the estimated latency (occurs during initialization)
  103. * @GST_AUDIO_BASE_SINK_DISCONT_REASON_ALIGNMENT: Aligning buffers failed because the timestamps are too discontinuous
  104. * @GST_AUDIO_BASE_SINK_DISCONT_REASON_DEVICE_FAILURE: Audio output device experienced and recovered from an error but introduced latency in the process (see also @gst_audio_base_sink_report_device_failure())
  105. *
  106. * Different possible reasons for discontinuities. This enum is useful for the custom
  107. * slave method.
  108. *
  109. * Since: 1.6
  110. */
  111. typedef enum
  112. {
  113. GST_AUDIO_BASE_SINK_DISCONT_REASON_NO_DISCONT,
  114. GST_AUDIO_BASE_SINK_DISCONT_REASON_NEW_CAPS,
  115. GST_AUDIO_BASE_SINK_DISCONT_REASON_FLUSH,
  116. GST_AUDIO_BASE_SINK_DISCONT_REASON_SYNC_LATENCY,
  117. GST_AUDIO_BASE_SINK_DISCONT_REASON_ALIGNMENT,
  118. GST_AUDIO_BASE_SINK_DISCONT_REASON_DEVICE_FAILURE
  119. } GstAudioBaseSinkDiscontReason;
  120. /**
  121. * GstAudioBaseSinkCustomSlavingCallback:
  122. * @sink: a #GstAudioBaseSink
  123. * @etime: external clock time
  124. * @itime: internal clock time
  125. * @requested_skew: skew amount requested by the callback
  126. * @discont_reason: reason for discontinuity (if any)
  127. * @user_data: user data
  128. *
  129. * This function is set with gst_audio_base_sink_set_custom_slaving_callback()
  130. * and is called during playback. It receives the current time of external and
  131. * internal clocks, which the callback can then use to apply any custom
  132. * slaving/synchronization schemes.
  133. *
  134. * The external clock is the sink's element clock, the internal one is the
  135. * internal audio clock. The internal audio clock's calibration is applied to
  136. * the timestamps before they are passed to the callback. The difference between
  137. * etime and itime is the skew; how much internal and external clock lie apart
  138. * from each other. A skew of 0 means both clocks are perfectly in sync.
  139. * itime > etime means the external clock is going slower, while itime < etime
  140. * means it is going faster than the internal clock. etime and itime are always
  141. * valid timestamps, except for when a discontinuity happens.
  142. *
  143. * requested_skew is an output value the callback can write to. It informs the
  144. * sink of whether or not it should move the playout pointer, and if so, by how
  145. * much. This pointer is only NULL if a discontinuity occurs; otherwise, it is
  146. * safe to write to *requested_skew. The default skew is 0.
  147. *
  148. * The sink may experience discontinuities. If one happens, discont is TRUE,
  149. * itime, etime are set to GST_CLOCK_TIME_NONE, and requested_skew is NULL.
  150. * This makes it possible to reset custom clock slaving algorithms when a
  151. * discontinuity happens.
  152. *
  153. * Since: 1.6
  154. */
  155. typedef void (*GstAudioBaseSinkCustomSlavingCallback) (GstAudioBaseSink *sink, GstClockTime etime, GstClockTime itime, GstClockTimeDiff *requested_skew, GstAudioBaseSinkDiscontReason discont_reason, gpointer user_data);
  156. /**
  157. * GstAudioBaseSink:
  158. *
  159. * Opaque #GstAudioBaseSink.
  160. */
  161. struct _GstAudioBaseSink {
  162. GstBaseSink element;
  163. /*< protected >*/ /* with LOCK */
  164. /* our ringbuffer */
  165. GstAudioRingBuffer *ringbuffer;
  166. /* required buffer and latency in microseconds */
  167. guint64 buffer_time;
  168. guint64 latency_time;
  169. /* the next sample to write */
  170. guint64 next_sample;
  171. /* clock */
  172. GstClock *provided_clock;
  173. /* with g_atomic_; currently rendering eos */
  174. gboolean eos_rendering;
  175. /*< private >*/
  176. GstAudioBaseSinkPrivate *priv;
  177. gpointer _gst_reserved[GST_PADDING];
  178. };
  179. /**
  180. * GstAudioBaseSinkClass:
  181. * @parent_class: the parent class.
  182. * @create_ringbuffer: create and return a #GstAudioRingBuffer to write to.
  183. * @payload: payload data in a format suitable to write to the sink. If no
  184. * payloading is required, returns a reffed copy of the original
  185. * buffer, else returns the payloaded buffer with all other metadata
  186. * copied.
  187. *
  188. * #GstAudioBaseSink class. Override the vmethod to implement
  189. * functionality.
  190. */
  191. struct _GstAudioBaseSinkClass {
  192. GstBaseSinkClass parent_class;
  193. /* subclass ringbuffer allocation */
  194. GstAudioRingBuffer* (*create_ringbuffer) (GstAudioBaseSink *sink);
  195. /* subclass payloader */
  196. GstBuffer* (*payload) (GstAudioBaseSink *sink,
  197. GstBuffer *buffer);
  198. /*< private >*/
  199. gpointer _gst_reserved[GST_PADDING];
  200. };
  201. GType gst_audio_base_sink_get_type(void);
  202. GType gst_audio_base_sink_slave_method_get_type (void);
  203. GstAudioRingBuffer *
  204. gst_audio_base_sink_create_ringbuffer (GstAudioBaseSink *sink);
  205. void gst_audio_base_sink_set_provide_clock (GstAudioBaseSink *sink, gboolean provide);
  206. gboolean gst_audio_base_sink_get_provide_clock (GstAudioBaseSink *sink);
  207. void gst_audio_base_sink_set_slave_method (GstAudioBaseSink *sink,
  208. GstAudioBaseSinkSlaveMethod method);
  209. GstAudioBaseSinkSlaveMethod
  210. gst_audio_base_sink_get_slave_method (GstAudioBaseSink *sink);
  211. void gst_audio_base_sink_set_drift_tolerance (GstAudioBaseSink *sink,
  212. gint64 drift_tolerance);
  213. gint64 gst_audio_base_sink_get_drift_tolerance (GstAudioBaseSink *sink);
  214. void gst_audio_base_sink_set_alignment_threshold (GstAudioBaseSink * sink,
  215. GstClockTime alignment_threshold);
  216. GstClockTime
  217. gst_audio_base_sink_get_alignment_threshold (GstAudioBaseSink * sink);
  218. void gst_audio_base_sink_set_discont_wait (GstAudioBaseSink * sink,
  219. GstClockTime discont_wait);
  220. GstClockTime
  221. gst_audio_base_sink_get_discont_wait (GstAudioBaseSink * sink);
  222. void
  223. gst_audio_base_sink_set_custom_slaving_callback (GstAudioBaseSink * sink,
  224. GstAudioBaseSinkCustomSlavingCallback callback,
  225. gpointer user_data,
  226. GDestroyNotify notify);
  227. void gst_audio_base_sink_report_device_failure (GstAudioBaseSink * sink);
  228. #ifdef G_DEFINE_AUTOPTR_CLEANUP_FUNC
  229. G_DEFINE_AUTOPTR_CLEANUP_FUNC(GstAudioBaseSink, gst_object_unref)
  230. #endif
  231. G_END_DECLS
  232. #endif /* __GST_AUDIO_BASE_SINK_H__ */