Skip to content

Commit 6a259cb

Browse files
committed
CopyAll correction
1 parent c91cf6a commit 6a259cb

File tree

3 files changed

+27
-9
lines changed

3 files changed

+27
-9
lines changed

examples/examples-webserver/player-sd-webserverex_mp3/player-sd-webserverex_mp3.ino

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,18 +12,28 @@
1212
#include "AudioLibs/AudioServerEx.h"
1313
#include "AudioCodecs/CodecCopy.h"
1414

15+
#define PIN_AUDIO_KIT_SD_CARD_CS 13
16+
#define PIN_AUDIO_KIT_SD_CARD_MISO 2
17+
#define PIN_AUDIO_KIT_SD_CARD_MOSI 15
18+
#define PIN_AUDIO_KIT_SD_CARD_CLK 14
19+
1520
const char *ssid = "SSID";
1621
const char *password = "PWD";
1722
const char *startFilePath="/";
1823
const char* ext="mp3";
19-
AudioSourceSD source(startFilePath, ext);
24+
25+
AudioSourceSD source(startFilePath, ext, PIN_AUDIO_KIT_SD_CARD_CS);
2026
AudioServerEx out;
2127
AudioPlayer player(source, out, *new CopyDecoder());
2228

2329
void setup() {
2430
Serial.begin(115200);
2531
AudioLogger::instance().begin(Serial, AudioLogger::Warning);
2632

33+
// setup SPI for SD card
34+
SPI.begin(PIN_AUDIO_KIT_SD_CARD_CLK, PIN_AUDIO_KIT_SD_CARD_MISO, PIN_AUDIO_KIT_SD_CARD_MOSI, PIN_AUDIO_KIT_SD_CARD_CS);
35+
36+
2737
// setup output - We need to login and serve the data as audio/mp3
2838
auto cfg = out.defaultConfig();
2939
cfg.password = password;

src/AudioHttp/URLStream.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -105,12 +105,12 @@ class URLStreamDefault : public AbstractURLStream {
105105
}
106106

107107
virtual int available() override {
108-
if (!active) return 0;
108+
if (!active || !request) return 0;
109109
return request.available();
110110
}
111111

112112
virtual size_t readBytes(uint8_t *buffer, size_t length) override {
113-
if (!active) return -1;
113+
if (!active || !request) return 0;
114114

115115
size_t read = request.read((uint8_t*)buffer, length);
116116
total_read+=read;

src/AudioTools/AudioCopy.h

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,10 @@ class StreamCopyT {
109109
}
110110

111111
// get the data now
112-
bytes_read = from->readBytes((uint8_t*)buffer, bytes_to_read);
112+
bytes_read = 0;
113+
if (bytes_to_read>0){
114+
bytes_read = from->readBytes((uint8_t*)buffer, bytes_to_read);
115+
}
113116

114117
// determine mime
115118
notifyMime(buffer, bytes_to_read);
@@ -120,9 +123,11 @@ class StreamCopyT {
120123

121124
// callback with unconverted data
122125
if (onWrite!=nullptr) onWrite(onWriteObj, buffer, result);
126+
123127
#ifndef COPY_LOG_OFF
124128
LOGI("StreamCopy::copy %u -> %u -> %u bytes - in %u hops", (unsigned int)bytes_to_read,(unsigned int) bytes_read, (unsigned int)result, (unsigned int)delayCount);
125129
#endif
130+
126131
CHECK_MEMORY();
127132
} else {
128133
// give the processor some time
@@ -252,23 +257,26 @@ class StreamCopyT {
252257

253258
// blocking write - until everything is processed
254259
size_t write(size_t len, size_t &delayCount ){
255-
if (buffer==nullptr) return 0;
260+
if (buffer==nullptr || len==0) return 0;
256261
size_t total = 0;
262+
long open = len;
257263
int retry = 0;
258-
while(total<len){
259-
size_t written = to->write((const uint8_t*)buffer+total, len-total);
264+
while(open>0){
265+
size_t written = to->write((const uint8_t*)buffer+total, open);
260266
total += written;
267+
open -= written;
261268
delayCount++;
262269

263270
if (retry++ > retryLimit){
264-
LOGE("write to target has failed!");
271+
LOGE("write to target has failed! (%ld bytes)", open);
265272
break;
266273
}
267274

268275
if (retry>1) {
269276
delay(5);
270-
LOGI("try write - %d ",retry);
277+
LOGI("try write - %d (open %ld bytes) ",retry, open);
271278
}
279+
272280
CHECK_MEMORY();
273281
}
274282
return total;

0 commit comments

Comments
 (0)