1
1
/*
2
- * Copyright (c) 2023 Arm Limited. All rights reserved.
2
+ * Copyright (c) 2023-2024 Arm Limited. All rights reserved.
3
3
*
4
4
* SPDX-License-Identifier: Apache-2.0
5
5
*
@@ -48,17 +48,15 @@ typedef struct {
48
48
uint32_t buf_size ;
49
49
sdsId_t stream ;
50
50
sdsioId_t sdsio ;
51
- PlayHead_t head_in ;
52
51
PlayHead_t head_out ;
53
- volatile uint32_t cnt_in ;
54
- volatile uint32_t cnt_out ;
52
+ uint32_t record_data_cnt ;
55
53
} sdsPlay_t ;
56
54
57
55
static sdsPlay_t PlayStreams [SDS_PLAY_MAX_STREAMS ] = {0 };
58
56
static sdsPlay_t * pPlayStreams [SDS_PLAY_MAX_STREAMS ] = {NULL };
59
57
60
58
// Player buffer
61
- static uint8_t PlayBuf [SDS_PLAY_MAX_RECORD_SIZE ];
59
+ static uint8_t PlayBuf [SDS_PLAY_BUF_SIZE ];
62
60
63
61
// Event callback
64
62
static sdsPlayEvent_t sdsPlayEvent = NULL ;
@@ -153,7 +151,7 @@ static void sdsPlayEventCallback (sdsId_t id, uint32_t event, void *arg) {
153
151
// Player thread
154
152
static __NO_RETURN void sdsPlayThread (void * arg ) {
155
153
sdsPlay_t * play ;
156
- uint32_t size , num , flags , n ;
154
+ uint32_t flags , cnt , num , n ;
157
155
(void )arg ;
158
156
159
157
while (1 ) {
@@ -175,55 +173,30 @@ static __NO_RETURN void sdsPlayThread (void *arg) {
175
173
osEventFlagsSet (sdsPlayCloseEventFlags , 1U << play -> index );
176
174
continue ;
177
175
}
178
- do {
179
- if (play -> head_in .data_size == 0U ) {
180
- // Read new header
181
- num = sdsioRead (play -> sdsio , & play -> head_in , HEAD_SIZE );
182
- if (num == 0U ) {
183
- if (sdsioEndOfStream (play -> sdsio ) == 0 ) {
184
- if (sdsPlayEvent != NULL ) {
185
- sdsPlayEvent (play , SDS_PLAY_EVENT_IO_ERROR );
186
- }
187
- } else {
188
- play -> flags |= SDS_PLAY_FLAG_EOS ;
189
- }
190
- break ;
191
- }
192
176
193
- if (num == HEAD_SIZE ) {
194
- if (play -> head_in .data_size == 0U ) {
195
- break ;
196
- }
197
- } else {
198
- if (sdsPlayEvent != NULL ) {
199
- sdsPlayEvent (play , SDS_PLAY_EVENT_IO_ERROR );
200
- }
201
- break ;
202
- }
203
- }
204
-
205
- size = play -> head_in .data_size + HEAD_SIZE ;
206
- if (size > SDS_PLAY_MAX_RECORD_SIZE ) {
207
- if (sdsPlayEvent != NULL ) {
208
- sdsPlayEvent (play , SDS_PLAY_EVENT_IO_ERROR );
209
- }
177
+ while (1 ) {
178
+ cnt = play -> buf_size - sdsGetCount (play -> stream );
179
+ if (cnt == 0U ) {
210
180
break ;
211
181
}
212
- if (size <= (play -> buf_size - sdsGetCount (play -> stream ))) {
213
- memcpy (PlayBuf , & play -> head_in , HEAD_SIZE );
214
- if (sdsioRead (play -> sdsio , PlayBuf + HEAD_SIZE , play -> head_in .data_size ) == play -> head_in .data_size ) {
215
- if (sdsWrite (play -> stream , PlayBuf , size ) == size ) {
216
- play -> head_in .data_size = 0U ;
217
- play -> cnt_in ++ ;
218
- }
182
+ if (cnt > sizeof (PlayBuf )) {
183
+ cnt = sizeof (PlayBuf );
184
+ }
185
+ num = sdsioRead (play -> sdsio , PlayBuf , cnt );
186
+ if (num != 0U ) {
187
+ sdsWrite (play -> stream , PlayBuf , num );
188
+ }
189
+ if (num != cnt ) {
190
+ if (sdsioEndOfStream (play -> sdsio ) != 0 ) {
191
+ play -> flags |= SDS_PLAY_FLAG_EOS ;
219
192
} else {
220
193
if (sdsPlayEvent != NULL ) {
221
194
sdsPlayEvent (play , SDS_PLAY_EVENT_IO_ERROR );
222
195
}
223
- break ;
224
196
}
197
+ break ;
225
198
}
226
- } while ( play -> head_in . data_size == 0U );
199
+ }
227
200
}
228
201
}
229
202
}
@@ -270,7 +243,7 @@ sdsPlayId_t sdsPlayOpen (const char *name, void *buf, uint32_t buf_size, uint32_
270
243
uint32_t index ;
271
244
272
245
if ((name != NULL ) && (buf != NULL ) && (buf_size != 0U ) &&
273
- (buf_size <= SDS_PLAY_MAX_RECORD_SIZE ) && (io_threshold <= buf_size )) {
246
+ (buf_size <= SDS_PLAY_BUF_SIZE ) && (io_threshold <= buf_size )) {
274
247
275
248
play = sdsPlayAlloc (& index );
276
249
if (play != NULL ) {
@@ -279,12 +252,9 @@ sdsPlayId_t sdsPlayOpen (const char *name, void *buf, uint32_t buf_size, uint32_
279
252
play -> event_close = 0U ;
280
253
play -> flags = 0U ;
281
254
play -> buf_size = buf_size ;
282
- play -> head_in .timestamp = 0U ;
283
- play -> head_in .data_size = 0U ;
284
255
play -> head_out .timestamp = 0U ;
285
256
play -> head_out .data_size = 0U ;
286
- play -> cnt_in = 0U ;
287
- play -> cnt_out = 0U ;
257
+ play -> record_data_cnt = 0U ;
288
258
play -> stream = sdsOpen (buf , buf_size , io_threshold , 0U );
289
259
play -> sdsio = sdsioOpen (name , sdsioModeRead );
290
260
@@ -335,28 +305,25 @@ int32_t sdsPlayClose (sdsPlayId_t id) {
335
305
uint32_t sdsPlayRead (sdsPlayId_t id , uint32_t * timestamp , void * buf , uint32_t buf_size ) {
336
306
sdsPlay_t * play = id ;
337
307
uint32_t num = 0U ;
308
+ uint32_t size ;
338
309
339
310
if ((play != NULL ) && (buf != NULL ) && (buf_size != 0U )) {
340
- if (play -> cnt_out < play -> cnt_in ) {
341
- if (play -> head_out .data_size == 0U ) {
342
- if (sdsRead (play -> stream , & play -> head_out , HEAD_SIZE ) != HEAD_SIZE ) {
343
- play -> head_out .data_size = 0U ;
311
+ size = sdsGetCount (play -> stream );
312
+ if ((play -> head_out .data_size == 0U ) && (size >= HEAD_SIZE )) {
313
+ sdsRead (play -> stream , & play -> head_out , HEAD_SIZE );
314
+ size -= HEAD_SIZE ;
315
+ }
316
+
317
+ if ((play -> head_out .data_size != 0U ) && (play -> head_out .data_size <= size )) {
318
+ if (play -> head_out .data_size <= buf_size ) {
319
+ num = sdsRead (play -> stream , buf , play -> head_out .data_size );
320
+ if (timestamp != NULL ) {
321
+ * timestamp = play -> head_out .timestamp ;
344
322
}
345
- }
346
- if (play -> head_out .data_size != 0U ) {
347
- if (play -> head_out .data_size <= buf_size ) {
348
- if (sdsRead (play -> stream , buf , play -> head_out .data_size ) == play -> head_out .data_size ) {
349
- if (timestamp != NULL ) {
350
- * timestamp = play -> head_out .timestamp ;
351
- }
352
- num = play -> head_out .data_size ;
353
- play -> head_out .data_size = 0U ;
354
- play -> cnt_out ++ ;
355
- if (play -> event_threshold != 0U ) {
356
- play -> event_threshold = 0U ;
357
- osThreadFlagsSet (sdsPlayThreadId , 1U << play -> index );
358
- }
359
- }
323
+ play -> head_out .data_size = 0U ;
324
+ if (play -> event_threshold != 0U ) {
325
+ play -> event_threshold = 0U ;
326
+ osThreadFlagsSet (sdsPlayThreadId , 1U << play -> index );
360
327
}
361
328
}
362
329
}
@@ -367,20 +334,19 @@ uint32_t sdsPlayRead (sdsPlayId_t id, uint32_t *timestamp, void *buf, uint32_t b
367
334
// Get record data size from Player stream
368
335
uint32_t sdsPlayGetSize (sdsPlayId_t id ) {
369
336
sdsPlay_t * play = id ;
370
- uint32_t num = 0U ;
337
+ uint32_t size = 0U ;
338
+ uint32_t cnt ;
371
339
372
340
if (play != NULL ) {
373
- if (play -> cnt_out < play -> cnt_in ) {
374
- if (play -> head_out .data_size == 0U ) {
375
- if (sdsRead (play -> stream , & play -> head_out , HEAD_SIZE ) == HEAD_SIZE ) {
376
- num = play -> head_out .data_size ;
377
- } else {
378
- play -> head_out .data_size = 0U ;
379
- }
341
+ if (play -> head_out .data_size == 0U ) {
342
+ cnt = sdsGetCount (play -> stream );
343
+ if (cnt >= HEAD_SIZE ) {
344
+ sdsRead (play -> stream , & play -> head_out , HEAD_SIZE );
380
345
}
381
346
}
347
+ size = play -> head_out .data_size ;
382
348
}
383
- return num ;
349
+ return size ;
384
350
}
385
351
386
352
// Check if end of stream has been reached
@@ -389,7 +355,7 @@ int32_t sdsPlayEndOfStream (sdsPlayId_t id) {
389
355
int32_t eos = 0 ;
390
356
391
357
if (play != NULL ) {
392
- if ((play -> cnt_out == play -> cnt_in ) && ((play -> flags & SDS_PLAY_FLAG_EOS ) != 0U )) {
358
+ if ((sdsGetCount ( play -> stream ) == 0U ) && ((play -> flags & SDS_PLAY_FLAG_EOS ) != 0U )) {
393
359
eos = 1 ;
394
360
}
395
361
}
0 commit comments