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:12:06 +02:00
parent 0162e27293
commit be2c072556
2 changed files with 30 additions and 12 deletions
@@ -16,7 +16,7 @@
class FileListener : public BUrlProtocolAsynchronousListener { class FileListener : public BUrlProtocolAsynchronousListener {
public: public:
FileListener(BAdapterIO* owner) FileListener(HTTPMediaIO* owner)
: :
BUrlProtocolAsynchronousListener(true), BUrlProtocolAsynchronousListener(true),
fRequest(NULL), fRequest(NULL),
@@ -39,6 +39,8 @@ public:
if (fRequest != NULL) if (fRequest != NULL)
fRequest->Stop(); fRequest->Stop();
fAdapterIO->UpdateSize();
fRequest = request; fRequest = request;
} }
@@ -80,7 +82,7 @@ private:
} }
BUrlRequest* fRequest; BUrlRequest* fRequest;
BAdapterIO* fAdapterIO; HTTPMediaIO* fAdapterIO;
BInputAdapter* fInputAdapter; BInputAdapter* fInputAdapter;
sem_id fInitSem; sem_id fInitSem;
}; };
@@ -91,6 +93,7 @@ HTTPMediaIO::HTTPMediaIO(BUrl url)
BAdapterIO(B_MEDIA_STREAMING | B_MEDIA_SEEKABLE, HTTP_TIMEOUT), BAdapterIO(B_MEDIA_STREAMING | B_MEDIA_SEEKABLE, HTTP_TIMEOUT),
fContext(NULL), fContext(NULL),
fReq(NULL), fReq(NULL),
fReqThread(-1),
fListener(NULL), fListener(NULL),
fUrl(url), fUrl(url),
fIsMutable(false) fIsMutable(false)
@@ -140,22 +143,14 @@ HTTPMediaIO::Open()
if (fReq == NULL) if (fReq == NULL)
return B_ERROR; return B_ERROR;
if (fReq->Run() < 0) fReqThread = fReq->Run();
if (fReqThread < 0)
return B_ERROR; return B_ERROR;
status_t ret = fListener->LockOnInit(HTTP_TIMEOUT); status_t ret = fListener->LockOnInit(HTTP_TIMEOUT);
if (ret != B_OK) if (ret != B_OK)
return ret; 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(); return BAdapterIO::Open();
} }
@@ -163,6 +158,10 @@ HTTPMediaIO::Open()
void void
HTTPMediaIO::Close() HTTPMediaIO::Close()
{ {
fReq->Stop();
status_t status;
wait_for_thread(fReqThread, &status);
delete fReq; delete fReq;
delete fListener; delete fListener;
@@ -185,3 +184,16 @@ HTTPMediaIO::SeekRequested(off_t position)
{ {
return BAdapterIO::SeekRequested(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;
}
@@ -35,10 +35,16 @@ public:
protected: protected:
virtual status_t SeekRequested(off_t position); virtual status_t SeekRequested(off_t position);
// Other custom stuff
void UpdateSize();
friend class FileListener;
private: private:
BUrlContext* fContext; BUrlContext* fContext;
BUrlRequest* fReq; BUrlRequest* fReq;
FileListener* fListener; FileListener* fListener;
thread_id fReqThread;
BUrl fUrl; BUrl fUrl;
off_t fTotalSize; off_t fTotalSize;