Song.hxx 3.79 KB
Newer Older
1
/*
2
 * Copyright 2003-2018 The Music Player Daemon Project
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
 * http://www.musicpd.org
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */

#ifndef MPD_SONG_HXX
#define MPD_SONG_HXX

23
#include "Chrono.hxx"
24
#include "tag/Tag.hxx"
25
#include "AudioFormat.hxx"
26
#include "util/Compiler.h"
27
#include "config.h"
28

29 30
#include <boost/intrusive/list.hpp>

31 32
#include <string>

33
#include <time.h>
34

35
struct LightSong;
36 37
struct Directory;
class DetachedSong;
38
class Storage;
39
class ArchiveFile;
Max Kellermann's avatar
Max Kellermann committed
40

41
/**
42 43
 * A song file inside the configured music directory.  Internal
 * #SimpleDatabase class.
44 45
 */
struct Song {
46 47 48 49 50 51 52 53 54 55
	static constexpr auto link_mode = boost::intrusive::normal_link;
	typedef boost::intrusive::link_mode<link_mode> LinkMode;
	typedef boost::intrusive::list_member_hook<LinkMode> Hook;

	struct Disposer {
		void operator()(Song *song) const {
			song->Free();
		}
	};

56 57 58 59 60 61 62 63
	/**
	 * Pointers to the siblings of this directory within the
	 * parent directory.  It is unused (undefined) if this song is
	 * not in the database.
	 *
	 * This attribute is protected with the global #db_mutex.
	 * Read access in the update thread does not need protection.
	 */
64
	Hook siblings;
65

66
	Tag tag;
67 68

	/**
69
	 * The #Directory that contains this song.  Must be
70
	 * non-nullptr.
71
	 */
72
	Directory *const parent;
73

74 75 76 77
	/**
	 * The time stamp of the last file modification.  A negative
	 * value means that this is unknown/unavailable.
	 */
78 79
	std::chrono::system_clock::time_point mtime =
		std::chrono::system_clock::time_point::min();
80 81

	/**
82
	 * Start of this sub-song within the file.
83
	 */
84
	SongTime start_time = SongTime::zero();
85 86

	/**
87
	 * End of this sub-song within the file.
88 89
	 * Unused if zero.
	 */
90
	SongTime end_time = SongTime::zero();
91

92 93 94 95 96 97
	/**
	 * The audio format of the song, if given by the decoder
	 * plugin.  May be undefined if unknown.
	 */
	AudioFormat audio_format = AudioFormat::Undefined();

98
	/**
99
	 * The file name.
100
	 */
101 102
	char uri[sizeof(int)];

103
	Song(const char *_uri, size_t uri_length, Directory &parent);
104 105
	~Song();

106
	gcc_malloc gcc_returns_nonnull
107
	static Song *NewFrom(DetachedSong &&other, Directory &parent);
108 109

	/** allocate a new song with a local file name */
110
	gcc_malloc gcc_returns_nonnull
111
	static Song *NewFile(const char *path_utf8, Directory &parent);
112 113 114 115 116 117 118

	/**
	 * allocate a new song structure with a local file name and attempt to
	 * load its metadata.  If all decoder plugin fail to read its meta
	 * data, nullptr is returned.
	 */
	gcc_malloc
119
	static Song *LoadFile(Storage &storage, const char *name_utf8,
120
			      Directory &parent) noexcept;
121

122 123
	void Free();

124
	bool UpdateFile(Storage &storage) noexcept;
125 126

#ifdef ENABLE_ARCHIVE
127 128
	static Song *LoadFromArchive(ArchiveFile &archive,
				     const char *name_utf8,
129 130
				     Directory &parent) noexcept;
	bool UpdateFileInArchive(ArchiveFile &archive) noexcept;
131
#endif
132 133 134 135 136

	/**
	 * Returns the URI of the song in UTF-8 encoding, including its
	 * location within the music directory.
	 */
137
	gcc_pure
138
	std::string GetURI() const noexcept;
139 140

	gcc_pure
141
	LightSong Export() const noexcept;
142 143
};

144 145 146 147 148
typedef boost::intrusive::list<Song,
			       boost::intrusive::member_hook<Song, Song::Hook,
							     &Song::siblings>,
			       boost::intrusive::constant_time_size<false>> SongList;

149
#endif