Skip to content

Commit

Permalink
Merge pull request #339 from yousong/mp4-conc
Browse files Browse the repository at this point in the history
Fixes for working with rtsp stream of TL-IPC44GW
  • Loading branch information
AlexxIT committed Apr 14, 2023
2 parents abbf180 + cf6a35d commit 93be5cd
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 12 deletions.
13 changes: 9 additions & 4 deletions cmd/mp4/mp4.go
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
package mp4

import (
"net/http"
"strconv"
"strings"
"sync"
"time"

"github.com/AlexxIT/go2rtc/cmd/api"
"github.com/AlexxIT/go2rtc/cmd/app"
"github.com/AlexxIT/go2rtc/cmd/streams"
"github.com/AlexxIT/go2rtc/pkg/core"
"github.com/AlexxIT/go2rtc/pkg/mp4"
"github.com/AlexxIT/go2rtc/pkg/tcp"
"github.com/rs/zerolog"
"net/http"
"strconv"
"strings"
"time"
)

func Init() {
Expand Down Expand Up @@ -111,8 +113,11 @@ func handlerMP4(w http.ResponseWriter, r *http.Request) {
Medias: core.ParseQuery(r.URL.Query()),
}

mu := &sync.Mutex{}
cons.Listen(func(msg any) {
if data, ok := msg.([]byte); ok {
mu.Lock()
defer mu.Unlock()
if _, err := w.Write(data); err != nil && exit != nil {
exit <- err
exit = nil
Expand Down
8 changes: 5 additions & 3 deletions cmd/rtsp/rtsp.go
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
package rtsp

import (
"net"
"net/url"
"strings"

"github.com/AlexxIT/go2rtc/cmd/app"
"github.com/AlexxIT/go2rtc/cmd/streams"
"github.com/AlexxIT/go2rtc/pkg/core"
"github.com/AlexxIT/go2rtc/pkg/mp4"
"github.com/AlexxIT/go2rtc/pkg/rtsp"
"github.com/AlexxIT/go2rtc/pkg/tcp"
"github.com/rs/zerolog"
"net"
"net/url"
"strings"
)

func Init() {
Expand Down Expand Up @@ -123,6 +124,7 @@ func rtspHandler(url string) (core.Producer, error) {
if !backchannel {
return nil, err
}
log.Trace().Msgf("[rtsp] describe (backchannel=%t) err: %v", backchannel, err)

// second try without backchannel, we need to reconnect
conn.Backchannel = false
Expand Down
6 changes: 4 additions & 2 deletions pkg/rtsp/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,15 @@ import (
"crypto/tls"
"errors"
"fmt"
"github.com/AlexxIT/go2rtc/pkg/core"
"github.com/AlexxIT/go2rtc/pkg/tcp"
"net"
"net/http"
"net/url"
"strconv"
"strings"
"time"

"github.com/AlexxIT/go2rtc/pkg/core"
"github.com/AlexxIT/go2rtc/pkg/tcp"
)

func NewClient(uri string) *Conn {
Expand Down Expand Up @@ -309,6 +310,7 @@ func (c *Conn) SetupMedia(media *core.Media, first bool) (byte, error) {
// some Dahua/Amcrest cameras fail here because two simultaneous
// backchannel connections
if c.Backchannel {
c.Close()
c.Backchannel = false
if err := c.Dial(); err != nil {
return 0, err
Expand Down
11 changes: 8 additions & 3 deletions pkg/rtsp/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@ package rtsp

import (
"bytes"
"github.com/AlexxIT/go2rtc/pkg/core"
"github.com/pion/rtcp"
"github.com/pion/sdp/v3"
"io"
"net/url"
"regexp"
"strconv"
"strings"

"github.com/AlexxIT/go2rtc/pkg/core"
"github.com/pion/rtcp"
"github.com/pion/sdp/v3"
)

type RTCP struct {
Expand All @@ -35,6 +36,10 @@ func UnmarshalSDP(rawSDP []byte) ([]*core.Media, error) {
rawSDP = append([]byte(sdpHeader), rawSDP[i:]...)
}

// Fix invalid media type (errSDPInvalidValue) caused by
// some TP-LINK IP camera, e.g. TL-IPC44GW
rawSDP = bytes.ReplaceAll(rawSDP, []byte("m=application/TP-LINK "), []byte("m=application "))

if err == io.EOF {
rawSDP = append(rawSDP, '\n')
}
Expand Down

0 comments on commit 93be5cd

Please sign in to comment.