|
| 1 | +package dahua |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/json" |
| 5 | + "fmt" |
| 6 | + "io" |
| 7 | + |
| 8 | + "github.com/AlexxIT/go2rtc/internal/app" |
| 9 | + "github.com/AlexxIT/go2rtc/pkg/core" |
| 10 | + "github.com/pion/rtp" |
| 11 | +) |
| 12 | + |
| 13 | +func (c *Client) GetMedias() []*core.Media { |
| 14 | + return c.medias |
| 15 | +} |
| 16 | + |
| 17 | +func (c *Client) GetTrack(media *core.Media, codec *core.Codec) (*core.Receiver, error) { |
| 18 | + return nil, core.ErrCantGetTrack |
| 19 | +} |
| 20 | + |
| 21 | +func (c *Client) AddTrack(media *core.Media, _ *core.Codec, track *core.Receiver) error { |
| 22 | + log := app.GetLogger("dahua") |
| 23 | + log.Debug(). |
| 24 | + Str("media_kind", string(media.Kind)). |
| 25 | + Str("codec", track.Codec.Name). |
| 26 | + Msg("[dahua] adding track") |
| 27 | + |
| 28 | + if c.sender == nil { |
| 29 | + c.sender = core.NewSender(media, track.Codec) |
| 30 | + c.sender.Handler = func(packet *rtp.Packet) { |
| 31 | + // Get connection in a thread-safe way |
| 32 | + conn := c.getConnection() |
| 33 | + if conn == nil { |
| 34 | + return |
| 35 | + } |
| 36 | + |
| 37 | + // Send multipart boundary with payload |
| 38 | + boundary := "\r\n--go2rtc-audio-boundary\r\n" |
| 39 | + |
| 40 | + // Determine content type based on codec |
| 41 | + var contentType string |
| 42 | + switch track.Codec.Name { |
| 43 | + case core.CodecPCMA: |
| 44 | + contentType = "Audio/G.711A" |
| 45 | + case core.CodecPCMU: |
| 46 | + contentType = "Audio/G.711Mu" |
| 47 | + default: |
| 48 | + contentType = "Audio/G.711A" |
| 49 | + } |
| 50 | + |
| 51 | + boundary += "Content-Type: " + contentType + "\r\n" |
| 52 | + boundary += "Content-Length: " + fmt.Sprintf("%d", len(packet.Payload)) + "\r\n\r\n" |
| 53 | + |
| 54 | + // Write boundary first |
| 55 | + if _, err := conn.Write([]byte(boundary)); err != nil { |
| 56 | + log.Error().Err(err).Msg("[dahua] failed to write boundary") |
| 57 | + return |
| 58 | + } |
| 59 | + |
| 60 | + // Write payload |
| 61 | + n, err := conn.Write(packet.Payload) |
| 62 | + if err != nil { |
| 63 | + log.Error().Err(err).Msg("[dahua] failed to write audio payload") |
| 64 | + return |
| 65 | + } |
| 66 | + |
| 67 | + c.send += n |
| 68 | + } |
| 69 | + |
| 70 | + log.Debug(). |
| 71 | + Str("codec", track.Codec.Name). |
| 72 | + Uint32("clock_rate", track.Codec.ClockRate). |
| 73 | + Msg("[dahua] audio sender created") |
| 74 | + } |
| 75 | + |
| 76 | + c.sender.HandleRTP(track) |
| 77 | + return nil |
| 78 | +} |
| 79 | + |
| 80 | +// getConnection returns the current connection in a thread-safe way |
| 81 | +func (c *Client) getConnection() io.WriteCloser { |
| 82 | + // For now, just return the connection directly |
| 83 | + // TODO: Add proper synchronization if needed |
| 84 | + return c.conn |
| 85 | +} |
| 86 | + |
| 87 | +func (c *Client) Start() (err error) { |
| 88 | + log := app.GetLogger("dahua") |
| 89 | + log.Debug().Msg("[dahua] starting client") |
| 90 | + |
| 91 | + if err = c.open(); err != nil { |
| 92 | + log.Error().Err(err).Msg("[dahua] failed to open connection") |
| 93 | + return |
| 94 | + } |
| 95 | + |
| 96 | + log.Debug().Msg("[dahua] client started successfully") |
| 97 | + return |
| 98 | +} |
| 99 | + |
| 100 | +func (c *Client) Stop() (err error) { |
| 101 | + log := app.GetLogger("dahua") |
| 102 | + log.Debug().Msg("[dahua] stopping client") |
| 103 | + |
| 104 | + if c.sender != nil { |
| 105 | + c.sender.Close() |
| 106 | + log.Debug().Msg("[dahua] sender closed") |
| 107 | + } |
| 108 | + |
| 109 | + if c.conn != nil { |
| 110 | + err = c.close() |
| 111 | + if err != nil { |
| 112 | + log.Error().Err(err).Msg("[dahua] error during close") |
| 113 | + } |
| 114 | + // Don't call c.conn.Close() again since c.close() already handles it |
| 115 | + log.Debug().Msg("[dahua] connection closed") |
| 116 | + } |
| 117 | + |
| 118 | + log.Debug().Msg("[dahua] client stopped") |
| 119 | + return nil |
| 120 | +} |
| 121 | + |
| 122 | +func (c *Client) MarshalJSON() ([]byte, error) { |
| 123 | + info := &core.Connection{ |
| 124 | + ID: core.NewID(), |
| 125 | + FormatName: "dahua", |
| 126 | + Protocol: "http", |
| 127 | + Medias: c.medias, |
| 128 | + Send: c.send, |
| 129 | + } |
| 130 | + if c.conn != nil { |
| 131 | + info.RemoteAddr = c.url // Use URL instead of RemoteAddr since we don't have a net.Conn |
| 132 | + } |
| 133 | + if c.sender != nil { |
| 134 | + info.Senders = []*core.Sender{c.sender} |
| 135 | + } |
| 136 | + return json.Marshal(info) |
| 137 | +} |
0 commit comments