blob: c7a85326fbb49031d1c5ef1d82353dc530b829f6 (
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
|
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
* You can obtain one at http://mozilla.org/MPL/2.0/. */
#import <MediaPlayer/MediaPlayer.h>
#include "MediaHardwareKeysEventSourceMacMediaCenter.h"
#include "mozilla/dom/MediaControlUtils.h"
#include "nsCocoaUtils.h"
using namespace mozilla::dom;
// avoid redefined macro in unified build
#undef LOG
#define LOG(msg, ...) \
MOZ_LOG(gMediaControlLog, LogLevel::Debug, \
("MediaHardwareKeysEventSourceMacMediaCenter=%p, " msg, this, \
##__VA_ARGS__))
namespace mozilla {
namespace widget {
MediaCenterEventHandler
MediaHardwareKeysEventSourceMacMediaCenter::CreatePlayPauseHandler() {
return Block_copy(^MPRemoteCommandHandlerStatus(MPRemoteCommandEvent* event) {
MPNowPlayingInfoCenter* center = [MPNowPlayingInfoCenter defaultCenter];
center.playbackState =
center.playbackState == MPNowPlayingPlaybackStatePlaying
? MPNowPlayingPlaybackStatePaused
: MPNowPlayingPlaybackStatePlaying;
HandleEvent(MediaControlKey::Playpause);
return MPRemoteCommandHandlerStatusSuccess;
});
}
MediaCenterEventHandler
MediaHardwareKeysEventSourceMacMediaCenter::CreateNextTrackHandler() {
return Block_copy(^MPRemoteCommandHandlerStatus(MPRemoteCommandEvent* event) {
HandleEvent(MediaControlKey::Nexttrack);
return MPRemoteCommandHandlerStatusSuccess;
});
}
MediaCenterEventHandler
MediaHardwareKeysEventSourceMacMediaCenter::CreatePreviousTrackHandler() {
return Block_copy(^MPRemoteCommandHandlerStatus(MPRemoteCommandEvent* event) {
HandleEvent(MediaControlKey::Previoustrack);
return MPRemoteCommandHandlerStatusSuccess;
});
}
MediaCenterEventHandler
MediaHardwareKeysEventSourceMacMediaCenter::CreatePlayHandler() {
return Block_copy(^MPRemoteCommandHandlerStatus(MPRemoteCommandEvent* event) {
MPNowPlayingInfoCenter* center = [MPNowPlayingInfoCenter defaultCenter];
if (center.playbackState != MPNowPlayingPlaybackStatePlaying) {
center.playbackState = MPNowPlayingPlaybackStatePlaying;
}
HandleEvent(MediaControlKey::Play);
return MPRemoteCommandHandlerStatusSuccess;
});
}
MediaCenterEventHandler
MediaHardwareKeysEventSourceMacMediaCenter::CreatePauseHandler() {
return Block_copy(^MPRemoteCommandHandlerStatus(MPRemoteCommandEvent* event) {
MPNowPlayingInfoCenter* center = [MPNowPlayingInfoCenter defaultCenter];
if (center.playbackState != MPNowPlayingPlaybackStatePaused) {
center.playbackState = MPNowPlayingPlaybackStatePaused;
}
HandleEvent(MediaControlKey::Pause);
return MPRemoteCommandHandlerStatusSuccess;
});
}
MediaHardwareKeysEventSourceMacMediaCenter::
MediaHardwareKeysEventSourceMacMediaCenter() {
mPlayPauseHandler = CreatePlayPauseHandler();
mNextTrackHandler = CreateNextTrackHandler();
mPreviousTrackHandler = CreatePreviousTrackHandler();
mPlayHandler = CreatePlayHandler();
mPauseHandler = CreatePauseHandler();
LOG("Create MediaHardwareKeysEventSourceMacMediaCenter");
}
MediaHardwareKeysEventSourceMacMediaCenter::
~MediaHardwareKeysEventSourceMacMediaCenter() {
LOG("Destroy MediaHardwareKeysEventSourceMacMediaCenter");
EndListeningForEvents();
MPNowPlayingInfoCenter* center = [MPNowPlayingInfoCenter defaultCenter];
center.playbackState = MPNowPlayingPlaybackStateStopped;
}
void MediaHardwareKeysEventSourceMacMediaCenter::BeginListeningForEvents() {
MPNowPlayingInfoCenter* center = [MPNowPlayingInfoCenter defaultCenter];
center.playbackState = MPNowPlayingPlaybackStatePlaying;
MPRemoteCommandCenter* commandCenter =
[MPRemoteCommandCenter sharedCommandCenter];
commandCenter.togglePlayPauseCommand.enabled = true;
[commandCenter.togglePlayPauseCommand addTargetWithHandler:mPlayPauseHandler];
commandCenter.nextTrackCommand.enabled = true;
[commandCenter.nextTrackCommand addTargetWithHandler:mNextTrackHandler];
commandCenter.previousTrackCommand.enabled = true;
[commandCenter.previousTrackCommand
addTargetWithHandler:mPreviousTrackHandler];
commandCenter.playCommand.enabled = true;
[commandCenter.playCommand addTargetWithHandler:mPlayHandler];
commandCenter.pauseCommand.enabled = true;
[commandCenter.pauseCommand addTargetWithHandler:mPauseHandler];
}
void MediaHardwareKeysEventSourceMacMediaCenter::EndListeningForEvents() {
MPNowPlayingInfoCenter* center = [MPNowPlayingInfoCenter defaultCenter];
center.playbackState = MPNowPlayingPlaybackStatePaused;
center.nowPlayingInfo = nil;
MPRemoteCommandCenter* commandCenter =
[MPRemoteCommandCenter sharedCommandCenter];
commandCenter.togglePlayPauseCommand.enabled = false;
[commandCenter.togglePlayPauseCommand removeTarget:nil];
commandCenter.nextTrackCommand.enabled = false;
[commandCenter.nextTrackCommand removeTarget:nil];
commandCenter.previousTrackCommand.enabled = false;
[commandCenter.previousTrackCommand removeTarget:nil];
commandCenter.playCommand.enabled = false;
[commandCenter.playCommand removeTarget:nil];
commandCenter.pauseCommand.enabled = false;
[commandCenter.pauseCommand removeTarget:nil];
}
bool MediaHardwareKeysEventSourceMacMediaCenter::Open() {
LOG("Open MediaHardwareKeysEventSourceMacMediaCenter");
mOpened = true;
BeginListeningForEvents();
return true;
}
void MediaHardwareKeysEventSourceMacMediaCenter::Close() {
LOG("Close MediaHardwareKeysEventSourceMacMediaCenter");
SetPlaybackState(dom::MediaSessionPlaybackState::None);
EndListeningForEvents();
mOpened = false;
MediaControlKeySource::Close();
}
bool MediaHardwareKeysEventSourceMacMediaCenter::IsOpened() const {
return mOpened;
}
void MediaHardwareKeysEventSourceMacMediaCenter::HandleEvent(
MediaControlKey aEvent) {
for (auto iter = mListeners.begin(); iter != mListeners.end(); ++iter) {
(*iter)->OnActionPerformed(MediaControlAction(aEvent));
}
}
void MediaHardwareKeysEventSourceMacMediaCenter::SetPlaybackState(
MediaSessionPlaybackState aState) {
MPNowPlayingInfoCenter* center = [MPNowPlayingInfoCenter defaultCenter];
if (aState == MediaSessionPlaybackState::Playing) {
center.playbackState = MPNowPlayingPlaybackStatePlaying;
} else if (aState == MediaSessionPlaybackState::Paused) {
center.playbackState = MPNowPlayingPlaybackStatePaused;
} else if (aState == MediaSessionPlaybackState::None) {
center.playbackState = MPNowPlayingPlaybackStateStopped;
}
MediaControlKeySource::SetPlaybackState(aState);
}
void MediaHardwareKeysEventSourceMacMediaCenter::SetMediaMetadata(
const dom::MediaMetadataBase& aMetadata) {
NSMutableDictionary* nowPlayingInfo = [NSMutableDictionary dictionary];
[nowPlayingInfo setObject:nsCocoaUtils::ToNSString(aMetadata.mTitle)
forKey:MPMediaItemPropertyTitle];
[nowPlayingInfo setObject:nsCocoaUtils::ToNSString(aMetadata.mArtist)
forKey:MPMediaItemPropertyArtist];
[nowPlayingInfo setObject:nsCocoaUtils::ToNSString(aMetadata.mAlbum)
forKey:MPMediaItemPropertyAlbumTitle];
// The procedure of updating `nowPlayingInfo` is actually an async operation
// from our testing, Apple's documentation doesn't mention that though. So be
// aware that checking `nowPlayingInfo` immedately after setting it might not
// yield the expected result.
[MPNowPlayingInfoCenter defaultCenter].nowPlayingInfo = nowPlayingInfo;
}
} // namespace widget
} // namespace mozilla
|