45
45
#include "third_party/utlist/utlist.h"
46
46
47
47
#define NUM_OPEN_DEVS_MAX 10
48
+ #define NUM_STREAMS_ATTACHED_MAX 256
48
49
#define NUM_FLOOP_PAIRS_MAX 20
49
50
50
51
#define FOR_ALL_DEVS (list , dir , tmp , func ) \
@@ -885,20 +886,24 @@ static void possibly_enable_fallback(enum CRAS_STREAM_DIRECTION dir,
885
886
}
886
887
887
888
/*
888
- * Adds stream to one or more open iodevs. If the stream has processing effect
889
+ * Adds streams to one or more open iodevs. If a stream has processing effect
889
890
* turned on, create new APM instance and add to the list. This makes sure the
890
891
* time consuming APM creation happens in main thread.
891
892
*/
892
- static int add_stream_to_open_devs (struct cras_rstream * stream ,
893
- struct cras_iodev * * iodevs ,
894
- unsigned int num_iodevs ) {
895
- int i ;
896
- if (stream -> stream_apm ) {
897
- for (i = 0 ; i < num_iodevs ; i ++ ) {
898
- cras_stream_apm_add (stream -> stream_apm , iodevs [i ], iodevs [i ]-> format );
893
+ static int add_streams_to_open_devs (struct cras_rstream * * streams ,
894
+ unsigned int num_streams ,
895
+ struct cras_iodev * * iodevs ,
896
+ unsigned int num_iodevs ) {
897
+ for (int j = 0 ; j < num_streams ; j ++ ) {
898
+ if (streams [j ]-> stream_apm ) {
899
+ for (int i = 0 ; i < num_iodevs ; i ++ ) {
900
+ cras_stream_apm_add (streams [j ]-> stream_apm , iodevs [i ],
901
+ iodevs [i ]-> format );
902
+ }
899
903
}
900
904
}
901
- return audio_thread_add_stream (audio_thread , stream , iodevs , num_iodevs );
905
+ return audio_thread_add_streams (audio_thread , streams , num_streams , iodevs ,
906
+ num_iodevs );
902
907
}
903
908
904
909
// Returns true if dev is one of the fallback devices.
@@ -910,8 +915,10 @@ static inline bool is_fallback_dev(struct cras_iodev* dev) {
910
915
static int init_and_attach_streams (struct cras_iodev * dev ) {
911
916
int rc ;
912
917
enum CRAS_STREAM_DIRECTION dir = dev -> direction ;
918
+ struct cras_rstream * streams [NUM_STREAMS_ATTACHED_MAX ];
913
919
struct cras_rstream * stream ;
914
920
int dev_enabled = cras_iodev_list_dev_is_enabled (dev );
921
+ unsigned int num_streams = 0 ;
915
922
916
923
/* If called after suspend, for example bluetooth
917
924
* profile switching, don't add back the stream list. */
@@ -965,8 +972,16 @@ static int init_and_attach_streams(struct cras_iodev* dev) {
965
972
syslog (LOG_WARNING , "Enable %s failed, rc = %d" , dev -> info .name , rc );
966
973
return rc ;
967
974
}
968
- add_stream_to_open_devs (stream , & dev , 1 );
975
+ if (num_streams == NUM_STREAMS_ATTACHED_MAX ) {
976
+ syslog (LOG_ERR , "The number of streams exceeds the limitation." );
977
+ break ;
978
+ }
979
+ streams [num_streams ++ ] = stream ;
969
980
}
981
+ if (num_streams ) {
982
+ add_streams_to_open_devs (streams , num_streams , & dev , 1 );
983
+ }
984
+
970
985
return 0 ;
971
986
}
972
987
@@ -1139,7 +1154,7 @@ static int pinned_stream_added(struct cras_rstream* rstream) {
1139
1154
1140
1155
// Attach the stream to devices that are opened successfully on the first try.
1141
1156
if (num_open_devs ) {
1142
- int rc = add_stream_to_open_devs ( rstream , open_devs , num_open_devs );
1157
+ int rc = add_streams_to_open_devs ( & rstream , 1 , open_devs , num_open_devs );
1143
1158
if (rc ) {
1144
1159
syslog (LOG_ERR , "Adding pinned stream to thread failed, rc %d" , rc );
1145
1160
if (!first_err ) {
@@ -1238,7 +1253,8 @@ static int stream_added_cb(struct cras_rstream* rstream) {
1238
1253
// Catch the stream with fallback if it is already enabled
1239
1254
if (cras_iodev_list_dev_is_enabled (fallback_devs [rstream -> direction ])) {
1240
1255
init_device (fallback_devs [rstream -> direction ], rstream );
1241
- add_stream_to_open_devs (rstream , & fallback_devs [rstream -> direction ], 1 );
1256
+ add_streams_to_open_devs (& rstream , 1 , & fallback_devs [rstream -> direction ],
1257
+ 1 );
1242
1258
}
1243
1259
1244
1260
/* Add the new stream to all enabled iodevs at once to avoid offset
@@ -1329,7 +1345,7 @@ static int stream_added_cb(struct cras_rstream* rstream) {
1329
1345
/* Add stream failure is considered critical because it'll
1330
1346
* trigger client side error. Collect the error type and send
1331
1347
* for UMA metrics. */
1332
- rc = add_stream_to_open_devs ( rstream , iodevs , num_iodevs );
1348
+ rc = add_streams_to_open_devs ( & rstream , 1 , iodevs , num_iodevs );
1333
1349
if (rc == - EIO ) {
1334
1350
cras_server_metrics_stream_add_failure (CRAS_STREAM_ADD_IO_ERROR );
1335
1351
} else if (rc == - EINVAL ) {
@@ -2044,7 +2060,7 @@ int cras_iodev_list_suspend_hotword_streams() {
2044
2060
}
2045
2061
2046
2062
audio_thread_disconnect_stream (audio_thread , stream , hotword_dev );
2047
- audio_thread_add_stream (audio_thread , stream , & empty_hotword_dev , 1 );
2063
+ audio_thread_add_streams (audio_thread , & stream , 1 , & empty_hotword_dev , 1 );
2048
2064
}
2049
2065
close_pinned_device (hotword_dev );
2050
2066
hotword_suspended = 1 ;
@@ -2078,7 +2094,7 @@ int cras_iodev_list_resume_hotword_stream() {
2078
2094
}
2079
2095
2080
2096
audio_thread_disconnect_stream (audio_thread , stream , empty_hotword_dev );
2081
- audio_thread_add_stream (audio_thread , stream , & hotword_dev , 1 );
2097
+ audio_thread_add_streams (audio_thread , & stream , 1 , & hotword_dev , 1 );
2082
2098
}
2083
2099
close_pinned_device (empty_hotword_dev );
2084
2100
hotword_suspended = 0 ;
@@ -2638,7 +2654,7 @@ static int remove_then_reconnect_stream(struct cras_rstream* rstream) {
2638
2654
cras_stream_apm_remove (rstream -> stream_apm , iodevs [i ]);
2639
2655
}
2640
2656
2641
- return add_stream_to_open_devs ( rstream , iodevs , num_iodevs );
2657
+ return add_streams_to_open_devs ( & rstream , 1 , iodevs , num_iodevs );
2642
2658
}
2643
2659
2644
2660
int cras_iodev_list_set_aec_ref (unsigned int stream_id , unsigned int dev_idx ) {
@@ -2781,7 +2797,7 @@ void cras_iodev_list_enable_floop_pair(struct cras_floop_pair* pair) {
2781
2797
continue ;
2782
2798
}
2783
2799
struct cras_iodev * devs [] = {& pair -> output };
2784
- add_stream_to_open_devs ( stream , devs , 1 );
2800
+ add_streams_to_open_devs ( & stream , 1 , devs , 1 );
2785
2801
}
2786
2802
}
2787
2803
}
0 commit comments