int dabInputDabplusFifoRead(void* args, void* buffer, int size) { dabInputDabplusFifoData* data = (dabInputDabplusFifoData*)args; if (data->bufferSize != size * 5) { if (data->buffer != NULL) { delete[] data->buffer; } data->buffer = new uint8_t[size * 5]; data->bufferSize = size * 5; data->bufferIndex = 0; } if (data->bufferOffset < data->bufferSize) { int ret = dabInputFifoRead(data->fifoData, &data->buffer[data->bufferOffset], data->bufferSize - data->bufferOffset); if (ret < 0) { return ret; } data->bufferOffset += ret; if (data->bufferOffset != data->bufferSize) { etiLog.log(alert, "ERROR: Incomplete DAB+ frame!\n"); return 0; } } memcpy(buffer, &data->buffer[data->bufferIndex], size); data->bufferIndex += size; if (data->bufferIndex >= data->bufferSize) { data->bufferIndex = 0; data->bufferOffset = 0; } return size; }
int dabInputFifoRead(void* args, void* buffer, int size) { //fprintf(stderr, "INFO: read %i bytes\n", size); dabInputFifoData* data = (dabInputFifoData*)args; dabInputFifoStats* stats = &data->stats; int head; int tail; int curSize; int maxSize; #ifdef _WIN32 WaitForSingleObject(data->semInfo, INFINITE); #else sem_wait(&data->semInfo); #endif head = data->head; tail = data->tail; curSize = data->curSize; maxSize = data->maxSize; #ifdef _WIN32 ReleaseSemaphore(data->semInfo, 1, NULL); #else sem_post(&data->semInfo); #endif //fprintf(stderr, "head: %i, tail: %i, curSize: %i\n", head, tail, curSize); if (size > curSize) { if (curSize == 0) { stats->empty = true; } else { etiLog.print(TcpLog::WARNING, "Not enough data in FIFO buffer: (%i) %i/%i\n", data->stats.id, curSize, size); } return 0; } if (head > tail) { memcpy(buffer, data->buffer + tail, size); #ifdef _WIN32 WaitForSingleObject(data->semInfo, INFINITE); #else sem_wait(&data->semInfo); #endif data->tail += size; data->curSize -= size; #ifdef _WIN32 ReleaseSemaphore(data->semInfo, 1, NULL); #else sem_post(&data->semInfo); #endif return size; } else { if (maxSize - tail >= size) { memcpy(buffer, data->buffer + tail, size); #ifdef _WIN32 WaitForSingleObject(data->semInfo, INFINITE); #else sem_wait(&data->semInfo); #endif data->tail += size; data->curSize -= size; #ifdef _WIN32 ReleaseSemaphore(data->semInfo, 1, NULL); #else sem_post(&data->semInfo); #endif return size; } else { memcpy(buffer, data->buffer + tail, maxSize - tail); #ifdef _WIN32 WaitForSingleObject(data->semInfo, INFINITE); #else sem_wait(&data->semInfo); #endif data->tail = 0; data->curSize -= maxSize - tail; #ifdef _WIN32 ReleaseSemaphore(data->semInfo, 1, NULL); #else sem_post(&data->semInfo); #endif return maxSize - tail + dabInputFifoRead(data, (char*)buffer + maxSize - tail, size - (maxSize - tail)); } } return -1; }