start work webrtc adapter
This commit is contained in:
		
							
								
								
									
										1
									
								
								.gitignore
									
									
									
									
										vendored
									
									
										Executable file
									
								
							
							
						
						
									
										1
									
								
								.gitignore
									
									
									
									
										vendored
									
									
										Executable file
									
								
							| @@ -0,0 +1 @@ | |||||||
|  | .idea/ | ||||||
							
								
								
									
										8
									
								
								.idea/modules.xml
									
									
									
										generated
									
									
									
								
							
							
						
						
									
										8
									
								
								.idea/modules.xml
									
									
									
										generated
									
									
									
								
							| @@ -1,8 +0,0 @@ | |||||||
| <?xml version="1.0" encoding="UTF-8"?> |  | ||||||
| <project version="4"> |  | ||||||
|   <component name="ProjectModuleManager"> |  | ||||||
|     <modules> |  | ||||||
|       <module fileurl="file://$PROJECT_DIR$/.idea/vdk.iml" filepath="$PROJECT_DIR$/.idea/vdk.iml" /> |  | ||||||
|     </modules> |  | ||||||
|   </component> |  | ||||||
| </project> |  | ||||||
							
								
								
									
										6
									
								
								.idea/vcs.xml
									
									
									
										generated
									
									
									
								
							
							
						
						
									
										6
									
								
								.idea/vcs.xml
									
									
									
										generated
									
									
									
								
							| @@ -1,6 +0,0 @@ | |||||||
| <?xml version="1.0" encoding="UTF-8"?> |  | ||||||
| <project version="4"> |  | ||||||
|   <component name="VcsDirectoryMappings"> |  | ||||||
|     <mapping directory="$PROJECT_DIR$" vcs="Git" /> |  | ||||||
|   </component> |  | ||||||
| </project> |  | ||||||
							
								
								
									
										9
									
								
								.idea/vdk.iml
									
									
									
										generated
									
									
									
								
							
							
						
						
									
										9
									
								
								.idea/vdk.iml
									
									
									
										generated
									
									
									
								
							| @@ -1,9 +0,0 @@ | |||||||
| <?xml version="1.0" encoding="UTF-8"?> |  | ||||||
| <module type="WEB_MODULE" version="4"> |  | ||||||
|   <component name="Go" enabled="true" /> |  | ||||||
|   <component name="NewModuleRootManager"> |  | ||||||
|     <content url="file://$MODULE_DIR$" /> |  | ||||||
|     <orderEntry type="inheritedJdk" /> |  | ||||||
|     <orderEntry type="sourceFolder" forTests="false" /> |  | ||||||
|   </component> |  | ||||||
| </module> |  | ||||||
							
								
								
									
										9
									
								
								.idea/watcherTasks.xml
									
									
									
										generated
									
									
									
								
							
							
						
						
									
										9
									
								
								.idea/watcherTasks.xml
									
									
									
										generated
									
									
									
								
							| @@ -1,9 +0,0 @@ | |||||||
| <?xml version="1.0" encoding="UTF-8"?> |  | ||||||
| <project version="4"> |  | ||||||
|   <component name="ProjectTasksOptions"> |  | ||||||
|     <enabled-global> |  | ||||||
|       <option value="go fmt" /> |  | ||||||
|       <option value="goimports" /> |  | ||||||
|     </enabled-global> |  | ||||||
|   </component> |  | ||||||
| </project> |  | ||||||
							
								
								
									
										87
									
								
								.idea/workspace.xml
									
									
									
										generated
									
									
									
								
							
							
						
						
									
										87
									
								
								.idea/workspace.xml
									
									
									
										generated
									
									
									
								
							| @@ -1,87 +0,0 @@ | |||||||
| <?xml version="1.0" encoding="UTF-8"?> |  | ||||||
| <project version="4"> |  | ||||||
|   <component name="BranchesTreeState"> |  | ||||||
|     <expand> |  | ||||||
|       <path> |  | ||||||
|         <item name="ROOT" type="e8cecc67:BranchNodeDescriptor" /> |  | ||||||
|         <item name="LOCAL_ROOT" type="e8cecc67:BranchNodeDescriptor" /> |  | ||||||
|       </path> |  | ||||||
|       <path> |  | ||||||
|         <item name="ROOT" type="e8cecc67:BranchNodeDescriptor" /> |  | ||||||
|         <item name="REMOTE_ROOT" type="e8cecc67:BranchNodeDescriptor" /> |  | ||||||
|       </path> |  | ||||||
|       <path> |  | ||||||
|         <item name="ROOT" type="e8cecc67:BranchNodeDescriptor" /> |  | ||||||
|         <item name="REMOTE_ROOT" type="e8cecc67:BranchNodeDescriptor" /> |  | ||||||
|         <item name="GROUP_NODE:origin" type="e8cecc67:BranchNodeDescriptor" /> |  | ||||||
|       </path> |  | ||||||
|     </expand> |  | ||||||
|     <select /> |  | ||||||
|   </component> |  | ||||||
|   <component name="ChangeListManager"> |  | ||||||
|     <list default="true" id="42b0d4fc-bb69-455e-85b5-24bbd2011819" name="Default Changelist" comment=""> |  | ||||||
|       <change afterPath="$PROJECT_DIR$/.idea/modules.xml" afterDir="false" /> |  | ||||||
|       <change afterPath="$PROJECT_DIR$/.idea/vcs.xml" afterDir="false" /> |  | ||||||
|       <change afterPath="$PROJECT_DIR$/.idea/vdk.iml" afterDir="false" /> |  | ||||||
|       <change afterPath="$PROJECT_DIR$/.idea/watcherTasks.xml" afterDir="false" /> |  | ||||||
|       <change afterPath="$PROJECT_DIR$/format/webrtc/adapter.go" afterDir="false" /> |  | ||||||
|       <change beforePath="$PROJECT_DIR$/format/format.go" beforeDir="false" afterPath="$PROJECT_DIR$/format/format.go" afterDir="false" /> |  | ||||||
|       <change beforePath="$PROJECT_DIR$/go.mod" beforeDir="false" afterPath="$PROJECT_DIR$/go.mod" afterDir="false" /> |  | ||||||
|     </list> |  | ||||||
|     <option name="SHOW_DIALOG" value="false" /> |  | ||||||
|     <option name="HIGHLIGHT_CONFLICTS" value="true" /> |  | ||||||
|     <option name="HIGHLIGHT_NON_ACTIVE_CHANGELIST" value="false" /> |  | ||||||
|     <option name="LAST_RESOLUTION" value="IGNORE" /> |  | ||||||
|   </component> |  | ||||||
|   <component name="FileTemplateManagerImpl"> |  | ||||||
|     <option name="RECENT_TEMPLATES"> |  | ||||||
|       <list> |  | ||||||
|         <option value="Go File" /> |  | ||||||
|       </list> |  | ||||||
|     </option> |  | ||||||
|   </component> |  | ||||||
|   <component name="GOROOT" path="/usr/local/go" /> |  | ||||||
|   <component name="Git.Settings"> |  | ||||||
|     <option name="RECENT_GIT_ROOT_PATH" value="$PROJECT_DIR$" /> |  | ||||||
|   </component> |  | ||||||
|   <component name="GoLibraries"> |  | ||||||
|     <option name="indexEntireGoPath" value="false" /> |  | ||||||
|   </component> |  | ||||||
|   <component name="ProjectId" id="1fp1qhBVMkG2VqoaXsEJGv1aMUs" /> |  | ||||||
|   <component name="ProjectViewState"> |  | ||||||
|     <option name="hideEmptyMiddlePackages" value="true" /> |  | ||||||
|     <option name="showLibraryContents" value="true" /> |  | ||||||
|   </component> |  | ||||||
|   <component name="PropertiesComponent"> |  | ||||||
|     <property name="ASKED_SHARE_PROJECT_CONFIGURATION_FILES" value="true" /> |  | ||||||
|     <property name="DefaultGoTemplateProperty" value="Go File" /> |  | ||||||
|     <property name="RunOnceActivity.OpenProjectViewOnStart" value="true" /> |  | ||||||
|     <property name="SHARE_PROJECT_CONFIGURATION_FILES" value="true" /> |  | ||||||
|     <property name="go.import.settings.migrated" value="true" /> |  | ||||||
|     <property name="go.sdk.automatically.set" value="true" /> |  | ||||||
|     <property name="go.tried.to.enable.integration.vgo.integrator" value="true" /> |  | ||||||
|     <property name="last_opened_file_path" value="$USER_HOME$" /> |  | ||||||
|     <property name="settings.editor.selected.configurable" value="watcher.settings" /> |  | ||||||
|   </component> |  | ||||||
|   <component name="SpellCheckerSettings" RuntimeDictionaries="0" Folders="0" CustomDictionaries="0" DefaultDictionary="application-level" UseSingleDictionary="true" transferred="true" /> |  | ||||||
|   <component name="Vcs.Log.Tabs.Properties"> |  | ||||||
|     <option name="TAB_STATES"> |  | ||||||
|       <map> |  | ||||||
|         <entry key="MAIN"> |  | ||||||
|           <value> |  | ||||||
|             <State /> |  | ||||||
|           </value> |  | ||||||
|         </entry> |  | ||||||
|       </map> |  | ||||||
|     </option> |  | ||||||
|   </component> |  | ||||||
|   <component name="VgoProject"> |  | ||||||
|     <integration-enabled>true</integration-enabled> |  | ||||||
|   </component> |  | ||||||
|   <component name="WindowStateProjectService"> |  | ||||||
|     <state x="789" y="318" key="SettingsEditor" timestamp="1596900451085"> |  | ||||||
|       <screen x="0" y="23" width="2560" height="1313" /> |  | ||||||
|     </state> |  | ||||||
|     <state x="789" y="318" key="SettingsEditor/0.23.2560.1313@0.23.2560.1313" timestamp="1596900451085" /> |  | ||||||
|   </component> |  | ||||||
| </project> |  | ||||||
| @@ -4,11 +4,12 @@ import ( | |||||||
| 	"encoding/base64" | 	"encoding/base64" | ||||||
| 	"errors" | 	"errors" | ||||||
| 	"fmt" | 	"fmt" | ||||||
|  | 	"math/rand" | ||||||
|  |  | ||||||
| 	"github.com/deepch/vdk/av" | 	"github.com/deepch/vdk/av" | ||||||
| 	"github.com/deepch/vdk/codec/h264parser" | 	"github.com/deepch/vdk/codec/h264parser" | ||||||
| 	"github.com/pion/webrtc/v3" | 	"github.com/pion/webrtc/v3" | ||||||
| 	"github.com/pion/webrtc/v3/pkg/media" | 	"github.com/pion/webrtc/v3/pkg/media" | ||||||
| 	"math/rand" |  | ||||||
| ) | ) | ||||||
|  |  | ||||||
| type Muxer struct { | type Muxer struct { | ||||||
| @@ -18,9 +19,11 @@ type Stream struct { | |||||||
| 	codec av.CodecData | 	codec av.CodecData | ||||||
| 	track *webrtc.Track | 	track *webrtc.Track | ||||||
| } | } | ||||||
|  |  | ||||||
| func NewMuxer() *Muxer { | func NewMuxer() *Muxer { | ||||||
| 	return &Muxer{streams: make(map[int8]*Stream)} | 	return &Muxer{streams: make(map[int8]*Stream)} | ||||||
| } | } | ||||||
|  |  | ||||||
| func (self *Muxer) WriteHeader(streams []av.CodecData, sdp64 string) (string, error) { | func (self *Muxer) WriteHeader(streams []av.CodecData, sdp64 string) (string, error) { | ||||||
| 	if len(streams) == 0 { | 	if len(streams) == 0 { | ||||||
| 		return "", errors.New("No Stream Forund") | 		return "", errors.New("No Stream Forund") | ||||||
| @@ -69,7 +72,7 @@ func (self *Muxer) WriteHeader(streams []av.CodecData, sdp64 string) (string, er | |||||||
| 		if err != nil { | 		if err != nil { | ||||||
| 			return "", err | 			return "", err | ||||||
| 		} | 		} | ||||||
| 		_, err = peerConnection.AddTrack(track); | 		_, err = peerConnection.AddTrack(track) | ||||||
| 		if err != nil { | 		if err != nil { | ||||||
| 			return "", err | 			return "", err | ||||||
| 		} | 		} | ||||||
| @@ -95,9 +98,9 @@ func (self *Muxer) WriteHeader(streams []av.CodecData, sdp64 string) (string, er | |||||||
| 	} | 	} | ||||||
| 	//<-gatherComplete | 	//<-gatherComplete | ||||||
|  |  | ||||||
|  |  | ||||||
| 	return base64.StdEncoding.EncodeToString([]byte(answer.SDP)), nil | 	return base64.StdEncoding.EncodeToString([]byte(answer.SDP)), nil | ||||||
| } | } | ||||||
|  |  | ||||||
| func (self *Muxer) WritePacket(pkt av.Packet) (err error) { | func (self *Muxer) WritePacket(pkt av.Packet) (err error) { | ||||||
| 	if tmp, ok := self.streams[pkt.Idx]; ok { | 	if tmp, ok := self.streams[pkt.Idx]; ok { | ||||||
| 		switch tmp.codec.Type() { | 		switch tmp.codec.Type() { | ||||||
| @@ -116,6 +119,7 @@ func (self *Muxer) WritePacket(pkt av.Packet) (err error) { | |||||||
| 	} | 	} | ||||||
| 	return errors.New("Media Track Not Found") | 	return errors.New("Media Track Not Found") | ||||||
| } | } | ||||||
|  |  | ||||||
| func getPayloadType(m webrtc.MediaEngine, codecType webrtc.RTPCodecType, codecName string) uint8 { | func getPayloadType(m webrtc.MediaEngine, codecType webrtc.RTPCodecType, codecName string) uint8 { | ||||||
| 	for _, codec := range m.GetCodecsByKind(codecType) { | 	for _, codec := range m.GetCodecsByKind(codecType) { | ||||||
| 		if codec.Name == codecName { | 		if codec.Name == codecName { | ||||||
| @@ -124,96 +128,3 @@ func getPayloadType(m webrtc.MediaEngine, codecType webrtc.RTPCodecType, codecNa | |||||||
| 	} | 	} | ||||||
| 	panic(fmt.Sprintf("Remote peer does not support %s", codecName)) | 	panic(fmt.Sprintf("Remote peer does not support %s", codecName)) | ||||||
| } | } | ||||||
|  |  | ||||||
| /* |  | ||||||
| //MuxerRTC muxer |  | ||||||
| type MuxerRTC struct { |  | ||||||
| 	Peer  *webrtc.PeerConnection |  | ||||||
| 	Muxer string |  | ||||||
| 	Track []*webrtc.Track |  | ||||||
| } |  | ||||||
|  |  | ||||||
| //NewWebRT adapter |  | ||||||
| func NewWebRT() *MuxerRTC { |  | ||||||
| 	peerConnection, err := webrtc.NewPeerConnection(webrtc.Configuration{ |  | ||||||
| 		ICEServers: []webrtc.ICEServer{ |  | ||||||
| 			{ |  | ||||||
| 				URLs: []string{"stun:stun.l.google.com:19302"}, |  | ||||||
| 			}, |  | ||||||
| 		}, |  | ||||||
| 	}) |  | ||||||
| 	if err != nil { |  | ||||||
| 		panic(err) |  | ||||||
| 	} |  | ||||||
| 	log.Println(peerConnection) |  | ||||||
| 	return &MuxerRTC{Peer: peerConnection} |  | ||||||
| } |  | ||||||
|  |  | ||||||
| //AddTrackVideo video |  | ||||||
| func (element *MuxerRTC) AddTrackVideo() error { |  | ||||||
| 	Track, err := element.Peer.NewTrack(webrtc.DefaultPayloadTypeH264, rand.Uint32(), "video", "video") |  | ||||||
| 	if err != nil { |  | ||||||
| 		return err |  | ||||||
| 	} |  | ||||||
| 	_, err = element.Peer.AddTrack(Track) |  | ||||||
| 	if err != nil { |  | ||||||
| 		return err |  | ||||||
| 	} |  | ||||||
| 	element.Track = append(element.Track, Track) |  | ||||||
| 	return nil |  | ||||||
| } |  | ||||||
|  |  | ||||||
| //AddTrackAudio audio |  | ||||||
| func (element *MuxerRTC) AddTrackAudio() error { |  | ||||||
| 	Track, err := element.Peer.NewTrack(webrtc.DefaultPayloadTypePCMA, rand.Uint32(), "audio", "audio") |  | ||||||
| 	if err != nil { |  | ||||||
| 		return err |  | ||||||
| 	} |  | ||||||
| 	_, err = element.Peer.AddTrack(Track) |  | ||||||
| 	if err != nil { |  | ||||||
| 		return err |  | ||||||
| 	} |  | ||||||
| 	_, err = element.Peer.AddTransceiverFromTrack(Track, webrtc.RtpTransceiverInit{Direction: webrtc.RTPTransceiverDirectionSendonly}) |  | ||||||
| 	if err != nil { |  | ||||||
| 		return err |  | ||||||
| 	} |  | ||||||
| 	element.Track = append(element.Track, Track) |  | ||||||
| 	return nil |  | ||||||
| } |  | ||||||
|  |  | ||||||
| //CreateAnswer func |  | ||||||
| func (element *MuxerRTC) CreateAnswer(data string) ([]byte, error) { |  | ||||||
| 	//	log.Println(string(data)) |  | ||||||
|  |  | ||||||
| 	//	sd, err := base64.StdEncoding.DecodeString(data) |  | ||||||
| 	//	if err != nil { |  | ||||||
| 	//		log.Println("DecodeString", err) |  | ||||||
| 	//		return nil, err |  | ||||||
| 	//	} |  | ||||||
| 	offer := webrtc.SessionDescription{ |  | ||||||
| 		Type: webrtc.SDPTypeOffer, |  | ||||||
| 		SDP:  data, |  | ||||||
| 	} |  | ||||||
| 	if err := element.Peer.SetRemoteDescription(offer); err != nil { |  | ||||||
| 		log.Println("SetRemoteDescription", err) |  | ||||||
|  |  | ||||||
| 		return nil, err |  | ||||||
| 	} |  | ||||||
| 	answer, err := element.Peer.CreateAnswer(nil) |  | ||||||
| 	if err != nil { |  | ||||||
| 		log.Println("CreateAnswer", err) |  | ||||||
|  |  | ||||||
| 		return nil, err |  | ||||||
| 	} |  | ||||||
| 	return []byte(answer.SDP), nil |  | ||||||
| } |  | ||||||
|  |  | ||||||
| //WritePacket to stream |  | ||||||
| func (element *MuxerRTC) WritePacket(id int, data []byte, smaple uint32) error { |  | ||||||
| 	err := element.Track[id].WriteSample(media.Sample{Data: data, Samples: smaple}) |  | ||||||
| 	return err |  | ||||||
| } |  | ||||||
| func (element *MuxerRTC) Close() { |  | ||||||
| 	return |  | ||||||
| } |  | ||||||
| */ |  | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user
	 Andrey Semochkin
					Andrey Semochkin