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
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
|
/*
* gsttiaudenc1.c
*
* This file defines the "TIAudenc1" element, which encodes an xDM 1.x audio
* stream.
*
* Example usage (Encoding a PCM file with playback):
* gst-launch filesrc location=<audio file> ! audio/x-raw-int,
* endianness=1234, signed=true, width=16, depth=16, rate=44100,
* channels=2 ! tee name=t ! queue ! TIAudenc1 bitrate=64000
* engineName="<engine name>" codecName="<codec name>" ! filesink
* location="test.aac" t. ! queue ! alsasink sync=false
*
* Example usage (Encoding from alsasrc with playback):
* gst-launch ${DEBUG} alsasrc num-buffers=2000 ! audio/x-raw-int,
* endianness=1234, signed=true, width=16, depth=16, rate=44100,
* channels=2 ! tee name=t ! queue ! TIAudenc1 bitrate=64000
* engineName=encode codecName=aacheenc ! filesink location="test.aac"
* t. ! queue ! alsasink sync=false
*
* Notes:
* * This element currently assumes that the codec produces AAC-HE output.
*
* Original Author:
* Chase Maupin, Texas Instruments, Inc.
*
* Based on TIAuddec1 from:
* Brijesh Singh, Texas Instruments, Inc.
* Diego Dompe, RidgeRun
*
* Copyright (C) 2008-2010 Texas Instruments Incorporated - http://www.ti.com/
* Copyright (C) 2009 RidgeRun
*
* This program 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 program is distributed #as is# WITHOUT ANY WARRANTY of any kind,
* whether express or implied; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
*/
#ifdef HAVE_CONFIG_H
# include <config.h>
#endif
#include <stdio.h>
#include <string.h>
#include <gst/gst.h>
#include <gst/audio/audio.h>
#include <pthread.h>
#include <ti/sdo/dmai/Dmai.h>
#include <ti/sdo/dmai/Buffer.h>
#include <ti/sdo/dmai/ce/Aenc1.h>
#include "gsttiaudenc1.h"
#include "gsttidmaibuffertransport.h"
#include "gstticodecs.h"
#include "gsttithreadprops.h"
#include "gsttiquicktime_aac.h"
#include "gstticommonutils.h"
/* Enclare variable used to categorize GST_LOG output */
GST_DEBUG_CATEGORY_STATIC (gst_tiaudenc1_debug);
#define GST_CAT_DEFAULT gst_tiaudenc1_debug
/* define property defaults */
#define DEFAULT_NUM_CHANNELES 2
#define DEFAULT_CODEC_NAME "unspecified"
#define DEFAULT_BITRATE 64000
#define DEFAULT_SAMPLEFREQ 44100
#define DEFAULT_NUMOUTPUT_BUFS 3
#define DEFAULT_DISPLAY_BUFFER FALSE
#define DEFAULT_GENTIMESTAMPS TRUE
/* define platform specific defaults */
#if defined(Platform_dm6446)
#define DEFAULT_ENGINE_NAME "encode"
#else
#define DEFAULT_ENGINE_NAME "codecServer"
#endif
/* Element property identifiers */
enum
{
PROP_0,
PROP_ENGINE_NAME, /* engineName (string) */
PROP_CODEC_NAME, /* codecName (string) */
PROP_NUM_CHANNELS, /* numChannels (int) */
PROP_BITRATE, /* bitrate (int) */
PROP_SAMPLEFREQ, /* sample frequency (int) */
PROP_NUM_OUTPUT_BUFS, /* numOutputBufs (int) */
PROP_DISPLAY_BUFFER, /* displayBuffer (boolean) */
PROP_GEN_TIMESTAMPS /* genTimeStamps (boolean) */
};
/* Define sink (input) pad capabilities. Currently, RAW is
* supported.
*/
static GstStaticPadTemplate sink_factory = GST_STATIC_PAD_TEMPLATE(
"sink",
GST_PAD_SINK,
GST_PAD_ALWAYS,
GST_STATIC_CAPS
("audio/x-raw-int, "
"endianness = (int) " G_STRINGIFY (G_BYTE_ORDER) ", "
"signed = (boolean)true, "
"width = (int) 16, depth = (int) 16, "
"rate = (int) {16000, 22050, 24000, 32000, 44100, 48000 }, "
"channels = (int) [ 1, 2 ]")
);
/* Define source (output) pad capabilities. Currently, AAC is supported. */
static GstStaticPadTemplate src_factory = GST_STATIC_PAD_TEMPLATE(
"src",
GST_PAD_SRC,
GST_PAD_ALWAYS,
GST_STATIC_CAPS
("audio/mpeg, "
"mpegversion = (int) { 4 }")
);
/* Enclare a global pointer to our element base class */
static GstElementClass *parent_class = NULL;
/* Static Function Enclarations */
static void
gst_tiaudenc1_base_init(gpointer g_class);
static void
gst_tiaudenc1_class_init(GstTIAudenc1Class *g_class);
static void
gst_tiaudenc1_init(GstTIAudenc1 *object, GstTIAudenc1Class *g_class);
static void
gst_tiaudenc1_set_property (GObject *object, guint prop_id,
const GValue *value, GParamSpec *pspec);
static void
gst_tiaudenc1_get_property (GObject *object, guint prop_id, GValue *value,
GParamSpec *pspec);
static gboolean
gst_tiaudenc1_set_sink_caps(GstPad *pad, GstCaps *caps);
static gboolean
gst_tiaudenc1_set_source_caps(GstTIAudenc1 *audenc1);
static gboolean
gst_tiaudenc1_sink_event(GstPad *pad, GstEvent *event);
static GstFlowReturn
gst_tiaudenc1_chain(GstPad *pad, GstBuffer *buf);
static gboolean
gst_tiaudenc1_init_audio(GstTIAudenc1 *audenc1);
static gboolean
gst_tiaudenc1_exit_audio(GstTIAudenc1 *audenc1);
static GstStateChangeReturn
gst_tiaudenc1_change_state(GstElement *element, GstStateChange transition);
static void*
gst_tiaudenc1_encode_thread(void *arg);
static void
gst_tiaudenc1_drain_pipeline(GstTIAudenc1 *audenc1);
static gboolean
gst_tiaudenc1_codec_start (GstTIAudenc1 *audenc);
static gboolean
gst_tiaudenc1_codec_stop (GstTIAudenc1 *audenc1);
static void
gst_tiaudenc1_init_env(GstTIAudenc1 *audenc1);
/******************************************************************************
* gst_tiaudenc1_class_init_trampoline
* Boiler-plate function auto-generated by "make_element" script.
******************************************************************************/
static void gst_tiaudenc1_class_init_trampoline(gpointer g_class, gpointer data)
{
parent_class = (GstElementClass*) g_type_class_peek_parent(g_class);
gst_tiaudenc1_class_init((GstTIAudenc1Class*)g_class);
}
/******************************************************************************
* gst_tiaudenc1_get_type
* Boiler-plate function auto-generated by "make_element" script.
* Defines function pointers for initialization routines for this element.
******************************************************************************/
GType gst_tiaudenc1_get_type(void)
{
static GType object_type = 0;
if (G_UNLIKELY(object_type == 0)) {
static const GTypeInfo object_info = {
sizeof(GstTIAudenc1Class),
gst_tiaudenc1_base_init,
NULL,
gst_tiaudenc1_class_init_trampoline,
NULL,
NULL,
sizeof(GstTIAudenc1),
0,
(GInstanceInitFunc) gst_tiaudenc1_init
};
object_type = g_type_register_static((gst_element_get_type()),
"GstTIAudenc1", &object_info, (GTypeFlags)0);
/* Initialize GST_LOG for this object */
GST_DEBUG_CATEGORY_INIT(gst_tiaudenc1_debug, "TIAudenc1", 0,
"TI xDM 1.x Audio Encoder");
GST_LOG("initialized get_type\n");
}
return object_type;
};
/******************************************************************************
* gst_tiaudenc1_base_init
* Boiler-plate function auto-generated by "make_element" script.
* Initializes element base class.
******************************************************************************/
static void gst_tiaudenc1_base_init(gpointer gclass)
{
static GstElementDetails element_details = {
"TI xDM 1.x Audio Encoder",
"Codec/Encoder/Audio",
"Encodes audio using an xDM 1.x-based codec",
"Chase Maupin; Texas Instruments, Inc."
};
GstElementClass *element_class = GST_ELEMENT_CLASS(gclass);
gst_element_class_add_pad_template(element_class,
gst_static_pad_template_get (&src_factory));
gst_element_class_add_pad_template(element_class,
gst_static_pad_template_get (&sink_factory));
gst_element_class_set_details(element_class, &element_details);
}
/******************************************************************************
* gst_tiaudenc1_class_init
* Boiler-plate function auto-generated by "make_element" script.
* Initializes the TIAudenc1 class.
******************************************************************************/
static void gst_tiaudenc1_class_init(GstTIAudenc1Class *klass)
{
GObjectClass *gobject_class;
GstElementClass *gstelement_class;
gobject_class = (GObjectClass*) klass;
gstelement_class = (GstElementClass*) klass;
gobject_class->set_property = gst_tiaudenc1_set_property;
gobject_class->get_property = gst_tiaudenc1_get_property;
gstelement_class->change_state = gst_tiaudenc1_change_state;
g_object_class_install_property(gobject_class, PROP_ENGINE_NAME,
g_param_spec_string("engineName", "Engine Name",
"Engine name used by Codec Engine", DEFAULT_ENGINE_NAME,
G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
g_object_class_install_property(gobject_class, PROP_CODEC_NAME,
g_param_spec_string("codecName", "Codec Name", "Name of audio codec",
DEFAULT_CODEC_NAME, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
g_object_class_install_property(gobject_class, PROP_NUM_CHANNELS,
g_param_spec_int("numChannels",
"Number of Channels",
"Number of audio channels",
1, G_MAXINT32, DEFAULT_NUM_CHANNELES, G_PARAM_READWRITE));
g_object_class_install_property(gobject_class, PROP_BITRATE,
g_param_spec_int("bitrate", "Bitrate (bps)", "Bitrate in bits/sec",
8000, 128000, DEFAULT_BITRATE, G_PARAM_READWRITE));
g_object_class_install_property(gobject_class, PROP_SAMPLEFREQ,
g_param_spec_int("samplefreq", "samplefreq (KHz)",
"Sampe Frequency in KHz", 16000, 48000, DEFAULT_SAMPLEFREQ, G_PARAM_READWRITE));
g_object_class_install_property(gobject_class, PROP_NUM_OUTPUT_BUFS,
g_param_spec_int("numOutputBufs",
"Number of Ouput Buffers",
"Number of output buffers to allocate for codec",
2, G_MAXINT32, DEFAULT_NUMOUTPUT_BUFS, G_PARAM_READWRITE));
g_object_class_install_property(gobject_class, PROP_DISPLAY_BUFFER,
g_param_spec_boolean("displayBuffer", "Display Buffer",
"Display circular buffer status while processing",
DEFAULT_DISPLAY_BUFFER, G_PARAM_READWRITE));
g_object_class_install_property(gobject_class, PROP_GEN_TIMESTAMPS,
g_param_spec_boolean("genTimeStamps", "Generate Time Stamps",
"Set timestamps on output buffers",
DEFAULT_GENTIMESTAMPS, G_PARAM_READWRITE));
}
/******************************************************************************
* gst_tiaudenc1_init_env
* Initialize element property default by reading environment variables.
*****************************************************************************/
static void gst_tiaudenc1_init_env(GstTIAudenc1 *audenc1)
{
GST_LOG("gst_tiaudenc1_init_env - begin");
if (gst_ti_env_is_defined("GST_TI_TIAudenc1_engineName")) {
audenc1->engineName = gst_ti_env_get_string("GST_TI_TIAudenc1_engineName");
GST_LOG("Setting engineName=%s\n", audenc1->engineName);
}
if (gst_ti_env_is_defined("GST_TI_TIAudenc1_codecName")) {
audenc1->codecName = gst_ti_env_get_string("GST_TI_TIAudenc1_codecName");
GST_LOG("Setting codecName=%s\n", audenc1->codecName);
}
if (gst_ti_env_is_defined("GST_TI_TIAudenc1_numOutputBufs")) {
audenc1->numOutputBufs =
gst_ti_env_get_int("GST_TI_TIAudenc1_numOutputBufs");
GST_LOG("Setting numOutputBufs=%ld\n", audenc1->numOutputBufs);
}
if (gst_ti_env_is_defined("GST_TI_TIAudenc1_bitrate")) {
audenc1->bitrate = gst_ti_env_get_int("GST_TI_TIAudenc1_bitrate");
GST_LOG("Setting bitrate=%d\n", audenc1->bitrate);
}
if (gst_ti_env_is_defined("GST_TI_TIAudenc1_channels")) {
audenc1->channels = gst_ti_env_get_int("GST_TI_TIAudenc1_channels");
GST_LOG("Setting channels=%d\n", audenc1->channels);
}
if (gst_ti_env_is_defined("GST_TI_TIAudenc1_samplefreq")) {
audenc1->samplefreq = gst_ti_env_get_int("GST_TI_TIAudenc1_samplefreq");
GST_LOG("Setting samplefreq=%d\n", audenc1->samplefreq);
}
if (gst_ti_env_is_defined("GST_TI_TIAudenc1_displayBuffer")) {
audenc1->displayBuffer =
gst_ti_env_get_boolean("GST_TI_TIAudenc1_displayBuffer");
GST_LOG("Setting displayBuffer=%s\n",
audenc1->displayBuffer ? "TRUE" : "FALSE");
}
if (gst_ti_env_is_defined("GST_TI_TIAudenc1_genTimeStamps")) {
audenc1->genTimeStamps =
gst_ti_env_get_boolean("GST_TI_TIAudenc1_genTimeStamps");
GST_LOG("Setting genTimeStamps =%s\n",
audenc1->genTimeStamps ? "TRUE" : "FALSE");
}
GST_LOG("gst_tiaudenc1_init_env - end");
}
/******************************************************************************
* gst_tiaudenc1_init
* Initializes a new element instance, instantiates pads and sets the pad
* callback functions.
******************************************************************************/
static void gst_tiaudenc1_init(GstTIAudenc1 *audenc1, GstTIAudenc1Class *gclass)
{
/* Instantiate RAW audio sink pad.
*
* Fixate on our static template caps instead of writing a getcaps
* function, which is overkill for this element.
*/
audenc1->sinkpad =
gst_pad_new_from_static_template(&sink_factory, "sink");
gst_pad_set_setcaps_function(
audenc1->sinkpad, GST_DEBUG_FUNCPTR(gst_tiaudenc1_set_sink_caps));
gst_pad_set_event_function(
audenc1->sinkpad, GST_DEBUG_FUNCPTR(gst_tiaudenc1_sink_event));
gst_pad_set_chain_function(
audenc1->sinkpad, GST_DEBUG_FUNCPTR(gst_tiaudenc1_chain));
gst_pad_fixate_caps(audenc1->sinkpad,
gst_caps_make_writable(
gst_caps_copy(gst_pad_get_pad_template_caps(audenc1->sinkpad))));
/* Instantiate encoded audio source pad.
*
* Fixate on our static template caps instead of writing a getcaps
* function, which is overkill for this element.
*/
audenc1->srcpad =
gst_pad_new_from_static_template(&src_factory, "src");
gst_pad_fixate_caps(audenc1->srcpad,
gst_caps_make_writable(
gst_caps_copy(gst_pad_get_pad_template_caps(audenc1->srcpad))));
/* Add pads to TIAudenc1 element */
gst_element_add_pad(GST_ELEMENT(audenc1), audenc1->sinkpad);
gst_element_add_pad(GST_ELEMENT(audenc1), audenc1->srcpad);
/* Initialize TIAudenc1 state */
g_object_set(audenc1, "engineName", DEFAULT_ENGINE_NAME, (gchar*)NULL);
g_object_set(audenc1, "codecName", DEFAULT_CODEC_NAME, (gchar*)NULL);
audenc1->displayBuffer = DEFAULT_DISPLAY_BUFFER;
audenc1->genTimeStamps = DEFAULT_GENTIMESTAMPS;
audenc1->hEngine = NULL;
audenc1->hAe = NULL;
audenc1->channels = DEFAULT_NUM_CHANNELES;
audenc1->bitrate = DEFAULT_BITRATE;
audenc1->samplefreq = DEFAULT_SAMPLEFREQ;
audenc1->drainingEOS = FALSE;
audenc1->threadStatus = 0UL;
audenc1->waitOnEncodeThread = NULL;
audenc1->waitOnEncodeDrain = NULL;
audenc1->numOutputBufs = DEFAULT_NUMOUTPUT_BUFS;
audenc1->hOutBufTab = NULL;
audenc1->circBuf = NULL;
gst_tiaudenc1_init_env(audenc1);
}
/******************************************************************************
* gst_tiaudenc1_set_property
* Set element properties when requested.
******************************************************************************/
static void gst_tiaudenc1_set_property(GObject *object, guint prop_id,
const GValue *value, GParamSpec *pspec)
{
GstTIAudenc1 *audenc1 = GST_TIAUDENC1(object);
GST_LOG("begin set_property\n");
switch (prop_id) {
case PROP_ENGINE_NAME:
if (audenc1->engineName) {
g_free((gpointer)audenc1->engineName);
}
audenc1->engineName =
(gchar*)g_malloc(strlen(g_value_get_string(value)) + 1);
strcpy((gchar *)audenc1->engineName, g_value_get_string(value));
GST_LOG("setting \"engineName\" to \"%s\"\n", audenc1->engineName);
break;
case PROP_CODEC_NAME:
if (audenc1->codecName) {
g_free((gpointer)audenc1->codecName);
}
audenc1->codecName =
(gchar*)g_malloc(strlen(g_value_get_string(value)) + 1);
strcpy((gchar*)audenc1->codecName, g_value_get_string(value));
GST_LOG("setting \"codecName\" to \"%s\"\n", audenc1->codecName);
break;
case PROP_NUM_CHANNELS:
audenc1->channels = g_value_get_int(value);
GST_LOG("setting \"numChannels\" to \"%d\"\n",
audenc1->channels);
break;
case PROP_BITRATE:
audenc1->bitrate = g_value_get_int(value);
GST_LOG("setting \"bitrate\" to \"%d\"\n",
audenc1->bitrate);
break;
case PROP_SAMPLEFREQ:
audenc1->samplefreq = g_value_get_int(value);
GST_LOG("setting \"samplefreq\" to \"%d\"\n",
audenc1->samplefreq);
break;
case PROP_NUM_OUTPUT_BUFS:
audenc1->numOutputBufs = g_value_get_int(value);
GST_LOG("setting \"numOutputBufs\" to \"%ld\"\n",
audenc1->numOutputBufs);
break;
case PROP_DISPLAY_BUFFER:
audenc1->displayBuffer = g_value_get_boolean(value);
GST_LOG("setting \"displayBuffer\" to \"%s\"\n",
audenc1->displayBuffer ? "TRUE" : "FALSE");
break;
case PROP_GEN_TIMESTAMPS:
audenc1->genTimeStamps = g_value_get_boolean(value);
GST_LOG("setting \"genTimeStamps\" to \"%s\"\n",
audenc1->genTimeStamps ? "TRUE" : "FALSE");
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
break;
}
GST_LOG("end set_property\n");
}
/******************************************************************************
* gst_tiaudenc1_get_property
* Return values for requested element property.
******************************************************************************/
static void gst_tiaudenc1_get_property(GObject *object, guint prop_id,
GValue *value, GParamSpec *pspec)
{
GstTIAudenc1 *audenc1 = GST_TIAUDENC1(object);
GST_LOG("begin get_property\n");
switch (prop_id) {
case PROP_ENGINE_NAME:
g_value_set_string(value, audenc1->engineName);
break;
case PROP_CODEC_NAME:
g_value_set_string(value, audenc1->codecName);
break;
case PROP_BITRATE:
g_value_set_int(value, audenc1->bitrate);
break;
case PROP_SAMPLEFREQ:
g_value_set_int(value, audenc1->samplefreq);
break;
case PROP_NUM_OUTPUT_BUFS:
g_value_set_int(value, audenc1->numOutputBufs);
break;
case PROP_DISPLAY_BUFFER:
g_value_set_boolean(value, audenc1->displayBuffer);
break;
case PROP_GEN_TIMESTAMPS:
g_value_set_boolean(value, audenc1->genTimeStamps);
break;
case PROP_NUM_CHANNELS:
g_value_set_int(value, audenc1->channels);
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
break;
}
GST_LOG("end get_property\n");
}
/******************************************************************************
* gst_tiaudenc1_set_sink_caps
* Negotiate our sink pad capabilities.
******************************************************************************/
static gboolean gst_tiaudenc1_set_sink_caps(GstPad *pad, GstCaps *caps)
{
GstTIAudenc1 *audenc1;
GstStructure *capStruct;
const gchar *mime;
char *string;
gint samplefreq;
gint channels;
audenc1 = GST_TIAUDENC1(gst_pad_get_parent(pad));
capStruct = gst_caps_get_structure(caps, 0);
mime = gst_structure_get_name(capStruct);
string = gst_caps_to_string(caps);
GST_INFO("requested sink caps: %s", string);
g_free(string);
/* Generic Audio Properties */
if (!strncmp(mime, "audio/", 6)) {
if (!gst_structure_get_int(capStruct, "rate", &samplefreq)) {
samplefreq = 0;
}
if (!gst_structure_get_int(capStruct, "channels", &channels)) {
channels = 0;
}
}
/* Mime type not supported */
else {
GST_ELEMENT_ERROR(audenc1, STREAM, NOT_IMPLEMENTED, ("stream type not supported"), (NULL));
gst_object_unref(audenc1);
return FALSE;
}
/* Shut-down any running audio encoder */
if (!gst_tiaudenc1_exit_audio(audenc1)) {
gst_object_unref(audenc1);
return FALSE;
}
/* Configure the element to use the detected samplefreq and channels, unless
* they have been set using the set_property function.
*/
if (!audenc1->samplefreq) {
audenc1->samplefreq = samplefreq;
}
if (!audenc1->channels) {
audenc1->channels = channels;
}
/* If we're still showing 0 channels, we were not able to determine the
* number of channels from the input stream. The channels will be
* set to the default in gst_tiaudenc1_codec_start so print a
* warning for now.
*/
if (audenc1->channels == 0) {
GST_WARNING("Could not determine the number of channels in the "
"input stream; Using default values.");
}
gst_object_unref(audenc1);
GST_LOG("sink caps negotiation successful\n");
return TRUE;
}
/******************************************************************************
* gst_tiaudenc1_set_source_caps
* Negotiate our source pad capabilities.
******************************************************************************/
static gboolean gst_tiaudenc1_set_source_caps(GstTIAudenc1* audenc1)
{
GstCaps *caps;
gboolean ret;
char *string;
GstTICodec *aacCodec = NULL;
aacCodec = gst_ticodec_get_codec("AAC Audio Encoder");
/* Create AAC source caps */
if (aacCodec && (!strcmp(aacCodec->CE_CodecName, audenc1->codecName))) {
caps =
gst_caps_new_simple ("audio/mpeg",
"mpegversion", G_TYPE_INT, 4,
"channels", G_TYPE_INT, audenc1->channels,
"rate", G_TYPE_INT, audenc1->samplefreq,
"bitrate", G_TYPE_INT, audenc1->bitrate,
NULL);
/* Set the source pad caps */
string = gst_caps_to_string(caps);
GST_INFO("setting source caps to AAC: %s", string);
g_free(string);
} else {
GST_ELEMENT_ERROR(audenc1, STREAM, CODEC_NOT_FOUND,
("Unknown codecName (%s). Failed to set src caps\n",
audenc1->codecName), (NULL));
return FALSE;
}
if (!gst_pad_set_caps(audenc1->srcpad, caps)) {
ret = FALSE;
}
gst_caps_unref(caps);
return ret;
}
/******************************************************************************
* gst_tiaudenc1_sink_event
* Perform event processing on the input stream. At the moment, this
* function isn't needed as this element doesn't currently perform any
* specialized event processing. We'll leave it in for now in case we need
* it later on.
******************************************************************************/
static gboolean gst_tiaudenc1_sink_event(GstPad *pad, GstEvent *event)
{
GstTIAudenc1 *audenc1;
gboolean ret;
audenc1 = GST_TIAUDENC1(GST_OBJECT_PARENT(pad));
GST_DEBUG("pad \"%s\" received: %s\n", GST_PAD_NAME(pad),
GST_EVENT_TYPE_NAME(event));
switch (GST_EVENT_TYPE(event)) {
case GST_EVENT_NEWSEGMENT:
/* Propagate NEWSEGMENT to downstream elements */
ret = gst_pad_push_event(audenc1->srcpad, event);
break;
case GST_EVENT_EOS:
/* end-of-stream: process any remaining encoded frame data */
GST_LOG("no more input; draining remaining encoded audio data\n");
if (!audenc1->drainingEOS) {
gst_tiaudenc1_drain_pipeline(audenc1);
}
/* Propagate EOS to downstream elements */
ret = gst_pad_push_event(audenc1->srcpad, event);
break;
case GST_EVENT_FLUSH_STOP:
ret = gst_pad_push_event(audenc1->srcpad, event);
break;
/* Unhandled events */
case GST_EVENT_BUFFERSIZE:
case GST_EVENT_CUSTOM_BOTH:
case GST_EVENT_CUSTOM_BOTH_OOB:
case GST_EVENT_CUSTOM_DOWNSTREAM:
case GST_EVENT_CUSTOM_DOWNSTREAM_OOB:
case GST_EVENT_CUSTOM_UPSTREAM:
case GST_EVENT_FLUSH_START:
case GST_EVENT_NAVIGATION:
case GST_EVENT_QOS:
case GST_EVENT_SEEK:
case GST_EVENT_TAG:
default:
ret = gst_pad_event_default(pad, event);
break;
}
return ret;
}
/******************************************************************************
* gst_tiaudenc1_chain
* This is the main processing routine. This function receives a buffer
* from the sink pad, processes it, and pushes the result to the source
* pad.
******************************************************************************/
static GstFlowReturn gst_tiaudenc1_chain(GstPad * pad, GstBuffer * buf)
{
GstTIAudenc1 *audenc1 = GST_TIAUDENC1(GST_OBJECT_PARENT(pad));
GstFlowReturn flow = GST_FLOW_OK;
gboolean checkResult;
/* If the encode thread aborted, signal it to let it know it's ok to
* shut down, and communicate the failure to the pipeline.
*/
if (gst_tithread_check_status(audenc1, TIThread_CODEC_ABORTED,
checkResult)) {
flow = GST_FLOW_UNEXPECTED;
goto exit;
}
/* If our engine handle is currently NULL, then either this is our first
* buffer or the upstream element has re-negotiated our capabilities which
* resulted in our engine being closed. In either case, we need to
* initialize (or re-initialize) our audio encoder to handle the new
* stream.
*/
if (audenc1->hEngine == NULL) {
if (!gst_tiaudenc1_init_audio(audenc1)) {
GST_ELEMENT_ERROR(audenc1, RESOURCE, FAILED,
("Unable to initialize audio\n"), (NULL));
flow = GST_FLOW_UNEXPECTED;
goto exit;
}
GST_TICIRCBUFFER_TIMESTAMP(audenc1->circBuf) =
GST_CLOCK_TIME_IS_VALID(GST_BUFFER_TIMESTAMP(buf)) ?
GST_BUFFER_TIMESTAMP(buf) : 0ULL;
}
/* Queue up the encoded data stream into a circular buffer */
if (!gst_ticircbuffer_queue_data(audenc1->circBuf, buf)) {
GST_ELEMENT_ERROR(audenc1, RESOURCE, WRITE,
("Failed to queue input buffer into circular buffer\n"), (NULL));
flow = GST_FLOW_UNEXPECTED;
goto exit;
}
exit:
gst_buffer_unref(buf);
return flow;
}
/******************************************************************************
* gst_tiaudenc1_init_audio
* Initialize or re-initializes the audio stream
******************************************************************************/
static gboolean gst_tiaudenc1_init_audio(GstTIAudenc1 * audenc1)
{
Rendezvous_Attrs rzvAttrs = Rendezvous_Attrs_DEFAULT;
struct sched_param schedParam;
pthread_attr_t attr;
GST_LOG("begin init_audio\n");
/* If audio has already been initialized, shut down previous encoder */
if (audenc1->hEngine) {
if (!gst_tiaudenc1_exit_audio(audenc1)) {
GST_ELEMENT_ERROR(audenc1, RESOURCE, FAILED,
("Failed to shut down existing audio encoder\n"), (NULL));
return FALSE;
}
}
/* Make sure we know what codec we're using */
if (!audenc1->engineName) {
GST_ELEMENT_ERROR(audenc1, RESOURCE, FAILED,
("Engine name not specified\n"), (NULL));
return FALSE;
}
if (!audenc1->codecName) {
GST_ELEMENT_ERROR(audenc1, RESOURCE, FAILED,
("Codec name not specified\n"), (NULL));
return FALSE;
}
/* Initialize thread status management */
audenc1->threadStatus = 0UL;
pthread_mutex_init(&audenc1->threadStatusMutex, NULL);
/* Initialize rendezvous objects for making threads wait on conditions */
audenc1->waitOnEncodeThread = Rendezvous_create(2, &rzvAttrs);
audenc1->waitOnEncodeDrain = Rendezvous_create(100, &rzvAttrs);
audenc1->drainingEOS = FALSE;
/* Initialize the custom thread attributes */
if (pthread_attr_init(&attr)) {
GST_WARNING("failed to initialize thread attrs\n");
gst_tiaudenc1_exit_audio(audenc1);
return FALSE;
}
/* Force the thread to use the system scope */
if (pthread_attr_setscope(&attr, PTHREAD_SCOPE_SYSTEM)) {
GST_WARNING("failed to set scope attribute\n");
gst_tiaudenc1_exit_audio(audenc1);
return FALSE;
}
/* Force the thread to use custom scheduling attributes */
if (pthread_attr_setinheritsched(&attr, PTHREAD_EXPLICIT_SCHED)) {
GST_WARNING("failed to set schedule inheritance attribute\n");
gst_tiaudenc1_exit_audio(audenc1);
return FALSE;
}
/* Set the thread to be fifo real time scheduled */
if (pthread_attr_setschedpolicy(&attr, SCHED_FIFO)) {
GST_WARNING("failed to set FIFO scheduling policy\n");
gst_tiaudenc1_exit_audio(audenc1);
return FALSE;
}
/* Set the display thread priority */
schedParam.sched_priority = GstTIAudioThreadPriority;
if (pthread_attr_setschedparam(&attr, &schedParam)) {
GST_WARNING("failed to set scheduler parameters\n");
return FALSE;
}
/* Create encoder thread */
if (pthread_create(&audenc1->encodeThread, &attr,
gst_tiaudenc1_encode_thread, (void*)audenc1)) {
GST_ELEMENT_ERROR(audenc1, RESOURCE, FAILED,
("Failed to create encode thread\n"), (NULL));
gst_tiaudenc1_exit_audio(audenc1);
return FALSE;
}
gst_tithread_set_status(audenc1, TIThread_CODEC_CREATED);
/* Destroy the custom thread attributes */
if (pthread_attr_destroy(&attr)) {
GST_WARNING("failed to destroy thread attrs\n");
gst_tiaudenc1_exit_audio(audenc1);
return FALSE;
}
/* Make sure circular buffer and display buffer handles are created by
* encoder thread.
*/
Rendezvous_meet(audenc1->waitOnEncodeThread);
if (audenc1->circBuf == NULL || audenc1->hOutBufTab == NULL) {
GST_ELEMENT_ERROR(audenc1, RESOURCE, FAILED,
("Encode thread failed to create circbuf or display buffer handles\n"),
(NULL));
return FALSE;
}
GST_LOG("end init_audio\n");
return TRUE;
}
/******************************************************************************
* gst_tiaudenc1_exit_audio
* Shut down any running audio encoder, and reset the element state.
******************************************************************************/
static gboolean gst_tiaudenc1_exit_audio(GstTIAudenc1 *audenc1)
{
gboolean checkResult;
void* thread_ret;
GST_LOG("begin exit_audio\n");
/* Drain the pipeline if it hasn't already been drained */
if (!audenc1->drainingEOS) {
gst_tiaudenc1_drain_pipeline(audenc1);
}
/* Shut down the encode thread */
if (gst_tithread_check_status(
audenc1, TIThread_CODEC_CREATED, checkResult)) {
GST_LOG("shutting down encode thread\n");
Rendezvous_force(audenc1->waitOnEncodeThread);
if (pthread_join(audenc1->encodeThread, &thread_ret) == 0) {
if (thread_ret == GstTIThreadFailure) {
GST_DEBUG("encode thread exited with an error condition\n");
}
}
}
/* Shut down thread status management */
audenc1->threadStatus = 0UL;
pthread_mutex_destroy(&audenc1->threadStatusMutex);
/* Shut down remaining items */
if (audenc1->waitOnEncodeDrain) {
Rendezvous_delete(audenc1->waitOnEncodeDrain);
audenc1->waitOnEncodeDrain = NULL;
}
if (audenc1->waitOnEncodeThread) {
Rendezvous_delete(audenc1->waitOnEncodeThread);
audenc1->waitOnEncodeThread = NULL;
}
GST_LOG("end exit_audio\n");
return TRUE;
}
/******************************************************************************
* gst_tiaudenc1_change_state
* Manage state changes for the audio stream. The gStreamer documentation
* states that state changes must be handled in this manner:
* 1) Handle ramp-up states
* 2) Pass state change to base class
* 3) Handle ramp-down states
******************************************************************************/
static GstStateChangeReturn gst_tiaudenc1_change_state(GstElement *element,
GstStateChange transition)
{
GstStateChangeReturn ret = GST_STATE_CHANGE_SUCCESS;
GstTIAudenc1 *audenc1 = GST_TIAUDENC1(element);
GST_LOG("begin change_state (%d)\n", transition);
/* Handle ramp-up state changes */
switch (transition) {
case GST_STATE_CHANGE_NULL_TO_READY:
break;
default:
break;
}
/* Pass state changes to base class */
ret = GST_ELEMENT_CLASS(parent_class)->change_state(element, transition);
if (ret == GST_STATE_CHANGE_FAILURE)
return ret;
/* Handle ramp-down state changes */
switch (transition) {
case GST_STATE_CHANGE_READY_TO_NULL:
/* Shut down any running audio encoder */
if (!gst_tiaudenc1_exit_audio(audenc1)) {
return GST_STATE_CHANGE_FAILURE;
}
break;
default:
break;
}
GST_LOG("end change_state\n");
return ret;
}
/******************************************************************************
* gst_tiaudenc1_codec_stop
* Release codec engine resources
*****************************************************************************/
static gboolean gst_tiaudenc1_codec_stop (GstTIAudenc1 *audenc1)
{
if (audenc1->circBuf) {
GstTICircBuffer *circBuf;
GST_LOG("freeing cicrular input buffer\n");
circBuf = audenc1->circBuf;
audenc1->circBuf = NULL;
gst_ticircbuffer_unref(circBuf);
}
if (audenc1->hOutBufTab) {
GST_LOG("freeing output buffers\n");
gst_tidmaibuftab_unref(audenc1->hOutBufTab);
audenc1->hOutBufTab = NULL;
}
if (audenc1->hAe) {
GST_LOG("closing audio encoder\n");
Aenc1_delete(audenc1->hAe);
audenc1->hAe = NULL;
}
if (audenc1->hEngine) {
GST_LOG("closing codec engine\n");
Engine_close(audenc1->hEngine);
audenc1->hEngine = NULL;
}
return TRUE;
}
/******************************************************************************
* gst_tiaudenc1_codec_start
* Initialize codec engine
*****************************************************************************/
static gboolean gst_tiaudenc1_codec_start (GstTIAudenc1 *audenc1)
{
AUDENC1_Params params = Aenc1_Params_DEFAULT;
AUDENC1_DynamicParams dynParams = Aenc1_DynamicParams_DEFAULT;
Buffer_Attrs bAttrs = Buffer_Attrs_DEFAULT;
/* Override the default parameters to use the defaults specified or the
* user settings.
* Order of setting is:
* 1. Parameters set on command line
* 2. Settings detected during caps negotiation
* 3. Default values defined in gsttiaudenc1.h
*/
params.sampleRate = audenc1->samplefreq == 0 ? TIAUDENC1_SAMPLEFREQ_DEFAULT:
audenc1->samplefreq;
params.bitRate = audenc1->bitrate = audenc1->bitrate == 0 ?
TIAUDENC1_BITRATE_DEFAULT : audenc1->bitrate;
params.channelMode = audenc1->channels == 0 ? params.channelMode :
audenc1->channels == 1 ? IAUDIO_1_0 : IAUDIO_2_0;
/* Initialize dynamic parameters */
dynParams.sampleRate = params.sampleRate;
dynParams.bitRate = params.bitRate;
dynParams.channelMode = params.channelMode;
/* Open the codec engine */
GST_LOG("opening codec engine \"%s\"\n", audenc1->engineName);
audenc1->hEngine = Engine_open((Char *) audenc1->engineName, NULL, NULL);
if (audenc1->hEngine == NULL) {
GST_ELEMENT_ERROR(audenc1, RESOURCE, READ,
("Failed to open codec engine \"%s\"\n", audenc1->engineName), (NULL));
return FALSE;
}
/* Initialize audio encoder */
GST_LOG("opening audio encoder \"%s\"\n", audenc1->codecName);
audenc1->hAe = Aenc1_create(audenc1->hEngine, (Char*)audenc1->codecName,
¶ms, &dynParams);
if (audenc1->hAe == NULL) {
GST_ELEMENT_ERROR(audenc1, RESOURCE, FAILED,
("Failed to create audio encoder: %s\n", audenc1->codecName), (NULL));
GST_ELEMENT_ERROR(audenc1, RESOURCE, FAILED,
("This may be caused by specifying channels/bitrate "
"combinations that are too high for your codec. Please "
"make sure that channels * bitrate does not exceed the max "
"bitrate supported by your codec. Current settings are:\n"
"\tbitrate = %d\n\tchannels = %d\n\ttotal bitrate = %d\n",
audenc1->bitrate, audenc1->channels,
audenc1->bitrate * audenc1->channels), (NULL));
GST_LOG("closing codec engine\n");
return FALSE;
}
/* Set up a circular input buffer capable of holding 3 RAW frames */
audenc1->circBuf = gst_ticircbuffer_new(
Aenc1_getInBufSize(audenc1->hAe), 3, FALSE);
if (audenc1->circBuf == NULL) {
GST_ELEMENT_ERROR(audenc1, RESOURCE, NO_SPACE_LEFT,
("Failed to create circular input buffer\n"), (NULL));
return FALSE;
}
/* Display buffer contents if displayBuffer=TRUE was specified */
gst_ticircbuffer_set_display(audenc1->circBuf, audenc1->displayBuffer);
/* Define the number of display buffers to allocate. This number must be
* at least 2, If this has not been set via set_property(), default to the
* minimal value.
*/
if (audenc1->numOutputBufs == 0) {
audenc1->numOutputBufs = 2;
}
/* Create codec output buffers.
*/
GST_LOG("creating output buffers\n");
/* By default, new buffers are marked as in-use by the codec */
bAttrs.useMask = gst_tidmaibuffer_CODEC_FREE;
audenc1->hOutBufTab = gst_tidmaibuftab_new(audenc1->numOutputBufs,
Aenc1_getOutBufSize(audenc1->hAe), &bAttrs);
if (audenc1->hOutBufTab == NULL) {
GST_ELEMENT_ERROR(audenc1, RESOURCE, NO_SPACE_LEFT,
("Failed to create output buffer\n"), (NULL));
return FALSE;
}
return TRUE;
}
/******************************************************************************
* gst_tiaudenc1_encode_thread
* Call the audio codec to process a full input buffer
******************************************************************************/
static void* gst_tiaudenc1_encode_thread(void *arg)
{
GstTIAudenc1 *audenc1 = GST_TIAUDENC1(gst_object_ref(arg));
void *threadRet = GstTIThreadSuccess;
Buffer_Handle hDstBuf;
Int32 encDataConsumed;
GstBuffer *encDataWindow = NULL;
GstClockTime encDataTime;
Buffer_Handle hEncDataWindow;
GstBuffer *outBuf;
GstClockTime sampleDuration;
guint sampleRate;
guint numSamples;
Int bufIdx;
Int ret;
GST_LOG("starting audenc encode thread\n");
/* Initialize codec engine */
ret = gst_tiaudenc1_codec_start(audenc1);
/* Notify main thread that it is ok to continue initialization */
Rendezvous_meet(audenc1->waitOnEncodeThread);
Rendezvous_reset(audenc1->waitOnEncodeThread);
if (ret == FALSE) {
GST_ELEMENT_ERROR(audenc1, RESOURCE, FAILED,
("Failed to start codec\n"), (NULL));
goto thread_exit;
}
while (TRUE) {
/* Obtain an raw data frame */
encDataWindow = gst_ticircbuffer_get_data(audenc1->circBuf);
encDataTime = GST_BUFFER_TIMESTAMP(encDataWindow);
hEncDataWindow = GST_TIDMAIBUFFERTRANSPORT_DMAIBUF(encDataWindow);
/* Check if there is enough encoded data to be sent to the codec.
* The last frame of data may not be sufficient to meet the codec
* requirements for the amount of input data. If so just throw
* away the last bit of data rather than filling with bogus
* data.
*/
if (GST_BUFFER_SIZE(encDataWindow) <
Aenc1_getInBufSize(audenc1->hAe)) {
GST_LOG("Not enough audio data remains\n");
if (!audenc1->drainingEOS) {
goto thread_failure;
}
goto thread_exit;
}
/* Obtain a free output buffer for the encoded data */
if (!(hDstBuf = gst_tidmaibuftab_get_buf(audenc1->hOutBufTab))) {
GST_ELEMENT_ERROR(audenc1, RESOURCE, READ,
("Failed to get a free contiguous buffer from BufTab\n"),
(NULL));
goto thread_exit;
}
/* Invoke the audio encoder */
GST_LOG("Invoking the audio encoder at 0x%08lx with %u bytes\n",
(unsigned long)Buffer_getUserPtr(hEncDataWindow),
GST_BUFFER_SIZE(encDataWindow));
ret = Aenc1_process(audenc1->hAe, hEncDataWindow, hDstBuf);
encDataConsumed = Buffer_getNumBytesUsed(hEncDataWindow);
if (ret < 0) {
GST_ELEMENT_ERROR(audenc1, STREAM, ENCODE,
("Failed to encode audio buffer\n"), (NULL));
goto thread_failure;
}
/* If no encoded data was used we cannot find the next frame */
if (ret == Dmai_EBITERROR && encDataConsumed == 0) {
GST_ELEMENT_ERROR(audenc1, STREAM, ENCODE,
("Fatal bit error\n"), (NULL));
goto thread_failure;
}
if (ret > 0) {
GST_LOG("Aenc1_process returned success code %d\n", ret);
}
sampleRate = audenc1->samplefreq;
numSamples = encDataConsumed / (2 * audenc1->channels) ;
sampleDuration = GST_FRAMES_TO_CLOCK_TIME(numSamples, sampleRate);
/* Release the reference buffer, and tell the circular buffer how much
* data was consumed.
*/
ret = gst_ticircbuffer_data_consumed(audenc1->circBuf, encDataWindow,
encDataConsumed);
encDataWindow = NULL;
if (!ret) {
goto thread_failure;
}
/* Set the source pad capabilities based on the encoded frame
* properties.
*/
gst_tiaudenc1_set_source_caps(audenc1);
/* Create a DMAI transport buffer object to carry a DMAI buffer to
* the source pad. The transport buffer knows how to release the
* buffer for re-use in this element when the source pad calls
* gst_buffer_unref().
*/
outBuf = gst_tidmaibuffertransport_new(hDstBuf, audenc1->hOutBufTab);
gst_buffer_set_data(outBuf, GST_BUFFER_DATA(outBuf),
Buffer_getNumBytesUsed(hDstBuf));
gst_buffer_set_caps(outBuf, GST_PAD_CAPS(audenc1->srcpad));
/* Set timestamp on output buffer */
if (audenc1->genTimeStamps) {
GST_BUFFER_DURATION(outBuf) = sampleDuration;
GST_BUFFER_TIMESTAMP(outBuf) = encDataTime;
}
else {
GST_BUFFER_TIMESTAMP(outBuf) = GST_CLOCK_TIME_NONE;
}
/* Tell circular buffer how much time we consumed */
gst_ticircbuffer_time_consumed(audenc1->circBuf, sampleDuration);
/* Push the transport buffer to the source pad */
GST_LOG("pushing buffer to source pad with timestamp : %"
GST_TIME_FORMAT ", duration: %" GST_TIME_FORMAT,
GST_TIME_ARGS (GST_BUFFER_TIMESTAMP(outBuf)),
GST_TIME_ARGS (GST_BUFFER_DURATION(outBuf)));
if (gst_pad_push(audenc1->srcpad, outBuf) != GST_FLOW_OK) {
GST_DEBUG("push to source pad failed\n");
goto thread_failure;
}
/* Release buffers no longer in use by the codec */
Buffer_freeUseMask(hDstBuf, gst_tidmaibuffer_CODEC_FREE);
}
thread_failure:
gst_tithread_set_status(audenc1, TIThread_CODEC_ABORTED);
gst_ticircbuffer_consumer_aborted(audenc1->circBuf);
threadRet = GstTIThreadFailure;
thread_exit:
/* Re-claim any buffers owned by the codec */
bufIdx = BufTab_getNumBufs(GST_TIDMAIBUFTAB_BUFTAB(audenc1->hOutBufTab));
while (bufIdx-- > 0) {
Buffer_Handle hBuf = BufTab_getBuf(
GST_TIDMAIBUFTAB_BUFTAB(audenc1->hOutBufTab), bufIdx);
Buffer_freeUseMask(hBuf, gst_tidmaibuffer_CODEC_FREE);
}
/* Release the last buffer we retrieved from the circular buffer */
if (encDataWindow) {
gst_ticircbuffer_data_consumed(audenc1->circBuf, encDataWindow, 0);
}
/* We have to wait to shut down this thread until we can guarantee that
* no more input buffers will be queued into the circular buffer
* (we're about to delete it).
*/
Rendezvous_meet(audenc1->waitOnEncodeThread);
Rendezvous_reset(audenc1->waitOnEncodeThread);
/* Notify main thread that we are done draining before we shutdown the
* codec, or we will hang. We proceed in this order so the EOS event gets
* propagated downstream before we attempt to shut down the codec. The
* codec-shutdown process will block until all BufTab buffers have been
* released, and downstream-elements may hang on to buffers until
* they get the EOS.
*/
Rendezvous_force(audenc1->waitOnEncodeDrain);
/* Initialize codec engine */
if (gst_tiaudenc1_codec_stop(audenc1) < 0) {
GST_ERROR("failed to stop codec\n");
GST_ELEMENT_ERROR(audenc1, RESOURCE, FAILED,
("Failed to stop codec\n"), (NULL));
}
gst_object_unref(audenc1);
GST_LOG("exit audio encode_thread (%d)\n", (int)threadRet);
return threadRet;
}
/******************************************************************************
* gst_tiaudenc1_drain_pipeline
* Wait for the encode thread to finish processing queued input data.
******************************************************************************/
static void gst_tiaudenc1_drain_pipeline(GstTIAudenc1 *audenc1)
{
gboolean checkResult;
/* If the encode thread hasn't been created, there is nothing to drain. */
if (!gst_tithread_check_status(
audenc1, TIThread_CODEC_CREATED, checkResult)) {
return;
}
audenc1->drainingEOS = TRUE;
gst_ticircbuffer_drain(audenc1->circBuf, TRUE);
/* Tell the encode thread that it is ok to shut down */
Rendezvous_force(audenc1->waitOnEncodeThread);
/* Wait for the encoder to finish draining */
Rendezvous_meet(audenc1->waitOnEncodeDrain);
}
/******************************************************************************
* Custom ViM Settings for editing this file
******************************************************************************/
#if 0
Tabs (use 4 spaces for indentation)
vim:set tabstop=4: /* Use 4 spaces for tabs */
vim:set shiftwidth=4: /* Use 4 spaces for >> operations */
vim:set expandtab: /* Expand tabs into white spaces */
#endif
|