Skip to content

Commit

Permalink
Code refactoring for stream MarshalJSON
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexxIT committed Jun 10, 2024
1 parent 0395696 commit 1ac9d54
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 15 deletions.
7 changes: 3 additions & 4 deletions internal/streams/producer.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,11 +132,10 @@ func (p *Producer) AddTrack(media *core.Media, codec *core.Codec, track *core.Re
}

func (p *Producer) MarshalJSON() ([]byte, error) {
if p.conn != nil {
return json.Marshal(p.conn)
if conn := p.conn; conn != nil {
return json.Marshal(conn)
}

info := core.Info{URL: p.url}
info := map[string]string{"url": p.url}
return json.Marshal(info)
}

Expand Down
15 changes: 4 additions & 11 deletions internal/streams/stream.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,19 +112,12 @@ producers:
}

func (s *Stream) MarshalJSON() ([]byte, error) {
if !s.mu.TryLock() {
log.Warn().Msgf("[streams] json locked")
return json.Marshal(nil)
}

var info struct {
var info = struct {
Producers []*Producer `json:"producers"`
Consumers []core.Consumer `json:"consumers"`
}{
Producers: s.producers,
Consumers: s.consumers,
}
info.Producers = s.producers
info.Consumers = s.consumers

s.mu.Unlock()

return json.Marshal(info)
}

0 comments on commit 1ac9d54

Please sign in to comment.