diff --git a/daemon/src/dbus/configurationmanager.cpp b/daemon/src/dbus/configurationmanager.cpp index 70098b542ab56a7c60bf0cfbad62a0a983baf08d..b2070e852aa7cfd53d062a7f7e271b0ebcf9009d 100644 --- a/daemon/src/dbus/configurationmanager.cpp +++ b/daemon/src/dbus/configurationmanager.cpp @@ -46,7 +46,7 @@ const char* ConfigurationManager::SERVER_PATH = "/org/sflphone/SFLphone/ConfigurationManager"; ConfigurationManager::ConfigurationManager(DBus::Connection& connection) : - DBus::ObjectAdaptor(connection, SERVER_PATH) + DBus::ObjectAdaptor(connection, SERVER_PATH), preview_() {} std::map<std::string, std::string> ConfigurationManager::getIp2IpDetails() diff --git a/daemon/src/dbus/configurationmanager.h b/daemon/src/dbus/configurationmanager.h index 4fcb9a2b008d7111518099047cab0edd9dff5b68..0000b9a52db2544636d92711f9ba64ae4030cb32 100644 --- a/daemon/src/dbus/configurationmanager.h +++ b/daemon/src/dbus/configurationmanager.h @@ -63,7 +63,6 @@ class ConfigurationManager public DBus::IntrospectableAdaptor, public DBus::ObjectAdaptor { private: - std::vector<std::string> shortcutsKeys; // FIXME: this probably shouldn't live here std::tr1::shared_ptr<sfl_video::VideoPreview> preview_; diff --git a/daemon/src/managerimpl.cpp b/daemon/src/managerimpl.cpp index 7842120da183532e59d9e8b323c0d98466262239..d21a55e723e83023307d03c44329439dabf266e2 100644 --- a/daemon/src/managerimpl.cpp +++ b/daemon/src/managerimpl.cpp @@ -71,7 +71,7 @@ ManagerImpl::ManagerImpl() : preferences(), voipPreferences(), addressbookPreference(), - hookPreference(), audioPreference(), shortcutPreferences(), + hookPreference(), audioPreference(), videoPreference(), shortcutPreferences(), hasTriedToRegister_(false), audioCodecFactory(), dbus_(), config_(), currentCallId_(), currentCallMutex_(), audiodriver_(0), dtmfKey_(0), toneMutex_(), telephoneTone_(0), audiofile_(0), speakerVolume_(0), micVolume_(0), diff --git a/daemon/src/preferences.cpp b/daemon/src/preferences.cpp index 72cca4cc2b133b45c1ad939d30b6aa9a04995bf2..b50f6cd3978a7fc071dbeb3862abf74f7074d574 100644 --- a/daemon/src/preferences.cpp +++ b/daemon/src/preferences.cpp @@ -409,9 +409,9 @@ void AudioPreference::unserialize(Conf::MappingNode *map) } VideoPreference::VideoPreference() : - device_(), channel_(), size_(), rate_() + v4l2_list_(0), device_(), channel_(), size_(), rate_() { - v4l2_list_ = new VideoV4l2List(); + v4l2_list_ = new VideoV4l2ListThread(); v4l2_list_->start(); } diff --git a/daemon/src/preferences.h b/daemon/src/preferences.h index f072756240f62393c2f931ce59e91fc097c94e6c..3f072dddda30fd5a21598358d92a41ea3d8394fe 100644 --- a/daemon/src/preferences.h +++ b/daemon/src/preferences.h @@ -636,9 +636,10 @@ class VideoPreference : public Serializable private: + NON_COPYABLE(VideoPreference); // V4L2 devices - sfl_video::VideoV4l2List *v4l2_list_; + sfl_video::VideoV4l2ListThread *v4l2_list_; std::string device_; std::string channel_; diff --git a/daemon/src/video/video_preview.cpp b/daemon/src/video/video_preview.cpp index 958232faf52a31d018121e47ef7f6df39dc7d08b..b1524437f007bd198a6d1877d71c117b6bf32bf2 100644 --- a/daemon/src/video/video_preview.cpp +++ b/daemon/src/video/video_preview.cpp @@ -35,7 +35,9 @@ namespace sfl_video { -VideoPreview::VideoPreview(const std::map<std::string, std::string> &args) : args_(args) {} +VideoPreview::VideoPreview(const std::map<std::string, std::string> &args) : + receiveThread_(), args_(args), shmKey_(0), semKey_(0), videoBufferSize_(0) + {} bool VideoPreview::start() { diff --git a/daemon/src/video/video_preview.h b/daemon/src/video/video_preview.h index 8b46cf61237a3adb50a07440497bb20f3655c05c..f7d9d84eaad3fad32fdbc5824b5d3a9a57d90721 100644 --- a/daemon/src/video/video_preview.h +++ b/daemon/src/video/video_preview.h @@ -56,7 +56,6 @@ class VideoPreview { int semKey_; int videoBufferSize_; }; - } #endif // __VIDEO_PREVIEW_H__ diff --git a/daemon/src/video/video_receive_thread.cpp b/daemon/src/video/video_receive_thread.cpp index f0a4c6ab013572d5f9bc2b7b0d46d5ec08cea253..7b52eaa8485f36bcf4811b2c574797501b8d472c 100644 --- a/daemon/src/video/video_receive_thread.cpp +++ b/daemon/src/video/video_receive_thread.cpp @@ -379,11 +379,14 @@ void VideoReceiveThread::createScalingContext() } VideoReceiveThread::VideoReceiveThread(const std::map<std::string, std::string> &args) : args_(args), + test_source_(args_["input"] == "SFLTEST"), + frameNumber_(0), shmBuffer_(0), shmID_(-1), semSetID_(-1), shmKey_(-1), semKey_(-1), + videoBufferSize_(0), decoderCtx_(0), rawFrame_(0), scaledPicture_(0), @@ -391,9 +394,10 @@ VideoReceiveThread::VideoReceiveThread(const std::map<std::string, std::string> inputCtx_(0), imgConvertCtx_(0), dstWidth_(-1), - dstHeight_(-1) + dstHeight_(-1), + shmReady_(), + sdpFilename_() { - test_source_ = (args_["input"] == "SFLTEST"); setCancel(cancelDeferred); } diff --git a/daemon/src/video/video_receive_thread.h b/daemon/src/video/video_receive_thread.h index c9be1a835c047671a710257c9aa13c6ad4c5dab5..0cc147f0b6f42f1c23188ab2d339ace825a716e7 100644 --- a/daemon/src/video/video_receive_thread.h +++ b/daemon/src/video/video_receive_thread.h @@ -34,7 +34,8 @@ #include <cc++/thread.h> #include <map> #include <string> -#include <limits.h> +#include <climits> +#include "noncopyable.h" class SwsContext; class AVCodecContext; @@ -45,9 +46,10 @@ class AVFrame; namespace sfl_video { class VideoReceiveThread : public ost::Thread { private: + NON_COPYABLE(VideoReceiveThread); + std::map<std::string, std::string> args_; bool test_source_; unsigned frameNumber_; - std::map<std::string, std::string> args_; /*-------------------------------------------------------------*/ /* These variables should be used in thread (i.e. run()) only! */ @@ -84,9 +86,9 @@ class VideoReceiveThread : public ost::Thread { virtual void run(); void waitForShm(); - int getShmKey() { return shmKey_; } - int getSemKey() { return semKey_; } - int getVideoBufferSize(void) { return videoBufferSize_; } + int getShmKey() const { return shmKey_; } + int getSemKey() const { return semKey_; } + int getVideoBufferSize() const { return videoBufferSize_; } }; } diff --git a/daemon/src/video/video_rtp_session.cpp b/daemon/src/video/video_rtp_session.cpp index fd39b10f745e1ab42f6b0e23163b0ee7b15e6897..0318f2ee35947f09001bdba6b055605da4dbafe8 100644 --- a/daemon/src/video/video_rtp_session.cpp +++ b/daemon/src/video/video_rtp_session.cpp @@ -44,7 +44,8 @@ namespace sfl_video { -VideoRtpSession::VideoRtpSession() : sending_(true), receiving_(true) +VideoRtpSession::VideoRtpSession() : sendThread_(), receiveThread_(), + txArgs_(), rxArgs_(), sending_(true), receiving_(true) { txArgs_ = Manager::instance().videoPreference.getVideoSettings(); txArgs_["bitrate"] = "500000"; @@ -52,7 +53,8 @@ VideoRtpSession::VideoRtpSession() : sending_(true), receiving_(true) VideoRtpSession::VideoRtpSession(const std::map<std::string, std::string> &txArgs, const std::map<std::string, std::string> &rxArgs) : - txArgs_(txArgs), rxArgs_(rxArgs), sending_(true), receiving_(true) + sendThread_(), receiveThread_(), txArgs_(txArgs), rxArgs_(rxArgs), + sending_(true), receiving_(true) {} void VideoRtpSession::updateSDP(const Sdp &sdp) diff --git a/daemon/src/video/video_send_thread.cpp b/daemon/src/video/video_send_thread.cpp index ab9deac208adfbac7a65c8a0a89da277222644ca..eda8dd82e3c8eb2f4d99344f16ff1351dabeac62 100644 --- a/daemon/src/video/video_send_thread.cpp +++ b/daemon/src/video/video_send_thread.cpp @@ -303,6 +303,8 @@ void VideoSendThread::createScalingContext() } VideoSendThread::VideoSendThread(const std::map<std::string, std::string> &args) : + test_source_(false), + sdpReady_(), args_(args), scaledPictureBuf_(0), outbuf_(0), @@ -316,7 +318,7 @@ VideoSendThread::VideoSendThread(const std::map<std::string, std::string> &args) inputCtx_(0), outputCtx_(0), imgConvertCtx_(0), - sdp_("") + sdp_() { test_source_ = (args_["input"] == "SFLTEST"); setCancel(cancelDeferred); diff --git a/daemon/src/video/video_send_thread.h b/daemon/src/video/video_send_thread.h index 997f42421e041dfa2624406a13e8a44058480150..74424aa67d8b56e2017b40d53ab4a3d9670eb07c 100644 --- a/daemon/src/video/video_send_thread.h +++ b/daemon/src/video/video_send_thread.h @@ -34,6 +34,7 @@ #include <cc++/thread.h> #include <map> #include <string> +#include "noncopyable.h" class SwsContext; class AVCodecContext; @@ -46,12 +47,13 @@ namespace sfl_video { class VideoSendThread : public ost::Thread { private: - bool test_source_; + NON_COPYABLE(VideoSendThread); void forcePresetX264(); void print_and_save_sdp(); void setup(); void prepareEncoderContext(AVCodec *encoder); void createScalingContext(); + bool test_source_; ost::Event sdpReady_; std::map<std::string, std::string> args_; diff --git a/daemon/src/video/video_v4l2.cpp b/daemon/src/video/video_v4l2.cpp index a92870ed13a12b22f42a19bedfda3a367d0068d1..422cd204a8b68749c67bbc29a7ed377a140ecf75 100644 --- a/daemon/src/video/video_v4l2.cpp +++ b/daemon/src/video/video_v4l2.cpp @@ -125,18 +125,19 @@ static unsigned int pixelformat_score(unsigned pixelformat) return UINT_MAX - 1; } -VideoV4l2Size::VideoV4l2Size(unsigned height, unsigned width) : height(height), width(width) {} +VideoV4l2Size::VideoV4l2Size(unsigned height, unsigned width) : + height(height), width(width), rates_() {} std::vector<std::string> VideoV4l2Size::getRateList() { std::vector<std::string> v; std::stringstream ss; - size_t n = rates.size(); + size_t n = rates_.size(); unsigned i; for (i = 0 ; i < n ; i++) { std::stringstream ss; - ss << rates[i]; + ss << rates_[i]; v.push_back(ss.str()); } @@ -144,10 +145,10 @@ std::vector<std::string> VideoV4l2Size::getRateList() } -void VideoV4l2Size::GetFrameRates(int fd, unsigned int pixel_format) +void VideoV4l2Size::getFrameRates(int fd, unsigned int pixel_format) { if (fd == -1) { // SFL_TEST - rates.push_back(30); + rates_.push_back(30); return; } @@ -158,7 +159,7 @@ void VideoV4l2Size::GetFrameRates(int fd, unsigned int pixel_format) frmival.height = height; if (ioctl(fd, VIDIOC_ENUM_FRAMEINTERVALS, &frmival)) { - rates.push_back(25); + rates_.push_back(25); ERROR("could not query frame interval for size"); return; } @@ -166,47 +167,47 @@ void VideoV4l2Size::GetFrameRates(int fd, unsigned int pixel_format) switch(frmival.type) { case V4L2_FRMIVAL_TYPE_DISCRETE: do { - rates.push_back(frmival.discrete.denominator/frmival.discrete.numerator); + rates_.push_back(frmival.discrete.denominator/frmival.discrete.numerator); frmival.index++; } while (!ioctl(fd, VIDIOC_ENUM_FRAMEINTERVALS, &frmival)); break; case V4L2_FRMIVAL_TYPE_CONTINUOUS: - rates.push_back(25); + rates_.push_back(25); // TODO ERROR("Continuous Frame Intervals not supported"); break; case V4L2_FRMIVAL_TYPE_STEPWISE: - rates.push_back(25); + rates_.push_back(25); // TODO ERROR("Stepwise Frame Intervals not supported"); break; } } -VideoV4l2Channel::VideoV4l2Channel(unsigned idx, const char *s) : idx(idx), name(s) { } +VideoV4l2Channel::VideoV4l2Channel(unsigned idx, const char *s) : idx(idx), + name(s), sizes_() {} -void VideoV4l2Channel::SetFourcc(unsigned code) +void VideoV4l2Channel::setFourcc(unsigned code) { - fourcc[0] = code; - fourcc[1] = code >> 8; - fourcc[2] = code >> 16; - fourcc[3] = code >> 24; - fourcc[4] = '\0'; + fourcc_[0] = code; + fourcc_[1] = code >> 8; + fourcc_[2] = code >> 16; + fourcc_[3] = code >> 24; + fourcc_[4] = '\0'; } -const char * VideoV4l2Channel::GetFourcc() +const char * VideoV4l2Channel::getFourcc() { - return fourcc; + return fourcc_; } -std::vector<std::string> VideoV4l2Channel::getSizeList(void) +std::vector<std::string> VideoV4l2Channel::getSizeList() { std::vector<std::string> v; - size_t n = sizes.size(); - unsigned i; - for (i = 0 ; i < n ; i++) { - VideoV4l2Size &size = sizes[i]; + size_t n = sizes_.size(); + for (size_t i = 0 ; i < n ; ++i) { + VideoV4l2Size &size = sizes_[i]; std::stringstream ss; ss << size.width << "x" << size.height; v.push_back(ss.str()); @@ -217,12 +218,12 @@ std::vector<std::string> VideoV4l2Channel::getSizeList(void) -unsigned int VideoV4l2Channel::GetSizes(int fd, unsigned int pixelformat) +unsigned int VideoV4l2Channel::getSizes(int fd, unsigned int pixelformat) { if (fd == -1) { //SFL_TEST VideoV4l2Size s(288, 352); - s.GetFrameRates(-1, 0); - sizes.push_back(s); + s.getFrameRates(-1, 0); + sizes_.push_back(s); return 0; } @@ -237,9 +238,9 @@ unsigned int VideoV4l2Channel::GetSizes(int fd, unsigned int pixelformat) case V4L2_FRMSIZE_TYPE_DISCRETE: do { VideoV4l2Size size(frmsize.discrete.height, frmsize.discrete.width); - size.GetFrameRates(fd, frmsize.pixel_format); - sizes.push_back(size); - frmsize.index++; + size.getFrameRates(fd, frmsize.pixel_format); + sizes_.push_back(size); + ++frmsize.index; } while (!ioctl(fd, VIDIOC_ENUM_FRAMESIZES, &frmsize)); return pixelformat; @@ -263,13 +264,13 @@ fallback: throw std::runtime_error("Couldnt get format"); VideoV4l2Size size(fmt.fmt.pix.height, fmt.fmt.pix.width); - size.GetFrameRates(fd, fmt.fmt.pix.pixelformat); - sizes.push_back(size); + size.getFrameRates(fd, fmt.fmt.pix.pixelformat); + sizes_.push_back(size); return fmt.fmt.pix.pixelformat; } -void VideoV4l2Channel::GetFormat(int fd) +void VideoV4l2Channel::getFormat(int fd) { if (ioctl(fd, VIDIOC_S_INPUT, &idx)) throw std::runtime_error("VIDIOC_S_INPUT failed"); @@ -300,38 +301,34 @@ void VideoV4l2Channel::GetFormat(int fd) throw std::runtime_error("Could not enumerate formats"); fmt.index = best_idx; - pixelformat = GetSizes(fd, pixelformat); + pixelformat = getSizes(fd, pixelformat); - SetFourcc(pixelformat); + setFourcc(pixelformat); } VideoV4l2Size VideoV4l2Channel::getSize(const std::string &name) { - for (size_t i = 0; i < sizes.size(); i++) { + for (size_t i = 0; i < sizes_.size(); ++i) { std::stringstream ss; - ss << sizes[i].width << "x" << sizes[i].height; + ss << sizes_[i].width << "x" << sizes_[i].height; if (ss.str() == name) - return sizes[i]; + return sizes_[i]; } - return sizes.back(); + return sizes_.back(); } -VideoV4l2Device::VideoV4l2Device(int fd, const std::string &device) +VideoV4l2Device::VideoV4l2Device(int fd, const std::string &device) : + device(device), name("TEST"), channels() { if (fd == -1) { VideoV4l2Channel c(0, "#^&"); - c.GetSizes(-1, 0); + c.getSizes(-1, 0); channels.push_back(c); - name = "TEST"; - - this->device = device; return; } - unsigned idx; - v4l2_capability cap; if (ioctl(fd, VIDIOC_QUERYCAP, &cap)) throw std::runtime_error("could not query capabilities"); @@ -343,6 +340,7 @@ VideoV4l2Device::VideoV4l2Device(int fd, const std::string &device) v4l2_input input; memset(&input, 0x0, sizeof input); + unsigned idx; input.index = idx = 0; while (!ioctl(fd, VIDIOC_ENUMINPUT, &input)) { if (idx != input.index) @@ -350,23 +348,20 @@ VideoV4l2Device::VideoV4l2Device(int fd, const std::string &device) if (input.type & V4L2_INPUT_TYPE_CAMERA) { VideoV4l2Channel channel(idx, (const char*)input.name); - channel.GetFormat(fd); - + channel.getFormat(fd); channels.push_back(channel); } input.index = ++idx; } - this->device = device; } -std::vector<std::string> VideoV4l2Device::getChannelList(void) +std::vector<std::string> VideoV4l2Device::getChannelList() const { std::vector<std::string> v; size_t n = channels.size(); - unsigned i; - for (i = 0 ; i < n ; i++) + for (size_t i = 0 ; i < n ; i++) v.push_back(channels[i].name); return v; diff --git a/daemon/src/video/video_v4l2.h b/daemon/src/video/video_v4l2.h index 68497c8a9ab0bd17d1d6d1ffef028c0e4d85d64e..7f319c0b7bc1ee65a55849ded6cff3ec4f98734b 100644 --- a/daemon/src/video/video_v4l2.h +++ b/daemon/src/video/video_v4l2.h @@ -52,14 +52,14 @@ class VideoV4l2Size { /** * @throw std::runtime_error */ - void GetFrameRates(int fd, unsigned int pixel_format); + void getFrameRates(int fd, unsigned int pixel_format); std::vector<std::string> getRateList(); unsigned height; unsigned width; private: - std::vector<float> rates; + std::vector<float> rates_; }; class VideoV4l2Channel { @@ -69,24 +69,24 @@ class VideoV4l2Channel { /** * @throw std::runtime_error */ - void GetFormat(int fd); + void getFormat(int fd); /** * @throw std::runtime_error */ - unsigned int GetSizes(int fd, unsigned int pixel_format); + unsigned int getSizes(int fd, unsigned int pixel_format); - void SetFourcc(unsigned code); - const char * GetFourcc(); + void setFourcc(unsigned code); + const char * getFourcc(); - std::vector<std::string> getSizeList(void); + std::vector<std::string> getSizeList(); VideoV4l2Size getSize(const std::string &name); unsigned idx; std::string name; private: - std::vector<VideoV4l2Size> sizes; - char fourcc[5]; + std::vector<VideoV4l2Size> sizes_; + char fourcc_[5]; }; class VideoV4l2Device { @@ -99,7 +99,7 @@ class VideoV4l2Device { std::string device; std::string name; - std::vector<std::string> getChannelList(void); + std::vector<std::string> getChannelList() const; VideoV4l2Channel &getChannel(const std::string &name); diff --git a/daemon/src/video/video_v4l2_list.cpp b/daemon/src/video/video_v4l2_list.cpp index 798896661852501bc7ec89e337700ea33da37af4..3747c38594e9a5781535e3eee794abee0ee8c494 100644 --- a/daemon/src/video/video_v4l2_list.cpp +++ b/daemon/src/video/video_v4l2_list.cpp @@ -62,53 +62,52 @@ namespace sfl_video { static int is_v4l2(struct udev_device *dev) { - const char *version; - version = udev_device_get_property_value (dev, "ID_V4L_VERSION"); + const char *version = udev_device_get_property_value(dev, "ID_V4L_VERSION"); /* we do not support video4linux 1 */ - return version && strcmp (version, "1"); + return version and strcmp(version, "1"); } -VideoV4l2List::VideoV4l2List() : _udev_mon(NULL) +VideoV4l2ListThread::VideoV4l2ListThread() : devices_(), mutex_(), udev_(0), udev_mon_(0) { - struct udev_list_entry *devlist; - struct udev_enumerate *devenum; + udev_list_entry *devlist; + udev_enumerate *devenum; addDevice("SFLTEST"); - _udev = udev_new(); - if (!_udev) + udev_ = udev_new(); + if (!udev_) goto udev_failed; - _udev_mon = udev_monitor_new_from_netlink (_udev, "udev"); - if (!_udev_mon) + udev_mon_ = udev_monitor_new_from_netlink(udev_, "udev"); + if (!udev_mon_) goto udev_failed; - if (udev_monitor_filter_add_match_subsystem_devtype (_udev_mon, "video4linux", NULL)) + if (udev_monitor_filter_add_match_subsystem_devtype(udev_mon_, "video4linux", NULL)) goto udev_failed; /* Enumerate existing devices */ - devenum = udev_enumerate_new (_udev); + devenum = udev_enumerate_new(udev_); if (devenum == NULL) goto udev_failed; - if (udev_enumerate_add_match_subsystem (devenum, "video4linux")) + if (udev_enumerate_add_match_subsystem(devenum, "video4linux")) { - udev_enumerate_unref (devenum); + udev_enumerate_unref(devenum); goto udev_failed; } - udev_monitor_enable_receiving (_udev_mon); + udev_monitor_enable_receiving(udev_mon_); /* Note that we enumerate _after_ monitoring is enabled so that we do not * loose device events occuring while we are enumerating. We could still * loose events if the Netlink socket receive buffer overflows. */ - udev_enumerate_scan_devices (devenum); - devlist = udev_enumerate_get_list_entry (devenum); + udev_enumerate_scan_devices(devenum); + devlist = udev_enumerate_get_list_entry(devenum); struct udev_list_entry *deventry; - udev_list_entry_foreach (deventry, devlist) + udev_list_entry_foreach(deventry, devlist) { - const char *path = udev_list_entry_get_name (deventry); - struct udev_device *dev = udev_device_new_from_syspath (_udev, path); + const char *path = udev_list_entry_get_name(deventry); + struct udev_device *dev = udev_device_new_from_syspath(udev_, path); if (is_v4l2(dev)) { - const char *devpath = udev_device_get_devnode (dev); + const char *devpath = udev_device_get_devnode(dev); if (devpath) { try { addDevice(devpath); @@ -117,9 +116,9 @@ VideoV4l2List::VideoV4l2List() : _udev_mon(NULL) } } } - udev_device_unref (dev); + udev_device_unref(dev); } - udev_enumerate_unref (devenum); + udev_enumerate_unref(devenum); return; @@ -127,12 +126,12 @@ udev_failed: ERROR("udev enumeration failed"); - if (_udev_mon) - udev_monitor_unref (_udev_mon); - if (_udev) - udev_unref (_udev); - _udev_mon = NULL; - _udev = NULL; + if (udev_mon_) + udev_monitor_unref(udev_mon_); + if (udev_) + udev_unref(udev_); + udev_mon_ = NULL; + udev_ = NULL; /* fallback : go through /dev/video* */ int idx; @@ -154,10 +153,9 @@ namespace { int GetNumber(const std::string &name, size_t *sharp) { size_t len = name.length(); - if (len < 3) { - // name is too short to be numbered + // name is too short to be numbered + if (len < 3) return -1; - } for (size_t c = len; c; --c) { if (name[c] == '#') { @@ -174,41 +172,39 @@ int GetNumber(const std::string &name, size_t *sharp) void GiveUniqueName(VideoV4l2Device &dev, const std::vector<VideoV4l2Device> &devices) { - const std::string &name = dev.name; start: for (size_t i = 0; i < devices.size(); i++) { - if (name == devices[i].name) { + if (dev.name == devices[i].name) { size_t sharp; - int num = GetNumber(name, &sharp); + int num = GetNumber(dev.name, &sharp); if (num < 0) // not numbered dev.name += " #0"; else { std::stringstream ss; ss << num+1; - dev.name.replace(sharp+1, ss.str().length(), ss.str()); + dev.name.replace(sharp + 1, ss.str().length(), ss.str()); } goto start; // we changed the name, let's look again if it is unique } } } - } // end anonymous namespace -VideoV4l2List::~VideoV4l2List() +VideoV4l2ListThread::~VideoV4l2ListThread() { terminate(); - if (_udev_mon) - udev_monitor_unref (_udev_mon); - if (_udev) - udev_unref (_udev); + if (udev_mon_) + udev_monitor_unref(udev_mon_); + if (udev_) + udev_unref(udev_); } -void VideoV4l2List::run() +void VideoV4l2ListThread::run() { - if (!_udev_mon) + if (!udev_mon_) return; - int fd = udev_monitor_get_fd(_udev_mon); + int fd = udev_monitor_get_fd(udev_mon_); fd_set set; FD_ZERO(&set); FD_SET(fd, &set); @@ -218,68 +214,64 @@ void VideoV4l2List::run() int ret = select(fd+1, &set, NULL, NULL, NULL); switch(ret) { - case 1: - dev = udev_monitor_receive_device(_udev_mon); - if (!is_v4l2(dev)) { - udev_device_unref(dev); - continue; - } + case 1: + dev = udev_monitor_receive_device(udev_mon_); + if (!is_v4l2(dev)) { + udev_device_unref(dev); + continue; + } - node = udev_device_get_devnode(dev); - action = udev_device_get_action(dev); - if (!strcmp(action, "add")) { - DEBUG("udev: adding %s", node); - try { - addDevice(node); - Manager::instance().notifyVideoDeviceEvent(); - } catch (const std::runtime_error &e) { - ERROR(e.what()); + node = udev_device_get_devnode(dev); + action = udev_device_get_action(dev); + if (!strcmp(action, "add")) { + DEBUG("udev: adding %s", node); + try { + addDevice(node); + Manager::instance().notifyVideoDeviceEvent(); + } catch (const std::runtime_error &e) { + ERROR(e.what()); + } + } else if (!strcmp(action, "remove")) { + DEBUG("udev: removing %s", node); + delDevice(std::string(node)); } - } else if (!strcmp(action, "remove")) { - DEBUG("udev: removing %s", node); - delDevice(std::string(node)); - } - udev_device_unref(dev); - continue; - - default: - ERROR("select() returned %d (%m)", ret); - return; - - case -1: - if (errno == EAGAIN) - continue; - ERROR("udev monitoring thread: select failed (%m)"); - return; - } - } -} + udev_device_unref(dev); + continue; -void VideoV4l2List::finalize() -{} + default: + ERROR("select() returned %d (%m)", ret); + return; + + case -1: + if (errno == EAGAIN) + continue; + ERROR("udev monitoring thread: select failed (%m)"); + return; + } + } +} -void VideoV4l2List::delDevice(const std::string &node) +void VideoV4l2ListThread::delDevice(const std::string &node) { - ost::MutexLock lock(_mutex); + ost::MutexLock lock(mutex_); std::vector<std::string> v; - size_t n = devices.size(); - unsigned i; - for (i = 0 ; i < n ; i++) { - if (devices[i].device == node) { - devices.erase(devices.begin() + i); + size_t n = devices_.size(); + for (size_t i = 0 ; i < n ; i++) { + if (devices_[i].device == node) { + devices_.erase(devices_.begin() + i); Manager::instance().notifyVideoDeviceEvent(); return; } } } -bool VideoV4l2List::addDevice(const std::string &dev) +bool VideoV4l2ListThread::addDevice(const std::string &dev) { - ost::MutexLock lock(_mutex); + ost::MutexLock lock(mutex_); if (dev == "SFLTEST") { - devices.push_back(VideoV4l2Device(-1, dev)); + devices_.push_back(VideoV4l2Device(-1, dev)); return true; } @@ -289,74 +281,71 @@ bool VideoV4l2List::addDevice(const std::string &dev) std::string s(dev); VideoV4l2Device v(fd, s); - GiveUniqueName(v, devices); - devices.push_back(v); + GiveUniqueName(v, devices_); + devices_.push_back(v); ::close(fd); return true; } -std::vector<std::string> VideoV4l2List::getChannelList(const std::string &dev) +std::vector<std::string> VideoV4l2ListThread::getChannelList(const std::string &dev) { - ost::MutexLock lock(_mutex); + ost::MutexLock lock(mutex_); return getDevice(dev).getChannelList(); } -std::vector<std::string> VideoV4l2List::getSizeList(const std::string &dev, const std::string &channel) +std::vector<std::string> VideoV4l2ListThread::getSizeList(const std::string &dev, const std::string &channel) { - ost::MutexLock lock(_mutex); + ost::MutexLock lock(mutex_); return getDevice(dev).getChannel(channel).getSizeList(); } -std::vector<std::string> VideoV4l2List::getRateList(const std::string &dev, const std::string &channel, const std::string &size) +std::vector<std::string> VideoV4l2ListThread::getRateList(const std::string &dev, const std::string &channel, const std::string &size) { - ost::MutexLock lock(_mutex); + ost::MutexLock lock(mutex_); return getDevice(dev).getChannel(channel).getSize(size).getRateList(); } -std::vector<std::string> VideoV4l2List::getDeviceList(void) +std::vector<std::string> VideoV4l2ListThread::getDeviceList(void) { - ost::MutexLock lock(_mutex); + ost::MutexLock lock(mutex_); std::vector<std::string> v; - size_t n = devices.size(); - unsigned i; - for (i = 0 ; i < n ; i++) { + size_t n = devices_.size(); + for (size_t i = 0 ; i < n ; i++) { std::stringstream ss; - VideoV4l2Device &dev = devices[i]; + VideoV4l2Device &dev = devices_[i]; std::string &name = dev.name; - if (name.length()) { + if (name.length()) ss << name; - } else { + else ss << dev.device; - } + v.push_back(ss.str()); } return v; } -VideoV4l2Device &VideoV4l2List::getDevice(const std::string &name) +VideoV4l2Device &VideoV4l2ListThread::getDevice(const std::string &name) { - ost::MutexLock lock(_mutex); - for (size_t i = 0; i < devices.size(); i++) { - if (devices[i].name == name) - return devices[i]; - } + ost::MutexLock lock(mutex_); + for (size_t i = 0; i < devices_.size(); i++) + if (devices_[i].name == name) + return devices_[i]; - return devices.back(); + return devices_.back(); } -unsigned VideoV4l2List::getChannelNum(const std::string &dev, const std::string &name) +unsigned VideoV4l2ListThread::getChannelNum(const std::string &dev, const std::string &name) { - ost::MutexLock lock(_mutex); + ost::MutexLock lock(mutex_); return getDevice(dev).getChannel(name).idx; } -const std::string &VideoV4l2List::getDeviceNode(const std::string &name) +const std::string &VideoV4l2ListThread::getDeviceNode(const std::string &name) { - ost::MutexLock lock(_mutex); + ost::MutexLock lock(mutex_); return getDevice(name).device; } - } // namespace sfl_video diff --git a/daemon/src/video/video_v4l2_list.h b/daemon/src/video/video_v4l2_list.h index ebf69967a487697f8e3e96b0fbd55e203a7f3cb5..e0cfeb242e168983d8ef94139ee21bdadf70d9e3 100644 --- a/daemon/src/video/video_v4l2_list.h +++ b/daemon/src/video/video_v4l2_list.h @@ -37,19 +37,19 @@ #include <libudev.h> #include "video_v4l2.h" - +#include "noncopyable.h" namespace sfl_video { -class VideoV4l2List : public ost::Thread { +class VideoV4l2ListThread : public ost::Thread { public: - VideoV4l2List(); - ~VideoV4l2List(); + VideoV4l2ListThread(); + ~VideoV4l2ListThread(); virtual void run(); - virtual void finalize(); + virtual void finalize() {} - std::vector<std::string> getDeviceList(void); + std::vector<std::string> getDeviceList(); std::vector<std::string> getChannelList(const std::string &dev); std::vector<std::string> getSizeList(const std::string &dev, const std::string &channel); std::vector<std::string> getRateList(const std::string &dev, const std::string &channel, const std::string &size); @@ -59,13 +59,14 @@ class VideoV4l2List : public ost::Thread { unsigned getChannelNum(const std::string &dev, const std::string &name); private: + NON_COPYABLE(VideoV4l2ListThread); void delDevice(const std::string &node); bool addDevice(const std::string &dev); - std::vector<VideoV4l2Device> devices; - ost::Mutex _mutex; + std::vector<VideoV4l2Device> devices_; + ost::Mutex mutex_; - struct udev *_udev; - struct udev_monitor *_udev_mon; + udev *udev_; + udev_monitor *udev_mon_; }; } // namespace sfl_video