forked from prominenceai/deepstream-services-library
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path1file_webrtc_connect_post_play.py
206 lines (169 loc) · 7.05 KB
/
1file_webrtc_connect_post_play.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
################################################################################
# The MIT License
#
# Copyright (c) 2021, Prominence AI, Inc.
#
# Permission is hereby granted, free of charge, to any person obtaining a
# copy of this software and associated documentation files (the "Software"),
# to deal in the Software without restriction, including without limitation
# the rights to use, copy, modify, merge, publish, distribute, sublicense,
# and/or sell copies of the Software, and to permit persons to whom the
# Software is furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
# THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
# DEALINGS IN THE SOFTWARE.
################################################################################
#!/usr/bin/env python
import sys
from dsl import *
#-------------------------------------------------------------------------------------------
#
# This script demonstrates the use of a WebRtc Sink
# File path for the single File Source
file_path = '/opt/nvidia/deepstream/deepstream/samples/streams/sample_qHD.mp4'
stun_server = "stun://stun.l.google.com:19302"
# Window Sink Dimensions
sink_width = 640
sink_height = 360
CUR_SINK_NUMBER = 0
MAX_SINK_NUMBER = 4
##
# Function to be called on XWindow KeyRelease event
##
def xwindow_key_event_handler(key_string, client_data):
print('key released = ', key_string)
if key_string.upper() == 'C':
print('closing connection')
dsl_sink_webrtc_connection_close('webrtc-sink')
elif key_string.upper() == 'P':
dsl_pipeline_pause('pipeline')
elif key_string.upper() == 'R':
dsl_pipeline_play('pipeline')
elif key_string.upper() == 'Q' or key_string == '' or key_string == '':
dsl_pipeline_stop('pipeline')
dsl_main_loop_quit()
##
# Function to be called on XWindow Delete event
##
def xwindow_delete_event_handler(client_data):
print('delete window event')
dsl_pipeline_stop('pipeline')
dsl_main_loop_quit()
# Function to be called on End-of-Stream (EOS) event
def eos_event_listener(client_data):
print('Pipeline EOS event')
# dsl_main_loop_quit()
##
# Function to be called on every change of Pipeline state
##
def state_change_listener(old_state, new_state, client_data):
print('previous state = ', old_state, ', new state = ', new_state)
if new_state == DSL_STATE_PLAYING:
dsl_pipeline_dump_to_dot('pipeline', "state-playing")
def webrtc_sink_client_listener(connection_data_ptr, client_data):
connection_data = connection_data_ptr.contents
print('Current connection state for the WebRTC Sink is =',
connection_data.current_state)
##
# Function to be called on every socket connection event
##
def websocket_server_client_listener_cb(path, client_data):
global CUR_SINK_NUMBER, MAX_SINK_NUMBER
print("Incoming Websocket connection event with path =", path)
if CUR_SINK_NUMBER == MAX_SINK_NUMBER:
print('maximum number of WebRTC Sinks have been created')
return
CUR_SINK_NUMBER += 1
sink_name = 'webrtc-sink-{}'.format(CUR_SINK_NUMBER)
print(sink_name)
# New WebRTC Sink with .
retval = dsl_sink_webrtc_new(sink_name,
stun_server = stun_server,
turn_server = None,
codec = DSL_CODEC_H264,
bitrate = 4000000,
interval = 0)
if retval != DSL_RETURN_SUCCESS:
print('failed to create new WebRTC Sink')
return
# Add the client listener callback function to the WebRTC Sink
retval = dsl_sink_webrtc_client_listener_add(sink_name,
webrtc_sink_client_listener, None)
if retval != DSL_RETURN_SUCCESS:
print('failed to add client listner to sink')
return
retval = dsl_pipeline_component_add('pipeline', sink_name)
if retval != DSL_RETURN_SUCCESS:
print('failed to add new WebRTC Sink to Pipeline')
return
def main(args):
# Since we're not using args, we can Let DSL initialize GST on first call
while True:
# New RTSP Source
# retval = dsl_source_rtsp_new('source',
# uri = rtsp_uri,
# protocol = DSL_RTP_ALL,
# cudadec_mem_type = DSL_NVBUF_MEM_TYPE_DEFAULT,
# skip_frames = 0,
# drop_frame_interval = 0,
# latency = 100,
# timeout = 5)
# if (retval != DSL_RETURN_SUCCESS):
# break
# New File Source using the file path specified above, repeat enabled.
retval = dsl_source_file_new('source', file_path, True)
if retval != DSL_RETURN_SUCCESS:
break
# New Window Sink, 0 x/y offsets and dimensions
retval = dsl_sink_window_egl_new('egl-sink', 0, 0, sink_width, sink_height)
if retval != DSL_RETURN_SUCCESS:
break
# Add the XWindow event handler functions defined above
retval = dsl_sink_window_key_event_handler_add("egl-sink",
xwindow_key_event_handler, None)
if retval != DSL_RETURN_SUCCESS:
break
retval = dsl_sink_window_delete_event_handler_add("egl-sink",
xwindow_delete_event_handler, None)
if retval != DSL_RETURN_SUCCESS:
break
# Add the window sink to a new pipeline
retval = dsl_pipeline_new_component_add_many('pipeline',
['source', 'egl-sink', None])
if retval != DSL_RETURN_SUCCESS:
break
# Add the listener callback functions defined above
retval = dsl_pipeline_state_change_listener_add('pipeline',
state_change_listener, None)
if retval != DSL_RETURN_SUCCESS:
break
retval = dsl_pipeline_eos_listener_add('pipeline',
eos_event_listener, None)
if retval != DSL_RETURN_SUCCESS:
break
# Play the pipeline
retval = dsl_pipeline_play('pipeline')
if retval != DSL_RETURN_SUCCESS:
break
# Add the client listener to the Websocket Server to listen for socket connections
retval = dsl_websocket_server_client_listener_add(websocket_server_client_listener_cb, None)
# Start the Websocket Server listening on the default port number
retval = dsl_websocket_server_listening_start(DSL_WEBSOCKET_SERVER_DEFAULT_HTTP_PORT)
if retval != DSL_RETURN_SUCCESS:
break
# Join with main loop until released - blocking call
dsl_main_loop_run()
break
# Print out the final result
print(dsl_return_value_to_string(retval))
dsl_delete_all()
if __name__ == '__main__':
sys.exit(main(sys.argv))