enabling omx decoder
[platform/upstream/gstreamer.git] / omx / gstomxaudioenc.c
1 /*
2  * Copyright (C) 2011, Hewlett-Packard Development Company, L.P.
3  *   Author: Sebastian Dröge <sebastian.droege@collabora.co.uk>, Collabora Ltd.
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation
8  * version 2.1 of the License.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301 USA
18  *
19  */
20
21 #ifdef HAVE_CONFIG_H
22 #include "config.h"
23 #endif
24
25 #include <gst/gst.h>
26 #include <string.h>
27
28 #include "gstomxaudioenc.h"
29
30 GST_DEBUG_CATEGORY_STATIC (gst_omx_audio_enc_debug_category);
31 #define GST_CAT_DEFAULT gst_omx_audio_enc_debug_category
32
33 /* prototypes */
34 static void gst_omx_audio_enc_finalize (GObject * object);
35
36 static GstStateChangeReturn
37 gst_omx_audio_enc_change_state (GstElement * element,
38     GstStateChange transition);
39
40 static gboolean gst_omx_audio_enc_open (GstAudioEncoder * encoder);
41 static gboolean gst_omx_audio_enc_close (GstAudioEncoder * encoder);
42 static gboolean gst_omx_audio_enc_start (GstAudioEncoder * encoder);
43 static gboolean gst_omx_audio_enc_stop (GstAudioEncoder * encoder);
44 static gboolean gst_omx_audio_enc_set_format (GstAudioEncoder * encoder,
45     GstAudioInfo * info);
46 static GstFlowReturn gst_omx_audio_enc_handle_frame (GstAudioEncoder *
47     encoder, GstBuffer * buffer);
48 static void gst_omx_audio_enc_flush (GstAudioEncoder * encoder);
49
50 static GstFlowReturn gst_omx_audio_enc_drain (GstOMXAudioEnc * self);
51
52 enum
53 {
54   PROP_0
55 };
56
57 /* class initialization */
58
59 #define DEBUG_INIT \
60   GST_DEBUG_CATEGORY_INIT (gst_omx_audio_enc_debug_category, "omxaudioenc", 0, \
61       "debug category for gst-omx audio encoder base class");
62
63 G_DEFINE_ABSTRACT_TYPE_WITH_CODE (GstOMXAudioEnc, gst_omx_audio_enc,
64     GST_TYPE_AUDIO_ENCODER, DEBUG_INIT);
65
66 static void
67 gst_omx_audio_enc_class_init (GstOMXAudioEncClass * klass)
68 {
69   GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
70   GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
71   GstAudioEncoderClass *audio_encoder_class = GST_AUDIO_ENCODER_CLASS (klass);
72
73   gobject_class->finalize = gst_omx_audio_enc_finalize;
74
75   element_class->change_state =
76       GST_DEBUG_FUNCPTR (gst_omx_audio_enc_change_state);
77
78   audio_encoder_class->open = GST_DEBUG_FUNCPTR (gst_omx_audio_enc_open);
79   audio_encoder_class->close = GST_DEBUG_FUNCPTR (gst_omx_audio_enc_close);
80   audio_encoder_class->start = GST_DEBUG_FUNCPTR (gst_omx_audio_enc_start);
81   audio_encoder_class->stop = GST_DEBUG_FUNCPTR (gst_omx_audio_enc_stop);
82   audio_encoder_class->flush = GST_DEBUG_FUNCPTR (gst_omx_audio_enc_flush);
83   audio_encoder_class->set_format =
84       GST_DEBUG_FUNCPTR (gst_omx_audio_enc_set_format);
85   audio_encoder_class->handle_frame =
86       GST_DEBUG_FUNCPTR (gst_omx_audio_enc_handle_frame);
87
88   klass->cdata.type = GST_OMX_COMPONENT_TYPE_FILTER;
89   klass->cdata.default_sink_template_caps = "audio/x-raw, "
90       "rate = (int) [ 1, MAX ], "
91       "channels = (int) [ 1, " G_STRINGIFY (OMX_AUDIO_MAXCHANNELS) " ], "
92       "format = (string) { S8, U8, S16LE, S16BE, U16LE, U16BE, "
93       "S24LE, S24BE, U24LE, U24BE, S32LE, S32BE, U32LE, U32BE }";
94 }
95
96 static void
97 gst_omx_audio_enc_init (GstOMXAudioEnc * self)
98 {
99   g_mutex_init (&self->drain_lock);
100   g_cond_init (&self->drain_cond);
101 }
102
103 static gboolean
104 gst_omx_audio_enc_open (GstAudioEncoder * encoder)
105 {
106   GstOMXAudioEnc *self = GST_OMX_AUDIO_ENC (encoder);
107   GstOMXAudioEncClass *klass = GST_OMX_AUDIO_ENC_GET_CLASS (self);
108   gint in_port_index, out_port_index;
109
110   self->enc =
111       gst_omx_component_new (GST_OBJECT_CAST (self), klass->cdata.core_name,
112       klass->cdata.component_name, klass->cdata.component_role,
113       klass->cdata.hacks);
114   self->started = FALSE;
115
116   if (!self->enc)
117     return FALSE;
118
119   if (gst_omx_component_get_state (self->enc,
120           GST_CLOCK_TIME_NONE) != OMX_StateLoaded)
121     return FALSE;
122
123   in_port_index = klass->cdata.in_port_index;
124   out_port_index = klass->cdata.out_port_index;
125
126   if (in_port_index == -1 || out_port_index == -1) {
127     OMX_PORT_PARAM_TYPE param;
128     OMX_ERRORTYPE err;
129
130     GST_OMX_INIT_STRUCT (&param);
131
132     err =
133         gst_omx_component_get_parameter (self->enc, OMX_IndexParamAudioInit,
134         &param);
135     if (err != OMX_ErrorNone) {
136       GST_WARNING_OBJECT (self, "Couldn't get port information: %s (0x%08x)",
137           gst_omx_error_to_string (err), err);
138       /* Fallback */
139       in_port_index = 0;
140       out_port_index = 1;
141     } else {
142       GST_DEBUG_OBJECT (self, "Detected %u ports, starting at %u",
143           (guint) param.nPorts, (guint) param.nStartPortNumber);
144       in_port_index = param.nStartPortNumber + 0;
145       out_port_index = param.nStartPortNumber + 1;
146     }
147   }
148
149   self->enc_in_port = gst_omx_component_add_port (self->enc, in_port_index);
150   self->enc_out_port = gst_omx_component_add_port (self->enc, out_port_index);
151
152   if (!self->enc_in_port || !self->enc_out_port)
153     return FALSE;
154
155   return TRUE;
156 }
157
158
159 static gboolean
160 gst_omx_audio_enc_shutdown (GstOMXAudioEnc * self)
161 {
162   OMX_STATETYPE state;
163
164   GST_DEBUG_OBJECT (self, "Shutting down encoder");
165
166   state = gst_omx_component_get_state (self->enc, 0);
167   if (state > OMX_StateLoaded || state == OMX_StateInvalid) {
168     if (state > OMX_StateIdle) {
169       gst_omx_component_set_state (self->enc, OMX_StateIdle);
170       gst_omx_component_get_state (self->enc, 5 * GST_SECOND);
171     }
172     gst_omx_component_set_state (self->enc, OMX_StateLoaded);
173     gst_omx_port_deallocate_buffers (self->enc_in_port);
174     gst_omx_port_deallocate_buffers (self->enc_out_port);
175     if (state > OMX_StateLoaded)
176       gst_omx_component_get_state (self->enc, 5 * GST_SECOND);
177   }
178
179   return TRUE;
180 }
181
182 static gboolean
183 gst_omx_audio_enc_close (GstAudioEncoder * encoder)
184 {
185   GstOMXAudioEnc *self = GST_OMX_AUDIO_ENC (encoder);
186
187   GST_DEBUG_OBJECT (self, "Closing encoder");
188
189   if (!gst_omx_audio_enc_shutdown (self))
190     return FALSE;
191
192   self->enc_in_port = NULL;
193   self->enc_out_port = NULL;
194   if (self->enc)
195     gst_omx_component_free (self->enc);
196   self->enc = NULL;
197
198   return TRUE;
199 }
200
201 static void
202 gst_omx_audio_enc_finalize (GObject * object)
203 {
204   GstOMXAudioEnc *self = GST_OMX_AUDIO_ENC (object);
205
206   g_mutex_clear (&self->drain_lock);
207   g_cond_clear (&self->drain_cond);
208
209   G_OBJECT_CLASS (gst_omx_audio_enc_parent_class)->finalize (object);
210 }
211
212 static GstStateChangeReturn
213 gst_omx_audio_enc_change_state (GstElement * element, GstStateChange transition)
214 {
215   GstOMXAudioEnc *self;
216   GstStateChangeReturn ret = GST_STATE_CHANGE_SUCCESS;
217
218   g_return_val_if_fail (GST_IS_OMX_AUDIO_ENC (element),
219       GST_STATE_CHANGE_FAILURE);
220   self = GST_OMX_AUDIO_ENC (element);
221
222   switch (transition) {
223     case GST_STATE_CHANGE_NULL_TO_READY:
224       break;
225     case GST_STATE_CHANGE_READY_TO_PAUSED:
226       self->downstream_flow_ret = GST_FLOW_OK;
227
228       self->draining = FALSE;
229       self->started = FALSE;
230       break;
231     case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
232       break;
233     case GST_STATE_CHANGE_PAUSED_TO_READY:
234       if (self->enc_in_port)
235         gst_omx_port_set_flushing (self->enc_in_port, 5 * GST_SECOND, TRUE);
236       if (self->enc_out_port)
237         gst_omx_port_set_flushing (self->enc_out_port, 5 * GST_SECOND, TRUE);
238
239       g_mutex_lock (&self->drain_lock);
240       self->draining = FALSE;
241       g_cond_broadcast (&self->drain_cond);
242       g_mutex_unlock (&self->drain_lock);
243       break;
244     default:
245       break;
246   }
247
248   ret =
249       GST_ELEMENT_CLASS (gst_omx_audio_enc_parent_class)->change_state (element,
250       transition);
251
252   if (ret == GST_STATE_CHANGE_FAILURE)
253     return ret;
254
255   switch (transition) {
256     case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
257       break;
258     case GST_STATE_CHANGE_PAUSED_TO_READY:
259       self->downstream_flow_ret = GST_FLOW_FLUSHING;
260       self->started = FALSE;
261
262       if (!gst_omx_audio_enc_shutdown (self))
263         ret = GST_STATE_CHANGE_FAILURE;
264       break;
265     case GST_STATE_CHANGE_READY_TO_NULL:
266       break;
267     default:
268       break;
269   }
270
271   return ret;
272 }
273
274 static void
275 gst_omx_audio_enc_loop (GstOMXAudioEnc * self)
276 {
277   GstOMXAudioEncClass *klass;
278   GstOMXPort *port = self->enc_out_port;
279   GstOMXBuffer *buf = NULL;
280   GstFlowReturn flow_ret = GST_FLOW_OK;
281   GstOMXAcquireBufferReturn acq_return;
282   OMX_ERRORTYPE err;
283
284   klass = GST_OMX_AUDIO_ENC_GET_CLASS (self);
285
286   acq_return = gst_omx_port_acquire_buffer (port, &buf);
287   if (acq_return == GST_OMX_ACQUIRE_BUFFER_ERROR) {
288     goto component_error;
289   } else if (acq_return == GST_OMX_ACQUIRE_BUFFER_FLUSHING) {
290     goto flushing;
291   } else if (acq_return == GST_OMX_ACQUIRE_BUFFER_EOS) {
292     goto eos;
293   }
294
295   if (!gst_pad_has_current_caps (GST_AUDIO_ENCODER_SRC_PAD (self))
296       || acq_return == GST_OMX_ACQUIRE_BUFFER_RECONFIGURE) {
297     GstAudioInfo *info =
298         gst_audio_encoder_get_audio_info (GST_AUDIO_ENCODER (self));
299     GstCaps *caps;
300
301     GST_DEBUG_OBJECT (self, "Port settings have changed, updating caps");
302
303     /* Reallocate all buffers */
304     if (acq_return == GST_OMX_ACQUIRE_BUFFER_RECONFIGURE) {
305       err = gst_omx_port_set_enabled (port, FALSE);
306       if (err != OMX_ErrorNone)
307         goto reconfigure_error;
308
309       err = gst_omx_port_wait_buffers_released (port, 5 * GST_SECOND);
310       if (err != OMX_ErrorNone)
311         goto reconfigure_error;
312
313       err = gst_omx_port_deallocate_buffers (port);
314       if (err != OMX_ErrorNone)
315         goto reconfigure_error;
316
317       err = gst_omx_port_wait_enabled (port, 1 * GST_SECOND);
318       if (err != OMX_ErrorNone)
319         goto reconfigure_error;
320
321     }
322
323     GST_AUDIO_ENCODER_STREAM_LOCK (self);
324
325     caps = klass->get_caps (self, self->enc_out_port, info);
326     if (!caps) {
327       if (buf)
328         gst_omx_port_release_buffer (self->enc_out_port, buf);
329       GST_AUDIO_ENCODER_STREAM_UNLOCK (self);
330       goto caps_failed;
331     }
332
333     GST_DEBUG_OBJECT (self, "Setting output caps: %" GST_PTR_FORMAT, caps);
334
335     if (!gst_pad_set_caps (GST_AUDIO_ENCODER_SRC_PAD (self), caps)) {
336       gst_caps_unref (caps);
337       if (buf)
338         gst_omx_port_release_buffer (self->enc_out_port, buf);
339       GST_AUDIO_ENCODER_STREAM_UNLOCK (self);
340       goto caps_failed;
341     }
342     gst_caps_unref (caps);
343
344     GST_AUDIO_ENCODER_STREAM_UNLOCK (self);
345
346     if (acq_return == GST_OMX_ACQUIRE_BUFFER_RECONFIGURE) {
347       err = gst_omx_port_set_enabled (port, TRUE);
348       if (err != OMX_ErrorNone)
349         goto reconfigure_error;
350
351       err = gst_omx_port_allocate_buffers (port);
352       if (err != OMX_ErrorNone)
353         goto reconfigure_error;
354
355       err = gst_omx_port_wait_enabled (port, 5 * GST_SECOND);
356       if (err != OMX_ErrorNone)
357         goto reconfigure_error;
358
359       err = gst_omx_port_populate (port);
360       if (err != OMX_ErrorNone)
361         goto reconfigure_error;
362
363       err = gst_omx_port_mark_reconfigured (port);
364       if (err != OMX_ErrorNone)
365         goto reconfigure_error;
366     }
367
368     /* Now get a buffer */
369     if (acq_return != GST_OMX_ACQUIRE_BUFFER_OK) {
370       return;
371     }
372   }
373
374   g_assert (acq_return == GST_OMX_ACQUIRE_BUFFER_OK);
375   if (!buf) {
376     g_assert ((klass->cdata.hacks & GST_OMX_HACK_NO_EMPTY_EOS_BUFFER));
377     GST_AUDIO_ENCODER_STREAM_LOCK (self);
378     goto eos;
379   }
380
381   GST_DEBUG_OBJECT (self, "Handling buffer: 0x%08x %" G_GUINT64_FORMAT,
382       (guint) buf->omx_buf->nFlags, (guint64) buf->omx_buf->nTimeStamp);
383
384   /* This prevents a deadlock between the srcpad stream
385    * lock and the videocodec stream lock, if ::reset()
386    * is called at the wrong time
387    */
388   if (gst_omx_port_is_flushing (self->enc_out_port)) {
389     GST_DEBUG_OBJECT (self, "Flushing");
390     gst_omx_port_release_buffer (self->enc_out_port, buf);
391     goto flushing;
392   }
393
394   GST_AUDIO_ENCODER_STREAM_LOCK (self);
395
396   if ((buf->omx_buf->nFlags & OMX_BUFFERFLAG_CODECCONFIG)
397       && buf->omx_buf->nFilledLen > 0) {
398     GstCaps *caps;
399     GstBuffer *codec_data;
400     GstMapInfo map = GST_MAP_INFO_INIT;
401
402     GST_DEBUG_OBJECT (self, "Handling codec data");
403     caps =
404         gst_caps_copy (gst_pad_get_current_caps (GST_AUDIO_ENCODER_SRC_PAD
405             (self)));
406     codec_data = gst_buffer_new_and_alloc (buf->omx_buf->nFilledLen);
407
408     gst_buffer_map (codec_data, &map, GST_MAP_WRITE);
409     memcpy (map.data,
410         buf->omx_buf->pBuffer + buf->omx_buf->nOffset,
411         buf->omx_buf->nFilledLen);
412     gst_buffer_unmap (codec_data, &map);
413
414     gst_caps_set_simple (caps, "codec_data", GST_TYPE_BUFFER, codec_data, NULL);
415     if (!gst_pad_set_caps (GST_AUDIO_ENCODER_SRC_PAD (self), caps)) {
416       gst_caps_unref (caps);
417       if (buf)
418         gst_omx_port_release_buffer (self->enc_out_port, buf);
419       GST_AUDIO_ENCODER_STREAM_UNLOCK (self);
420       goto caps_failed;
421     }
422     gst_caps_unref (caps);
423     flow_ret = GST_FLOW_OK;
424   } else if (buf->omx_buf->nFilledLen > 0) {
425     GstBuffer *outbuf;
426     guint n_samples;
427
428     GST_DEBUG_OBJECT (self, "Handling output data");
429
430     n_samples =
431         klass->get_num_samples (self, self->enc_out_port,
432         gst_audio_encoder_get_audio_info (GST_AUDIO_ENCODER (self)), buf);
433
434     if (buf->omx_buf->nFilledLen > 0) {
435       GstMapInfo map = GST_MAP_INFO_INIT;
436       outbuf = gst_buffer_new_and_alloc (buf->omx_buf->nFilledLen);
437
438       gst_buffer_map (outbuf, &map, GST_MAP_WRITE);
439
440       memcpy (map.data,
441           buf->omx_buf->pBuffer + buf->omx_buf->nOffset,
442           buf->omx_buf->nFilledLen);
443       gst_buffer_unmap (outbuf, &map);
444
445     } else {
446       outbuf = gst_buffer_new ();
447     }
448
449     GST_BUFFER_TIMESTAMP (outbuf) =
450         gst_util_uint64_scale (buf->omx_buf->nTimeStamp, GST_SECOND,
451         OMX_TICKS_PER_SECOND);
452     if (buf->omx_buf->nTickCount != 0)
453       GST_BUFFER_DURATION (outbuf) =
454           gst_util_uint64_scale (buf->omx_buf->nTickCount, GST_SECOND,
455           OMX_TICKS_PER_SECOND);
456
457     flow_ret =
458         gst_audio_encoder_finish_frame (GST_AUDIO_ENCODER (self),
459         outbuf, n_samples);
460   }
461
462   GST_DEBUG_OBJECT (self, "Handled output data");
463
464   GST_DEBUG_OBJECT (self, "Finished frame: %s", gst_flow_get_name (flow_ret));
465
466   err = gst_omx_port_release_buffer (port, buf);
467   if (err != OMX_ErrorNone)
468     goto release_error;
469
470   self->downstream_flow_ret = flow_ret;
471
472   if (flow_ret != GST_FLOW_OK)
473     goto flow_error;
474
475   GST_AUDIO_ENCODER_STREAM_UNLOCK (self);
476
477   return;
478
479 component_error:
480   {
481     GST_ELEMENT_ERROR (self, LIBRARY, FAILED, (NULL),
482         ("OpenMAX component in error state %s (0x%08x)",
483             gst_omx_component_get_last_error_string (self->enc),
484             gst_omx_component_get_last_error (self->enc)));
485     gst_pad_push_event (GST_AUDIO_ENCODER_SRC_PAD (self), gst_event_new_eos ());
486     gst_pad_pause_task (GST_AUDIO_ENCODER_SRC_PAD (self));
487     self->downstream_flow_ret = GST_FLOW_ERROR;
488     self->started = FALSE;
489     return;
490   }
491 flushing:
492   {
493     GST_DEBUG_OBJECT (self, "Flushing -- stopping task");
494     gst_pad_pause_task (GST_AUDIO_ENCODER_SRC_PAD (self));
495     self->downstream_flow_ret = GST_FLOW_FLUSHING;
496     self->started = FALSE;
497     return;
498   }
499 eos:
500   {
501     g_mutex_lock (&self->drain_lock);
502     if (self->draining) {
503       GST_DEBUG_OBJECT (self, "Drained");
504       self->draining = FALSE;
505       g_cond_broadcast (&self->drain_cond);
506       flow_ret = GST_FLOW_OK;
507       gst_pad_pause_task (GST_AUDIO_ENCODER_SRC_PAD (self));
508     } else {
509       GST_DEBUG_OBJECT (self, "Component signalled EOS");
510       flow_ret = GST_FLOW_EOS;
511     }
512     g_mutex_unlock (&self->drain_lock);
513
514     GST_AUDIO_ENCODER_STREAM_LOCK (self);
515     self->downstream_flow_ret = flow_ret;
516
517     /* Here we fallback and pause the task for the EOS case */
518     if (flow_ret != GST_FLOW_OK)
519       goto flow_error;
520
521     GST_AUDIO_ENCODER_STREAM_UNLOCK (self);
522
523     return;
524   }
525 flow_error:
526   {
527     if (flow_ret == GST_FLOW_EOS) {
528       GST_DEBUG_OBJECT (self, "EOS");
529
530       gst_pad_push_event (GST_AUDIO_ENCODER_SRC_PAD (self),
531           gst_event_new_eos ());
532       gst_pad_pause_task (GST_AUDIO_ENCODER_SRC_PAD (self));
533       self->started = FALSE;
534     } else if (flow_ret < GST_FLOW_EOS) {
535       GST_ELEMENT_ERROR (self, STREAM, FAILED, ("Internal data stream error."),
536           ("stream stopped, reason %s", gst_flow_get_name (flow_ret)));
537
538       gst_pad_push_event (GST_AUDIO_ENCODER_SRC_PAD (self),
539           gst_event_new_eos ());
540       gst_pad_pause_task (GST_AUDIO_ENCODER_SRC_PAD (self));
541       self->started = FALSE;
542     } else if (flow_ret == GST_FLOW_FLUSHING) {
543       GST_DEBUG_OBJECT (self, "Flushing -- stopping task");
544       gst_pad_pause_task (GST_AUDIO_ENCODER_SRC_PAD (self));
545       self->started = FALSE;
546     }
547     GST_AUDIO_ENCODER_STREAM_UNLOCK (self);
548     return;
549   }
550 reconfigure_error:
551   {
552     GST_ELEMENT_ERROR (self, LIBRARY, SETTINGS, (NULL),
553         ("Unable to reconfigure output port"));
554     gst_pad_push_event (GST_AUDIO_ENCODER_SRC_PAD (self), gst_event_new_eos ());
555     gst_pad_pause_task (GST_AUDIO_ENCODER_SRC_PAD (self));
556     self->downstream_flow_ret = GST_FLOW_NOT_NEGOTIATED;
557     self->started = FALSE;
558     return;
559   }
560 caps_failed:
561   {
562     GST_ELEMENT_ERROR (self, LIBRARY, SETTINGS, (NULL), ("Failed to set caps"));
563     gst_pad_push_event (GST_AUDIO_ENCODER_SRC_PAD (self), gst_event_new_eos ());
564     gst_pad_pause_task (GST_AUDIO_ENCODER_SRC_PAD (self));
565     self->downstream_flow_ret = GST_FLOW_NOT_NEGOTIATED;
566     self->started = FALSE;
567     return;
568   }
569 release_error:
570   {
571     GST_ELEMENT_ERROR (self, LIBRARY, SETTINGS, (NULL),
572         ("Failed to relase output buffer to component: %s (0x%08x)",
573             gst_omx_error_to_string (err), err));
574     gst_pad_push_event (GST_AUDIO_ENCODER_SRC_PAD (self), gst_event_new_eos ());
575     gst_pad_pause_task (GST_AUDIO_ENCODER_SRC_PAD (self));
576     self->downstream_flow_ret = GST_FLOW_ERROR;
577     self->started = FALSE;
578     GST_AUDIO_ENCODER_STREAM_UNLOCK (self);
579     return;
580   }
581 }
582
583 static gboolean
584 gst_omx_audio_enc_start (GstAudioEncoder * encoder)
585 {
586   GstOMXAudioEnc *self;
587
588   self = GST_OMX_AUDIO_ENC (encoder);
589
590   self->last_upstream_ts = 0;
591   self->downstream_flow_ret = GST_FLOW_OK;
592
593   return TRUE;
594 }
595
596 static gboolean
597 gst_omx_audio_enc_stop (GstAudioEncoder * encoder)
598 {
599   GstOMXAudioEnc *self;
600
601   self = GST_OMX_AUDIO_ENC (encoder);
602
603   GST_DEBUG_OBJECT (self, "Stopping encoder");
604
605   gst_omx_port_set_flushing (self->enc_in_port, 5 * GST_SECOND, TRUE);
606   gst_omx_port_set_flushing (self->enc_out_port, 5 * GST_SECOND, TRUE);
607
608   gst_pad_stop_task (GST_AUDIO_ENCODER_SRC_PAD (encoder));
609
610   if (gst_omx_component_get_state (self->enc, 0) > OMX_StateIdle)
611     gst_omx_component_set_state (self->enc, OMX_StateIdle);
612
613   self->downstream_flow_ret = GST_FLOW_FLUSHING;
614   self->started = FALSE;
615
616   g_mutex_lock (&self->drain_lock);
617   self->draining = FALSE;
618   g_cond_broadcast (&self->drain_cond);
619   g_mutex_unlock (&self->drain_lock);
620
621   gst_omx_component_get_state (self->enc, 5 * GST_SECOND);
622
623   return TRUE;
624 }
625
626 static gboolean
627 gst_omx_audio_enc_set_format (GstAudioEncoder * encoder, GstAudioInfo * info)
628 {
629   GstOMXAudioEnc *self;
630   GstOMXAudioEncClass *klass;
631   gboolean needs_disable = FALSE;
632   OMX_PARAM_PORTDEFINITIONTYPE port_def;
633   OMX_AUDIO_PARAM_PCMMODETYPE pcm_param;
634   gint i;
635   OMX_ERRORTYPE err;
636
637   self = GST_OMX_AUDIO_ENC (encoder);
638   klass = GST_OMX_AUDIO_ENC_GET_CLASS (encoder);
639
640   GST_DEBUG_OBJECT (self, "Setting new caps");
641
642   /* Set audio encoder base class properties */
643   gst_audio_encoder_set_frame_samples_min (encoder,
644       gst_util_uint64_scale_ceil (OMX_MIN_PCMPAYLOAD_MSEC,
645           GST_MSECOND * info->rate, GST_SECOND));
646   gst_audio_encoder_set_frame_samples_max (encoder, 0);
647
648   gst_omx_port_get_port_definition (self->enc_in_port, &port_def);
649
650   needs_disable =
651       gst_omx_component_get_state (self->enc,
652       GST_CLOCK_TIME_NONE) != OMX_StateLoaded;
653   /* If the component is not in Loaded state and a real format change happens
654    * we have to disable the port and re-allocate all buffers. If no real
655    * format change happened we can just exit here.
656    */
657   if (needs_disable) {
658     GST_DEBUG_OBJECT (self, "Need to disable and drain encoder");
659     gst_omx_audio_enc_drain (self);
660     gst_omx_port_set_flushing (self->enc_out_port, 5 * GST_SECOND, TRUE);
661
662     /* Wait until the srcpad loop is finished,
663      * unlock GST_AUDIO_ENCODER_STREAM_LOCK to prevent deadlocks
664      * caused by using this lock from inside the loop function */
665     GST_AUDIO_ENCODER_STREAM_UNLOCK (self);
666     gst_pad_stop_task (GST_AUDIO_ENCODER_SRC_PAD (encoder));
667     GST_AUDIO_ENCODER_STREAM_LOCK (self);
668
669     if (klass->cdata.hacks & GST_OMX_HACK_NO_COMPONENT_RECONFIGURE) {
670       GST_AUDIO_ENCODER_STREAM_UNLOCK (self);
671       gst_omx_audio_enc_stop (GST_AUDIO_ENCODER (self));
672       gst_omx_audio_enc_close (GST_AUDIO_ENCODER (self));
673       GST_AUDIO_ENCODER_STREAM_LOCK (self);
674
675       if (!gst_omx_audio_enc_open (GST_AUDIO_ENCODER (self)))
676         return FALSE;
677       needs_disable = FALSE;
678     } else {
679       if (gst_omx_port_set_enabled (self->enc_in_port, FALSE) != OMX_ErrorNone)
680         return FALSE;
681       if (gst_omx_port_set_enabled (self->enc_out_port, FALSE) != OMX_ErrorNone)
682         return FALSE;
683       if (gst_omx_port_wait_buffers_released (self->enc_in_port,
684               5 * GST_SECOND) != OMX_ErrorNone)
685         return FALSE;
686       if (gst_omx_port_wait_buffers_released (self->enc_out_port,
687               1 * GST_SECOND) != OMX_ErrorNone)
688         return FALSE;
689       if (gst_omx_port_deallocate_buffers (self->enc_in_port) != OMX_ErrorNone)
690         return FALSE;
691       if (gst_omx_port_deallocate_buffers (self->enc_out_port) != OMX_ErrorNone)
692         return FALSE;
693       if (gst_omx_port_wait_enabled (self->enc_in_port,
694               1 * GST_SECOND) != OMX_ErrorNone)
695         return FALSE;
696       if (gst_omx_port_wait_enabled (self->enc_out_port,
697               1 * GST_SECOND) != OMX_ErrorNone)
698         return FALSE;
699     }
700
701     GST_DEBUG_OBJECT (self, "Encoder drained and disabled");
702   }
703
704   port_def.format.audio.eEncoding = OMX_AUDIO_CodingPCM;
705   GST_DEBUG_OBJECT (self, "Setting inport port definition");
706   if (gst_omx_port_update_port_definition (self->enc_in_port,
707           &port_def) != OMX_ErrorNone)
708     return FALSE;
709
710   GST_OMX_INIT_STRUCT (&pcm_param);
711   pcm_param.nPortIndex = self->enc_in_port->index;
712   pcm_param.nChannels = info->channels;
713   pcm_param.eNumData =
714       ((info->finfo->flags & GST_AUDIO_FORMAT_FLAG_SIGNED) ?
715       OMX_NumericalDataSigned : OMX_NumericalDataUnsigned);
716   pcm_param.eEndian =
717       ((info->finfo->endianness == G_LITTLE_ENDIAN) ?
718       OMX_EndianLittle : OMX_EndianBig);
719   pcm_param.bInterleaved = OMX_TRUE;
720   pcm_param.nBitPerSample = info->finfo->width;
721   pcm_param.nSamplingRate = info->rate;
722   pcm_param.ePCMMode = OMX_AUDIO_PCMModeLinear;
723
724   for (i = 0; i < pcm_param.nChannels; i++) {
725     OMX_AUDIO_CHANNELTYPE pos;
726
727     switch (info->position[i]) {
728       case GST_AUDIO_CHANNEL_POSITION_MONO:
729       case GST_AUDIO_CHANNEL_POSITION_FRONT_CENTER:
730         pos = OMX_AUDIO_ChannelCF;
731         break;
732       case GST_AUDIO_CHANNEL_POSITION_FRONT_LEFT:
733         pos = OMX_AUDIO_ChannelLF;
734         break;
735       case GST_AUDIO_CHANNEL_POSITION_FRONT_RIGHT:
736         pos = OMX_AUDIO_ChannelRF;
737         break;
738       case GST_AUDIO_CHANNEL_POSITION_SIDE_LEFT:
739         pos = OMX_AUDIO_ChannelLS;
740         break;
741       case GST_AUDIO_CHANNEL_POSITION_SIDE_RIGHT:
742         pos = OMX_AUDIO_ChannelRS;
743         break;
744       case GST_AUDIO_CHANNEL_POSITION_LFE1:
745         pos = OMX_AUDIO_ChannelLFE;
746         break;
747       case GST_AUDIO_CHANNEL_POSITION_REAR_CENTER:
748         pos = OMX_AUDIO_ChannelCS;
749         break;
750       case GST_AUDIO_CHANNEL_POSITION_REAR_LEFT:
751         pos = OMX_AUDIO_ChannelLR;
752         break;
753       case GST_AUDIO_CHANNEL_POSITION_REAR_RIGHT:
754         pos = OMX_AUDIO_ChannelRR;
755         break;
756       default:
757         pos = OMX_AUDIO_ChannelNone;
758         break;
759     }
760     pcm_param.eChannelMapping[i] = pos;
761   }
762
763   GST_DEBUG_OBJECT (self, "Setting PCM parameters");
764   err =
765       gst_omx_component_set_parameter (self->enc, OMX_IndexParamAudioPcm,
766       &pcm_param);
767   if (err != OMX_ErrorNone) {
768     GST_ERROR_OBJECT (self, "Failed to set PCM parameters: %s (0x%08x)",
769         gst_omx_error_to_string (err), err);
770     return FALSE;
771   }
772
773   if (klass->set_format) {
774     if (!klass->set_format (self, self->enc_in_port, info)) {
775       GST_ERROR_OBJECT (self, "Subclass failed to set the new format");
776       return FALSE;
777     }
778   }
779
780   GST_DEBUG_OBJECT (self, "Updating outport port definition");
781   if (gst_omx_port_update_port_definition (self->enc_out_port,
782           NULL) != OMX_ErrorNone)
783     return FALSE;
784
785   GST_DEBUG_OBJECT (self, "Enabling component");
786   if (needs_disable) {
787     if (gst_omx_port_set_enabled (self->enc_in_port, TRUE) != OMX_ErrorNone)
788       return FALSE;
789     if (gst_omx_port_allocate_buffers (self->enc_in_port) != OMX_ErrorNone)
790       return FALSE;
791
792     if ((klass->cdata.hacks & GST_OMX_HACK_NO_DISABLE_OUTPORT)) {
793       if (gst_omx_port_set_enabled (self->enc_out_port, TRUE) != OMX_ErrorNone)
794         return FALSE;
795       if (gst_omx_port_allocate_buffers (self->enc_out_port) != OMX_ErrorNone)
796         return FALSE;
797
798       if (gst_omx_port_wait_enabled (self->enc_out_port,
799               5 * GST_SECOND) != OMX_ErrorNone)
800         return FALSE;
801     }
802
803     if (gst_omx_port_wait_enabled (self->enc_in_port,
804             5 * GST_SECOND) != OMX_ErrorNone)
805       return FALSE;
806     if (gst_omx_port_mark_reconfigured (self->enc_in_port) != OMX_ErrorNone)
807       return FALSE;
808   } else {
809     if (!(klass->cdata.hacks & GST_OMX_HACK_NO_DISABLE_OUTPORT)) {
810       /* Disable output port */
811       if (gst_omx_port_set_enabled (self->enc_out_port, FALSE) != OMX_ErrorNone)
812         return FALSE;
813
814       if (gst_omx_port_wait_enabled (self->enc_out_port,
815               1 * GST_SECOND) != OMX_ErrorNone)
816         return FALSE;
817
818       if (gst_omx_component_set_state (self->enc,
819               OMX_StateIdle) != OMX_ErrorNone)
820         return FALSE;
821
822       /* Need to allocate buffers to reach Idle state */
823       if (gst_omx_port_allocate_buffers (self->enc_in_port) != OMX_ErrorNone)
824         return FALSE;
825     } else {
826       if (gst_omx_component_set_state (self->enc,
827               OMX_StateIdle) != OMX_ErrorNone)
828         return FALSE;
829
830       /* Need to allocate buffers to reach Idle state */
831       if (gst_omx_port_allocate_buffers (self->enc_in_port) != OMX_ErrorNone)
832         return FALSE;
833       if (gst_omx_port_allocate_buffers (self->enc_out_port) != OMX_ErrorNone)
834         return FALSE;
835     }
836
837     if (gst_omx_component_get_state (self->enc,
838             GST_CLOCK_TIME_NONE) != OMX_StateIdle)
839       return FALSE;
840
841     if (gst_omx_component_set_state (self->enc,
842             OMX_StateExecuting) != OMX_ErrorNone)
843       return FALSE;
844
845     if (gst_omx_component_get_state (self->enc,
846             GST_CLOCK_TIME_NONE) != OMX_StateExecuting)
847       return FALSE;
848   }
849
850   /* Unset flushing to allow ports to accept data again */
851   gst_omx_port_set_flushing (self->enc_in_port, 5 * GST_SECOND, FALSE);
852   gst_omx_port_set_flushing (self->enc_out_port, 5 * GST_SECOND, FALSE);
853
854   if (gst_omx_component_get_last_error (self->enc) != OMX_ErrorNone) {
855     GST_ERROR_OBJECT (self, "Component in error state: %s (0x%08x)",
856         gst_omx_component_get_last_error_string (self->enc),
857         gst_omx_component_get_last_error (self->enc));
858     return FALSE;
859   }
860
861   /* Start the srcpad loop again */
862   GST_DEBUG_OBJECT (self, "Starting task again");
863   self->downstream_flow_ret = GST_FLOW_OK;
864   gst_pad_start_task (GST_AUDIO_ENCODER_SRC_PAD (self),
865       (GstTaskFunction) gst_omx_audio_enc_loop, encoder, NULL);
866
867   return TRUE;
868 }
869
870 static void
871 gst_omx_audio_enc_flush (GstAudioEncoder * encoder)
872 {
873   GstOMXAudioEnc *self;
874
875   self = GST_OMX_AUDIO_ENC (encoder);
876
877   GST_DEBUG_OBJECT (self, "Resetting encoder");
878
879   gst_omx_port_set_flushing (self->enc_in_port, 5 * GST_SECOND, TRUE);
880   gst_omx_port_set_flushing (self->enc_out_port, 5 * GST_SECOND, TRUE);
881
882   /* Wait until the srcpad loop is finished */
883   GST_AUDIO_ENCODER_STREAM_UNLOCK (self);
884   GST_PAD_STREAM_LOCK (GST_AUDIO_ENCODER_SRC_PAD (self));
885   GST_PAD_STREAM_UNLOCK (GST_AUDIO_ENCODER_SRC_PAD (self));
886   GST_AUDIO_ENCODER_STREAM_LOCK (self);
887
888   gst_omx_port_set_flushing (self->enc_in_port, 5 * GST_SECOND, FALSE);
889   gst_omx_port_set_flushing (self->enc_out_port, 5 * GST_SECOND, FALSE);
890   gst_omx_port_populate (self->enc_out_port);
891
892   /* Start the srcpad loop again */
893   self->last_upstream_ts = 0;
894   self->downstream_flow_ret = GST_FLOW_OK;
895   self->started = FALSE;
896   gst_pad_start_task (GST_AUDIO_ENCODER_SRC_PAD (self),
897       (GstTaskFunction) gst_omx_audio_enc_loop, encoder, NULL);
898 }
899
900 static GstFlowReturn
901 gst_omx_audio_enc_handle_frame (GstAudioEncoder * encoder, GstBuffer * inbuf)
902 {
903   GstOMXAcquireBufferReturn acq_ret = GST_OMX_ACQUIRE_BUFFER_ERROR;
904   GstOMXAudioEnc *self;
905   GstOMXPort *port;
906   GstOMXBuffer *buf;
907   gsize size;
908   guint offset = 0;
909   GstClockTime timestamp, duration, timestamp_offset = 0;
910   OMX_ERRORTYPE err;
911
912   self = GST_OMX_AUDIO_ENC (encoder);
913
914   if (self->downstream_flow_ret != GST_FLOW_OK) {
915     return self->downstream_flow_ret;
916   }
917
918   if (inbuf == NULL)
919     return gst_omx_audio_enc_drain (self);
920
921   GST_DEBUG_OBJECT (self, "Handling frame");
922
923   timestamp = GST_BUFFER_TIMESTAMP (inbuf);
924   duration = GST_BUFFER_DURATION (inbuf);
925
926   port = self->enc_in_port;
927
928   size = gst_buffer_get_size (inbuf);
929   while (offset < size) {
930     /* Make sure to release the base class stream lock, otherwise
931      * _loop() can't call _finish_frame() and we might block forever
932      * because no input buffers are released */
933     GST_AUDIO_ENCODER_STREAM_UNLOCK (self);
934     acq_ret = gst_omx_port_acquire_buffer (port, &buf);
935
936     if (acq_ret == GST_OMX_ACQUIRE_BUFFER_ERROR) {
937       GST_AUDIO_ENCODER_STREAM_LOCK (self);
938       goto component_error;
939     } else if (acq_ret == GST_OMX_ACQUIRE_BUFFER_FLUSHING) {
940       GST_AUDIO_ENCODER_STREAM_LOCK (self);
941       goto flushing;
942     } else if (acq_ret == GST_OMX_ACQUIRE_BUFFER_RECONFIGURE) {
943       /* Reallocate all buffers */
944       err = gst_omx_port_set_enabled (port, FALSE);
945       if (err != OMX_ErrorNone) {
946         GST_AUDIO_ENCODER_STREAM_LOCK (self);
947         goto reconfigure_error;
948       }
949
950       err = gst_omx_port_wait_buffers_released (port, 5 * GST_SECOND);
951       if (err != OMX_ErrorNone) {
952         GST_AUDIO_ENCODER_STREAM_LOCK (self);
953         goto reconfigure_error;
954       }
955
956       err = gst_omx_port_deallocate_buffers (port);
957       if (err != OMX_ErrorNone) {
958         GST_AUDIO_ENCODER_STREAM_LOCK (self);
959         goto reconfigure_error;
960       }
961
962       err = gst_omx_port_wait_enabled (port, 1 * GST_SECOND);
963       if (err != OMX_ErrorNone) {
964         GST_AUDIO_ENCODER_STREAM_LOCK (self);
965         goto reconfigure_error;
966       }
967
968       err = gst_omx_port_set_enabled (port, TRUE);
969       if (err != OMX_ErrorNone) {
970         GST_AUDIO_ENCODER_STREAM_LOCK (self);
971         goto reconfigure_error;
972       }
973
974       err = gst_omx_port_allocate_buffers (port);
975       if (err != OMX_ErrorNone) {
976         GST_AUDIO_ENCODER_STREAM_LOCK (self);
977         goto reconfigure_error;
978       }
979
980       err = gst_omx_port_wait_enabled (port, 5 * GST_SECOND);
981       if (err != OMX_ErrorNone) {
982         GST_AUDIO_ENCODER_STREAM_LOCK (self);
983         goto reconfigure_error;
984       }
985
986       err = gst_omx_port_mark_reconfigured (port);
987       if (err != OMX_ErrorNone) {
988         GST_AUDIO_ENCODER_STREAM_LOCK (self);
989         goto reconfigure_error;
990       }
991
992       /* Now get a new buffer and fill it */
993       GST_AUDIO_ENCODER_STREAM_LOCK (self);
994       continue;
995     }
996     GST_AUDIO_ENCODER_STREAM_LOCK (self);
997
998     g_assert (acq_ret == GST_OMX_ACQUIRE_BUFFER_OK && buf != NULL);
999
1000     if (self->downstream_flow_ret != GST_FLOW_OK) {
1001       gst_omx_port_release_buffer (port, buf);
1002       return self->downstream_flow_ret;
1003     }
1004
1005     if (buf->omx_buf->nAllocLen - buf->omx_buf->nOffset <= 0) {
1006       gst_omx_port_release_buffer (port, buf);
1007       goto full_buffer;
1008     }
1009
1010     GST_DEBUG_OBJECT (self, "Handling frame at offset %d", offset);
1011
1012     /* Copy the buffer content in chunks of size as requested
1013      * by the port */
1014     buf->omx_buf->nFilledLen =
1015         MIN (size - offset, buf->omx_buf->nAllocLen - buf->omx_buf->nOffset);
1016     gst_buffer_extract (inbuf, offset,
1017         buf->omx_buf->pBuffer + buf->omx_buf->nOffset,
1018         buf->omx_buf->nFilledLen);
1019
1020     /* Interpolate timestamps if we're passing the buffer
1021      * in multiple chunks */
1022     if (offset != 0 && duration != GST_CLOCK_TIME_NONE) {
1023       timestamp_offset = gst_util_uint64_scale (offset, duration, size);
1024     }
1025
1026     if (timestamp != GST_CLOCK_TIME_NONE) {
1027       buf->omx_buf->nTimeStamp =
1028           gst_util_uint64_scale (timestamp + timestamp_offset,
1029           OMX_TICKS_PER_SECOND, GST_SECOND);
1030       self->last_upstream_ts = timestamp + timestamp_offset;
1031     }
1032     if (duration != GST_CLOCK_TIME_NONE) {
1033       buf->omx_buf->nTickCount =
1034           gst_util_uint64_scale (buf->omx_buf->nFilledLen, duration, size);
1035       buf->omx_buf->nTickCount =
1036           gst_util_uint64_scale (buf->omx_buf->nTickCount,
1037           OMX_TICKS_PER_SECOND, GST_SECOND);
1038       self->last_upstream_ts += duration;
1039     }
1040
1041     offset += buf->omx_buf->nFilledLen;
1042     self->started = TRUE;
1043     err = gst_omx_port_release_buffer (port, buf);
1044     if (err != OMX_ErrorNone)
1045       goto release_error;
1046   }
1047
1048   GST_DEBUG_OBJECT (self, "Passed frame to component");
1049
1050   return self->downstream_flow_ret;
1051
1052 full_buffer:
1053   {
1054     GST_ELEMENT_ERROR (self, LIBRARY, FAILED, (NULL),
1055         ("Got OpenMAX buffer with no free space (%p, %u/%u)", buf,
1056             (guint) buf->omx_buf->nOffset, (guint) buf->omx_buf->nAllocLen));
1057     return GST_FLOW_ERROR;
1058   }
1059 component_error:
1060   {
1061     GST_ELEMENT_ERROR (self, LIBRARY, FAILED, (NULL),
1062         ("OpenMAX component in error state %s (0x%08x)",
1063             gst_omx_component_get_last_error_string (self->enc),
1064             gst_omx_component_get_last_error (self->enc)));
1065     return GST_FLOW_ERROR;
1066   }
1067
1068 flushing:
1069   {
1070     GST_DEBUG_OBJECT (self, "Flushing -- returning FLUSHING");
1071     return GST_FLOW_FLUSHING;
1072   }
1073 reconfigure_error:
1074   {
1075     GST_ELEMENT_ERROR (self, LIBRARY, SETTINGS, (NULL),
1076         ("Unable to reconfigure input port"));
1077     return GST_FLOW_ERROR;
1078   }
1079 release_error:
1080   {
1081     GST_ELEMENT_ERROR (self, LIBRARY, SETTINGS, (NULL),
1082         ("Failed to relase input buffer to component: %s (0x%08x)",
1083             gst_omx_error_to_string (err), err));
1084     return GST_FLOW_ERROR;
1085   }
1086 }
1087
1088 static GstFlowReturn
1089 gst_omx_audio_enc_drain (GstOMXAudioEnc * self)
1090 {
1091   GstOMXAudioEncClass *klass;
1092   GstOMXBuffer *buf;
1093   GstOMXAcquireBufferReturn acq_ret;
1094   OMX_ERRORTYPE err;
1095
1096   GST_DEBUG_OBJECT (self, "Draining component");
1097
1098   klass = GST_OMX_AUDIO_ENC_GET_CLASS (self);
1099
1100   if (!self->started) {
1101     GST_DEBUG_OBJECT (self, "Component not started yet");
1102     return GST_FLOW_OK;
1103   }
1104   self->started = FALSE;
1105
1106   if ((klass->cdata.hacks & GST_OMX_HACK_NO_EMPTY_EOS_BUFFER)) {
1107     GST_WARNING_OBJECT (self, "Component does not support empty EOS buffers");
1108     return GST_FLOW_OK;
1109   }
1110
1111   /* Make sure to release the base class stream lock, otherwise
1112    * _loop() can't call _finish_frame() and we might block forever
1113    * because no input buffers are released */
1114   GST_AUDIO_ENCODER_STREAM_UNLOCK (self);
1115
1116   /* Send an EOS buffer to the component and let the base
1117    * class drop the EOS event. We will send it later when
1118    * the EOS buffer arrives on the output port. */
1119   acq_ret = gst_omx_port_acquire_buffer (self->enc_in_port, &buf);
1120   if (acq_ret != GST_OMX_ACQUIRE_BUFFER_OK) {
1121     GST_AUDIO_ENCODER_STREAM_LOCK (self);
1122     GST_ERROR_OBJECT (self, "Failed to acquire buffer for draining: %d",
1123         acq_ret);
1124     return GST_FLOW_ERROR;
1125   }
1126
1127   g_mutex_lock (&self->drain_lock);
1128   self->draining = TRUE;
1129   buf->omx_buf->nFilledLen = 0;
1130   buf->omx_buf->nTimeStamp =
1131       gst_util_uint64_scale (self->last_upstream_ts, OMX_TICKS_PER_SECOND,
1132       GST_SECOND);
1133   buf->omx_buf->nTickCount = 0;
1134   buf->omx_buf->nFlags |= OMX_BUFFERFLAG_EOS;
1135   err = gst_omx_port_release_buffer (self->enc_in_port, buf);
1136   if (err != OMX_ErrorNone) {
1137     GST_ERROR_OBJECT (self, "Failed to drain component: %s (0x%08x)",
1138         gst_omx_error_to_string (err), err);
1139     GST_AUDIO_ENCODER_STREAM_LOCK (self);
1140     return GST_FLOW_ERROR;
1141   }
1142   GST_DEBUG_OBJECT (self, "Waiting until component is drained");
1143   g_cond_wait (&self->drain_cond, &self->drain_lock);
1144   GST_DEBUG_OBJECT (self, "Drained component");
1145   g_mutex_unlock (&self->drain_lock);
1146   GST_AUDIO_ENCODER_STREAM_LOCK (self);
1147
1148   self->started = FALSE;
1149
1150   return GST_FLOW_OK;
1151 }