Compare commits

...

38 Commits

Author SHA1 Message Date
Max Kellermann
3c93decdf0 release v0.21.2 2018-11-12 13:33:04 +01:00
Max Kellermann
89e7a5018d doc/protocol.rst: explain song positions vs ids 2018-11-12 13:19:10 +01:00
Max Kellermann
7235b46e5e doc/protocol.rst: rename "current playlist" to "queue" 2018-11-12 13:12:29 +01:00
Max Kellermann
0852226a48 doc/protocol.rst: deprecated close and kill 2018-11-12 13:03:09 +01:00
Max Kellermann
e20d215abf doc/protocol.rst: more markup 2018-11-12 13:01:43 +01:00
Max Kellermann
e4b9b67e24 doc/protocol.rst: deprecation 2018-11-12 12:57:53 +01:00
Max Kellermann
685b78828d doc/protocol.rst: mention that unknown lines may be omitted 2018-11-12 12:57:45 +01:00
Max Kellermann
060908d5c4 song/Filter: add operator "contains"
Closes 
2018-11-12 12:49:01 +01:00
Max Kellermann
0b0f4c61f1 doc/protocol.rst: remove documentation about == matching substrings
I added this sentence in commit
5271e81ebe, but this was merely
documented the legacy status quo, which has always been undocumented
for old-style filters.

But for new filters, using "==" for sub strings was a surprising
"feature", which I removed in commit
ac0852b4e3.
2018-11-12 12:45:40 +01:00
Max Kellermann
228bf7eb09 output/thread: cancel the AudioOutputSource() instead of closing it
This fixes the assertion failure due to calling
AudioOutputSource::Close() twice.
2018-11-12 12:24:25 +01:00
Max Kellermann
5eaf2b8fc3 output/control: always close the AudioOutputSource in RELEASE
Fixes a crash bug with `always_on` outputs which occurs because the
`AudioOutputSource` still has a pointer to an outdated `MusicChunk`.

Fixes 
2018-11-12 12:21:59 +01:00
Max Kellermann
e097fef79e output/control: add command RELEASE
With the new command, the decision to pause or close the output moves
into the output thread.
2018-11-12 12:09:37 +01:00
Max Kellermann
9a813cd3b1 output/Thread: update comment 2018-11-12 12:09:02 +01:00
Max Kellermann
1c60c8e014 output/Filtered: catch Drain() exceptions in CloseOutput() 2018-11-12 12:05:54 +01:00
Max Kellermann
eddda95900 output/interface: document that Drain() may throw 2018-11-12 12:04:42 +01:00
Max Kellermann
72184dccfc song/StringFilter: support regular expressions with "=~" and "!~"
This feature requires `libpcre`.
2018-11-11 12:55:35 +01:00
Max Kellermann
fee75dc766 {output,mixer}/alsa: use snd_pcm_poll_descriptors_revents()
This call was missing, causing very high CPU usage when the ALSA
output plugin was used with dmix.

Closes 
2018-11-11 12:37:29 +01:00
Max Kellermann
ba5c856f15 events/MultiSocketMonitor: add method ForEachResult() 2018-11-11 12:37:28 +01:00
Max Kellermann
12308a0f55 lib/alsa/NonBlock: move the functions into a class managing the state 2018-11-11 12:37:25 +01:00
Max Kellermann
a958abde2f Merge branch 'fix_362' of git://github.com/miccoli/MPD 2018-11-11 12:37:13 +01:00
Max Kellermann
583208db7e output/httpd: fix nullptr dereference crash bug
When `metadata_sent` is `false`, the plugin assumes there is metadata
which must be sent, even if no metadata page was passed to the plugin.
Initializing it to `true` avoids dereferencing this `nullptr`.

Fixes 
2018-11-08 09:37:18 +01:00
Max Kellermann
7b5ba15170 song/Filter: move code to ParseStringFilter() 2018-11-08 00:02:10 +01:00
Max Kellermann
d5e0d49f86 song/{Tag,Uri}SongFilter: pass StringFilter&& to constructor 2018-11-07 23:57:42 +01:00
Max Kellermann
73b22d82aa song/StringFilter: move negated flag from containing class 2018-11-07 23:47:31 +01:00
Max Kellermann
db51cc4e02 lib/zlib/meson.build: add zlib_dep to declare_dependency
Fixes potential compiler error when zlib is installed in a
non-standard directory.
2018-11-07 23:32:23 +01:00
Max Kellermann
be8a52a914 NEWS: mention the ENABLE_ZLIB fix 2018-11-07 23:26:33 +01:00
Max Kellermann
ad597a8ff0 lib/zlib/meson.build: define ENABLE_ZLIB
Fixes 
2018-11-07 23:24:58 +01:00
Max Kellermann
b1fe105904 output/Source: reset current_chunk in Open()
If the output is already open, the `current_chunk` pointer may be
bogus and out of sync with `SharedPipeConsumer::chunk`, leading to an
assertion failure in `SharedPipeConsumer::Consume()`.

Fixes 
2018-11-07 00:17:48 +01:00
Max Kellermann
451b142e3a player/Thread: finish decoder startup before checking the buffer
This fixes a valgrind warning because `buffer_before_play`
initialization needs to know the audio format from the decoder.
2018-11-06 23:52:26 +01:00
Max Kellermann
2833625266 doc/user.rst: more markup 2018-11-06 22:38:34 +01:00
Max Kellermann
0464028872 doc/user.rst: add information about debug build 2018-11-06 22:38:24 +01:00
Max Kellermann
98985c03b0 check.h: remove obsolete ENABLE_LARGEFILE check
Meson always enables large file support on the compiler command line,
thus config.h doesn't need to be included anymore.  We'll remove the
whole `check.h` header soon.

Closes 
2018-11-05 21:25:59 +01:00
Max Kellermann
793fd8c479 decoder/ffmpeg: eliminate GetSampleFormat() 2018-11-04 22:36:17 +01:00
Max Kellermann
6c602811df decoder/ffmepg: fill AudioFormat from AVCodecContext, not AVCodecParameters
`AVCodecParameters` contains values from the codec detected by
avformat_find_stream_info(), but after avcodec_open2(), a different
codec might be selected with a different `AVSampleFormat`.  This leads
to misinterpretation of data returned from FFmpeg, leading to random
noise or silence.

This was observed with FFmpeg 4.0.2 and a TS container file containing
MP2.  A mp3-float codec was detected returning `AV_SAMPLE_FMT_FLTP`,
but finally the `mpegaudiodec_fixed.c` was used, returning
`AV_SAMPLE_FMT_S16`.

By using the audio format from `AVCodecContext`, we ensure that MPD
and FFmpeg always agree on the actual audio format in the buffer.

This removes the FFmpeg bug workaround from commit e1b032cbad which I
assume is obsolete after 7 years.

Fixes 
2018-11-04 22:30:50 +01:00
Stefano Miccoli
6d48a5684a clamp 'set_normalized_volume' to valid values also for ALSA softvol
ensure that valid mixer values are set also when the ALSA driver
does not report a valid dB range ('set_raw' fallback)

correct a bug in which volume is assumed to lie in [0..100]
instead of [0..1]
2018-11-04 22:21:56 +01:00
Max Kellermann
bd115a4008 decoder/ffmpeg: use AtScopeExit() to call av_packet_unref() 2018-11-04 22:01:33 +01:00
Max Kellermann
08272cdee2 decoder/ffmpeg: require FFmpeg 3.1 or later
Drop some compatibility code.
2018-11-04 21:55:06 +01:00
Max Kellermann
b14a5141a6 increment version number to 0.21.2 2018-11-04 19:47:04 +01:00
39 changed files with 736 additions and 270 deletions

14
NEWS

@@ -1,3 +1,17 @@
ver 0.21.2 (2018/11/12)
* protocol
- operator "=~" matches a regular expression
- operator "contains" matches substrings
* decoder
- ffmpeg: require FFmpeg 3.1 or later
- ffmpeg: fix broken sound with certain codecs
* output
- alsa: fix high CPU usage with dmix
- httpd: fix three crash bugs
* mixer
- alsa: fix more rounding errors
* fix zlib support
ver 0.21.1 (2018/11/04)
* protocol
- allow escaping quotes in filter expressions

@@ -2,8 +2,8 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="org.musicpd"
android:installLocation="auto"
android:versionCode="23"
android:versionName="0.21">
android:versionCode="24"
android:versionName="0.21.2">
<uses-sdk android:minSdkVersion="21" android:targetSdkVersion="26"/>

@@ -38,7 +38,7 @@ author = 'Max Kellermann'
# built documents.
#
# The short X.Y version.
version = '0.21.1'
version = '0.21.2'
# The full version, including alpha/beta/rc tags.
release = version

@@ -8,7 +8,7 @@ General protocol syntax
Protocol overview
=================
The ``MPD`` command protocol exchanges
The :program:`MPD` command protocol exchanges
line-based text records between client and server over TCP.
Once the client is connected to the server, they conduct a
conversation until the client closes the connection. The
@@ -152,10 +152,15 @@ of:
``VALUE`` in ``AlbumArtist``
and falls back to ``Artist`` tags if
``AlbumArtist`` does not exist.
``VALUE`` is what to find. The
`find` commands specify an exact value
and are case-sensitive; the `search`
commands specify a sub string and ignore case.
``VALUE`` is what to find.
- ``(TAG contains 'VALUE')`` checks if the given value is a substring
of the tag value.
- ``(TAG =~ 'VALUE')`` and ``(TAG !~ 'VALUE')`` use a Perl-compatible
regular expression instead of doing a simple string comparison.
(This feature is only available if :program:`MPD` was compiled with
:file:`libpcre`)
- ``(file == 'VALUE')``: match the full song URI
(relative to the music directory).
@@ -184,6 +189,9 @@ of:
be enclosed in parantheses, e.g. :code:`((artist == 'FOO') AND
(album == 'BAR'))`
The :command:`find` commands are case sensitive, which
:command:`search` and related commands ignore case.
Prior to MPD 0.21, the syntax looked like this::
find TYPE VALUE
@@ -223,8 +231,7 @@ arguments for you.
Tags
====
The following tags are supported by
``MPD``:
The following tags are supported by :program:`MPD`:
* **artist**: the artist name. Its meaning is not well-defined; see "*composer*" and "*performer*" for more specific tags.
* **artistsort**: same as artist, but for sorting. This usually omits prefixes such as "The".
@@ -250,7 +257,7 @@ The following tags are supported by
* **musicbrainz_workid**: the work id in the `MusicBrainz <https://picard.musicbrainz.org/docs/mappings/>`_ database.
There can be multiple values for some of these tags. For
example, ``MPD`` may return multiple
example, :program:`MPD` may return multiple
lines with a ``performer`` tag. A tag value is
a UTF-8 string.
@@ -293,16 +300,17 @@ Recipes
Queuing
=======
Often, users run ``MPD`` with :ref:`random <command_random>` enabled,
but want to be able to insert songs "before" the rest of the playlist.
That is commonly called "queuing".
Often, users run :program:`MPD` with :ref:`random <command_random>`
enabled, but want to be able to insert songs "before" the rest of the
playlist. That is commonly called "queuing".
``MPD`` implements this by allowing the client to specify a "priority"
for each song in the playlist (commands :ref:`priod <command_prio>`
and :ref:`priodid <command_prioid>`). A higher priority means that
the song is going to be played before the other songs.
:program:`MPD` implements this by allowing the client to specify a
"priority" for each song in the playlist (commands :ref:`priod
<command_prio>` and :ref:`priodid <command_prioid>`). A higher
priority means that the song is going to be played before the other
songs.
In "random" mode, ``MPD`` maintains an
In "random" mode, :program:`MPD` maintains an
internal randomized sequence of songs. In this sequence,
songs with a higher priority come first, and all songs with
the same priority are shuffled (by default, all songs are
@@ -327,9 +335,9 @@ Command reference
commands using song ids should be used instead of the commands
that manipulate and control playback based on playlist
position. Using song ids is a safer method when multiple
clients are interacting with ``MPD``.
clients are interacting with :program:`MPD`.
Querying ``MPD``'s status
Querying :program:`MPD`'s status
================================
:command:`clearerror`
@@ -344,7 +352,7 @@ Querying ``MPD``'s status
:command:`idle [SUBSYSTEMS...]` [#since_0_14]_
Waits until there is a noteworthy change in one or more
of ``MPD``'s subsystems. As soon
of :program:`MPD`'s subsystems. As soon
as there is one, it lists all changed systems in a line
in the format ``changed:
SUBSYSTEM``, where SUBSYSTEM is one of the
@@ -353,7 +361,7 @@ Querying ``MPD``'s status
- ``database``: the song database has been modified after :ref:`update <command_update>`.
- ``update``: a database update has started or finished. If the database was modified during the update, the ``database`` event is also emitted.
- ``stored_playlist``: a stored playlist has been modified, renamed, created or deleted
- ``playlist``: the current playlist has been modified
- ``playlist``: the queue (i.e. the current playlist) has been modified
- ``player``: the player has been started, stopped or seeked
- ``mixer``: the volume has been changed
- ``output``: an audio output has been added, removed or modified (e.g. renamed, enabled or disabled)
@@ -374,11 +382,11 @@ Querying ``MPD``'s status
to wait for events as long as mpd runs. The
`idle` command can be canceled by
sending the command `noidle` (no other
commands are allowed). ``MPD``
commands are allowed). :program:`MPD`
will then leave `idle` mode and print
results immediately; might be empty at this time.
If the optional ``SUBSYSTEMS`` argument
is used, ``MPD`` will only send
is used, :program:`MPD` will only send
notifications when something changed in one of the
specified subsytems.
@@ -388,7 +396,8 @@ Querying ``MPD``'s status
Reports the current status of the player and the volume
level.
- ``volume``: ``0-100`` or ``-1`` if the volume cannot be determined
- ``volume``: ``0-100`` (deprecated: ``-1`` if the volume cannot
be determined)
- ``repeat``: ``0`` or ``1``
- ``random``: ``0`` or ``1``
- ``single`` [#since_0_15]_: ``0``, ``1``, or ``oneshot`` [#since_0_21]_
@@ -401,6 +410,7 @@ Querying ``MPD``'s status
- ``nextsong`` [#since_0_15]_: playlist song number of the next song to be played
- ``nextsongid`` [#since_0_15]_: playlist songid of the next song to be played
- ``time``: total time elapsed (of current playing/paused song)
(deprecated, use ``elapsed`` instead)
- ``elapsed`` [#since_0_16]_: Total time elapsed within the current song, but with higher resolution.
- ``duration`` [#since_0_20]_: Duration of the current song in seconds.
- ``bitrate``: instantaneous bitrate in kbps
@@ -411,6 +421,10 @@ Querying ``MPD``'s status
- ``updating_db``: ``job id``
- ``error``: if there is an error, returns message here
:program:`MPD` may omit lines which have no (known) value. Older
:program:`MPD` versions used to have a "magic" value for
"unknown", e.g. ":samp:`volume: -1`".
:command:`stats`
Displays statistics.
@@ -524,8 +538,36 @@ Controlling playback
:command:`stop`
Stops playing.
The current playlist
====================
The Queue
=========
.. note:: The "queue" used to be called "current playlist" or just
"playlist", but that was deemed confusing, because
"playlists" are also files containing a sequence of songs.
Those "playlist files" or "stored playlists" can be
:ref:`loaded into the queue <command_load>` and the queue
can be :ref:`saved into a playlist file <command_save>`, but
they are not to be confused with the queue.
Many of the command names in this section reflect the old
naming convention, but for the sake of compatibility, we
cannot rename commands.
There are two ways to address songs within the queue: by their
position and by their id.
The position is a 1-based index. It is unstable by design: if you
move, delete or insert songs, all following indices will change, and a
client can never be sure what song is behind a given index/position.
Song ids on the other hand are stable: an id is assigned to a song
when it is added, and will stay the same, no matter how much it is
moved around. Adding the same song twice will assign different ids to
them, and a deleted-and-readded song will have a new id. This way, a
client can always be sure the correct song is being used.
Many commands come in two flavors, one for each address type.
Whenever possible, ids should be used.
:command:`add {URI}`
Adds the file ``URI`` to the playlist
@@ -541,7 +583,7 @@ The current playlist
OK
:command:`clear`
Clears the current playlist.
Clears the queue.
.. _command_delete:
@@ -566,13 +608,13 @@ The current playlist
:command:`playlist`
Displays the current playlist.
Displays the queue.
Do not use this, instead use :ref:`playlistinfo
<command_playlistinfo>`.
:command:`playlistfind {TAG} {NEEDLE}`
Finds songs in the current playlist with strict
Finds songs in the queue with strict
matching.
:command:`playlistid {SONGID}`
@@ -590,7 +632,7 @@ The current playlist
:command:`playlistsearch {TAG} {NEEDLE}`
Searches case-insensitively for partial matches in the
current playlist.
queue.
:command:`plchanges {VERSION} [START:END]`
Displays changed songs currently in the playlist since
@@ -627,7 +669,7 @@ The current playlist
but address the songs with their id.
:command:`rangeid {ID} {START:END}` [#since_0_19]_
Since ``MPD``
Since :program:`MPD`
0.19 Specifies the portion of the
song that shall be played. ``START`` and
``END`` are offsets in seconds
@@ -637,7 +679,7 @@ The current playlist
playing cannot be manipulated this way.
:command:`shuffle [START:END]`
Shuffles the current playlist.
Shuffles the queue.
``START:END`` is optional and specifies
a range of songs.
@@ -691,6 +733,8 @@ remote playlists (absolute URI with a supported scheme).
between clients and the server, clients should not
compare this value with their local clock.
.. _command_load:
:command:`load {NAME} [START:END]`
Loads the playlist into the current queue. Playlist
plugins are supported. A range may be specified to load
@@ -721,8 +765,10 @@ remote playlists (absolute URI with a supported scheme).
Removes the playlist `NAME.m3u` from
the playlist directory.
.. _command_save:
:command:`save {NAME}`
Saves the current playlist to
Saves the queue to
`NAME.m3u` in the playlist directory.
The music database
@@ -807,7 +853,7 @@ The music database
:command:`list {TYPE} {FILTER} [group {GROUPTYPE}]`
Lists unique tags values of the specified type.
``TYPE`` can be any tag supported by
``MPD`` or
:program:`MPD` or
*file*.
Additional arguments may specify a :ref:`filter <filter_syntax>`.
@@ -826,10 +872,10 @@ The music database
``URI``.
Do not use this command. Do not manage a client-side
copy of ``MPD``'s database. That
copy of :program:`MPD`'s database. That
is fragile and adds huge overhead. It will break with
large databases. Instead, query
``MPD`` whenever you need
:program:`MPD` whenever you need
something.
.. _command_listallinfo:
@@ -840,16 +886,16 @@ The music database
as :ref:`lsinfo <command_lsinfo>`
Do not use this command. Do not manage a client-side
copy of ``MPD``'s database. That
copy of :program:`MPD`'s database. That
is fragile and adds huge overhead. It will break with
large databases. Instead, query
``MPD`` whenever you need
:program:`MPD` whenever you need
something.
:command:`listfiles {URI}`
Lists the contents of the directory
``URI``, including files are not
recognized by ``MPD``.
recognized by :program:`MPD`.
``URI`` can be a path relative to the
music directory or an URI understood by one of the
storage plugins. The response contains at least one
@@ -956,7 +1002,7 @@ Multiple storages can be "mounted" together, similar to the
`mount` command on many operating
systems, but without cooperation from the kernel. No
superuser privileges are necessary, beause this mapping exists
only inside the ``MPD`` process
only inside the :program:`MPD` process
.. _command_mount:
@@ -999,15 +1045,15 @@ Stickers
"Stickers" [#since_0_15]_ are pieces of
information attached to existing
``MPD`` objects (e.g. song files,
:program:`MPD` objects (e.g. song files,
directories, albums). Clients can create arbitrary name/value
pairs. ``MPD`` itself does not assume
pairs. :program:`MPD` itself does not assume
any special meaning in them.
The goal is to allow clients to share additional (possibly
dynamic) information about songs, which is neither stored on
the client (not available to other clients), nor stored in the
song files (``MPD`` has no write
song files (:program:`MPD` has no write
access).
Client developers should create a standard for common sticker
@@ -1049,14 +1095,21 @@ Connection settings
===================
:command:`close`
Closes the connection to ``MPD``.
``MPD`` will try to send the
Closes the connection to :program:`MPD`.
:program:`MPD` will try to send the
remaining output buffer before it actually closes the
connection, but that cannot be guaranteed. This command
will not generate a response.
Clients should not use this command; instead, they should just
close the socket.
:command:`kill`
Kills ``MPD``.
Kills :program:`MPD`.
Do not use this command. Send ``SIGTERM`` to :program:`MPD`
instead, or better: let your service manager handle :program:`MPD`
shutdown (e.g. :command:`systemctl stop mpd`).
:command:`password {PASSWORD}`
This is used for authentication with the server.
@@ -1090,7 +1143,7 @@ Connection settings
:command:`tagtypes clear`
Clear the list of tag types this client is interested
in. This means that ``MPD`` will
in. This means that :program:`MPD` will
not send any tags to this client.
:command:`tagtypes all`

@@ -67,6 +67,7 @@ For example, the following installs a fairly complete list of build dependencies
.. code-block:: none
apt install g++ \
libpcre3-dev \
libmad0-dev libmpg123-dev libid3tag0-dev \
libflac-dev libvorbis-dev libopus-dev \
libadplug-dev libaudiofile-dev libsndfile1-dev libfaad-dev \
@@ -967,7 +968,16 @@ All :program:`MPD` crashes are bugs which must be fixed by a developer, and you
A crash bug report needs to contain a "backtrace".
First of all, your :program:`MPD` executable must not be "stripped" (i.e. debug information deleted). The executables shipped with Linux distributions are usually stripped, but some have so-called "debug" packages (package mpd-dbg or mpd-dbgsym on Debian, mpd-debug on other distributions). Make sure this package is installed.
First of all, your :program:`MPD` executable must not be "stripped"
(i.e. debug information deleted). The executables shipped with Linux
distributions are usually stripped, but some have so-called "debug"
packages (package :file:`mpd-dbgsym` or :file:`mpd-dbg` on Debian,
:file:`mpd-debug` on other distributions). Make sure this package is
installed.
If you built :program:`MPD` from sources, please recompile with Meson
option ":code:`--buildtype=debug -Db_ndebug=false`", because this will
add more helpful information to the backtrace.
You can extract the backtrace from a core dump, or by running :program:`MPD` in a debugger, e.g.:
@@ -976,4 +986,5 @@ You can extract the backtrace from a core dump, or by running :program:`MPD` in
gdb --args mpd --stdout --no-daemon --verbose
run
As soon as you have reproduced the crash, type "bt" on the gdb command prompt. Copy the output to your bug report.
As soon as you have reproduced the crash, type ":command:`bt`" on the
gdb command prompt. Copy the output to your bug report.

@@ -1,7 +1,7 @@
project(
'mpd',
['c', 'cpp'],
version: '0.21.1',
version: '0.21.2',
meson_version: '>= 0.47.2',
default_options: [
'c_std=c99',
@@ -315,6 +315,7 @@ subdir('src/lib/gcrypt')
subdir('src/lib/wrap')
subdir('src/lib/nfs')
subdir('src/lib/oss')
subdir('src/lib/pcre')
subdir('src/lib/pulse')
subdir('src/lib/sndio')
subdir('src/lib/sqlite')

@@ -176,6 +176,7 @@ option('expat', type: 'feature', description: 'Expat XML support')
option('icu', type: 'feature', description: 'Use libicu for Unicode')
option('iconv', type: 'feature', description: 'Use iconv() for character set conversion')
option('libwrap', type: 'feature', description: 'libwrap support')
option('pcre', type: 'feature', description: 'Enable regular expression support (using libpcre)')
option('sqlite', type: 'feature', description: 'SQLite database support (for stickers)')
option('yajl', type: 'feature', description: 'libyajl for YAML support')
option('zlib', type: 'feature', description: 'zlib support (for database compression)')

@@ -37,11 +37,4 @@
#error config.h missing
#endif
#if defined(__linux__) && !defined(NDEBUG) && defined(ENABLE_LARGEFILE) && \
defined(_FEATURES_H) && defined(__i386__) && \
!defined(__USE_FILE_OFFSET64)
/* on i386, check if LFS is enabled */
#error config.h was included too late
#endif
#endif

@@ -105,45 +105,11 @@ ffmpeg_finish() noexcept
av_dict_free(&avformat_options);
}
#if LIBAVCODEC_VERSION_INT >= AV_VERSION_INT(57, 25, 0) /* FFmpeg 3.1 */
gcc_pure
static const AVCodecParameters &
GetCodecParameters(const AVStream &stream) noexcept
{
return *stream.codecpar;
}
gcc_pure
static AVSampleFormat
GetSampleFormat(const AVCodecParameters &codec_params) noexcept
{
return AVSampleFormat(codec_params.format);
}
#else
gcc_pure
static const AVCodecContext &
GetCodecParameters(const AVStream &stream) noexcept
{
return *stream.codec;
}
gcc_pure
static AVSampleFormat
GetSampleFormat(const AVCodecContext &codec_context) noexcept
{
return codec_context.sample_fmt;
}
#endif
gcc_pure
static bool
IsAudio(const AVStream &stream) noexcept
{
return GetCodecParameters(stream).codec_type == AVMEDIA_TYPE_AUDIO;
return stream.codecpar->codec_type == AVMEDIA_TYPE_AUDIO;
}
gcc_pure
@@ -278,8 +244,6 @@ FfmpegSendFrame(DecoderClient &client, InputStream &is,
codec_context.bit_rate / 1000);
}
#if LIBAVCODEC_VERSION_INT >= AV_VERSION_INT(57, 37, 0)
static DecoderCommand
FfmpegReceiveFrames(DecoderClient &client, InputStream &is,
AVCodecContext &codec_context,
@@ -324,8 +288,6 @@ FfmpegReceiveFrames(DecoderClient &client, InputStream &is,
}
}
#endif
/**
* Decode an #AVPacket and send the resulting PCM data to the decoder
* API.
@@ -357,7 +319,6 @@ ffmpeg_send_packet(DecoderClient &client, InputStream &is,
stream.time_base));
}
#if LIBAVCODEC_VERSION_INT >= AV_VERSION_INT(57, 37, 0)
bool eof = false;
int err = avcodec_send_packet(&codec_context, &packet);
@@ -386,30 +347,6 @@ ffmpeg_send_packet(DecoderClient &client, InputStream &is,
if (eof)
cmd = DecoderCommand::STOP;
#else
DecoderCommand cmd = DecoderCommand::NONE;
while (packet.size > 0 && cmd == DecoderCommand::NONE) {
int got_frame = 0;
int len = avcodec_decode_audio4(&codec_context,
&frame, &got_frame,
&packet);
if (len < 0) {
/* if error, we skip the frame */
LogFfmpegError(len, "decoding failed, frame skipped");
break;
}
packet.data += len;
packet.size -= len;
if (!got_frame || frame.nb_samples <= 0)
continue;
cmd = FfmpegSendFrame(client, is, codec_context,
frame, skip_bytes,
buffer);
}
#endif
return cmd;
}
@@ -585,11 +522,7 @@ FfmpegDecode(DecoderClient &client, InputStream &input,
AVStream &av_stream = *format_context.streams[audio_stream];
#if LIBAVFORMAT_VERSION_INT < AV_VERSION_INT(57, 5, 0)
AVCodecContext *codec_context = av_stream.codec;
#endif
const auto &codec_params = GetCodecParameters(av_stream);
const auto &codec_params = *av_stream.codecpar;
const AVCodecDescriptor *codec_descriptor =
avcodec_descriptor_get(codec_params.codec_id);
@@ -604,7 +537,6 @@ FfmpegDecode(DecoderClient &client, InputStream &input,
return;
}
#if LIBAVFORMAT_VERSION_INT >= AV_VERSION_INT(57, 5, 0)
AVCodecContext *codec_context = avcodec_alloc_context3(codec);
if (codec_context == nullptr) {
LogError(ffmpeg_domain, "avcodec_alloc_context3() failed");
@@ -615,26 +547,7 @@ FfmpegDecode(DecoderClient &client, InputStream &input,
avcodec_free_context(&codec_context);
};
#if LIBAVCODEC_VERSION_INT >= AV_VERSION_INT(57, 25, 0) /* FFmpeg 3.1 */
avcodec_parameters_to_context(codec_context, av_stream.codecpar);
#endif
#endif
const SampleFormat sample_format =
ffmpeg_sample_format(GetSampleFormat(codec_params));
if (sample_format == SampleFormat::UNDEFINED) {
// (error message already done by ffmpeg_sample_format())
return;
}
const auto audio_format = CheckAudioFormat(codec_params.sample_rate,
sample_format,
codec_params.channels);
/* the audio format must be read from AVCodecContext by now,
because avcodec_open() has been demonstrated to fill bogus
values into AVCodecContext.channels - a change that will be
reverted later by avcodec_decode_audio3() */
const int open_result = avcodec_open2(codec_context, codec, nullptr);
if (open_result < 0) {
@@ -642,11 +555,16 @@ FfmpegDecode(DecoderClient &client, InputStream &input,
return;
}
#if LIBAVFORMAT_VERSION_INT < AV_VERSION_INT(57, 5, 0)
AtScopeExit(codec_context) {
avcodec_close(codec_context);
};
#endif
const SampleFormat sample_format =
ffmpeg_sample_format(codec_context->sample_fmt);
if (sample_format == SampleFormat::UNDEFINED) {
// (error message already done by ffmpeg_sample_format())
return;
}
const auto audio_format = CheckAudioFormat(codec_context->sample_rate,
sample_format,
codec_context->channels);
const SignedSongTime total_time =
av_stream.duration != (int64_t)AV_NOPTS_VALUE
@@ -697,6 +615,10 @@ FfmpegDecode(DecoderClient &client, InputStream &input,
/* end of file */
break;
AtScopeExit(&packet) {
av_packet_unref(&packet);
};
FfmpegCheckTag(client, input, format_context, audio_stream);
if (packet.size > 0 && packet.stream_index == audio_stream) {
@@ -710,12 +632,6 @@ FfmpegDecode(DecoderClient &client, InputStream &input,
min_frame = 0;
} else
cmd = client.GetCommand();
#if LIBAVCODEC_VERSION_INT >= AV_VERSION_INT(56, 25, 100)
av_packet_unref(&packet);
#else
av_free_packet(&packet);
#endif
}
}
@@ -769,10 +685,10 @@ FfmpegScanStream(AVFormatContext &format_context,
handler.OnDuration(FromFfmpegTime(format_context.duration,
AV_TIME_BASE_Q));
const auto &codec_params = GetCodecParameters(stream);
const auto &codec_params = *stream.codecpar;
try {
handler.OnAudioFormat(CheckAudioFormat(codec_params.sample_rate,
ffmpeg_sample_format(GetSampleFormat(codec_params)),
ffmpeg_sample_format(AVSampleFormat(codec_params.format)),
codec_params.channels));
} catch (...) {
}

@@ -194,6 +194,19 @@ public:
void ReplaceSocketList(pollfd *pfds, unsigned n) noexcept;
#endif
/**
* Invoke a function for each socket which has become ready.
*/
template<typename F>
void ForEachReturnedEvent(F &&f) noexcept {
for (auto &i : fds) {
if (i.GetReturnedEvents() != 0) {
f(i.GetSocket(), i.GetReturnedEvents());
i.ClearReturnedEvents();
}
}
}
protected:
/**
* Override this method and update the socket registrations.

@@ -34,7 +34,6 @@
#include "util/Domain.hxx"
#include "util/RuntimeError.hxx"
#include "util/StringCompare.hxx"
#include "util/ReusableArray.hxx"
#include "util/ASCII.hxx"
#include "Log.hxx"
#include "event/MultiSocketMonitor.hxx"
@@ -69,7 +68,7 @@ class AlsaInputStream final
snd_pcm_t *const capture_handle;
const size_t frame_size;
ReusableArray<pollfd> pfd_buffer;
AlsaNonBlockPcm non_block;
DeferEvent defer_invalidate_sockets;
@@ -180,12 +179,14 @@ AlsaInputStream::PrepareSockets() noexcept
return std::chrono::steady_clock::duration(-1);
}
return PrepareAlsaPcmSockets(*this, capture_handle, pfd_buffer);
return non_block.PrepareSockets(*this, capture_handle);
}
void
AlsaInputStream::DispatchSockets() noexcept
{
non_block.DispatchSockets(*this, capture_handle);
const std::lock_guard<Mutex> protect(mutex);
auto w = PrepareWriteBuffer();

@@ -23,8 +23,7 @@
#include "util/RuntimeError.hxx"
std::chrono::steady_clock::duration
PrepareAlsaPcmSockets(MultiSocketMonitor &m, snd_pcm_t *pcm,
ReusableArray<pollfd> &pfd_buffer)
AlsaNonBlockPcm::PrepareSockets(MultiSocketMonitor &m, snd_pcm_t *pcm)
{
int count = snd_pcm_poll_descriptors_count(pcm);
if (count <= 0) {
@@ -50,9 +49,32 @@ PrepareAlsaPcmSockets(MultiSocketMonitor &m, snd_pcm_t *pcm,
return std::chrono::steady_clock::duration(-1);
}
void
AlsaNonBlockPcm::DispatchSockets(MultiSocketMonitor &m,
snd_pcm_t *pcm) noexcept
{
int count = snd_pcm_poll_descriptors_count(pcm);
if (count <= 0)
return;
const auto pfds = pfd_buffer.Get(count), end = pfds + count;
auto *i = pfds;
m.ForEachReturnedEvent([&i, end](SocketDescriptor s, unsigned events){
if (i >= end)
return;
i->fd = s.Get();
i->events = i->revents = events;
++i;
});
unsigned short dummy;
snd_pcm_poll_descriptors_revents(pcm, pfds, i - pfds, &dummy);
}
std::chrono::steady_clock::duration
PrepareAlsaMixerSockets(MultiSocketMonitor &m, snd_mixer_t *mixer,
ReusableArray<pollfd> &pfd_buffer) noexcept
AlsaNonBlockMixer::PrepareSockets(MultiSocketMonitor &m, snd_mixer_t *mixer) noexcept
{
int count = snd_mixer_poll_descriptors_count(mixer);
if (count <= 0) {
@@ -69,3 +91,27 @@ PrepareAlsaMixerSockets(MultiSocketMonitor &m, snd_mixer_t *mixer,
m.ReplaceSocketList(pfds, count);
return std::chrono::steady_clock::duration(-1);
}
void
AlsaNonBlockMixer::DispatchSockets(MultiSocketMonitor &m,
snd_mixer_t *mixer) noexcept
{
int count = snd_mixer_poll_descriptors_count(mixer);
if (count <= 0)
return;
const auto pfds = pfd_buffer.Get(count), end = pfds + count;
auto *i = pfds;
m.ForEachReturnedEvent([&i, end](SocketDescriptor s, unsigned events){
if (i >= end)
return;
i->fd = s.Get();
i->events = i->revents = events;
++i;
});
unsigned short dummy;
snd_mixer_poll_descriptors_revents(mixer, pfds, i - pfds, &dummy);
}

@@ -30,23 +30,42 @@
class MultiSocketMonitor;
/**
* Update #MultiSocketMonitor's socket list from
* snd_pcm_poll_descriptors(). To be called from
* MultiSocketMonitor::PrepareSockets().
*
* Throws exception on error.
* Helper class for #MultiSocketMonitor's virtual methods which
* manages the file descriptors for a #snd_pcm_t.
*/
std::chrono::steady_clock::duration
PrepareAlsaPcmSockets(MultiSocketMonitor &m, snd_pcm_t *pcm,
ReusableArray<pollfd> &pfd_buffer);
class AlsaNonBlockPcm {
ReusableArray<pollfd> pfd_buffer;
public:
/**
* Throws on error.
*/
std::chrono::steady_clock::duration PrepareSockets(MultiSocketMonitor &m,
snd_pcm_t *pcm);
/**
* Wrapper for snd_pcm_poll_descriptors_revents(), to be
* called from MultiSocketMonitor::DispatchSockets().
*/
void DispatchSockets(MultiSocketMonitor &m, snd_pcm_t *pcm) noexcept;
};
/**
* Update #MultiSocketMonitor's socket list from
* snd_mixer_poll_descriptors(). To be called from
* MultiSocketMonitor::PrepareSockets().
* Helper class for #MultiSocketMonitor's virtual methods which
* manages the file descriptors for a #snd_mixer_t.
*/
std::chrono::steady_clock::duration
PrepareAlsaMixerSockets(MultiSocketMonitor &m, snd_mixer_t *mixer,
ReusableArray<pollfd> &pfd_buffer) noexcept;
class AlsaNonBlockMixer {
ReusableArray<pollfd> pfd_buffer;
public:
std::chrono::steady_clock::duration PrepareSockets(MultiSocketMonitor &m,
snd_mixer_t *mixer) noexcept;
/**
* Wrapper for snd_mixer_poll_descriptors_revents(), to be
* called from MultiSocketMonitor::DispatchSockets().
*/
void DispatchSockets(MultiSocketMonitor &m, snd_mixer_t *mixer) noexcept;
};
#endif

@@ -1,6 +1,6 @@
libavformat_dep = dependency('libavformat', version: '>= 56.1', required: get_option('ffmpeg'))
libavcodec_dep = dependency('libavcodec', version: '>= 56.1', required: get_option('ffmpeg'))
libavutil_dep = dependency('libavutil', version: '>= 54.3', required: get_option('ffmpeg'))
libavformat_dep = dependency('libavformat', version: '>= 57.40', required: get_option('ffmpeg'))
libavcodec_dep = dependency('libavcodec', version: '>= 57.48', required: get_option('ffmpeg'))
libavutil_dep = dependency('libavutil', version: '>= 55.27', required: get_option('ffmpeg'))
enable_ffmpeg = libavformat_dep.found() and libavcodec_dep.found() and libavutil_dep.found()
conf.set('ENABLE_FFMPEG', enable_ffmpeg)

@@ -0,0 +1,66 @@
/*
* Copyright 2007-2018 Content Management AG
* All rights reserved.
*
* author: Max Kellermann <mk@cm4all.com>
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* - Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* - Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the
* distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* FOUNDATION OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
* OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#ifndef REGEX_POINTER_HXX
#define REGEX_POINTER_HXX
#include "util/StringView.hxx"
#include "util/Compiler.h"
#include <pcre.h>
#include <array>
class RegexPointer {
protected:
pcre *re = nullptr;
pcre_extra *extra = nullptr;
unsigned n_capture = 0;
public:
constexpr bool IsDefined() const noexcept {
return re != nullptr;
}
gcc_pure
bool Match(StringView s) const noexcept {
/* we don't need the data written to ovector, but PCRE can
omit internal allocations if we pass a buffer to
pcre_exec() */
std::array<int, 16> ovector;
return pcre_exec(re, extra, s.data, s.size,
0, 0, &ovector.front(), ovector.size()) >= 0;
}
};
#endif

@@ -0,0 +1,71 @@
/*
* Copyright 2007-2018 Content Management AG
* All rights reserved.
*
* author: Max Kellermann <mk@cm4all.com>
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* - Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* - Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the
* distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* FOUNDATION OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
* OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "UniqueRegex.hxx"
#include "util/RuntimeError.hxx"
void
UniqueRegex::Compile(const char *pattern, bool anchored, bool capture,
bool caseless)
{
constexpr int default_options = PCRE_DOTALL|PCRE_NO_AUTO_CAPTURE|PCRE_UTF8;
int options = default_options;
if (anchored)
options |= PCRE_ANCHORED;
if (capture)
options &= ~PCRE_NO_AUTO_CAPTURE;
if (caseless)
options |= PCRE_CASELESS;
const char *error_string;
int error_offset;
re = pcre_compile(pattern, options, &error_string, &error_offset, nullptr);
if (re == nullptr)
throw FormatRuntimeError("Error in regex at offset %d: %s",
error_offset, error_string);
int study_options = 0;
#ifdef PCRE_CONFIG_JIT
study_options |= PCRE_STUDY_JIT_COMPILE;
#endif
extra = pcre_study(re, study_options, &error_string);
if (extra == nullptr && error_string != nullptr) {
pcre_free(re);
re = nullptr;
throw FormatRuntimeError("Regex study error: %s", error_string);
}
int n;
if (capture && pcre_fullinfo(re, extra, PCRE_INFO_CAPTURECOUNT, &n) == 0)
n_capture = n;
}

@@ -0,0 +1,79 @@
/*
* Copyright 2007-2018 Content Management AG
* All rights reserved.
*
* author: Max Kellermann <mk@cm4all.com>
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* - Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* - Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the
* distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* FOUNDATION OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
* OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#ifndef UNIQUE_REGEX_HXX
#define UNIQUE_REGEX_HXX
#include "RegexPointer.hxx"
#include "util/StringView.hxx"
#include <algorithm>
#include <pcre.h>
class UniqueRegex : public RegexPointer {
public:
UniqueRegex() = default;
UniqueRegex(const char *pattern, bool anchored, bool capture,
bool caseless) {
Compile(pattern, anchored, capture, caseless);
}
UniqueRegex(UniqueRegex &&src) noexcept:RegexPointer(src) {
src.re = nullptr;
src.extra = nullptr;
}
~UniqueRegex() noexcept {
pcre_free(re);
#ifdef PCRE_CONFIG_JIT
pcre_free_study(extra);
#else
pcre_free(extra);
#endif
}
UniqueRegex &operator=(UniqueRegex &&src) {
using std::swap;
swap<RegexPointer>(*this, src);
return *this;
}
/**
* Throws std::runtime_error on error.
*/
void Compile(const char *pattern, bool anchored, bool capture,
bool caseless);
};
#endif

21
src/lib/pcre/meson.build Normal file

@@ -0,0 +1,21 @@
pcre_dep = dependency('libpcre', required: get_option('pcre'))
conf.set('HAVE_PCRE', pcre_dep.found())
if not pcre_dep.found()
subdir_done()
endif
pcre = static_library(
'pcre',
'UniqueRegex.cxx',
include_directories: inc,
dependencies: [
pcre_dep,
],
)
pcre_dep = declare_dependency(
link_with: pcre,
dependencies: [
pcre_dep,
],
)

@@ -1,4 +1,5 @@
zlib_dep = dependency('zlib', required: get_option('zlib'))
conf.set('ENABLE_ZLIB', zlib_dep.found())
if not zlib_dep.found()
subdir_done()
endif
@@ -14,4 +15,7 @@ zlib = static_library(
zlib_dep = declare_dependency(
link_with: zlib,
dependencies: [
zlib_dep,
],
)

@@ -26,7 +26,6 @@
#include "event/DeferEvent.hxx"
#include "event/Call.hxx"
#include "util/ASCII.hxx"
#include "util/ReusableArray.hxx"
#include "util/Domain.hxx"
#include "util/RuntimeError.hxx"
#include "Log.hxx"
@@ -48,7 +47,7 @@ class AlsaMixerMonitor final : MultiSocketMonitor {
snd_mixer_t *mixer;
ReusableArray<pollfd> pfd_buffer;
AlsaNonBlockMixer non_block;
public:
AlsaMixerMonitor(EventLoop &_loop, snd_mixer_t *_mixer)
@@ -110,7 +109,7 @@ AlsaMixerMonitor::PrepareSockets() noexcept
return std::chrono::steady_clock::duration(-1);
}
return PrepareAlsaMixerSockets(*this, mixer, pfd_buffer);
return non_block.PrepareSockets(*this, mixer);
}
void
@@ -118,6 +117,8 @@ AlsaMixerMonitor::DispatchSockets() noexcept
{
assert(mixer != nullptr);
non_block.DispatchSockets(*this, mixer);
int err = snd_mixer_handle_events(mixer);
if (err < 0) {
FormatError(alsa_mixer_domain,

@@ -135,6 +135,13 @@ static int set_normalized_volume(snd_mixer_elem_t *elem,
if (err < 0)
return err;
/* two special cases to avoid rounding errors at 0% and
100% */
if (volume <= 0)
return set_raw[ctl_dir](elem, min);
else if (volume >= 1)
return set_raw[ctl_dir](elem, max);
value = lrint_dir(volume * (max - min), dir) + min;
return set_raw[ctl_dir](elem, value);
}
@@ -143,7 +150,7 @@ static int set_normalized_volume(snd_mixer_elem_t *elem,
100% */
if (volume <= 0)
return set_dB[ctl_dir](elem, min, dir);
else if (volume >= 100)
else if (volume >= 1)
return set_dB[ctl_dir](elem, max, dir);
if (use_linear_dB_scale(min, max)) {

@@ -347,10 +347,22 @@ AudioOutputControl::LockAllowPlay() noexcept
void
AudioOutputControl::LockRelease() noexcept
{
if (always_on)
LockPauseAsync();
if (output->mixer != nullptr &&
(!always_on || !output->SupportsPause()))
/* the device has no pause mode: close the mixer,
unless its "global" flag is set (checked by
mixer_auto_close()) */
mixer_auto_close(output->mixer);
const std::lock_guard<Mutex> protect(mutex);
assert(!open || !fail_timer.IsDefined());
assert(allow_play);
if (IsOpen())
CommandWait(Command::RELEASE);
else
LockCloseWait();
fail_timer.Reset();
}
void

@@ -131,6 +131,12 @@ class AudioOutputControl {
CLOSE,
PAUSE,
/**
* Close or pause the device, depending on the
* #always_on setting.
*/
RELEASE,
/**
* Drains the internal (hardware) buffers of the device. This
* operation may take a while to complete.

@@ -124,9 +124,14 @@ FilteredAudioOutput::OpenOutputAndConvert(AudioFormat desired_audio_format)
void
FilteredAudioOutput::CloseOutput(bool drain) noexcept
{
if (drain)
Drain();
else
if (drain) {
try {
Drain();
} catch (...) {
FormatError(std::current_exception(),
"Failed to drain %s", GetLogName());
}
} else
Cancel();
output->Close();

@@ -159,6 +159,8 @@ public:
/**
* Wait until the device has finished playing.
*
* Throws on error.
*/
virtual void Drain() {}

@@ -41,8 +41,10 @@ AudioOutputSource::Open(const AudioFormat audio_format, const MusicPipe &_pipe,
{
assert(audio_format.IsValid());
if (!IsOpen() || &_pipe != &pipe.GetPipe())
if (!IsOpen() || &_pipe != &pipe.GetPipe()) {
current_chunk = nullptr;
pipe.Init(_pipe);
}
/* (re)open the filter */

@@ -443,8 +443,7 @@ AudioOutputControl::Task() noexcept
case Command::PAUSE:
if (!open) {
/* the output has failed after
audio_output_all_pause() has
submitted the PAUSE command; bail
the PAUSE command was submitted; bail
out */
CommandFinished();
break;
@@ -457,6 +456,35 @@ AudioOutputControl::Task() noexcept
the new command first */
continue;
case Command::RELEASE:
if (!open) {
/* the output has failed after
the PAUSE command was submitted; bail
out */
CommandFinished();
break;
}
if (always_on) {
/* in "always_on" mode, the output is
paused instead of being closed;
however we need to flush the
AudioOutputSource because its data
have been invalidated by stopping
the actual playback */
source.Cancel();
InternalPause();
} else {
InternalClose(false);
CommandFinished();
}
/* don't "break" here: this might cause
Play() to be called when command==CLOSE
ends the paused state - "continue" checks
the new command first */
continue;
case Command::DRAIN:
if (open)
InternalDrain();

@@ -148,10 +148,7 @@ class AlsaOutput final
*/
uint8_t *silence;
/**
* For PrepareAlsaPcmSockets().
*/
ReusableArray<pollfd> pfd_buffer;
AlsaNonBlockPcm non_block;
/**
* For copying data from OutputThread to IOThread.
@@ -881,7 +878,7 @@ AlsaOutput::PrepareSockets() noexcept
}
try {
return PrepareAlsaPcmSockets(*this, pcm, pfd_buffer);
return non_block.PrepareSockets(*this, pcm);
} catch (...) {
ClearSocketList();
LockCaughtError();
@@ -892,6 +889,8 @@ AlsaOutput::PrepareSockets() noexcept
void
AlsaOutput::DispatchSockets() noexcept
try {
non_block.DispatchSockets(*this, pcm);
{
const std::lock_guard<Mutex> lock(mutex);

@@ -98,8 +98,11 @@ class HttpdClient final
/**
* If the current metadata was already sent to the client.
*
* Initialized to `true` because there is no metadata #Page
* pending to be sent.
*/
bool metadata_sent = false;
bool metadata_sent = true;
/**
* The amount of streaming data between each metadata block

@@ -23,7 +23,8 @@
* between the decoder thread and the output thread(s): it receives
* #MusicChunk objects from the decoder, optionally mixes them
* (cross-fading), applies software volume, and sends them to the
* audio outputs via audio_output_all_play().
* audio outputs via PlayerOutputs::Play()
* (i.e. MultipleOutputs::Play()).
*
* It is controlled by the main thread (the playlist code), see
* Control.hxx. The playlist enqueues new songs into the player
@@ -970,6 +971,15 @@ Player::Run() noexcept
pc.CommandFinished();
while (ProcessCommand()) {
if (decoder_starting) {
/* wait until the decoder is initialized completely */
if (!CheckDecoderStartup())
break;
continue;
}
if (buffering) {
/* buffering at the start of the song - wait
until the buffer is large enough, to
@@ -987,15 +997,6 @@ Player::Run() noexcept
}
}
if (decoder_starting) {
/* wait until the decoder is initialized completely */
if (!CheckDecoderStartup())
break;
continue;
}
if (dc.IsIdle() && queued && dc.pipe == pipe) {
/* the decoder has finished the current song;
make it decode the next song */

@@ -93,9 +93,8 @@ SongFilter::SongFilter(TagType tag, const char *value, bool fold_case)
{
/* for compatibility with MPD 0.20 and older, "fold_case" also
switches on "substring" */
and_filter.AddItem(std::make_unique<TagSongFilter>(tag, value,
fold_case, fold_case,
false));
and_filter.AddItem(std::make_unique<TagSongFilter>(tag,
StringFilter(value, fold_case, fold_case, false)));
}
SongFilter::~SongFilter()
@@ -198,6 +197,56 @@ ExpectQuoted(const char *&s)
return {buffer, length};
}
/**
* Parse a string operator and its second operand and convert it to a
* #StringFilter.
*
* Throws on error.
*/
static StringFilter
ParseStringFilter(const char *&s, bool fold_case)
{
if (auto after_contains = StringAfterPrefixIgnoreCase(s, "contains ")) {
s = StripLeft(after_contains);
auto value = ExpectQuoted(s);
return StringFilter(std::move(value),
fold_case, true, false);
}
if (auto after_not_contains = StringAfterPrefixIgnoreCase(s, "!contains ")) {
s = StripLeft(after_not_contains);
auto value = ExpectQuoted(s);
return StringFilter(std::move(value),
fold_case, true, true);
}
bool negated = false;
#ifdef HAVE_PCRE
if ((s[0] == '!' || s[0] == '=') && s[1] == '~') {
negated = s[0] == '!';
s = StripLeft(s + 2);
auto value = ExpectQuoted(s);
StringFilter f(std::move(value), fold_case, false, negated);
f.SetRegex(std::make_shared<UniqueRegex>(f.GetValue().c_str(),
false, false,
fold_case));
return f;
}
#endif
if (s[0] == '!' && s[1] == '=')
negated = true;
else if (s[0] != '=' || s[1] != '=')
throw std::runtime_error("'==' or '!=' expected");
s = StripLeft(s + 2);
auto value = ExpectQuoted(s);
return StringFilter(std::move(value),
fold_case, false, negated);
}
ISongFilterPtr
SongFilter::ParseExpression(const char *&s, bool fold_case)
{
@@ -280,14 +329,7 @@ SongFilter::ParseExpression(const char *&s, bool fold_case)
return std::make_unique<AudioFormatSongFilter>(value);
} else {
bool negated = false;
if (s[0] == '!' && s[1] == '=')
negated = true;
else if (s[0] != '=' || s[1] != '=')
throw std::runtime_error("'==' or '!=' expected");
s = StripLeft(s + 2);
auto value = ExpectQuoted(s);
auto string_filter = ParseStringFilter(s, fold_case);
if (*s != ')')
throw std::runtime_error("')' expected");
@@ -297,15 +339,10 @@ SongFilter::ParseExpression(const char *&s, bool fold_case)
type = TAG_NUM_OF_ITEM_TYPES;
if (type == LOCATE_TAG_FILE_TYPE)
return std::make_unique<UriSongFilter>(std::move(value),
fold_case,
false,
negated);
return std::make_unique<UriSongFilter>(std::move(string_filter));
return std::make_unique<TagSongFilter>(TagType(type),
std::move(value),
fold_case, false,
negated);
std::move(string_filter));
}
}
@@ -332,10 +369,10 @@ SongFilter::Parse(const char *tag_string, const char *value, bool fold_case)
case LOCATE_TAG_FILE_TYPE:
/* for compatibility with MPD 0.20 and older,
"fold_case" also switches on "substring" */
and_filter.AddItem(std::make_unique<UriSongFilter>(value,
fold_case,
fold_case,
false));
and_filter.AddItem(std::make_unique<UriSongFilter>(StringFilter(value,
fold_case,
fold_case,
false)));
break;
default:
@@ -345,10 +382,10 @@ SongFilter::Parse(const char *tag_string, const char *value, bool fold_case)
/* for compatibility with MPD 0.20 and older,
"fold_case" also switches on "substring" */
and_filter.AddItem(std::make_unique<TagSongFilter>(TagType(tag),
value,
fold_case,
fold_case,
false));
StringFilter(value,
fold_case,
fold_case,
false)));
break;
}
}

@@ -58,11 +58,11 @@ OptimizeSongFilter(ISongFilterPtr f) noexcept
/* #TagSongFilter has its own "negated" flag,
so we can drop the #NotSongFilter
container */
tf->negated = !tf->negated;
tf->ToggleNegated();
return child;
} else if (auto *uf = dynamic_cast<UriSongFilter *>(child.get())) {
/* same for #UriSongFilter */
uf->negated = !uf->negated;
uf->ToggleNegated();
return child;
}

@@ -23,14 +23,19 @@
#include <assert.h>
bool
StringFilter::Match(const char *s) const noexcept
inline bool
StringFilter::MatchWithoutNegation(const char *s) const noexcept
{
#if !CLANG_CHECK_VERSION(3,6)
/* disabled on clang due to -Wtautological-pointer-compare */
assert(s != nullptr);
#endif
#ifdef HAVE_PCRE
if (regex)
return regex->Match(s);
#endif
if (fold_case) {
return substring
? fold_case.IsIn(s)
@@ -41,3 +46,9 @@ StringFilter::Match(const char *s) const noexcept
: value == s;
}
}
bool
StringFilter::Match(const char *s) const noexcept
{
return MatchWithoutNegation(s) != negated;
}

@@ -23,7 +23,12 @@
#include "lib/icu/Compare.hxx"
#include "util/Compiler.h"
#ifdef HAVE_PCRE
#include "lib/pcre/UniqueRegex.hxx"
#endif
#include <string>
#include <memory>
class StringFilter {
std::string value;
@@ -33,24 +38,45 @@ class StringFilter {
*/
IcuCompare fold_case;
#ifdef HAVE_PCRE
std::shared_ptr<UniqueRegex> regex;
#endif
/**
* Search for substrings instead of matching the whole string?
*/
bool substring;
bool negated;
public:
template<typename V>
StringFilter(V &&_value, bool _fold_case, bool _substring)
StringFilter(V &&_value, bool _fold_case, bool _substring, bool _negated)
:value(std::forward<V>(_value)),
fold_case(_fold_case
? IcuCompare(value.c_str())
: IcuCompare()),
substring(_substring) {}
substring(_substring), negated(_negated) {}
bool empty() const noexcept {
return value.empty();
}
bool IsRegex() const noexcept {
#ifdef HAVE_PCRE
return !!regex;
#else
return false;
#endif
}
#ifdef HAVE_PCRE
template<typename R>
void SetRegex(R &&_regex) noexcept {
regex = std::forward<R>(_regex);
}
#endif
const auto &GetValue() const noexcept {
return value;
}
@@ -59,8 +85,28 @@ public:
return fold_case;
}
bool IsNegated() const noexcept {
return negated;
}
void ToggleNegated() noexcept {
negated = !negated;
}
const char *GetOperator() const noexcept {
return IsRegex()
? (negated ? "!~" : "=~")
: (substring
? (negated ? "!contains" : "contains")
: (negated ? "!=" : "=="));
}
gcc_pure
bool Match(const char *s) const noexcept;
private:
gcc_pure
bool MatchWithoutNegation(const char *s) const noexcept;
};
#endif

@@ -31,7 +31,8 @@ TagSongFilter::ToExpression() const noexcept
? "any"
: tag_item_names[type];
return std::string("(") + name + " " + (negated ? "!=" : "==") + " \"" + EscapeFilterString(filter.GetValue()) + "\")";
return std::string("(") + name + " " + filter.GetOperator()
+ " \"" + EscapeFilterString(filter.GetValue()) + "\")";
}
bool
@@ -89,5 +90,5 @@ TagSongFilter::MatchNN(const Tag &tag) const noexcept
bool
TagSongFilter::Match(const LightSong &song) const noexcept
{
return MatchNN(song.tag) != negated;
return MatchNN(song.tag);
}

@@ -34,18 +34,11 @@ struct LightSong;
class TagSongFilter final : public ISongFilter {
TagType type;
bool negated;
StringFilter filter;
friend ISongFilterPtr OptimizeSongFilter(ISongFilterPtr) noexcept;
public:
template<typename V>
TagSongFilter(TagType _type, V &&_value, bool fold_case, bool substring,
bool _negated)
:type(_type), negated(_negated),
filter(std::forward<V>(_value), fold_case, substring) {}
TagSongFilter(TagType _type, StringFilter &&_filter) noexcept
:type(_type), filter(std::move(_filter)) {}
TagType GetTagType() const {
return type;
@@ -60,7 +53,11 @@ public:
}
bool IsNegated() const noexcept {
return negated;
return filter.IsNegated();
}
void ToggleNegated() noexcept {
filter.ToggleNegated();
}
ISongFilterPtr Clone() const noexcept override {

@@ -25,11 +25,12 @@
std::string
UriSongFilter::ToExpression() const noexcept
{
return std::string("(file ") + (negated ? "!=" : "==") + " \"" + EscapeFilterString(filter.GetValue()) + "\")";
return std::string("(file ") + filter.GetOperator()
+ " \"" + EscapeFilterString(filter.GetValue()) + "\")";
}
bool
UriSongFilter::Match(const LightSong &song) const noexcept
{
return filter.Match(song.GetURI().c_str()) != negated;
return filter.Match(song.GetURI().c_str());
}

@@ -26,16 +26,9 @@
class UriSongFilter final : public ISongFilter {
StringFilter filter;
bool negated;
friend ISongFilterPtr OptimizeSongFilter(ISongFilterPtr) noexcept;
public:
template<typename V>
UriSongFilter(V &&_value, bool fold_case, bool substring,
bool _negated)
:filter(std::forward<V>(_value), fold_case, substring),
negated(_negated) {}
UriSongFilter(StringFilter &&_filter) noexcept
:filter(std::move(_filter)) {}
const auto &GetValue() const noexcept {
return filter.GetValue();
@@ -46,7 +39,11 @@ public:
}
bool IsNegated() const noexcept {
return negated;
return filter.IsNegated();
}
void ToggleNegated() noexcept {
filter.ToggleNegated();
}
ISongFilterPtr Clone() const noexcept override {

@@ -19,6 +19,7 @@ song_dep = declare_dependency(
link_with: song,
dependencies: [
icu_dep,
pcre_dep,
tag_dep,
util_dep,
],