Fixed TranscodingDataStream to continue transcoding after an explicit

Close() for about 100k worth of data. This should generally be enough to
finish off any rounding errors caused by calculating Content-Length.
This commit is contained in:
casey langen 2017-06-13 09:34:25 -07:00
parent 6ce475b9ac
commit 8a04959d6b
4 changed files with 43 additions and 13 deletions

View File

@ -18,15 +18,13 @@
android:id="@+id/action_genres"
app:showAsAction="never"
android:title="@string/menu_genres"/>
<item
android:id="@+id/action_settings"
app:showAsAction="never"
android:title="@string/menu_settings"/>
<item
android:id="@+id/action_offline_tracks"
app:showAsAction="never"
android:title="@string/menu_offline_tracks"/>
<item
android:id="@+id/action_settings"
app:showAsAction="never"
android:title="@string/menu_settings"/>
</menu>

View File

@ -136,7 +136,7 @@ static void fileFreeCallback(void *cls) {
std::cerr << "******** REQUEST CLOSE: " << range->file << " ********\n\n";
#endif
range->file->Destroy();
range->file->Close(); /* lazy destroy */
range->file = nullptr;
}
delete range;

View File

@ -39,6 +39,7 @@
#define BUFFER_SIZE 8192
#define SAMPLES_PER_BUFFER BUFFER_SIZE / 4 /* sizeof(float) */
#define DETACH_TOLERANCE_BYTES 131072 /* 2^17, ~131k */
using PositionType = TranscodingDataStream::PositionType;
@ -65,7 +66,7 @@ TranscodingDataStream::TranscodingDataStream(
/* note that we purposely under-estimate the content length by a couple
seconds. we do this because http clients seem to be more likely to be
throw a fit if we over estimate, instead of under-estimate. */
this->length = (PositionType)((this->decoder->GetDuration() - 1.0) * 1000.0 * (float)bitrate / 8.0);
this->length = (PositionType)((this->decoder->GetDuration() - 0.2) * 1000.0 * (float)bitrate / 8.0);
}
}
}
@ -91,7 +92,6 @@ TranscodingDataStream::TranscodingDataStream(
}
TranscodingDataStream::~TranscodingDataStream() {
this->Close();
}
bool TranscodingDataStream::Open(const char *uri, unsigned int options) {
@ -99,17 +99,47 @@ bool TranscodingDataStream::Open(const char *uri, unsigned int options) {
}
bool TranscodingDataStream::Close() {
if (this->eof) {
this->Dispose();
}
else {
std::thread([this]() { /* detach and finish. hopefully. */
char buffer[8192];
int count = 0;
int last = 0;
while (!Eof() && count < DETACH_TOLERANCE_BYTES) {
last = Read(buffer, sizeof(buffer));
count += last;
std::this_thread::yield();
}
if (last != 0 && this->outFile) {
/* incomplete, delete... */
fclose(this->outFile);
this->outFile = nullptr;
boost::system::error_code ec;
boost::filesystem::remove(this->tempFilename, ec);
}
Dispose();
}).detach();
}
return true;
}
void TranscodingDataStream::Dispose() {
if (this->pcmBuffer) {
this->pcmBuffer->Destroy();
this->pcmBuffer = nullptr;
}
if (this->decoder != nullptr) {
if (this->decoder) {
this->decoder->Destroy();
this->decoder = nullptr;
}
if (this->input != nullptr) {
if (this->input) {
this->input->Destroy();
this->input = nullptr;
}
@ -126,7 +156,7 @@ bool TranscodingDataStream::Close() {
boost::filesystem::remove(this->tempFilename, ec);
}
return true;
delete this;
}
void TranscodingDataStream::Interrupt() {
@ -134,7 +164,7 @@ void TranscodingDataStream::Interrupt() {
}
void TranscodingDataStream::Destroy() {
delete this;
this->Dispose();
}
PositionType TranscodingDataStream::Read(void *buffer, PositionType bytesToRead) {

View File

@ -78,6 +78,8 @@ class TranscodingDataStream : public musik::core::sdk::IDataStream {
musik::core::sdk::IDecoder* decoder;
musik::core::sdk::IBuffer* pcmBuffer;
void Dispose();
template <typename T>
struct ByteBuffer {
ByteBuffer() {