2020-10-02 10:12:47 +03:00
|
|
|
package rtmp
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"regexp"
|
2020-11-17 07:14:22 +03:00
|
|
|
"strings"
|
2020-10-02 10:12:47 +03:00
|
|
|
|
|
|
|
"github.com/nareix/joy5/format/flv/flvio"
|
2020-11-15 05:39:53 +03:00
|
|
|
"github.com/owncast/owncast/models"
|
2021-06-05 06:09:43 +03:00
|
|
|
log "github.com/sirupsen/logrus"
|
2020-10-02 10:12:47 +03:00
|
|
|
)
|
|
|
|
|
2020-12-17 10:01:09 +03:00
|
|
|
const unknownString = "Unknown"
|
|
|
|
|
2020-10-02 10:12:47 +03:00
|
|
|
func getInboundDetailsFromMetadata(metadata []interface{}) (models.RTMPStreamMetadata, error) {
|
|
|
|
metadataComponentsString := fmt.Sprintf("%+v", metadata)
|
2020-12-06 05:33:17 +03:00
|
|
|
if !strings.Contains(metadataComponentsString, "onMetaData") {
|
|
|
|
return models.RTMPStreamMetadata{}, errors.New("Not a onMetaData message")
|
2020-11-17 07:14:22 +03:00
|
|
|
}
|
2020-10-02 10:12:47 +03:00
|
|
|
re := regexp.MustCompile(`\{(.*?)\}`)
|
|
|
|
submatchall := re.FindAllString(metadataComponentsString, 1)
|
|
|
|
|
|
|
|
if len(submatchall) == 0 {
|
|
|
|
return models.RTMPStreamMetadata{}, errors.New("unable to parse inbound metadata")
|
|
|
|
}
|
|
|
|
|
|
|
|
metadataJSONString := submatchall[0]
|
|
|
|
var details models.RTMPStreamMetadata
|
2020-11-15 05:39:53 +03:00
|
|
|
err := json.Unmarshal([]byte(metadataJSONString), &details)
|
|
|
|
return details, err
|
2020-10-02 10:12:47 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func getAudioCodec(codec interface{}) string {
|
2020-12-02 11:19:55 +03:00
|
|
|
if codec == nil {
|
|
|
|
return "No audio"
|
|
|
|
}
|
|
|
|
|
2020-10-02 10:12:47 +03:00
|
|
|
var codecID float64
|
|
|
|
if assertedCodecID, ok := codec.(float64); ok {
|
|
|
|
codecID = assertedCodecID
|
|
|
|
} else {
|
|
|
|
return codec.(string)
|
|
|
|
}
|
|
|
|
|
|
|
|
switch codecID {
|
|
|
|
case flvio.SOUND_MP3:
|
|
|
|
return "MP3"
|
|
|
|
case flvio.SOUND_AAC:
|
|
|
|
return "AAC"
|
|
|
|
case flvio.SOUND_SPEEX:
|
|
|
|
return "Speex"
|
|
|
|
}
|
|
|
|
|
2020-12-17 10:01:09 +03:00
|
|
|
return unknownString
|
2020-10-02 10:12:47 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func getVideoCodec(codec interface{}) string {
|
2020-12-07 01:28:00 +03:00
|
|
|
if codec == nil {
|
2020-12-17 10:01:09 +03:00
|
|
|
return unknownString
|
2020-12-07 01:28:00 +03:00
|
|
|
}
|
|
|
|
|
2020-10-02 10:12:47 +03:00
|
|
|
var codecID float64
|
|
|
|
if assertedCodecID, ok := codec.(float64); ok {
|
|
|
|
codecID = assertedCodecID
|
|
|
|
} else {
|
|
|
|
return codec.(string)
|
|
|
|
}
|
|
|
|
|
|
|
|
switch codecID {
|
|
|
|
case flvio.VIDEO_H264:
|
|
|
|
return "H.264"
|
|
|
|
case flvio.VIDEO_H265:
|
|
|
|
return "H.265"
|
|
|
|
}
|
|
|
|
|
2020-12-17 10:01:09 +03:00
|
|
|
return unknownString
|
2020-10-02 10:12:47 +03:00
|
|
|
}
|
2021-06-05 06:09:43 +03:00
|
|
|
|
|
|
|
func secretMatch(configStreamKey string, path string) bool {
|
|
|
|
prefix := "/live/"
|
|
|
|
|
|
|
|
if !strings.HasPrefix(path, prefix) {
|
|
|
|
log.Debug("RTMP path does not start with " + prefix)
|
|
|
|
return false // We need the path to begin with $prefix
|
|
|
|
}
|
|
|
|
|
|
|
|
streamingKey := path[len(prefix):] // Remove $prefix
|
|
|
|
return streamingKey == configStreamKey
|
|
|
|
}
|