Skip to content

avm1: implement Sound getBytesTotal(), getBytesLoaded() #20235

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

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
53 changes: 43 additions & 10 deletions core/src/avm1/globals/sound.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,12 @@ struct SoundData<'gc> {
/// Duration of the currently attached sound in milliseconds.
duration: Cell<Option<u32>>,

/// Size of the loaded sound buffer in bytes.
bytes_loaded: Cell<Option<u32>>,

/// Total size of the sound in bytes.
bytes_total: Cell<Option<u32>>,

/// Whether this sound is an external streaming MP3.
/// This will be true if `Sound.loadSound` was called with `isStreaming` of `true`.
/// A streaming sound can only have a single active instance.
Expand Down Expand Up @@ -70,6 +76,8 @@ impl<'gc> Sound<'gc> {
owner,
position: Cell::new(0),
duration: Cell::new(None),
bytes_loaded: Cell::new(None),
bytes_total: Cell::new(None),
is_streaming: Cell::new(false),
},
))
Expand Down Expand Up @@ -111,6 +119,22 @@ impl<'gc> Sound<'gc> {
self.0.position.set(position);
}

pub fn bytes_loaded(self) -> Option<u32> {
self.0.bytes_loaded.get()
}

pub fn set_bytes_loaded(self, bytes_loaded: Option<u32>) {
self.0.bytes_loaded.set(bytes_loaded);
}

pub fn bytes_total(self) -> Option<u32> {
self.0.bytes_total.get()
}

pub fn set_bytes_total(self, bytes_total: Option<u32>) {
self.0.bytes_total.set(bytes_total);
}

pub fn is_streaming(self) -> bool {
self.0.is_streaming.get()
}
Expand Down Expand Up @@ -206,6 +230,9 @@ fn attach_sound<'gc>(
.get_sound_duration(*sound_handle)
.map(|d| d.round() as u32),
);
let sound_size = activation.context.audio.get_sound_size(*sound_handle);
sound.set_bytes_loaded(sound_size);
sound.set_bytes_total(sound_size);
sound.set_position(0);
} else {
avm_warn!(activation, "Sound.attachSound: Sound '{}' not found", name);
Expand Down Expand Up @@ -241,28 +268,32 @@ fn set_duration<'gc>(

fn get_bytes_loaded<'gc>(
activation: &mut Activation<'_, 'gc>,
_this: Object<'gc>,
this: Object<'gc>,
_args: &[Value<'gc>],
) -> Result<Value<'gc>, Error<'gc>> {
if activation.swf_version() >= 6 {
avm1_stub!(activation, "Sound", "getBytesLoaded");
Ok(1.into())
} else {
Ok(Value::Undefined)
if let NativeObject::Sound(sound) = this.native() {
return Ok(sound.bytes_loaded().map_or(Value::Undefined, |d| d.into()));
} else {
avm_warn!(activation, "Sound.getBytesLoaded: this is not a Sound");
}
}
Ok(Value::Undefined)
}

fn get_bytes_total<'gc>(
activation: &mut Activation<'_, 'gc>,
_this: Object<'gc>,
this: Object<'gc>,
_args: &[Value<'gc>],
) -> Result<Value<'gc>, Error<'gc>> {
if activation.swf_version() >= 6 {
avm1_stub!(activation, "Sound", "getBytesTotal");
Ok(1.into())
} else {
Ok(Value::Undefined)
if let NativeObject::Sound(sound) = this.native() {
return Ok(sound.bytes_total().map_or(Value::Undefined, |d| d.into()));
} else {
avm_warn!(activation, "Sound.getBytesTotal: this is not a Sound");
}
}
Ok(Value::Undefined)
}

fn get_pan<'gc>(
Expand Down Expand Up @@ -354,6 +385,8 @@ fn load_sound<'gc>(
}
}
sound.set_is_streaming(is_streaming);
// When loadSound is called, getBytesLoaded starts to return 0
sound.set_bytes_loaded(Some(0));
let future = activation.context.load_manager.load_sound_avm1(
activation.context.player.clone(),
this,
Expand Down
3 changes: 3 additions & 0 deletions core/src/loader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1749,6 +1749,9 @@ impl<'gc> Loader<'gc> {
.map_err(|e| e.error)
.and_then(|(body, _, _, _)| {
let handle = uc.audio.register_mp3(&body)?;
let total_len = body.len();
sound.set_bytes_loaded(Some(total_len as u32));
sound.set_bytes_total(Some(total_len as u32));
sound.set_sound(Some(handle));
let duration = uc
.audio
Expand Down
Loading