http_streamer: Enforce safeness in constructor/thread

* Wait for the thread to exit.
* Update size from the data thread.
This commit is contained in:
Dario Casalinuovo 2016-07-10 19:05:52 +02:00
parent 0162e27293
commit be2c072556
2 changed files with 30 additions and 12 deletions

View File

@ -16,7 +16,7 @@
class FileListener : public BUrlProtocolAsynchronousListener {
public:
FileListener(BAdapterIO* owner)
FileListener(HTTPMediaIO* owner)
:
BUrlProtocolAsynchronousListener(true),
fRequest(NULL),
@ -39,6 +39,8 @@ public:
if (fRequest != NULL)
fRequest->Stop();
fAdapterIO->UpdateSize();
fRequest = request;
}
@ -80,7 +82,7 @@ private:
}
BUrlRequest* fRequest;
BAdapterIO* fAdapterIO;
HTTPMediaIO* fAdapterIO;
BInputAdapter* fInputAdapter;
sem_id fInitSem;
};
@ -91,6 +93,7 @@ HTTPMediaIO::HTTPMediaIO(BUrl url)
BAdapterIO(B_MEDIA_STREAMING | B_MEDIA_SEEKABLE, HTTP_TIMEOUT),
fContext(NULL),
fReq(NULL),
fReqThread(-1),
fListener(NULL),
fUrl(url),
fIsMutable(false)
@ -140,22 +143,14 @@ HTTPMediaIO::Open()
if (fReq == NULL)
return B_ERROR;
if (fReq->Run() < 0)
fReqThread = fReq->Run();
if (fReqThread < 0)
return B_ERROR;
status_t ret = fListener->LockOnInit(HTTP_TIMEOUT);
if (ret != B_OK)
return ret;
off_t totalSize = fReq->Result().Length();
// At this point we decide if our size is fixed or mutable,
// this will change the behavior of our parent.
if (totalSize > 0)
BAdapterIO::SetSize(totalSize);
else
fIsMutable = true;
return BAdapterIO::Open();
}
@ -163,6 +158,10 @@ HTTPMediaIO::Open()
void
HTTPMediaIO::Close()
{
fReq->Stop();
status_t status;
wait_for_thread(fReqThread, &status);
delete fReq;
delete fListener;
@ -185,3 +184,16 @@ HTTPMediaIO::SeekRequested(off_t position)
{
return BAdapterIO::SeekRequested(position);
}
void
HTTPMediaIO::UpdateSize()
{
// At this point we decide if our size is fixed or mutable,
// this will change the behavior of our parent.
off_t size = fReq->Result().Length();
if (size > 0)
BAdapterIO::SetSize(size);
else
fIsMutable = true;
}

View File

@ -35,10 +35,16 @@ public:
protected:
virtual status_t SeekRequested(off_t position);
// Other custom stuff
void UpdateSize();
friend class FileListener;
private:
BUrlContext* fContext;
BUrlRequest* fReq;
FileListener* fListener;
thread_id fReqThread;
BUrl fUrl;
off_t fTotalSize;