encoder_manager.h
Go to the documentation of this file.
00001 /*
00002  * Copyright (c) 2014, Willow Garage, Inc.
00003  * All rights reserved.
00004  *
00005  * Redistribution and use in source and binary forms, with or without
00006  * modification, are permitted provided that the following conditions are met:
00007  *
00008  *     * Redistributions of source code must retain the above copyright
00009  *       notice, this list of conditions and the following disclaimer.
00010  *     * Redistributions in binary form must reproduce the above copyright
00011  *       notice, this list of conditions and the following disclaimer in the
00012  *       documentation and/or other materials provided with the distribution.
00013  *     * Neither the name of the Willow Garage, Inc. nor the names of its
00014  *       contributors may be used to endorse or promote products derived from
00015  *       this software without specific prior written permission.
00016  *
00017  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00018  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00019  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00020  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
00021  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
00022  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
00023  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
00024  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
00025  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
00026  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00027  * POSSIBILITY OF SUCH DAMAGE.
00028  *
00029  *
00030  * encoder_manager.h
00031  *
00032  *  Created on: Oct 30, 2012
00033  *      Author: jkammerl
00034  */
00035 
00036 
00037 #ifndef ENCODING_MANAGER_H_
00038 #define ENCODING_MANAGER_H_
00039 
00040 #include "ffmpeg_encoder.h"
00041 
00042 #include "server_configuration.h"
00043 
00044 #include <map>
00045 #include <string>
00046 
00047 //#define SHARED_ENCODERS
00048 
00049 namespace ros_http_video_streamer
00050 {
00051 
00052 class EncoderManager
00053 {
00054 public:
00055   EncoderManager() :
00056       mutex_(),
00057       image_encoder_map_(),
00058       request_counter_(0)
00059   {
00060   }
00061 
00062   virtual ~EncoderManager()
00063   {
00064   }
00065 
00066   //
00067   FFMPEGEncoder::ptr subscribe(const std::string& topic,
00068                                const ServerConfiguration& config)
00069   {
00070     boost::mutex::scoped_lock lock(mutex_);
00071 
00072     ++request_counter_;
00073 
00074     std::string refID = topic + ":" +
00075                         config.codec_ + ":" +
00076                         "BR:"+boost::lexical_cast<std::string>(config.bitrate_) +
00077                         "FR:"+boost::lexical_cast<std::string>(config.framerate_) +
00078                         "FW:"+boost::lexical_cast<std::string>(config.frame_width_) +
00079                         "FH:"+boost::lexical_cast<std::string>(config.frame_height_) +
00080                         "Q:"+boost::lexical_cast<std::string>(config.quality_) +
00081                         "GOP:"+boost::lexical_cast<std::string>(config.gop_);
00082 
00083     FFMPEGEncoder::ptr image_encoder;
00084 
00085 #ifdef SHARED_ENCODERS
00086     // search for encoder instance in image_encoder_map_
00087     std::map<std::string, EncoderInfo>::iterator it;
00088     it = image_encoder_map_.find(refID);
00089 
00090     if (it!=image_encoder_map_.end())
00091     {
00092       image_encoder = (it->second).enc_;
00093       (it->second).listener_count_++;
00094     }
00095     else
00096     {
00097       // if not found, create new instance of encoder
00098       image_encoder = FFMPEGEncoder::ptr(new FFMPEGEncoder(refID, topic, config));
00099 
00100       EncoderInfo encInfo;
00101       encInfo.enc_ = image_encoder;
00102       encInfo.listener_count_ = 1;
00103 
00104       // add it to the image_encoder_map_
00105       image_encoder_map_[refID] = encInfo;
00106     }
00107 #else
00108     // make request ID unique
00109     refID += "ReqID:" + boost::lexical_cast<std::string>(request_counter_);
00110     image_encoder = FFMPEGEncoder::ptr(new FFMPEGEncoder(refID, topic, config));
00111 
00112     EncoderInfo encInfo;
00113     encInfo.enc_ = image_encoder;
00114     encInfo.listener_count_ = 1;
00115 
00116     image_encoder_map_[refID] = encInfo;
00117 #endif
00118 
00119     return image_encoder;
00120   }
00121 
00122   void unsubscribe(const std::string& refID)
00123   {
00124     boost::mutex::scoped_lock lock(mutex_);
00125 
00126     // search for encoder instance
00127     std::map<std::string, EncoderInfo>::iterator it;
00128     it = image_encoder_map_.find(refID);
00129 
00130     if (it != image_encoder_map_.end())
00131     {
00132       EncoderInfo& encInfo = it->second;
00133       encInfo.listener_count_--;
00134 
00135       if (!encInfo.listener_count_)
00136         image_encoder_map_.erase(refID);
00137 
00138       ROS_INFO("Deleting encoder: %s", refID.c_str());
00139     }
00140   }
00141 
00142 private:
00143 
00144   // struct that contains a shared pointer to an encoder instance and
00145   // manages the listener count
00146   struct EncoderInfo
00147   {
00148     EncoderInfo() :
00149         listener_count_(0)
00150     {
00151     }
00152 
00153     // shared pointer to encoder
00154     FFMPEGEncoder::ptr enc_;
00155     // amount of subscribed http listeners
00156     std::size_t listener_count_;
00157   };
00158 
00159   // mutex to protect the image_encoder_map
00160   boost::mutex mutex_;
00161   // hash map that maps reference strings to image encoder instances
00162   std::map<std::string, EncoderInfo> image_encoder_map_;
00163 
00164   // total amounts of encoder requests to manager class (used to construct unique reference IDs)
00165   unsigned int request_counter_;
00166 
00167 };
00168 
00169 } // ros_http_video_streamer
00170 
00171 
00172 #endif /* ENCODING_MANAGER_H_ */


ros_web_video
Author(s): Julius Kammer
autogenerated on Fri Aug 28 2015 12:38:36