github.com/e154/smart-home@v0.17.2-0.20240311175135-e530a6e5cd45/system/media/storageClient.go (about)

     1  // This file is part of the Smart Home
     2  // Program complex distribution https://github.com/e154/smart-home
     3  // Copyright (C) 2023, Filippov Alex
     4  //
     5  // This library is free software: you can redistribute it and/or
     6  // modify it under the terms of the GNU Lesser General Public
     7  // License as published by the Free Software Foundation; either
     8  // version 3 of the License, or (at your option) any later version.
     9  //
    10  // This library is distributed in the hope that it will be useful,
    11  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    12  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    13  // Library General Public License for more details.
    14  //
    15  // You should have received a copy of the GNU Lesser General Public
    16  // License along with this library.  If not, see
    17  // <https://www.gnu.org/licenses/>.
    18  
    19  package media
    20  
    21  import (
    22  	"time"
    23  
    24  	"github.com/google/uuid"
    25  
    26  	"github.com/deepch/vdk/av"
    27  )
    28  
    29  // ClientAdd Add New Client to Translations
    30  func (obj *StorageST) ClientAdd(streamID string, channelID string, mode int) (string, chan *av.Packet, chan *[]byte, error) {
    31  	obj.mutex.Lock()
    32  	defer obj.mutex.Unlock()
    33  	streamTmp, ok := obj.Streams[streamID]
    34  	if !ok {
    35  		return "", nil, nil, ErrorStreamNotFound
    36  	}
    37  	//Generate UUID client
    38  	cid := uuid.New().String()
    39  	chAV := make(chan *av.Packet, 2000)
    40  	chRTP := make(chan *[]byte, 2000)
    41  	channelTmp, ok := streamTmp.Channels[channelID]
    42  	if !ok {
    43  		return "", nil, nil, ErrorStreamNotFound
    44  	}
    45  
    46  	channelTmp.clients[cid] = ClientST{mode: mode, outgoingAVPacket: chAV, outgoingRTPPacket: chRTP, signals: make(chan int, 100)}
    47  	channelTmp.ack = time.Now()
    48  	streamTmp.Channels[channelID] = channelTmp
    49  	obj.Streams[streamID] = streamTmp
    50  	return cid, chAV, chRTP, nil
    51  
    52  }
    53  
    54  // ClientDelete Delete Client
    55  func (obj *StorageST) ClientDelete(streamID string, cid string, channelID string) {
    56  	obj.mutex.Lock()
    57  	defer obj.mutex.Unlock()
    58  	if _, ok := obj.Streams[streamID]; ok {
    59  		delete(obj.Streams[streamID].Channels[channelID].clients, cid)
    60  	}
    61  }
    62  
    63  // ClientHas check is client ext
    64  func (obj *StorageST) ClientHas(streamID string, channelID string) bool {
    65  	obj.mutex.Lock()
    66  	defer obj.mutex.Unlock()
    67  	streamTmp, ok := obj.Streams[streamID]
    68  	if !ok {
    69  		return false
    70  	}
    71  	channelTmp, ok := streamTmp.Channels[channelID]
    72  	if !ok {
    73  		return false
    74  	}
    75  	if time.Since(channelTmp.ack).Seconds() > 30 {
    76  		return false
    77  	}
    78  	return true
    79  }