summaryrefslogtreecommitdiff
path: root/lib/avtp_pipeline/platform/Linux/intf_alsa/openavb_intf_alsa.c
blob: 37d291405420b7f5df18eaf82f7135377a770e28 (plain)
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
/*************************************************************************************************************
Copyright (c) 2012-2015, Symphony Teleca Corporation, a Harman International Industries, Incorporated company
Copyright (c) 2016-2017, Harman International Industries, Incorporated
All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:

1. Redistributions of source code must retain the above copyright notice, this
   list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice,
   this list of conditions and the following disclaimer in the documentation
   and/or other materials provided with the distribution.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS LISTED "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS LISTED BE LIABLE FOR
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

Attributions: The inih library portion of the source code is licensed from
Brush Technology and Ben Hoyt - Copyright (c) 2009, Brush Technology and Copyright (c) 2009, Ben Hoyt.
Complete license and copyright information can be found at
https://github.com/benhoyt/inih/commit/74d2ca064fb293bc60a77b0bd068075b293cf175.
*************************************************************************************************************/

/*
* MODULE SUMMARY : ALSA interface module.
*/

#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include "openavb_types_pub.h"
#include "openavb_audio_pub.h"
#include "openavb_trace_pub.h"
#include "openavb_mediaq_pub.h"
#include "openavb_map_uncmp_audio_pub.h"
#include "openavb_map_aaf_audio_pub.h"
#include "openavb_intf_pub.h"
#include "openavb_mcs.h"

#define	AVB_LOG_COMPONENT	"ALSA Interface"
#include "openavb_log_pub.h"

// The asoundlib.h header needs to appear after openavb_trace_pub.h otherwise an incompatible version of time.h gets pulled in.
#include <alsa/asoundlib.h>

#define PCM_DEVICE_NAME_DEFAULT	"default"
#define PCM_ACCESS_TYPE			SND_PCM_ACCESS_RW_INTERLEAVED

typedef struct {
	/////////////
	// Config data
	/////////////
	// Ignore timestamp at listener.
	bool ignoreTimestamp;

	// ALSA Device name
	char *pDeviceName;

	// map_nv_audio_rate
	avb_audio_rate_t audioRate;

	// map_nv_audio_type
	avb_audio_type_t audioType;

	// map_nv_audio_bit_depth
	avb_audio_bit_depth_t audioBitDepth;

	// map_nv_audio_endian
	avb_audio_endian_t audioEndian;

	// map_nv_channels
	avb_audio_channels_t audioChannels;

	// map_nv_allow_resampling
	bool allowResampling;

	U32 startThresholdPeriods;

	U32 periodTimeUsec;

	/////////////
	// Variable data
	/////////////
	// Handle for the PCM device
	snd_pcm_t *pcmHandle;

	// ALSA stream
	snd_pcm_stream_t pcmStream;

	// ALSA read/write interval
	U32 intervalCounter;

	// Media clock synthesis for precise timestamps
	mcs_t mcs;

	// Estimate of media clock skew in Parts Per Billion (ns per second)
	S32 clockSkewPPB;

	// Use Media Clock Synth module instead of timestamps taken during Tx callback
	bool fixedTimestampEnabled;
} pvt_data_t;


static snd_pcm_format_t x_AVBAudioFormatToAlsaFormat(avb_audio_type_t type,
											  avb_audio_bit_depth_t bitDepth,
											  avb_audio_endian_t endian,
											  char const* pMediaQDataFormat)
{
	bool tight = FALSE;
	if (bitDepth == AVB_AUDIO_BIT_DEPTH_24BIT) {
		if (pMediaQDataFormat != NULL
			&& (strcmp(pMediaQDataFormat, MapAVTPAudioMediaQDataFormat) == 0
			|| strcmp(pMediaQDataFormat, MapUncmpAudioMediaQDataFormat) == 0)) {
			tight = TRUE;
		}
	}

	if (type == AVB_AUDIO_TYPE_FLOAT) {
		switch (bitDepth) {
			case AVB_AUDIO_BIT_DEPTH_32BIT:
				if (endian == AVB_AUDIO_ENDIAN_BIG)
					return SND_PCM_FORMAT_FLOAT_BE;
				else
					return SND_PCM_FORMAT_FLOAT_LE;
			default:
				AVB_LOGF_ERROR("Unsupported audio bit depth for float: %d", bitDepth);
			break;
		}
	}
	else if (type == AVB_AUDIO_TYPE_UINT) {
		switch (bitDepth) {
		case AVB_AUDIO_BIT_DEPTH_8BIT:
			return SND_PCM_FORMAT_U8;
		case AVB_AUDIO_BIT_DEPTH_16BIT:
				if (endian == AVB_AUDIO_ENDIAN_BIG)
					return SND_PCM_FORMAT_U16_BE;
				else
					return SND_PCM_FORMAT_U16_LE;
		case AVB_AUDIO_BIT_DEPTH_20BIT:
				if (endian == AVB_AUDIO_ENDIAN_BIG)
					return SND_PCM_FORMAT_U20_3BE;
				else
					return SND_PCM_FORMAT_U20_3LE;
		case AVB_AUDIO_BIT_DEPTH_24BIT:
		    if (tight) {
				if (endian == AVB_AUDIO_ENDIAN_BIG)
					return SND_PCM_FORMAT_U24_3BE;
				else
					return SND_PCM_FORMAT_U24_3LE;
			}
			else {
				if (endian == AVB_AUDIO_ENDIAN_BIG)
					return SND_PCM_FORMAT_U24_BE;
				else
					return SND_PCM_FORMAT_U24_LE;
			}
		case AVB_AUDIO_BIT_DEPTH_32BIT:
				if (endian == AVB_AUDIO_ENDIAN_BIG)
					return SND_PCM_FORMAT_U32_BE;
				else
					return SND_PCM_FORMAT_U32_LE;
			case AVB_AUDIO_BIT_DEPTH_1BIT:
		case AVB_AUDIO_BIT_DEPTH_48BIT:
		case AVB_AUDIO_BIT_DEPTH_64BIT:
		default:
				AVB_LOGF_ERROR("Unsupported integer audio bit depth: %d", bitDepth);
			break;
		}
	}
	else {
		// AVB_AUDIO_TYPE_INT
		// or unspecified (defaults to signed int)
		switch (bitDepth) {
			case AVB_AUDIO_BIT_DEPTH_8BIT:
				// 8bit samples don't worry about endianness,
				// but default to unsigned instead of signed.
				if (type == AVB_AUDIO_TYPE_INT)
					return SND_PCM_FORMAT_S8;
				else // default
					return SND_PCM_FORMAT_U8;
			case AVB_AUDIO_BIT_DEPTH_16BIT:
				if (endian == AVB_AUDIO_ENDIAN_BIG)
					return SND_PCM_FORMAT_S16_BE;
				else
					return SND_PCM_FORMAT_S16_LE;
			case AVB_AUDIO_BIT_DEPTH_20BIT:
				if (endian == AVB_AUDIO_ENDIAN_BIG)
					return SND_PCM_FORMAT_S20_3BE;
				else
					return SND_PCM_FORMAT_S20_3LE;
			case AVB_AUDIO_BIT_DEPTH_24BIT:
				if (tight) {
					if (endian == AVB_AUDIO_ENDIAN_BIG)
						return SND_PCM_FORMAT_S24_3BE;
					else
						return SND_PCM_FORMAT_S24_3LE;
				}
				else {
					if (endian == AVB_AUDIO_ENDIAN_BIG)
						return SND_PCM_FORMAT_S24_BE;
					else
						return SND_PCM_FORMAT_S24_LE;
				}
			case AVB_AUDIO_BIT_DEPTH_32BIT:
				if (endian == AVB_AUDIO_ENDIAN_BIG)
					return SND_PCM_FORMAT_S32_BE;
				else
					return SND_PCM_FORMAT_S32_LE;
			case AVB_AUDIO_BIT_DEPTH_1BIT:
			case AVB_AUDIO_BIT_DEPTH_48BIT:
			case AVB_AUDIO_BIT_DEPTH_64BIT:
			default:
				AVB_LOGF_ERROR("Unsupported audio bit depth: %d", bitDepth);
				break;
		}
	}

	return SND_PCM_FORMAT_UNKNOWN;
}


// Each configuration name value pair for this mapping will result in this callback being called.
void openavbIntfAlsaCfgCB(media_q_t *pMediaQ, const char *name, const char *value)
{
	AVB_TRACE_ENTRY(AVB_TRACE_INTF);
	if (pMediaQ) {
		char *pEnd;
		long tmp;
		U32 val;

		pvt_data_t *pPvtData = pMediaQ->pPvtIntfInfo;
		if (!pPvtData) {
			AVB_LOG_ERROR("Private interface module data not allocated.");
			return;
		}

		media_q_pub_map_uncmp_audio_info_t *pPubMapUncmpAudioInfo;
		pPubMapUncmpAudioInfo = (media_q_pub_map_uncmp_audio_info_t *)pMediaQ->pPubMapInfo;
		if (!pPubMapUncmpAudioInfo) {
			AVB_LOG_ERROR("Public map data for audio info not allocated.");
			return;
		}


		if (strcmp(name, "intf_nv_ignore_timestamp") == 0) {
			tmp = strtol(value, &pEnd, 10);
			if (*pEnd == '\0' && tmp == 1) {
				pPvtData->ignoreTimestamp = (tmp == 1);
			}
		}

		else if (strcmp(name, "intf_nv_device_name") == 0) {
			if (pPvtData->pDeviceName)
				free(pPvtData->pDeviceName);
			pPvtData->pDeviceName = strdup(value);
		}

		else if (strcmp(name, "intf_nv_audio_rate") == 0) {
			val = strtol(value, &pEnd, 10);
			// TODO: Should check for specific values
			if (val >= AVB_AUDIO_RATE_8KHZ && val <= AVB_AUDIO_RATE_192KHZ) {
				pPvtData->audioRate = val;
			}
			else {
				AVB_LOG_ERROR("Invalid audio rate configured for intf_nv_audio_rate.");
				pPvtData->audioRate = AVB_AUDIO_RATE_44_1KHZ;
			}

			// Give the audio parameters to the mapping module.
			if (pMediaQ->pMediaQDataFormat) {
				if (strcmp(pMediaQ->pMediaQDataFormat, MapUncmpAudioMediaQDataFormat) == 0
					|| strcmp(pMediaQ->pMediaQDataFormat, MapAVTPAudioMediaQDataFormat) == 0) {
					pPubMapUncmpAudioInfo->audioRate = pPvtData->audioRate;
				}
				//else if (pMediaQ->pMediaQDataFormat == MapSAFMediaQDataFormat) {
				//}
			}
		}

		else if (strcmp(name, "intf_nv_audio_bit_depth") == 0) {
			val = strtol(value, &pEnd, 10);
			// TODO: Should check for specific values
			if (val >= AVB_AUDIO_BIT_DEPTH_1BIT && val <= AVB_AUDIO_BIT_DEPTH_64BIT) {
				pPvtData->audioBitDepth = val;
			}
			else {
				AVB_LOG_ERROR("Invalid audio type configured for intf_nv_audio_bits.");
				pPvtData->audioBitDepth = AVB_AUDIO_BIT_DEPTH_24BIT;
			}

			// Give the audio parameters to the mapping module.
			if (pMediaQ->pMediaQDataFormat) {
				if (strcmp(pMediaQ->pMediaQDataFormat, MapUncmpAudioMediaQDataFormat) == 0
					|| strcmp(pMediaQ->pMediaQDataFormat, MapAVTPAudioMediaQDataFormat) == 0) {
					pPubMapUncmpAudioInfo->audioBitDepth = pPvtData->audioBitDepth;
				}
				//else if (pMediaQ->pMediaQDataFormat == MapSAFMediaQDataFormat) {
				//}
			}
		}

		else if (strcmp(name, "intf_nv_audio_type") == 0) {
			if (strncasecmp(value, "float", 5) == 0)
				pPvtData->audioType = AVB_AUDIO_TYPE_FLOAT;
			else if (strncasecmp(value, "sign", 4) == 0
					 || strncasecmp(value, "int", 4) == 0)
				pPvtData->audioType = AVB_AUDIO_TYPE_INT;
			else if (strncasecmp(value, "unsign", 6) == 0
					 || strncasecmp(value, "uint", 4) == 0)
				pPvtData->audioType = AVB_AUDIO_TYPE_UINT;
			else {
				AVB_LOG_ERROR("Invalid audio type configured for intf_nv_audio_type.");
				pPvtData->audioType = AVB_AUDIO_TYPE_UNSPEC;
			}

			// Give the audio parameters to the mapping module.
			if (pMediaQ->pMediaQDataFormat) {
				if (strcmp(pMediaQ->pMediaQDataFormat, MapUncmpAudioMediaQDataFormat) == 0
					|| strcmp(pMediaQ->pMediaQDataFormat, MapAVTPAudioMediaQDataFormat) == 0) {
					pPubMapUncmpAudioInfo->audioType = pPvtData->audioType;
				}
				//else if (pMediaQ->pMediaQDataFormat == MapSAFMediaQDataFormat) {
				//}
			}
		}

		else if (strcmp(name, "intf_nv_audio_endian") == 0) {
			if (strncasecmp(value, "big", 3) == 0)
				pPvtData->audioEndian = AVB_AUDIO_ENDIAN_BIG;
			else if (strncasecmp(value, "little", 6) == 0)
				pPvtData->audioEndian = AVB_AUDIO_ENDIAN_LITTLE;
			else {
				AVB_LOG_ERROR("Invalid audio type configured for intf_nv_audio_endian.");
				pPvtData->audioEndian = AVB_AUDIO_ENDIAN_UNSPEC;
			}

			// Give the audio parameters to the mapping module.
			if (pMediaQ->pMediaQDataFormat) {
				if (strcmp(pMediaQ->pMediaQDataFormat, MapUncmpAudioMediaQDataFormat) == 0
					|| strcmp(pMediaQ->pMediaQDataFormat, MapAVTPAudioMediaQDataFormat) == 0) {
					pPubMapUncmpAudioInfo->audioEndian = pPvtData->audioEndian;
				}
				//else if (pMediaQ->pMediaQDataFormat == MapSAFMediaQDataFormat) {
				//}
			}
		}

		else if (strcmp(name, "intf_nv_audio_channels") == 0) {
			val = strtol(value, &pEnd, 10);
			// TODO: Should check for specific values
			if (val >= AVB_AUDIO_CHANNELS_1 && val <= AVB_AUDIO_CHANNELS_8) {
				pPvtData->audioChannels = val;
			}
			else {
				AVB_LOG_ERROR("Invalid audio channels configured for intf_nv_audio_channels.");
				pPvtData->audioChannels = AVB_AUDIO_CHANNELS_2;
			}

			// Give the audio parameters to the mapping module.
			if (pMediaQ->pMediaQDataFormat) {
				if (strcmp(pMediaQ->pMediaQDataFormat, MapUncmpAudioMediaQDataFormat) == 0
					|| strcmp(pMediaQ->pMediaQDataFormat, MapAVTPAudioMediaQDataFormat) == 0) {
					pPubMapUncmpAudioInfo->audioChannels = pPvtData->audioChannels;
				}
				//else if (pMediaQ->pMediaQDataFormat == MapSAFMediaQDataFormat) {
				//}
			}

		}

		if (strcmp(name, "intf_nv_allow_resampling") == 0) {
			tmp = strtol(value, &pEnd, 10);
			if (*pEnd == '\0' && tmp == 1) {
				pPvtData->allowResampling = (tmp == 1);
			}
		}

		else if (strcmp(name, "intf_nv_start_threshold_periods") == 0) {
			pPvtData->startThresholdPeriods = strtol(value, &pEnd, 10);
		}

		else if (strcmp(name, "intf_nv_period_time") == 0) {
			pPvtData->periodTimeUsec = strtol(value, &pEnd, 10);
		}

		else if (strcmp(name, "intf_nv_clock_skew_ppb") == 0) {
			pPvtData->clockSkewPPB = strtol(value, &pEnd, 10);
		}

	}

	AVB_TRACE_EXIT(AVB_TRACE_INTF);
}

void openavbIntfAlsaGenInitCB(media_q_t *pMediaQ)
{
	AVB_TRACE_ENTRY(AVB_TRACE_INTF);
	AVB_TRACE_EXIT(AVB_TRACE_INTF);
}

// A call to this callback indicates that this interface module will be
// a talker. Any talker initialization can be done in this function.
void openavbIntfAlsaTxInitCB(media_q_t *pMediaQ)
{
	AVB_TRACE_ENTRY(AVB_TRACE_INTF);

	if (pMediaQ) {
		S32 rslt;
		pvt_data_t *pPvtData = pMediaQ->pPvtIntfInfo;
		if (!pPvtData) {
			AVB_LOG_ERROR("Private interface module data not allocated.");
			return;
		}

		// Holds the hardware parameters
		snd_pcm_hw_params_t *hwParams;

		// Open the pcm device.
		rslt = snd_pcm_open(&pPvtData->pcmHandle, pPvtData->pDeviceName, SND_PCM_STREAM_CAPTURE, SND_PCM_NONBLOCK);

		if (rslt < 0) {
			AVB_LOGF_ERROR("snd_pcm_open error(): %s", snd_strerror(rslt));
			AVB_TRACE_EXIT(AVB_TRACE_INTF);
			return;
		}

		// Allocate the parameter structure
		rslt = snd_pcm_hw_params_malloc(&hwParams);
		if (rslt < 0) {
			AVB_LOGF_ERROR("snd_pcm_hw_params_malloc error(): %s", snd_strerror(rslt));
			snd_pcm_close(pPvtData->pcmHandle);
			pPvtData->pcmHandle = NULL;
			AVB_TRACE_EXIT(AVB_TRACE_INTF);
			return;
		}

		// Initialize the hardware parameters
		rslt = snd_pcm_hw_params_any(pPvtData->pcmHandle, hwParams);
		if (rslt < 0) {
			AVB_LOGF_ERROR("snd_pcm_hw_params_any() error: %s", snd_strerror(rslt));
			snd_pcm_close(pPvtData->pcmHandle);
			pPvtData->pcmHandle = NULL;
			snd_pcm_hw_params_free(hwParams);
			hwParams = NULL;
			AVB_TRACE_EXIT(AVB_TRACE_INTF);
			return;
		}

		// Set if resampling allowed.
		rslt = snd_pcm_hw_params_set_rate_resample(pPvtData->pcmHandle, hwParams, pPvtData->allowResampling);
		if (rslt < 0) {
			AVB_LOGF_ERROR("snd_pcm_hw_params_set_rate_resample() error: %s", snd_strerror(rslt));
			snd_pcm_close(pPvtData->pcmHandle);
			pPvtData->pcmHandle = NULL;
			snd_pcm_hw_params_free(hwParams);
			hwParams = NULL;
			AVB_TRACE_EXIT(AVB_TRACE_INTF);
			return;
		}

		// Set the access type
		rslt = snd_pcm_hw_params_set_access(pPvtData->pcmHandle, hwParams, PCM_ACCESS_TYPE);
		if (rslt < 0) {
			AVB_LOGF_ERROR("snd_pcm_hw_params_set_access() error: %s", snd_strerror(rslt));
			snd_pcm_close(pPvtData->pcmHandle);
			pPvtData->pcmHandle = NULL;
			snd_pcm_hw_params_free(hwParams);
			hwParams = NULL;
			AVB_TRACE_EXIT(AVB_TRACE_INTF);
			return;
		}

		// Set the sample format
		int fmt = x_AVBAudioFormatToAlsaFormat(pPvtData->audioType,
											   pPvtData->audioBitDepth,
											   pPvtData->audioEndian,
											   pMediaQ->pMediaQDataFormat);
		rslt = snd_pcm_hw_params_set_format(pPvtData->pcmHandle, hwParams, fmt);
		if (rslt < 0) {
			AVB_LOGF_ERROR("snd_pcm_hw_params_set_format() error: %s", snd_strerror(rslt));
			snd_pcm_close(pPvtData->pcmHandle);
			pPvtData->pcmHandle = NULL;
			snd_pcm_hw_params_free(hwParams);
			hwParams = NULL;
			AVB_TRACE_EXIT(AVB_TRACE_INTF);
			return;
		}

		// Set the sample rate
		U32 rate = pPvtData->audioRate;
		rslt = snd_pcm_hw_params_set_rate_near(pPvtData->pcmHandle, hwParams, &rate, 0);
		if (rslt < 0) {
			AVB_LOGF_ERROR("snd_pcm_hw_params_set_rate_near() error: %s", snd_strerror(rslt));
			snd_pcm_close(pPvtData->pcmHandle);
			pPvtData->pcmHandle = NULL;
			snd_pcm_hw_params_free(hwParams);
			hwParams = NULL;
			AVB_TRACE_EXIT(AVB_TRACE_INTF);
			return;
		}
		if (rate != pPvtData->audioRate) {
			AVB_LOGF_ERROR("Could not set the exact rate. Requested: %u  Using: %u", pPvtData->audioRate, rate);
		}

		// Set the number of channels
		rslt = snd_pcm_hw_params_set_channels(pPvtData->pcmHandle, hwParams, pPvtData->audioChannels);
		if (rslt < 0) {
			AVB_LOGF_ERROR("snd_pcm_hw_params_set_channels() error: %s", snd_strerror(rslt));
			snd_pcm_close(pPvtData->pcmHandle);
			pPvtData->pcmHandle = NULL;
			snd_pcm_hw_params_free(hwParams);
			hwParams = NULL;
			AVB_TRACE_EXIT(AVB_TRACE_INTF);
			return;
		}

		// Give the hardware parameters to ALSA
		rslt = snd_pcm_hw_params(pPvtData->pcmHandle, hwParams);
		if (rslt < 0) {
			AVB_LOGF_ERROR("snd_pcm_hw_params() error: %s", snd_strerror(rslt));
			snd_pcm_close(pPvtData->pcmHandle);
			pPvtData->pcmHandle = NULL;
			snd_pcm_hw_params_free(hwParams);
			hwParams = NULL;
			AVB_TRACE_EXIT(AVB_TRACE_INTF);
			return;
		}

		// Free the hardware parameters
		snd_pcm_hw_params_free(hwParams);
		hwParams = NULL;

		// Get ready for playback
		rslt = snd_pcm_prepare(pPvtData->pcmHandle);
		if (rslt < 0) {
			AVB_LOGF_ERROR("snd_pcm_prepare() error: %s", snd_strerror(rslt));
			snd_pcm_close(pPvtData->pcmHandle);
			pPvtData->pcmHandle = NULL;
			AVB_TRACE_EXIT(AVB_TRACE_INTF);
			return;
		}

		// Dump settings
		snd_output_t* out;
		snd_output_stdio_attach(&out, stderr, 0);
		snd_pcm_dump(pPvtData->pcmHandle, out);
		snd_output_close(out);

		// Start capture
		snd_pcm_start(pPvtData->pcmHandle);
		{
			media_q_pub_map_uncmp_audio_info_t *pPubMapUncmpAudioInfo = pMediaQ->pPubMapInfo;

			AVB_LOGF_INFO("Finished ALSA Setup: packingFactor %d", pPubMapUncmpAudioInfo->packingFactor);
		}
	}
	AVB_TRACE_EXIT(AVB_TRACE_INTF);
}

// This callback will be called for each AVB transmit interval.
bool openavbIntfAlsaTxCB(media_q_t *pMediaQ)
{
	bool moreItems = TRUE;
	AVB_TRACE_ENTRY(AVB_TRACE_INTF_DETAIL);

	S32 rslt;

	if (pMediaQ) {
		media_q_pub_map_uncmp_audio_info_t *pPubMapUncmpAudioInfo = pMediaQ->pPubMapInfo;
		pvt_data_t *pPvtData = pMediaQ->pPvtIntfInfo;
		media_q_item_t *pMediaQItem = NULL;
		if (!pPvtData) {
			AVB_LOG_ERROR("Private interface module data not allocated.");
			AVB_TRACE_EXIT(AVB_TRACE_INTF_DETAIL);
			return FALSE;
		}

		if (pPvtData->intervalCounter++ % pPubMapUncmpAudioInfo->packingFactor != 0) {
			AVB_TRACE_EXIT(AVB_TRACE_INTF_DETAIL);
			return TRUE;
		}

		while (moreItems) {
			pMediaQItem = openavbMediaQHeadLock(pMediaQ);
			if (pMediaQItem) {
				if (pMediaQItem->itemSize < pPubMapUncmpAudioInfo->itemSize) {
					AVB_LOG_ERROR("Media queue item not large enough for samples");
					AVB_TRACE_EXIT(AVB_TRACE_INTF_DETAIL);
					return FALSE;
				}

				rslt = snd_pcm_readi(pPvtData->pcmHandle, pMediaQItem->pPubData + pMediaQItem->dataLen, pPubMapUncmpAudioInfo->framesPerItem - (pMediaQItem->dataLen / pPubMapUncmpAudioInfo->itemFrameSizeBytes));

				if (rslt < 0) {
					switch(rslt) {
					case -EPIPE:
						AVB_LOGF_ERROR("snd_pcm_readi() error: %s", snd_strerror(rslt));
						rslt = snd_pcm_recover(pPvtData->pcmHandle, rslt, 0);
						if (rslt < 0) {
							AVB_LOGF_ERROR("snd_pcm_recover: %s", snd_strerror(rslt));
						}
						break;
					case -EAGAIN:
						{ IF_LOG_INTERVAL(1000) AVB_LOG_DEBUG("snd_pcm_readi() had no data available"); }
						break;
					default:
						AVB_LOGF_ERROR("Unhandled snd_pcm_readi() error: %s", snd_strerror(rslt));
						break;
					}

					if (rslt < 0) {
						openavbMediaQHeadUnlock(pMediaQ);
						break;
					}
				}

				pMediaQItem->dataLen += rslt * pPubMapUncmpAudioInfo->itemFrameSizeBytes;
				if (pMediaQItem->dataLen != pPubMapUncmpAudioInfo->itemSize) {
					openavbMediaQHeadUnlock(pMediaQ);
				}
				else {
					// Always get the timestamp.  Protocols such as AAF can choose to ignore them if not needed.
					if (!pPvtData->fixedTimestampEnabled) {
						openavbAvtpTimeSetToWallTime(pMediaQItem->pAvtpTime);
					} else {
						openavbMcsAdvance(&pPvtData->mcs);
						openavbAvtpTimeSetToTimestampNS(pMediaQItem->pAvtpTime, pPvtData->mcs.edgeTime);
					}
					openavbMediaQHeadPush(pMediaQ);
				}
			}
			else {
				moreItems = FALSE;
			}
		}
	}

	AVB_TRACE_EXIT(AVB_TRACE_INTF_DETAIL);
	return !moreItems;
}

// A call to this callback indicates that this interface module will be
// a listener. Any listener initialization can be done in this function.
void openavbIntfAlsaRxInitCB(media_q_t *pMediaQ)
{
	AVB_TRACE_ENTRY(AVB_TRACE_INTF);

	S32 rslt;

	// Holds the hardware parameters
	snd_pcm_hw_params_t *hwParams;
	snd_pcm_sw_params_t *swParams;

	if (pMediaQ) {
		pvt_data_t *pPvtData = pMediaQ->pPvtIntfInfo;
		if (!pPvtData) {
			AVB_LOG_ERROR("Private interface module data not allocated.");
			return;
		}

		// Open the pcm device.
		rslt = snd_pcm_open(&pPvtData->pcmHandle, pPvtData->pDeviceName, SND_PCM_STREAM_PLAYBACK, 0);

		if (rslt < 0) {
			AVB_LOGF_ERROR("snd_pcm_open error(): %s", snd_strerror(rslt));
			AVB_TRACE_EXIT(AVB_TRACE_INTF);
			return;
		}

		// Allocate the parameter structure
		rslt = snd_pcm_hw_params_malloc(&hwParams);
		if (rslt < 0) {
			AVB_LOGF_ERROR("snd_pcm_hw_params_malloc error(): %s", snd_strerror(rslt));
			snd_pcm_close(pPvtData->pcmHandle);
			pPvtData->pcmHandle = NULL;
			AVB_TRACE_EXIT(AVB_TRACE_INTF);
			return;
		}

		// Initialize the hardware parameters
		rslt = snd_pcm_hw_params_any(pPvtData->pcmHandle, hwParams);
		if (rslt < 0) {
			AVB_LOGF_ERROR("snd_pcm_hw_params_any() error: %s", snd_strerror(rslt));
			snd_pcm_close(pPvtData->pcmHandle);
			pPvtData->pcmHandle = NULL;
			snd_pcm_hw_params_free(hwParams);
			hwParams = NULL;
			AVB_TRACE_EXIT(AVB_TRACE_INTF);
			return;
		}

		// Set if resampling allowed.
		rslt = snd_pcm_hw_params_set_rate_resample(pPvtData->pcmHandle, hwParams, pPvtData->allowResampling);
		if (rslt < 0) {
			AVB_LOGF_ERROR("snd_pcm_hw_params_set_rate_resample() error: %s", snd_strerror(rslt));
			snd_pcm_close(pPvtData->pcmHandle);
			pPvtData->pcmHandle = NULL;
			snd_pcm_hw_params_free(hwParams);
			hwParams = NULL;
			AVB_TRACE_EXIT(AVB_TRACE_INTF);
			return;
		}

		// Set the access type
		rslt = snd_pcm_hw_params_set_access(pPvtData->pcmHandle, hwParams, PCM_ACCESS_TYPE);
		if (rslt < 0) {
			AVB_LOGF_ERROR("snd_pcm_hw_params_set_access() error: %s", snd_strerror(rslt));
			snd_pcm_close(pPvtData->pcmHandle);
			pPvtData->pcmHandle = NULL;
			snd_pcm_hw_params_free(hwParams);
			hwParams = NULL;
			AVB_TRACE_EXIT(AVB_TRACE_INTF);
			return;
		}

		// Set the sample format
		int fmt = x_AVBAudioFormatToAlsaFormat(pPvtData->audioType,
											   pPvtData->audioBitDepth,
											   pPvtData->audioEndian,
											   pMediaQ->pMediaQDataFormat);
		rslt = snd_pcm_hw_params_set_format(pPvtData->pcmHandle, hwParams, fmt);
		if (rslt < 0) {
			AVB_LOGF_ERROR("snd_pcm_hw_params_set_format() error: %s", snd_strerror(rslt));
			snd_pcm_close(pPvtData->pcmHandle);
			pPvtData->pcmHandle = NULL;
			snd_pcm_hw_params_free(hwParams);
			hwParams = NULL;
			AVB_TRACE_EXIT(AVB_TRACE_INTF);
			return;
		}

		// Set the sample rate
		U32 rate = pPvtData->audioRate;
		rslt = snd_pcm_hw_params_set_rate_near(pPvtData->pcmHandle, hwParams, &rate, 0);
		if (rslt < 0) {
			AVB_LOGF_ERROR("snd_pcm_hw_params_set_rate_near() error: %s", snd_strerror(rslt));
			snd_pcm_close(pPvtData->pcmHandle);
			pPvtData->pcmHandle = NULL;
			snd_pcm_hw_params_free(hwParams);
			hwParams = NULL;
			AVB_TRACE_EXIT(AVB_TRACE_INTF);
			return;
		}
		if (rate != pPvtData->audioRate) {
			AVB_LOGF_ERROR("Could not set the exact rate. Requested: %u  Using: %u", pPvtData->audioRate, rate);
		}

		// Set the number of channels
		rslt = snd_pcm_hw_params_set_channels(pPvtData->pcmHandle, hwParams, pPvtData->audioChannels);
		if (rslt < 0) {
			AVB_LOGF_ERROR("snd_pcm_hw_params_set_channels() error: %s", snd_strerror(rslt));
			snd_pcm_close(pPvtData->pcmHandle);
			pPvtData->pcmHandle = NULL;
			snd_pcm_hw_params_free(hwParams);
			hwParams = NULL;
			AVB_TRACE_EXIT(AVB_TRACE_INTF);
			return;
		}


		// Time based buffer and period setup
		int dir;

		unsigned int buffer_time = 500000;						// ring buffer length in us
		unsigned int period_time = pPvtData->periodTimeUsec;	// period time in us
		int period_event = 1;									// produce poll event after each period
		snd_pcm_uframes_t buffer_size;
		snd_pcm_uframes_t period_size;
		const unsigned int usec_round = 10000;
		unsigned int max;

		// Hard-coded buffer and period times were failing for 192KHz.
		// Check for maximum buffer time and adjust ours down if necessary
		rslt = snd_pcm_hw_params_get_buffer_time_max(hwParams, &max, &dir);
		if (rslt < 0) {
			AVB_LOGF_ERROR("snd_pcm_hw_params_get_buffer_time_max() error: %s", snd_strerror(rslt));
			snd_pcm_close(pPvtData->pcmHandle);
			pPvtData->pcmHandle = NULL;
			snd_pcm_hw_params_free(hwParams);
			hwParams = NULL;
			AVB_TRACE_EXIT(AVB_TRACE_INTF);
			return;
		}
		else if (max < buffer_time) {
			buffer_time = (max / usec_round) * usec_round;
		}

		// Check for maximum period time and adjust ours down if necessary
		rslt = snd_pcm_hw_params_get_period_time_max(hwParams, &max, &dir);
		if (rslt < 0) {
			AVB_LOGF_ERROR("snd_pcm_hw_params_get_period_time_max() error: %s", snd_strerror(rslt));
			snd_pcm_close(pPvtData->pcmHandle);
			pPvtData->pcmHandle = NULL;
			snd_pcm_hw_params_free(hwParams);
			hwParams = NULL;
			AVB_TRACE_EXIT(AVB_TRACE_INTF);
			return;
		}
		else if (max < period_time) {
			period_time = (max / usec_round) * usec_round;
		}

		rslt = snd_pcm_hw_params_set_buffer_time_near(pPvtData->pcmHandle, hwParams, &buffer_time, &dir);
		if (rslt < 0) {
			AVB_LOGF_ERROR("snd_pcm_hw_params_set_buffer_time_near() error: %s", snd_strerror(rslt));
			snd_pcm_close(pPvtData->pcmHandle);
			pPvtData->pcmHandle = NULL;
			snd_pcm_hw_params_free(hwParams);
			hwParams = NULL;
			AVB_TRACE_EXIT(AVB_TRACE_INTF);
			return;
		}

		rslt = snd_pcm_hw_params_get_buffer_size(hwParams, &buffer_size);
		if (rslt < 0) {
			AVB_LOGF_ERROR("snd_pcm_hw_params_get_buffer_size() error: %s", snd_strerror(rslt));
			snd_pcm_close(pPvtData->pcmHandle);
			pPvtData->pcmHandle = NULL;
			snd_pcm_hw_params_free(hwParams);
			hwParams = NULL;
			AVB_TRACE_EXIT(AVB_TRACE_INTF);
			return;
		}

		rslt = snd_pcm_hw_params_set_period_time_near(pPvtData->pcmHandle, hwParams, &period_time, &dir);
		if (rslt < 0) {
			AVB_LOGF_ERROR("snd_pcm_hw_params_set_period_time_near() error: %s", snd_strerror(rslt));
			snd_pcm_close(pPvtData->pcmHandle);
			pPvtData->pcmHandle = NULL;
			snd_pcm_hw_params_free(hwParams);
			hwParams = NULL;
			AVB_TRACE_EXIT(AVB_TRACE_INTF);
			return;
		}
		pPvtData->periodTimeUsec = period_time;

		rslt = snd_pcm_hw_params_get_period_size(hwParams, &period_size, &dir);
		if (rslt < 0) {
			AVB_LOGF_ERROR("snd_pcm_hw_params_get_period_size() error: %s", snd_strerror(rslt));
			snd_pcm_close(pPvtData->pcmHandle);
			pPvtData->pcmHandle = NULL;
			snd_pcm_hw_params_free(hwParams);
			hwParams = NULL;
			AVB_TRACE_EXIT(AVB_TRACE_INTF);
			return;
		}

		// Give the hardware parameters to ALSA
		rslt = snd_pcm_hw_params(pPvtData->pcmHandle, hwParams);
		if (rslt < 0) {
			AVB_LOGF_ERROR("snd_pcm_hw_params() error: %s", snd_strerror(rslt));
			snd_pcm_close(pPvtData->pcmHandle);
			pPvtData->pcmHandle = NULL;
			snd_pcm_hw_params_free(hwParams);
			hwParams = NULL;
			AVB_TRACE_EXIT(AVB_TRACE_INTF);
			return;
		}

		// Free the hardware parameters
		snd_pcm_hw_params_free(hwParams);
		hwParams = NULL;


		// Set software parameters

		// Allocate the parameter structure
		rslt = snd_pcm_sw_params_malloc(&swParams);
		if (rslt < 0) {
			AVB_LOGF_ERROR("snd_pcm_sw_params_malloc error(): %s", snd_strerror(rslt));
			snd_pcm_close(pPvtData->pcmHandle);
			pPvtData->pcmHandle = NULL;
			snd_pcm_sw_params_free(swParams);
			swParams = NULL;
			AVB_TRACE_EXIT(AVB_TRACE_INTF);
			return;
		}

		rslt = snd_pcm_sw_params_current(pPvtData->pcmHandle, swParams);
		if (rslt < 0) {
			AVB_LOGF_ERROR("snd_pcm_sw_params_current error(): %s", snd_strerror(rslt));
			snd_pcm_close(pPvtData->pcmHandle);
			pPvtData->pcmHandle = NULL;
			snd_pcm_sw_params_free(swParams);
			swParams = NULL;
			AVB_TRACE_EXIT(AVB_TRACE_INTF);
			return;
		}

		rslt = snd_pcm_sw_params_set_start_threshold(pPvtData->pcmHandle, swParams, period_size * pPvtData->startThresholdPeriods);
		if (rslt < 0) {
			AVB_LOGF_ERROR("snd_pcm_sw_params_set_start_threshold error(): %s", snd_strerror(rslt));
			snd_pcm_close(pPvtData->pcmHandle);
			pPvtData->pcmHandle = NULL;
			snd_pcm_sw_params_free(swParams);
			swParams = NULL;
			AVB_TRACE_EXIT(AVB_TRACE_INTF);
			return;
		}

		rslt = snd_pcm_sw_params_set_avail_min(pPvtData->pcmHandle, swParams, period_event ? buffer_size : period_size);
		if (rslt < 0) {
			AVB_LOGF_ERROR("snd_pcm_sw_params_set_avail_min error(): %s", snd_strerror(rslt));
			snd_pcm_close(pPvtData->pcmHandle);
			pPvtData->pcmHandle = NULL;
			snd_pcm_sw_params_free(swParams);
			swParams = NULL;
			AVB_TRACE_EXIT(AVB_TRACE_INTF);
			return;
		}

		rslt = snd_pcm_sw_params_set_period_event(pPvtData->pcmHandle, swParams, 1);
		if (rslt < 0) {
			AVB_LOGF_ERROR("snd_pcm_sw_params_set_period_event error(): %s", snd_strerror(rslt));
			snd_pcm_close(pPvtData->pcmHandle);
			pPvtData->pcmHandle = NULL;
			snd_pcm_sw_params_free(swParams);
			swParams = NULL;
			AVB_TRACE_EXIT(AVB_TRACE_INTF);
			return;
		}

		rslt = snd_pcm_sw_params(pPvtData->pcmHandle, swParams);
		if (rslt < 0) {
			AVB_LOGF_ERROR("snd_pcm_sw_params error(): %s", snd_strerror(rslt));
			snd_pcm_close(pPvtData->pcmHandle);
			pPvtData->pcmHandle = NULL;
			snd_pcm_sw_params_free(swParams);
			swParams = NULL;
			AVB_TRACE_EXIT(AVB_TRACE_INTF);
			return;
		}

		// Free the hardware parameters
		snd_pcm_sw_params_free(swParams);
		swParams = NULL;


		// Get ready for playback
		rslt = snd_pcm_prepare(pPvtData->pcmHandle);
		if (rslt < 0) {
			AVB_LOGF_ERROR("snd_pcm_prepare() error: %s", snd_strerror(rslt));
			snd_pcm_close(pPvtData->pcmHandle);
			pPvtData->pcmHandle = NULL;
			AVB_TRACE_EXIT(AVB_TRACE_INTF);
			return;
		}

		// Dump settings
		snd_output_t* out;
		snd_output_stdio_attach(&out, stderr, 0);
		snd_pcm_dump(pPvtData->pcmHandle, out);
		snd_output_close(out);
	}

	AVB_TRACE_EXIT(AVB_TRACE_INTF);
}

// This callback is called when acting as a listener.
bool openavbIntfAlsaRxCB(media_q_t *pMediaQ)
{
	AVB_TRACE_ENTRY(AVB_TRACE_INTF_DETAIL);

	if (pMediaQ) {
		media_q_pub_map_uncmp_audio_info_t *pPubMapUncmpAudioInfo = pMediaQ->pPubMapInfo;
		pvt_data_t *pPvtData = pMediaQ->pPvtIntfInfo;
		if (!pPvtData) {
			AVB_LOG_ERROR("Private interface module data not allocated.");
			AVB_TRACE_EXIT(AVB_TRACE_INTF_DETAIL);
			return FALSE;
		}

		bool moreItems = TRUE;

		while (moreItems) {
			media_q_item_t *pMediaQItem = openavbMediaQTailLock(pMediaQ, pPvtData->ignoreTimestamp);
			if (pMediaQItem) {
				if (pMediaQItem->dataLen) {
					S32 rslt;

					rslt = snd_pcm_writei(pPvtData->pcmHandle, pMediaQItem->pPubData, pPubMapUncmpAudioInfo->framesPerItem);
					if (rslt < 0) {
						AVB_LOGF_ERROR("snd_pcm_writei: %s", snd_strerror(rslt));
						rslt = snd_pcm_recover(pPvtData->pcmHandle, rslt, 0);
						if (rslt < 0) {
							AVB_LOGF_ERROR("snd_pcm_recover: %s", snd_strerror(rslt));
						}
						rslt = snd_pcm_writei(pPvtData->pcmHandle, pMediaQItem->pPubData, pPubMapUncmpAudioInfo->framesPerItem);
					}
					if (rslt != pPubMapUncmpAudioInfo->framesPerItem) {
						AVB_LOGF_WARNING("Not all pcm data consumed written:%u  consumed:%u", pMediaQItem->dataLen, rslt * pPubMapUncmpAudioInfo->audioChannels);
					}

					// DEBUG
					// rslt = snd_pcm_avail(pPvtData->pcmHandle);
					// AVB_LOGF_INFO("%d\n", rslt);

				}
				else {
				}
				openavbMediaQTailPull(pMediaQ);
			}
			else {
				moreItems = FALSE;
			}
		}
	}

	AVB_TRACE_EXIT(AVB_TRACE_INTF_DETAIL);
	return TRUE;
}

// This callback will be called when the interface needs to be closed. All shutdown should
// occur in this function.
void openavbIntfAlsaEndCB(media_q_t *pMediaQ)
{
	AVB_TRACE_ENTRY(AVB_TRACE_INTF);

	if (pMediaQ) {
		pvt_data_t *pPvtData = pMediaQ->pPvtIntfInfo;
		if (!pPvtData) {
			AVB_LOG_ERROR("Private interface module data not allocated.");
			return;
		}

		if (pPvtData->pcmHandle) {
			snd_pcm_close(pPvtData->pcmHandle);
			pPvtData->pcmHandle = NULL;

#if 0
			// Optional call when using Valgrind to stop reports of memory leaks.
			snd_config_update_free_global();
#endif
		}
	}

	AVB_TRACE_EXIT(AVB_TRACE_INTF);
}

void openavbIntfAlsaGenEndCB(media_q_t *pMediaQ)
{
	AVB_TRACE_ENTRY(AVB_TRACE_INTF);
	AVB_TRACE_EXIT(AVB_TRACE_INTF);
}

void openavbIntfAlsaEnableFixedTimestamp(media_q_t *pMediaQ, bool enabled, U32 transmitInterval, U32 batchFactor)
{
	AVB_TRACE_ENTRY(AVB_TRACE_INTF);
	if (pMediaQ && pMediaQ->pPvtIntfInfo && pMediaQ->pPubMapInfo) {
		media_q_pub_map_uncmp_audio_info_t *pPubMapUncmpAudioInfo = pMediaQ->pPubMapInfo;
		pvt_data_t *pPvtData = (pvt_data_t *)pMediaQ->pPvtIntfInfo;

		pPvtData->fixedTimestampEnabled = enabled;
		if (pPvtData->fixedTimestampEnabled) {
			U32 per, rate, rem;
			S32 skewEst = pPvtData->clockSkewPPB;
			/* Ignore passed in transmit interval and use framesPerItem and audioRate so
			   we work with both AAF and 61883-6 */
			/* Carefully scale values to avoid U32 overflow or loss of precision */
			per = MICROSECONDS_PER_SECOND * pPubMapUncmpAudioInfo->framesPerItem * 10;
			per += (skewEst/10);
			rate = pPvtData->audioRate/100;
			transmitInterval = per/rate;
			rem = per % rate;
			if (rem != 0) {
				rem *= 10;
				rem /= rate;
			}
			openavbMcsInit(&pPvtData->mcs, transmitInterval, rem, 10);
			AVB_LOGF_INFO("Fixed timestamping enabled: %d %d/%d", transmitInterval, rem, 10);
		}

	}


	AVB_TRACE_EXIT(AVB_TRACE_INTF);
}

// Main initialization entry point into the interface module
extern DLL_EXPORT bool openavbIntfAlsaInitialize(media_q_t *pMediaQ, openavb_intf_cb_t *pIntfCB)
{
	AVB_TRACE_ENTRY(AVB_TRACE_INTF);

	if (pMediaQ) {
		pMediaQ->pPvtIntfInfo = calloc(1, sizeof(pvt_data_t));		// Memory freed by the media queue when the media queue is destroyed.

		if (!pMediaQ->pPvtIntfInfo) {
			AVB_LOG_ERROR("Unable to allocate memory for AVTP interface module.");
			return FALSE;
		}

		pvt_data_t *pPvtData = pMediaQ->pPvtIntfInfo;

		pIntfCB->intf_cfg_cb = openavbIntfAlsaCfgCB;
		pIntfCB->intf_gen_init_cb = openavbIntfAlsaGenInitCB;
		pIntfCB->intf_tx_init_cb = openavbIntfAlsaTxInitCB;
		pIntfCB->intf_tx_cb = openavbIntfAlsaTxCB;
		pIntfCB->intf_rx_init_cb = openavbIntfAlsaRxInitCB;
		pIntfCB->intf_rx_cb = openavbIntfAlsaRxCB;
		pIntfCB->intf_end_cb = openavbIntfAlsaEndCB;
		pIntfCB->intf_gen_end_cb = openavbIntfAlsaGenEndCB;
		pIntfCB->intf_enable_fixed_timestamp = openavbIntfAlsaEnableFixedTimestamp;

		pPvtData->ignoreTimestamp = FALSE;
		pPvtData->pDeviceName = strdup(PCM_DEVICE_NAME_DEFAULT);
		pPvtData->allowResampling = TRUE;
		pPvtData->intervalCounter = 0;
		pPvtData->startThresholdPeriods = 2;	// Default to 2 periods of frames as the start threshold
		pPvtData->periodTimeUsec = 100000;

		pPvtData->fixedTimestampEnabled = FALSE;
		pPvtData->clockSkewPPB = 0;
	}

	AVB_TRACE_EXIT(AVB_TRACE_INTF);
	return TRUE;
}