-
Notifications
You must be signed in to change notification settings - Fork 54
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Tim Cooper
committed
Nov 3, 2014
1 parent
32f880a
commit 9ad87e5
Showing
4 changed files
with
188 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package gumble | ||
|
||
type AudioFlag int | ||
|
||
const ( | ||
AudioSource AudioFlag = 1 << iota // An audio stream that outputs audio | ||
) | ||
|
||
type AudioStream interface { | ||
OnAttach() error | ||
OnAttachSource(chan<- []int16) error | ||
OnDetach() | ||
} | ||
|
||
type Audio interface { | ||
Detachable | ||
} | ||
|
||
type audioImpl struct { | ||
client *Client | ||
stream AudioStream | ||
flags AudioFlag | ||
outgoing chan []int16 | ||
} | ||
|
||
func (a *audioImpl) Detach() { | ||
if a.client.audio != a { | ||
return | ||
} | ||
a.client.audio = nil | ||
a.stream.OnDetach() | ||
close(a.outgoing) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package gumble | ||
|
||
import ( | ||
"github.com/bontibon/gumble/gopus" | ||
) | ||
|
||
func audioOutgoing(audio *audioImpl) { | ||
outgoing := audio.outgoing | ||
message := audioMessage{ | ||
Format: audioOpus, | ||
Target: audioNormal, | ||
} | ||
encoder, _ := opus.NewEncoder(48000, 1, opus.Voip) | ||
encoder.SetVbr(false) | ||
encoder.SetBitrate(40000) | ||
for { | ||
if buf, ok := <-outgoing; !ok { | ||
return | ||
} else { | ||
if opusBuf, err := encoder.Encode(buf, 480, 1024); err == nil { | ||
audio.client.outgoing <- &message | ||
message.sequence = (message.sequence + 1) % 10000 | ||
message.opus = opusBuf | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
package gumble | ||
|
||
import ( | ||
"bytes" | ||
"encoding/binary" | ||
"errors" | ||
"io" | ||
) | ||
|
||
type audioFormat byte | ||
|
||
const ( | ||
audioOpus audioFormat = 4 | ||
) | ||
|
||
type audioTarget byte | ||
|
||
const ( | ||
audioNormal audioTarget = 0 | ||
) | ||
|
||
type audioMessage struct { | ||
Format audioFormat | ||
Target audioTarget | ||
opus []byte | ||
sequence int | ||
} | ||
|
||
func (am *audioMessage) WriteTo(w io.Writer) (int64, error) { | ||
var written int64 = 0 | ||
// Create audio header | ||
var header bytes.Buffer | ||
formatTarget := byte(am.Format)<<5 | byte(am.Target) | ||
if err := header.WriteByte(formatTarget); err != nil { | ||
return 0, err | ||
} | ||
if _, err := writeVarint(int64(am.sequence), &header); err != nil { | ||
return 0, err | ||
} | ||
if _, err := writeVarint(int64(len(am.opus)), &header); err != nil { | ||
return 0, err | ||
} | ||
|
||
// Write packet header | ||
wireType := uint16(1) | ||
wireLength := uint32(header.Len() + len(am.opus)) | ||
if err := binary.Write(w, binary.BigEndian, wireType); err != nil { | ||
return written, err | ||
} else { | ||
written += 2 | ||
} | ||
if err := binary.Write(w, binary.BigEndian, wireLength); err != nil { | ||
return written, err | ||
} else { | ||
written += 4 | ||
} | ||
|
||
// Write audio header | ||
if n, err := header.WriteTo(w); err != nil { | ||
return (written + n), err | ||
} else { | ||
written += n | ||
} | ||
|
||
// Write audio data | ||
if n, err := w.Write(am.opus); err != nil { | ||
return (written + int64(n)), err | ||
} else { | ||
written += int64(n) | ||
} | ||
return written, nil | ||
} | ||
|
||
func writeVarint(value int64, w io.Writer) (int64, error) { | ||
var buff []byte | ||
if value <= 0x7F { | ||
buff = []byte{ | ||
byte(value), | ||
} | ||
} else if value <= 0x3FFF { | ||
buff = []byte{ | ||
byte(((value >> 8) & 0x3F) | 0x80), | ||
byte(value & 0xFF), | ||
} | ||
} | ||
if buff != nil { | ||
if n, err := w.Write(buff); err != nil { | ||
return int64(n), err | ||
} else { | ||
return int64(n), nil | ||
} | ||
} | ||
return 0, errors.New("out of range") | ||
} | ||
|
||
func (am *audioMessage) gumbleMessage() { | ||
} |