-
-
Notifications
You must be signed in to change notification settings - Fork 5.4k
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
Forward: support config full rtmp url forward to other server #2799
Merged
winlinvip
merged 6 commits into
ossrs:develop
from
chundonglinlin:feature/multi-forward
Feb 16, 2022
Merged
Changes from 3 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
931d19e
Forward: add backend config and demo server for dynamic create forwar…
chundonglinlin 6458e37
Forward: if call forward backend failed, then return directly.
chundonglinlin d1894c3
Forward: add API description and change return value format.
chundonglinlin 45c27bb
Forward: add backend conf file and wrapper function for backend service.
chundonglinlin 66af013
Forward: add backend comment in full.conf and update forward.backend.…
chundonglinlin 77a14e1
Forward: rename backend param and add comment tips.
chundonglinlin File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -805,6 +805,176 @@ def POST(self): | |
def OPTIONS(self, *args, **kwargs): | ||
enable_crossdomain() | ||
|
||
''' | ||
handle the forward requests: dynamic forward url. | ||
''' | ||
class RESTForward(object): | ||
exposed = True | ||
|
||
def __init__(self): | ||
self.__forwards = [] | ||
|
||
# match vhost+app+stream to forward url | ||
self.__forwards.append({ | ||
"vhost":"ossrs.net", | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Change to slave,
|
||
"app":"live", | ||
"stream":"livestream", | ||
"url":"push.ossrs.com", | ||
}) | ||
|
||
# match app+stream to forward url | ||
self.__forwards.append({ | ||
"vhost":"", | ||
"app":"live", | ||
"stream":"livestream", | ||
"url":"push.ossrs.com", | ||
}) | ||
|
||
# match app+stream to forward url | ||
self.__forwards.append({ | ||
"vhost":"", | ||
"app":"live", | ||
"stream":"livestream", | ||
"url":"rtmp://push.ossrs.com/test/teststream?auth_token=123456", | ||
}) | ||
|
||
def GET(self): | ||
enable_crossdomain() | ||
|
||
forwards = {} | ||
return json.dumps(forwards) | ||
|
||
''' | ||
for SRS hook: on_forward | ||
on_forward: | ||
when srs reap a dvr file, call the hook, | ||
the request in the POST data string is a object encode by json: | ||
{ | ||
"action": "on_forward", | ||
"server_id": "server_test", | ||
"client_id": 1985, | ||
"ip": "192.168.1.10", | ||
"vhost": "video.test.com", | ||
"app": "live", | ||
"tcUrl": "rtmp://video.test.com/live?key=d2fa801d08e3f90ed1e1670e6e52651a", | ||
"stream": "livestream", | ||
"param":"?token=xxx&salt=yyy" | ||
} | ||
if valid, the hook must return HTTP code 200(Stauts OK) and response | ||
an int value specifies the error code(0 corresponding to success): | ||
0 | ||
''' | ||
def POST(self): | ||
enable_crossdomain() | ||
|
||
# return the error code in str | ||
code = Error.success | ||
|
||
req = cherrypy.request.body.read() | ||
trace("post to forwards, req=%s"%(req)) | ||
try: | ||
json_req = json.loads(req) | ||
except Exception, ex: | ||
code = Error.system_parse_json | ||
trace("parse the request to json failed, req=%s, ex=%s, code=%s"%(req, ex, code)) | ||
return json.dumps({"code": int(code), "data": None}) | ||
|
||
action = json_req["action"] | ||
if action == "on_forward": | ||
return self.__on_forward(json_req) | ||
else: | ||
trace("invalid request action: %s"%(json_req["action"])) | ||
code = Error.request_invalid_action | ||
|
||
return json.dumps({"code": int(code), "data": None}) | ||
|
||
def OPTIONS(self, *args, **kwargs): | ||
enable_crossdomain() | ||
|
||
def __on_forward(self, req): | ||
code = Error.success | ||
|
||
trace("srs %s: client id=%s, ip=%s, vhost=%s, app=%s, tcUrl=%s, stream=%s, param=%s"%( | ||
req["action"], req["client_id"], req["ip"], req["vhost"], req["app"], req["tcUrl"], req["stream"], req["param"] | ||
)) | ||
|
||
# dynamic create forward config | ||
forwards = [] | ||
destinations = [] | ||
|
||
# handle param: ?forward=xxxxx&auth_token=xxxxx | ||
# 1.delete ? | ||
req_param = req["param"].replace('?', '', 1) | ||
|
||
# 2.delete 'forward=xxxxx' | ||
new_req_param = "" | ||
params = req_param.split("&") | ||
for param in params: | ||
result = param.split("=") | ||
if result[0].find("forward") != -1: | ||
destinations.append(result[1],) | ||
elif len(new_req_param) > 0: | ||
new_req_param = new_req_param + "&" + param | ||
else: | ||
new_req_param = param | ||
|
||
# secne: dynamic config | ||
for forward in self.__forwards: | ||
# empty forward url | ||
if forward["url"] is None: | ||
continue | ||
|
||
# vhost exist | ||
if len(forward['vhost']) > 0 and req['vhost'] != forward['vhost']: | ||
continue | ||
# app exist | ||
if len(forward["app"]) > 0 and req["app"] != forward["app"]: | ||
continue | ||
# stream exist | ||
if len(forward["stream"]) > 0 and req["stream"] != forward["stream"]: | ||
continue | ||
|
||
# url maybe spell full rtmp address | ||
url = forward["url"] | ||
if url.find("rtmp://") == -1: | ||
# format: xxx:xxx | ||
# maybe you should use destination config | ||
url = "rtmp://%s/%s"%(url, req['app']) | ||
if len(req['vhost']) > 0 and req['vhost'] != "__defaultVhost__" and url.find(req['vhost']) == -1: | ||
url = url + "?vhost=" + req['vhost'] | ||
url = url + "/" + req['stream'] | ||
if len(new_req_param) > 0: | ||
url = url + "?" + new_req_param | ||
|
||
# append | ||
forwards.append(url) | ||
|
||
#trace log | ||
trace("add dynamic forward url: %s"%(url)) | ||
|
||
# secne: parse client params, like: | ||
# format1: rtmp://srs-server/live/stream?forward=ossrs.com:1936&token=xxxxxx | ||
# format2: rtmp://srs-server/live/stream?forward=rtmp://cdn.com/myapp/mystream?XXXXXX | ||
for dest in destinations: | ||
url = dest | ||
if url.find("rtmp://") == -1: | ||
# format: xxx:xxx | ||
# maybe you should use destination config | ||
url = "rtmp://%s/%s"%(url, req['app']) | ||
if len(req['vhost']) > 0 and req['vhost'] != "__defaultVhost__" and url.find(req['vhost']) == -1: | ||
url = url + "?vhost=" + req['vhost'] | ||
url = url + "/" + req['stream'] | ||
if len(new_req_param) > 0: | ||
url = url + "?" + new_req_param | ||
|
||
# append | ||
forwards.append(url) | ||
|
||
#trace log | ||
trace("add client params forward url: %s, %s"%(url, req_param)) | ||
|
||
return json.dumps({"code": int(code), "data": {"urls": forwards}}) | ||
winlinvip marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
# HTTP RESTful path. | ||
class Root(object): | ||
exposed = True | ||
|
@@ -846,6 +1016,7 @@ def __init__(self): | |
self.chats = RESTChats() | ||
self.servers = RESTServers() | ||
self.snapshots = RESTSnapshots() | ||
self.forward = RESTForward() | ||
def GET(self): | ||
enable_crossdomain(); | ||
return json.dumps({"code":Error.success, "urls":{ | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Change it to slave.
TRANS_BY_GPT3