summaryrefslogtreecommitdiffstats
path: root/src/VLCInput.cpp
blob: 6b7a4bb0f2dbfdf862fe01bf7e8845d6f3d82179 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
/* ------------------------------------------------------------------
 * Copyright (C) 2015 Matthias P. Braendli
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
 * express or implied.
 * See the License for the specific language governing permissions
 * and limitations under the License.
 * -------------------------------------------------------------------
 */

#include <cstdio>
#include <string>

#include "VLCInput.h"

#include "config.h"

#if HAVE_VLC

#include <sys/time.h>
#include <boost/date_time/posix_time/posix_time.hpp>


using namespace std;

// VLC Audio prerender callback
void prepareRender(
        void* p_audio_data,
        uint8_t** pp_pcm_buffer,
        size_t size)
{
    VLCInput* in = (VLCInput*)p_audio_data;

    in->preRender(pp_pcm_buffer, size);
}


// 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)
{
    VLCInput* in = (VLCInput*)p_audio_data;

    assert(channels == 2);
    assert(rate == in->getRate());
    assert(bits_per_sample == 8*BYTES_PER_SAMPLE);

    in->postRender(p_pcm_buffer, size);
}

int VLCInput::prepare()
{
    int err;
    fprintf(stderr, "Initialising VLC...\n");

    // 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,"
                "audio-data=%lld"
            "}",
            m_rate,
            (long long int)(intptr_t)(void*)&handleStream,
            (long long int)(intptr_t)(void*)&prepareRender,
            (long long int)(intptr_t)this);

    char verb_options[512];
    snprintf(verb_options, sizeof(verb_options),
            "--verbose=%d", m_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, m_uri.c_str());
    m_mp = libvlc_media_player_new_from_media(m);
    libvlc_media_release(m);

    // Start playing
    libvlc_media_player_play(m_mp);

    fprintf(stderr, "VLC launched.\n");
    return 0;
}

void VLCInput::preRender(uint8_t** pp_pcm_buffer, size_t size)
{
    const size_t max_length = 20 * size;

    for (;;) {
        boost::mutex::scoped_lock lock(m_queue_mutex);

        if (m_queue.size() < max_length) {
            m_current_buf.resize(size);
            *pp_pcm_buffer = &m_current_buf[0];
            return;
        }

        lock.unlock();
        boost::this_thread::sleep(boost::posix_time::milliseconds(1));
    }
}

void VLCInput::postRender(uint8_t* p_pcm_buffer, size_t size)
{
    boost::mutex::scoped_lock lock(m_queue_mutex);

    assert(m_current_buf.size() == size);

    size_t queue_size = m_queue.size();
    m_queue.resize(m_queue.size() + size);
    std::copy(m_current_buf.begin(), m_current_buf.end(),
            m_queue.begin() + queue_size);
}

ssize_t VLCInput::m_read(uint8_t* buf, size_t length)
{
    ssize_t err = 0;
    for (;;) {
        boost::mutex::scoped_lock lock(m_queue_mutex);

        if (m_queue.size() >= length) {
            for (size_t i = 0; i < length; i++) {
                buf[i] = m_queue[i];
            }
            m_queue.erase(m_queue.begin(), m_queue.begin() + length);

            return length;
        }

        lock.unlock();
        boost::this_thread::sleep(boost::posix_time::milliseconds(1));
    }
    return err;
}

ssize_t VLCInput::read(uint8_t* buf, size_t length)
{
    int bytes_per_frame = m_channels * BYTES_PER_SAMPLE;
    assert(length % bytes_per_frame == 0);

    ssize_t read = m_read(buf, length);

    return read;
}

#endif // HAVE_VLC