From 32f5465ec993900cc7872d6a7ae6390ac75ab521 Mon Sep 17 00:00:00 2001 From: q191201771 <191201771@qq.com> Date: Sun, 4 Jul 2021 20:25:28 +0800 Subject: [PATCH] =?UTF-8?q?(#82)=20[feat]=20sdp=E6=89=93=E5=8C=85?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pkg/base/version.go | 5 +++ pkg/sdp/avconfig.go | 14 ++------- pkg/sdp/pack.go | 75 +++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 82 insertions(+), 12 deletions(-) create mode 100644 pkg/sdp/pack.go diff --git a/pkg/base/version.go b/pkg/base/version.go index 671237f..ed351f9 100644 --- a/pkg/base/version.go +++ b/pkg/base/version.go @@ -73,6 +73,9 @@ var ( // e.g. lal/0.12.3 LalRtspPullSessionUa string + + // e.g. lal 0.12.3 + LalPackSdp string ) // - rtmp handshake random buf @@ -124,4 +127,6 @@ func init() { LalRtspPullSessionUa = LalLibraryName + "/" + LalVersionDot LalRtmpHandshakeWaterMark = LalFullInfo + + LalPackSdp = LalLibraryName + " " + LalVersionDot } diff --git a/pkg/sdp/avconfig.go b/pkg/sdp/avconfig.go index f4ea13d..517d691 100644 --- a/pkg/sdp/avconfig.go +++ b/pkg/sdp/avconfig.go @@ -10,7 +10,7 @@ package sdp import ( "encoding/base64" - "strconv" + "encoding/hex" "strings" "github.com/q191201771/lal/pkg/base" @@ -28,17 +28,7 @@ func ParseAsc(a *AFmtPBase) ([]byte, error) { if len(v) < 4 || (len(v)%2) != 0 { return nil, ErrSdp } - l := len(v) / 2 - r := make([]byte, l) - for i := 0; i < l; i++ { - b, err := strconv.ParseInt(v[i*2:i*2+2], 16, 0) - if err != nil { - return nil, ErrSdp - } - r[i] = uint8(b) - } - - return r, nil + return hex.DecodeString(v) } func ParseVpsSpsPps(a *AFmtPBase) (vps, sps, pps []byte, err error) { diff --git a/pkg/sdp/pack.go b/pkg/sdp/pack.go new file mode 100644 index 0000000..87f0227 --- /dev/null +++ b/pkg/sdp/pack.go @@ -0,0 +1,75 @@ +// Copyright 2021, Chef. All rights reserved. +// https://github.com/q191201771/lal +// +// Use of this source code is governed by a MIT-style license +// that can be found in the License file. +// +// Author: Chef (191201771@qq.com) + +package sdp + +import ( + "encoding/base64" + "encoding/hex" + "fmt" + + "github.com/q191201771/lal/pkg/base" +) + +func Pack(vps, sps, pps, asc []byte) (ctx LogicContext, raw []byte, err error) { + var hasAudio, hasVideo, isHevc bool + + if sps != nil && pps != nil { + hasVideo = true + if vps != nil { + isHevc = true + } + } + if asc != nil { + hasAudio = true + } + + if !hasAudio && !hasVideo { + err = ErrSdp + return + } + + sdpStr := fmt.Sprintf(`v=0 + o=- 0 0 IN IP4 127.0.0.1 + s=No Name + c=IN IP4 127.0.0.1 + t=0 0 + a=tool:%s +`, base.LalPackSdp) + + streamid := 0 + + if hasVideo { + if isHevc { + + } else { + tmpl := `m=video 0 RTP/AVP 96 + a=rtpmap:96 H264/90000 + a=fmtp:96 packetization-mode=1; sprop-parameter-sets=%s,%s; profile-level-id=640016 + a=control:streamid=%d +` + sdpStr += fmt.Sprintf(tmpl, base64.StdEncoding.EncodeToString(sps), base64.StdEncoding.EncodeToString(pps), streamid) + } + + streamid++ + } + + if hasAudio { + tmpl := `m=audio 0 RTP/AVP 97 + b=AS:128 + a=rtpmap:97 MPEG4-GENERIC/44100/2 + a=fmtp:97 profile-level-id=1;mode=AAC-hbr;sizelength=13;indexlength=3;indexdeltalength=3; config=%s + a=control:streamid=%d +` + sdpStr += fmt.Sprintf(tmpl, hex.EncodeToString(asc), streamid) + } + + raw = []byte(sdpStr) + ctx, err = ParseSdp2LogicContext(raw) + return +}