github.com/cnotch/ipchub@v1.1.0/av/codec/frame.go (about)

     1  // Copyright (c) 2019,CAOHONGJU All rights reserved.
     2  // Use of this source code is governed by a MIT-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package codec
     6  
     7  import (
     8  	"fmt"
     9  	"strings"
    10  )
    11  
    12  // MediaType 媒体类型
    13  type MediaType int
    14  
    15  // 媒体类型常量
    16  const (
    17  	MediaTypeUnknown MediaType = iota - 1 // Usually treated as MediaTypeData
    18  	MediaTypeVideo
    19  	MediaTypeAudio
    20  	MediaTypeData // Opaque data information usually continuous
    21  	MediaTypeSubtitle
    22  	MediaTypeAttachment // Opaque data information usually sparse
    23  	MediaTypeNB
    24  )
    25  
    26  // String returns a lower-case ASCII representation of the media type.
    27  func (mt MediaType) String() string {
    28  	switch mt {
    29  	case MediaTypeVideo:
    30  		return "video"
    31  	case MediaTypeAudio:
    32  		return "audio"
    33  	case MediaTypeData:
    34  		return "data"
    35  	case MediaTypeSubtitle:
    36  		return "subtitle"
    37  	case MediaTypeAttachment:
    38  		return "attachment"
    39  	default:
    40  		return ""
    41  	}
    42  }
    43  
    44  // MarshalText marshals the MediaType to text.
    45  func (mt *MediaType) MarshalText() ([]byte, error) {
    46  	return []byte(mt.String()), nil
    47  }
    48  
    49  // UnmarshalText unmarshals text to a MediaType.
    50  func (mt *MediaType) UnmarshalText(text []byte) error {
    51  	if !mt.unmarshalText(string(text)) {
    52  		return fmt.Errorf("unrecognized media type: %q", text)
    53  	}
    54  	return nil
    55  }
    56  
    57  func (mt *MediaType) unmarshalText(text string) bool {
    58  	switch strings.ToLower(text) {
    59  	case "video":
    60  		*mt = MediaTypeVideo
    61  	case "audio":
    62  		*mt = MediaTypeAudio
    63  	case "data":
    64  		*mt = MediaTypeData
    65  	case "subtitle":
    66  		*mt = MediaTypeSubtitle
    67  	case "attachment":
    68  		*mt = MediaTypeAttachment
    69  	default:
    70  		return false
    71  	}
    72  	return true
    73  }
    74  
    75  // Frame 音视频完整帧
    76  type Frame struct {
    77  	MediaType        // 媒体类型
    78  	Dts       int64  // DTS,单位为 ns
    79  	Pts       int64  // PTS,单位为 ns
    80  	Payload   []byte // 媒体数据载荷
    81  }
    82  
    83  // FrameWriter 包装 WriteFrame 方法的接口
    84  type FrameWriter interface {
    85  	WriteFrame(frame *Frame) error
    86  }