-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPal.cpp
1233 lines (1079 loc) · 38.2 KB
/
Pal.cpp
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
/*
* Copyright (c) 2019-2021, The Linux Foundation. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are
* met:
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * 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.
* * Neither the name of The Linux Foundation nor the names of its
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
* 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.
*
* Changes from Qualcomm Innovation Center are provided under the following license:
*
* Copyright (c) 2022 Qualcomm Innovation Center, Inc. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted (subject to the limitations in the
* disclaimer below) provided that the following conditions are met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* * 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.
*
* * Neither the name of Qualcomm Innovation Center, Inc. nor the names of its
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE
* GRANTED BY THIS LICENSE. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT
* HOLDERS AND CONTRIBUTORS "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 HOLDER OR CONTRIBUTORS 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.
*/
#define LOG_TAG "PAL: API"
#include <set>
#include <unistd.h>
#include <stdlib.h>
#include <PalApi.h>
#include "Stream.h"
#include "Device.h"
#include "ResourceManager.h"
#include "PalCommon.h"
class Stream;
/**
* Get PAL version in the form of Major and Minor number
* seperated by period.
*
* @return the version string in the form of Major and Minor
* e.g '1.0'
*/
const char* pal_get_version( ){
return PAL_VERSION;
}
static void notify_concurrent_stream(pal_stream_type_t type,
pal_stream_direction_t dir,
bool active)
{
std::shared_ptr<ResourceManager> rm = ResourceManager::getInstance();
if (!rm) {
PAL_ERR(LOG_TAG,"Resource manager unavailable");
return;
}
PAL_DBG(LOG_TAG, "Notify concurrent stream type %d, direction %d, active %d",
type, dir, active);
rm->ConcurrentStreamStatus(type, dir, active);
}
/*
* pal_init - Initialize PAL
*
* Return 0 on success or error code otherwise
*
* Prerequisites
* None.
*/
int32_t pal_init(void)
{
PAL_DBG(LOG_TAG, "Enter.");
int32_t ret = 0;
std::shared_ptr<ResourceManager> ri = NULL;
try {
ri = ResourceManager::getInstance();
} catch (const std::exception& e) {
PAL_ERR(LOG_TAG, "pal init failed: %s", e.what());
ret = -EINVAL;
goto exit;
}
ret = ri->initSndMonitor();
if (ret != 0) {
PAL_ERR(LOG_TAG, "snd monitor init failed");
goto exit;
}
ri->init();
ret = ri->initContextManager();
if (ret != 0) {
PAL_ERR(LOG_TAG, "ContextManager init failed, error:%d", ret);
goto exit;
}
exit:
PAL_DBG(LOG_TAG, "Exit. exit status : %d ", ret);
return ret;
}
/*
* pal_deinit - De-initialize PAL
*
* Prerequisites
* PAL must be initialized.
*/
void pal_deinit(void)
{
PAL_DBG(LOG_TAG, "Enter.");
std::shared_ptr<ResourceManager> ri = NULL;
try {
ri = ResourceManager::getInstance();
} catch (const std::exception& e) {
PAL_ERR(LOG_TAG, "ResourceManager::getInstance() failed: %s", e.what());
}
ri->deInitContextManager();
ResourceManager::deinit();
PAL_DBG(LOG_TAG, "Exit.");
return;
}
int32_t pal_stream_open(struct pal_stream_attributes *attributes,
uint32_t no_of_devices, struct pal_device *devices,
uint32_t no_of_modifiers, struct modifier_kv *modifiers,
pal_stream_callback cb, uint64_t cookie,
pal_stream_handle_t **stream_handle)
{
uint64_t *stream = NULL;
Stream *s = NULL;
int status;
struct pal_stream_attributes sAttr;
std::shared_ptr<ResourceManager> rm = NULL;
rm = ResourceManager::getInstance();
if (!rm) {
PAL_ERR(LOG_TAG, "Invalid resource manager");
status = -EINVAL;
return status;
}
if (!attributes) {
status = -EINVAL;
PAL_ERR(LOG_TAG, "Invalid input parameters status %d", status);
return status;
}
PAL_INFO(LOG_TAG, "Enter, stream type:%d", attributes->type);
#ifdef SOC_PERIPHERAL_PROT
if (ResourceManager::isTZSecureZone) {
PAL_DBG(LOG_TAG, "In secure zone, so stop the usecase");
status = -ENODEV;
goto exit;
}
#endif
try {
s = Stream::create(attributes, devices, no_of_devices, modifiers,
no_of_modifiers);
} catch (const std::exception& e) {
status = -EINVAL;
PAL_ERR(LOG_TAG, "Stream create failed: %s", e.what());
Stream::handleStreamException(attributes, cb, cookie);
goto exit;
}
if (!s) {
status = -EINVAL;
PAL_ERR(LOG_TAG, "stream creation failed status %d", status);
goto exit;
}
status = s->open();
if (0 != status) {
PAL_ERR(LOG_TAG, "pal_stream_open failed with status %d", status);
if (s->close() != 0) {
PAL_ERR(LOG_TAG, "stream closed failed.");
}
delete s;
goto exit;
}
s->getStreamAttributes(&sAttr);
notify_concurrent_stream(sAttr.type, sAttr.direction, true);
if (cb)
s->registerCallBack(cb, cookie);
rm->initStreamUserCounter(s);
stream = reinterpret_cast<uint64_t *>(s);
*stream_handle = stream;
exit:
PAL_INFO(LOG_TAG, "Exit. Value of stream_handle %pK, status %d", stream, status);
return status;
}
int32_t pal_stream_close(pal_stream_handle_t *stream_handle)
{
Stream *s = NULL;
int status;
struct pal_stream_attributes sAttr;
std::shared_ptr<ResourceManager> rm = NULL;
if (!stream_handle) {
status = -EINVAL;
PAL_ERR(LOG_TAG, "Invalid stream handle status %d", status);
return status;
}
PAL_INFO(LOG_TAG, "Enter. Stream handle :%pK", stream_handle);
rm = ResourceManager::getInstance();
if (!rm) {
PAL_ERR(LOG_TAG, "Invalid resource manager");
status = -EINVAL;
return status;
}
rm->lockActiveStream();
if (!rm->isActiveStream(stream_handle)) {
status = -EINVAL;
rm->unlockActiveStream();
return status;
}
rm->unlockActiveStream();
s = reinterpret_cast<Stream *>(stream_handle);
s->setCachedState(STREAM_IDLE);
status = s->close();
if (rm->deactivateStreamUserCounter(s)) {
PAL_ERR(LOG_TAG, "stream is being closed by another client");
return 0;
}
if (0 != status) {
PAL_ERR(LOG_TAG, "stream closed failed. status %d", status);
goto exit;
}
exit:
s->getStreamAttributes(&sAttr);
notify_concurrent_stream(sAttr.type, sAttr.direction, false);
delete s;
rm->eraseStreamUserCounter(s);
PAL_INFO(LOG_TAG, "Exit. status %d", status);
return status;
}
int32_t pal_stream_start(pal_stream_handle_t *stream_handle)
{
Stream *s = NULL;
std::shared_ptr<ResourceManager> rm = NULL;
int status;
if (!stream_handle) {
status = -EINVAL;
PAL_ERR(LOG_TAG, "Invalid stream handle status %d", status);
return status;
}
PAL_INFO(LOG_TAG, "Enter. Stream handle %pK", stream_handle);
#ifdef SOC_PERIPHERAL_PROT
if (ResourceManager::isTZSecureZone) {
PAL_DBG(LOG_TAG, "In secure zone, so stop the usecase");
status = -ENODEV;
goto exit;
}
#endif
rm = ResourceManager::getInstance();
if (!rm) {
PAL_ERR(LOG_TAG, "Invalid resource manager");
status = -EINVAL;
goto exit;
}
rm->lockActiveStream();
if (!rm->isActiveStream(stream_handle)) {
rm->unlockActiveStream();
status = -EINVAL;
goto exit;
}
s = reinterpret_cast<Stream *>(stream_handle);
status = rm->increaseStreamUserCounter(s);
if (0 != status) {
rm->unlockActiveStream();
PAL_ERR(LOG_TAG, "failed to increase stream user count");
goto exit;
}
rm->unlockActiveStream();
status = s->start();
rm->lockActiveStream();
rm->decreaseStreamUserCounter(s);
rm->unlockActiveStream();
if (0 != status) {
PAL_ERR(LOG_TAG, "stream start failed. status %d", status);
goto exit;
}
exit:
PAL_INFO(LOG_TAG, "Exit. status %d", status);
return status;
}
int32_t pal_stream_stop(pal_stream_handle_t *stream_handle)
{
Stream *s = NULL;
std::shared_ptr<ResourceManager> rm = NULL;
int status;
if (!stream_handle) {
status = -EINVAL;
PAL_ERR(LOG_TAG, "Invalid stream handle status %d", status);
return status;
}
PAL_INFO(LOG_TAG, "Enter. Stream handle :%pK", stream_handle);
rm = ResourceManager::getInstance();
if (!rm) {
PAL_ERR(LOG_TAG, "Invalid resource manager");
status = -EINVAL;
goto exit;
}
rm->lockActiveStream();
if (!rm->isActiveStream(stream_handle)) {
rm->unlockActiveStream();
status = -EINVAL;
goto exit;
}
s = reinterpret_cast<Stream *>(stream_handle);
status = rm->increaseStreamUserCounter(s);
if (0 != status) {
rm->unlockActiveStream();
PAL_ERR(LOG_TAG, "failed to increase stream user count");
goto exit;
}
rm->unlockActiveStream();
s->setCachedState(STREAM_STOPPED);
status = s->stop();
rm->lockActiveStream();
rm->decreaseStreamUserCounter(s);
rm->unlockActiveStream();
if (0 != status) {
PAL_ERR(LOG_TAG, "stream stop failed. status : %d", status);
goto exit;
}
exit:
PAL_INFO(LOG_TAG, "Exit. status %d", status);
return status;
}
ssize_t pal_stream_write(pal_stream_handle_t *stream_handle, struct pal_buffer *buf)
{
Stream *s = NULL;
int status;
if (!stream_handle || !buf) {
status = -EINVAL;
PAL_ERR(LOG_TAG, "Invalid input parameters status %d", status);
return status;
}
PAL_VERBOSE(LOG_TAG, "Enter. Stream handle :%pK", stream_handle);
s = reinterpret_cast<Stream *>(stream_handle);
status = s->write(buf);
if (status < 0) {
PAL_ERR(LOG_TAG, "stream write failed status %d", status);
return status;
}
PAL_VERBOSE(LOG_TAG, "Exit. status %d", status);
return status;
}
ssize_t pal_stream_read(pal_stream_handle_t *stream_handle, struct pal_buffer *buf)
{
Stream *s = NULL;
int status;
if (!stream_handle || !buf) {
status = -EINVAL;
PAL_ERR(LOG_TAG, "Invalid input parameters status %d", status);
return status;
}
PAL_VERBOSE(LOG_TAG, "Enter. Stream handle :%pK", stream_handle);
s = reinterpret_cast<Stream *>(stream_handle);
status = s->read(buf);
if (status < 0) {
PAL_ERR(LOG_TAG, "stream read failed status %d", status);
return status;
}
PAL_VERBOSE(LOG_TAG, "Exit. status %d", status);
return status;
}
int32_t pal_stream_get_param(pal_stream_handle_t *stream_handle,
uint32_t param_id, pal_param_payload **param_payload)
{
Stream *s = NULL;
int status;
if (!stream_handle) {
status = -EINVAL;
PAL_ERR(LOG_TAG, "Invalid input parameters status %d", status);
return status;
}
PAL_DBG(LOG_TAG, "Enter. Stream handle :%pK", stream_handle);
s = reinterpret_cast<Stream *>(stream_handle);
status = s->getParameters(param_id, (void **)param_payload);
if (0 != status) {
PAL_ERR(LOG_TAG, "get parameters failed status %d param_id %u", status, param_id);
return status;
}
PAL_DBG(LOG_TAG, "Exit. status %d", status);
return status;
}
int32_t pal_stream_set_param(pal_stream_handle_t *stream_handle, uint32_t param_id,
pal_param_payload *param_payload)
{
Stream *s = NULL;
int status;
std::shared_ptr<ResourceManager> rm = NULL;
if (!stream_handle) {
status = -EINVAL;
PAL_ERR(LOG_TAG, "Invalid stream handle, status %d", status);
return status;
}
PAL_DBG(LOG_TAG, "Enter. Stream handle :%pK param_id %d", stream_handle,
param_id);
s = reinterpret_cast<Stream *>(stream_handle);
if (PAL_PARAM_ID_UIEFFECT == param_id) {
status = s->setEffectParameters((void *)param_payload);
} else {
status = s->setParameters(param_id, (void *)param_payload);
}
if (0 != status) {
PAL_ERR(LOG_TAG, "set parameters failed status %d param_id %u", status, param_id);
return status;
}
rm = ResourceManager::getInstance();
if (!rm) {
status = -EINVAL;
PAL_ERR(LOG_TAG, "Invalid resource manager");
return status;
}
if (param_id == PAL_PARAM_ID_STOP_BUFFERING) {
PAL_DBG(LOG_TAG, "Buffering stopped, handle deferred LPI<->NLPI switch");
rm->handleDeferredSwitch();
}
PAL_DBG(LOG_TAG, "Exit. status %d", status);
return status;
}
int32_t pal_stream_set_volume(pal_stream_handle_t *stream_handle,
struct pal_volume_data *volume)
{
Stream *s = NULL;
int status;
std::shared_ptr<ResourceManager> rm = NULL;
rm = ResourceManager::getInstance();
if (!rm) {
PAL_ERR(LOG_TAG, "Invalid resource manager");
status = -EINVAL;
return status;
}
if (!stream_handle || !volume) {
status = -EINVAL;
PAL_ERR(LOG_TAG,"Invalid input parameters status %d", status);
return status;
}
PAL_DBG(LOG_TAG, "Enter. Stream handle :%pK", stream_handle);
rm->lockActiveStream();
if (!rm->isActiveStream(stream_handle)) {
rm->unlockActiveStream();
status = -EINVAL;
return status;
}
s = reinterpret_cast<Stream *>(stream_handle);
status = rm->increaseStreamUserCounter(s);
if (0 != status) {
rm->unlockActiveStream();
PAL_ERR(LOG_TAG, "failed to increase stream user count");
return status;
}
rm->unlockActiveStream();
s->lockStreamMutex();
status = s->setVolume(volume);
s->unlockStreamMutex();
rm->lockActiveStream();
rm->decreaseStreamUserCounter(s);
rm->unlockActiveStream();
if (0 != status) {
PAL_ERR(LOG_TAG, "setVolume failed with status %d", status);
return status;
}
PAL_DBG(LOG_TAG, "Exit. status %d", status);
return status;
}
int32_t pal_stream_set_mute(pal_stream_handle_t *stream_handle, bool state)
{
Stream *s = NULL;
std::shared_ptr<ResourceManager> rm = NULL;
int status = 0;
if (!stream_handle) {
status = -EINVAL;
PAL_ERR(LOG_TAG, "Invalid stream handle status %d", status);
return status;
}
rm = ResourceManager::getInstance();
if (!rm) {
status = -EINVAL;
PAL_ERR(LOG_TAG, "Invalid resource manager");
return status;
}
PAL_DBG(LOG_TAG, "Enter. Stream handle :%pK", stream_handle);
rm->lockActiveStream();
if (!rm->isActiveStream(stream_handle)) {
rm->unlockActiveStream();
status = -EINVAL;
goto exit;
}
s = reinterpret_cast<Stream *>(stream_handle);
status = rm->increaseStreamUserCounter(s);
if (0 != status) {
rm->unlockActiveStream();
PAL_ERR(LOG_TAG, "failed to increase stream user count");
goto exit;
}
rm->unlockActiveStream();
status = s->mute(state);
rm->lockActiveStream();
rm->decreaseStreamUserCounter(s);
rm->unlockActiveStream();
if (0 != status) {
PAL_ERR(LOG_TAG, "mute failed with status %d", status);
return status;
}
exit:
PAL_DBG(LOG_TAG, "Exit. status %d", status);
return status;
}
int32_t pal_stream_pause(pal_stream_handle_t *stream_handle)
{
Stream *s = NULL;
int status;
if (!stream_handle) {
status = -EINVAL;
PAL_ERR(LOG_TAG, "Invalid stream handle status %d", status);
return status;
}
PAL_DBG(LOG_TAG, "Enter. Stream handle :%pK", stream_handle);
s = reinterpret_cast<Stream *>(stream_handle);
status = s->pause();
if (0 != status) {
PAL_ERR(LOG_TAG, "pal_stream_pause failed with status %d", status);
return status;
}
PAL_DBG(LOG_TAG, "Exit. status %d", status);
return status;
}
int32_t pal_stream_resume(pal_stream_handle_t *stream_handle)
{
Stream *s = NULL;
int status;
if (!stream_handle) {
status = -EINVAL;
PAL_ERR(LOG_TAG, "Invalid stream handle status %d", status);
return status;
}
PAL_DBG(LOG_TAG, "Enter. Stream handle :%pK", stream_handle);
s = reinterpret_cast<Stream *>(stream_handle);
status = s->resume();
if (0 != status) {
PAL_ERR(LOG_TAG, "resume failed with status %d", status);
return status;
}
PAL_DBG(LOG_TAG, "Exit. status %d", status);
return status;
}
int32_t pal_stream_drain(pal_stream_handle_t *stream_handle, pal_drain_type_t type)
{
Stream *s = NULL;
int status;
std::shared_ptr<ResourceManager> rm = NULL;
if (!stream_handle) {
status = -EINVAL;
PAL_ERR(LOG_TAG, "Invalid stream handle status %d", status);
return status;
}
PAL_DBG(LOG_TAG, "Enter. Stream handle :%pK", stream_handle);
rm = ResourceManager::getInstance();
if (!rm) {
PAL_ERR(LOG_TAG, "Invalid resource manager");
status = -EINVAL;
goto exit;
}
rm->lockActiveStream();
if (!rm->isActiveStream(stream_handle)) {
rm->unlockActiveStream();
status = -EINVAL;
goto exit;
}
s = reinterpret_cast<Stream *>(stream_handle);
status = rm->increaseStreamUserCounter(s);
if (0 != status) {
rm->unlockActiveStream();
PAL_ERR(LOG_TAG, "failed to increase stream user count");
goto exit;
}
rm->unlockActiveStream();
status = s->drain(type);
rm->lockActiveStream();
rm->decreaseStreamUserCounter(s);
rm->unlockActiveStream();
if (0 != status) {
PAL_ERR(LOG_TAG, "drain failed with status %d", status);
return status;
}
exit:
PAL_DBG(LOG_TAG, "Exit. status %d", status);
return status;
}
int32_t pal_stream_flush(pal_stream_handle_t *stream_handle)
{
Stream *s = NULL;
int status;
if (!stream_handle) {
status = -EINVAL;
PAL_ERR(LOG_TAG, "Invalid stream handle status %d", status);
return status;
}
PAL_DBG(LOG_TAG, "Enter. Stream handle :%pK", stream_handle);
s = reinterpret_cast<Stream *>(stream_handle);
status = s->flush();
if (0 != status) {
PAL_ERR(LOG_TAG, "flush failed with status %d", status);
return status;
}
PAL_DBG(LOG_TAG, "Exit. status %d", status);
return status;
}
int32_t pal_stream_suspend(pal_stream_handle_t *stream_handle)
{
Stream *s = NULL;
int status;
if (!stream_handle) {
status = -EINVAL;
PAL_ERR(LOG_TAG, "Invalid stream handle status %d", status);
return status;
}
PAL_DBG(LOG_TAG, "Enter. Stream handle :%pK", stream_handle);
s = reinterpret_cast<Stream *>(stream_handle);
status = s->suspend();
if (0 != status) {
PAL_ERR(LOG_TAG, "suspend failed with status %d", status);
}
PAL_DBG(LOG_TAG, "Exit. status %d", status);
return status;
}
int32_t pal_stream_set_buffer_size (pal_stream_handle_t *stream_handle,
pal_buffer_config *in_buffer_cfg,
pal_buffer_config *out_buffer_cfg)
{
Stream *s = NULL;
int status;
if (!stream_handle) {
status = -EINVAL;
PAL_ERR(LOG_TAG, "Invalid input parameters status %d", status);
return status;
}
PAL_DBG(LOG_TAG, "Enter. Stream handle :%pK", stream_handle);
s = reinterpret_cast<Stream *>(stream_handle);
status = s->setBufInfo(in_buffer_cfg, out_buffer_cfg);
if (0 != status) {
PAL_ERR(LOG_TAG, "pal_stream_set_buffer_size failed with status %d", status);
return status;
}
PAL_DBG(LOG_TAG, "Exit. status %d", status);
return status;
}
int32_t pal_get_timestamp(pal_stream_handle_t *stream_handle,
struct pal_session_time *stime)
{
Stream *s = NULL;
int status = -EINVAL;
std::shared_ptr<ResourceManager> rm = NULL;
if (!stream_handle) {
PAL_ERR(LOG_TAG, "Invalid input parameters status %d\n", status);
return status;
}
PAL_DBG(LOG_TAG, "Enter. Stream handle :%pK\n", stream_handle);
rm = ResourceManager::getInstance();
if (!rm) {
PAL_ERR(LOG_TAG, "Invalid resource manager");
return status;
}
rm->lockActiveStream();
if (rm->isActiveStream(stream_handle)) {
s = reinterpret_cast<Stream *>(stream_handle);
status = s->getTimestamp(stime);
} else {
PAL_ERR(LOG_TAG, "stream handle in stale state.\n");
}
rm->unlockActiveStream();
if (0 != status) {
PAL_ERR(LOG_TAG, "pal_get_timestamp failed with status %d\n", status);
return status;
}
PAL_VERBOSE(LOG_TAG, "stime->session_time.value_lsw = %u, stime->session_time.value_msw = %u \n", stime->session_time.value_lsw, stime->session_time.value_msw);
PAL_VERBOSE(LOG_TAG, "stime->absolute_time.value_lsw = %u, stime->absolute_time.value_msw = %u \n", stime->absolute_time.value_lsw, stime->absolute_time.value_msw);
PAL_VERBOSE(LOG_TAG, "stime->timestamp.value_lsw = %u, stime->timestamp.value_msw = %u \n", stime->timestamp.value_lsw, stime->timestamp.value_msw);
PAL_DBG(LOG_TAG, "Exit. status %d", status);
return status;
}
int32_t pal_add_remove_effect(pal_stream_handle_t *stream_handle,
pal_audio_effect_t effect, bool enable)
{
Stream *s = NULL;
int status = 0;
if (!stream_handle) {
status = -EINVAL;
PAL_ERR(LOG_TAG, "Invalid stream handle status %d", status);
return status;
}
PAL_DBG(LOG_TAG, "Enter. Stream handle :%pK", stream_handle);
s = reinterpret_cast<Stream *>(stream_handle);
status = s->addRemoveEffect(effect, enable);
if (0 != status) {
PAL_ERR(LOG_TAG, "pal_add_effect failed with status %d", status);
return status;
}
PAL_DBG(LOG_TAG, "Exit. status %d", status);
return status;
}
int32_t pal_stream_set_device(pal_stream_handle_t *stream_handle,
uint32_t no_of_devices, struct pal_device *devices)
{
int status = -EINVAL;
Stream *s = NULL;
std::shared_ptr<ResourceManager> rm = NULL;
struct pal_stream_attributes sattr;
struct pal_device_info devinfo = {};
struct pal_device *pDevices = NULL;
struct pal_device curPalDevAttr;
std::vector <std::shared_ptr<Device>> aDevices, palDevices;
if (!stream_handle) {
status = -EINVAL;
PAL_ERR(LOG_TAG, "Invalid stream handle status %d", status);
return status;
}
PAL_INFO(LOG_TAG, "Enter. Stream handle :%pK", stream_handle);
if (no_of_devices == 0 || !devices) {
status = -EINVAL;
PAL_ERR(LOG_TAG, "Invalid device status %d", status);
return status;
}
rm = ResourceManager::getInstance();
if (!rm) {
status = -EINVAL;
PAL_ERR(LOG_TAG, "Invalid resource manager");
return status;
}
rm->lockActiveStream();
if (!rm->isActiveStream(stream_handle)) {
rm->unlockActiveStream();
status = -EINVAL;
return status;
}
/* Choose best device config for this stream */
/* TODO: Decide whether to update device config or not based on flag */
s = reinterpret_cast<Stream *>(stream_handle);
status = rm->increaseStreamUserCounter(s);
if (0 != status) {
rm->unlockActiveStream();
PAL_ERR(LOG_TAG, "failed to increase stream user count");
return status;
}
rm->unlockActiveStream();
s->getStreamAttributes(&sattr);
// device switch will be handled in global param setting for SVA
if (sattr.type == PAL_STREAM_VOICE_UI) {
PAL_DBG(LOG_TAG,
"Device switch handles in global param set, skip here");
goto exit;
}
if (sattr.type == PAL_STREAM_VOICE_CALL_RECORD ||
sattr.type == PAL_STREAM_VOICE_CALL_MUSIC) {
PAL_DBG(LOG_TAG,
"Device switch skipped for Incall record/music stream");
status = 0;
goto exit;
}
s->getAssociatedDevices(aDevices);
s->getPalDevices(palDevices);
if (!aDevices.empty() && !palDevices.empty()) {
std::set<pal_device_id_t> activeDevices, curPalDevices;
std::set<pal_device_id_t> newDevices;
bool force_switch = s->isA2dpMuted();
for (auto &dev : aDevices)
activeDevices.insert((pal_device_id_t)dev->getSndDeviceId());
for (auto &dev : palDevices) {
curPalDevices.insert((pal_device_id_t)dev->getSndDeviceId());
// check if custom key matches for same device
for (int i = 0; i < no_of_devices; i++) {
if (dev->getSndDeviceId() == devices[i].id) {
dev->getDeviceAttributes(&curPalDevAttr, s);
if (strcmp(devices[i].custom_config.custom_key,
curPalDevAttr.custom_config.custom_key) != 0) {
PAL_DBG(LOG_TAG, "diff custom key found, force device switch");
force_switch = true;
break;
}
}
}
if (force_switch)
break;
}
/*
* When USB headset is disconnected the music playback pauses
* and the policy manager sends routing=0. But if the USB is connected
* back before the standby time, it can not switch device to usb hs any more
* because current pal device is usb hs which equals new device when resuming playback.
* So routing to default device first during handling routing = 0 msg will guarantee
* the device switch to usb can be executed once USB is connected again.
*/
bool curPalDevice_usb = curPalDevices.find(PAL_DEVICE_OUT_USB_DEVICE) != curPalDevices.end() ||
curPalDevices.find(PAL_DEVICE_OUT_USB_HEADSET) != curPalDevices.end();
bool activeDevices_usb = activeDevices.find(PAL_DEVICE_OUT_USB_DEVICE) != activeDevices.end() ||
activeDevices.find(PAL_DEVICE_OUT_USB_HEADSET) != activeDevices.end();
bool usb_active = rm->isDeviceAvailable(PAL_DEVICE_OUT_USB_DEVICE) ||
rm->isDeviceAvailable(PAL_DEVICE_OUT_USB_HEADSET);
if (devices[0].id == PAL_DEVICE_NONE &&
curPalDevice_usb && activeDevices_usb && !usb_active)
{
devices[0].id = PAL_DEVICE_OUT_SPEAKER;
}
if (!force_switch) {
for (int i = 0; i < no_of_devices; i++) {
newDevices.insert(devices[i].id);
if ((devices[i].id == PAL_DEVICE_OUT_BLUETOOTH_A2DP) ||
(devices[i].id == PAL_DEVICE_OUT_BLUETOOTH_SCO) ||
(devices[i].id == PAL_DEVICE_IN_BLUETOOTH_SCO_HEADSET)) {
PAL_DBG(LOG_TAG, "always switch device for bt device");
force_switch = true;
break;
}
}
}
if (!force_switch && (activeDevices == newDevices) &&
(curPalDevices == newDevices)) {
status = 0;
PAL_DBG(LOG_TAG, "devices are same, no need to switch");
goto exit;
}
}
pDevices = (struct pal_device *) calloc(no_of_devices, sizeof(struct pal_device));
if (!pDevices) {
status = -ENOMEM;
PAL_ERR(LOG_TAG, "Memory alloc failed");
goto exit;
}
ar_mem_cpy(pDevices, no_of_devices * sizeof(struct pal_device),
devices, no_of_devices * sizeof(struct pal_device));
for (int i = 0; i < no_of_devices; i++) {
if (strlen(pDevices[i].custom_config.custom_key)) {
PAL_DBG(LOG_TAG, "Device has custom key %s",
pDevices[i].custom_config.custom_key);
} else {
PAL_DBG(LOG_TAG, "Device has no custom key");
strlcpy(pDevices[i].custom_config.custom_key, "", PAL_MAX_CUSTOM_KEY_SIZE);
}
status = rm->getDeviceConfig((struct pal_device *)&pDevices[i], &sattr);
if (status) {
PAL_ERR(LOG_TAG, "Failed to get Device config, err: %d", status);
goto exit;
}
}
// TODO: Check with RM if the same device is being used by other stream with different
// configuration then update corresponding stream device configuration also based on priority.
PAL_DBG(LOG_TAG, "Stream handle :%pK no_of_devices %d first_device id %d",
stream_handle, no_of_devices, pDevices[0].id);
status = s->switchDevice(s, no_of_devices, pDevices);
if (0 != status) {