github.com/0chain/gosdk@v1.17.11/wasmsdk/player.go (about)

     1  //go:build js && wasm
     2  // +build js,wasm
     3  
     4  package main
     5  
     6  import "errors"
     7  
     8  // Player is the interface for a file player
     9  type Player interface {
    10  	Start() error
    11  	Stop()
    12  
    13  	GetNext() []byte
    14  }
    15  
    16  var currentPlayer Player
    17  
    18  // play starts playing a playable file or stream
    19  //   - allocationID is the allocation id
    20  //   - remotePath is the remote path of the file or stream
    21  //   - authTicket is the auth ticket, in case of accessing as a shared file
    22  //   - lookupHash is the lookup hash for the file
    23  //   - isLive is the flag to indicate if the file is live or not
    24  func play(allocationID, remotePath, authTicket, lookupHash string, isLive bool) error {
    25  	var err error
    26  
    27  	if currentPlayer != nil {
    28  		currentPlayer.Stop()
    29  		currentPlayer = nil
    30  	}
    31  
    32  	if isLive {
    33  		currentPlayer, err = createStreamPalyer(allocationID, remotePath, authTicket, lookupHash)
    34  		if err != nil {
    35  			return err
    36  		}
    37  
    38  	} else {
    39  		currentPlayer, err = createFilePalyer(allocationID, remotePath, authTicket, lookupHash)
    40  		if err != nil {
    41  			return err
    42  		}
    43  	}
    44  
    45  	return currentPlayer.Start()
    46  
    47  }
    48  
    49  // stop stops the current player
    50  func stop() error {
    51  	if currentPlayer != nil {
    52  		currentPlayer.Stop()
    53  	}
    54  
    55  	currentPlayer = nil
    56  
    57  	return nil
    58  }
    59  
    60  // getNextSegment gets the next segment of the current player
    61  func getNextSegment() ([]byte, error) {
    62  	if currentPlayer == nil {
    63  		return nil, errors.New("No player is available")
    64  	}
    65  
    66  	return currentPlayer.GetNext(), nil
    67  }
    68  
    69  func withRecover(send func()) (success bool) {
    70  	defer func() {
    71  		if recover() != nil {
    72  			//recover panic from `send on closed channel`
    73  			success = false
    74  		}
    75  	}()
    76  
    77  	send()
    78  
    79  	return true
    80  }