2021-05-05 19:36:44 +02:00
|
|
|
// Copyright 2021 The Ebiten Authors
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
package readerdriver
|
|
|
|
|
|
|
|
// #cgo LDFLAGS: -framework AudioToolbox
|
|
|
|
//
|
|
|
|
// #import <AudioToolbox/AudioToolbox.h>
|
|
|
|
//
|
|
|
|
// void ebiten_readerdriver_render(void* inUserData, AudioQueueRef inAQ, AudioQueueBufferRef inBuffer);
|
|
|
|
//
|
|
|
|
// void ebiten_readerdriver_setNotificationHandler();
|
|
|
|
import "C"
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"runtime"
|
|
|
|
"sync"
|
2021-05-09 09:30:19 +02:00
|
|
|
"time"
|
2021-05-05 19:36:44 +02:00
|
|
|
"unsafe"
|
|
|
|
)
|
|
|
|
|
|
|
|
func IsAvailable() bool {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2021-05-09 09:30:19 +02:00
|
|
|
type audioQueuePoolItem struct {
|
|
|
|
queue C.AudioQueueRef
|
|
|
|
bufs []C.AudioQueueBufferRef
|
|
|
|
}
|
|
|
|
|
2021-05-09 10:23:54 +02:00
|
|
|
const audioQueuePoolMaxItemNum = 32 // 32 is an arbitrary number.
|
|
|
|
|
2021-05-09 09:30:19 +02:00
|
|
|
type audioQueuePool struct {
|
|
|
|
c *context
|
|
|
|
unused []audioQueuePoolItem
|
|
|
|
used []audioQueuePoolItem
|
|
|
|
m sync.Mutex
|
|
|
|
}
|
|
|
|
|
2021-05-09 10:23:54 +02:00
|
|
|
func (a *audioQueuePool) Prepare(context *context) error {
|
|
|
|
a.c = context
|
|
|
|
for i := 0; i < audioQueuePoolMaxItemNum; i++ {
|
|
|
|
if _, _, err := a.Get(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
a.unused = a.used
|
|
|
|
a.used = a.used[:0]
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-05-09 09:30:19 +02:00
|
|
|
func (a *audioQueuePool) Get() (C.AudioQueueRef, []C.AudioQueueBufferRef, error) {
|
|
|
|
a.m.Lock()
|
|
|
|
defer a.m.Unlock()
|
|
|
|
|
|
|
|
if len(a.unused) > 0 {
|
|
|
|
q := a.unused[0]
|
|
|
|
a.unused = a.unused[1:]
|
|
|
|
a.used = append(a.used, q)
|
|
|
|
return q.queue, q.bufs, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
flags := C.kAudioFormatFlagIsPacked
|
|
|
|
if a.c.bitDepthInBytes != 1 {
|
|
|
|
flags |= C.kAudioFormatFlagIsSignedInteger
|
|
|
|
}
|
|
|
|
desc := C.AudioStreamBasicDescription{
|
|
|
|
mSampleRate: C.double(a.c.sampleRate),
|
|
|
|
mFormatID: C.kAudioFormatLinearPCM,
|
|
|
|
mFormatFlags: C.UInt32(flags),
|
|
|
|
mBytesPerPacket: C.UInt32(a.c.channelNum * a.c.bitDepthInBytes),
|
|
|
|
mFramesPerPacket: 1,
|
|
|
|
mBytesPerFrame: C.UInt32(a.c.channelNum * a.c.bitDepthInBytes),
|
|
|
|
mChannelsPerFrame: C.UInt32(a.c.channelNum),
|
|
|
|
mBitsPerChannel: C.UInt32(8 * a.c.bitDepthInBytes),
|
|
|
|
}
|
|
|
|
|
|
|
|
var audioQueue C.AudioQueueRef
|
|
|
|
if osstatus := C.AudioQueueNewOutput(
|
|
|
|
&desc,
|
|
|
|
(C.AudioQueueOutputCallback)(C.ebiten_readerdriver_render),
|
|
|
|
nil,
|
|
|
|
(C.CFRunLoopRef)(0),
|
|
|
|
(C.CFStringRef)(0),
|
|
|
|
0,
|
|
|
|
&audioQueue); osstatus != C.noErr {
|
|
|
|
return nil, nil, fmt.Errorf("readerdriver: AudioQueueNewFormat with StreamFormat failed: %d", osstatus)
|
|
|
|
}
|
|
|
|
|
|
|
|
size := a.c.oneBufferSize()
|
|
|
|
bufs := make([]C.AudioQueueBufferRef, 0, 2)
|
|
|
|
for len(bufs) < cap(bufs) {
|
|
|
|
var buf C.AudioQueueBufferRef
|
|
|
|
if osstatus := C.AudioQueueAllocateBuffer(audioQueue, C.UInt32(size), &buf); osstatus != C.noErr {
|
|
|
|
return nil, nil, fmt.Errorf("readerdriver: AudioQueueAllocateBuffer failed: %d", osstatus)
|
|
|
|
}
|
|
|
|
buf.mAudioDataByteSize = C.UInt32(size)
|
|
|
|
bufs = append(bufs, buf)
|
|
|
|
}
|
|
|
|
|
|
|
|
a.used = append(a.used, audioQueuePoolItem{
|
|
|
|
queue: audioQueue,
|
|
|
|
bufs: bufs,
|
|
|
|
})
|
|
|
|
|
|
|
|
return audioQueue, bufs, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *audioQueuePool) Put(audioQueue C.AudioQueueRef) error {
|
|
|
|
a.m.Lock()
|
|
|
|
defer a.m.Unlock()
|
|
|
|
|
|
|
|
for i, q := range a.used {
|
|
|
|
if q.queue != audioQueue {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
a.used = append(a.used[:i], a.used[i+1:]...)
|
2021-05-09 10:23:54 +02:00
|
|
|
if len(a.unused)+len(a.used) < audioQueuePoolMaxItemNum {
|
2021-05-09 09:30:19 +02:00
|
|
|
a.unused = append(a.unused, q)
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
// As the pool is too big, remove the AudioQueue.
|
|
|
|
for _, b := range q.bufs {
|
|
|
|
if osstatus := C.AudioQueueFreeBuffer(q.queue, b); osstatus != C.noErr {
|
|
|
|
return fmt.Errorf("readerdriver: AudioQueueFreeBuffer failed: %d", osstatus)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if osstatus := C.AudioQueueDispose(q.queue, C.true); osstatus != C.noErr {
|
|
|
|
return fmt.Errorf("readerdriver: AudioQueueDispose failed: %d", osstatus)
|
|
|
|
}
|
|
|
|
break
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-05-05 19:36:44 +02:00
|
|
|
type context struct {
|
|
|
|
sampleRate int
|
|
|
|
channelNum int
|
|
|
|
bitDepthInBytes int
|
2021-05-09 09:30:19 +02:00
|
|
|
|
|
|
|
audioQueuePool audioQueuePool
|
2021-05-05 19:36:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// TOOD: Convert the error code correctly.
|
|
|
|
// See https://stackoverflow.com/questions/2196869/how-do-you-convert-an-iphone-osstatus-code-to-something-useful
|
|
|
|
|
|
|
|
func NewContext(sampleRate, channelNum, bitDepthInBytes int) (Context, chan struct{}, error) {
|
|
|
|
ready := make(chan struct{})
|
|
|
|
close(ready)
|
|
|
|
|
|
|
|
c := &context{
|
|
|
|
sampleRate: sampleRate,
|
|
|
|
channelNum: channelNum,
|
|
|
|
bitDepthInBytes: bitDepthInBytes,
|
|
|
|
}
|
2021-05-09 10:23:54 +02:00
|
|
|
if err := c.audioQueuePool.Prepare(c); err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
2021-05-05 19:36:44 +02:00
|
|
|
C.ebiten_readerdriver_setNotificationHandler()
|
|
|
|
return c, ready, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *context) Suspend() error {
|
|
|
|
return thePlayers.suspend()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *context) Resume() error {
|
|
|
|
return thePlayers.resume()
|
|
|
|
}
|
|
|
|
|
|
|
|
type player struct {
|
|
|
|
p *playerImpl
|
|
|
|
}
|
|
|
|
|
|
|
|
type playerImpl struct {
|
|
|
|
context *context
|
|
|
|
src io.Reader
|
|
|
|
id int
|
|
|
|
audioQueue C.AudioQueueRef
|
|
|
|
buf []byte
|
|
|
|
unqueuedBufs []C.AudioQueueBufferRef
|
|
|
|
state playerState
|
|
|
|
err error
|
|
|
|
eof bool
|
|
|
|
cond *sync.Cond
|
2021-05-08 22:36:03 +02:00
|
|
|
volume float64
|
2021-05-05 19:36:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
type players struct {
|
2021-05-09 08:53:33 +02:00
|
|
|
players map[C.AudioQueueRef]*playerImpl
|
2021-05-05 19:36:44 +02:00
|
|
|
toResume map[*playerImpl]struct{}
|
|
|
|
m sync.Mutex
|
|
|
|
}
|
|
|
|
|
2021-05-09 08:53:33 +02:00
|
|
|
func (p *players) add(player *playerImpl, audioQueue C.AudioQueueRef) {
|
2021-05-05 19:36:44 +02:00
|
|
|
p.m.Lock()
|
|
|
|
defer p.m.Unlock()
|
|
|
|
|
|
|
|
if p.players == nil {
|
2021-05-09 08:53:33 +02:00
|
|
|
p.players = map[C.AudioQueueRef]*playerImpl{}
|
2021-05-05 19:36:44 +02:00
|
|
|
}
|
2021-05-09 08:53:33 +02:00
|
|
|
p.players[audioQueue] = player
|
2021-05-05 19:36:44 +02:00
|
|
|
}
|
|
|
|
|
2021-05-09 08:53:33 +02:00
|
|
|
func (p *players) get(audioQueue C.AudioQueueRef) *playerImpl {
|
2021-05-05 19:36:44 +02:00
|
|
|
p.m.Lock()
|
|
|
|
defer p.m.Unlock()
|
2021-05-09 08:53:33 +02:00
|
|
|
return p.players[audioQueue]
|
2021-05-05 19:36:44 +02:00
|
|
|
}
|
|
|
|
|
2021-05-09 08:53:33 +02:00
|
|
|
func (p *players) remove(audioQueue C.AudioQueueRef) {
|
2021-05-05 19:36:44 +02:00
|
|
|
p.m.Lock()
|
|
|
|
defer p.m.Unlock()
|
|
|
|
|
2021-05-09 08:53:33 +02:00
|
|
|
pl, ok := p.players[audioQueue]
|
2021-05-05 19:36:44 +02:00
|
|
|
if !ok {
|
|
|
|
return
|
|
|
|
}
|
2021-05-09 08:53:33 +02:00
|
|
|
delete(p.players, audioQueue)
|
2021-05-05 19:36:44 +02:00
|
|
|
delete(p.toResume, pl)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *players) suspend() error {
|
|
|
|
p.m.Lock()
|
|
|
|
defer p.m.Unlock()
|
|
|
|
|
|
|
|
for _, pl := range p.players {
|
|
|
|
if !pl.IsPlaying() {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
// TODO: Is this OK to Pause instead of Close?
|
|
|
|
// Oboe (Android) closes players when suspending to avoid hogging audio resources which other apps could use.
|
|
|
|
pl.Pause()
|
|
|
|
if err := pl.Err(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if p.toResume == nil {
|
|
|
|
p.toResume = map[*playerImpl]struct{}{}
|
|
|
|
}
|
|
|
|
p.toResume[pl] = struct{}{}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *players) resume() error {
|
2021-05-09 11:30:46 +02:00
|
|
|
// playerImpl's Play can touch p. Avoid the deadlock.
|
2021-05-05 19:36:44 +02:00
|
|
|
p.m.Lock()
|
2021-05-09 11:30:46 +02:00
|
|
|
players := map[*playerImpl]struct{}{}
|
2021-05-05 19:36:44 +02:00
|
|
|
for pl := range p.toResume {
|
2021-05-09 11:30:46 +02:00
|
|
|
players[pl] = struct{}{}
|
|
|
|
delete(p.toResume, pl)
|
|
|
|
}
|
|
|
|
p.m.Unlock()
|
|
|
|
|
|
|
|
for pl := range players {
|
2021-05-05 19:36:44 +02:00
|
|
|
pl.Play()
|
|
|
|
if err := pl.Err(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
var thePlayers players
|
|
|
|
|
|
|
|
func (c *context) NewPlayer(src io.Reader) Player {
|
|
|
|
p := &player{
|
|
|
|
p: &playerImpl{
|
|
|
|
context: c,
|
|
|
|
src: src,
|
|
|
|
cond: sync.NewCond(&sync.Mutex{}),
|
2021-05-08 22:36:03 +02:00
|
|
|
volume: 1,
|
2021-05-05 19:36:44 +02:00
|
|
|
},
|
|
|
|
}
|
|
|
|
runtime.SetFinalizer(p, (*player).Close)
|
|
|
|
return p
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *player) Err() error {
|
|
|
|
return p.p.Err()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *playerImpl) Err() error {
|
|
|
|
p.cond.L.Lock()
|
|
|
|
defer p.cond.L.Unlock()
|
|
|
|
|
|
|
|
return p.err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *player) Play() {
|
|
|
|
p.p.Play()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *playerImpl) Play() {
|
2021-05-09 14:49:45 +02:00
|
|
|
// Call Play asynchronously since AudioQueuePrime and AudioQueuePlay might take long.
|
2021-05-16 13:07:51 +02:00
|
|
|
ch := make(chan struct{})
|
2021-05-09 14:49:45 +02:00
|
|
|
go func() {
|
2021-05-16 13:07:51 +02:00
|
|
|
p.cond.L.Lock()
|
|
|
|
defer p.cond.L.Unlock()
|
|
|
|
close(ch)
|
2021-05-09 14:49:45 +02:00
|
|
|
p.playImpl()
|
|
|
|
}()
|
2021-05-16 13:07:51 +02:00
|
|
|
|
|
|
|
// Wait until the mutex is locked in the above goroutine.
|
|
|
|
<-ch
|
2021-05-09 14:49:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (p *playerImpl) playImpl() {
|
2021-05-08 22:58:55 +02:00
|
|
|
if p.err != nil {
|
|
|
|
return
|
|
|
|
}
|
2021-05-05 19:36:44 +02:00
|
|
|
if p.state != playerPaused {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
var runLoop bool
|
|
|
|
if p.audioQueue == nil {
|
2021-05-09 09:30:19 +02:00
|
|
|
audioQueue, audioQueueBuffers, err := p.context.audioQueuePool.Get()
|
|
|
|
if err != nil {
|
|
|
|
p.setErrorImpl(err)
|
2021-05-05 19:36:44 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
p.audioQueue = audioQueue
|
2021-05-09 09:30:19 +02:00
|
|
|
p.unqueuedBufs = audioQueueBuffers
|
2021-05-08 22:36:03 +02:00
|
|
|
C.AudioQueueSetParameter(p.audioQueue, C.kAudioQueueParam_Volume, C.AudioQueueParameterValue(p.volume))
|
2021-05-09 08:53:33 +02:00
|
|
|
thePlayers.add(p, p.audioQueue)
|
|
|
|
|
2021-05-05 19:36:44 +02:00
|
|
|
runLoop = true
|
|
|
|
}
|
|
|
|
|
2021-05-09 07:41:23 +02:00
|
|
|
buf := make([]byte, p.context.maxBufferSize())
|
2021-05-05 19:36:44 +02:00
|
|
|
for len(p.buf) < p.context.maxBufferSize() {
|
|
|
|
n, err := p.src.Read(buf)
|
|
|
|
if err != nil && err != io.EOF {
|
|
|
|
p.setErrorImpl(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
p.buf = append(p.buf, buf[:n]...)
|
|
|
|
if err == io.EOF {
|
|
|
|
p.eof = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bufs := make([]C.AudioQueueBufferRef, len(p.unqueuedBufs))
|
|
|
|
copy(bufs, p.unqueuedBufs)
|
|
|
|
var unenqueued []C.AudioQueueBufferRef
|
|
|
|
for _, buf := range bufs {
|
|
|
|
queued, err := p.appendBufferImpl(buf)
|
|
|
|
if err != nil {
|
|
|
|
p.setErrorImpl(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if !queued {
|
|
|
|
unenqueued = append(unenqueued, buf)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
p.unqueuedBufs = unenqueued
|
|
|
|
if len(p.unqueuedBufs) == 2 && p.eof {
|
|
|
|
p.state = playerPaused
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-05-09 09:30:19 +02:00
|
|
|
for {
|
2021-05-09 14:31:55 +02:00
|
|
|
if osstatus := C.AudioQueuePrime(p.audioQueue, 0, nil); osstatus != C.noErr {
|
|
|
|
// AudioQueuePrime might fail just after recovering from Siri.
|
2021-05-09 09:30:19 +02:00
|
|
|
if osstatus == C.AVAudioSessionErrorCodeSiriIsRecording {
|
|
|
|
time.Sleep(10 * time.Millisecond)
|
|
|
|
continue
|
|
|
|
}
|
2021-05-09 14:31:55 +02:00
|
|
|
p.setErrorImpl(fmt.Errorf("readerdriver: AudioQueuePrime failed: %d", osstatus))
|
2021-05-09 09:30:19 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
break
|
2021-05-05 19:36:44 +02:00
|
|
|
}
|
2021-05-09 09:30:19 +02:00
|
|
|
|
2021-05-09 14:31:55 +02:00
|
|
|
if osstatus := C.AudioQueueStart(p.audioQueue, nil); osstatus != C.noErr {
|
|
|
|
p.setErrorImpl(fmt.Errorf("readerdriver: AudioQueueStart failed: %d", osstatus))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-05-05 19:36:44 +02:00
|
|
|
p.state = playerPlay
|
|
|
|
p.cond.Signal()
|
|
|
|
|
|
|
|
if runLoop {
|
|
|
|
go p.loop()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *player) Pause() {
|
|
|
|
p.p.Pause()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *playerImpl) Pause() {
|
|
|
|
p.cond.L.Lock()
|
|
|
|
defer p.cond.L.Unlock()
|
|
|
|
|
2021-05-08 22:58:55 +02:00
|
|
|
if p.err != nil {
|
|
|
|
return
|
|
|
|
}
|
2021-05-05 19:36:44 +02:00
|
|
|
if p.state != playerPlay {
|
|
|
|
return
|
|
|
|
}
|
2021-05-08 22:46:55 +02:00
|
|
|
if p.audioQueue == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-05-05 19:36:44 +02:00
|
|
|
if osstatus := C.AudioQueuePause(p.audioQueue); osstatus != C.noErr && p.err == nil {
|
|
|
|
p.setErrorImpl(fmt.Errorf("readerdriver: AudioQueuePause failed: %d", osstatus))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
p.state = playerPaused
|
|
|
|
p.cond.Signal()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *player) Reset() {
|
|
|
|
p.p.Reset()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *playerImpl) Reset() {
|
|
|
|
p.cond.L.Lock()
|
|
|
|
defer p.cond.L.Unlock()
|
2021-05-17 16:18:55 +02:00
|
|
|
p.resetImpl()
|
|
|
|
}
|
2021-05-05 19:36:44 +02:00
|
|
|
|
2021-05-17 16:18:55 +02:00
|
|
|
func (p *playerImpl) resetImpl() {
|
2021-05-08 22:58:55 +02:00
|
|
|
if p.err != nil {
|
|
|
|
return
|
|
|
|
}
|
2021-05-05 19:36:44 +02:00
|
|
|
if p.state == playerClosed {
|
|
|
|
return
|
|
|
|
}
|
2021-05-08 22:46:55 +02:00
|
|
|
if p.audioQueue == nil {
|
|
|
|
return
|
|
|
|
}
|
2021-05-08 22:28:42 +02:00
|
|
|
|
2021-05-08 22:46:55 +02:00
|
|
|
if osstatus := C.AudioQueuePause(p.audioQueue); osstatus != C.noErr && p.err == nil {
|
|
|
|
p.setErrorImpl(fmt.Errorf("readerdriver: AudioQueuePause failed: %d", osstatus))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
// AudioQueueReset invokes the callback directry.
|
2021-05-09 14:07:39 +02:00
|
|
|
q := p.audioQueue
|
2021-05-08 22:46:55 +02:00
|
|
|
p.cond.L.Unlock()
|
2021-05-09 14:07:39 +02:00
|
|
|
osstatus := C.AudioQueueReset(q)
|
2021-05-09 07:41:23 +02:00
|
|
|
p.cond.L.Lock()
|
|
|
|
if osstatus != C.noErr && p.err == nil {
|
2021-05-08 22:46:55 +02:00
|
|
|
p.setErrorImpl(fmt.Errorf("readerdriver: AudioQueueReset failed: %d", osstatus))
|
|
|
|
return
|
|
|
|
}
|
2021-05-05 19:36:44 +02:00
|
|
|
|
|
|
|
p.state = playerPaused
|
|
|
|
p.buf = p.buf[:0]
|
|
|
|
p.eof = false
|
|
|
|
p.cond.Signal()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *player) IsPlaying() bool {
|
|
|
|
return p.p.IsPlaying()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *playerImpl) IsPlaying() bool {
|
|
|
|
p.cond.L.Lock()
|
|
|
|
defer p.cond.L.Unlock()
|
2021-05-09 14:49:45 +02:00
|
|
|
|
2021-05-05 19:36:44 +02:00
|
|
|
return p.state == playerPlay
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *player) Volume() float64 {
|
2021-05-08 22:36:03 +02:00
|
|
|
return p.p.Volume()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *playerImpl) Volume() float64 {
|
|
|
|
p.cond.L.Lock()
|
|
|
|
defer p.cond.L.Unlock()
|
|
|
|
return p.volume
|
2021-05-05 19:36:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (p *player) SetVolume(volume float64) {
|
2021-05-08 22:36:03 +02:00
|
|
|
p.p.SetVolume(volume)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *playerImpl) SetVolume(volume float64) {
|
|
|
|
p.cond.L.Lock()
|
|
|
|
defer p.cond.L.Unlock()
|
|
|
|
|
|
|
|
p.volume = volume
|
|
|
|
if p.audioQueue == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
C.AudioQueueSetParameter(p.audioQueue, C.kAudioQueueParam_Volume, C.AudioQueueParameterValue(volume))
|
2021-05-05 19:36:44 +02:00
|
|
|
}
|
|
|
|
|
2021-05-09 20:22:19 +02:00
|
|
|
func (p *player) UnplayedBufferSize() int {
|
2021-05-05 19:36:44 +02:00
|
|
|
return p.p.UnplayedBufferSize()
|
|
|
|
}
|
|
|
|
|
2021-05-09 20:22:19 +02:00
|
|
|
func (p *playerImpl) UnplayedBufferSize() int {
|
2021-05-05 19:36:44 +02:00
|
|
|
p.cond.L.Lock()
|
|
|
|
defer p.cond.L.Unlock()
|
2021-05-09 20:22:19 +02:00
|
|
|
return len(p.buf)
|
2021-05-05 19:36:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (p *player) Close() error {
|
|
|
|
runtime.SetFinalizer(p, nil)
|
|
|
|
return p.p.Close()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *playerImpl) Close() error {
|
|
|
|
p.cond.L.Lock()
|
|
|
|
defer p.cond.L.Unlock()
|
2021-05-17 16:18:55 +02:00
|
|
|
return p.closeImpl()
|
2021-05-09 09:30:19 +02:00
|
|
|
}
|
|
|
|
|
2021-05-17 16:18:55 +02:00
|
|
|
func (p *playerImpl) closeImpl() error {
|
2021-05-05 19:36:44 +02:00
|
|
|
if p.audioQueue != nil {
|
2021-05-09 12:55:12 +02:00
|
|
|
// Even if reuseLater is true, AudioQueuePause is not efficient for reusing.
|
2021-05-09 12:54:34 +02:00
|
|
|
// AudioQueueStart takes long if the AudioQueueStop is not called.
|
2021-05-09 14:07:39 +02:00
|
|
|
|
|
|
|
// AudioQueueStop might invoke AudioQueueReset. Unlock the mutex here to avoid a deadlock.
|
|
|
|
q := p.audioQueue
|
|
|
|
p.cond.L.Unlock()
|
|
|
|
osstatus := C.AudioQueueStop(q, C.true)
|
|
|
|
p.cond.L.Lock()
|
|
|
|
|
2021-05-18 14:08:26 +02:00
|
|
|
if osstatus != C.noErr && p.err == nil {
|
2021-05-05 19:36:44 +02:00
|
|
|
// setErrorImpl calls closeImpl. Do not call this.
|
|
|
|
p.err = fmt.Errorf("readerdriver: AudioQueueStop failed: %d", osstatus)
|
|
|
|
}
|
2021-05-09 14:07:39 +02:00
|
|
|
|
|
|
|
// All the AudioQueueBuffers are already dequeued. It is safe to dispose the AudioQueue and its buffers.
|
2021-05-18 14:08:26 +02:00
|
|
|
if err := p.context.audioQueuePool.Put(p.audioQueue); err != nil && p.err == nil {
|
2021-05-09 09:30:19 +02:00
|
|
|
p.err = err
|
2021-05-05 19:36:44 +02:00
|
|
|
}
|
2021-05-09 08:53:33 +02:00
|
|
|
thePlayers.remove(p.audioQueue)
|
2021-05-05 19:36:44 +02:00
|
|
|
p.audioQueue = nil
|
|
|
|
}
|
2021-05-09 12:54:34 +02:00
|
|
|
|
2021-05-17 16:18:55 +02:00
|
|
|
p.state = playerClosed
|
2021-05-05 19:36:44 +02:00
|
|
|
p.cond.Signal()
|
|
|
|
return p.err
|
|
|
|
}
|
|
|
|
|
|
|
|
//export ebiten_readerdriver_render
|
|
|
|
func ebiten_readerdriver_render(inUserData unsafe.Pointer, inAQ C.AudioQueueRef, inBuffer C.AudioQueueBufferRef) {
|
2021-05-09 08:53:33 +02:00
|
|
|
p := thePlayers.get(inAQ)
|
2021-05-05 19:36:44 +02:00
|
|
|
queued, err := p.appendBuffer(inBuffer)
|
|
|
|
if err != nil {
|
|
|
|
p.setError(err)
|
|
|
|
return
|
|
|
|
}
|
2021-05-16 21:03:04 +02:00
|
|
|
if queued {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
p.cond.L.Lock()
|
|
|
|
defer p.cond.L.Unlock()
|
|
|
|
p.unqueuedBufs = append(p.unqueuedBufs, inBuffer)
|
|
|
|
if len(p.unqueuedBufs) == 2 && p.eof {
|
2021-05-17 16:18:55 +02:00
|
|
|
p.resetImpl()
|
2021-05-05 19:36:44 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *playerImpl) appendBuffer(inBuffer C.AudioQueueBufferRef) (bool, error) {
|
|
|
|
p.cond.L.Lock()
|
|
|
|
defer p.cond.L.Unlock()
|
|
|
|
return p.appendBufferImpl(inBuffer)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *playerImpl) appendBufferImpl(inBuffer C.AudioQueueBufferRef) (bool, error) {
|
|
|
|
if p.eof && len(p.buf) == 0 {
|
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
|
2021-05-16 20:51:24 +02:00
|
|
|
oneBufferSize := p.context.oneBufferSize()
|
|
|
|
n := oneBufferSize
|
|
|
|
if len(p.buf) < n {
|
|
|
|
n = len(p.buf)
|
2021-05-05 19:36:44 +02:00
|
|
|
}
|
2021-05-16 20:51:24 +02:00
|
|
|
buf := p.buf[:n]
|
|
|
|
signal := len(p.buf[n:]) < p.context.maxBufferSize()
|
2021-05-05 19:36:44 +02:00
|
|
|
|
2021-05-16 20:51:24 +02:00
|
|
|
for i, b := range buf {
|
2021-05-05 19:36:44 +02:00
|
|
|
*(*byte)(unsafe.Pointer(uintptr(inBuffer.mAudioData) + uintptr(i))) = b
|
|
|
|
}
|
2021-05-16 20:51:24 +02:00
|
|
|
for i := len(buf); i < oneBufferSize; i++ {
|
|
|
|
*(*byte)(unsafe.Pointer(uintptr(inBuffer.mAudioData) + uintptr(i))) = 0
|
|
|
|
}
|
2021-05-05 19:36:44 +02:00
|
|
|
|
|
|
|
if osstatus := C.AudioQueueEnqueueBuffer(p.audioQueue, inBuffer, 0, nil); osstatus != C.noErr {
|
|
|
|
// This can happen just after resetting.
|
|
|
|
if osstatus == C.kAudioQueueErr_EnqueueDuringReset {
|
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
return false, fmt.Errorf("readerdriver: AudioQueueEnqueueBuffer failed: %d", osstatus)
|
|
|
|
}
|
|
|
|
|
|
|
|
p.buf = p.buf[n:]
|
|
|
|
if signal {
|
|
|
|
p.cond.Signal()
|
|
|
|
}
|
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *playerImpl) shouldWait() bool {
|
|
|
|
switch p.state {
|
|
|
|
case playerPaused:
|
|
|
|
return true
|
|
|
|
case playerPlay:
|
2021-05-15 15:39:54 +02:00
|
|
|
return len(p.buf) >= p.context.maxBufferSize()
|
2021-05-05 19:36:44 +02:00
|
|
|
case playerClosed:
|
|
|
|
return false
|
|
|
|
default:
|
|
|
|
panic("not reached")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *playerImpl) wait() bool {
|
|
|
|
p.cond.L.Lock()
|
|
|
|
defer p.cond.L.Unlock()
|
|
|
|
|
|
|
|
for p.shouldWait() {
|
|
|
|
p.cond.Wait()
|
|
|
|
}
|
|
|
|
return p.state == playerPlay
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *playerImpl) setError(err error) {
|
|
|
|
p.cond.L.Lock()
|
|
|
|
defer p.cond.L.Unlock()
|
|
|
|
p.setErrorImpl(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *playerImpl) setErrorImpl(err error) {
|
|
|
|
p.err = err
|
2021-05-17 16:18:55 +02:00
|
|
|
p.closeImpl()
|
2021-05-05 19:36:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (p *playerImpl) loop() {
|
|
|
|
buf := make([]byte, 4096)
|
|
|
|
for {
|
|
|
|
if !p.wait() {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
n, err := p.src.Read(buf)
|
|
|
|
if err != nil && err != io.EOF {
|
|
|
|
p.setError(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
p.cond.L.Lock()
|
|
|
|
p.buf = append(p.buf, buf[:n]...)
|
2021-05-16 21:04:04 +02:00
|
|
|
if err == io.EOF && len(p.buf) == 0 {
|
2021-05-05 19:36:44 +02:00
|
|
|
p.eof = true
|
|
|
|
}
|
2021-05-16 21:04:04 +02:00
|
|
|
p.cond.L.Unlock()
|
2021-05-05 19:36:44 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//export ebiten_readerdriver_setGlobalPause
|
|
|
|
func ebiten_readerdriver_setGlobalPause() {
|
|
|
|
thePlayers.suspend()
|
|
|
|
}
|
|
|
|
|
|
|
|
//export ebiten_readerdriver_setGlobalResume
|
|
|
|
func ebiten_readerdriver_setGlobalResume() {
|
|
|
|
thePlayers.resume()
|
|
|
|
}
|