github.com/0chain/gosdk@v1.17.11/zboxcore/sdk/live_upload.go (about)

     1  package sdk
     2  
     3  import "github.com/0chain/gosdk/zboxcore/zboxutil"
     4  
     5  // LiveUpload live streaming video upload manager
     6  type LiveUpload struct {
     7  	allocationObj *Allocation
     8  	homedir       string
     9  
    10  	// delay  delay to upload video
    11  	delay int
    12  
    13  	liveMeta   LiveMeta
    14  	liveReader LiveUploadReader
    15  
    16  	// encryptOnUpload encrypt data on upload or not.
    17  	encryptOnUpload bool
    18  	// chunkSize how much bytes a chunk has. 64KB is default value.
    19  	chunkSize int64
    20  	// chunkNumber the number of chunks in a http upload request. 1 is default value
    21  	chunkNumber int
    22  
    23  	clipsIndex int
    24  
    25  	// statusCallback trigger progress on StatusCallback
    26  	statusCallback func() StatusCallback
    27  }
    28  
    29  // CreateLiveUpload create a LiveChunkedUpload instance to upload live streaming video
    30  //   - homedir: home directory of the allocation
    31  //   - allocationObj: allocation object
    32  //   - liveMeta: live meta data
    33  //   - liveReader: live reader to read video data
    34  //   - opts: live upload option functions which customize the LiveUpload instance.
    35  func CreateLiveUpload(homedir string, allocationObj *Allocation, liveMeta LiveMeta, liveReader LiveUploadReader, opts ...LiveUploadOption) *LiveUpload {
    36  	u := &LiveUpload{
    37  		allocationObj: allocationObj,
    38  		homedir:       homedir,
    39  		chunkSize:     DefaultChunkSize,
    40  		chunkNumber:   1,
    41  		liveMeta:      liveMeta,
    42  		liveReader:    liveReader,
    43  		clipsIndex:    1,
    44  	}
    45  
    46  	for _, opt := range opts {
    47  		opt(u)
    48  	}
    49  
    50  	return u
    51  }
    52  
    53  // Start start live streaming upload
    54  func (lu *LiveUpload) Start() error {
    55  
    56  	var err error
    57  	var clipsUpload *ChunkedUpload
    58  	for {
    59  
    60  		clipsUpload, err = lu.createClipsUpload(lu.clipsIndex, lu.liveReader)
    61  		if err != nil {
    62  			return err
    63  		}
    64  
    65  		err = clipsUpload.Start()
    66  
    67  		if err != nil {
    68  			return err
    69  		}
    70  
    71  		lu.clipsIndex++
    72  
    73  	}
    74  
    75  }
    76  
    77  func (lu *LiveUpload) createClipsUpload(clipsIndex int, reader LiveUploadReader) (*ChunkedUpload, error) {
    78  	fileMeta := FileMeta{
    79  		Path:       reader.GetClipsFile(clipsIndex),
    80  		ActualSize: reader.Size(),
    81  
    82  		MimeType:   lu.liveMeta.MimeType,
    83  		RemoteName: reader.GetClipsFileName(clipsIndex),
    84  		RemotePath: lu.liveMeta.RemotePath + "/" + reader.GetClipsFileName(clipsIndex),
    85  	}
    86  
    87  	return CreateChunkedUpload(lu.allocationObj.ctx, lu.homedir, lu.allocationObj, fileMeta, reader, false, false, false, zboxutil.NewConnectionId(),
    88  		WithEncrypt(lu.encryptOnUpload),
    89  		WithStatusCallback(lu.statusCallback()))
    90  }