Skip to content

Commit

Permalink
feat: expose rtp session
Browse files Browse the repository at this point in the history
  • Loading branch information
emiago committed Nov 22, 2024
1 parent 6b76caa commit 17659e6
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 10 deletions.
6 changes: 1 addition & 5 deletions diago.go
Original file line number Diff line number Diff line change
Expand Up @@ -684,11 +684,7 @@ func (dg *Diago) InviteBridge(ctx context.Context, recipient sip.Uri, bridge *Br
}

d.mu.Lock()
d.initMediaSessionUnsafe(
sess,
media.NewRTPPacketReaderSession(rtpSess),
media.NewRTPPacketWriterSession(rtpSess),
)
d.initRTPSessionUnsafe(sess, rtpSess)
d.onCloseUnsafe(func() {
if err := rtpSess.Close(); err != nil {
log.Error().Err(err).Msg("Closing session")
Expand Down
22 changes: 22 additions & 0 deletions dialog_media.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,10 @@ type DialogMedia struct {
// Only safe to use after dialog Answered (Completed state)
mediaSession *media.MediaSession

// rtp session is created for usage with RTPPacketReader and RTPPacketWriter
// it adds RTCP layer and RTP monitoring before passing packets to MediaSession
rtpSession *media.RTPSession

// Packet reader is default reader for RTP audio stream
// Use always AudioReader to get current Audio reader
// Use this only as read only
Expand Down Expand Up @@ -122,6 +126,13 @@ func (d *DialogMedia) initMediaSessionUnsafe(m *media.MediaSession, r *media.RTP
d.RTPPacketWriter = w
}

func (d *DialogMedia) initRTPSessionUnsafe(m *media.MediaSession, rtpSess *media.RTPSession) {
d.mediaSession = m
d.rtpSession = rtpSess
d.RTPPacketReader = media.NewRTPPacketReaderSession(rtpSess)
d.RTPPacketWriter = media.NewRTPPacketWriterSession(rtpSess)
}

func (d *DialogMedia) createMediaSession(formats sdp.Formats, bindIP net.IP) (*media.MediaSession, error) {
if bindIP == nil {
var err error
Expand All @@ -146,6 +157,14 @@ func (d *DialogMedia) createMediaSessionConf(conf MediaConfig) (*media.MediaSess
return sess, nil
}

// RTPSession returns underhood rtp session
// NOTE: this can be nil
func (d *DialogMedia) RTPSession() *media.RTPSession {
d.mu.Lock()
defer d.mu.Unlock()
return d.rtpSession
}

// Must be protected with lock
func (d *DialogMedia) sdpReInviteUnsafe(sdp []byte) error {
msess := d.mediaSession.Fork()
Expand All @@ -167,6 +186,9 @@ func (d *DialogMedia) sdpReInviteUnsafe(sdp []byte) error {
d.RTPPacketWriter.UpdateRTPSession(rtpSess)
rtpSess.MonitorBackground()

// hold the reference
d.rtpSession = rtpSess

log.Info().
Str("formats", msess.Formats.String()).
Str("localAddr", msess.Laddr.String()).
Expand Down
6 changes: 1 addition & 5 deletions dialog_server_session.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,11 +139,7 @@ func (d *DialogServerSession) answerSession(rtpSess *media.RTPSession) error {
}

d.mu.Lock()
d.initMediaSessionUnsafe(
sess,
media.NewRTPPacketReaderSession(rtpSess),
media.NewRTPPacketWriterSession(rtpSess),
)
d.initRTPSessionUnsafe(sess, rtpSess)
// Close RTP session
d.onCloseUnsafe(func() {
if err := rtpSess.Close(); err != nil {
Expand Down

0 comments on commit 17659e6

Please sign in to comment.