MediaList.hpp 6.96 KB
Newer Older
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
1
/*****************************************************************************
2
 * MediaList.hpp: MediaList API
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
3
 *****************************************************************************
4
 * Copyright © 2015 libvlcpp authors & VideoLAN
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
5
 *
Alexey Sokolov's avatar
Alexey Sokolov committed
6
 * Authors: Alexey Sokolov <alexey+vlc@asokolov.org>
7
 *          Hugo Beauzée-Luyssen <hugo@beauzee.fr>
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation; either version 2.1 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 Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser 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 LIBVLC_CXX_MEDIALIST_H
#define LIBVLC_CXX_MEDIALIST_H

27
#include "common.hpp"
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
28

29 30
#include <mutex>

31
namespace VLC
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
32 33
{

34
class Media;
35
class MediaListEventManager;
36 37
class MediaDiscoverer;
class MediaLibrary;
38

39
class MediaList : public Internal<libvlc_media_list_t>
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
40 41
{
public:
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
42 43 44
    ///
    /// @brief A convenience RAII type to handle MediaList's lock
    ///
45 46
    using Lock = std::lock_guard<MediaList>;

Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
47 48 49 50 51
    /**
     * Check if 2 MediaList objects contain the same libvlc_media_list_t.
     * \param another another MediaList
     * \return true if they contain the same libvlc_media_list_t
     */
52 53 54 55
    bool operator==(const MediaList& another) const
    {
        return m_obj == another.m_obj;
    }
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
56 57

    /**
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
58
     * Get subitems of media descriptor object.
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
59
     */
60 61
    MediaList(Media& md)
        : Internal{ libvlc_media_subitems( getInternalPtr<libvlc_media_t>( md ) ), libvlc_media_list_release }
62 63
    {
    }
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
64 65 66 67 68 69

    /**
     * Get media service discover media list.
     *
     * \param p_mdis  media service discover object
     */
70 71 72
    MediaList(MediaDiscoverer& mdis)
        : Internal{ libvlc_media_discoverer_media_list( getInternalPtr<libvlc_media_discoverer_t>( mdis ) ),
                    libvlc_media_list_release }
73 74
    {
    }
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
75 76 77 78 79 80

    /**
     * Get media library subitems.
     *
     * \param p_mlib  media library object
     */
81 82
    MediaList(MediaLibrary& mlib )
        : Internal{ libvlc_media_library_media_list( getInternalPtr<libvlc_media_library_t>( mlib ) ), libvlc_media_list_release }
83 84 85
    {
    }

Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
86 87 88 89 90 91

    /**
     * Create an empty media list.
     *
     * \param p_instance  libvlc instance
     */
92 93 94
    MediaList(Instance& instance)
        : Internal{ libvlc_media_list_new( getInternalPtr<libvlc_instance_t>( instance ) ),
                                           libvlc_media_list_release }
95 96
    {
    }
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
97

98 99 100 101 102
    MediaList( Internal::InternalPtr mediaList )
        : Internal{ mediaList, libvlc_media_list_release }
    {
    }

103 104 105 106 107 108 109
    /**
     * Create an empty VLC MediaList instance.
     *
     * Calling any method on such an instance is undefined.
    */
    MediaList() = default;

Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
110 111 112
    /**
     * Associate media instance with this media list instance. If another
     * media instance was present it will be released. The
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
113
     * MediaList lock should NOT be held upon entering this function.
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
114 115 116
     *
     * \param p_md  media instance to add
     */
117
    void setMedia(Media& md)
118
    {
119
        libvlc_media_list_set_media( *this, getInternalPtr<libvlc_media_t>( md ) );
120
    }
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
121 122

    /**
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
123
     * Add media instance to media list The MediaList lock should be
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
124 125 126 127
     * held upon entering this function.
     *
     * \param p_md  a media instance
     */
128
    bool addMedia(Media& md)
129
    {
130
        return libvlc_media_list_add_media( *this, getInternalPtr<libvlc_media_t>( md ) ) == 0;
131
    }
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
132 133 134

    /**
     * Insert media instance in media list on a position The
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
135
     * MediaList lock should be held upon entering this function.
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
136 137 138 139 140
     *
     * \param p_md  a media instance
     *
     * \param i_pos  position in array where to insert
     */
141
    bool insertMedia(Media& md, int pos)
142
    {
143
        return libvlc_media_list_insert_media( *this, getInternalPtr<libvlc_media_t>( md ), pos ) == 0;
144
    }
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
145 146 147

    /**
     * Remove media instance from media list on a position The
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
148
     * MediaList lock should be held upon entering this function.
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
149 150 151
     *
     * \param i_pos  position in array where to insert
     */
152
    bool removeIndex(int i_pos)
153
    {
154
        return libvlc_media_list_remove_index( *this, i_pos ) == 0;
155
    }
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
156 157

    /**
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
158
     * Get count on media list items The MediaList lock should be
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
159 160 161 162
     * held upon entering this function.
     *
     * \return number of items in media list
     */
163 164
    int count()
    {
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
165
        return libvlc_media_list_count( *this );
166
    }
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
167 168 169

    /**
     * List media instance in media list at a position The
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
170
     * MediaList lock should be held upon entering this function.
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
171 172 173
     *
     * \param i_pos  position in array where to insert
     *
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
174
     * \return media instance at position i_pos, or nullptr if not found.
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
175
     */
176
    MediaPtr itemAtIndex(int i_pos)
177
    {
178
        auto ptr = libvlc_media_list_item_at_index(*this,i_pos);
179 180
        if ( ptr == nullptr )
            return nullptr;
181
        return std::make_shared<Media>( ptr, false );
182
    }
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
183 184 185 186

    /**
     * Find index position of List media instance in media list. Warning: the
     * function will return the first matched position. The
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
187
     * MediaList lock should be held upon entering this function.
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
188 189 190 191 192
     *
     * \param p_md  media instance
     *
     * \return position of media instance or -1 if media not found
     */
193
    int indexOfItem(Media& md)
194
    {
195
        return libvlc_media_list_index_of_item( *this, getInternalPtr<libvlc_media_t>( md ) );
196
    }
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
197 198 199 200 201

    /**
     * This indicates if this media list is read-only from a user point of
     * view
     *
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
202
     * \return true if readonly, false otherwise
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
203
     */
204 205
    bool isReadonly()
    {
206
        return libvlc_media_list_is_readonly(*this) == 1;
207
    }
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
208 209 210 211

    /**
     * Get lock on media list items
     */
212 213
    void lock()
    {
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
214
        libvlc_media_list_lock( *this );
215
    }
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
216 217

    /**
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
218
     * Release lock on media list items The MediaList lock should be
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
219 220
     * held upon entering this function.
     */
221 222
    void unlock()
    {
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
223
        libvlc_media_list_unlock( *this );
224
    }
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
225 226 227 228 229 230 231

    /**
     * Get libvlc_event_manager from this media list instance. The
     * p_event_manager is immutable, so you don't have to hold the lock
     *
     * \return libvlc_event_manager
     */
232
    MediaListEventManager& eventManager()
233
    {
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
234
        if ( m_eventManager == nullptr )
235
        {
236
            libvlc_event_manager_t* obj = libvlc_media_list_event_manager( *this );
237
            m_eventManager = std::make_shared<MediaListEventManager>( obj );
238
        }
239
        return *m_eventManager;
240
    }
241 242

private:
243
    std::shared_ptr<MediaListEventManager> m_eventManager;
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
244 245 246 247 248 249
};

} // namespace VLC

#endif