andrew@webrtc.org | a7b57da | 2012-10-22 18:19:23 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license |
| 5 | * that can be found in the LICENSE file in the root of the source |
| 6 | * tree. An additional intellectual property rights grant can be found |
| 7 | * in the file PATENTS. All contributing project authors may |
| 8 | * be found in the AUTHORS file in the root of the source tree. |
| 9 | */ |
| 10 | |
| 11 | #include "video_engine/vie_input_manager.h" |
| 12 | |
| 13 | #include <cassert> |
| 14 | |
| 15 | #include "common_types.h" // NOLINT |
andrew@webrtc.org | 5f6856f | 2012-10-30 21:58:00 +0000 | [diff] [blame^] | 16 | #include "webrtc/modules/video_capture/include/video_capture_factory.h" |
andrew@webrtc.org | a7b57da | 2012-10-22 18:19:23 +0000 | [diff] [blame] | 17 | #include "modules/video_coding/main/interface/video_coding.h" |
| 18 | #include "modules/video_coding/main/interface/video_coding_defines.h" |
| 19 | #include "system_wrappers/interface/critical_section_wrapper.h" |
| 20 | #include "system_wrappers/interface/rw_lock_wrapper.h" |
| 21 | #include "system_wrappers/interface/trace.h" |
| 22 | #include "video_engine/include/vie_errors.h" |
| 23 | #include "video_engine/vie_capturer.h" |
| 24 | #include "video_engine/vie_defines.h" |
| 25 | #include "video_engine/vie_file_player.h" |
| 26 | |
| 27 | namespace webrtc { |
| 28 | |
| 29 | ViEInputManager::ViEInputManager(const int engine_id) |
| 30 | : engine_id_(engine_id), |
| 31 | map_cs_(CriticalSectionWrapper::CreateCriticalSection()), |
| 32 | vie_frame_provider_map_(), |
| 33 | capture_device_info_(NULL), |
| 34 | module_process_thread_(NULL) { |
| 35 | WEBRTC_TRACE(webrtc::kTraceMemory, webrtc::kTraceVideo, ViEId(engine_id_), |
| 36 | "%s", __FUNCTION__); |
| 37 | |
| 38 | for (int idx = 0; idx < kViEMaxCaptureDevices; idx++) { |
| 39 | free_capture_device_id_[idx] = true; |
| 40 | } |
| 41 | capture_device_info_ = VideoCaptureFactory::CreateDeviceInfo( |
| 42 | ViEModuleId(engine_id_)); |
| 43 | for (int idx = 0; idx < kViEMaxFilePlayers; idx++) { |
| 44 | free_file_id_[idx] = true; |
| 45 | } |
| 46 | } |
| 47 | |
| 48 | ViEInputManager::~ViEInputManager() { |
| 49 | WEBRTC_TRACE(webrtc::kTraceMemory, webrtc::kTraceVideo, ViEId(engine_id_), |
| 50 | "%s", __FUNCTION__); |
| 51 | while (vie_frame_provider_map_.Size() != 0) { |
| 52 | MapItem* item = vie_frame_provider_map_.First(); |
| 53 | assert(item); |
| 54 | ViEFrameProviderBase* frame_provider = |
| 55 | static_cast<ViEFrameProviderBase*>(item->GetItem()); |
| 56 | vie_frame_provider_map_.Erase(item); |
| 57 | delete frame_provider; |
| 58 | } |
| 59 | |
| 60 | if (capture_device_info_) { |
| 61 | delete capture_device_info_; |
| 62 | capture_device_info_ = NULL; |
| 63 | } |
| 64 | } |
| 65 | void ViEInputManager::SetModuleProcessThread( |
| 66 | ProcessThread* module_process_thread) { |
| 67 | assert(!module_process_thread_); |
| 68 | module_process_thread_ = module_process_thread; |
| 69 | } |
| 70 | |
| 71 | int ViEInputManager::NumberOfCaptureDevices() { |
| 72 | WEBRTC_TRACE(webrtc::kTraceInfo, webrtc::kTraceVideo, ViEId(engine_id_), "%s", |
| 73 | __FUNCTION__); |
| 74 | assert(capture_device_info_); |
| 75 | return capture_device_info_->NumberOfDevices(); |
| 76 | } |
| 77 | |
| 78 | int ViEInputManager::GetDeviceName(WebRtc_UWord32 device_number, |
| 79 | char* device_nameUTF8, |
| 80 | WebRtc_UWord32 device_name_length, |
| 81 | char* device_unique_idUTF8, |
| 82 | WebRtc_UWord32 device_unique_idUTF8Length) { |
| 83 | WEBRTC_TRACE(webrtc::kTraceInfo, webrtc::kTraceVideo, ViEId(engine_id_), |
| 84 | "%s(device_number: %d)", __FUNCTION__, device_number); |
| 85 | assert(capture_device_info_); |
| 86 | return capture_device_info_->GetDeviceName(device_number, device_nameUTF8, |
| 87 | device_name_length, |
| 88 | device_unique_idUTF8, |
| 89 | device_unique_idUTF8Length); |
| 90 | } |
| 91 | |
| 92 | int ViEInputManager::NumberOfCaptureCapabilities( |
| 93 | const char* device_unique_idUTF8) { |
| 94 | WEBRTC_TRACE(webrtc::kTraceInfo, webrtc::kTraceVideo, ViEId(engine_id_), "%s", |
| 95 | __FUNCTION__); |
| 96 | assert(capture_device_info_); |
| 97 | return capture_device_info_->NumberOfCapabilities(device_unique_idUTF8); |
| 98 | } |
| 99 | |
| 100 | int ViEInputManager::GetCaptureCapability( |
| 101 | const char* device_unique_idUTF8, |
| 102 | const WebRtc_UWord32 device_capability_number, |
| 103 | CaptureCapability& capability) { |
| 104 | WEBRTC_TRACE(webrtc::kTraceInfo, webrtc::kTraceVideo, ViEId(engine_id_), |
| 105 | "%s(device_unique_idUTF8: %s, device_capability_number: %d)", |
| 106 | __FUNCTION__, device_unique_idUTF8, device_capability_number); |
| 107 | assert(capture_device_info_); |
| 108 | VideoCaptureCapability module_capability; |
| 109 | int result = capture_device_info_->GetCapability(device_unique_idUTF8, |
| 110 | device_capability_number, |
| 111 | module_capability); |
| 112 | if (result != 0) |
| 113 | return result; |
| 114 | |
| 115 | // Copy from module type to public type. |
| 116 | capability.expectedCaptureDelay = module_capability.expectedCaptureDelay; |
| 117 | capability.height = module_capability.height; |
| 118 | capability.width = module_capability.width; |
| 119 | capability.interlaced = module_capability.interlaced; |
| 120 | capability.rawType = module_capability.rawType; |
| 121 | capability.codecType = module_capability.codecType; |
| 122 | capability.maxFPS = module_capability.maxFPS; |
| 123 | return result; |
| 124 | } |
| 125 | |
| 126 | int ViEInputManager::GetOrientation(const char* device_unique_idUTF8, |
| 127 | RotateCapturedFrame& orientation) { |
| 128 | WEBRTC_TRACE(webrtc::kTraceInfo, webrtc::kTraceVideo, ViEId(engine_id_), |
| 129 | "%s(device_unique_idUTF8: %s,)", __FUNCTION__, |
| 130 | device_unique_idUTF8); |
| 131 | assert(capture_device_info_); |
| 132 | VideoCaptureRotation module_orientation; |
| 133 | int result = capture_device_info_->GetOrientation(device_unique_idUTF8, |
| 134 | module_orientation); |
| 135 | // Copy from module type to public type. |
| 136 | switch (module_orientation) { |
| 137 | case kCameraRotate0: |
| 138 | orientation = RotateCapturedFrame_0; |
| 139 | break; |
| 140 | case kCameraRotate90: |
| 141 | orientation = RotateCapturedFrame_90; |
| 142 | break; |
| 143 | case kCameraRotate180: |
| 144 | orientation = RotateCapturedFrame_180; |
| 145 | break; |
| 146 | case kCameraRotate270: |
| 147 | orientation = RotateCapturedFrame_270; |
| 148 | break; |
| 149 | } |
| 150 | return result; |
| 151 | } |
| 152 | |
| 153 | int ViEInputManager::DisplayCaptureSettingsDialogBox( |
| 154 | const char* device_unique_idUTF8, |
| 155 | const char* dialog_titleUTF8, |
| 156 | void* parent_window, |
| 157 | WebRtc_UWord32 positionX, |
| 158 | WebRtc_UWord32 positionY) { |
| 159 | assert(capture_device_info_); |
| 160 | return capture_device_info_->DisplayCaptureSettingsDialogBox( |
| 161 | device_unique_idUTF8, dialog_titleUTF8, parent_window, positionX, |
| 162 | positionY); |
| 163 | } |
| 164 | |
| 165 | int ViEInputManager::CreateCaptureDevice( |
| 166 | const char* device_unique_idUTF8, |
| 167 | const WebRtc_UWord32 device_unique_idUTF8Length, |
| 168 | int& capture_id) { |
| 169 | WEBRTC_TRACE(webrtc::kTraceInfo, webrtc::kTraceVideo, ViEId(engine_id_), |
| 170 | "%s(device_unique_id: %s)", __FUNCTION__, device_unique_idUTF8); |
| 171 | CriticalSectionScoped cs(map_cs_.get()); |
| 172 | |
| 173 | // Make sure the device is not already allocated. |
| 174 | for (MapItem* item = vie_frame_provider_map_.First(); item != NULL; |
| 175 | item = vie_frame_provider_map_.Next(item)) { |
| 176 | // Make sure this is a capture device. |
| 177 | if (item->GetId() >= kViECaptureIdBase && |
| 178 | item->GetId() <= kViECaptureIdMax) { |
| 179 | ViECapturer* vie_capture = static_cast<ViECapturer*>(item->GetItem()); |
| 180 | assert(vie_capture); |
| 181 | // TODO(mflodman) Can we change input to avoid this cast? |
| 182 | const char* device_name = |
| 183 | reinterpret_cast<const char*>(vie_capture->CurrentDeviceName()); |
| 184 | if (strncmp(device_name, |
| 185 | reinterpret_cast<const char*>(device_unique_idUTF8), |
| 186 | strlen(device_name)) == 0) { |
| 187 | return kViECaptureDeviceAlreadyAllocated; |
| 188 | } |
| 189 | } |
| 190 | } |
| 191 | |
| 192 | // Make sure the device name is valid. |
| 193 | bool found_device = false; |
| 194 | for (WebRtc_UWord32 device_index = 0; |
| 195 | device_index < capture_device_info_->NumberOfDevices(); ++device_index) { |
| 196 | if (device_unique_idUTF8Length > kVideoCaptureUniqueNameLength) { |
| 197 | // User's string length is longer than the max. |
| 198 | return -1; |
| 199 | } |
| 200 | |
| 201 | char found_name[kVideoCaptureDeviceNameLength] = ""; |
| 202 | char found_unique_name[kVideoCaptureUniqueNameLength] = ""; |
| 203 | capture_device_info_->GetDeviceName(device_index, found_name, |
| 204 | kVideoCaptureDeviceNameLength, |
| 205 | found_unique_name, |
| 206 | kVideoCaptureUniqueNameLength); |
| 207 | |
| 208 | // TODO(mflodman) Can we change input to avoid this cast? |
| 209 | const char* cast_id = reinterpret_cast<const char*>(device_unique_idUTF8); |
| 210 | if (strncmp(cast_id, reinterpret_cast<const char*>(found_unique_name), |
| 211 | strlen(cast_id)) == 0) { |
| 212 | WEBRTC_TRACE(webrtc::kTraceDebug, webrtc::kTraceVideo, ViEId(engine_id_), |
| 213 | "%s:%d Capture device was found by unique ID: %s. Returning", |
| 214 | __FUNCTION__, __LINE__, device_unique_idUTF8); |
| 215 | found_device = true; |
| 216 | break; |
| 217 | } |
| 218 | } |
| 219 | if (!found_device) { |
| 220 | WEBRTC_TRACE(webrtc::kTraceDebug, webrtc::kTraceVideo, ViEId(engine_id_), |
| 221 | "%s:%d Capture device NOT found by unique ID: %s. Returning", |
| 222 | __FUNCTION__, __LINE__, device_unique_idUTF8); |
| 223 | return kViECaptureDeviceDoesNotExist; |
| 224 | } |
| 225 | |
| 226 | int newcapture_id = 0; |
| 227 | if (GetFreeCaptureId(&newcapture_id) == false) { |
| 228 | WEBRTC_TRACE(webrtc::kTraceError, webrtc::kTraceVideo, ViEId(engine_id_), |
| 229 | "%s: Maximum supported number of capture devices already in " |
| 230 | "use", __FUNCTION__); |
| 231 | return kViECaptureDeviceMaxNoDevicesAllocated; |
| 232 | } |
| 233 | ViECapturer* vie_capture = ViECapturer::CreateViECapture( |
| 234 | newcapture_id, engine_id_, device_unique_idUTF8, |
| 235 | device_unique_idUTF8Length, *module_process_thread_); |
| 236 | if (!vie_capture) { |
| 237 | ReturnCaptureId(newcapture_id); |
| 238 | WEBRTC_TRACE(webrtc::kTraceError, webrtc::kTraceVideo, ViEId(engine_id_), |
| 239 | "%s: Could not create capture module for %s", __FUNCTION__, |
| 240 | device_unique_idUTF8); |
| 241 | return kViECaptureDeviceUnknownError; |
| 242 | } |
| 243 | |
| 244 | if (vie_frame_provider_map_.Insert(newcapture_id, vie_capture) != 0) { |
| 245 | ReturnCaptureId(newcapture_id); |
| 246 | WEBRTC_TRACE(webrtc::kTraceError, webrtc::kTraceVideo, ViEId(engine_id_), |
| 247 | "%s: Could not insert capture module for %s", __FUNCTION__, |
| 248 | device_unique_idUTF8); |
| 249 | return kViECaptureDeviceUnknownError; |
| 250 | } |
| 251 | capture_id = newcapture_id; |
| 252 | WEBRTC_TRACE(webrtc::kTraceInfo, webrtc::kTraceVideo, ViEId(engine_id_), |
| 253 | "%s(device_unique_id: %s, capture_id: %d)", __FUNCTION__, |
| 254 | device_unique_idUTF8, capture_id); |
| 255 | return 0; |
| 256 | } |
| 257 | |
| 258 | int ViEInputManager::CreateCaptureDevice(VideoCaptureModule* capture_module, |
| 259 | int& capture_id) { |
| 260 | WEBRTC_TRACE(webrtc::kTraceInfo, webrtc::kTraceVideo, ViEId(engine_id_), "%s", |
| 261 | __FUNCTION__); |
| 262 | |
| 263 | CriticalSectionScoped cs(map_cs_.get()); |
| 264 | int newcapture_id = 0; |
| 265 | if (!GetFreeCaptureId(&newcapture_id)) { |
| 266 | WEBRTC_TRACE(webrtc::kTraceError, webrtc::kTraceVideo, ViEId(engine_id_), |
| 267 | "%s: Maximum supported number of capture devices already in " |
| 268 | "use", __FUNCTION__); |
| 269 | return kViECaptureDeviceMaxNoDevicesAllocated; |
| 270 | } |
| 271 | |
| 272 | ViECapturer* vie_capture = ViECapturer::CreateViECapture( |
| 273 | newcapture_id, engine_id_, capture_module, *module_process_thread_); |
| 274 | if (!vie_capture) { |
| 275 | ReturnCaptureId(newcapture_id); |
| 276 | WEBRTC_TRACE(webrtc::kTraceError, webrtc::kTraceVideo, ViEId(engine_id_), |
| 277 | "%s: Could attach capture module.", __FUNCTION__); |
| 278 | return kViECaptureDeviceUnknownError; |
| 279 | } |
| 280 | if (vie_frame_provider_map_.Insert(newcapture_id, vie_capture) != 0) { |
| 281 | ReturnCaptureId(newcapture_id); |
| 282 | WEBRTC_TRACE(webrtc::kTraceError, webrtc::kTraceVideo, ViEId(engine_id_), |
| 283 | "%s: Could not insert capture module", __FUNCTION__); |
| 284 | return kViECaptureDeviceUnknownError; |
| 285 | } |
| 286 | capture_id = newcapture_id; |
| 287 | WEBRTC_TRACE(webrtc::kTraceInfo, webrtc::kTraceVideo, ViEId(engine_id_), |
| 288 | "%s, capture_id: %d", __FUNCTION__, capture_id); |
| 289 | return 0; |
| 290 | } |
| 291 | |
| 292 | int ViEInputManager::DestroyCaptureDevice(const int capture_id) { |
| 293 | WEBRTC_TRACE(webrtc::kTraceInfo, webrtc::kTraceVideo, ViEId(engine_id_), |
| 294 | "%s(capture_id: %d)", __FUNCTION__, capture_id); |
| 295 | ViECapturer* vie_capture = NULL; |
| 296 | { |
| 297 | // We need exclusive access to the object to delete it. |
| 298 | // Take this write lock first since the read lock is taken before map_cs_. |
| 299 | ViEManagerWriteScoped wl(this); |
| 300 | CriticalSectionScoped cs(map_cs_.get()); |
| 301 | |
| 302 | vie_capture = ViECapturePtr(capture_id); |
| 303 | if (!vie_capture) { |
| 304 | WEBRTC_TRACE(webrtc::kTraceError, webrtc::kTraceVideo, ViEId(engine_id_), |
| 305 | "%s(capture_id: %d) - No such capture device id", |
| 306 | __FUNCTION__, capture_id); |
| 307 | return -1; |
| 308 | } |
| 309 | WebRtc_UWord32 num_callbacks = |
| 310 | vie_capture->NumberOfRegisteredFrameCallbacks(); |
| 311 | if (num_callbacks > 0) { |
| 312 | WEBRTC_TRACE(webrtc::kTraceWarning, webrtc::kTraceVideo, |
| 313 | ViEId(engine_id_), "%s(capture_id: %d) - %u registered " |
| 314 | "callbacks when destroying capture device", |
| 315 | __FUNCTION__, capture_id, num_callbacks); |
| 316 | } |
| 317 | vie_frame_provider_map_.Erase(capture_id); |
| 318 | ReturnCaptureId(capture_id); |
| 319 | // Leave cs before deleting the capture object. This is because deleting the |
| 320 | // object might cause deletions of renderers so we prefer to not have a lock |
| 321 | // at that time. |
| 322 | } |
| 323 | delete vie_capture; |
| 324 | return 0; |
| 325 | } |
| 326 | |
| 327 | int ViEInputManager::CreateExternalCaptureDevice( |
| 328 | ViEExternalCapture*& external_capture, |
| 329 | int& capture_id) { |
| 330 | WEBRTC_TRACE(webrtc::kTraceInfo, webrtc::kTraceVideo, ViEId(engine_id_), "%s", |
| 331 | __FUNCTION__); |
| 332 | CriticalSectionScoped cs(map_cs_.get()); |
| 333 | |
| 334 | int newcapture_id = 0; |
| 335 | if (GetFreeCaptureId(&newcapture_id) == false) { |
| 336 | WEBRTC_TRACE(webrtc::kTraceError, webrtc::kTraceVideo, ViEId(engine_id_), |
| 337 | "%s: Maximum supported number of capture devices already in " |
| 338 | "use", __FUNCTION__); |
| 339 | return kViECaptureDeviceMaxNoDevicesAllocated; |
| 340 | } |
| 341 | |
| 342 | ViECapturer* vie_capture = ViECapturer::CreateViECapture( |
| 343 | newcapture_id, engine_id_, NULL, 0, *module_process_thread_); |
| 344 | if (!vie_capture) { |
| 345 | ReturnCaptureId(newcapture_id); |
| 346 | WEBRTC_TRACE(webrtc::kTraceError, webrtc::kTraceVideo, ViEId(engine_id_), |
| 347 | "%s: Could not create capture module for external capture.", |
| 348 | __FUNCTION__); |
| 349 | return kViECaptureDeviceUnknownError; |
| 350 | } |
| 351 | |
| 352 | if (vie_frame_provider_map_.Insert(newcapture_id, vie_capture) != 0) { |
| 353 | ReturnCaptureId(newcapture_id); |
| 354 | WEBRTC_TRACE(webrtc::kTraceError, webrtc::kTraceVideo, ViEId(engine_id_), |
| 355 | "%s: Could not insert capture module for external capture.", |
| 356 | __FUNCTION__); |
| 357 | return kViECaptureDeviceUnknownError; |
| 358 | } |
| 359 | capture_id = newcapture_id; |
| 360 | external_capture = vie_capture; |
| 361 | WEBRTC_TRACE(webrtc::kTraceInfo, webrtc::kTraceVideo, ViEId(engine_id_), |
| 362 | "%s, capture_id: %d)", __FUNCTION__, capture_id); |
| 363 | return 0; |
| 364 | } |
| 365 | |
| 366 | int ViEInputManager::CreateFilePlayer(const char* file_nameUTF8, |
| 367 | const bool loop, |
| 368 | const webrtc::FileFormats file_format, |
| 369 | VoiceEngine* voe_ptr, int& file_id) { |
| 370 | WEBRTC_TRACE(webrtc::kTraceInfo, webrtc::kTraceVideo, ViEId(engine_id_), |
| 371 | "%s(device_unique_id: %s)", __FUNCTION__, file_nameUTF8); |
| 372 | |
| 373 | CriticalSectionScoped cs(map_cs_.get()); |
| 374 | int new_file_id = 0; |
| 375 | if (GetFreeFileId(&new_file_id) == false) { |
| 376 | WEBRTC_TRACE(webrtc::kTraceError, webrtc::kTraceVideo, ViEId(engine_id_), |
| 377 | "%s: Maximum supported number of file players already in use", |
| 378 | __FUNCTION__); |
| 379 | return kViEFileMaxNoOfFilesOpened; |
| 380 | } |
| 381 | |
| 382 | ViEFilePlayer* vie_file_player = ViEFilePlayer::CreateViEFilePlayer( |
| 383 | new_file_id, engine_id_, file_nameUTF8, loop, file_format, voe_ptr); |
| 384 | if (!vie_file_player) { |
| 385 | ReturnFileId(new_file_id); |
| 386 | WEBRTC_TRACE(webrtc::kTraceError, webrtc::kTraceVideo, ViEId(engine_id_), |
| 387 | "%s: Could not open file %s for playback", __FUNCTION__, |
| 388 | file_nameUTF8); |
| 389 | return kViEFileUnknownError; |
| 390 | } |
| 391 | |
| 392 | if (vie_frame_provider_map_.Insert(new_file_id, vie_file_player) != 0) { |
| 393 | ReturnCaptureId(new_file_id); |
| 394 | WEBRTC_TRACE(webrtc::kTraceError, webrtc::kTraceVideo, ViEId(engine_id_), |
| 395 | "%s: Could not insert file player for %s", __FUNCTION__, |
| 396 | file_nameUTF8); |
| 397 | delete vie_file_player; |
| 398 | return kViEFileUnknownError; |
| 399 | } |
| 400 | |
| 401 | file_id = new_file_id; |
| 402 | WEBRTC_TRACE(webrtc::kTraceInfo, webrtc::kTraceVideo, ViEId(engine_id_), |
| 403 | "%s(filename: %s, file_id: %d)", __FUNCTION__, file_nameUTF8, |
| 404 | new_file_id); |
| 405 | return 0; |
| 406 | } |
| 407 | |
| 408 | int ViEInputManager::DestroyFilePlayer(int file_id) { |
| 409 | WEBRTC_TRACE(webrtc::kTraceInfo, webrtc::kTraceVideo, ViEId(engine_id_), |
| 410 | "%s(file_id: %d)", __FUNCTION__, file_id); |
| 411 | |
| 412 | ViEFilePlayer* vie_file_player = NULL; |
| 413 | { |
| 414 | // We need exclusive access to the object to delete it. |
| 415 | // Take this write lock first since the read lock is taken before map_cs_. |
| 416 | ViEManagerWriteScoped wl(this); |
| 417 | |
| 418 | CriticalSectionScoped cs(map_cs_.get()); |
| 419 | vie_file_player = ViEFilePlayerPtr(file_id); |
| 420 | if (!vie_file_player) { |
| 421 | WEBRTC_TRACE(webrtc::kTraceError, webrtc::kTraceVideo, ViEId(engine_id_), |
| 422 | "%s(file_id: %d) - No such file player", __FUNCTION__, |
| 423 | file_id); |
| 424 | return -1; |
| 425 | } |
| 426 | int num_callbacks = vie_file_player->NumberOfRegisteredFrameCallbacks(); |
| 427 | if (num_callbacks > 0) { |
| 428 | WEBRTC_TRACE(webrtc::kTraceWarning, webrtc::kTraceVideo, |
| 429 | ViEId(engine_id_), "%s(file_id: %d) - %u registered " |
| 430 | "callbacks when destroying file player", __FUNCTION__, |
| 431 | file_id, num_callbacks); |
| 432 | } |
| 433 | vie_frame_provider_map_.Erase(file_id); |
| 434 | ReturnFileId(file_id); |
| 435 | // Leave cs before deleting the file object. This is because deleting the |
| 436 | // object might cause deletions of renderers so we prefer to not have a lock |
| 437 | // at that time. |
| 438 | } |
| 439 | delete vie_file_player; |
| 440 | return 0; |
| 441 | } |
| 442 | |
| 443 | bool ViEInputManager::GetFreeCaptureId(int* freecapture_id) { |
| 444 | WEBRTC_TRACE(webrtc::kTraceInfo, webrtc::kTraceVideo, ViEId(engine_id_), "%s", |
| 445 | __FUNCTION__); |
| 446 | for (int id = 0; id < kViEMaxCaptureDevices; id++) { |
| 447 | if (free_capture_device_id_[id]) { |
| 448 | // We found a free capture device id. |
| 449 | free_capture_device_id_[id] = false; |
| 450 | *freecapture_id = id + kViECaptureIdBase; |
| 451 | WEBRTC_TRACE(webrtc::kTraceInfo, webrtc::kTraceVideo, ViEId(engine_id_), |
| 452 | "%s: new id: %d", __FUNCTION__, *freecapture_id); |
| 453 | return true; |
| 454 | } |
| 455 | } |
| 456 | return false; |
| 457 | } |
| 458 | |
| 459 | void ViEInputManager::ReturnCaptureId(int capture_id) { |
| 460 | WEBRTC_TRACE(webrtc::kTraceInfo, webrtc::kTraceVideo, ViEId(engine_id_), |
| 461 | "%s(%d)", __FUNCTION__, capture_id); |
| 462 | CriticalSectionScoped cs(map_cs_.get()); |
| 463 | if (capture_id >= kViECaptureIdBase && |
| 464 | capture_id < kViEMaxCaptureDevices + kViECaptureIdBase) { |
| 465 | free_capture_device_id_[capture_id - kViECaptureIdBase] = true; |
| 466 | } |
| 467 | return; |
| 468 | } |
| 469 | |
| 470 | bool ViEInputManager::GetFreeFileId(int* free_file_id) { |
| 471 | WEBRTC_TRACE(webrtc::kTraceInfo, webrtc::kTraceVideo, ViEId(engine_id_), "%s", |
| 472 | __FUNCTION__); |
| 473 | |
| 474 | for (int id = 0; id < kViEMaxFilePlayers; id++) { |
| 475 | if (free_file_id_[id]) { |
| 476 | // We found a free capture device id. |
| 477 | free_file_id_[id] = false; |
| 478 | *free_file_id = id + kViEFileIdBase; |
| 479 | WEBRTC_TRACE(webrtc::kTraceInfo, webrtc::kTraceVideo, ViEId(engine_id_), |
| 480 | "%s: new id: %d", __FUNCTION__, *free_file_id); |
| 481 | return true; |
| 482 | } |
| 483 | } |
| 484 | return false; |
| 485 | } |
| 486 | |
| 487 | void ViEInputManager::ReturnFileId(int file_id) { |
| 488 | WEBRTC_TRACE(webrtc::kTraceInfo, webrtc::kTraceVideo, ViEId(engine_id_), |
| 489 | "%s(%d)", __FUNCTION__, file_id); |
| 490 | |
| 491 | CriticalSectionScoped cs(map_cs_.get()); |
| 492 | if (file_id >= kViEFileIdBase && |
| 493 | file_id < kViEMaxFilePlayers + kViEFileIdBase) { |
| 494 | free_file_id_[file_id - kViEFileIdBase] = true; |
| 495 | } |
| 496 | return; |
| 497 | } |
| 498 | |
| 499 | ViEFrameProviderBase* ViEInputManager::ViEFrameProvider( |
| 500 | const ViEFrameCallback* capture_observer) const { |
| 501 | assert(capture_observer); |
| 502 | CriticalSectionScoped cs(map_cs_.get()); |
| 503 | |
| 504 | for (MapItem* provider_item = vie_frame_provider_map_.First(); provider_item |
| 505 | != NULL; provider_item = vie_frame_provider_map_.Next(provider_item)) { |
| 506 | ViEFrameProviderBase* vie_frame_provider = |
| 507 | static_cast<ViEFrameProviderBase*>(provider_item->GetItem()); |
| 508 | assert(vie_frame_provider != NULL); |
| 509 | |
| 510 | if (vie_frame_provider->IsFrameCallbackRegistered(capture_observer)) { |
| 511 | // We found it. |
| 512 | return vie_frame_provider; |
| 513 | } |
| 514 | } |
| 515 | // No capture device set for this channel. |
| 516 | return NULL; |
| 517 | } |
| 518 | |
| 519 | ViEFrameProviderBase* ViEInputManager::ViEFrameProvider(int provider_id) const { |
| 520 | CriticalSectionScoped cs(map_cs_.get()); |
| 521 | MapItem* map_item = vie_frame_provider_map_.Find(provider_id); |
| 522 | if (!map_item) { |
| 523 | return NULL; |
| 524 | } |
| 525 | ViEFrameProviderBase* vie_frame_provider = |
| 526 | static_cast<ViEFrameProviderBase*>(map_item->GetItem()); |
| 527 | return vie_frame_provider; |
| 528 | } |
| 529 | |
| 530 | ViECapturer* ViEInputManager::ViECapturePtr(int capture_id) const { |
| 531 | if (!(capture_id >= kViECaptureIdBase && |
| 532 | capture_id <= kViECaptureIdBase + kViEMaxCaptureDevices)) |
| 533 | return NULL; |
| 534 | |
| 535 | CriticalSectionScoped cs(map_cs_.get()); |
| 536 | MapItem* map_item = vie_frame_provider_map_.Find(capture_id); |
| 537 | if (!map_item) { |
| 538 | return NULL; |
| 539 | } |
| 540 | ViECapturer* vie_capture = static_cast<ViECapturer*>(map_item->GetItem()); |
| 541 | return vie_capture; |
| 542 | } |
| 543 | |
| 544 | ViEFilePlayer* ViEInputManager::ViEFilePlayerPtr(int file_id) const { |
| 545 | if (file_id < kViEFileIdBase || file_id > kViEFileIdMax) { |
| 546 | return NULL; |
| 547 | } |
| 548 | CriticalSectionScoped cs(map_cs_.get()); |
| 549 | MapItem* map_item = vie_frame_provider_map_.Find(file_id); |
| 550 | if (!map_item) { |
| 551 | return NULL; |
| 552 | } |
| 553 | ViEFilePlayer* vie_file_player = |
| 554 | static_cast<ViEFilePlayer*>(map_item->GetItem()); |
| 555 | return vie_file_player; |
| 556 | } |
| 557 | |
| 558 | ViEInputManagerScoped::ViEInputManagerScoped( |
| 559 | const ViEInputManager& vie_input_manager) |
| 560 | : ViEManagerScopedBase(vie_input_manager) { |
| 561 | } |
| 562 | |
| 563 | ViECapturer* ViEInputManagerScoped::Capture(int capture_id) const { |
| 564 | return static_cast<const ViEInputManager*>(vie_manager_)->ViECapturePtr( |
| 565 | capture_id); |
| 566 | } |
| 567 | |
| 568 | ViEFrameProviderBase* ViEInputManagerScoped::FrameProvider( |
| 569 | const ViEFrameCallback* capture_observer) const { |
| 570 | return static_cast<const ViEInputManager*>(vie_manager_)->ViEFrameProvider( |
| 571 | capture_observer); |
| 572 | } |
| 573 | |
| 574 | ViEFrameProviderBase* ViEInputManagerScoped::FrameProvider( |
| 575 | int provider_id) const { |
| 576 | return static_cast<const ViEInputManager*>(vie_manager_)->ViEFrameProvider( |
| 577 | provider_id); |
| 578 | } |
| 579 | |
| 580 | ViEFilePlayer* ViEInputManagerScoped::FilePlayer(int file_id) const { |
| 581 | return static_cast<const ViEInputManager*>(vie_manager_)->ViEFilePlayerPtr( |
| 582 | file_id); |
| 583 | } |
| 584 | |
| 585 | } // namespace webrtc |