Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WebGear_RTC: Once the camera stream on the browser is closed, it cannot be reloaded again on a new tab until the app is closed and restarted. #258

Closed
3 tasks done
megha-seventhsense opened this issue Oct 22, 2021 · 5 comments · Fixed by #272
Assignees
Labels
ENHANCEMENT ⚡ New Feature/Addition/Improvement PROPOSAL 📩 A proposal/proposition SOLVED 🏁 This issue/PR is resolved now. Goal Achieved!
Milestone

Comments

@megha-seventhsense
Copy link

megha-seventhsense commented Oct 22, 2021

Description

WebGear_RTC: Once the camera stream on the browser is closed, it cannot be reloaded again on a new tab until the app is closed and restarted.

Acknowledgment

Environment

  • VidGear version: 0.2.2
  • Branch: v0.2.2
  • Python version: Python 3.7.9
  • PiP version: 21.3
  • Operating System and version: Ubuntu 18.04.3 LTS

Expected Behavior

Once the browser tab with camera stream is closed, it should be possible to reopen it in a new browser/tab successfully without the need to close and restart the application.

Actual Behavior

Once we close the first browser tab with camera stream and we try to reopen it again in a new tab, camera stream cannot be loaded at all.

Steps to reproduce

Tested with the below sample code from
https://abhitronix.github.io/vidgear/latest/gears/webgear_rtc/advanced/

  1. Run the following sample code to view the camera stream on a browser-
#import necessary libs
import uvicorn, asyncio, cv2
from av import VideoFrame
from aiortc import VideoStreamTrack
from aiortc.mediastreams import MediaStreamError
from vidgear.gears.asyncio import WebGear_RTC
from vidgear.gears.asyncio.helper import reducer

#initialize WebGear_RTC app without any source
web = WebGear_RTC(logging=True)

#create your own Bare-Minimum Custom Media Server
class Custom_RTCServer(VideoStreamTrack):
    """
    Custom Media Server using OpenCV, an inherit-class
    to aiortc's VideoStreamTrack.
    """

    def __init__(self, source=None):

        # don't forget this line!
        super().__init__()

        # initialize global params
        self.stream = cv2.VideoCapture(source)

    async def recv(self):
        """
        A coroutine function that yields `av.frame.Frame`.
        """
        # don't forget this function!!!

        # get next timestamp
        pts, time_base = await self.next_timestamp()

        # read video frame
        (grabbed, frame) = self.stream.read()

        # if NoneType
        if not grabbed:
            return MediaStreamError

        # reducer frames size if you want more performance otherwise comment this line
        frame = await reducer(frame, percentage=30)  # reduce frame by 30%

        # contruct `av.frame.Frame` from `numpy.nd.array`
        av_frame = VideoFrame.from_ndarray(frame, format="bgr24")
        av_frame.pts = pts
        av_frame.time_base = time_base

        # return `av.frame.Frame`
        return av_frame

    def terminate(self):
        """
        Gracefully terminates VideoGear stream
        """
        # don't forget this function!!!

        # terminate
        if not (self.stream is None):
            self.stream.release()
            self.stream = None


#assign your custom media server to config with adequate source (for e.g. foo.mp4)
web.config["server"] = Custom_RTCServer(source=0)

#run this app on Uvicorn server at address http://localhost:8000/
uvicorn.run(web(), host="localhost", port=8000)

#close app safely
web.shutdown()
  1. Open a browser (tested with Chrome and Firefox) and navigate to http://localhost:8000/. It should show the camera stream on the browser successfully.
  2. Now close the above browser tab and open a new tab. Try to navigate to the URL http://localhost:8000/ again. Camera stream cannot be loaded at all.

Optional

Screenshot from 2021-10-22 12-03-59
Screenshot from 2021-10-22 12-15-42

@welcome
Copy link

welcome bot commented Oct 22, 2021

Thanks for opening this issue, a maintainer will get back to you shortly!

In the meantime:

  • Read our Issue Guidelines, and update your issue accordingly. Please note that your issue will be fixed much faster if you spend about half an hour preparing it, including the exact reproduction steps and a demo.
  • Go comprehensively through our dedicated FAQ & Troubleshooting section.
  • For any quick questions and typos, please refrain from opening an issue, as you can reach us on Gitter community channel.

@abhiTronix

This comment has been minimized.

@abhiTronix abhiTronix added QUESTION ❓ User asked about the working/usage of VidGear APIs. WON'T FIXED 🚫 This issue will not be worked on labels Oct 22, 2021
@abhiTronix abhiTronix removed their assignment Oct 22, 2021
abhiTronix pushed a commit that referenced this issue Oct 27, 2021
@abhiTronix abhiTronix added ENHANCEMENT ⚡ New Feature/Addition/Improvement PROPOSAL 📩 A proposal/proposition WORK IN PROGRESS 🚧 currently been worked on. and removed QUESTION ❓ User asked about the working/usage of VidGear APIs. WON'T FIXED 🚫 This issue will not be worked on labels Dec 1, 2021
@abhiTronix abhiTronix self-assigned this Dec 1, 2021
@abhiTronix abhiTronix added this to the 0.2.4 milestone Dec 1, 2021
@abhiTronix abhiTronix added this to Backlog in VidGear v0.2.4 via automation Dec 1, 2021
@abhiTronix
Copy link
Owner

@megha-seventhsense This issue is being worked on and will be resolved soon. Please stay tuned.

@abhiTronix abhiTronix reopened this Dec 1, 2021
VidGear v0.2.4 automation moved this from Backlog to In progress Dec 1, 2021
abhiTronix added a commit that referenced this issue Dec 1, 2021
…ng Class. (Fixes #258)

💥 Removed support for assigning Custom Media Server Class(inherited from aiortc's VideoStreamTrack) in WebGear_RTC through its `config` global parameter.

- ✨ Added new `custom_stream` attribute with WebGear_RTC `options` parameter that allows you to easily define your own Custom Streaming Class with suitable source(such as OpenCV).
- ⚡️ This implementation supports repeated Auto-Reconnection or Auto-Refresh out-of-the-box.
- 🧑‍💻 This implementation is more user-friendly and easy to integrate within complex APIs.
- 🎨 This implementation supports all vidgear's VideoCapture APIs readily as input.
- 🏗️ This implementation requires at-least `read()` and `stop()` methods implemented within Custom Streaming Class, otherwise WebGear_RTC will throw ValueError.
- 🥚 WebGear_RTC API now automatically constructs `av.frame.Frame` from `numpy.nd.array` based on available channels in frames.
- 🏗️ WebGear_RTC API will now throws ValueError if `source` parameter is None and `custom_stream` attribute isn't defined.

CI:
- 👷 Updated CI tests for new WebGear_RTC custom streaming class.

Docs:
- 📝 Added related usage docs for new WebGear_RTC custom streaming class.
- 📝 Updated Advanced examples using WebGear_RTC's custom streaming class.
- 👽️ Added changes for upgrading mkdocs-material from 7.x to 8.x in docs.
- 🚸 Added outdated version warning block.
- 🐛 Fixed content tabs failing to work.
- 🚩 Updated WebGear_RTC parameters.
- 🔥 Removed slugify from mkdocs causing invalid hyperlinks in docs.
- 🐛 Fixed hyperlink in announcement bar.
- 💄 Updated code highlighting.
@abhiTronix
Copy link
Owner

Successfully resolved and merged in commit: cd481c1

VidGear v0.2.4 automation moved this from In progress to Done Dec 2, 2021
@abhiTronix abhiTronix added SOLVED 🏁 This issue/PR is resolved now. Goal Achieved! and removed WORK IN PROGRESS 🚧 currently been worked on. labels Dec 2, 2021
@abhiTronix
Copy link
Owner

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
ENHANCEMENT ⚡ New Feature/Addition/Improvement PROPOSAL 📩 A proposal/proposition SOLVED 🏁 This issue/PR is resolved now. Goal Achieved!
Projects
No open projects
Development

Successfully merging a pull request may close this issue.

2 participants