OLD | NEW |
(Empty) | |
| 1 // Copyright 2017 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "extensions/browser/api/media_perception_private/media_perception_api_m
anager.h" |
| 6 |
| 7 #include "base/lazy_instance.h" |
| 8 #include "base/memory/ptr_util.h" |
| 9 #include "chromeos/dbus/dbus_thread_manager.h" |
| 10 #include "chromeos/dbus/media_analytics_client.h" |
| 11 #include "extensions/browser/event_router.h" |
| 12 #include "extensions/browser/extension_function.h" |
| 13 |
| 14 namespace media_perception = extensions::api::media_perception_private; |
| 15 |
| 16 namespace extensions { |
| 17 |
| 18 namespace { |
| 19 |
| 20 // p_result is owned by the caller. |
| 21 void PointProtoToIdl(const mri::Point& point, |
| 22 media_perception::Point* p_result) { |
| 23 if (point.has_x()) { |
| 24 p_result->x.reset(new double(point.x())); |
| 25 } |
| 26 if (point.has_y()) { |
| 27 p_result->y.reset(new double(point.y())); |
| 28 } |
| 29 } |
| 30 |
| 31 // bbox_result is owned by the caller. |
| 32 void BoundingBoxProtoToIdl(const mri::BoundingBox& bounding_box, |
| 33 media_perception::BoundingBox* bbox_result) { |
| 34 if (bounding_box.has_normalized()) { |
| 35 bbox_result->normalized.reset(new bool(bounding_box.normalized())); |
| 36 } |
| 37 if (bounding_box.has_top_left()) { |
| 38 bbox_result->top_left.reset(new media_perception::Point()); |
| 39 PointProtoToIdl(bounding_box.top_left(), bbox_result->top_left.get()); |
| 40 } |
| 41 if (bounding_box.has_bottom_right()) { |
| 42 bbox_result->bottom_right.reset(new media_perception::Point()); |
| 43 PointProtoToIdl(bounding_box.bottom_right(), |
| 44 bbox_result->bottom_right.get()); |
| 45 } |
| 46 } |
| 47 |
| 48 media_perception::Entity EntityProtoToIdl(const mri::Entity& entity) { |
| 49 media_perception::Entity e_result; |
| 50 if (entity.has_id()) { |
| 51 e_result.id.reset(new int(entity.id())); |
| 52 } |
| 53 if (entity.has_type()) { |
| 54 switch (entity.type()) { |
| 55 case mri::Entity::FACE: |
| 56 e_result.type = media_perception::ENTITY_TYPE_FACE; |
| 57 break; |
| 58 case mri::Entity::PERSON: |
| 59 e_result.type = media_perception::ENTITY_TYPE_PERSON; |
| 60 break; |
| 61 default: |
| 62 e_result.type = media_perception::ENTITY_TYPE_UNSPECIFIED; |
| 63 } |
| 64 } |
| 65 if (entity.has_confidence()) { |
| 66 e_result.confidence.reset(new double(entity.confidence())); |
| 67 } |
| 68 if (entity.has_bounding_box()) { |
| 69 e_result.bounding_box.reset(new media_perception::BoundingBox()); |
| 70 BoundingBoxProtoToIdl(entity.bounding_box(), e_result.bounding_box.get()); |
| 71 } |
| 72 return e_result; |
| 73 } |
| 74 |
| 75 media_perception::FramePerception FramePerceptionProtoToIdl( |
| 76 const mri::FramePerception& frame_perception) { |
| 77 media_perception::FramePerception fp_result; |
| 78 if (frame_perception.has_frame_id()) { |
| 79 fp_result.frame_id.reset(new int(frame_perception.frame_id())); |
| 80 } |
| 81 if (frame_perception.has_frame_width_in_px()) { |
| 82 fp_result.frame_width_in_px.reset( |
| 83 new int(frame_perception.frame_width_in_px())); |
| 84 } |
| 85 if (frame_perception.has_frame_height_in_px()) { |
| 86 fp_result.frame_height_in_px.reset( |
| 87 new int(frame_perception.frame_height_in_px())); |
| 88 } |
| 89 if (frame_perception.has_timestamp()) { |
| 90 fp_result.timestamp.reset(new double(frame_perception.timestamp())); |
| 91 } |
| 92 if (frame_perception.entity_size() > 0) { |
| 93 fp_result.entities.reset(new std::vector<media_perception::Entity>()); |
| 94 for (const auto& entity : frame_perception.entity()) { |
| 95 fp_result.entities->emplace_back(EntityProtoToIdl(entity)); |
| 96 } |
| 97 } |
| 98 return fp_result; |
| 99 } |
| 100 |
| 101 media_perception::PerceptionSample PerceptionSampleProtoToIdl( |
| 102 const mri::PerceptionSample& perception_sample) { |
| 103 media_perception::PerceptionSample ps_result; |
| 104 if (perception_sample.has_frame_perception()) { |
| 105 ps_result.frame_perception.reset(new media_perception::FramePerception( |
| 106 FramePerceptionProtoToIdl(perception_sample.frame_perception()))); |
| 107 } |
| 108 // TODO(lasoren): Implement ImageFrameProtoToIdl. |
| 109 return ps_result; |
| 110 } |
| 111 |
| 112 } // namespace |
| 113 |
| 114 media_perception::State StateProtoToIdl(const mri::State& state) { |
| 115 media_perception::State s_result; |
| 116 if (state.has_status()) { |
| 117 switch (state.status()) { |
| 118 case mri::State::UNINITIALIZED: |
| 119 s_result.status = media_perception::STATUS_UNINITIALIZED; |
| 120 break; |
| 121 case mri::State::STARTED: |
| 122 s_result.status = media_perception::STATUS_STARTED; |
| 123 break; |
| 124 case mri::State::RUNNING: |
| 125 s_result.status = media_perception::STATUS_RUNNING; |
| 126 break; |
| 127 case mri::State::SUSPENDED: |
| 128 s_result.status = media_perception::STATUS_SUSPENDED; |
| 129 break; |
| 130 default: |
| 131 // Status unset. |
| 132 break; |
| 133 } |
| 134 } |
| 135 if (state.has_device_context()) { |
| 136 s_result.device_context.reset(new std::string(state.device_context())); |
| 137 } |
| 138 return s_result; |
| 139 } |
| 140 |
| 141 mri::State StateIdlToProto(const media_perception::State& state) { |
| 142 mri::State s_result; |
| 143 switch (state.status) { |
| 144 case media_perception::STATUS_UNINITIALIZED: |
| 145 s_result.set_status(mri::State::UNINITIALIZED); |
| 146 break; |
| 147 case media_perception::STATUS_STARTED: |
| 148 s_result.set_status(mri::State::STARTED); |
| 149 break; |
| 150 case media_perception::STATUS_RUNNING: |
| 151 s_result.set_status(mri::State::RUNNING); |
| 152 break; |
| 153 case media_perception::STATUS_SUSPENDED: |
| 154 s_result.set_status(mri::State::SUSPENDED); |
| 155 break; |
| 156 default: |
| 157 // Status unset. |
| 158 break; |
| 159 } |
| 160 if (state.device_context) { |
| 161 s_result.set_device_context(*state.device_context); |
| 162 } |
| 163 return s_result; |
| 164 } |
| 165 |
| 166 media_perception::MediaPerception MediaPerceptionProtoToIdl( |
| 167 const mri::MediaPerception& media_perception) { |
| 168 media_perception::MediaPerception mp_result; |
| 169 if (media_perception.has_timestamp()) { |
| 170 mp_result.timestamp.reset(new double(media_perception.timestamp())); |
| 171 } |
| 172 if (media_perception.frame_perception_size() > 0) { |
| 173 mp_result.frame_perceptions.reset( |
| 174 new std::vector<media_perception::FramePerception>()); |
| 175 for (const auto& frame_perception : media_perception.frame_perception()) { |
| 176 mp_result.frame_perceptions->emplace_back( |
| 177 FramePerceptionProtoToIdl(frame_perception)); |
| 178 } |
| 179 } |
| 180 return mp_result; |
| 181 } |
| 182 |
| 183 media_perception::Diagnostics DiagnosticsProtoToIdl( |
| 184 const mri::Diagnostics& diagnostics) { |
| 185 media_perception::Diagnostics d_result; |
| 186 if (diagnostics.perception_sample_size() > 0) { |
| 187 d_result.perception_samples.reset( |
| 188 new std::vector<media_perception::PerceptionSample>()); |
| 189 for (const auto& perception_sample : diagnostics.perception_sample()) { |
| 190 d_result.perception_samples->emplace_back( |
| 191 PerceptionSampleProtoToIdl(perception_sample)); |
| 192 } |
| 193 } |
| 194 return d_result; |
| 195 } |
| 196 |
| 197 // static |
| 198 MediaPerceptionAPIManager* MediaPerceptionAPIManager::Get( |
| 199 content::BrowserContext* context) { |
| 200 return GetFactoryInstance()->Get(context); |
| 201 } |
| 202 |
| 203 static base::LazyInstance< |
| 204 BrowserContextKeyedAPIFactory<MediaPerceptionAPIManager>>::DestructorAtExit |
| 205 g_factory = LAZY_INSTANCE_INITIALIZER; |
| 206 |
| 207 // static |
| 208 BrowserContextKeyedAPIFactory<MediaPerceptionAPIManager>* |
| 209 MediaPerceptionAPIManager::GetFactoryInstance() { |
| 210 return g_factory.Pointer(); |
| 211 } |
| 212 |
| 213 MediaPerceptionAPIManager::MediaPerceptionAPIManager( |
| 214 content::BrowserContext* context) |
| 215 : browser_context_(context), analytics_process_running_(false) { |
| 216 chromeos::MediaAnalyticsClient* dbus_client = |
| 217 chromeos::DBusThreadManager::Get()->GetMediaAnalyticsClient(); |
| 218 dbus_client->SetMediaPerceptionSignalHandler( |
| 219 base::Bind(&MediaPerceptionAPIManager::MediaPerceptionSignalHandler, |
| 220 base::Unretained(this))); |
| 221 } |
| 222 |
| 223 MediaPerceptionAPIManager::~MediaPerceptionAPIManager() { |
| 224 chromeos::MediaAnalyticsClient* dbus_client = |
| 225 chromeos::DBusThreadManager::Get()->GetMediaAnalyticsClient(); |
| 226 dbus_client->UnsetMediaPerceptionSignalHandler(); |
| 227 } |
| 228 |
| 229 void MediaPerceptionAPIManager::GetState(const APIStateCallback& callback) { |
| 230 chromeos::MediaAnalyticsClient* dbus_client = |
| 231 chromeos::DBusThreadManager::Get()->GetMediaAnalyticsClient(); |
| 232 dbus_client->State(mri::State(), // Passing empty state changes nothing. |
| 233 base::Bind(&MediaPerceptionAPIManager::StateCallback, |
| 234 base::Unretained(this), callback)); |
| 235 } |
| 236 |
| 237 void MediaPerceptionAPIManager::SetState(const media_perception::State& state, |
| 238 const APIStateCallback& callback) { |
| 239 mri::State state_proto = StateIdlToProto(state); |
| 240 if (state_proto.status() != mri::State::RUNNING && |
| 241 state_proto.status() != mri::State::SUSPENDED) { |
| 242 LOG(ERROR) << "Cannot set state to something other than RUNNING or " |
| 243 "SUSPENDED."; |
| 244 media_perception::State empty_state; |
| 245 callback.Run(false, std::move(empty_state)); |
| 246 return; |
| 247 } |
| 248 if (!analytics_process_running_) { |
| 249 if (state_proto.status() == mri::State::RUNNING) { |
| 250 chromeos::MediaAnalyticsClient* dbus_client = |
| 251 chromeos::DBusThreadManager::Get()->GetMediaAnalyticsClient(); |
| 252 dbus_client->StartMediaAnalytics( |
| 253 base::Bind(&MediaPerceptionAPIManager::UpstartCallback, |
| 254 base::Unretained(this), callback, state_proto)); |
| 255 } else { |
| 256 media_perception::State empty_state; |
| 257 callback.Run(false, std::move(empty_state)); |
| 258 } |
| 259 } else { |
| 260 SetStateInternal(callback, state_proto); |
| 261 } |
| 262 } |
| 263 |
| 264 void MediaPerceptionAPIManager::SetStateInternal( |
| 265 const APIStateCallback& callback, |
| 266 const mri::State& state) { |
| 267 chromeos::MediaAnalyticsClient* dbus_client = |
| 268 chromeos::DBusThreadManager::Get()->GetMediaAnalyticsClient(); |
| 269 dbus_client->State(state, |
| 270 base::Bind(&MediaPerceptionAPIManager::StateCallback, |
| 271 base::Unretained(this), callback)); |
| 272 } |
| 273 |
| 274 void MediaPerceptionAPIManager::GetDiagnostics( |
| 275 const APIGetDiagnosticsCallback& callback) { |
| 276 chromeos::MediaAnalyticsClient* dbus_client = |
| 277 chromeos::DBusThreadManager::Get()->GetMediaAnalyticsClient(); |
| 278 dbus_client->GetDiagnostics( |
| 279 base::Bind(&MediaPerceptionAPIManager::GetDiagnosticsCallback, |
| 280 base::Unretained(this), callback)); |
| 281 } |
| 282 |
| 283 void MediaPerceptionAPIManager::UpstartCallback( |
| 284 const APIStateCallback& callback, |
| 285 const mri::State& state, |
| 286 bool succeeded) { |
| 287 if (succeeded) { |
| 288 analytics_process_running_ = true; |
| 289 SetStateInternal(callback, state); |
| 290 } else { |
| 291 analytics_process_running_ = false; |
| 292 LOG(ERROR) << "Failed to start media analytics process via Upstart."; |
| 293 media_perception::State empty_state; |
| 294 callback.Run(false, std::move(empty_state)); |
| 295 } |
| 296 } |
| 297 |
| 298 void MediaPerceptionAPIManager::StateCallback(const APIStateCallback& callback, |
| 299 bool succeeded, |
| 300 const mri::State& state_proto) { |
| 301 media_perception::State state; |
| 302 if (!succeeded) { |
| 303 state.status = media_perception::STATUS_TIMEOUT; |
| 304 callback.Run(false, std::move(state)); |
| 305 return; |
| 306 } |
| 307 state = StateProtoToIdl(state_proto); |
| 308 callback.Run(true, std::move(state)); |
| 309 } |
| 310 |
| 311 void MediaPerceptionAPIManager::GetDiagnosticsCallback( |
| 312 const APIGetDiagnosticsCallback& callback, |
| 313 bool succeeded, |
| 314 const mri::Diagnostics& diagnostics_proto) { |
| 315 media_perception::Diagnostics diagnostics; |
| 316 if (!succeeded) { |
| 317 callback.Run(false, std::move(diagnostics)); |
| 318 return; |
| 319 } |
| 320 diagnostics = DiagnosticsProtoToIdl(diagnostics_proto); |
| 321 callback.Run(true, std::move(diagnostics)); |
| 322 } |
| 323 |
| 324 void MediaPerceptionAPIManager::MediaPerceptionSignalHandler( |
| 325 const mri::MediaPerception& media_perception_proto) { |
| 326 EventRouter* router = EventRouter::Get(browser_context_); |
| 327 if (!router || !router->HasEventListener( |
| 328 media_perception::OnMediaPerception::kEventName)) { |
| 329 return; |
| 330 } |
| 331 media_perception::MediaPerception media_perception = |
| 332 MediaPerceptionProtoToIdl(media_perception_proto); |
| 333 std::unique_ptr<Event> event( |
| 334 new Event(events::MEDIA_PERCEPTION_PRIVATE_ON_MEDIA_PERCEPTION, |
| 335 media_perception::OnMediaPerception::kEventName, |
| 336 media_perception::OnMediaPerception::Create(media_perception))); |
| 337 router->BroadcastEvent(std::move(event)); |
| 338 } |
| 339 |
| 340 } // namespace extensions |
OLD | NEW |