1/*
2 * Copyright (C) 2015 Apple Inc. All Rights Reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 *
13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR
17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24 */
25
26#pragma once
27
28#if ENABLE(MEDIA_SESSION)
29
30#include "MediaRemoteControls.h"
31#include "MediaSessionMetadata.h"
32#include <wtf/Function.h>
33#include <wtf/HashSet.h>
34
35namespace WebCore {
36
37class Document;
38class HTMLMediaElement;
39
40class MediaSession final : public RefCounted<MediaSession> {
41public:
42 enum class Kind { Content, Transient, TransientSolo, Ambient };
43 enum class State { Idle, Active, Interrupted };
44
45 struct Metadata {
46 String title;
47 String artist;
48 String album;
49 String artwork;
50 };
51
52 static Ref<MediaSession> create(Document& document, Kind kind)
53 {
54 return adoptRef(*new MediaSession(document, kind));
55 }
56
57 ~MediaSession();
58
59 Kind kind() const { return m_kind; }
60 MediaRemoteControls* controls();
61
62 WEBCORE_EXPORT State currentState() const { return m_currentState; }
63 bool hasActiveMediaElements() const;
64
65 void setMetadata(const Optional<Metadata>&);
66
67 void deactivate();
68
69 // Runs the media session invocation algorithm and returns true on success.
70 bool invoke();
71
72 void handleDuckInterruption();
73 void handleIndefinitePauseInterruption();
74 void handlePauseInterruption();
75 void handleUnduckInterruption();
76 void handleUnpauseInterruption();
77
78 void togglePlayback();
79 void skipToNextTrack();
80 void skipToPreviousTrack();
81
82 void controlIsEnabledDidChange();
83
84private:
85 friend class HTMLMediaElement;
86
87 MediaSession(Document&, Kind);
88
89 void addMediaElement(HTMLMediaElement&);
90 void removeMediaElement(HTMLMediaElement&);
91
92 void safelyIterateActiveMediaElements(const WTF::Function<void(HTMLMediaElement*)>&);
93 void changeActiveMediaElements(const WTF::Function<void(void)>&);
94 void addActiveMediaElement(HTMLMediaElement&);
95 bool isMediaElementActive(HTMLMediaElement&);
96
97 void releaseInternal();
98
99 State m_currentState { State::Idle };
100 HashSet<HTMLMediaElement*> m_participatingElements;
101 HashSet<HTMLMediaElement*> m_activeParticipatingElements;
102 HashSet<HTMLMediaElement*>* m_iteratedActiveParticipatingElements { nullptr };
103
104 Document& m_document;
105 const Kind m_kind;
106 RefPtr<MediaRemoteControls> m_controls;
107 MediaSessionMetadata m_metadata;
108};
109
110} // namespace WebCore
111
112#endif /* ENABLE(MEDIA_SESSION) */
113