Music Hub  ..
A session-wide music playback service
hybris_recorder_observer.cpp
Go to the documentation of this file.
1 /*
2  * Copyright © 2014 Canonical Ltd.
3  *
4  * This program is free software: you can redistribute it and/or modify it
5  * under the terms of the GNU Lesser General Public License version 3,
6  * as published by the Free Software Foundation.
7  *
8  * This program is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11  * GNU Lesser General Public License for more details.
12  *
13  * You should have received a copy of the GNU Lesser General Public License
14  * along with this program. If not, see <http://www.gnu.org/licenses/>.
15  *
16  * Authored by: Thomas Voß <thomas.voss@canonical.com>
17  */
18 
20 
21 namespace media = core::ubuntu::media;
22 
23 #if defined(MEDIA_HUB_HAVE_HYBRIS_MEDIA_COMPAT_LAYER)
24 #include <hybris/media/media_recorder_layer.h>
25 
26 struct media::HybrisRecorderObserver::Private
27 {
28  struct Holder
29  {
30  std::weak_ptr<media::HybrisRecorderObserver::Private> wp;
31  };
32 
33  // The fringe that we hand over to hybris.
34  static void on_media_recording_state_changed(bool started, void* context)
35  {
36  if (auto holder = static_cast<Holder*>(context))
37  {
38  if (auto sp = holder->wp.lock())
39  {
40  sp->recording_state = started ? media::RecordingState::started : media::RecordingState::stopped;
41  }
42  }
43  }
44 
45  // TODO: We have no way of freeing the observer and thus leak
46  // an instance here.
47  MediaRecorderObserver* observer
48  {
49  android_media_recorder_observer_new()
50  };
51 
52  core::Property<media::RecordingState> recording_state
53  {
55  };
56 
57  Holder* holder
58  {
59  nullptr
60  };
61 };
62 
63 media::HybrisRecorderObserver::HybrisRecorderObserver() : d{new Private{}}
64 {
65  android_media_recorder_observer_set_cb(
66  d->observer,
67  &Private::on_media_recording_state_changed,
68  d->holder = new Private::Holder{d});
69 }
70 
72 {
73  // We first reset the context of the callback.
74  android_media_recorder_observer_set_cb(
75  d->observer,
76  &Private::on_media_recording_state_changed,
77  nullptr);
78 
79  delete d->holder;
80 }
81 
82 const core::Property<media::RecordingState>& media::HybrisRecorderObserver::recording_state() const
83 {
84  return d->recording_state;
85 }
86 
88 {
90 }
91 #else // MEDIA_HUB_HAVE_HYBRIS_MEDIA_COMPAT_LAYER
93 {
94  throw std::logic_error
95  {
96  "Hybris-based recorder observer implementation not supported on this platform."
97  };
98 }
99 #endif // MEDIA_HUB_HAVE_HYBRIS_MEDIA_COMPAT_LAYER
std::shared_ptr< RecorderObserver > Ptr
const core::Property< RecordingState > & recording_state() const override