39 #include <boost/asio.hpp> 49 #include <pulse/pulseaudio.h> 59 Private(
const ServiceImplementation::Configuration& configuration)
60 : configuration(configuration),
61 resume_key(
std::numeric_limits<
std::uint32_t>::max()),
64 display_state_lock(power_state_controller->display_state_lock()),
100 const bool resume_play_after_phonecall =
false;
105 case media::power::Level::low:
106 case media::power::Level::very_low:
109 pause_all_multimedia_sessions(resume_play_after_phonecall);
116 d->battery_observer->is_warning_active().changed().connect([
this](
bool active)
121 resume_multimedia_session();
124 d->audio_output_observer->external_output_state().changed().connect([
this](
audio::OutputState state)
126 const bool resume_play_after_phonecall =
false;
129 case audio::OutputState::Earpiece:
130 MH_INFO(
"AudioOutputObserver reports that output is now Headphones/Headset.");
132 case audio::OutputState::Speaker:
133 MH_INFO(
"AudioOutputObserver reports that output is now Speaker.");
136 pause_all_multimedia_sessions(resume_play_after_phonecall);
138 case audio::OutputState::External:
139 MH_INFO(
"AudioOutputObserver reports that output is now External.");
142 d->audio_output_state = state;
145 d->call_monitor->on_call_state_changed().connect([
this](media::telephony::CallMonitor::State state)
147 const bool resume_play_after_phonecall =
true;
149 case media::telephony::CallMonitor::State::OffHook:
150 MH_INFO(
"Got call started signal, pausing all multimedia sessions");
153 pause_all_multimedia_sessions(resume_play_after_phonecall);
155 case media::telephony::CallMonitor::State::OnHook:
156 MH_INFO(
"Got call ended signal, resuming paused multimedia sessions");
157 resume_paused_multimedia_sessions(
false);
162 d->recorder_observer->recording_state().changed().connect([
this](
RecordingState state)
164 if (state == media::RecordingState::started)
166 d->display_state_lock->request_acquire(media::power::DisplayState::on);
169 const bool resume_play_after_phonecall =
false;
170 pause_all_multimedia_sessions(resume_play_after_phonecall);
174 d->display_state_lock->request_release(media::power::DisplayState::on);
184 const media::Player::Configuration& conf)
187 auto player = std::make_shared<media::PlayerImplementation<media::PlayerSkeleton>>
191 media::PlayerSkeleton::Configuration
197 d->request_context_resolver,
198 d->request_authenticator
201 d->client_death_observer,
202 d->power_state_controller
207 player->on_client_disconnected().connect([
this, key]()
214 d->configuration.external_services.io_service.post([
this, key]()
216 if (!d->configuration.player_store->has_player_for_key(key))
220 if (d->configuration.player_store->player_for_key(key)->lifetime() == Player::Lifetime::normal)
221 d->configuration.player_store->remove_player_for_key(key);
223 catch (
const std::out_of_range &e) {
224 MH_WARNING(
"Failed to look up Player instance for key %d" 225 ", no valid Player instance for that key value. Removal of Player from Player store" 226 " might not have completed. This most likely means that media-hub-server has" 227 " crashed and restarted.", key);
244 return std::shared_ptr<media::Player>();
255 return std::shared_ptr<media::Player>();
261 return std::shared_ptr<media::Player>();
268 if (not d->configuration.player_store->has_player_for_key(key))
270 MH_WARNING(
"Could not find Player by key: %d", key);
274 const std::shared_ptr<media::Player> current_player =
275 d->configuration.player_store->player_for_key(key);
277 d->configuration.player_store->enumerate_players([current_player, key]
279 const std::shared_ptr<media::Player>& other_player)
291 MH_INFO(
"Pausing Player with key: %d", other_key);
292 other_player->pause();
297 void media::ServiceImplementation::pause_all_multimedia_sessions(
bool resume_play_after_phonecall)
299 d->configuration.player_store->enumerate_players([
this, resume_play_after_phonecall](
const media::Player::PlayerKey& key,
const std::shared_ptr<media::Player>& player)
304 auto paused_player_pair = std::make_pair(key, resume_play_after_phonecall);
305 d->paused_sessions.push_back(paused_player_pair);
306 MH_INFO(
"Pausing Player with key: %d, resuming after phone call? %s", key,
307 (resume_play_after_phonecall ?
"yes" :
"no"));
313 void media::ServiceImplementation::resume_paused_multimedia_sessions(
bool resume_video_sessions)
315 std::for_each(d->paused_sessions.begin(), d->paused_sessions.end(),
316 [
this, resume_video_sessions](
const std::pair<media::Player::PlayerKey, bool> &paused_player_pair) {
318 const bool resume_play_after_phonecall = paused_player_pair.second;
319 std::shared_ptr<media::Player> player;
321 player = d->configuration.player_store->player_for_key(key);
323 catch (
const std::out_of_range &e) {
324 MH_WARNING(
"Failed to look up Player instance for key %d" 325 ", no valid Player instance for that key value and cannot automatically resume" 326 " paused players. This most likely means that media-hub-server has crashed and" 331 if ((resume_video_sessions || player->is_audio_source()) && resume_play_after_phonecall)
334 MH_INFO(
"Not auto-resuming video player session or other type of player session.");
337 d->paused_sessions.clear();
340 void media::ServiceImplementation::resume_multimedia_session()
342 if (not d->configuration.player_store->has_player_for_key(d->resume_key))
345 std::shared_ptr<media::Player> player;
347 player = d->configuration.player_store->player_for_key(d->resume_key);
349 catch (
const std::out_of_range &e) {
350 MH_WARNING(
"Failed to look up Player instance for key %d" 351 ", no valid Player instance for that key value and cannot automatically resume" 352 " paused Player. This most likely means that media-hub-server has crashed and" 353 " restarted.", d->resume_key);
359 MH_INFO(
"Resuming playback of Player with key: %d", d->resume_key);
361 d->resume_key = std::numeric_limits<std::uint32_t>::max();
367 throw std::runtime_error(
"This signal is only accessible from the ServiceStub");
368 static const core::Signal<void> s;
374 throw std::runtime_error(
"This signal is only accessible from the ServiceStub");
375 static const core::Signal<void> s;