diff options
author | Matthias P. Braendli <matthias.braendli@mpb.li> | 2015-03-13 09:50:04 +0100 |
---|---|---|
committer | Matthias P. Braendli <matthias.braendli@mpb.li> | 2015-03-13 09:50:04 +0100 |
commit | ce10052a134f8b6c84112f5785a8b48cdc9bba22 (patch) | |
tree | 9509d2b169942a7c85eac93531fd2aa430ccecae /vlc_input.c | |
parent | 845746006e79d554478090126dbdab799299f9b3 (diff) | |
download | toolame-dab-ce10052a134f8b6c84112f5785a8b48cdc9bba22.tar.gz toolame-dab-ce10052a134f8b6c84112f5785a8b48cdc9bba22.tar.bz2 toolame-dab-ce10052a134f8b6c84112f5785a8b48cdc9bba22.zip |
Add libvlc input
Diffstat (limited to 'vlc_input.c')
-rw-r--r-- | vlc_input.c | 201 |
1 files changed, 201 insertions, 0 deletions
diff --git a/vlc_input.c b/vlc_input.c new file mode 100644 index 0000000..fab8273 --- /dev/null +++ b/vlc_input.c @@ -0,0 +1,201 @@ +#include <stdlib.h> +#include <pthread.h> +#include <assert.h> +#include <unistd.h> +#include <string.h> +#include "vlc_input.h" + + +libvlc_instance_t *m_vlc; +libvlc_media_player_t *m_mp; + +unsigned int vlc_rate; + +struct vlc_buffer *head_buffer; + +pthread_mutex_t buffer_lock = PTHREAD_MUTEX_INITIALIZER; + +struct vlc_buffer* vlc_buffer_new() +{ + struct vlc_buffer* node; + node = malloc(sizeof(struct vlc_buffer)); + memset(node, 0, sizeof(struct vlc_buffer)); + return node; +} + +void vlc_buffer_free(struct vlc_buffer* node) +{ + if (node->buf) { + free(node->buf); + } + + free(node); +} + +size_t vlc_buffer_totalsize(struct vlc_buffer* node) +{ + size_t totalsize = 0; + for (; node != NULL; node = node->next) { + totalsize += node->size; + } + + return totalsize; +} + +// VLC Audio prerender callback, we must allocate a buffer here +void prepareRender( + void* p_audio_data, + uint8_t** pp_pcm_buffer, + size_t size) +{ + *pp_pcm_buffer = malloc(size); + return; +} + + + +// Audio postrender callback +void handleStream( + void* p_audio_data, + uint8_t* p_pcm_buffer, + unsigned int channels, + unsigned int rate, + unsigned int nb_samples, + unsigned int bits_per_sample, + size_t size, + int64_t pts) +{ + assert(channels == 2); + assert(rate == vlc_rate); + assert(bits_per_sample == 16); + + const size_t max_length = 4 * size; + + for (;;) { + pthread_mutex_lock(&buffer_lock); + + if (vlc_buffer_totalsize(head_buffer) < max_length) { + struct vlc_buffer* newbuf = vlc_buffer_new(); + + newbuf->buf = p_pcm_buffer; + newbuf->size = size; + + // Append the new buffer to the end of the linked list + struct vlc_buffer* tail = head_buffer; + while (tail->next) { + tail = tail->next; + } + tail->next = newbuf; + + pthread_mutex_unlock(&buffer_lock); + return; + } + + pthread_mutex_unlock(&buffer_lock); + usleep(100); + } +} + +int vlc_in_prepare(unsigned verbosity, unsigned int rate, const char* uri) +{ + int err; + fprintf(stderr, "Initialising VLC...\n"); + + vlc_rate = rate; + + // VLC options + char smem_options[512]; + snprintf(smem_options, sizeof(smem_options), + "#transcode{acodec=s16l,samplerate=%d}:" + // We are using transcode because smem only support raw audio and + // video formats + "smem{" + "audio-postrender-callback=%lld," + "audio-prerender-callback=%lld" + "}", + vlc_rate, + (long long int)(intptr_t)(void*)&handleStream, + (long long int)(intptr_t)(void*)&prepareRender); + + char verb_options[512]; + snprintf(verb_options, sizeof(verb_options), + "--verbose=%d", verbosity); + + const char * const vlc_args[] = { + verb_options, + "--sout", smem_options // Stream to memory + }; + + // Launch VLC + m_vlc = libvlc_new(sizeof(vlc_args) / sizeof(vlc_args[0]), vlc_args); + + // Load the media + libvlc_media_t *m; + m = libvlc_media_new_location(m_vlc, uri); + m_mp = libvlc_media_player_new_from_media(m); + libvlc_media_release(m); + + // Allocate the list + head_buffer = vlc_buffer_new(); + + // Start playing + libvlc_media_player_play(m_mp); + + fprintf(stderr, "VLC launched.\n"); + return 0; +} + +size_t vlc_in_read(void *buf, size_t len) +{ + size_t requested = len; + for (;;) { + pthread_mutex_lock(&buffer_lock); + + if (vlc_buffer_totalsize(head_buffer) >= len) { + while (len >= head_buffer->size) { + if (head_buffer->buf) { + // Get all the data from this list element + memcpy(buf, head_buffer->buf, head_buffer->size); + + buf += head_buffer->size; + len -= head_buffer->size; + } + + if (head_buffer->next) { + struct vlc_buffer *next_head = head_buffer->next; + vlc_buffer_free(head_buffer); + head_buffer = next_head; + } + else { + vlc_buffer_free(head_buffer); + head_buffer = vlc_buffer_new(); + break; + } + } + + if (len > 0) { + assert(len < head_buffer->size); + + memcpy(buf, head_buffer->buf, len); + + // split the current head into two parts + size_t remaining = head_buffer->size - len; + uint8_t *newbuf = malloc(remaining); + + memcpy(newbuf, head_buffer->buf + len, remaining); + free(head_buffer->buf); + head_buffer->buf = newbuf; + head_buffer->size = remaining; + } + + pthread_mutex_unlock(&buffer_lock); + return requested; + } + + pthread_mutex_unlock(&buffer_lock); + usleep(100); + } + + abort(); +} + |