Skip to content

core: Queue up Sound and SoundChannel methods during loading #9457

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

Merged
merged 1 commit into from
Feb 11, 2023
Merged
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
74 changes: 43 additions & 31 deletions core/src/avm2/globals/flash/media/sound.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
use crate::avm2::activation::Activation;
use crate::avm2::class::{Class, ClassAttributes};
use crate::avm2::method::{Method, NativeMethodImpl};
use crate::avm2::object::{sound_allocator, Object, SoundChannelObject, TObject};
use crate::avm2::object::{sound_allocator, Object, QueuedPlay, SoundChannelObject, TObject};
use crate::avm2::value::Value;
use crate::avm2::Error;
use crate::avm2::Multiname;
Expand All @@ -12,20 +12,24 @@ use crate::avm2::QName;
use crate::backend::navigator::Request;
use crate::character::Character;
use crate::display_object::SoundTransform;
use crate::{avm2_stub_getter, avm2_stub_method};
use crate::{avm2_stub_constructor, avm2_stub_getter, avm2_stub_method};
use gc_arena::{GcCell, MutationContext};
use swf::{SoundEvent, SoundInfo};

/// Implements `flash.media.Sound`'s instance constructor.
pub fn instance_init<'gc>(
activation: &mut Activation<'_, 'gc>,
this: Option<Object<'gc>>,
_args: &[Value<'gc>],
args: &[Value<'gc>],
) -> Result<Value<'gc>, Error<'gc>> {
if let Some(this) = this {
activation.super_init(this, &[])?;

if this.as_sound().is_none() {
if !args.is_empty() {
avm2_stub_constructor!(activation, "flash.media.Sound", "with arguments");
}

if let Some(sound_object) = this.as_sound_object() {
let class_object = this
.instance_of()
.ok_or("Attempted to construct Sound on a bare object.")?;
Expand All @@ -42,7 +46,8 @@ pub fn instance_init<'gc>(
.library_for_movie_mut(movie)
.character_by_id(symbol)
{
this.set_sound(activation.context.gc_context, *sound);
let sound = *sound;
sound_object.set_sound(&mut activation.context, sound)?;
} else {
tracing::warn!("Attempted to construct subclass of Sound, {}, which is associated with non-Sound character {}", class_object.inner_class_definition().read().name().local_name(), symbol);
}
Expand All @@ -68,10 +73,13 @@ pub fn bytes_total<'gc>(
this: Option<Object<'gc>>,
_args: &[Value<'gc>],
) -> Result<Value<'gc>, Error<'gc>> {
if let Some(sound) = this.and_then(|this| this.as_sound()) {
if let Some(length) = activation.context.audio.get_sound_size(sound) {
return Ok((length).into());
if let Some(sound) = this.and_then(|this| this.as_sound_object()) {
if let Some(sound_handle) = sound.sound_handle() {
if let Some(length) = activation.context.audio.get_sound_size(sound_handle) {
return Ok((length).into());
}
}
return Ok(0.into());
}

Ok(Value::Undefined)
Expand Down Expand Up @@ -105,10 +113,13 @@ pub fn length<'gc>(
this: Option<Object<'gc>>,
_args: &[Value<'gc>],
) -> Result<Value<'gc>, Error<'gc>> {
if let Some(sound) = this.and_then(|this| this.as_sound()) {
if let Some(duration) = activation.context.audio.get_sound_duration(sound) {
return Ok((duration).into());
if let Some(sound) = this.and_then(|this| this.as_sound_object()) {
if let Some(sound_handle) = sound.sound_handle() {
if let Some(duration) = activation.context.audio.get_sound_duration(sound_handle) {
return Ok((duration).into());
}
}
return Ok(0.into());
}

Ok(Value::Undefined)
Expand All @@ -120,7 +131,7 @@ pub fn play<'gc>(
this: Option<Object<'gc>>,
args: &[Value<'gc>],
) -> Result<Value<'gc>, Error<'gc>> {
if let Some(sound) = this.and_then(|this| this.as_sound()) {
if let Some(sound_object) = this.and_then(|this| this.as_sound_object()) {
let position = args
.get(0)
.cloned()
Expand All @@ -133,12 +144,6 @@ pub fn play<'gc>(
.coerce_to_i32(activation)?;
let sound_transform = args.get(2).cloned().unwrap_or(Value::Null).as_object();

if let Some(duration) = activation.context.audio.get_sound_duration(sound) {
if position > duration {
return Ok(Value::Null);
}
}

let in_sample = if position > 0.0 {
Some((position / 1000.0 * 44100.0) as u32)
} else {
Expand All @@ -153,23 +158,29 @@ pub fn play<'gc>(
envelope: None,
};

if let Some(instance) = activation
.context
.start_sound(sound, &sound_info, None, None)
{
if let Some(sound_transform) = sound_transform {
let st = SoundTransform::from_avm2_object(activation, sound_transform)?;
activation.context.set_local_sound_transform(instance, st);
}

let sound_channel = SoundChannelObject::from_sound_instance(activation, instance)?;
let sound_transform = if let Some(sound_transform) = sound_transform {
Some(SoundTransform::from_avm2_object(
activation,
sound_transform,
)?)
} else {
None
};

activation
.context
.attach_avm2_sound_channel(instance, sound_channel);
let sound_channel = SoundChannelObject::empty(activation)?;

let queued_play = QueuedPlay {
position,
sound_info,
sound_transform,
sound_channel,
};
if sound_object.play(queued_play, activation)? {
return Ok(sound_channel.into());
}
// If we start playing a loaded sound with an invalid position,
// this method returns `null`
return Ok(Value::Null);
}

Ok(Value::Null)
Expand Down Expand Up @@ -202,6 +213,7 @@ pub fn load<'gc>(
args: &[Value<'gc>],
) -> Result<Value<'gc>, Error<'gc>> {
if let Some(this) = this {
// FIXME - don't allow replacing an existing sound
let url_request = match args.get(0) {
Some(Value::Object(request)) => request,
// This should never actually happen
Expand Down
22 changes: 5 additions & 17 deletions core/src/avm2/globals/flash/media/soundchannel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,11 +89,7 @@ pub fn sound_transform<'gc>(
_args: &[Value<'gc>],
) -> Result<Value<'gc>, Error<'gc>> {
if let Some(channel) = this.and_then(|this| this.as_sound_channel()) {
let dobj_st = channel
.instance()
.and_then(|instance| activation.context.local_sound_transform(instance))
.cloned()
.unwrap_or_default();
let dobj_st = channel.sound_transform(activation).unwrap_or_default();

return Ok(dobj_st.into_avm2_object(activation)?.into());
}
Expand All @@ -107,20 +103,15 @@ pub fn set_sound_transform<'gc>(
this: Option<Object<'gc>>,
args: &[Value<'gc>],
) -> Result<Value<'gc>, Error<'gc>> {
if let Some(instance) = this
.and_then(|this| this.as_sound_channel())
.and_then(|channel| channel.instance())
{
if let Some(sound_channel) = this.and_then(|this| this.as_sound_channel()) {
let as3_st = args
.get(0)
.cloned()
.unwrap_or(Value::Undefined)
.coerce_to_object(activation)?;
let dobj_st = SoundTransform::from_avm2_object(activation, as3_st)?;

activation
.context
.set_local_sound_transform(instance, dobj_st);
sound_channel.set_sound_transform(activation, dobj_st);
}

Ok(Value::Undefined)
Expand All @@ -132,11 +123,8 @@ pub fn stop<'gc>(
this: Option<Object<'gc>>,
_args: &[Value<'gc>],
) -> Result<Value<'gc>, Error<'gc>> {
if let Some(instance) = this
.and_then(|this| this.as_sound_channel())
.and_then(|channel| channel.instance())
{
activation.context.stop_sound(instance);
if let Some(sound_channel) = this.and_then(|this| this.as_sound_channel()) {
sound_channel.stop(activation);
}

Ok(Value::Undefined)
Expand Down
15 changes: 2 additions & 13 deletions core/src/avm2/object.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ use crate::avm2::Error;
use crate::avm2::Multiname;
use crate::avm2::Namespace;
use crate::avm2::QName;
use crate::backend::audio::{SoundHandle, SoundInstanceHandle};
use crate::bitmap::bitmap_data::{BitmapData, BitmapDataWrapper};
use crate::display_object::DisplayObject;
use crate::html::TextFormat;
Expand Down Expand Up @@ -81,7 +80,7 @@ pub use crate::avm2::object::proxy_object::{proxy_allocator, ProxyObject};
pub use crate::avm2::object::qname_object::{qname_allocator, QNameObject};
pub use crate::avm2::object::regexp_object::{regexp_allocator, RegExpObject};
pub use crate::avm2::object::script_object::{ScriptObject, ScriptObjectData};
pub use crate::avm2::object::sound_object::{sound_allocator, SoundObject};
pub use crate::avm2::object::sound_object::{sound_allocator, QueuedPlay, SoundData, SoundObject};
pub use crate::avm2::object::soundchannel_object::{soundchannel_allocator, SoundChannelObject};
pub use crate::avm2::object::stage3d_object::{stage_3d_allocator, Stage3DObject};
pub use crate::avm2::object::stage_object::{stage_allocator, StageObject};
Expand Down Expand Up @@ -1087,25 +1086,15 @@ pub trait TObject<'gc>: 'gc + Collect + Debug + Into<Object<'gc>> + Clone + Copy
}

/// Unwrap this object's sound handle.
fn as_sound(self) -> Option<SoundHandle> {
fn as_sound_object(self) -> Option<SoundObject<'gc>> {
None
}

/// Associate the object with a particular sound handle.
///
/// This does nothing if the object is not a sound.
fn set_sound(self, _mc: MutationContext<'gc, '_>, _sound: SoundHandle) {}

/// Unwrap this object's sound instance handle.
fn as_sound_channel(self) -> Option<SoundChannelObject<'gc>> {
None
}

/// Associate the object with a particular sound instance handle.
///
/// This does nothing if the object is not a sound channel.
fn set_sound_instance(self, _mc: MutationContext<'gc, '_>, _sound: SoundInstanceHandle) {}

/// Unwrap this object's bitmap data
fn as_bitmap_data(&self) -> Option<GcCell<'gc, BitmapData<'gc>>> {
None
Expand Down
Loading