summaryrefslogtreecommitdiffstats
path: root/src/VLCInput.h
blob: 703641decce49ce34acdfc0ce4c73203825a8e75 (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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
/* ------------------------------------------------------------------
 * Copyright (C) 2017 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.
 * -------------------------------------------------------------------
 */
/*! \file VLCInput.h
 *
 * This input uses libvlc to get audio data. It is extremely useful, and allows
 * the encoder to use all inputs VLC supports.
 */

#pragma once

#include "config.h"

#if HAVE_VLC

#include <cstdio>
#include <string>
#include <sstream>
#include <vector>
#include <deque>
#include <thread>
#include <mutex>
#include <future>

#include <vlc/vlc.h>

#include "SampleQueue.h"
#include "common.h"
#include "InputInterface.h"

extern "C" {
#include "utils.h"
}

/*! Common functionality for the direct libvlc input and the
 *  threaded libvlc input
 */

struct ICY_TEXT_T {
    std::string artist;
    std::string title;
    std::string now_playing;

    bool operator==(const ICY_TEXT_T& other) const {
        return
            artist == other.artist and
            title == other.title and
            now_playing == other.now_playing;
    }
    bool operator!=(const ICY_TEXT_T& other) const {
        return !(*this == other);
    }
    void useArtistTitle(const std::string& artist, const std::string& title) {
        this->artist = artist;
        this->title = title;
        now_playing = "";
    }
    void useNowPlaying(const std::string& now_playing) {
        artist = "";
        title = "";
        this->now_playing = now_playing;
    }
};


class VLCInput : public InputInterface
{
    public:
        VLCInput(const std::string& uri,
                 int rate,
                 unsigned channels,
                 unsigned verbosity,
                 std::string& gain,
                 std::string& cache,
                 std::vector<std::string>& additional_opts,
                 SampleQueue<uint8_t>& queue) :
            m_uri(uri),
            m_verbosity(verbosity),
            m_channels(channels),
            m_rate(rate),
            m_cache(cache),
            m_additional_opts(additional_opts),
            m_gain(gain),
            m_vlc(nullptr),
            m_mp(nullptr),
            m_fault(false),
            m_running(false),
            m_samplequeue(queue) {}

        VLCInput(const VLCInput& other) = delete;
        VLCInput& operator=(const VLCInput& other) = delete;
        ~VLCInput()
        {
            if (m_running) {
                m_running = false;
                m_thread.join();
            }

            cleanup();
        }

        /*! Initialise VLC and start playing, and start
         *  the libVLC thread that fills m_samplequeue */
        virtual void prepare() override;

        /*! Write the last received ICY-Text to the
         * file.
         */
        void write_icy_text(const std::string& filename, bool dl_plus);

        //! Callbacks for VLC

        /*! Notification of VLC exit */
        void exit_cb(void);

        /*! Prepare a buffer for VLC */
        void preRender_cb(
                uint8_t** pp_pcm_buffer,
                size_t size);

        /*! Notification from VLC that the buffer is now filled
         */
        void postRender_cb();

        int getRate() { return m_rate; }

        int getChannels() { return m_channels; }

        virtual bool fault_detected(void) const override { return m_fault; };

        /*! Separator string used when artist/title are written
         */
        static const std::string ICY_TEXT_SEPARATOR;
    private:
        /*! Stop the player and release resources
         */
        void cleanup(void);

        /*! Fill exactly length bytes into buf. Blocking.
         *
         * \return number of bytes written into buf, or
         * -1 in case of error
         */
        ssize_t m_read(uint8_t* buf, size_t length);

        /*! Buffer used in the callback functions for VLC */
        std::vector<float> m_current_buf;

        std::mutex m_queue_mutex;

        /*! Buffer containing all available samples from VLC */
        std::deque<float> m_queue;

        std::string m_uri;
        unsigned m_verbosity;
        unsigned m_channels;
        int m_rate;

        //! Whether to enable network caching in VLC or not
        std::string m_cache;

        //! Given as-is to libvlc, useful for additional arguments
        std::vector<std::string> m_additional_opts;

        /*! value for the VLC compressor filter --compressor-makeup
         * setting. Many more compressor settings could be set.
         */
        std::string m_gain;

        /*! VLC can give us the ICY-Text from an Icecast stream,
         * which we optionally write into a text file for ODR-PadEnc
         */
        std::future<bool> icy_text_written;
        std::mutex m_nowplaying_mutex;
        ICY_TEXT_T m_nowplaying;
        ICY_TEXT_T m_nowplaying_previous;

        // VLC pointers
        libvlc_instance_t     *m_vlc;
        libvlc_media_player_t *m_mp;

        // For the thread

        /* The thread running process takes samples from m_queue and writes
         * them into m_samplequeue. This decouples m_queue from m_samplequeue
         * which is directly used by odr-audioenc.cpp
         */
        void process();

        std::atomic<bool> m_fault;
        std::atomic<bool> m_running;
        std::thread m_thread;

        SampleQueue<uint8_t>& m_samplequeue;
};

#endif // HAVE_VLC