mirror of
https://github.com/zoriya/Kyoo.git
synced 2025-05-31 04:04:21 -04:00
Fix rw mutexes handling
This commit is contained in:
parent
0517f85d76
commit
2a491ded00
@ -20,9 +20,9 @@ type FileStream struct {
|
|||||||
CanTransmux bool
|
CanTransmux bool
|
||||||
Info *MediaInfo
|
Info *MediaInfo
|
||||||
streams map[Quality]*VideoStream
|
streams map[Quality]*VideoStream
|
||||||
vlock sync.RWMutex
|
vlock sync.Mutex
|
||||||
audios map[int32]*AudioStream
|
audios map[int32]*AudioStream
|
||||||
alock sync.RWMutex
|
alock sync.Mutex
|
||||||
}
|
}
|
||||||
|
|
||||||
func GetOutPath() string {
|
func GetOutPath() string {
|
||||||
@ -201,16 +201,13 @@ func (fs *FileStream) GetMaster() string {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (fs *FileStream) getVideoStream(quality Quality) *VideoStream {
|
func (fs *FileStream) getVideoStream(quality Quality) *VideoStream {
|
||||||
fs.vlock.RLock()
|
fs.vlock.Lock()
|
||||||
|
defer fs.vlock.Unlock()
|
||||||
stream, ok := fs.streams[quality]
|
stream, ok := fs.streams[quality]
|
||||||
fs.vlock.RUnlock()
|
|
||||||
|
|
||||||
if ok {
|
if ok {
|
||||||
return stream
|
return stream
|
||||||
}
|
}
|
||||||
|
|
||||||
fs.vlock.Lock()
|
|
||||||
defer fs.vlock.Unlock()
|
|
||||||
fs.streams[quality] = NewVideoStream(fs, quality)
|
fs.streams[quality] = NewVideoStream(fs, quality)
|
||||||
return fs.streams[quality]
|
return fs.streams[quality]
|
||||||
}
|
}
|
||||||
@ -226,16 +223,13 @@ func (fs *FileStream) GetVideoSegment(quality Quality, segment int32) (string, e
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (fs *FileStream) getAudioStream(audio int32) *AudioStream {
|
func (fs *FileStream) getAudioStream(audio int32) *AudioStream {
|
||||||
fs.alock.RLock()
|
fs.alock.Lock()
|
||||||
|
defer fs.alock.Unlock()
|
||||||
stream, ok := fs.audios[audio]
|
stream, ok := fs.audios[audio]
|
||||||
fs.alock.RUnlock()
|
|
||||||
|
|
||||||
if ok {
|
if ok {
|
||||||
return stream
|
return stream
|
||||||
}
|
}
|
||||||
|
|
||||||
fs.alock.Lock()
|
|
||||||
defer fs.alock.Unlock()
|
|
||||||
fs.audios[audio] = NewAudioStream(fs, audio)
|
fs.audios[audio] = NewAudioStream(fs, audio)
|
||||||
return fs.audios[audio]
|
return fs.audios[audio]
|
||||||
}
|
}
|
||||||
|
@ -124,12 +124,12 @@ func (t *Tracker) KillAudioIfDead(path string, audio int32) bool {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
log.Printf("Nobody is listening audio %d of %s. Killing it", audio, path)
|
log.Printf("Nobody is listening audio %d of %s. Killing it", audio, path)
|
||||||
t.transcoder.mutex.RLock()
|
t.transcoder.mutex.Lock()
|
||||||
stream := t.transcoder.streams[path]
|
stream := t.transcoder.streams[path]
|
||||||
t.transcoder.mutex.RUnlock()
|
t.transcoder.mutex.Unlock()
|
||||||
|
|
||||||
stream.alock.RLock()
|
stream.alock.Lock()
|
||||||
defer stream.alock.RUnlock()
|
defer stream.alock.Unlock()
|
||||||
stream.audios[audio].Kill()
|
stream.audios[audio].Kill()
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
@ -141,32 +141,32 @@ func (t *Tracker) KillQualityIfDead(path string, quality Quality) bool {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
log.Printf("Nobody is watching quality %s of %s. Killing it", quality, path)
|
log.Printf("Nobody is watching quality %s of %s. Killing it", quality, path)
|
||||||
t.transcoder.mutex.RLock()
|
t.transcoder.mutex.Lock()
|
||||||
stream := t.transcoder.streams[path]
|
stream := t.transcoder.streams[path]
|
||||||
t.transcoder.mutex.RUnlock()
|
t.transcoder.mutex.Unlock()
|
||||||
|
|
||||||
stream.vlock.RLock()
|
stream.vlock.Lock()
|
||||||
defer stream.vlock.RUnlock()
|
defer stream.vlock.Unlock()
|
||||||
stream.streams[quality].Kill()
|
stream.streams[quality].Kill()
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t *Tracker) KillOrphanedHeads(path string, quality *Quality, audio int32) {
|
func (t *Tracker) KillOrphanedHeads(path string, quality *Quality, audio int32) {
|
||||||
t.transcoder.mutex.RLock()
|
t.transcoder.mutex.Lock()
|
||||||
stream := t.transcoder.streams[path]
|
stream := t.transcoder.streams[path]
|
||||||
t.transcoder.mutex.RUnlock()
|
t.transcoder.mutex.Unlock()
|
||||||
|
|
||||||
if quality != nil {
|
if quality != nil {
|
||||||
stream.vlock.RLock()
|
stream.vlock.Lock()
|
||||||
vstream := stream.streams[*quality]
|
vstream := stream.streams[*quality]
|
||||||
stream.vlock.RUnlock()
|
stream.vlock.Unlock()
|
||||||
|
|
||||||
t.killOrphanedeheads(&vstream.Stream)
|
t.killOrphanedeheads(&vstream.Stream)
|
||||||
}
|
}
|
||||||
if audio != -1 {
|
if audio != -1 {
|
||||||
stream.alock.RLock()
|
stream.alock.Lock()
|
||||||
astream := stream.audios[audio]
|
astream := stream.audios[audio]
|
||||||
stream.alock.RUnlock()
|
stream.alock.Unlock()
|
||||||
|
|
||||||
t.killOrphanedeheads(&astream.Stream)
|
t.killOrphanedeheads(&astream.Stream)
|
||||||
}
|
}
|
||||||
|
@ -13,7 +13,7 @@ type Transcoder struct {
|
|||||||
streams map[string]*FileStream
|
streams map[string]*FileStream
|
||||||
// Streams that are staring up
|
// Streams that are staring up
|
||||||
preparing map[string]chan *FileStream
|
preparing map[string]chan *FileStream
|
||||||
mutex sync.RWMutex
|
mutex sync.Mutex
|
||||||
clientChan chan ClientInfo
|
clientChan chan ClientInfo
|
||||||
tracker *Tracker
|
tracker *Tracker
|
||||||
}
|
}
|
||||||
@ -41,10 +41,14 @@ func NewTranscoder() (*Transcoder, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (t *Transcoder) getFileStream(path string) (*FileStream, error) {
|
func (t *Transcoder) getFileStream(path string) (*FileStream, error) {
|
||||||
t.mutex.RLock()
|
t.mutex.Lock()
|
||||||
stream, ok := t.streams[path]
|
stream, ok := t.streams[path]
|
||||||
channel, preparing := t.preparing[path]
|
channel, preparing := t.preparing[path]
|
||||||
t.mutex.RUnlock()
|
if !preparing && !ok {
|
||||||
|
channel = make(chan *FileStream, 1)
|
||||||
|
t.preparing[path] = channel
|
||||||
|
}
|
||||||
|
t.mutex.Unlock()
|
||||||
|
|
||||||
if preparing {
|
if preparing {
|
||||||
stream = <-channel
|
stream = <-channel
|
||||||
@ -52,11 +56,6 @@ func (t *Transcoder) getFileStream(path string) (*FileStream, error) {
|
|||||||
return nil, errors.New("could not transcode file. Try again later")
|
return nil, errors.New("could not transcode file. Try again later")
|
||||||
}
|
}
|
||||||
} else if !ok {
|
} else if !ok {
|
||||||
t.mutex.Lock()
|
|
||||||
channel = make(chan *FileStream, 1)
|
|
||||||
t.preparing[path] = channel
|
|
||||||
t.mutex.Unlock()
|
|
||||||
|
|
||||||
var err error
|
var err error
|
||||||
stream, err = NewFileStream(path)
|
stream, err = NewFileStream(path)
|
||||||
log.Printf("Stream created for %s", path)
|
log.Printf("Stream created for %s", path)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user