def test_fail_framedimension(): """ IO Test - made to fail with multiple frame dimension """ np.random.seed(0) # generate random data for 10 frames random_data1 = np.random.random(size=(480, 640, 3)) * 255 input_data1 = random_data1.astype(np.uint8) np.random.seed(0) random_data2 = np.random.random(size=(580, 640, 3)) * 255 input_data2 = random_data2.astype(np.uint8) streamer = None try: streamer = StreamGear(output="output.mpd") streamer.stream(None) streamer.stream(input_data1) streamer.stream(input_data2) except Exception as e: if isinstance(e, ValueError): pytest.xfail("Test Passed!") else: pytest.fail(str(e)) finally: if not streamer is None: streamer.terminate()
def test_rtf_stream(conversion): """ Testing Real-Time Frames Mode """ mpd_file_path = return_mpd_path() try: # Open stream stream = CamGear(source=return_testvideo_path(), colorspace=conversion).start() stream_params = { "-clear_prev_assets": True, "-input_framerate": "invalid", } streamer = StreamGear(output=mpd_file_path, **stream_params) while True: frame = stream.read() # check if frame is None if frame is None: break if conversion == "COLOR_BGR2RGBA": streamer.stream(frame, rgb_mode=True) else: streamer.stream(frame) stream.stop() streamer.terminate() mpd_file = [ os.path.join(mpd_file_path, f) for f in os.listdir(mpd_file_path) if f.endswith(".mpd") ] assert len(mpd_file) == 1, "Failed to create MPD file!" assert check_valid_mpd(mpd_file[0]) except Exception as e: pytest.fail(str(e))
def test_multistreams(stream_params): """ Testing Support for additional Secondary Streams of variable bitrates or spatial resolutions. """ mpd_file_path = os.path.join(return_mpd_path(), "dash_test.mpd") results = extract_resolutions(stream_params["-video_source"], stream_params["-streams"]) try: streamer = StreamGear(output=mpd_file_path, logging=True, **stream_params) streamer.transcode_source() streamer.terminate() metadata = extract_meta_mpd(mpd_file_path) meta_videos = [ x for x in metadata if x["mime_type"].startswith("video") ] assert meta_videos and (len(meta_videos) <= len(results)), "Test Failed!" for s_v in meta_videos: assert int(s_v["width"]) in results, "Width check failed!" assert (int(s_v["height"]) == results[int( s_v["width"])]), "Height check failed!" except Exception as e: pytest.fail(str(e))
def test_audio(stream_params, format): """ Testing external and audio audio for stream. """ assets_file_path = os.path.join( return_assets_path(False if format == "dash" else True), "format_test{}".format(".mpd" if format == "dash" else ".m3u8"), ) try: if format == "hls": stream_params.update( { "-hls_base_url": return_assets_path( False if format == "dash" else True ) + os.sep } ) streamer = StreamGear( output=assets_file_path, format=format, logging=True, **stream_params ) streamer.transcode_source() streamer.terminate() if format == "dash": assert check_valid_mpd(assets_file_path), "Test Failed!" else: assert extract_meta_video(assets_file_path), "Test Failed!" except Exception as e: pytest.fail(str(e))
def test_params(stream_params, format): """ Testing "-stream_params" parameters by StreamGear """ try: assets_file_path = os.path.join( return_assets_path(False if format == "dash" else True), "format_test{}".format(".mpd" if format == "dash" else ".m3u8"), ) if format == "hls": stream_params.update( { "-hls_base_url": return_assets_path( False if format == "dash" else True ) + os.sep } ) stream = cv2.VideoCapture(return_testvideo_path()) # Open stream streamer = StreamGear( output=assets_file_path, format=format, logging=True, **stream_params ) while True: (grabbed, frame) = stream.read() if not grabbed: break streamer.stream(frame) stream.release() streamer.terminate() if format == "dash": assert check_valid_mpd(assets_file_path), "Test Failed!" else: assert extract_meta_video(assets_file_path), "Test Failed!" except Exception as e: pytest.fail(str(e))
def test_delete_ext_safe(ext, result): """ Testing delete_ext_safe function """ try: path = os.path.join(expanduser("~"), "test_mpd") if ext: mkdir_safe(path, logging=True) # re-create directory for more coverage mkdir_safe(path, logging=True) mpd_file_path = os.path.join(path, "dash_test.mpd") from vidgear.gears import StreamGear stream_params = { "-video_source": return_testvideo_path(), } streamer = StreamGear(output=mpd_file_path, **stream_params) streamer.transcode_source() streamer.terminate() assert check_valid_mpd(mpd_file_path) delete_ext_safe(path, ext, logging=True) assert not os.listdir(path), "`delete_ext_safe` Test failed!" # cleanup if os.path.isdir(path): shutil.rmtree(path) except Exception as e: if result: pytest.fail(str(e))
def test_rtf_livestream(format): """ Testing Real-Time Frames Mode with livestream. """ assets_file_path = return_assets_path(False if format == "dash" else True) try: # Open stream options = {"THREAD_TIMEOUT": 300} stream = CamGear(source=return_testvideo_path(), **options).start() stream_params = { "-livestream": True, } streamer = StreamGear(output=assets_file_path, format=format, **stream_params) while True: frame = stream.read() # check if frame is None if frame is None: break streamer.stream(frame) stream.stop() streamer.terminate() except Exception as e: if not isinstance(e, queue.Empty): pytest.fail(str(e))
def test_input_framerate_rtf(): """ Testing "-input_framerate" parameter provided by StreamGear """ try: mpd_file_path = os.path.join(return_mpd_path(), "dash_test.mpd") stream = cv2.VideoCapture(return_testvideo_path()) # Open stream test_framerate = stream.get(cv2.CAP_PROP_FPS) stream_params = { "-clear_prev_assets": True, "-input_framerate": test_framerate, } streamer = StreamGear(output=mpd_file_path, logging=True, **stream_params) while True: (grabbed, frame) = stream.read() if not grabbed: break streamer.stream(frame) stream.release() streamer.terminate() meta_data = extract_meta_mpd(mpd_file_path) assert meta_data and len(meta_data) > 0, "Test Failed!" framerate_mpd = string_to_float(meta_data[0]["framerate"]) assert framerate_mpd > 0.0 and isinstance(framerate_mpd, float), "Test Failed!" assert round(framerate_mpd) == round(test_framerate), "Test Failed!" except Exception as e: pytest.fail(str(e))
def test_ss_stream(format): """ Testing Single-Source Mode """ assets_file_path = os.path.join( return_assets_path(False if format == "dash" else True), "format_test{}".format(".mpd" if format == "dash" else ".m3u8"), ) try: stream_params = { "-video_source": return_testvideo_path(), "-clear_prev_assets": True, } if format == "hls": stream_params.update( { "-hls_base_url": return_assets_path( False if format == "dash" else True ) + os.sep } ) streamer = StreamGear( output=assets_file_path, format=format, logging=True, **stream_params ) streamer.transcode_source() streamer.terminate() if format == "dash": assert check_valid_mpd(assets_file_path), "Test Failed!" else: assert extract_meta_video(assets_file_path), "Test Failed!" except Exception as e: pytest.fail(str(e))
def test_failedextension(output): """ IO Test - made to fail with filename with wrong extension """ stream_params = {"-video_source": return_testvideo_path()} streamer = StreamGear(output=output, logging=True, **stream_params) streamer.transcode_source() streamer.terminate()
def test_custom_ffmpeg(c_ffmpeg): """ Testing custom FFmpeg for StreamGear """ streamer = StreamGear(output="output.mpd", custom_ffmpeg=c_ffmpeg, logging=True) streamer.terminate()
def test_method_call_ss(): """ Method calling Test - Made to fail by calling method in the wrong context. """ stream_params = {"-video_source": return_testvideo_path()} streamer = StreamGear(output="output.mpd", logging=True, **stream_params) streamer.stream("garbage.garbage") streamer.terminate()
def test_assertfailedstream(): """ IO Test - made to fail with Wrong Output file path """ with pytest.raises(ValueError): # wrong folder path does not exist streamer = StreamGear(output="wrong_path/output.mpd", logging=True) streamer.terminate()
def test_method_call_rtf(): """ Method calling Test - Made to fail by calling method in the wrong context. """ stream_params = {"-video_source": 1234} # for CI testing only streamer = StreamGear(output="output.mpd", logging=True, **stream_params) streamer.transcode_source() streamer.terminate()
def test_invalid_params_ss(): """ Method calling Test - Made to fail by calling method in the wrong context. """ stream_params = { "-video_source": return_testvideo_path(), "-vcodec": "unknown" } streamer = StreamGear(output="output.mpd", logging=True, **stream_params) streamer.transcode_source() streamer.terminate()
def test_audio(stream_params): """ Testing Single-Source Mode """ mpd_file_path = os.path.join(return_mpd_path(), "dash_test.mpd") try: streamer = StreamGear(output=mpd_file_path, logging=True, **stream_params) streamer.transcode_source() streamer.terminate() assert check_valid_mpd(mpd_file_path) except Exception as e: pytest.fail(str(e))
def test_failedextension(): """ IO Test - made to fail with filename with wrong extension """ # 'garbage' extension does not exist with pytest.raises(AssertionError): stream_params = {"-video_source": return_testvideo_path()} streamer = StreamGear(output="garbage.garbage", logging=True, **stream_params) streamer.transcode_source() streamer.terminate()
def test_failedextensionsource(): """ IO Test - made to fail with filename with wrong extension for source """ with pytest.raises(RuntimeError): # 'garbage' extension does not exist stream_params = {"-video_source": "garbage.garbage"} streamer = StreamGear(output="output.mpd", logging=True, **stream_params) streamer.transcode_source() streamer.terminate()
def test_input_framerate_rtf(format): """ Testing "-input_framerate" parameter provided by StreamGear """ try: assets_file_path = os.path.join( return_assets_path(False if format == "dash" else True), "format_test{}".format(".mpd" if format == "dash" else ".m3u8"), ) stream = cv2.VideoCapture(return_testvideo_path()) # Open stream test_framerate = stream.get(cv2.CAP_PROP_FPS) stream_params = { "-clear_prev_assets": True, "-input_framerate": test_framerate, } if format == "hls": stream_params.update( { "-hls_base_url": return_assets_path( False if format == "dash" else True ) + os.sep } ) streamer = StreamGear( output=assets_file_path, format=format, logging=True, **stream_params ) while True: (grabbed, frame) = stream.read() if not grabbed: break streamer.stream(frame) stream.release() streamer.terminate() if format == "dash": meta_data = extract_meta_mpd(assets_file_path) assert meta_data and len(meta_data) > 0, "Test Failed!" framerate_mpd = string_to_float(meta_data[0]["framerate"]) assert framerate_mpd > 0.0 and isinstance( framerate_mpd, float ), "Test Failed!" assert round(framerate_mpd) == round(test_framerate), "Test Failed!" else: meta_data = extract_meta_video(assets_file_path) assert meta_data and "framerate" in meta_data, "Test Failed!" framerate_m3u8 = float(meta_data["framerate"]) assert framerate_m3u8 > 0.0 and isinstance( framerate_m3u8, float ), "Test Failed!" assert round(framerate_m3u8) == round(test_framerate), "Test Failed!" except Exception as e: pytest.fail(str(e))
def test_invalid_params_rtf(): """ Invalid parameter Failure Test - Made to fail by calling invalid parameters """ np.random.seed(0) # generate random data for 10 frames random_data = np.random.random(size=(480, 640, 3)) * 255 input_data = random_data.astype(np.uint8) stream_params = {"-vcodec": "unknown"} streamer = StreamGear(output="output.mpd", logging=True, **stream_params) streamer.stream(input_data) streamer.stream(input_data) streamer.terminate()
def test_failedchannels(): """ IO Test - made to fail with invalid channel length """ np.random.seed(0) # generate random data for 10 frames random_data = np.random.random(size=(480, 640, 5)) * 255 input_data = random_data.astype(np.uint8) # 'garbage' extension does not exist with pytest.raises(ValueError): streamer = StreamGear("output.mpd", logging=True) streamer.stream(input_data) streamer.terminate()
def test_rtf_stream(conversion, format): """ Testing Real-Time Frames Mode """ assets_file_path = return_assets_path(False if format == "dash" else True) try: # Open stream options = {"THREAD_TIMEOUT": 300} stream = CamGear( source=return_testvideo_path(), colorspace=conversion, **options ).start() stream_params = { "-clear_prev_assets": True, "-input_framerate": "invalid", } if format == "hls": stream_params.update( { "-hls_base_url": return_assets_path( False if format == "dash" else True ) + os.sep } ) streamer = StreamGear(output=assets_file_path, format=format, **stream_params) while True: frame = stream.read() # check if frame is None if frame is None: break if conversion == "COLOR_BGR2RGBA": streamer.stream(frame, rgb_mode=True) else: streamer.stream(frame) stream.stop() streamer.terminate() asset_file = [ os.path.join(assets_file_path, f) for f in os.listdir(assets_file_path) if f.endswith(".mpd" if format == "dash" else ".m3u8") ] assert len(asset_file) == 1, "Failed to create asset file!" if format == "dash": assert check_valid_mpd(asset_file[0]), "Test Failed!" else: assert extract_meta_video(asset_file[0]), "Test Failed!" except Exception as e: if not isinstance(e, queue.Empty): pytest.fail(str(e))
def test_invalid_params_ss(format): """ Method calling Test - Made to fail by calling method in the wrong context. """ stream_params = { "-video_source": return_testvideo_path(), "-vcodec": "unknown" } streamer = StreamGear( output="output{}".format(".mpd" if format == "dash" else ".m3u8"), format=format, logging=True, **stream_params) streamer.transcode_source() streamer.terminate()
def test_paths_ss(path): """ Paths Test - Test various paths/urls supported by StreamGear. """ streamer = None try: stream_params = {"-video_source": return_testvideo_path()} streamer = StreamGear(output=path, logging=True, **stream_params) except Exception as e: if isinstance(e, ValueError): pytest.xfail("Test Passed!") else: pytest.fail(str(e)) finally: if not streamer is None: streamer.terminate()
def test_ss_livestream(): """ Testing Single-Source Mode with livestream. """ mpd_file_path = os.path.join(return_mpd_path(), "dash_test.mpd") try: stream_params = { "-video_source": return_testvideo_path(), "-livestream": True, "-remove_at_exit": 1, } streamer = StreamGear(output=mpd_file_path, logging=True, **stream_params) streamer.transcode_source() streamer.terminate() except Exception as e: pytest.fail(str(e))
def test_ss_stream(): """ Testing Single-Source Mode """ mpd_file_path = os.path.join(return_mpd_path(), "dash_test.mpd") try: stream_params = { "-video_source": return_testvideo_path(), "-clear_prev_assets": True, } streamer = StreamGear(output=mpd_file_path, logging=True, **stream_params) streamer.transcode_source() streamer.terminate() assert check_valid_mpd(mpd_file_path) except Exception as e: pytest.fail(str(e))
def test_outputs(output): """ Testing different output for StreamGear """ stream_params = ( {"-clear_prev_assets": True} if (output and output == "output.mpd") else {"-clear_prev_assets": "invalid"} ) try: streamer = StreamGear(output=output, logging=True, **stream_params) streamer.terminate() except Exception as e: if output is None: pytest.xfail(str(e)) else: pytest.fail(str(e))
def test_fail_framedimension(): """ IO Test - made to fail with multiple frame dimension """ np.random.seed(0) # generate random data for 10 frames random_data1 = np.random.random(size=(480, 640, 3)) * 255 input_data1 = random_data1.astype(np.uint8) np.random.seed(0) random_data2 = np.random.random(size=(580, 640, 3)) * 255 input_data2 = random_data2.astype(np.uint8) streamer = StreamGear(output="output.mpd") streamer.stream(None) streamer.stream(input_data1) streamer.stream(input_data2) streamer.terminate()
def test_params(stream_params): """ Testing "-input_framerate" parameter provided by StreamGear """ try: mpd_file_path = os.path.join(return_mpd_path(), "dash_test.mpd") stream = cv2.VideoCapture(return_testvideo_path()) # Open stream streamer = StreamGear(output=mpd_file_path, logging=True, **stream_params) while True: (grabbed, frame) = stream.read() if not grabbed: break streamer.stream(frame) stream.release() streamer.terminate() assert check_valid_mpd(mpd_file_path) except Exception as e: pytest.fail(str(e))
def test_failedchannels(size): """ IO Test - made to fail with invalid channel lengths """ np.random.seed(0) if len(size) > 1: random_data_1 = np.random.random(size=size[0]) * 255 input_data_ch1 = random_data_1.astype(np.uint8) random_data_2 = np.random.random(size=size[1]) * 255 input_data_ch3 = random_data_2.astype(np.uint8) streamer = StreamGear("output.mpd", logging=True) streamer.stream(input_data_ch1) streamer.stream(input_data_ch3) streamer.terminate() else: random_data = np.random.random(size=size) * 255 input_data = random_data.astype(np.uint8) streamer = StreamGear("output.mpd", logging=True) streamer.stream(input_data) streamer.terminate()