RTSPtoWebRTC icon indicating copy to clipboard operation
RTSPtoWebRTC copied to clipboard

AAC Audio support

Open hengzheoduyou opened this issue 2 years ago • 5 comments

use https://github.com/deepch/vdk/blob/master/cgo/ffmpeg/audio.go can convert AAC to OPUS , why you not add this in this example ,and in your RTSPtoWeb not convrt . WebRTC not support AAC ,and aac auto convert to opus is must.

hengzheoduyou avatar Aug 28 '22 11:08 hengzheoduyou

How to do that,could you please give a code example?

Mosphere avatar Dec 08 '22 06:12 Mosphere

@Mosphere I'm using this:

https://gist.github.com/dbl0null/15dc217131e014a5deefcd7b3d68d94d

And transcode anything to opus like that:


func (element *ConfigST) coAd(suuid string, srcCodecs []av.CodecData) {
	element.mutex.Lock()
	defer element.mutex.Unlock()
	stream := element.Streams[suuid]

	var dstCodecs []av.CodecData

	for _, srcCodec := range srcCodecs {
		if srcCodec.Type().IsVideo() {
			log.Println(suuid, "Video Codec:", srcCodec.Type().String(), srcCodec.(av.VideoCodecData).Height(), srcCodec.(av.VideoCodecData).Width())
			dstCodecs = append(dstCodecs, srcCodec)
		}
		if srcCodec.Type().IsAudio() {
			log.Println(suuid, "Audio Codec [OLD]:", srcCodec.Type().String(), srcCodec.(av.AudioCodecData).ChannelLayout(), srcCodec.(av.AudioCodecData).SampleRate(), srcCodec.(av.AudioCodecData).SampleFormat())
			if err := stream.Transcoder.Setup(srcCodec); err != nil {
				log.Println("Error making transcoder", err)
				continue
			}
			log.Println(suuid, "Audio Codec [NEW]:", stream.Transcoder.EncoderData.(av.AudioCodecData).Type().String(), stream.Transcoder.EncoderData.ChannelLayout(), stream.Transcoder.EncoderData.SampleRate(), stream.Transcoder.EncoderData.SampleFormat())
			dstCodecs = append(dstCodecs, stream.Transcoder.EncoderData)
		}
	}

	stream.Codecs = dstCodecs
	element.Streams[suuid] = stream
}

func (element *ConfigST) cast(uuid string, pck av.Packet) {
	element.mutex.Lock()
	defer element.mutex.Unlock()
	stream := element.Streams[uuid]
	for _, v := range stream.Cl {
		if len(v.c) < cap(v.c) {
			c := stream.Codecs[pck.Idx]

			if c != nil && c.Type().IsVideo() {
				v.c <- pck
				continue
			}

			if c != nil && c.Type().IsAudio() && stream.Transcoder.Ready {
				vals, e := stream.Transcoder.Do(&pck)
				if e != nil {
					log.Println("Error transcoding: %w", e)
					continue
				}
				//log.Println("OK transcoding")
				for _, val := range vals {
					v.c <- *val
				}
			}
		}
	}
}

dbl0null avatar Dec 08 '22 07:12 dbl0null

@Mosphere I'm using this:

https://gist.github.com/dbl0null/15dc217131e014a5deefcd7b3d68d94d

And transcode anything to opus like that:


func (element *ConfigST) coAd(suuid string, srcCodecs []av.CodecData) {
	element.mutex.Lock()
	defer element.mutex.Unlock()
	stream := element.Streams[suuid]

	var dstCodecs []av.CodecData

	for _, srcCodec := range srcCodecs {
		if srcCodec.Type().IsVideo() {
			log.Println(suuid, "Video Codec:", srcCodec.Type().String(), srcCodec.(av.VideoCodecData).Height(), srcCodec.(av.VideoCodecData).Width())
			dstCodecs = append(dstCodecs, srcCodec)
		}
		if srcCodec.Type().IsAudio() {
			log.Println(suuid, "Audio Codec [OLD]:", srcCodec.Type().String(), srcCodec.(av.AudioCodecData).ChannelLayout(), srcCodec.(av.AudioCodecData).SampleRate(), srcCodec.(av.AudioCodecData).SampleFormat())
			if err := stream.Transcoder.Setup(srcCodec); err != nil {
				log.Println("Error making transcoder", err)
				continue
			}
			log.Println(suuid, "Audio Codec [NEW]:", stream.Transcoder.EncoderData.(av.AudioCodecData).Type().String(), stream.Transcoder.EncoderData.ChannelLayout(), stream.Transcoder.EncoderData.SampleRate(), stream.Transcoder.EncoderData.SampleFormat())
			dstCodecs = append(dstCodecs, stream.Transcoder.EncoderData)
		}
	}

	stream.Codecs = dstCodecs
	element.Streams[suuid] = stream
}

func (element *ConfigST) cast(uuid string, pck av.Packet) {
	element.mutex.Lock()
	defer element.mutex.Unlock()
	stream := element.Streams[uuid]
	for _, v := range stream.Cl {
		if len(v.c) < cap(v.c) {
			c := stream.Codecs[pck.Idx]

			if c != nil && c.Type().IsVideo() {
				v.c <- pck
				continue
			}

			if c != nil && c.Type().IsAudio() && stream.Transcoder.Ready {
				vals, e := stream.Transcoder.Do(&pck)
				if e != nil {
					log.Println("Error transcoding: %w", e)
					continue
				}
				//log.Println("OK transcoding")
				for _, val := range vals {
					v.c <- *val
				}
			}
		}
	}
}

the package github.com/dbl0null/rt/internal/cgo/ffmpeg is not found in file transcoder.go

Mosphere avatar Dec 20 '22 06:12 Mosphere

How to do that,could you please give a code example?

you must download vdk and update some file https://github.com/deepch/vdk/blob/master/cgo/ffmpeg/audio.go `func NewAudioEncoderByCodecType(typ av.CodecType) (enc *AudioEncoder, err error) { var id uint32

switch typ {
case av.AAC:
	id = C.AV_CODEC_ID_AAC
case av.OPUS:
	id = C.AV_CODEC_ID_OPUS
default:
	err = fmt.Errorf("ffmpeg: cannot find encoder codecType=%d", typ)
	return
}`

and in https://github.com/deepch/vdk/blob/master/format/rtspv2/client.go convert aac to opus

hengzheoduyou avatar Dec 22 '22 23:12 hengzheoduyou

does it supported?

evan-cai avatar Feb 22 '23 06:02 evan-cai