audio/internal/readerdriver: Refactoring: Remove player.state

This commit is contained in:
Hajime Hoshi 2021-05-05 15:32:50 +09:00
parent f556590582
commit ded3cd1433
4 changed files with 30 additions and 17 deletions

View File

@ -68,6 +68,10 @@ public:
std::atomic_store(&volume_, volume);
}
bool IsPlaying() {
return stream_->getState() == oboe::StreamState::Started;
}
void AppendBuffer(uint8_t* data, int length) {
// Sync this constants with internal/readerdriver/driver.go
const size_t bytes_per_sample = channel_num_ * bit_depth_in_bytes_;
@ -210,6 +214,11 @@ PlayerID Player_Create(int sample_rate, int channel_num, int bit_depth_in_bytes,
return reinterpret_cast<PlayerID>(p);
}
bool Player_IsPlaying(PlayerID audio_player) {
Player* p = reinterpret_cast<Player*>(audio_player);
return p->IsPlaying();
}
void Player_AppendBuffer(PlayerID audio_player, uint8_t* data, int length) {
Player* p = reinterpret_cast<Player*>(audio_player);
p->AppendBuffer(data, length);

View File

@ -60,6 +60,10 @@ func onWrittenCallback(player C.uintptr_t) {
p.onWritten()
}
func (p *Player) IsPlaying() bool {
return bool(C.Player_IsPlaying(p.player))
}
func (p *Player) AppendBuffer(buf []byte) {
ptr := C.CBytes(buf)
defer C.free(ptr)

View File

@ -28,6 +28,7 @@ typedef uintptr_t PlayerID;
const char* Suspend();
const char* Resume();
PlayerID Player_Create(int sample_rate, int channel_num, int bit_depth_in_bytes, double volume, uintptr_t go_player);
bool Player_IsPlaying(PlayerID audio_player);
void Player_AppendBuffer(PlayerID audio_player, uint8_t* data, int length);
const char* Player_Play(PlayerID audio_player);
const char* Player_Pause(PlayerID audio_player);

View File

@ -74,11 +74,12 @@ type player struct {
src io.Reader
err error
cond *sync.Cond
state playerState
closed bool
volume float64
}
func (p *player) Pause() {
// TODO: Implement the 'true' pause after #1633 is fixed.
p.Reset()
}
@ -89,7 +90,7 @@ func (p *player) Play() {
if p.err != nil {
return
}
if p.state != playerPaused {
if p.p != nil && p.p.IsPlaying() {
return
}
defer p.cond.Signal()
@ -113,7 +114,6 @@ func (p *player) Play() {
p.setErrorImpl(err)
return
}
p.state = playerPlay
if runLoop {
go p.loop()
}
@ -122,7 +122,10 @@ func (p *player) Play() {
func (p *player) IsPlaying() bool {
p.cond.L.Lock()
defer p.cond.L.Unlock()
return p.state == playerPlay
if p.p == nil {
return false
}
return p.p.IsPlaying()
}
func (p *player) Reset() {
@ -132,7 +135,7 @@ func (p *player) Reset() {
if p.err != nil {
return
}
if p.state == playerClosed {
if p.closed {
return
}
if p.p == nil {
@ -146,7 +149,6 @@ func (p *player) Reset() {
p.setErrorImpl(err)
return
}
p.state = playerPaused
}
func (p *player) Volume() float64 {
@ -190,7 +192,7 @@ func (p *player) closeImpl() error {
defer p.cond.Signal()
runtime.SetFinalizer(p, nil)
p.state = playerClosed
p.closed = true
if p.p == nil {
return p.err
}
@ -215,19 +217,16 @@ func (p *player) setErrorImpl(err error) {
}
func (p *player) shouldWait() bool {
if p.closed {
return false
}
if p.p == nil {
return false
}
switch p.state {
case playerPlay:
return p.p.UnplayedBufferSize() >= int64(p.context.MaxBufferSize())
case playerPaused:
return true
case playerClosed:
if !p.p.IsPlaying() {
return false
default:
panic("not reached")
}
return p.p.UnplayedBufferSize() >= int64(p.context.MaxBufferSize())
}
func (p *player) wait() bool {
@ -237,14 +236,14 @@ func (p *player) wait() bool {
for p.shouldWait() {
p.cond.Wait()
}
return p.p != nil && p.state == playerPlay
return p.p != nil && p.p.IsPlaying()
}
func (p *player) write(buf []byte) {
p.cond.L.Lock()
defer p.cond.L.Unlock()
if p.state == playerClosed {
if p.closed {
return
}
if p.p == nil {