Repository: opencv
Issue: udp streaeming using gstreamer opencv and video writer
Labels: question (invalid tracker),incomplete

### System Information

OpenCV version: '4.5.2'
Operating System: Ubuntu 20.04
python: 3.8


### Detailed description

I'm Processing image in the code and then sending it to another device using udp protocol

stream_string2 = f"appsrc ! videoconvert ! videoscale ! x265enc bitrate=1200000 ! h265parse ! " \
                                     f"rtph265pay config-interval=1 pt=96 ! udpsink host=192.168.0.5 port=8889 " \
                                     f"sync=false async=false",

and then streaming using VideoWriter

stream = cv2.VideoWriter(stream_string[0],
                                        cv2.CAP_GSTREAMER, 0, 20, (640, 480),True)

and then inside the loop in the code I do

stream.write(img)

### Steps to reproduce
``` python
if config.DEBUG:
                stream_string = ''
                if udp_stream:
                    stream_string = f"appsrc ! videoconvert ! videoscale ! x265enc bitrate=1200000 ! h265parse ! " \
                                    f"rtph265pay config-interval=1 pt=96" \
                                    f" ! udpsink host={config.UDP_VIDEO_OUTPUT_HOST_DEBUG} port=" \
                                    f"{config.UDP_VIDEO_OUTPUT_PORT} sync=false async=false",
        else:
                if udp_stream:
                    stream_string = f"appsrc ! videoconvert ! videoscale ! x265enc  bitrate=1200000 ! h265parse ! " \
                                    f"rtph265pay config-interval=1 pt=96" \
                                    f" ! udpsink host={config.UDP_VIDEO_OUTPUT_HOST} port=" \
                                    f"{config.UDP_VIDEO_OUTPUT_PORT} sync=false async=false",
                    stream_string = "appsrc ! videoconvert ! x264enc tune=zerolatency bitrate=500 speed-preset=superfast ! rtph264pay ! udpsink host=192.168.2.5 port=8888"
                    stream_string2 = f"appsrc ! videoconvert ! videoscale ! x265enc bitrate=1200000 ! h265parse ! " \
                                     f"rtph265pay config-interval=1 pt=96 ! udpsink host=192.168.0.5 port=8889 " \
                                     f"sync=false async=false",
                    gst_str  =  f"appsrc ! videoconvert ! videoscale ! video/x-raw,width=640,height=480 ! x264enc speed-preset=veryfast tune=zerolatency bitrate=800 ! rtspclientsink location=rtsp://localhost:8888/mystream "
        if udp_stream:
            logger.debug(f'{stream_string}')
            # print("+"*25)
            # print(stream_string[0])
            # print(stream_string)
            stream = cv2.VideoWriter(stream_string[0],
                                        cv2.CAP_GSTREAMER, 0, 20, (640, 480),True)
            # stream2 = cv2.VideoWriter(stream_string2[0],
            #                           cv2.CAP_GSTREAMER, 0, 30, (1280, 720))
            # stream3 = cv2.VideoWriter(gst_str,
            #                           cv2.CAP_GSTREAMER, 0, 30, (1280, 720))
        while True:
            fps += 1
            if True: # self.AiEngine.last_detection_time is not None and time.time() - self.AiEngine.last_detection_time < 2:
                img = self.latestProcessedFrame
            else:
                img = self.latestFrame

            # cv2.imshow("output", img)
            # if cv2.waitKey(0) & 0xFF == ord('q'):
            #     break
            time.sleep(.05)
            if img is not None:
                if config.DEBUG:
                    self.JPGFrameEncoder(img)
                if udp_stream:
                    
                    if stream.isOpened():
                        logger.info(f'Streaming to UDP: {img.shape}')
                        stream.write(img)
                    else:
                        logger.error(f'Error with streaming: {img.shape}')
                    # if stream2.isOpened():
                    #     stream2.write(img)
                    # else:
                    #     logger.error(f'Error with streaming: {img.shape}')
                    # if stream3.isOpened():
                    #     stream3.write(img)

                self.toc = time.time()

                if self.toc - self.tic > 1:
                    logger.debug(f"\n******************\n****FPS IS: {fps}****\n******************")
                    fps = 0
                    self.tic = time.time()
            else:
                logger.error(f'Error with streaming')
                break
```
### Issue submission checklist

- [X] I report the issue, it's not a question
- [X] I checked the problem with documentation, FAQ, open issues, forum.opencv.org, Stack Overflow, etc and have not found any solution
- [X] I updated to the latest OpenCV version and the issue is still there
- [X] There is reproducer code and related data files (videos, images, onnx, etc)