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