WebGear_RTC API Advanced Usage:¶
This is a continuation of the WebGear_RTC doc ➶. Thereby, It's advised to first get familiarize with this API, and its requirements.
After going through following Usage Examples, Checkout more bonus examples here ➶
Using WebGear_RTC as Real-time Broadcaster¶
WebGear_RTC by default only supports one-to-one peer connection with a single consumer or client. But you can use enable_live_broadcast
boolean attribute through its options dictionary parameter to easily enable live broadcast/stream to multiple peer consumers/clients at the same time.
Let's implement a bare-minimum example using WebGear_RTC as Real-time Broadcaster:
enable_infinite_frames
is enforced by default with this(enable_live_broadcast
) attribute.
For accessing WebGear_RTC on different Client Devices on the network, we use "0.0.0.0"
as host value instead of "localhost"
on Host Machine. More information can be found here ➶
And that's all, Now you can see output at http://localhost:8000/
address on your local machine.
Using WebGear_RTC with a Custom Source(OpenCV)¶
WebGear_RTC provides custom_stream
attribute with its options
parameter that allows you to easily define your own Custom Streaming Class with suitable source that you want to use to transform your frames before sending them onto the browser.
Let's implement a bare-minimum example with a Custom Source using WebGear_RTC API and OpenCV:
New in v0.2.4
This implementation was added in v0.2.4
.
Auto-Reconnection or Auto-Refresh works out-of-the-box with this implementation.
Make sure your Custom Streaming Class at-least implements read()
and stop()
methods as shown in following example, otherwise WebGear_RTC will throw ValueError!
Using Vidgear's VideoCapture APIs instead of OpenCV
You can directly replace Custom Streaming Class(Custom_Stream_Class
in following example) with any VideoCapture APIs. These APIs implements read()
and stop()
methods by-default, so they're also supported out-of-the-box.
See this example ➶ for more information.
And that's all, Now you can see output at http://localhost:8000/
address.
Using WebGear_RTC with Custom Mounting Points¶
With our highly extensible WebGear_RTC API, you can add your own mounting points, where additional files located, as follows:
Then you can use this folder in your HTML page, to host data-files. For example, if we have jQuery script jquery-3.3.1.slim.min.js
in this folder and want to integrate it, then, we can do something like this:
Using WebGear_RTC with Custom Webpage Routes¶
With Webgear_RTC's flexible API, you can even add your additional HTML Static webpages without any extra efforts.
Suppose we want to add a simple hello world
webpage to our WebGear_RTC server. So let's create a bare-minimum hello.html
file with HTML code as follows:
<html>
<header>
<title>This is Hello world page</title>
</header>
<body>
<h1>Hello World</h1>
<p>how ya doing?</p>
</body>
</html>
Then in our application code, we can integrate this webpage route, as follows:
http://localhost:8000/hello
address.
Using WebGear_RTC with MiddleWares¶
WebGear_RTC also natively supports ASGI middleware classes with Starlette for implementing behavior that is applied across your entire ASGI application easily.
New in v0.2.2
This example was added in v0.2.2
.
All supported middlewares can be found here ➶
For this example, let's use CORSMiddleware
for implementing appropriate CORS headers to outgoing responses in our application in order to allow cross-origin requests from browsers, as follows:
The default parameters used by the CORSMiddleware implementation are restrictive by default, so you'll need to explicitly enable particular origins, methods, or headers, in order for browsers to be permitted to use them in a Cross-Domain context.
Starlette provides several arguments for enabling origins, methods, or headers for CORSMiddleware API. More information can be found here ➶
And that's all, Now you can see output at http://localhost:8000
address.
Rules for Altering WebGear_RTC Files and Folders¶
WebGear_RTC gives us complete freedom of altering data files generated in Auto-Generation Process, But you've to keep the following rules in mind:
Rules for Altering Data Files¶
- You allowed to alter/change code in all existing default downloaded files at your convenience without any restrictions.
- You allowed to delete/rename all existing data files, except remember NOT to delete/rename three critical data-files (i.e
index.html
,404.html
&500.html
) present intemplates
folder inside thewebgear_rtc
directory at the default location, otherwise, it will trigger Auto-generation process, and it will overwrite the existing files with Server ones. - You're allowed to add your own additional
.html
,.css
,.js
, etc. files in the respective folders at the default location and custom mounted Data folders.
Rules for Altering Data Folders¶
- You're allowed to add/mount any number of additional folder as shown in this example above.
- You're allowed to delete/rename existing folders at the default location except remember NOT to delete/rename
templates
folder in thewebgear_rtc
directory where critical data-files (i.eindex.html
,404.html
&500.html
) are located, otherwise, it will trigger Auto-generation process.
Bonus Examples¶
Checkout more advanced WebGear_RTC examples with unusual configuration here ➶