Exemple #1
0
    def _process_open_channel(self, conn, msg_data):
        """
        Description: process open channel message
        Input:
            conn: a socket connection
            msg_data: msg body, serialized by protobuf
        Returns: True or False
        """
        request = presenter_message_pb2.OpenChannelRequest()
        response = presenter_message_pb2.OpenChannelResponse()
        if not self._parse_protobuf(request, msg_data):
            channel_name = "unknown channel"
            err_code = presenter_message_pb2.kOpenChannelErrorOther
            return self._response_open_channel(conn, channel_name, response,
                                               err_code)
        channel_name = request.channel_name

        # check channel name if exist
        if not self.channel_manager.is_channel_exist(channel_name):
            logging.error("channel name %s is not exist.", channel_name)
            err_code = presenter_message_pb2.kOpenChannelErrorNoSuchChannel
            return self._response_open_channel(conn, channel_name, response,
                                               err_code)
            #ret = self.channel_manager.register_one_channel(channel_name)
            #if ret != ChannelManager.err_code_ok:
            #    logging.error("Create the channel %s failed!, and ret is %d", channel_name, ret)
            #    err_code =  pb2.kOpenChannelErrorOther
            #    self._response_open_channel(conn, channel_name, response, err_code)

        # check channel path if busy
        if self.channel_manager.is_channel_busy(channel_name):
            logging.error("channel path %s is busy.", channel_name)
            err = presenter_message_pb2.kOpenChannelErrorChannelAlreadyOpened
            return self._response_open_channel(conn, channel_name, response,
                                               err)

        content_type = presenter_message_pb2.kChannelContentTypeVideo
        if request.content_type == content_type:
            media_type = "video"
        else:
            logging.error("media type %s is not recognized.",
                          request.content_type)
            err_code = presenter_message_pb2.kOpenChannelErrorOther
            return self._response_open_channel(conn, channel_name, response,
                                               err_code)

        handler = FacialRecognitionHandler(channel_name, media_type)
        sock = conn.fileno()
        self.channel_manager.create_channel_resource(channel_name, sock,
                                                     media_type, handler)
        err_code = presenter_message_pb2.kOpenChannelErrorNone
        return self._response_open_channel(conn, channel_name, response,
                                           err_code)
 def func_begin(self):
     channel_name = TestFacialRecognitionHandler.channel_name
     media_type = TestFacialRecognitionHandler.media_type
     TestFacialRecognitionHandler.channel_manager.register_one_channel(channel_name)
     self.handler = FacialRecognitionHandler(channel_name, media_type)
class TestFacialRecognitionHandler(unittest.TestCase):
    """TestFacialRecognitionHandler"""
    channel_name = "facial_recognition"
    media_type = "video"
    channel_manager = ChannelManager()
    handler = None

    def func_end(self):
        self.handler.close_thread_switch = True
        channel_name = TestFacialRecognitionHandler.channel_name
        TestFacialRecognitionHandler.channel_manager.unregister_one_channel(channel_name)

    def func_begin(self):
        channel_name = TestFacialRecognitionHandler.channel_name
        media_type = TestFacialRecognitionHandler.media_type
        TestFacialRecognitionHandler.channel_manager.register_one_channel(channel_name)
        self.handler = FacialRecognitionHandler(channel_name, media_type)

    @classmethod
    def tearDownClass(cls):
        pass

    @classmethod
    def setUpClass(cls):
        pass


    def run_thread(self, func):
        thread = threading.Thread(target=func)
        thread.start()

    def set_img_data(self):
        time.sleep(0.5)
        self.handler.img_data = b'1234'

    def set_img_data_none(self):
        time.sleep(1.5)
        self.handler.img_data = None


    @patch('threading.Event.clear', return_value = True)
    @patch('threading.Event.wait', return_value = True)
    def test_save_frame1(self, mock1, mock2):
        self.func_begin()
        image = b'1234'
        face_list = []
        self.handler.img_data = b'12'
        self.handler.save_frame(image, face_list)
        self.func_end()


    def test_save_frame2(self):
        self.func_begin()
        image = b'1234'
        face_list = []
        self.handler.img_data = b'12'
        self.run_thread(self.set_img_data_none)
        self.handler.save_frame(image, face_list)
        self.func_end()


    @patch('threading.Event.clear', return_value = True)
    @patch('threading.Event.wait', return_value = True)
    def test_frames(self, mock1, mock2):        
        self.func_begin()
        self.handler.close_thread_switch = True
        time.sleep(0.5) # wait thread exit
        self.handler.img_data = None
        backup_heartbeat = facial_recognition_handler.HEARTBEAT_TIMEOUT
        facial_recognition_handler.HEARTBEAT_TIMEOUT = 0
        self.handler.close_thread_switch = False
        for frame in self.handler.frames():
            self.assertEqual(frame, None)
            break
        facial_recognition_handler.HEARTBEAT_TIMEOUT = backup_heartbeat
        self.func_end()

    @patch('threading.Event.clear', return_value = True)
    @patch('threading.Event.wait')
    def test_get_frame(self, mock1, mock2):
        self.func_begin()
        self.handler.frame_data = b'123'
        self.handler.face_list = []
        self.handler.fps = 5
        mock1.return_value = True
        ret = self.handler.get_frame()
        self.assertNotEqual(ret, {})

        mock1.return_value = False
        ret = self.handler.get_frame()
        self.assertEqual(ret, {})
        mock1.return_value = True
        self.func_end()