Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for sending and receiving audio, and all that entails. #65

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
88 changes: 88 additions & 0 deletions audiotrack.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
#include <_cgo_export.h> // Allow calling certain Go functions.

#include "audiotrack.h"

#include "webrtc/api/mediastreaminterface.h"
#include "webrtc/pc/mediastreamtrack.h"

using namespace webrtc;

class AudioTrack : public MediaStreamTrack<AudioTrackInterface> {
public:
explicit AudioTrack(std::string label, CGO_GoAudioSource source)
: MediaStreamTrack<AudioTrackInterface>(label)
, source(source) {}

virtual std::string kind() const override {
return kAudioKind;
}

virtual AudioSourceInterface* GetSource() const override {
return nullptr;
}

virtual void AddSink(AudioTrackSinkInterface* sink) override {
cgoAudioSourceAddSink(source, (CGO_AudioSink)sink);
}
virtual void RemoveSink(AudioTrackSinkInterface* sink) override {
cgoAudioSourceRemoveSink(source, (CGO_AudioSink)sink);
}

virtual bool GetSignalLevel(int* level) override { return false; }

virtual rtc::scoped_refptr<AudioProcessorInterface> GetAudioProcessor() override {
return nullptr;
}

protected:
~AudioTrack() {
cgoAudioSourceDestruct(source);
}

private:
CGO_GoAudioSource source;
};

CGO_AudioTrack CGO_NewAudioTrack(const char* label, CGO_GoAudioSource source) {
return new rtc::RefCountedObject<AudioTrack>(label, source);
}

class AudioSink : public AudioTrackSinkInterface {
public:
explicit AudioSink(CGO_GoAudioSink s) : s(s) {}

virtual void OnData(const void* audio_data,
int bits_per_sample,
int sample_rate,
size_t number_of_channels,
size_t number_of_frames) {
cgoAudioSinkOnData(
s,
(void*)audio_data,
bits_per_sample,
sample_rate,
(int)number_of_channels,
(int)number_of_frames
);
}

CGO_GoAudioSink s;
};

void CGO_AudioSinkOnData(CGO_AudioSink s, void* data, int bitsPerSample, int sampleRate, int numberOfChannels, int numberOfFrames) {
((AudioTrackSinkInterface*)s)->OnData(data, bitsPerSample, sampleRate, (size_t)numberOfChannels, (size_t)numberOfFrames);
}

CGO_AudioSink CGO_AudioTrack_AddSink(CGO_AudioTrack t, CGO_GoAudioSink gs) {
auto s = new AudioSink(gs);
((AudioTrackInterface*)t)->AddSink(s);
return s;
}

CGO_GoAudioSink CGO_AudioTrack_RemoveSink(CGO_AudioTrack t, CGO_AudioSink cs) {
auto s = (AudioSink*)cs;
((AudioTrackInterface*)t)->RemoveSink(s);
auto gs = s->s;
delete s;
return gs;
}
142 changes: 142 additions & 0 deletions audiotrack.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
package webrtc

/*
#include "mediastreamtrack.h"
#include "audiotrack.h"
*/
import "C"
import (
"math"
"reflect"
"sync"
"unsafe"
)

/*
An AudioTrack can be obtained via NewAudioTrack (a local track) or via
PeerConnection.OnAddTrack (a remote track). Audio samples are provided or
retrieved, respectively, via an AudioSource or AudioSink.
*/
type AudioTrack struct {
*mediaStreamTrack
t C.CGO_AudioTrack
}

func newAudioTrack(t C.CGO_AudioTrack) *AudioTrack {
return &AudioTrack{
mediaStreamTrack: newMediaStreamTrack(C.CGO_MediaStreamTrack(t)),
t: t,
}
}

// NewAudioTrack creates a local audio track.
func NewAudioTrack(label string, source AudioSource) *AudioTrack {
t := C.CGO_NewAudioTrack(C.CString(label), C.CGO_GoAudioSource(audioSources.Set(source)))
return newAudioTrack(t)
}

func (t *AudioTrack) AddSink(s AudioSink) {
cAudioSinksMu.Lock()
defer cAudioSinksMu.Unlock()

if _, ok := cAudioSinks[s]; ok {
panic("AudioSink already added")
}
csink := C.CGO_AudioTrack_AddSink(t.t, C.CGO_GoAudioSink(audioSinks.Set(s)))
cAudioSinks[s] = csink
}

func (t *AudioTrack) RemoveSink(s AudioSink) {
cAudioSinksMu.Lock()
defer cAudioSinksMu.Unlock()

if csink, ok := cAudioSinks[s]; ok {
audioSinks.Delete(int(C.CGO_AudioTrack_RemoveSink(t.t, csink)))
delete(cAudioSinks, s)
}
}

var (
audioSources = NewCGOMap()
audioSinks = NewCGOMap()

cAudioSinksMu sync.Mutex
cAudioSinks = map[AudioSink]C.CGO_AudioSink{}
)

// An AudioSource pushes audio to the AudioSinks that are added to it.
type AudioSource interface {
AddAudioSink(AudioSink)
RemoveAudioSink(AudioSink)
}

// An AudioSink receives audio, typically from an AudioSource.
type AudioSink interface {
/*
OnAudioData is called when new audio data is available.
len(data) == numberOfChannels; len(data[i]) is the same for all i.
*/
OnAudioData(data [][]float64, sampleRate float64)
}

//export cgoAudioSourceAddSink
func cgoAudioSourceAddSink(source C.CGO_GoAudioSource, sink C.CGO_AudioSink) {
audioSources.Get(int(source)).(AudioSource).AddAudioSink(cAudioSink{sink})
}

//export cgoAudioSourceRemoveSink
func cgoAudioSourceRemoveSink(source C.CGO_GoAudioSource, sink C.CGO_AudioSink) {
audioSources.Get(int(source)).(AudioSource).RemoveAudioSink(cAudioSink{sink})
}

//export cgoAudioSourceDestruct
func cgoAudioSourceDestruct(source C.CGO_GoAudioSource) {
audioSources.Delete(int(source))
}

type cAudioSink struct {
s C.CGO_AudioSink
}

func (s cAudioSink) OnAudioData(data [][]float64, sampleRate float64) {
bitsPerSample := 16
numberOfChannels := len(data)
numberOfFrames := len(data[0])
buf := make([]int16, numberOfChannels*numberOfFrames*bitsPerSample/2)
i := 0
for j := range data[0] {
for _, ch := range data {
buf[i] = int16(ch[j] * float64(math.MaxInt16))
i++
}
}
C.CGO_AudioSinkOnData(s.s, unsafe.Pointer(&buf[0]), C.int(bitsPerSample), C.int(sampleRate), C.int(numberOfChannels), C.int(numberOfFrames))
}

//export cgoAudioSinkOnData
func cgoAudioSinkOnData(s C.CGO_GoAudioSink, audioData unsafe.Pointer, bitsPerSample, sampleRate, numberOfChannels, numberOfFrames int) {
if bitsPerSample != 16 {
panic("expected 16 bits per sample")
}

var buf []int16
sh := (*reflect.SliceHeader)(unsafe.Pointer(&buf))
sh.Data = uintptr(audioData)
sh.Len = numberOfChannels * numberOfFrames
sh.Cap = sh.Len

data := make([][]float64, numberOfChannels)
for i := range data {
data[i] = make([]float64, numberOfFrames)
}

i := 0
for j := range data[0] {
for _, ch := range data {
ch[j] = float64(buf[i]) / float64(math.MaxInt16)
i++
}
}

audioSinks.Get(int(s)).(AudioSink).OnAudioData(data, float64(sampleRate))
}
30 changes: 30 additions & 0 deletions audiotrack.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#ifndef _C_AUDIOTRACK_H
#define _C_AUDIOTRACK_H

#define WEBRTC_POSIX 1

#ifdef __cplusplus
extern "C" {
#endif

// In order to present an interface cgo is happy with, nothing in this file
// can directly reference header files from libwebrtc / C++ world. All the
// casting must be hidden in the .cc file.

typedef void* CGO_AudioTrack; // webrtc::AudioTrackInterface*
typedef int CGO_GoAudioSource; // key into Go audioSourceMap
typedef int CGO_GoAudioSink; // key into Go audioSinkMap
typedef void* CGO_AudioSink; // webrtc::AudioTrackSinkInterface

CGO_AudioTrack CGO_NewAudioTrack(const char* label, CGO_GoAudioSource source);

void CGO_AudioSinkOnData(CGO_AudioSink s, void* data, int bitsPerSample, int sampleRate, int numberOfChannels, int numberOfFrames);

CGO_AudioSink CGO_AudioTrack_AddSink(CGO_AudioTrack, CGO_GoAudioSink);
CGO_GoAudioSink CGO_AudioTrack_RemoveSink(CGO_AudioTrack, CGO_AudioSink);

#ifdef __cplusplus
}
#endif

#endif // _C_AUDIOTRACK_H
Loading