[vlc-devel] [PATCH 2/6] demux: adaptive: make Downloader interruptable
Zhao Zhili
quinkblack at foxmail.com
Wed Sep 20 05:49:12 CEST 2017
---
modules/demux/adaptive/http/Downloader.cpp | 41
++++++++++++++++++++--
modules/demux/adaptive/http/Downloader.hpp | 6 ++++
.../demux/adaptive/http/HTTPConnectionManager.cpp | 18 ++++++++++
.../demux/adaptive/http/HTTPConnectionManager.h | 6 ++++
4 files changed, 68 insertions(+), 3 deletions(-)
diff --git a/modules/demux/adaptive/http/Downloader.cpp
b/modules/demux/adaptive/http/Downloader.cpp
index b2e64ef..583d54b 100644
--- a/modules/demux/adaptive/http/Downloader.cpp
+++ b/modules/demux/adaptive/http/Downloader.cpp
@@ -31,7 +31,8 @@ using namespace adaptive::http;
Downloader::Downloader() :
thread_handle_valid(false),
killed(false),
- pending_requests(0)
+ pending_requests(0),
+ interrupt_ctx(NULL)
{
vlc_mutex_init(&lock);
vlc_cond_init(&waitcond);
@@ -39,10 +40,18 @@ Downloader::Downloader() :
bool Downloader::start()
{
- if(!thread_handle_valid &&
- vlc_clone(&thread_handle, downloaderThread,
+ if(thread_handle_valid)
+ return true;
+
+ interrupt_ctx = vlc_interrupt_create();
+ if(unlikely(interrupt_ctx == NULL))
+ return false;
+
+ if(vlc_clone(&thread_handle, downloaderThread,
static_cast<void *>(this), VLC_THREAD_PRIORITY_INPUT))
{
+ vlc_interrupt_destroy(interrupt_ctx);
+ interrupt_ctx = NULL;
return false;
}
thread_handle_valid = true;
@@ -51,6 +60,8 @@ bool Downloader::start()
Downloader::~Downloader()
{
+ if(interrupt_ctx != NULL)
+ vlc_interrupt_kill(interrupt_ctx);
killed.store(true);
vlc_mutex_lock( &lock );
vlc_cond_signal(&waitcond);
@@ -60,6 +71,11 @@ Downloader::~Downloader()
vlc_join(thread_handle, NULL);
vlc_mutex_destroy(&lock);
vlc_cond_destroy(&waitcond);
+ if(interrupt_ctx)
+ {
+ vlc_interrupt_destroy(interrupt_ctx);
+ interrupt_ctx = NULL;
+ }
}
void Downloader::schedule(HTTPChunkBufferedSource *source)
{
@@ -83,9 +99,28 @@ void Downloader::cancel(HTTPChunkBufferedSource *source)
vlc_mutex_unlock(&lock);
}
+void Downloader::interrupt()
+{
+ if(interrupt_ctx)
+ vlc_interrupt_kill(interrupt_ctx);
+}
+
+void Downloader::interrupt_forward_start(void *data[2])
+{
+ if(interrupt_ctx)
+ vlc_interrupt_forward_start(interrupt_ctx, data);
+}
+
+void Downloader::interrupt_forward_stop(void *data[2])
+{
+ if(interrupt_ctx)
+ vlc_interrupt_forward_stop(data);
+}
+
void * Downloader::downloaderThread(void *opaque)
{
Downloader *instance = static_cast<Downloader *>(opaque);
+ vlc_interrupt_set(instance->interrupt_ctx);
int canc = vlc_savecancel();
instance->Run();
vlc_restorecancel( canc );
diff --git a/modules/demux/adaptive/http/Downloader.hpp
b/modules/demux/adaptive/http/Downloader.hpp
index ec76b3f..1170230 100644
--- a/modules/demux/adaptive/http/Downloader.hpp
+++ b/modules/demux/adaptive/http/Downloader.hpp
@@ -23,6 +23,8 @@
#include "Chunk.h"
#include <vlc_common.h>
+#include <vlc_interrupt.h>
+
#include <atomic>
#include <list>
@@ -40,6 +42,9 @@ namespace adaptive
bool start();
void schedule(HTTPChunkBufferedSource *);
void cancel(HTTPChunkBufferedSource *);
+ void interrupt();
+ void interrupt_forward_start(void *data[2]);
+ void interrupt_forward_stop(void *data[2]);
private:
static void * downloaderThread(void *);
@@ -51,6 +56,7 @@ namespace adaptive
bool thread_handle_valid;
std::atomic_bool killed;
std::atomic_int pending_requests;
+ vlc_interrupt_t *interrupt_ctx;
std::list<HTTPChunkBufferedSource *> chunks;
};
diff --git a/modules/demux/adaptive/http/HTTPConnectionManager.cpp
b/modules/demux/adaptive/http/HTTPConnectionManager.cpp
index c6d3e45..fa45163 100644
--- a/modules/demux/adaptive/http/HTTPConnectionManager.cpp
+++ b/modules/demux/adaptive/http/HTTPConnectionManager.cpp
@@ -151,3 +151,21 @@ void
HTTPConnectionManager::cancel(AbstractChunkSource *source)
if(src)
downloader->cancel(src);
}
+
+void HTTPConnectionManager::interrupt()
+{
+ if(downloader)
+ downloader->interrupt();
+}
+
+void HTTPConnectionManager::interrupt_forward_start(void *data[2])
+{
+ if(downloader)
+ downloader->interrupt_forward_start(data);
+}
+
+void HTTPConnectionManager::interrupt_forward_stop(void *data[2])
+{
+ if(downloader)
+ downloader->interrupt_forward_stop(data);
+}
diff --git a/modules/demux/adaptive/http/HTTPConnectionManager.h
b/modules/demux/adaptive/http/HTTPConnectionManager.h
index 64ebf29..68d4769 100644
--- a/modules/demux/adaptive/http/HTTPConnectionManager.h
+++ b/modules/demux/adaptive/http/HTTPConnectionManager.h
@@ -50,6 +50,9 @@ namespace adaptive
virtual AbstractConnection *
getConnection(ConnectionParams &) = 0;
virtual void start(AbstractChunkSource *) = 0;
virtual void cancel(AbstractChunkSource *) = 0;
+ virtual void interrupt() = 0;
+ virtual void interrupt_forward_start(void *data[2]) = 0;
+ virtual void interrupt_forward_stop(void *data[2]) = 0;
virtual void updateDownloadRate(const ID &, size_t,
mtime_t); /* impl */
void setDownloadRateObserver(IDownloadRateObserver *);
@@ -72,6 +75,9 @@ namespace adaptive
virtual void start(AbstractChunkSource *) /* impl */;
virtual void cancel(AbstractChunkSource *) /* impl */;
+ virtual void interrupt() /* impl */;
+ virtual void interrupt_forward_start(void *data[2]);
+ virtual void interrupt_forward_stop(void *data[2]);
private:
void releaseAllConnections ();
--
2.7.4
-------------- next part --------------
A non-text attachment was scrubbed...
Name: 0002-demux-adaptive-make-Downloader-interruptable.patch
Type: text/x-patch
Size: 6192 bytes
Desc: not available
URL: <http://mailman.videolan.org/pipermail/vlc-devel/attachments/20170920/4dbbaa78/attachment.bin>
More information about the vlc-devel
mailing list