av/rtmp/parseurl.go

114 lines
2.8 KiB
Go
Raw Normal View History

/*
NAME
parseurl.go
DESCRIPTION
See Readme.md
AUTHOR
Dan Kortschak <dan@ausocean.org>
Saxon Nelson-Milton <saxon@ausocean.org>
2019-01-11 02:57:41 +03:00
Alan Noble <alan@ausocean.org>
LICENSE
2019-01-11 02:57:41 +03:00
parseurl.go is Copyright (C) 2017-2019 the Australian Ocean Lab (AusOcean)
It is free software: you can redistribute it and/or modify them
under the terms of the GNU General Public License as published by the
Free Software Foundation, either version 3 of the License, or (at your
option) any later version.
It is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
for more details.
You should have received a copy of the GNU General Public License
along with revid in gpl.txt. If not, see http://www.gnu.org/licenses.
Derived from librtmp under the GNU Lesser General Public License 2.1
Copyright (C) 2005-2008 Team XBMC http://www.xbmc.org
Copyright (C) 2008-2009 Andrej Stepanchuk
Copyright (C) 2009-2010 Howard Chu
*/
package rtmp
import (
"net/url"
"path"
"strconv"
"strings"
)
// parseURL parses an RTMP URL (ok, technically it is lexing).
func parseURL(addr string) (protocol int32, host string, port uint16, app, playpath string, err error) {
u, err := url.Parse(addr)
if err != nil {
return protocol, host, port, app, playpath, err
}
switch u.Scheme {
case "rtmp":
2019-01-11 02:57:41 +03:00
protocol = protoRTMP
case "rtmpt":
2019-01-11 02:57:41 +03:00
protocol = protoRTMPT
case "rtmps":
2019-01-11 02:57:41 +03:00
protocol = protoRTMPS
case "rtmpe":
2019-01-11 02:57:41 +03:00
protocol = protoRTMPE
case "rtmfp":
2019-01-11 02:57:41 +03:00
protocol = protoRTMFP
case "rtmpte":
2019-01-11 02:57:41 +03:00
protocol = protoRTMPTE
case "rtmpts":
2019-01-11 02:57:41 +03:00
protocol = protoRTMPTS
default:
return protocol, host, port, app, playpath, errUnknownScheme
}
2018-09-07 14:10:46 +03:00
host = u.Host
if p := u.Port(); p != "" {
pi, err := strconv.Atoi(p)
2018-09-07 00:31:59 +03:00
if err != nil {
return protocol, host, port, app, playpath, err
2018-09-07 00:31:59 +03:00
}
port = uint16(pi)
}
if !path.IsAbs(u.Path) {
return protocol, host, port, app, playpath, nil
}
elems := strings.SplitN(u.Path[1:], "/", 3)
2018-09-07 14:10:46 +03:00
app = elems[0]
if app == "" {
return protocol, host, port, app, playpath, errInvalidURL
}
2018-09-07 14:10:46 +03:00
playpath = elems[1]
if len(elems) == 3 && len(elems[2]) != 0 {
2018-09-07 14:10:46 +03:00
playpath = path.Join(elems[1:]...)
switch ext := path.Ext(playpath); ext {
case ".f4v", ".mp4":
2018-09-07 14:10:46 +03:00
playpath = "mp4:" + playpath[:len(playpath)-len(ext)]
case ".mp3":
2018-09-07 14:10:46 +03:00
playpath = "mp3:" + playpath[:len(playpath)-len(ext)]
case ".flv":
2018-09-07 14:10:46 +03:00
playpath = playpath[:len(playpath)-len(ext)]
}
if u.RawQuery != "" {
2018-09-07 14:10:46 +03:00
playpath += "?" + u.RawQuery
}
}
switch {
case port != 0:
case (protocol & featureSSL) != 0:
return protocol, host, port, app, playpath, errUnimplemented // port = 433
case (protocol & featureHTTP) != 0:
port = 80
default:
port = 1935
}
return protocol, host, port, app, playpath, nil
}