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 {
|
2021-06-25 18:11:33 +02:00
|
|
|
return true
|
2021-05-05 19:36:44 +02:00
|
|
|
}
|
|
|
|
|
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
|
2021-05-08 22:36:03 +02:00
|
|
|
volume float64
|
2021-06-08 18:47:36 +02:00
|
|
|
|
|
|
|
m sync.Mutex
|
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{}
|
2021-06-08 18:47:36 +02:00
|
|
|
cond *sync.Cond
|
2021-05-05 19:36:44 +02:00
|
|
|
}
|
|
|
|
|
2021-05-09 08:53:33 +02:00
|
|
|
func (p *players) add(player *playerImpl, audioQueue C.AudioQueueRef) {
|
2021-06-08 18:47:36 +02:00
|
|
|
p.cond.L.Lock()
|
|
|
|
defer p.cond.L.Unlock()
|
2021-05-05 19:36:44 +02:00
|
|
|
|
|
|
|
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-06-08 18:47:36 +02:00
|
|
|
runLoop := len(p.players) == 0
|
2021-05-09 08:53:33 +02:00
|
|
|
p.players[audioQueue] = player
|
2021-06-08 18:47:36 +02:00
|
|
|
if runLoop {
|
|
|
|
// Use the only one loop for multiple players (#1662).
|
|
|
|
go p.loop()
|
|
|
|
}
|
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-06-08 18:47:36 +02:00
|
|
|
p.cond.L.Lock()
|
|
|
|
defer p.cond.L.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-06-08 18:47:36 +02:00
|
|
|
p.cond.L.Lock()
|
|
|
|
defer p.cond.L.Unlock()
|
2021-05-05 19:36:44 +02:00
|
|
|
|
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)
|
2021-06-08 18:47:36 +02:00
|
|
|
|
|
|
|
p.cond.Signal()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *players) shouldWait() bool {
|
|
|
|
if len(p.players) == 0 {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, pl := range p.players {
|
|
|
|
if pl.canReadSourceToBuffer() {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *players) wait() bool {
|
|
|
|
p.cond.L.Lock()
|
|
|
|
defer p.cond.L.Unlock()
|
|
|
|
|
|
|
|
for p.shouldWait() {
|
|
|
|
p.cond.Wait()
|
|
|
|
}
|
|
|
|
return len(p.players) > 0
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *players) loop() {
|
|
|
|
var players []*playerImpl
|
|
|
|
for {
|
|
|
|
if !p.wait() {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
p.cond.L.Lock()
|
|
|
|
players = players[:0]
|
|
|
|
for _, pl := range p.players {
|
|
|
|
players = append(players, pl)
|
|
|
|
}
|
|
|
|
p.cond.L.Unlock()
|
|
|
|
|
|
|
|
for _, pl := range players {
|
|
|
|
pl.readSourceToBuffer()
|
|
|
|
}
|
|
|
|
}
|
2021-05-05 19:36:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (p *players) suspend() error {
|
2021-06-08 18:47:36 +02:00
|
|
|
p.cond.L.Lock()
|
|
|
|
defer p.cond.L.Unlock()
|
2021-05-05 19:36:44 +02:00
|
|
|
|
|
|
|
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-06-08 18:47:36 +02:00
|
|
|
p.cond.L.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)
|
|
|
|
}
|
2021-06-08 18:47:36 +02:00
|
|
|
p.cond.L.Unlock()
|
2021-05-09 11:30:46 +02:00
|
|
|
|
|
|
|
for pl := range players {
|
2021-05-05 19:36:44 +02:00
|
|
|
pl.Play()
|
|
|
|
if err := pl.Err(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-06-08 18:47:36 +02:00
|
|
|
var thePlayers = &players{
|
|
|
|
cond: sync.NewCond(&sync.Mutex{}),
|
|
|
|
}
|
2021-05-05 19:36:44 +02:00
|
|
|
|
|
|
|
func (c *context) NewPlayer(src io.Reader) Player {
|
|
|
|
p := &player{
|
|
|
|
p: &playerImpl{
|
|
|
|
context: c,
|
|
|
|
src: src,
|
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 {
|
2021-06-08 18:47:36 +02:00
|
|
|
p.m.Lock()
|
|
|
|
defer p.m.Unlock()
|
2021-05-05 19:36:44 +02:00
|
|
|
|
|
|
|
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-06-08 18:47:36 +02:00
|
|
|
p.m.Lock()
|
|
|
|
defer p.m.Unlock()
|
2021-05-16 13:07:51 +02:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
2021-06-08 18:47:36 +02:00
|
|
|
p.m.Unlock()
|
|
|
|
thePlayers.add(p, p.audioQueue)
|
|
|
|
p.m.Lock()
|
2021-05-05 19:36:44 +02:00
|
|
|
}
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *player) Pause() {
|
|
|
|
p.p.Pause()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *playerImpl) Pause() {
|
2021-06-08 18:47:36 +02:00
|
|
|
p.m.Lock()
|
|
|
|
defer p.m.Unlock()
|
2021-05-05 19:36:44 +02:00
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *player) Reset() {
|
|
|
|
p.p.Reset()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *playerImpl) Reset() {
|
2021-06-08 18:47:36 +02:00
|
|
|
p.m.Lock()
|
|
|
|
defer p.m.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-06-25 18:11:33 +02:00
|
|
|
// AudioQueueReset is not efficient (#1650, #1680).
|
|
|
|
// Discard the current AudioQueue and recreate one when playing this player again.
|
|
|
|
if err := p.closeAudioQueue(); err != nil {
|
|
|
|
p.setErrorImpl(err)
|
|
|
|
return
|
2021-05-08 22:46:55 +02:00
|
|
|
}
|
2021-05-05 19:36:44 +02:00
|
|
|
|
|
|
|
p.state = playerPaused
|
|
|
|
p.buf = p.buf[:0]
|
|
|
|
p.eof = false
|
2021-06-08 18:47:36 +02:00
|
|
|
thePlayers.cond.Signal()
|
2021-05-05 19:36:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (p *player) IsPlaying() bool {
|
|
|
|
return p.p.IsPlaying()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *playerImpl) IsPlaying() bool {
|
2021-06-08 18:47:36 +02:00
|
|
|
p.m.Lock()
|
|
|
|
defer p.m.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 {
|
2021-06-08 18:47:36 +02:00
|
|
|
p.m.Lock()
|
|
|
|
defer p.m.Unlock()
|
2021-05-08 22:36:03 +02:00
|
|
|
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) {
|
2021-06-08 18:47:36 +02:00
|
|
|
p.m.Lock()
|
|
|
|
defer p.m.Unlock()
|
2021-05-08 22:36:03 +02:00
|
|
|
|
|
|
|
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-06-08 18:47:36 +02:00
|
|
|
p.m.Lock()
|
|
|
|
defer p.m.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 {
|
2021-06-08 18:47:36 +02:00
|
|
|
p.m.Lock()
|
|
|
|
defer p.m.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-06-25 18:11:33 +02:00
|
|
|
if err := p.closeAudioQueue(); err != nil && p.err == nil {
|
|
|
|
// setErrorImpl calls closeImpl. Do not call this.
|
|
|
|
p.err = err
|
|
|
|
}
|
2021-05-09 14:07:39 +02:00
|
|
|
|
2021-06-25 18:11:33 +02:00
|
|
|
p.state = playerClosed
|
|
|
|
return p.err
|
|
|
|
}
|
2021-05-09 14:07:39 +02:00
|
|
|
|
2021-06-25 18:11:33 +02:00
|
|
|
func (p *playerImpl) closeAudioQueue() error {
|
|
|
|
if p.audioQueue == nil {
|
|
|
|
return nil
|
|
|
|
}
|
2021-05-09 14:07:39 +02:00
|
|
|
|
2021-06-25 18:11:33 +02:00
|
|
|
// Even if reuseLater is true, AudioQueuePause is not efficient for reusing.
|
|
|
|
// AudioQueueStart takes long if the AudioQueueStop is not called.
|
|
|
|
|
|
|
|
// AudioQueueStop might invoke AudioQueueReset. Unlock the mutex here to avoid a deadlock.
|
|
|
|
q := p.audioQueue
|
|
|
|
p.m.Unlock()
|
|
|
|
osstatus := C.AudioQueueStop(q, C.true)
|
|
|
|
p.m.Lock()
|
|
|
|
|
|
|
|
if osstatus != C.noErr && p.err == nil {
|
|
|
|
return fmt.Errorf("readerdriver: AudioQueueStop failed: %d", osstatus)
|
2021-05-05 19:36:44 +02:00
|
|
|
}
|
2021-05-09 12:54:34 +02:00
|
|
|
|
2021-06-25 18:11:33 +02:00
|
|
|
// All the AudioQueueBuffers are already dequeued. It is safe to dispose the AudioQueue and its buffers.
|
|
|
|
if err := p.context.audioQueuePool.Put(p.audioQueue); err != nil && p.err == nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
p.m.Unlock()
|
|
|
|
thePlayers.remove(p.audioQueue)
|
|
|
|
p.m.Lock()
|
|
|
|
p.audioQueue = nil
|
|
|
|
return nil
|
2021-05-05 19:36:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
//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-06-08 18:47:36 +02:00
|
|
|
// The player might be already closed.
|
|
|
|
if p == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2021-06-08 18:47:36 +02:00
|
|
|
p.m.Lock()
|
|
|
|
defer p.m.Unlock()
|
2021-05-16 21:03:04 +02:00
|
|
|
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) {
|
2021-06-08 18:47:36 +02:00
|
|
|
p.m.Lock()
|
|
|
|
defer p.m.Unlock()
|
2021-05-05 19:36:44 +02:00
|
|
|
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]
|
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:]
|
2021-06-08 18:47:36 +02:00
|
|
|
thePlayers.cond.Signal()
|
2021-05-05 19:36:44 +02:00
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *playerImpl) setError(err error) {
|
2021-06-08 18:47:36 +02:00
|
|
|
p.m.Lock()
|
|
|
|
defer p.m.Unlock()
|
2021-05-05 19:36:44 +02:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2021-06-08 18:47:36 +02:00
|
|
|
func (p *playerImpl) canReadSourceToBuffer() bool {
|
|
|
|
p.m.Lock()
|
|
|
|
defer p.m.Unlock()
|
2021-05-05 19:36:44 +02:00
|
|
|
|
2021-06-08 18:47:36 +02:00
|
|
|
if p.eof {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return len(p.buf) < p.context.maxBufferSize()
|
|
|
|
}
|
2021-05-05 19:36:44 +02:00
|
|
|
|
2021-06-08 18:47:36 +02:00
|
|
|
func (p *playerImpl) readSourceToBuffer() {
|
|
|
|
p.m.Lock()
|
|
|
|
defer p.m.Unlock()
|
|
|
|
|
|
|
|
if p.err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if p.state == playerClosed {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
maxBufferSize := p.context.maxBufferSize()
|
|
|
|
if len(p.buf) >= maxBufferSize {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
buf := make([]byte, maxBufferSize)
|
2021-06-24 17:32:19 +02:00
|
|
|
n, err := p.src.Read(buf)
|
2021-06-08 18:47:36 +02:00
|
|
|
|
|
|
|
if err != nil && err != io.EOF {
|
|
|
|
p.setError(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
p.buf = append(p.buf, buf[:n]...)
|
|
|
|
if err == io.EOF && len(p.buf) == 0 {
|
|
|
|
p.eof = true
|
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()
|
|
|
|
}
|