From e8be22c1924dbb7ec553ab0739cad65e9f80b38e Mon Sep 17 00:00:00 2001 From: "mflodman@webrtc.org" Date: Thu, 15 Dec 2011 10:19:29 +0000 Subject: [PATCH] Refactored ViEChannelManager ViEInputManager. Pointers/references and types will come in a future CL. Review URL: http://webrtc-codereview.appspot.com/317012 git-svn-id: http://webrtc.googlecode.com/svn/trunk@1202 4adac7df-926f-26a2-2b94-8c16560cd09d --- src/video_engine/vie_channel_manager.cc | 973 ++++++++---------- src/video_engine/vie_channel_manager.h | 149 +-- src/video_engine/vie_input_manager.cc | 1224 ++++++++++------------- src/video_engine/vie_input_manager.h | 193 ++-- 4 files changed, 1106 insertions(+), 1433 deletions(-) diff --git a/src/video_engine/vie_channel_manager.cc b/src/video_engine/vie_channel_manager.cc index 48c12dd90..403bf2081 100644 --- a/src/video_engine/vie_channel_manager.cc +++ b/src/video_engine/vie_channel_manager.cc @@ -8,603 +8,448 @@ * be found in the AUTHORS file in the root of the source tree. */ -/* - * vie_channel_manager.cc - */ +#include "video_engine/vie_channel_manager.h" -#include "vie_channel_manager.h" #include "engine_configurations.h" -#include "vie_defines.h" +#include "modules/utility/interface/process_thread.h" +#include "system_wrappers/interface/critical_section_wrapper.h" +#include "system_wrappers/interface/trace.h" +#include "video_engine/vie_channel.h" +#include "video_engine/vie_defines.h" +#include "video_engine/vie_encoder.h" +#include "voice_engine/main/interface/voe_video_sync.h" -#include "critical_section_wrapper.h" -#include "trace.h" -#include "vie_channel.h" -#include "vie_encoder.h" -#include "process_thread.h" +namespace webrtc { -// VoiceEngine -#include "voe_video_sync.h" - -namespace webrtc -{ - -ViEChannelManagerScoped::ViEChannelManagerScoped( - const ViEChannelManager& vieChannelManager) - : ViEManagerScopedBase(vieChannelManager) -{ +ViEChannelManager::ViEChannelManager( + int engine_id, + int number_of_cores, + ViEPerformanceMonitor& vie_performance_monitor) + : channel_id_critsect_(CriticalSectionWrapper::CreateCriticalSection()), + engine_id_(engine_id), + number_of_cores_(number_of_cores), + vie_performance_monitor_(vie_performance_monitor), + free_channel_ids_(new bool[kViEMaxNumberOfChannels]), + free_channel_ids_size_(kViEMaxNumberOfChannels), + voice_sync_interface_(NULL), + voice_engine_(NULL), + module_process_thread_(NULL) { + WEBRTC_TRACE(kTraceMemory, kTraceVideo, ViEId(engine_id), + "ViEChannelManager::ViEChannelManager(engine_id: %d)", + engine_id); + for (int idx = 0; idx < free_channel_ids_size_; idx++) { + free_channel_ids_[idx] = true; + } } -ViEChannel* ViEChannelManagerScoped::Channel(int vieChannelId) const -{ - return static_cast(vie_manager_)->ViEChannelPtr( - vieChannelId); -} -ViEEncoder* ViEChannelManagerScoped::Encoder(int vieChannelId) const -{ - return static_cast(vie_manager_)->ViEEncoderPtr( - vieChannelId); +ViEChannelManager::~ViEChannelManager() { + WEBRTC_TRACE(kTraceMemory, kTraceVideo, ViEId(engine_id_), + "ViEChannelManager Destructor, engine_id: %d", engine_id_); + + while (channel_map_.Size() != 0) { + MapItem* item = channel_map_.First(); + const int channel_id = item->GetId(); + item = NULL; + DeleteChannel(channel_id); + } + + if (voice_sync_interface_) { + voice_sync_interface_->Release(); + } + if (channel_id_critsect_) { + delete channel_id_critsect_; + channel_id_critsect_ = NULL; + } + if (free_channel_ids_) { + delete[] free_channel_ids_; + free_channel_ids_ = NULL; + free_channel_ids_size_ = 0; + } } -bool ViEChannelManagerScoped::ChannelUsingViEEncoder(int channelId) const -{ - return (static_cast(vie_manager_))-> - ChannelUsingViEEncoder(channelId); +void ViEChannelManager::SetModuleProcessThread( + ProcessThread& module_process_thread) { + assert(!module_process_thread_); + module_process_thread_ = &module_process_thread; } -// ============================================================================ -// VieChannelManager -// ============================================================================ +int ViEChannelManager::CreateChannel(int& channel_id) { + CriticalSectionScoped cs(*channel_id_critsect_); -// ---------------------------------------------------------------------------- -// Constructor -// ---------------------------------------------------------------------------- + // Get a free id for the new channel. + if (!GetFreeChannelId(channel_id)) { + WEBRTC_TRACE(kTraceError, kTraceVideo, ViEId(engine_id_), + "Max number of channels reached: %d", channel_map_.Size()); + return -1; + } -ViEChannelManager::ViEChannelManager(int engineId, - int numberOfCores, - ViEPerformanceMonitor& viePerformanceMonitor) - : _ptrChannelIdCritsect(CriticalSectionWrapper::CreateCriticalSection()), - _engineId(engineId), _numberOfCores(numberOfCores), - _viePerformanceMonitor(viePerformanceMonitor), _channelMap(), - _freeChannelIds(new bool[kViEMaxNumberOfChannels]), - _freeChannelIdsSize(kViEMaxNumberOfChannels), _vieEncoderMap(), - _voiceSyncInterface(NULL), _voiceEngine(NULL), - _moduleProcessThread(NULL) -{ - WEBRTC_TRACE(webrtc::kTraceMemory, webrtc::kTraceVideo, ViEId(engineId), - "ViEChannelManager::ViEChannelManager(engineId: %d) - Constructor", - engineId); + ViEChannel* vie_channel = new ViEChannel(channel_id, engine_id_, + number_of_cores_, + *module_process_thread_); + if (!vie_channel) { + ReturnChannelId(channel_id); + return -1; + } + if (vie_channel->Init() != 0) { + WEBRTC_TRACE(kTraceError, kTraceVideo, ViEId(engine_id_), + "%s could not init channel", __FUNCTION__, channel_id); + ReturnChannelId(channel_id); + delete vie_channel; + vie_channel = NULL; + return -1; + } - for (int idx = 0; idx < _freeChannelIdsSize; idx++) - { - _freeChannelIds[idx] = true; - } -} -// ---------------------------------------------------------------------------- -// SetModuleProcessThread -// Initialize the thread context used by none time critical tasks in video channels. -// ---------------------------------------------------------------------------- -void ViEChannelManager::SetModuleProcessThread( ProcessThread& moduleProcessThread) -{ - assert(!_moduleProcessThread); - _moduleProcessThread = &moduleProcessThread; + // There is no ViEEncoder for this channel, create one with default settings. + ViEEncoder* vie_encoder = new ViEEncoder(engine_id_, channel_id, + number_of_cores_, + *module_process_thread_); + if (!vie_encoder) { + WEBRTC_TRACE(kTraceError, kTraceVideo, ViEId(engine_id_), + "%s(video_channel_id: %d) - Could not create a new encoder", + __FUNCTION__, channel_id); + delete vie_channel; + return -1; + } + + if (vie_encoder_map_.Insert(channel_id, vie_encoder) != 0) { + // Could not add to the map. + WEBRTC_TRACE(kTraceError, kTraceVideo, ViEId(engine_id_), + "%s: Could not add new encoder for video channel %d", + __FUNCTION__, channel_id); + delete vie_channel; + delete vie_encoder; + return -1; + } + channel_map_.Insert(channel_id, vie_channel); + // Register the channel at the encoder. + RtpRtcp* send_rtp_rtcp_module = vie_encoder->SendRtpRtcpModule(); + if (vie_channel->RegisterSendRtpRtcpModule(*send_rtp_rtcp_module) != 0) { + assert(false); + vie_encoder_map_.Erase(channel_id); + channel_map_.Erase(channel_id); + ReturnChannelId(channel_id); + delete vie_channel; + WEBRTC_TRACE(kTraceError, kTraceVideo, ViEId(engine_id_, channel_id), + "%s: Could not register rtp module %d", __FUNCTION__, + channel_id); + return -1; + } + return 0; } -// ---------------------------------------------------------------------------- -// Destructor -// ---------------------------------------------------------------------------- +int ViEChannelManager::CreateChannel(int& channel_id, int original_channel) { + CriticalSectionScoped cs(*channel_id_critsect_); -ViEChannelManager::~ViEChannelManager() -{ - WEBRTC_TRACE(webrtc::kTraceMemory, webrtc::kTraceVideo, ViEId(_engineId), - "ViEChannelManager Destructor, engineId: %d", _engineId); + // Check that original_channel already exists. + ViEEncoder* vie_encoder = ViEEncoderPtr(original_channel); + if (!vie_encoder) { + WEBRTC_TRACE(kTraceError, kTraceVideo, ViEId(engine_id_), + "%s: Original channel doesn't exist", __FUNCTION__, + original_channel); + return -1; + } + VideoCodec video_codec; + if (vie_encoder->GetEncoder(video_codec) == 0) { + if (video_codec.numberOfSimulcastStreams > 0) { + WEBRTC_TRACE(kTraceError, kTraceVideo, + ViEId(engine_id_, original_channel), + "%s: Can't share a simulcast encoder", + __FUNCTION__); + return -1; + } + } - while (_channelMap.Size() != 0) - { - MapItem* item = _channelMap.First(); - const int channelId = item->GetId(); - item = NULL; - DeleteChannel(channelId); - } + // Get a free id for the new channel. + if (GetFreeChannelId(channel_id) == false) { + WEBRTC_TRACE(kTraceError, kTraceVideo, ViEId(engine_id_), + "Max number of channels reached: %d", channel_map_.Size()); + return -1; + } + ViEChannel* vie_channel = new ViEChannel(channel_id, engine_id_, + number_of_cores_, + *module_process_thread_); + if (!vie_channel) { + ReturnChannelId(channel_id); + return -1; + } + if (vie_channel->Init() != 0) { + WEBRTC_TRACE(kTraceError, kTraceVideo, ViEId(engine_id_), + "%s could not init channel", __FUNCTION__, channel_id); + ReturnChannelId(channel_id); + delete vie_channel; + vie_channel = NULL; + return -1; + } + if (vie_encoder_map_.Insert(channel_id, vie_encoder) != 0) { + WEBRTC_TRACE(kTraceError, kTraceVideo, ViEId(engine_id_), + "%s: Could not add new encoder for video channel %d", + __FUNCTION__, channel_id); + ReturnChannelId(channel_id); + delete vie_channel; + return -1; + } - if (_voiceSyncInterface) - _voiceSyncInterface->Release(); - if (_ptrChannelIdCritsect) - { - delete _ptrChannelIdCritsect; - _ptrChannelIdCritsect = NULL; - } - if (_freeChannelIds) - { - delete[] _freeChannelIds; - _freeChannelIds = NULL; - _freeChannelIdsSize = 0; - } + // Set the same encoder settings for the channel as used by the master + // channel. Do this before attaching rtp module to ensure all rtp children has + // the same codec type. + VideoCodec encoder; + if (vie_encoder->GetEncoder(encoder) == 0) { + vie_channel->SetSendCodec(encoder); + } + channel_map_.Insert(channel_id, vie_channel); + + // Register the channel at the encoder. + RtpRtcp* send_rtp_rtcp_module = vie_encoder->SendRtpRtcpModule(); + if (vie_channel->RegisterSendRtpRtcpModule(*send_rtp_rtcp_module) != 0) { + assert(false); + vie_encoder_map_.Erase(channel_id); + channel_map_.Erase(channel_id); + ReturnChannelId(channel_id); + delete vie_channel; + WEBRTC_TRACE(kTraceError, kTraceVideo, ViEId(engine_id_, channel_id), + "%s: Could not register rtp module %d", __FUNCTION__, + channel_id); + return -1; + } + return 0; } -// ---------------------------------------------------------------------------- -// CreateChannel -// -// Creates a new channel. 'channelId' will be the id of the created channel -// ---------------------------------------------------------------------------- -int ViEChannelManager::CreateChannel(int& channelId) -{ - CriticalSectionScoped cs(*_ptrChannelIdCritsect); - - // Get a free id for the new channel - if (GetFreeChannelId(channelId) == false) - { - WEBRTC_TRACE(webrtc::kTraceError, webrtc::kTraceVideo, ViEId(_engineId), - "Max number of channels reached: %d", _channelMap.Size()); - return -1; - } - - ViEChannel* vieChannel = new ViEChannel(channelId, _engineId, - _numberOfCores, - *_moduleProcessThread); - if (vieChannel == NULL) - { - ReturnChannelId(channelId); - return -1; - } - if (vieChannel->Init() != 0) - { - // Could not init channel - WEBRTC_TRACE(webrtc::kTraceError, webrtc::kTraceVideo, ViEId(_engineId), - "%s could not init channel", __FUNCTION__, channelId); - ReturnChannelId(channelId); - delete vieChannel; - vieChannel = NULL; - return -1; - - } - // There is no ViEEncoder for this channel, create one with default settings - ViEEncoder* vieEncoder = new ViEEncoder(_engineId, channelId, - _numberOfCores, - *_moduleProcessThread); - if (vieEncoder == NULL) - { - WEBRTC_TRACE(webrtc::kTraceError, webrtc::kTraceVideo, ViEId(_engineId), - "%s(videoChannelId: %d) - Could not create a new encoder", - __FUNCTION__, channelId); - delete vieChannel; - return -1; - } - - // Add to the map - if (_vieEncoderMap.Insert(channelId, vieEncoder) != 0) - { - // Could not add to the map - WEBRTC_TRACE(webrtc::kTraceError, webrtc::kTraceVideo, ViEId(_engineId), - "%s: Could not add new encoder for video channel %d", - __FUNCTION__, channelId); - delete vieChannel; - delete vieEncoder; - return -1; - } - _channelMap.Insert(channelId, vieChannel); - // Register the channel at the encoder - RtpRtcp* ptrSendRtpRtcpModule = vieEncoder->SendRtpRtcpModule(); - if (vieChannel->RegisterSendRtpRtcpModule(*ptrSendRtpRtcpModule) != 0) - { - assert(false); - _vieEncoderMap.Erase(channelId); - _channelMap.Erase(channelId); - ReturnChannelId(channelId); - delete vieChannel; - WEBRTC_TRACE(webrtc::kTraceError, webrtc::kTraceVideo, ViEId(_engineId, channelId), - "%s: Could not register rtp module %d", __FUNCTION__, - channelId); - return -1; - } - return 0; -} - -// ---------------------------------------------------------------------------- -// CreateChannel -// -// Creates a channel and attaches to an already existing ViEEncoder -// ---------------------------------------------------------------------------- - -int ViEChannelManager::CreateChannel(int& channelId, int originalChannel) -{ - CriticalSectionScoped cs(*_ptrChannelIdCritsect); - - // Check that originalChannel already exists - ViEEncoder* vieEncoder = ViEEncoderPtr(originalChannel); - if (vieEncoder == NULL) - { - // The original channel doesn't exist - WEBRTC_TRACE(webrtc::kTraceError, - webrtc::kTraceVideo, - ViEId(_engineId), - "%s: Original channel doesn't exist", - __FUNCTION__, - originalChannel); - return -1; - } - VideoCodec videoCodec; - if (vieEncoder->GetEncoder(videoCodec) == 0) - { - if (videoCodec.numberOfSimulcastStreams > 0) - { - WEBRTC_TRACE(webrtc::kTraceError, - webrtc::kTraceVideo, - ViEId(_engineId, originalChannel), - "%s: Can't share a simulcast encoder", - __FUNCTION__); - return -1; - } - } - - // Get a free id for the new channel - if (GetFreeChannelId(channelId) == false) - { - WEBRTC_TRACE(webrtc::kTraceError, webrtc::kTraceVideo, ViEId(_engineId), - "Max number of channels reached: %d", _channelMap.Size()); - return -1; - } - ViEChannel* vieChannel = new ViEChannel(channelId, _engineId, - _numberOfCores, - *_moduleProcessThread); - if (vieChannel == NULL) - { - ReturnChannelId(channelId); - return -1; - } - if (vieChannel->Init() != 0) - { - // Could not init channel - WEBRTC_TRACE(webrtc::kTraceError, webrtc::kTraceVideo, ViEId(_engineId), - "%s could not init channel", __FUNCTION__, channelId); - ReturnChannelId(channelId); - delete vieChannel; - vieChannel = NULL; - return -1; - } - if (_vieEncoderMap.Insert(channelId, vieEncoder) != 0) - { - // Could not add to the map - WEBRTC_TRACE(webrtc::kTraceError, webrtc::kTraceVideo, ViEId(_engineId), - "%s: Could not add new encoder for video channel %d", - __FUNCTION__, channelId); - ReturnChannelId(channelId); - delete vieChannel; - return -1; - } - - // Set the same encoder settings for the channel as used by the master channel. - // Do this before attaching rtp module to ensure all rtp cihldren has the same codec type - VideoCodec encoder; - if (vieEncoder->GetEncoder(encoder) == 0) - { - vieChannel->SetSendCodec(encoder); - } - _channelMap.Insert(channelId, vieChannel); - - // Register the channel at the encoder - RtpRtcp* ptrSendRtpRtcpModule = vieEncoder->SendRtpRtcpModule(); - if (vieChannel->RegisterSendRtpRtcpModule(*ptrSendRtpRtcpModule) != 0) - { - assert(false); - _vieEncoderMap.Erase(channelId); - _channelMap.Erase(channelId); - ReturnChannelId(channelId); - delete vieChannel; - WEBRTC_TRACE(webrtc::kTraceError, webrtc::kTraceVideo, ViEId(_engineId, channelId), - "%s: Could not register rtp module %d", __FUNCTION__, - channelId); - return -1; - } - return 0; -} - -// ---------------------------------------------------------------------------- -// DeleteChannel -// ---------------------------------------------------------------------------- - -int ViEChannelManager::DeleteChannel(int channelId) -{ - ViEChannel* vieChannel = NULL; - ViEEncoder* vieEncoder = NULL; - { - // Write lock to make sure no one is using the channel - ViEManagerWriteScoped wl(*this); - - // Protect the map - CriticalSectionScoped cs(*_ptrChannelIdCritsect); - - MapItem* mapItem = _channelMap.Find(channelId); - if (mapItem == NULL) - { - // No such channel - WEBRTC_TRACE(webrtc::kTraceError, webrtc::kTraceVideo, ViEId(_engineId), - "%s Channel doesn't exist: %d", __FUNCTION__, channelId); - return -1; - } - vieChannel = reinterpret_cast (mapItem->GetItem()); - _channelMap.Erase(mapItem); - // Deregister the channel from the ViEEncoder to stop the media flow - vieChannel->DeregisterSendRtpRtcpModule(); - ReturnChannelId(channelId); - - // Find the encoder object - mapItem = _vieEncoderMap.Find(channelId); - if (mapItem == NULL) - { - assert(false); - WEBRTC_TRACE(webrtc::kTraceInfo, webrtc::kTraceVideo, ViEId(_engineId), - "%s ViEEncoder not found for channel %d", __FUNCTION__, - channelId); - return -1; - } - // Get the ViEEncoder item - vieEncoder = reinterpret_cast (mapItem->GetItem()); - - // Check if other channels are using the same encoder - if (ChannelUsingViEEncoder(channelId)) - { - // Don't delete the ViEEncoder, at least on other channel is using it. - WEBRTC_TRACE( - webrtc::kTraceInfo, - webrtc::kTraceVideo, - ViEId(_engineId), - "%s ViEEncoder removed from map for channel %d, not deleted", - __FUNCTION__, channelId); - vieEncoder = NULL; - } else - { - WEBRTC_TRACE(webrtc::kTraceInfo, webrtc::kTraceVideo, ViEId(_engineId), - "%s ViEEncoder deleted for channel %d", __FUNCTION__, - channelId); - // Delete later when we've released the critsect - } - // We can't erase the item before we've checked for other channels using same ViEEncoder - _vieEncoderMap.Erase(mapItem); - - } - // Leave the write critsect before deleting the objects. - // Deleting a channel can cause other objects, such as renderers, to be deleted and might take time - if (vieEncoder) - { - delete vieEncoder; - } - delete vieChannel; - - WEBRTC_TRACE(webrtc::kTraceInfo, webrtc::kTraceVideo, ViEId(_engineId), - "%s Channel %d deleted", __FUNCTION__, channelId); - return 0; -} - -// ---------------------------------------------------------------------------- -// Channel -// -// Returns a pointer to the channel with id 'channelId' -// ---------------------------------------------------------------------------- - -ViEChannel* ViEChannelManager::ViEChannelPtr(int channelId) const -{ - CriticalSectionScoped cs(*_ptrChannelIdCritsect); - MapItem* mapItem = _channelMap.Find(channelId); - if (mapItem == NULL) - { - // No such channel - WEBRTC_TRACE(webrtc::kTraceError, webrtc::kTraceVideo, ViEId(_engineId), - "%s Channel doesn't exist: %d", __FUNCTION__, channelId); - return NULL; - } - ViEChannel* vieChannel = reinterpret_cast (mapItem->GetItem()); - return vieChannel; -} - -// ---------------------------------------------------------------------------- -// GetChannels -// -// Adds all channels to channelMap -// ---------------------------------------------------------------------------- - -void ViEChannelManager::GetViEChannels(MapWrapper& channelMap) -{ - CriticalSectionScoped cs(*_ptrChannelIdCritsect); - if (channelMap.Size() == 0) - { - // No channels - return; - } - // Add all items to 'channelMap' - for (MapItem* item = _channelMap.First(); item != NULL; item - = _channelMap.Next(item)) - { - channelMap.Insert(item->GetId(), item->GetItem()); - } - return; -} - -// ---------------------------------------------------------------------------- -// ViEEncoderPtr -// -// Gets the ViEEncoder used as input for videoChannelId -// ---------------------------------------------------------------------------- - -ViEEncoder* ViEChannelManager::ViEEncoderPtr(int videoChannelId) const -{ - CriticalSectionScoped cs(*_ptrChannelIdCritsect); - MapItem* mapItem = _vieEncoderMap.Find(videoChannelId); - if (mapItem == NULL) - { - // No ViEEncoder for this channel... - return NULL; - } - ViEEncoder* vieEncoder = static_cast (mapItem->GetItem()); - return vieEncoder; -} - -// ---------------------------------------------------------------------------- -// GetFreeChannelId -// -// Returns true if we found a new channel id, freeChannelId, false otherwise -// ---------------------------------------------------------------------------- -bool ViEChannelManager::GetFreeChannelId(int& freeChannelId) -{ - CriticalSectionScoped cs(*_ptrChannelIdCritsect); - int idx = 0; - while (idx < _freeChannelIdsSize) - { - if (_freeChannelIds[idx] == true) - { - // We've found a free id, allocate it and return - _freeChannelIds[idx] = false; - freeChannelId = idx + kViEChannelIdBase; - return true; - } - idx++; - } - // No free channel id - freeChannelId = -1; - return false; -} - -// ---------------------------------------------------------------------------- -// ReturnChannelID -// -// Returns a previously allocated channel id -// ---------------------------------------------------------------------------- -void ViEChannelManager::ReturnChannelId(int channelId) -{ - CriticalSectionScoped cs(*_ptrChannelIdCritsect); - assert(channelId < kViEMaxNumberOfChannels+kViEChannelIdBase && channelId>=kViEChannelIdBase); - _freeChannelIds[channelId - kViEChannelIdBase] = true; -} - -// ---------------------------------------------------------------------------- -// ChannelUsingViEEncoder -// -// Returns true if at least one nother channel is using the same encoder -// ---------------------------------------------------------------------------- - -bool ViEChannelManager::ChannelUsingViEEncoder(int channelId) const -{ - CriticalSectionScoped cs(*_ptrChannelIdCritsect); - MapItem* channelItem = _vieEncoderMap.Find(channelId); - if (channelItem == NULL) - { - // No ViEEncoder for this channel... - return false; - } - ViEEncoder* channelEncoder = - static_cast (channelItem->GetItem()); - - // Loop through all other channels to see if anyone points at the same ViEEncoder - MapItem* mapItem = _vieEncoderMap.First(); - while (mapItem) - { - if (mapItem->GetId() != channelId) - { - if (channelEncoder == static_cast (mapItem->GetItem())) - { - // We've found another channel using the same ViEEncoder - return true; - } - } - mapItem = _vieEncoderMap.Next(mapItem); - } - return false; -} - -// ---------------------------------------------------------------------------- -// SetVoiceEngine -// -// Set the voice engine instance to be used by all video channels. We are interested in the voice engine sync interfaces -// ---------------------------------------------------------------------------- -int ViEChannelManager::SetVoiceEngine(VoiceEngine* voiceEngine) -{ - - // Write lock to make sure no one is using the channel +int ViEChannelManager::DeleteChannel(int channel_id) { + ViEChannel* vie_channel = NULL; + ViEEncoder* vie_encoder = NULL; + { + // Write lock to make sure no one is using the channel. ViEManagerWriteScoped wl(*this); - CriticalSectionScoped cs(*_ptrChannelIdCritsect); + // Protect the map. + CriticalSectionScoped cs(*channel_id_critsect_); - VoEVideoSync* syncInterface = NULL; - if (voiceEngine) - { - // Get new sync interface; - syncInterface = VoEVideoSync::GetInterface(voiceEngine); - if (!syncInterface) - { - WEBRTC_TRACE(webrtc::kTraceError, webrtc::kTraceVideo, ViEId(_engineId), - "%s Can't get audio sync interface from VoiceEngine.", - __FUNCTION__); + MapItem* map_item = channel_map_.Find(channel_id); + if (!map_item) { + // No such channel. + WEBRTC_TRACE(kTraceError, kTraceVideo, ViEId(engine_id_), + "%s Channel doesn't exist: %d", __FUNCTION__, channel_id); + return -1; + } + vie_channel = reinterpret_cast(map_item->GetItem()); + channel_map_.Erase(map_item); + // Deregister the channel from the ViEEncoder to stop the media flow. + vie_channel->DeregisterSendRtpRtcpModule(); + ReturnChannelId(channel_id); - if (syncInterface) - { - syncInterface->Release(); - } - return -1; - } + // Find the encoder object. + map_item = vie_encoder_map_.Find(channel_id); + if (!map_item) { + assert(false); + WEBRTC_TRACE(kTraceInfo, kTraceVideo, ViEId(engine_id_), + "%s ViEEncoder not found for channel %d", __FUNCTION__, + channel_id); + return -1; } - for (MapItem* item = _channelMap.First(); item != NULL; item - = _channelMap.Next(item)) - { - ViEChannel* channel = static_cast (item->GetItem()); - assert(channel); - channel->SetVoiceChannel(-1, syncInterface); + // Get the ViEEncoder item. + vie_encoder = reinterpret_cast(map_item->GetItem()); + + // Check if other channels are using the same encoder. + if (ChannelUsingViEEncoder(channel_id)) { + // Don't delete the ViEEncoder, at least one other channel is using it. + WEBRTC_TRACE(kTraceInfo, kTraceVideo, ViEId(engine_id_), + "%s ViEEncoder removed from map for channel %d, not deleted", + __FUNCTION__, channel_id); + vie_encoder = NULL; + } else { + WEBRTC_TRACE(kTraceInfo, kTraceVideo, ViEId(engine_id_), + "%s ViEEncoder deleted for channel %d", __FUNCTION__, + channel_id); + // Delete later when we've released the critsect. } - if (_voiceSyncInterface) - { - _voiceSyncInterface->Release(); + + // We can't erase the item before we've checked for other channels using + // same ViEEncoder. + vie_encoder_map_.Erase(map_item); + } + + // Leave the write critsect before deleting the objects. + // Deleting a channel can cause other objects, such as renderers, to be + // deleted, which might take time. + if (vie_encoder) { + delete vie_encoder; + } + delete vie_channel; + + WEBRTC_TRACE(kTraceInfo, kTraceVideo, ViEId(engine_id_), + "%s Channel %d deleted", __FUNCTION__, channel_id); + return 0; +} + +int ViEChannelManager::SetVoiceEngine(VoiceEngine* voice_engine) { + // Write lock to make sure no one is using the channel. + ViEManagerWriteScoped wl(*this); + + CriticalSectionScoped cs(*channel_id_critsect_); + + VoEVideoSync* sync_interface = NULL; + if (voice_engine) { + // Get new sync interface. + sync_interface = VoEVideoSync::GetInterface(voice_engine); + if (!sync_interface) { + WEBRTC_TRACE(kTraceError, kTraceVideo, ViEId(engine_id_), + "%s Can't get audio sync interface from VoiceEngine.", + __FUNCTION__); + + if (sync_interface) { + sync_interface->Release(); + } + return -1; } - _voiceEngine = voiceEngine; - _voiceSyncInterface = syncInterface; + } + + for (MapItem* item = channel_map_.First(); item != NULL; + item = channel_map_.Next(item)) { + ViEChannel* channel = static_cast(item->GetItem()); + assert(channel); + channel->SetVoiceChannel(-1, sync_interface); + } + if (voice_sync_interface_) { + voice_sync_interface_->Release(); + } + voice_engine_ = voice_engine; + voice_sync_interface_ = sync_interface; + return 0; +} + +int ViEChannelManager::ConnectVoiceChannel(int channel_id, + int audio_channel_id) { + CriticalSectionScoped cs(*channel_id_critsect_); + if (!voice_sync_interface_) { + WEBRTC_TRACE(kTraceError, kTraceVideo, ViEId(engine_id_, channel_id), + "No VoE set"); + return -1; + } + ViEChannel* channel = ViEChannelPtr(channel_id); + if (!channel) { + return -1; + } + return channel->SetVoiceChannel(audio_channel_id, voice_sync_interface_); +} + +int ViEChannelManager::DisconnectVoiceChannel(int channel_id) { + CriticalSectionScoped cs(*channel_id_critsect_); + ViEChannel* channel = ViEChannelPtr(channel_id); + if (channel) { + channel->SetVoiceChannel(-1, NULL); return 0; - -} -VoiceEngine* ViEChannelManager::GetVoiceEngine() -{ - CriticalSectionScoped cs(*_ptrChannelIdCritsect); - return _voiceEngine; - + } + return -1; } -// ---------------------------------------------------------------------------- -// ConnectVoiceChannel -// -// Enables lip sync of the channel. -// ---------------------------------------------------------------------------- -int ViEChannelManager::ConnectVoiceChannel(int channelId, int audioChannelId) -{ - CriticalSectionScoped cs(*_ptrChannelIdCritsect); +VoiceEngine* ViEChannelManager::GetVoiceEngine() { + CriticalSectionScoped cs(*channel_id_critsect_); + return voice_engine_; +} - if (_voiceSyncInterface == NULL) - { - WEBRTC_TRACE(webrtc::kTraceError, webrtc::kTraceVideo, ViEId(_engineId, channelId), - "No VoE set"); - return -1; +ViEChannel* ViEChannelManager::ViEChannelPtr(int channel_id) const { + CriticalSectionScoped cs(*channel_id_critsect_); + MapItem* map_item = channel_map_.Find(channel_id); + if (!map_item) { + // No such channel. + WEBRTC_TRACE(kTraceError, kTraceVideo, ViEId(engine_id_), + "%s Channel doesn't exist: %d", __FUNCTION__, channel_id); + return NULL; + } + ViEChannel* vie_channel = reinterpret_cast(map_item->GetItem()); + return vie_channel; +} + +void ViEChannelManager::GetViEChannels(MapWrapper& channel_map) { + CriticalSectionScoped cs(*channel_id_critsect_); + if (channel_map.Size() == 0) { + return; + } + + // Add all items to 'channelMap'. + for (MapItem* item = channel_map_.First(); item != NULL; + item = channel_map_.Next(item)) { + channel_map.Insert(item->GetId(), item->GetItem()); + } + return; +} + +ViEEncoder* ViEChannelManager::ViEEncoderPtr(int video_channel_id) const { + CriticalSectionScoped cs(*channel_id_critsect_); + MapItem* map_item = vie_encoder_map_.Find(video_channel_id); + if (!map_item) { + return NULL; + } + ViEEncoder* vie_encoder = static_cast(map_item->GetItem()); + return vie_encoder; +} + +bool ViEChannelManager::GetFreeChannelId(int& free_channel_id) { + CriticalSectionScoped cs(*channel_id_critsect_); + int idx = 0; + while (idx < free_channel_ids_size_) { + if (free_channel_ids_[idx] == true) { + // We've found a free id, allocate it and return. + free_channel_ids_[idx] = false; + free_channel_id = idx + kViEChannelIdBase; + return true; } - ViEChannel* channel = ViEChannelPtr(channelId); - if (!channel) - { - return -1; - } - return channel->SetVoiceChannel(audioChannelId, _voiceSyncInterface); - + idx++; + } + // No free channel id. + free_channel_id = -1; + return false; } -// ---------------------------------------------------------------------------- -// DisconnectVoiceChannel -// -// Disables lip sync of the channel. -// ---------------------------------------------------------------------------- -int ViEChannelManager::DisconnectVoiceChannel(int channelId) -{ - CriticalSectionScoped cs(*_ptrChannelIdCritsect); - ViEChannel* channel = ViEChannelPtr(channelId); - if (channel) - { - channel->SetVoiceChannel(-1, NULL); - return 0; - } else - { - return -1; - } +void ViEChannelManager::ReturnChannelId(int channel_id) { + CriticalSectionScoped cs(*channel_id_critsect_); + assert(channel_id < kViEMaxNumberOfChannels + kViEChannelIdBase && + channel_id >= kViEChannelIdBase); + free_channel_ids_[channel_id - kViEChannelIdBase] = true; } -} // namespace webrtc + +bool ViEChannelManager::ChannelUsingViEEncoder(int channel_id) const { + CriticalSectionScoped cs(*channel_id_critsect_); + MapItem* channel_item = vie_encoder_map_.Find(channel_id); + if (!channel_item) { + // No ViEEncoder for this channel. + return false; + } + ViEEncoder* channel_encoder = + static_cast(channel_item->GetItem()); + + // Loop through all other channels to see if anyone points at the same + // ViEEncoder. + MapItem* map_item = vie_encoder_map_.First(); + while (map_item) { + if (map_item->GetId() != channel_id) { + if (channel_encoder == static_cast(map_item->GetItem())) { + return true; + } + } + map_item = vie_encoder_map_.Next(map_item); + } + return false; +} + +ViEChannelManagerScoped::ViEChannelManagerScoped( + const ViEChannelManager& vie_channel_manager) + : ViEManagerScopedBase(vie_channel_manager) { +} + +ViEChannel* ViEChannelManagerScoped::Channel(int vie_channel_id) const { + return static_cast(vie_manager_)->ViEChannelPtr( + vie_channel_id); +} +ViEEncoder* ViEChannelManagerScoped::Encoder(int vie_channel_id) const { + return static_cast(vie_manager_)->ViEEncoderPtr( + vie_channel_id); +} + +bool ViEChannelManagerScoped::ChannelUsingViEEncoder(int channel_id) const { + return (static_cast(vie_manager_))-> + ChannelUsingViEEncoder(channel_id); +} + +} // namespace webrtc diff --git a/src/video_engine/vie_channel_manager.h b/src/video_engine/vie_channel_manager.h index 2fb6c8499..f3bdb93be 100644 --- a/src/video_engine/vie_channel_manager.h +++ b/src/video_engine/vie_channel_manager.h @@ -8,95 +8,106 @@ * be found in the AUTHORS file in the root of the source tree. */ -/* - * vie_channel_manager.h - */ +#ifndef WEBRTC_VIDEO_ENGINE_VIE_CHANNEL_MANAGER_H_ +#define WEBRTC_VIDEO_ENGINE_VIE_CHANNEL_MANAGER_H_ -#ifndef WEBRTC_VIDEO_ENGINE_MAIN_SOURCE_VIE_CHANNEL_MANAGER_H_ -#define WEBRTC_VIDEO_ENGINE_MAIN_SOURCE_VIE_CHANNEL_MANAGER_H_ - -// Defines #include "engine_configurations.h" -#include "vie_defines.h" +#include "system_wrappers/interface/map_wrapper.h" #include "typedefs.h" -#include "map_wrapper.h" -#include "vie_manager_base.h" +#include "video_engine/vie_defines.h" +#include "video_engine/vie_manager_base.h" + +namespace webrtc { -namespace webrtc -{ class CriticalSectionWrapper; -//class VoiceEngine; class ProcessThread; class ViEChannel; -class VoEVideoSync; -class ViEPerformanceMonitor; class ViEEncoder; +class ViEPerformanceMonitor; +class VoEVideoSync; class VoiceEngine; -// ------------------------------------------------------------------ -// ViEChannelManager -// ------------------------------------------------------------------ +class ViEChannelManager: private ViEManagerBase { + friend class ViEChannelManagerScoped; + public: + ViEChannelManager(int engine_id, + int number_of_cores, + ViEPerformanceMonitor& vie_performance_monitor); + ~ViEChannelManager(); -class ViEChannelManager: private ViEManagerBase -{ - friend class ViEChannelManagerScoped; + void SetModuleProcessThread(ProcessThread& module_process_thread); -public: - ViEChannelManager(int engineId, int numberOfCores, - ViEPerformanceMonitor& viePerformanceMonitor); - ~ViEChannelManager(); + // Creates a new channel. 'channelId' will be the id of the created channel. + int CreateChannel(int& channel_id); - void SetModuleProcessThread(ProcessThread& moduleProcessThread); - int CreateChannel(int& channelId); - int CreateChannel(int& channelId, int originalChannel); - int DeleteChannel(int channelId); - int SetVoiceEngine(VoiceEngine* voiceEngine); - int ConnectVoiceChannel(int channelId, int audioChannelId); - int DisconnectVoiceChannel(int channelId); - VoiceEngine* GetVoiceEngine(); + // Creates a channel and attaches to an already existing ViEEncoder. + int CreateChannel(int& channel_id, int original_channel); -private: - // Used by ViEChannelScoped, forcing a manager user to use scoped - ViEChannel* ViEChannelPtr(int channelId) const; - void GetViEChannels(MapWrapper& channelMap); + // Deletes a channel. + int DeleteChannel(int channel_id); - // Methods used by ViECaptureScoped and ViEEncoderScoped - ViEEncoder* ViEEncoderPtr(int videoChannelId) const; + // Set the voice engine instance to be used by all video channels. + int SetVoiceEngine(VoiceEngine* voice_engine); - bool GetFreeChannelId(int& freeChannelId); - void ReturnChannelId(int channelId); + // Enables lip sync of the channel. + int ConnectVoiceChannel(int channel_id, int audio_channel_id); - // Returns true if at least one other channels uses the same ViEEncoder as channelId - bool ChannelUsingViEEncoder(int channelId) const; + // Disables lip sync of the channel. + int DisconnectVoiceChannel(int channel_id); - // Members - CriticalSectionWrapper* _ptrChannelIdCritsect; // protecting _channelMap and _freeChannelIds - int _engineId; - int _numberOfCores; - ViEPerformanceMonitor& _viePerformanceMonitor; - MapWrapper _channelMap; - bool* _freeChannelIds; - int _freeChannelIdsSize; - // Encoder - MapWrapper _vieEncoderMap; // Channel id -> ViEEncoder - VoEVideoSync* _voiceSyncInterface; - VoiceEngine* _voiceEngine; - ProcessThread* _moduleProcessThread; + VoiceEngine* GetVoiceEngine(); + + private: + // Used by ViEChannelScoped, forcing a manager user to use scoped. + // Returns a pointer to the channel with id 'channelId'. + ViEChannel* ViEChannelPtr(int channel_id) const; + + // Adds all channels to channel_map. + void GetViEChannels(MapWrapper& channel_map); + + // Methods used by ViECaptureScoped and ViEEncoderScoped. + // Gets the ViEEncoder used as input for video_channel_id + ViEEncoder* ViEEncoderPtr(int video_channel_id) const; + + // Returns true if we found a new channel id, free_channel_id, false + // otherwise. + bool GetFreeChannelId(int& free_channel_id); + + // Returns a previously allocated channel id. + void ReturnChannelId(int channel_id); + + // Returns true if at least one other channels uses the same ViEEncoder as + // channel_id. + bool ChannelUsingViEEncoder(int channel_id) const; + + // Protects channel_map_ and free_channel_ids_. + CriticalSectionWrapper* channel_id_critsect_; + int engine_id_; + int number_of_cores_; + ViEPerformanceMonitor& vie_performance_monitor_; + MapWrapper channel_map_; + bool* free_channel_ids_; + int free_channel_ids_size_; + + // Maps Channel id -> ViEEncoder. + MapWrapper vie_encoder_map_; + VoEVideoSync* voice_sync_interface_; + VoiceEngine* voice_engine_; + ProcessThread* module_process_thread_; }; -// ------------------------------------------------------------------ -// ViEChannelManagerScoped -// ------------------------------------------------------------------ -class ViEChannelManagerScoped: private ViEManagerScopedBase -{ -public: - ViEChannelManagerScoped(const ViEChannelManager& vieChannelManager); - ViEChannel* Channel(int vieChannelId) const; - ViEEncoder* Encoder(int vieChannelId) const; +class ViEChannelManagerScoped: private ViEManagerScopedBase { + public: + explicit ViEChannelManagerScoped( + const ViEChannelManager& vie_channel_manager); + ViEChannel* Channel(int vie_channel_id) const; + ViEEncoder* Encoder(int vie_channel_id) const; - // Returns true if at lease one other channels uses the same ViEEncoder as channelId - bool ChannelUsingViEEncoder(int channelId) const; + // Returns true if at lease one other channels uses the same ViEEncoder as + // channel_id. + bool ChannelUsingViEEncoder(int channel_id) const; }; -} //namespace webrtc -#endif // WEBRTC_VIDEO_ENGINE_MAIN_SOURCE_VIE_CHANNEL_MANAGER_H_ +} // namespace webrtc + +#endif // WEBRTC_VIDEO_ENGINE_VIE_CHANNEL_MANAGER_H_ diff --git a/src/video_engine/vie_input_manager.cc b/src/video_engine/vie_input_manager.cc index 49cbd40c8..23834eee2 100644 --- a/src/video_engine/vie_input_manager.cc +++ b/src/video_engine/vie_input_manager.cc @@ -8,800 +8,596 @@ * be found in the AUTHORS file in the root of the source tree. */ -/* - * vie_input_manager.cc - */ - -#include "vie_input_manager.h" -#include "vie_defines.h" - -#include "common_types.h" -#include "critical_section_wrapper.h" -#include "video_capture_factory.h" -#include "video_coding.h" -#include "video_coding_defines.h" -#include "rw_lock_wrapper.h" -#include "trace.h" -#include "vie_capturer.h" -#include "vie_file_player.h" -#include "vie_errors.h" +#include "video_engine/vie_input_manager.h" #include +#include "common_types.h" +#include "modules/video_capture/main/interface/video_capture_factory.h" +#include "modules/video_coding/main/interface/video_coding.h" +#include "modules/video_coding/main/interface/video_coding_defines.h" +#include "system_wrappers/interface/critical_section_wrapper.h" +#include "system_wrappers/interface/rw_lock_wrapper.h" +#include "system_wrappers/interface/trace.h" +#include "video_engine/main/interface/vie_errors.h" +#include "video_engine/vie_capturer.h" +#include "video_engine/vie_defines.h" +#include "video_engine/vie_file_player.h" + namespace webrtc { -//============================================================================= -// ViEInputManager -//============================================================================= +ViEInputManager::ViEInputManager(const int engine_id) + : engine_id_(engine_id), + map_cs_(*CriticalSectionWrapper::CreateCriticalSection()), + vie_frame_provider_map_(), + capture_device_info_(NULL), + module_process_thread_(NULL) { + WEBRTC_TRACE(webrtc::kTraceMemory, webrtc::kTraceVideo, ViEId(engine_id_), + "%s", __FUNCTION__); -// ---------------------------------------------------------------------------- -// Constructor -// ---------------------------------------------------------------------------- + for (int idx = 0; idx < kViEMaxCaptureDevices; idx++) { + free_capture_device_id_[idx] = true; + } + capture_device_info_ = VideoCaptureFactory::CreateDeviceInfo( + ViEModuleId(engine_id_)); + for (int idx = 0; idx < kViEMaxFilePlayers; idx++) { + free_file_id_[idx] = true; + } +} -ViEInputManager::ViEInputManager(const int engineId) - : _engineId(engineId), - _mapCritsect(*CriticalSectionWrapper::CreateCriticalSection()), - _vieFrameProviderMap(), - _ptrCaptureDeviceInfo(NULL), - _freeCaptureDeviceId(), - _freeFileId(), - _moduleProcessThread(NULL) -{ - WEBRTC_TRACE(webrtc::kTraceMemory, webrtc::kTraceVideo, ViEId(_engineId), "%s", +ViEInputManager::~ViEInputManager() { + WEBRTC_TRACE(webrtc::kTraceMemory, webrtc::kTraceVideo, ViEId(engine_id_), + "%s", __FUNCTION__); + while (vie_frame_provider_map_.Size() != 0) { + MapItem* item = vie_frame_provider_map_.First(); + assert(item); + ViEFrameProviderBase* frame_provider = + static_cast(item->GetItem()); + vie_frame_provider_map_.Erase(item); + delete frame_provider; + } + + delete &map_cs_; + if (capture_device_info_) { + delete capture_device_info_; + capture_device_info_ = NULL; + } +} +void ViEInputManager::SetModuleProcessThread( + ProcessThread& module_process_thread) { + assert(!module_process_thread_); + module_process_thread_ = &module_process_thread; +} + +int ViEInputManager::NumberOfCaptureDevices() { + WEBRTC_TRACE(webrtc::kTraceInfo, webrtc::kTraceVideo, ViEId(engine_id_), "%s", __FUNCTION__); - - for (int idx = 0; idx < kViEMaxCaptureDevices; idx++) - { - _freeCaptureDeviceId[idx] = true; - } - _ptrCaptureDeviceInfo = - VideoCaptureFactory::CreateDeviceInfo( - ViEModuleId(_engineId)); - for (int idx = 0; idx < kViEMaxFilePlayers; idx++) - { - _freeFileId[idx] = true; - } - + assert(capture_device_info_); + return capture_device_info_->NumberOfDevices(); } -// ---------------------------------------------------------------------------- -// Destructor -// ---------------------------------------------------------------------------- - -ViEInputManager::~ViEInputManager() -{ - WEBRTC_TRACE(webrtc::kTraceMemory, webrtc::kTraceVideo, ViEId(_engineId), "%s", - __FUNCTION__); - while (_vieFrameProviderMap.Size() != 0) - { - MapItem* item = _vieFrameProviderMap.First(); - assert(item); - ViEFrameProviderBase* frameProvider = static_cast - (item->GetItem()); - _vieFrameProviderMap.Erase(item); - delete frameProvider; - } - - delete &_mapCritsect; - if (_ptrCaptureDeviceInfo) - { - delete _ptrCaptureDeviceInfo; - _ptrCaptureDeviceInfo = NULL; - } +int ViEInputManager::GetDeviceName(WebRtc_UWord32 device_number, + WebRtc_UWord8* device_nameUTF8, + WebRtc_UWord32 device_name_length, + WebRtc_UWord8* device_unique_idUTF8, + WebRtc_UWord32 device_unique_idUTF8Length) { + WEBRTC_TRACE(webrtc::kTraceInfo, webrtc::kTraceVideo, ViEId(engine_id_), + "%s(device_number: %d)", __FUNCTION__, device_number); + assert(capture_device_info_); + return capture_device_info_->GetDeviceName(device_number, device_nameUTF8, + device_name_length, + device_unique_idUTF8, + device_unique_idUTF8Length); } -// ---------------------------------------------------------------------------- -// SetModuleProcessThread -// Initialize the thread context used by none time critical tasks in capture modules. -// ---------------------------------------------------------------------------- -void ViEInputManager::SetModuleProcessThread(ProcessThread& moduleProcessThread) -{ - assert(!_moduleProcessThread); - _moduleProcessThread = &moduleProcessThread; -} -// ---------------------------------------------------------------------------- -// NumberOfCaptureDevices -// -// Returns the number of available capture devices -// ---------------------------------------------------------------------------- - -// Capture device information -int ViEInputManager::NumberOfCaptureDevices() -{ - WEBRTC_TRACE(webrtc::kTraceInfo, webrtc::kTraceVideo, ViEId(_engineId), "%s", - __FUNCTION__); - assert(_ptrCaptureDeviceInfo); - return _ptrCaptureDeviceInfo->NumberOfDevices(); -} - -// ---------------------------------------------------------------------------- -// GetDeviceName -// ---------------------------------------------------------------------------- - -int ViEInputManager::GetDeviceName(WebRtc_UWord32 deviceNumber, - WebRtc_UWord8* deviceNameUTF8, - WebRtc_UWord32 deviceNameLength, - WebRtc_UWord8* deviceUniqueIdUTF8, - WebRtc_UWord32 deviceUniqueIdUTF8Length) -{ - WEBRTC_TRACE(webrtc::kTraceInfo, webrtc::kTraceVideo, ViEId(_engineId), - "%s(deviceNumber: %d)", __FUNCTION__, deviceNumber); - assert(_ptrCaptureDeviceInfo); - return _ptrCaptureDeviceInfo->GetDeviceName(deviceNumber, deviceNameUTF8, - deviceNameLength, - deviceUniqueIdUTF8, - deviceUniqueIdUTF8Length); -} - -// ---------------------------------------------------------------------------- -// NumberOfCaptureCapabilities -// -// Returns the number of capture capabilities for the specified capture device -// ---------------------------------------------------------------------------- - int ViEInputManager::NumberOfCaptureCapabilities( - const WebRtc_UWord8* deviceUniqueIdUTF8) -{ - WEBRTC_TRACE(webrtc::kTraceInfo, webrtc::kTraceVideo, ViEId(_engineId), "%s", + const WebRtc_UWord8* device_unique_idUTF8) { + WEBRTC_TRACE(webrtc::kTraceInfo, webrtc::kTraceVideo, ViEId(engine_id_), "%s", __FUNCTION__); - assert(_ptrCaptureDeviceInfo); - return _ptrCaptureDeviceInfo->NumberOfCapabilities(deviceUniqueIdUTF8); + assert(capture_device_info_); + return capture_device_info_->NumberOfCapabilities(device_unique_idUTF8); } -// ---------------------------------------------------------------------------- -// GetCaptureCapability -// ---------------------------------------------------------------------------- - -int ViEInputManager::GetCaptureCapability(const WebRtc_UWord8* deviceUniqueIdUTF8, - const WebRtc_UWord32 deviceCapabilityNumber, - CaptureCapability& capability) -{ - WEBRTC_TRACE(webrtc::kTraceInfo, webrtc::kTraceVideo, ViEId(_engineId), - "%s(deviceUniqueIdUTF8: %s, deviceCapabilityNumber: %d)", - __FUNCTION__, deviceUniqueIdUTF8, deviceCapabilityNumber); - assert(_ptrCaptureDeviceInfo); - VideoCaptureCapability moduleCapability; - int result = _ptrCaptureDeviceInfo->GetCapability(deviceUniqueIdUTF8, - deviceCapabilityNumber, - moduleCapability); - if (result != 0) - return result; - - // Copy from module type to public type - capability.expectedCaptureDelay = moduleCapability.expectedCaptureDelay; - capability.height = moduleCapability.height; - capability.width = moduleCapability.width; - capability.interlaced = moduleCapability.interlaced; - capability.rawType = moduleCapability.rawType; - capability.codecType = moduleCapability.codecType; - capability.maxFPS = moduleCapability.maxFPS; +int ViEInputManager::GetCaptureCapability( + const WebRtc_UWord8* device_unique_idUTF8, + const WebRtc_UWord32 device_capability_number, + CaptureCapability& capability) { + WEBRTC_TRACE(webrtc::kTraceInfo, webrtc::kTraceVideo, ViEId(engine_id_), + "%s(device_unique_idUTF8: %s, device_capability_number: %d)", + __FUNCTION__, device_unique_idUTF8, device_capability_number); + assert(capture_device_info_); + VideoCaptureCapability module_capability; + int result = capture_device_info_->GetCapability(device_unique_idUTF8, + device_capability_number, + module_capability); + if (result != 0) return result; + + // Copy from module type to public type. + capability.expectedCaptureDelay = module_capability.expectedCaptureDelay; + capability.height = module_capability.height; + capability.width = module_capability.width; + capability.interlaced = module_capability.interlaced; + capability.rawType = module_capability.rawType; + capability.codecType = module_capability.codecType; + capability.maxFPS = module_capability.maxFPS; + return result; } -int ViEInputManager::GetOrientation(const WebRtc_UWord8* deviceUniqueIdUTF8, - RotateCapturedFrame &orientation) -{ - WEBRTC_TRACE(webrtc::kTraceInfo, webrtc::kTraceVideo, ViEId(_engineId), - "%s(deviceUniqueIdUTF8: %s,)", __FUNCTION__, deviceUniqueIdUTF8); - assert(_ptrCaptureDeviceInfo); - VideoCaptureRotation moduleOrientation; - int result = _ptrCaptureDeviceInfo->GetOrientation(deviceUniqueIdUTF8, - moduleOrientation); - // Copy from module type to public type - switch (moduleOrientation) - { - case kCameraRotate0: - orientation = RotateCapturedFrame_0; - break; - case kCameraRotate90: - orientation = RotateCapturedFrame_90; - break; - case kCameraRotate180: - orientation = RotateCapturedFrame_180; - break; - case kCameraRotate270: - orientation = RotateCapturedFrame_270; - break; - default: - assert(!"Unknown enum"); - } - return result; +int ViEInputManager::GetOrientation(const WebRtc_UWord8* device_unique_idUTF8, + RotateCapturedFrame& orientation) { + WEBRTC_TRACE(webrtc::kTraceInfo, webrtc::kTraceVideo, ViEId(engine_id_), + "%s(device_unique_idUTF8: %s,)", __FUNCTION__, + device_unique_idUTF8); + assert(capture_device_info_); + VideoCaptureRotation module_orientation; + int result = capture_device_info_->GetOrientation(device_unique_idUTF8, + module_orientation); + // Copy from module type to public type. + switch (module_orientation) { + case kCameraRotate0: + orientation = RotateCapturedFrame_0; + break; + case kCameraRotate90: + orientation = RotateCapturedFrame_90; + break; + case kCameraRotate180: + orientation = RotateCapturedFrame_180; + break; + case kCameraRotate270: + orientation = RotateCapturedFrame_270; + break; + default: + assert(!"Unknown enum"); + } + return result; } -//------------------------------------------------------------------------------ -// -// DisplayCaptureSettingsDialogBox -// Show OS specific Capture settings. -// Return 0 on success. -//------------------------------------------------------------------------------ int ViEInputManager::DisplayCaptureSettingsDialogBox( - const WebRtc_UWord8* deviceUniqueIdUTF8, - const WebRtc_UWord8* dialogTitleUTF8, - void* parentWindow, - WebRtc_UWord32 positionX, - WebRtc_UWord32 positionY) -{ - assert(_ptrCaptureDeviceInfo); - return _ptrCaptureDeviceInfo->DisplayCaptureSettingsDialogBox( - deviceUniqueIdUTF8, - dialogTitleUTF8, - parentWindow, - positionX, - positionY); -} -// ---------------------------------------------------------------------------- -// CreateCaptureDevice -// -// Creates a capture module for the specified capture device and assigns -// a capture device id for the device -// ---------------------------------------------------------------------------- - -int ViEInputManager::CreateCaptureDevice(const WebRtc_UWord8* deviceUniqueIdUTF8, - const WebRtc_UWord32 deviceUniqueIdUTF8Length, - int& captureId) -{ - WEBRTC_TRACE(webrtc::kTraceInfo, webrtc::kTraceVideo, ViEId(_engineId), - "%s(deviceUniqueId: %s)", __FUNCTION__, deviceUniqueIdUTF8); - CriticalSectionScoped cs(_mapCritsect); - - // Make sure the device is not already allocated - for (MapItem* item = _vieFrameProviderMap.First(); item != NULL; - item = _vieFrameProviderMap.Next(item)) - { - if (item->GetId() >= kViECaptureIdBase && - item->GetId() <= kViECaptureIdMax) // Make sure it is a capture device - { - ViECapturer* vieCapture = static_cast (item->GetItem()); - assert(vieCapture); - if (strncmp((char*) vieCapture->CurrentDeviceName(), - (char*) deviceUniqueIdUTF8, - strlen((char*) vieCapture->CurrentDeviceName())) == 0) - { - return kViECaptureDeviceAlreadyAllocated; - } - } - } - - // Make sure the device name is valid - bool foundDevice = false; - for (WebRtc_UWord32 deviceIndex = 0; - deviceIndex < _ptrCaptureDeviceInfo->NumberOfDevices(); ++deviceIndex) - { - if (deviceUniqueIdUTF8Length >kVideoCaptureUniqueNameLength) - { - // user's string length is longer than the max - return -1; - } - - WebRtc_UWord8 foundName[kVideoCaptureDeviceNameLength] = ""; - WebRtc_UWord8 foundUniqueName[kVideoCaptureUniqueNameLength] = ""; - _ptrCaptureDeviceInfo->GetDeviceName(deviceIndex, foundName, - kVideoCaptureDeviceNameLength, - foundUniqueName, - kVideoCaptureUniqueNameLength); - - if (strncmp((char*) deviceUniqueIdUTF8, (char*) foundUniqueName, - strlen((char*) deviceUniqueIdUTF8)) == 0) - { - WEBRTC_TRACE(webrtc::kTraceDebug, webrtc::kTraceVideo, ViEId(_engineId), - "%s:%d Capture device was found by unique ID: %s. Returning", - __FUNCTION__, __LINE__, deviceUniqueIdUTF8); - foundDevice = true; - break; - } - } - if (!foundDevice) - { - WEBRTC_TRACE(webrtc::kTraceDebug, webrtc::kTraceVideo, ViEId(_engineId), - "%s:%d Capture device NOT found by unique ID: %s. Returning", - __FUNCTION__, __LINE__, deviceUniqueIdUTF8); - return kViECaptureDeviceDoesNotExist; - } - - int newcaptureId = 0; - if (GetFreeCaptureId(newcaptureId) == false) - { - WEBRTC_TRACE(webrtc::kTraceError, webrtc::kTraceVideo, ViEId(_engineId), - "%s: Maximum supported number of capture devices already in use", - __FUNCTION__); - return kViECaptureDeviceMaxNoDevicesAllocated; - } - ViECapturer* vieCapture =ViECapturer::CreateViECapture(newcaptureId, - _engineId, - deviceUniqueIdUTF8, - deviceUniqueIdUTF8Length, - *_moduleProcessThread); - if (vieCapture == NULL) - { - ReturnCaptureId(newcaptureId); - WEBRTC_TRACE(webrtc::kTraceError, webrtc::kTraceVideo, ViEId(_engineId), - "%s: Could not create capture module for %s", __FUNCTION__, - deviceUniqueIdUTF8); - return kViECaptureDeviceUnknownError; - } - - if (_vieFrameProviderMap.Insert(newcaptureId, vieCapture) != 0) - { - ReturnCaptureId(newcaptureId); - WEBRTC_TRACE(webrtc::kTraceError, webrtc::kTraceVideo, ViEId(_engineId), - "%s: Could not insert capture module for %s", __FUNCTION__, - deviceUniqueIdUTF8); - return kViECaptureDeviceUnknownError; - } - captureId = newcaptureId; - WEBRTC_TRACE(webrtc::kTraceInfo, webrtc::kTraceVideo, ViEId(_engineId), - "%s(deviceUniqueId: %s, captureId: %d)", __FUNCTION__, - deviceUniqueIdUTF8, captureId); - - return 0; + const WebRtc_UWord8* device_unique_idUTF8, + const WebRtc_UWord8* dialog_titleUTF8, + void* parent_window, + WebRtc_UWord32 positionX, + WebRtc_UWord32 positionY) { + assert(capture_device_info_); + return capture_device_info_->DisplayCaptureSettingsDialogBox( + device_unique_idUTF8, dialog_titleUTF8, parent_window, positionX, + positionY); } -int ViEInputManager::CreateCaptureDevice(VideoCaptureModule& captureModule, - int& captureId) -{ - WEBRTC_TRACE(webrtc::kTraceInfo, webrtc::kTraceVideo, ViEId(_engineId), "%s", __FUNCTION__); +int ViEInputManager::CreateCaptureDevice( + const WebRtc_UWord8* device_unique_idUTF8, + const WebRtc_UWord32 device_unique_idUTF8Length, + int& capture_id) { + WEBRTC_TRACE(webrtc::kTraceInfo, webrtc::kTraceVideo, ViEId(engine_id_), + "%s(device_unique_id: %s)", __FUNCTION__, device_unique_idUTF8); + CriticalSectionScoped cs(map_cs_); - CriticalSectionScoped cs(_mapCritsect); + // Make sure the device is not already allocated. + for (MapItem* item = vie_frame_provider_map_.First(); item != NULL; + item = vie_frame_provider_map_.Next(item)) { + // Make sure this is a capture device. + if (item->GetId() >= kViECaptureIdBase && + item->GetId() <= kViECaptureIdMax) { + ViECapturer* vie_capture = static_cast(item->GetItem()); + assert(vie_capture); + // TODO(mflodman) Can we change input to avoid this cast? + const char* device_name = + reinterpret_cast(vie_capture->CurrentDeviceName()); + if (strncmp(device_name, + reinterpret_cast(device_unique_idUTF8), + strlen(device_name)) == 0) { + return kViECaptureDeviceAlreadyAllocated; + } + } + } - int newcaptureId = 0; - if (GetFreeCaptureId(newcaptureId) == false) - { - WEBRTC_TRACE(webrtc::kTraceError, webrtc::kTraceVideo, ViEId(_engineId), - "%s: Maximum supported number of capture devices already in use", - __FUNCTION__); - return kViECaptureDeviceMaxNoDevicesAllocated; + // Make sure the device name is valid. + bool found_device = false; + for (WebRtc_UWord32 device_index = 0; + device_index < capture_device_info_->NumberOfDevices(); ++device_index) { + if (device_unique_idUTF8Length > kVideoCaptureUniqueNameLength) { + // User's string length is longer than the max. + return -1; } - ViECapturer* vieCapture = ViECapturer::CreateViECapture(newcaptureId, - _engineId, - captureModule, - *_moduleProcessThread); - if (vieCapture == NULL) - { - ReturnCaptureId(newcaptureId); - WEBRTC_TRACE(webrtc::kTraceError, webrtc::kTraceVideo, ViEId(_engineId), - "%s: Could attach capture module.", __FUNCTION__); - return kViECaptureDeviceUnknownError; - } - if (_vieFrameProviderMap.Insert(newcaptureId, vieCapture) != 0) - { - ReturnCaptureId(newcaptureId); - WEBRTC_TRACE(webrtc::kTraceError, webrtc::kTraceVideo, ViEId(_engineId), - "%s: Could not insert capture module", __FUNCTION__); - return kViECaptureDeviceUnknownError; + WebRtc_UWord8 found_name[kVideoCaptureDeviceNameLength] = ""; + WebRtc_UWord8 found_unique_name[kVideoCaptureUniqueNameLength] = ""; + capture_device_info_->GetDeviceName(device_index, found_name, + kVideoCaptureDeviceNameLength, + found_unique_name, + kVideoCaptureUniqueNameLength); + + // TODO(mflodman) Can we change input to avoid this cast? + const char* cast_id = reinterpret_cast(device_unique_idUTF8); + if (strncmp(cast_id, reinterpret_cast(found_unique_name), + strlen(cast_id)) == 0) { + WEBRTC_TRACE(webrtc::kTraceDebug, webrtc::kTraceVideo, ViEId(engine_id_), + "%s:%d Capture device was found by unique ID: %s. Returning", + __FUNCTION__, __LINE__, device_unique_idUTF8); + found_device = true; + break; } + } + if (!found_device) { + WEBRTC_TRACE(webrtc::kTraceDebug, webrtc::kTraceVideo, ViEId(engine_id_), + "%s:%d Capture device NOT found by unique ID: %s. Returning", + __FUNCTION__, __LINE__, device_unique_idUTF8); + return kViECaptureDeviceDoesNotExist; + } - captureId = newcaptureId; - WEBRTC_TRACE(webrtc::kTraceInfo, webrtc::kTraceVideo, ViEId(_engineId), - "%s, captureId: %d", __FUNCTION__, captureId); - - return 0; + int newcapture_id = 0; + if (GetFreeCaptureId(newcapture_id) == false) { + WEBRTC_TRACE(webrtc::kTraceError, webrtc::kTraceVideo, ViEId(engine_id_), + "%s: Maximum supported number of capture devices already in " + "use", __FUNCTION__); + return kViECaptureDeviceMaxNoDevicesAllocated; + } + ViECapturer* vie_capture = ViECapturer::CreateViECapture( + newcapture_id, engine_id_, device_unique_idUTF8, + device_unique_idUTF8Length, *module_process_thread_); + if (!vie_capture) { + ReturnCaptureId(newcapture_id); + WEBRTC_TRACE(webrtc::kTraceError, webrtc::kTraceVideo, ViEId(engine_id_), + "%s: Could not create capture module for %s", __FUNCTION__, + device_unique_idUTF8); + return kViECaptureDeviceUnknownError; + } + if (vie_frame_provider_map_.Insert(newcapture_id, vie_capture) != 0) { + ReturnCaptureId(newcapture_id); + WEBRTC_TRACE(webrtc::kTraceError, webrtc::kTraceVideo, ViEId(engine_id_), + "%s: Could not insert capture module for %s", __FUNCTION__, + device_unique_idUTF8); + return kViECaptureDeviceUnknownError; + } + capture_id = newcapture_id; + WEBRTC_TRACE(webrtc::kTraceInfo, webrtc::kTraceVideo, ViEId(engine_id_), + "%s(device_unique_id: %s, capture_id: %d)", __FUNCTION__, + device_unique_idUTF8, capture_id); + return 0; } -// ---------------------------------------------------------------------------- -// DestroyCaptureDevice -// -// Releases the capture device with specified id -// ---------------------------------------------------------------------------- - -int ViEInputManager::DestroyCaptureDevice(const int captureId) -{ - WEBRTC_TRACE(webrtc::kTraceInfo, webrtc::kTraceVideo, ViEId(_engineId), - "%s(captureId: %d)", __FUNCTION__, captureId); - - ViECapturer* vieCapture = NULL; - { - // We need exclusive access to the object to delete it - ViEManagerWriteScoped wl(*this); // Take this write lock first since the read lock is taken before _mapCritsect - CriticalSectionScoped cs(_mapCritsect); - - vieCapture = ViECapturePtr(captureId); - if (vieCapture == NULL) - { - // No capture deveice with that id - WEBRTC_TRACE(webrtc::kTraceError, webrtc::kTraceVideo, ViEId(_engineId), - "%s(captureId: %d) - No such capture device id", - __FUNCTION__, captureId); - return -1; - } - WebRtc_UWord32 numCallbacks = vieCapture->NumberOfRegisteredFrameCallbacks(); - if (numCallbacks > 0) - { - WEBRTC_TRACE(webrtc::kTraceWarning, webrtc::kTraceVideo, ViEId(_engineId), - "%s(captureId: %d) - %u registered callbacks when destroying capture device", - __FUNCTION__, captureId, numCallbacks); - } - _vieFrameProviderMap.Erase(captureId); - ReturnCaptureId(captureId); - } // Leave cs before deleting the capture object. This is because deleting the object might cause deletions of renderers so we prefer to not have a lock at that time. - delete vieCapture; - return 0; -} -// ---------------------------------------------------------------------------- -// CreateExternalCaptureDevice -// -// Creates a capture module to be used with external captureing. -// ---------------------------------------------------------------------------- -int ViEInputManager::CreateExternalCaptureDevice(ViEExternalCapture*& externalCapture, - int& captureId) -{ - WEBRTC_TRACE(webrtc::kTraceInfo, webrtc::kTraceVideo, ViEId(_engineId), "%s", +int ViEInputManager::CreateCaptureDevice(VideoCaptureModule& capture_module, + int& capture_id) { + WEBRTC_TRACE(webrtc::kTraceInfo, webrtc::kTraceVideo, ViEId(engine_id_), "%s", __FUNCTION__); - CriticalSectionScoped cs(_mapCritsect); - int newcaptureId = 0; - if (GetFreeCaptureId(newcaptureId) == false) - { - WEBRTC_TRACE( webrtc::kTraceError, webrtc::kTraceVideo, ViEId(_engineId), - "%s: Maximum supported number of capture devices already in use", - __FUNCTION__); - return kViECaptureDeviceMaxNoDevicesAllocated; - } + CriticalSectionScoped cs(map_cs_); + int newcapture_id = 0; + if (!GetFreeCaptureId(newcapture_id)) { + WEBRTC_TRACE(webrtc::kTraceError, webrtc::kTraceVideo, ViEId(engine_id_), + "%s: Maximum supported number of capture devices already in " + "use", __FUNCTION__); + return kViECaptureDeviceMaxNoDevicesAllocated; + } - ViECapturer* vieCapture = ViECapturer::CreateViECapture(newcaptureId, - _engineId, NULL, 0, - *_moduleProcessThread); - if (vieCapture == NULL) - { - ReturnCaptureId(newcaptureId); - WEBRTC_TRACE(webrtc::kTraceError, webrtc::kTraceVideo, ViEId(_engineId), - "%s: Could not create capture module for external capture.", - __FUNCTION__); - return kViECaptureDeviceUnknownError; - } - - if (_vieFrameProviderMap.Insert(newcaptureId, vieCapture) != 0) - { - ReturnCaptureId(newcaptureId); - WEBRTC_TRACE(webrtc::kTraceError, webrtc::kTraceVideo, ViEId(_engineId), - "%s: Could not insert capture module for external capture.", - __FUNCTION__); - return kViECaptureDeviceUnknownError; - } - captureId = newcaptureId; - externalCapture = vieCapture; - WEBRTC_TRACE(webrtc::kTraceInfo, webrtc::kTraceVideo, ViEId(_engineId), - "%s, captureId: %d)", __FUNCTION__, captureId); - return 0; + ViECapturer* vie_capture = ViECapturer::CreateViECapture( + newcapture_id, engine_id_, capture_module, *module_process_thread_); + if (!vie_capture) { + ReturnCaptureId(newcapture_id); + WEBRTC_TRACE(webrtc::kTraceError, webrtc::kTraceVideo, ViEId(engine_id_), + "%s: Could attach capture module.", __FUNCTION__); + return kViECaptureDeviceUnknownError; + } + if (vie_frame_provider_map_.Insert(newcapture_id, vie_capture) != 0) { + ReturnCaptureId(newcapture_id); + WEBRTC_TRACE(webrtc::kTraceError, webrtc::kTraceVideo, ViEId(engine_id_), + "%s: Could not insert capture module", __FUNCTION__); + return kViECaptureDeviceUnknownError; + } + capture_id = newcapture_id; + WEBRTC_TRACE(webrtc::kTraceInfo, webrtc::kTraceVideo, ViEId(engine_id_), + "%s, capture_id: %d", __FUNCTION__, capture_id); + return 0; } -int ViEInputManager::CreateFilePlayer(const WebRtc_Word8* fileNameUTF8, +int ViEInputManager::DestroyCaptureDevice(const int capture_id) { + WEBRTC_TRACE(webrtc::kTraceInfo, webrtc::kTraceVideo, ViEId(engine_id_), + "%s(capture_id: %d)", __FUNCTION__, capture_id); + ViECapturer* vie_capture = NULL; + { + // We need exclusive access to the object to delete it. + // Take this write lock first since the read lock is taken before map_cs_. + ViEManagerWriteScoped wl(*this); + CriticalSectionScoped cs(map_cs_); + + vie_capture = ViECapturePtr(capture_id); + if (!vie_capture) { + WEBRTC_TRACE(webrtc::kTraceError, webrtc::kTraceVideo, ViEId(engine_id_), + "%s(capture_id: %d) - No such capture device id", + __FUNCTION__, capture_id); + return -1; + } + WebRtc_UWord32 num_callbacks = + vie_capture->NumberOfRegisteredFrameCallbacks(); + if (num_callbacks > 0) { + WEBRTC_TRACE(webrtc::kTraceWarning, webrtc::kTraceVideo, + ViEId(engine_id_), "%s(capture_id: %d) - %u registered " + "callbacks when destroying capture device", + __FUNCTION__, capture_id, num_callbacks); + } + vie_frame_provider_map_.Erase(capture_id); + ReturnCaptureId(capture_id); + // Leave cs before deleting the capture object. This is because deleting the + // object might cause deletions of renderers so we prefer to not have a lock + // at that time. + } + delete vie_capture; + return 0; +} + +int ViEInputManager::CreateExternalCaptureDevice( + ViEExternalCapture*& external_capture, + int& capture_id) { + WEBRTC_TRACE(webrtc::kTraceInfo, webrtc::kTraceVideo, ViEId(engine_id_), "%s", + __FUNCTION__); + CriticalSectionScoped cs(map_cs_); + + int newcapture_id = 0; + if (GetFreeCaptureId(newcapture_id) == false) { + WEBRTC_TRACE(webrtc::kTraceError, webrtc::kTraceVideo, ViEId(engine_id_), + "%s: Maximum supported number of capture devices already in " + "use", __FUNCTION__); + return kViECaptureDeviceMaxNoDevicesAllocated; + } + + ViECapturer* vie_capture = ViECapturer::CreateViECapture( + newcapture_id, engine_id_, NULL, 0, *module_process_thread_); + if (!vie_capture) { + ReturnCaptureId(newcapture_id); + WEBRTC_TRACE(webrtc::kTraceError, webrtc::kTraceVideo, ViEId(engine_id_), + "%s: Could not create capture module for external capture.", + __FUNCTION__); + return kViECaptureDeviceUnknownError; + } + + if (vie_frame_provider_map_.Insert(newcapture_id, vie_capture) != 0) { + ReturnCaptureId(newcapture_id); + WEBRTC_TRACE(webrtc::kTraceError, webrtc::kTraceVideo, ViEId(engine_id_), + "%s: Could not insert capture module for external capture.", + __FUNCTION__); + return kViECaptureDeviceUnknownError; + } + capture_id = newcapture_id; + external_capture = vie_capture; + WEBRTC_TRACE(webrtc::kTraceInfo, webrtc::kTraceVideo, ViEId(engine_id_), + "%s, capture_id: %d)", __FUNCTION__, capture_id); + return 0; +} + +int ViEInputManager::CreateFilePlayer(const WebRtc_Word8* file_nameUTF8, const bool loop, - const webrtc::FileFormats fileFormat, - VoiceEngine* vePtr, int& fileId) -{ - WEBRTC_TRACE(webrtc::kTraceInfo, webrtc::kTraceVideo, ViEId(_engineId), - "%s(deviceUniqueId: %s)", __FUNCTION__, fileNameUTF8); + const webrtc::FileFormats file_format, + VoiceEngine* voe_ptr, int& file_id) { + WEBRTC_TRACE(webrtc::kTraceInfo, webrtc::kTraceVideo, ViEId(engine_id_), + "%s(device_unique_id: %s)", __FUNCTION__, file_nameUTF8); - CriticalSectionScoped cs(_mapCritsect); + CriticalSectionScoped cs(map_cs_); + int new_file_id = 0; + if (GetFreeFileId(new_file_id) == false) { + WEBRTC_TRACE(webrtc::kTraceError, webrtc::kTraceVideo, ViEId(engine_id_), + "%s: Maximum supported number of file players already in use", + __FUNCTION__); + return kViEFileMaxNoOfFilesOpened; + } - int newFileId = 0; - if (GetFreeFileId(newFileId) == false) - { - WEBRTC_TRACE(webrtc::kTraceError, webrtc::kTraceVideo, ViEId(_engineId), - "%s: Maximum supported number of file players already in use", - __FUNCTION__); - return kViEFileMaxNoOfFilesOpened; - } + ViEFilePlayer* vie_file_player = ViEFilePlayer::CreateViEFilePlayer( + new_file_id, engine_id_, file_nameUTF8, loop, file_format, *this, + voe_ptr); + if (!vie_file_player) { + ReturnFileId(new_file_id); + WEBRTC_TRACE(webrtc::kTraceError, webrtc::kTraceVideo, ViEId(engine_id_), + "%s: Could not open file %s for playback", __FUNCTION__, + file_nameUTF8); + return kViEFileUnknownError; + } - ViEFilePlayer* vieFilePlayer = ViEFilePlayer::CreateViEFilePlayer( - newFileId, _engineId, fileNameUTF8, - loop, fileFormat, *this, vePtr); - if (vieFilePlayer == NULL) - { - ReturnFileId(newFileId); - WEBRTC_TRACE(webrtc::kTraceError, webrtc::kTraceVideo, ViEId(_engineId), - "%s: Could not open file %s for playback", __FUNCTION__, - fileNameUTF8); - return kViEFileUnknownError; - } + if (vie_frame_provider_map_.Insert(new_file_id, vie_file_player) != 0) { + ReturnCaptureId(new_file_id); + WEBRTC_TRACE(webrtc::kTraceError, webrtc::kTraceVideo, ViEId(engine_id_), + "%s: Could not insert file player for %s", __FUNCTION__, + file_nameUTF8); + delete vie_file_player; + return kViEFileUnknownError; + } - if (_vieFrameProviderMap.Insert(newFileId, vieFilePlayer) != 0) - { - ReturnCaptureId(newFileId); - WEBRTC_TRACE(webrtc::kTraceError, webrtc::kTraceVideo, ViEId(_engineId), - "%s: Could not insert file player for %s", __FUNCTION__, - fileNameUTF8); - delete vieFilePlayer; - return kViEFileUnknownError; - } - - fileId = newFileId; - WEBRTC_TRACE(webrtc::kTraceInfo, webrtc::kTraceVideo, ViEId(_engineId), - "%s(filename: %s, fileId: %d)", __FUNCTION__, fileNameUTF8, - newFileId); - return 0; + file_id = new_file_id; + WEBRTC_TRACE(webrtc::kTraceInfo, webrtc::kTraceVideo, ViEId(engine_id_), + "%s(filename: %s, file_id: %d)", __FUNCTION__, file_nameUTF8, + new_file_id); + return 0; } -int ViEInputManager::DestroyFilePlayer(int fileId) -{ - WEBRTC_TRACE(webrtc::kTraceInfo, webrtc::kTraceVideo, ViEId(_engineId), - "%s(fileId: %d)", __FUNCTION__, fileId); +int ViEInputManager::DestroyFilePlayer(int file_id) { + WEBRTC_TRACE(webrtc::kTraceInfo, webrtc::kTraceVideo, ViEId(engine_id_), + "%s(file_id: %d)", __FUNCTION__, file_id); - ViEFilePlayer* vieFilePlayer = NULL; - { - // We need exclusive access to the object to delete it - ViEManagerWriteScoped wl(*this); // Take this write lock first since the read lock is taken before _mapCritsect + ViEFilePlayer* vie_file_player = NULL; + { + // We need exclusive access to the object to delete it. + // Take this write lock first since the read lock is taken before map_cs_. + ViEManagerWriteScoped wl(*this); - CriticalSectionScoped cs(_mapCritsect); - - vieFilePlayer = ViEFilePlayerPtr(fileId); - if (vieFilePlayer == NULL) - { - // No capture deveice with that id - WEBRTC_TRACE(webrtc::kTraceError, webrtc::kTraceVideo, ViEId(_engineId), - "%s(fileId: %d) - No such file player", - __FUNCTION__, fileId); - - return -1; - } - int numCallbacks = - vieFilePlayer->NumberOfRegisteredFrameCallbacks(); - if (numCallbacks > 0) - { - WEBRTC_TRACE( webrtc::kTraceWarning, webrtc::kTraceVideo, ViEId(_engineId), - "%s(fileId: %d) - %u registered callbacks when destroying file player", - __FUNCTION__, fileId, numCallbacks); - } - _vieFrameProviderMap.Erase(fileId); - ReturnFileId(fileId); - } // Leave cs before deleting the file object. This is because deleting the object might cause deletions of renderers so we prefer to not have a lock at that time. - delete vieFilePlayer; - return 0; + CriticalSectionScoped cs(map_cs_); + vie_file_player = ViEFilePlayerPtr(file_id); + if (!vie_file_player) { + WEBRTC_TRACE(webrtc::kTraceError, webrtc::kTraceVideo, ViEId(engine_id_), + "%s(file_id: %d) - No such file player", __FUNCTION__, + file_id); + return -1; + } + int num_callbacks = vie_file_player->NumberOfRegisteredFrameCallbacks(); + if (num_callbacks > 0) { + WEBRTC_TRACE(webrtc::kTraceWarning, webrtc::kTraceVideo, + ViEId(engine_id_), "%s(file_id: %d) - %u registered " + "callbacks when destroying file player", __FUNCTION__, + file_id, num_callbacks); + } + vie_frame_provider_map_.Erase(file_id); + ReturnFileId(file_id); + // Leave cs before deleting the file object. This is because deleting the + // object might cause deletions of renderers so we prefer to not have a lock + // at that time. + } + delete vie_file_player; + return 0; } -// ============================================================================ -// Private methods -// ============================================================================ +bool ViEInputManager::GetFreeCaptureId(int& freecapture_id) { + WEBRTC_TRACE(webrtc::kTraceInfo, webrtc::kTraceVideo, ViEId(engine_id_), "%s", + __FUNCTION__); + for (int id = 0; id < kViEMaxCaptureDevices; id++) { + if (free_capture_device_id_[id]) { + // We found a free capture device id. + free_capture_device_id_[id] = false; + freecapture_id = id + kViECaptureIdBase; + WEBRTC_TRACE(webrtc::kTraceInfo, webrtc::kTraceVideo, ViEId(engine_id_), + "%s: new id: %d", __FUNCTION__, freecapture_id); + return true; + } + } + return false; +} -// ---------------------------------------------------------------------------- -// GetFreeCaptureId -// -// Gets and allocates a free capture device id. Assumed protected by caller -// ---------------------------------------------------------------------------- +void ViEInputManager::ReturnCaptureId(int capture_id) { + WEBRTC_TRACE(webrtc::kTraceInfo, webrtc::kTraceVideo, ViEId(engine_id_), + "%s(%d)", __FUNCTION__, capture_id); + CriticalSectionScoped cs(map_cs_); + if (capture_id >= kViECaptureIdBase && + capture_id < kViEMaxCaptureDevices + kViECaptureIdBase) { + free_capture_device_id_[capture_id - kViECaptureIdBase] = true; + } + return; +} -// Private, asummed protected -bool ViEInputManager::GetFreeCaptureId(int& freecaptureId) -{ - WEBRTC_TRACE(webrtc::kTraceInfo, webrtc::kTraceVideo, ViEId(_engineId), "%s", +bool ViEInputManager::GetFreeFileId(int& free_file_id) { + WEBRTC_TRACE(webrtc::kTraceInfo, webrtc::kTraceVideo, ViEId(engine_id_), "%s", __FUNCTION__); - for (int id = 0; id < kViEMaxCaptureDevices; id++) - { - if (_freeCaptureDeviceId[id]) - { - // We found a free capture device id - _freeCaptureDeviceId[id] = false; - freecaptureId = id + kViECaptureIdBase; - WEBRTC_TRACE(webrtc::kTraceInfo, webrtc::kTraceVideo, ViEId(_engineId), - "%s: new id: %d", __FUNCTION__, freecaptureId); - return true; - } + for (int id = 0; id < kViEMaxFilePlayers; id++) { + if (free_file_id_[id]) { + // We found a free capture device id. + free_file_id_[id] = false; + free_file_id = id + kViEFileIdBase; + WEBRTC_TRACE(webrtc::kTraceInfo, webrtc::kTraceVideo, ViEId(engine_id_), + "%s: new id: %d", __FUNCTION__, free_file_id); + return true; } - return false; + } + return false; } -// ---------------------------------------------------------------------------- -// ReturnCaptureId -// -// Frees a capture id assigned in GetFreeCaptureId -// ---------------------------------------------------------------------------- +void ViEInputManager::ReturnFileId(int file_id) { + WEBRTC_TRACE(webrtc::kTraceInfo, webrtc::kTraceVideo, ViEId(engine_id_), + "%s(%d)", __FUNCTION__, file_id); -void ViEInputManager::ReturnCaptureId(int captureId) -{ - WEBRTC_TRACE(webrtc::kTraceInfo, webrtc::kTraceVideo, ViEId(_engineId), "%s(%d)", - __FUNCTION__, captureId); - - CriticalSectionScoped cs(_mapCritsect); - if (captureId >= kViECaptureIdBase && - captureId < kViEMaxCaptureDevices + kViECaptureIdBase) - { - _freeCaptureDeviceId[captureId - kViECaptureIdBase] = true; - } - - return; + CriticalSectionScoped cs(map_cs_); + if (file_id >= kViEFileIdBase && + file_id < kViEMaxFilePlayers + kViEFileIdBase) { + free_file_id_[file_id - kViEFileIdBase] = true; + } + return; } -// ---------------------------------------------------------------------------- -// GetFreeFileId -// -// Gets and allocates a free file id. Assumed protected by caller -// ---------------------------------------------------------------------------- - -// Private, asumed protected -bool ViEInputManager::GetFreeFileId(int& freeFileId) -{ - WEBRTC_TRACE(webrtc::kTraceInfo, webrtc::kTraceVideo, ViEId(_engineId), "%s", - __FUNCTION__); - - for (int id = 0; id < kViEMaxFilePlayers; id++) - { - if (_freeFileId[id]) - { - // We found a free capture device id - _freeFileId[id] = false; - freeFileId = id + kViEFileIdBase; - WEBRTC_TRACE(webrtc::kTraceInfo, webrtc::kTraceVideo, ViEId(_engineId), - "%s: new id: %d", __FUNCTION__, freeFileId); - return true; - } - } - return false; -} -// ---------------------------------------------------------------------------- -// ReturnFileId -// -// Frees a file id assigned in GetFreeFileId -// ---------------------------------------------------------------------------- -void ViEInputManager::ReturnFileId(int fileId) -{ - WEBRTC_TRACE(webrtc::kTraceInfo, webrtc::kTraceVideo, ViEId(_engineId), "%s(%d)", - __FUNCTION__, fileId); - - CriticalSectionScoped cs(_mapCritsect); - if (fileId >= kViEFileIdBase && - fileId < kViEMaxFilePlayers + kViEFileIdBase) - { - _freeFileId[fileId - kViEFileIdBase] = true; - } - return; -} - -// ============================================================================ -// Methods used by ViECaptureScoped - -// ---------------------------------------------------------------------------- -// ViECapturePtr -// -// Gets the ViECapturer for the capture device id -// ---------------------------------------------------------------------------- - -ViECapturer* ViEInputManager::ViECapturePtr(int captureId) const -{ - if (!(captureId >= kViECaptureIdBase && - captureId <= kViECaptureIdBase + kViEMaxCaptureDevices)) - return NULL; - - CriticalSectionScoped cs(_mapCritsect); - MapItem* mapItem = _vieFrameProviderMap.Find(captureId); - if (mapItem == NULL) - { - // No ViEEncoder for this channel... - return NULL; - } - ViECapturer* vieCapture = static_cast (mapItem->GetItem()); - return vieCapture; -} - -// ---------------------------------------------------------------------------- -// ViEFrameProvider -// -// Gets the ViEFrameProvider for this capture observer. -// ---------------------------------------------------------------------------- - ViEFrameProviderBase* ViEInputManager::ViEFrameProvider( - const ViEFrameCallback* captureObserver) const -{ + const ViEFrameCallback* capture_observer) const { + assert(capture_observer); + CriticalSectionScoped cs(map_cs_); - assert(captureObserver); - CriticalSectionScoped cs(_mapCritsect); + for (MapItem* provider_item = vie_frame_provider_map_.First(); provider_item + != NULL; provider_item = vie_frame_provider_map_.Next(provider_item)) { + ViEFrameProviderBase* vie_frame_provider = + static_cast(provider_item->GetItem()); + assert(vie_frame_provider != NULL); - for (MapItem* providerItem = _vieFrameProviderMap.First(); providerItem - != NULL; providerItem = _vieFrameProviderMap.Next(providerItem)) - { - ViEFrameProviderBase* vieFrameProvider = static_cast - (providerItem->GetItem()); - assert(vieFrameProvider != NULL); - - if (vieFrameProvider->IsFrameCallbackRegistered(captureObserver)) - { - // We found it - return vieFrameProvider; - } + if (vie_frame_provider->IsFrameCallbackRegistered(capture_observer)) { + // We found it. + return vie_frame_provider; } - // No capture device set for this channel + } + // No capture device set for this channel. + return NULL; +} + +ViEFrameProviderBase* ViEInputManager::ViEFrameProvider(int provider_id) const { + CriticalSectionScoped cs(map_cs_); + MapItem* map_item = vie_frame_provider_map_.Find(provider_id); + if (!map_item) { return NULL; + } + ViEFrameProviderBase* vie_frame_provider = + static_cast(map_item->GetItem()); + return vie_frame_provider; } -// ---------------------------------------------------------------------------- -// ViEFrameProvider -// -// Gets the ViEFrameProvider for this capture observer. -// ---------------------------------------------------------------------------- +ViECapturer* ViEInputManager::ViECapturePtr(int capture_id) const { + if (!(capture_id >= kViECaptureIdBase && + capture_id <= kViECaptureIdBase + kViEMaxCaptureDevices)) + return NULL; -ViEFrameProviderBase* ViEInputManager::ViEFrameProvider(int providerId) const -{ - CriticalSectionScoped cs(_mapCritsect); - MapItem* mapItem = _vieFrameProviderMap.Find(providerId); - if (mapItem == NULL) - { - return NULL; - } - - ViEFrameProviderBase* vieFrameProvider = static_cast - (mapItem->GetItem()); - return vieFrameProvider; + CriticalSectionScoped cs(map_cs_); + MapItem* map_item = vie_frame_provider_map_.Find(capture_id); + if (!map_item) { + return NULL; + } + ViECapturer* vie_capture = static_cast(map_item->GetItem()); + return vie_capture; } -// ---------------------------------------------------------------------------- -// GetViECaptures -// -// Gets the the entire map with GetViECaptures -// ---------------------------------------------------------------------------- -void ViEInputManager::GetViECaptures(MapWrapper& vieCaptureMap) -{ - CriticalSectionScoped cs(_mapCritsect); +void ViEInputManager::GetViECaptures(MapWrapper& vie_capture_map) { + CriticalSectionScoped cs(map_cs_); - if (_vieFrameProviderMap.Size() == 0) - { - // No ViECaptures - return; - } - // Add all items to the map - for (MapItem* item = _vieFrameProviderMap.First(); - item != NULL; - item = _vieFrameProviderMap.Next(item)) - { - vieCaptureMap.Insert(item->GetId(), item->GetItem()); - } + if (vie_frame_provider_map_.Size() == 0) { return; + } + // Add all items to the map. + for (MapItem* item = vie_frame_provider_map_.First(); item != NULL; + item = vie_frame_provider_map_.Next(item)) { + vie_capture_map.Insert(item->GetId(), item->GetItem()); + } + return; } -// ---------------------------------------------------------------------------- -// ViEFilePlayerPtr -// -// Gets the ViEFilePlayer for this fileId -// ---------------------------------------------------------------------------- - -ViEFilePlayer* ViEInputManager::ViEFilePlayerPtr(int fileId) const -{ - if (fileId < kViEFileIdBase || fileId > kViEFileIdMax) - return NULL; - - CriticalSectionScoped cs(_mapCritsect); - MapItem* mapItem = _vieFrameProviderMap.Find(fileId); - if (mapItem == NULL) - { - // No ViEFilePlayer for this fileId... - return NULL; - } - ViEFilePlayer* vieFilePlayer = - static_cast (mapItem->GetItem()); - return vieFilePlayer; +ViEFilePlayer* ViEInputManager::ViEFilePlayerPtr(int file_id) const { + if (file_id < kViEFileIdBase || file_id > kViEFileIdMax) { + return NULL; + } + CriticalSectionScoped cs(map_cs_); + MapItem* map_item = vie_frame_provider_map_.Find(file_id); + if (!map_item) { + return NULL; + } + ViEFilePlayer* vie_file_player = + static_cast(map_item->GetItem()); + return vie_file_player; } -// ---------------------------------------------------------------------------- -// ViEInputManagerScoped -// -// Provides protected access to ViEInputManater -// ---------------------------------------------------------------------------- ViEInputManagerScoped::ViEInputManagerScoped( - const ViEInputManager& vieInputManager) - : ViEManagerScopedBase(vieInputManager) -{ + const ViEInputManager& vie_input_manager) + : ViEManagerScopedBase(vie_input_manager) { } -ViECapturer* ViEInputManagerScoped::Capture(int captureId) const -{ - return static_cast - (vie_manager_)->ViECapturePtr(captureId); + +ViECapturer* ViEInputManagerScoped::Capture(int capture_id) const { + return static_cast(vie_manager_)->ViECapturePtr( + capture_id); } + ViEFrameProviderBase* ViEInputManagerScoped::FrameProvider( - const ViEFrameCallback* captureObserver) const -{ - return static_cast - (vie_manager_)->ViEFrameProvider(captureObserver); -} -ViEFrameProviderBase* ViEInputManagerScoped::FrameProvider(int providerId) const -{ - return static_cast - (vie_manager_)->ViEFrameProvider( providerId); + const ViEFrameCallback* capture_observer) const { + return static_cast(vie_manager_)->ViEFrameProvider( + capture_observer); } -ViEFilePlayer* ViEInputManagerScoped::FilePlayer(int fileId) const -{ - return static_cast - (vie_manager_)->ViEFilePlayerPtr(fileId); +ViEFrameProviderBase* ViEInputManagerScoped::FrameProvider( + int provider_id) const { + return static_cast(vie_manager_)->ViEFrameProvider( + provider_id); } -} // namespace webrtc +ViEFilePlayer* ViEInputManagerScoped::FilePlayer(int file_id) const { + return static_cast(vie_manager_)->ViEFilePlayerPtr( + file_id); +} + +} // namespace webrtc diff --git a/src/video_engine/vie_input_manager.h b/src/video_engine/vie_input_manager.h index ec4a81a48..ba4d08793 100644 --- a/src/video_engine/vie_input_manager.h +++ b/src/video_engine/vie_input_manager.h @@ -8,114 +8,135 @@ * be found in the AUTHORS file in the root of the source tree. */ -/* - * vie_input_manager.h - */ +#ifndef WEBRTC_VIDEO_ENGINE_VIE_INPUT_MANAGER_H_ +#define WEBRTC_VIDEO_ENGINE_VIE_INPUT_MANAGER_H_ -#ifndef WEBRTC_VIDEO_ENGINE_MAIN_SOURCE_VIE_INPUT_MANAGER_H_ -#define WEBRTC_VIDEO_ENGINE_MAIN_SOURCE_VIE_INPUT_MANAGER_H_ - -#include "vie_defines.h" +#include "modules/video_capture/main/interface/video_capture.h" +#include "system_wrappers/interface/map_wrapper.h" #include "typedefs.h" -#include "map_wrapper.h" -#include "video_capture.h" -#include "vie_manager_base.h" -#include "vie_frame_provider_base.h" -#include "vie_capture.h" - -class ViEExternalCapture; +#include "video_engine/main/interface/vie_capture.h" +#include "video_engine/vie_defines.h" +#include "video_engine/vie_frame_provider_base.h" +#include "video_engine/vie_manager_base.h" namespace webrtc { + class CriticalSectionWrapper; class ProcessThread; class RWLockWrapper; class ViECapturer; +class ViEExternalCapture; class ViEFilePlayer; class VoiceEngine; -class ViEInputManager: private ViEManagerBase -{ - friend class ViEInputManagerScoped; -public: - ViEInputManager(int engineId); - ~ViEInputManager(); +class ViEInputManager : private ViEManagerBase { + friend class ViEInputManagerScoped; + public: + explicit ViEInputManager(int engine_id); + ~ViEInputManager(); - void SetModuleProcessThread(ProcessThread& moduleProcessThread); + void SetModuleProcessThread(ProcessThread& module_process_thread); - // Capture device information - int NumberOfCaptureDevices(); - int GetDeviceName(WebRtc_UWord32 deviceNumber, - WebRtc_UWord8* deviceNameUTF8, - WebRtc_UWord32 deviceNameLength, - WebRtc_UWord8* deviceUniqueIdUTF8, - WebRtc_UWord32 deviceUniqueIdUTF8Length); - int NumberOfCaptureCapabilities(const WebRtc_UWord8* deviceUniqueIdUTF8); - int GetCaptureCapability(const WebRtc_UWord8* deviceUniqueIdUTF8, - const WebRtc_UWord32 deviceCapabilityNumber, - CaptureCapability& capability); - int DisplayCaptureSettingsDialogBox(const WebRtc_UWord8* deviceUniqueIdUTF8, - const WebRtc_UWord8* dialogTitleUTF8, - void* parentWindow, - WebRtc_UWord32 positionX, - WebRtc_UWord32 positionY); - int GetOrientation(const WebRtc_UWord8* deviceUniqueIdUTF8, - RotateCapturedFrame &orientation); + // Returns number of capture devices. + int NumberOfCaptureDevices(); - // Create/delete Capture device settings - // Return zero on success. A ViEError on failure. - int CreateCaptureDevice(const WebRtc_UWord8* deviceUniqueIdUTF8, - const WebRtc_UWord32 deviceUniqueIdUTF8Length, - int& captureId); - int CreateCaptureDevice(VideoCaptureModule& captureModule, - int& captureId); - int CreateExternalCaptureDevice(ViEExternalCapture*& externalCapture, - int& captureId); - int DestroyCaptureDevice(int captureId); + // Gets name and id for a capture device. + int GetDeviceName(WebRtc_UWord32 device_number, + WebRtc_UWord8* device_nameUTF8, + WebRtc_UWord32 device_name_length, + WebRtc_UWord8* device_unique_idUTF8, + WebRtc_UWord32 device_unique_idUTF8Length); - int CreateFilePlayer(const WebRtc_Word8* fileNameUTF8, const bool loop, - const webrtc::FileFormats fileFormat, VoiceEngine* vePtr, - int& fileId); - int DestroyFilePlayer(int fileId); + // Returns the number of capture capabilities for a specified device. + int NumberOfCaptureCapabilities(const WebRtc_UWord8* device_unique_idUTF8); -private: - bool GetFreeCaptureId(int& freecaptureId); - void ReturnCaptureId(int captureId); - bool GetFreeFileId(int& freeFileId); - void ReturnFileId(int fileId); + // Gets a specific capability for a capture device. + int GetCaptureCapability(const WebRtc_UWord8* device_unique_idUTF8, + const WebRtc_UWord32 device_capability_number, + CaptureCapability& capability); - ViEFrameProviderBase* ViEFrameProvider(const ViEFrameCallback* - captureObserver) const; - ViEFrameProviderBase* ViEFrameProvider(int providerId) const; + // Show OS specific Capture settings. + int DisplayCaptureSettingsDialogBox(const WebRtc_UWord8* device_unique_idUTF8, + const WebRtc_UWord8* dialog_titleUTF8, + void* parent_window, + WebRtc_UWord32 positionX, + WebRtc_UWord32 positionY); + int GetOrientation(const WebRtc_UWord8* device_unique_idUTF8, + RotateCapturedFrame& orientation); - ViECapturer* ViECapturePtr(int captureId) const; - void GetViECaptures(MapWrapper& vieCaptureMap); + // Creates a capture module for the specified capture device and assigns + // a capture device id for the device. + // Return zero on success, ViEError on failure. + int CreateCaptureDevice(const WebRtc_UWord8* device_unique_idUTF8, + const WebRtc_UWord32 device_unique_idUTF8Length, + int& capture_id); + int CreateCaptureDevice(VideoCaptureModule& capture_module, + int& capture_id); + int CreateExternalCaptureDevice(ViEExternalCapture*& external_capture, + int& capture_id); + int DestroyCaptureDevice(int capture_id); - ViEFilePlayer* ViEFilePlayerPtr(int fileId) const; + int CreateFilePlayer(const WebRtc_Word8* file_nameUTF8, const bool loop, + const FileFormats file_format, + VoiceEngine* voe_ptr, + int& file_id); + int DestroyFilePlayer(int file_id); - // Members - int _engineId; - CriticalSectionWrapper& _mapCritsect; - MapWrapper _vieFrameProviderMap; + private: + // Gets and allocates a free capture device id. Assumed protected by caller. + bool GetFreeCaptureId(int& freecapture_id); - // Capture devices - VideoCaptureModule::DeviceInfo* _ptrCaptureDeviceInfo; - int _freeCaptureDeviceId[kViEMaxCaptureDevices]; - //File Players - int _freeFileId[kViEMaxFilePlayers]; - //uses - ProcessThread* _moduleProcessThread; + // Frees a capture id assigned in GetFreeCaptureId. + void ReturnCaptureId(int capture_id); + + // Gets and allocates a free file id. Assumed protected by caller. + bool GetFreeFileId(int& free_file_id); + + // Frees a file id assigned in GetFreeFileId. + void ReturnFileId(int file_id); + + // Gets the ViEFrameProvider for this capture observer. + ViEFrameProviderBase* ViEFrameProvider( + const ViEFrameCallback* capture_observer) const; + + // Gets the ViEFrameProvider for this capture observer. + ViEFrameProviderBase* ViEFrameProvider(int provider_id) const; + + // Gets the ViECapturer for the capture device id. + ViECapturer* ViECapturePtr(int capture_id) const; + + // Gets the the entire map with GetViECaptures. + void GetViECaptures(MapWrapper& vie_capture_map); + + // Gets the ViEFilePlayer for this file_id. + ViEFilePlayer* ViEFilePlayerPtr(int file_id) const; + + int engine_id_; + CriticalSectionWrapper& map_cs_; + MapWrapper vie_frame_provider_map_; + + // Capture devices. + VideoCaptureModule::DeviceInfo* capture_device_info_; + int free_capture_device_id_[kViEMaxCaptureDevices]; + + // File Players. + int free_file_id_[kViEMaxFilePlayers]; + + ProcessThread* module_process_thread_; }; -class ViEInputManagerScoped: private ViEManagerScopedBase -{ -public: - ViEInputManagerScoped(const ViEInputManager& vieInputManager); +// Provides protected access to ViEInputManater. +class ViEInputManagerScoped: private ViEManagerScopedBase { + public: + explicit ViEInputManagerScoped(const ViEInputManager& vie_input_manager); - ViECapturer* Capture(int captureId) const; - ViEFilePlayer* FilePlayer(int fileId) const; - ViEFrameProviderBase* FrameProvider(int providerId) const; - ViEFrameProviderBase* FrameProvider(const ViEFrameCallback* - captureObserver) const; + ViECapturer* Capture(int capture_id) const; + ViEFilePlayer* FilePlayer(int file_id) const; + ViEFrameProviderBase* FrameProvider(int provider_id) const; + ViEFrameProviderBase* FrameProvider(const ViEFrameCallback* + capture_observer) const; }; -} // namespace webrtc -#endif // WEBRTC_VIDEO_ENGINE_MAIN_SOURCE_VIE_INPUT_MANAGER_H_ + +} // namespace webrtc + +#endif // WEBRTC_VIDEO_ENGINE_VIE_INPUT_MANAGER_H_