How to get current uploading speed when live stream with RTMP - actionscript-3

I want to create an application to live stream with RTMP.
And I use NetStream.publish to live stream.
When I set NetStream.bufferTime=0 (default value), I can create a Timer to get NetStream.info.currentBytesPerSecond as uploading speed.
But if I set NetStream.bufferTime= 1(Some value larger then 0) , NetStream.info.currentBytesPerSecond becomes 0 and never change~
Can someone help me? Thanks

Related

How to play a seamless loop from an AudioSprite in AS3, without SAMPLE_DATA

I created a batch of sounds assembled with this tool:
AudioSprite
https://github.com/tonistiigi/audiosprite
The output is generally used for JS libraries, such as Howler, Zynga Jukebox, or SoundJS - but I wanted to see if it's possible to implement in AS3.
I started creating a Sound player that can load, parse and play the sounds based on the JSON and MP3 file this tool generates.
So far so good! ... except for loops.
Now, the big question is - is there a way to play a Sound-loop seamlessly given that all music & sounds coexist in the same MP3 file, and it has a start & end range to play and stop it?
Example of how the sounds are placed in the file:
mygame_sounds.mp3 = [BUZZ + LASER + BOING ... + TRACKLOOP]
I'm looking for a solution that does not involve using the SAMPLE_DATA Event (given it eats up a lot of CPU usage). If there's no way around it, please explain why.
So far I've had mild success using flash.utils.Timer objects triggered after a given AudioSprite's duration, but it's not consistent.
To stop / dispose of a non-looping sound, I rely on a Master Timer (running at very short intervals) and that seems to "cut" the sample appropriately. But I already tried using this Master Timer to play a looped-sound over and over - same latency issues.
Is there any method to predict / measure how much latency is to be expected by the time the sound completes one pass?
In SoundJS we could not find a way to allow smooth looping of audio sprites in AS3 and went with a timer. We found Web Audio was the only api that allowed smooth looping, and therefore recommended staying away from audiosprites for sounds that needed to loop smoothly if any other plugin might be used.
Hope that helps.
The reason of why you can't get smooth loops of a track retrieved from a larger audio file is that you cannot check sound position faster than once per SWF frame, which length depends on stage.frameRate and total processing time of your application and is generally varied. So, if your looping sounds lasts say 5.123 seconds (I don't care how many samples, just that its length does not make a full number of frames regardless of stage.frameRate), your sound will attempt to play for either 5.125 seconds (205 frames at 40 fps, IMO best bet for this particular sound), 5.133 seconds (154 frames at 30 fps) or some weird number of frames if the SWF would experience lag. The excess milliseconds cannot be totally controlled by any means due to AS3/Flash engine optimization. So, consider not using audio sprites and shift into audio packs (several audio files in an SWF, or one sound in an MP3).
Although I'm still working on the perfect solution, this is the best I could come up with:
Load the JSON file / ByteArray.
Parse the JSON file to obtain each sprites' ID, start and end times.
Load the MP3 file / ByteArray (requires loadCompressedDataFromByteArray()) into a master Sound object.
Once loaded, check if any sprites are marked as "loops".
Create separate Sound objects for the above loops, and extract the portion from the master Sound via loadPCMFromByteArray() with some "magic-numbers" (details below).
To play a one-shot sound, call the master Sound's play(sprite.start * 1000) (depending on the format, usually the JSON's start values are in seconds, needs to be in milliseconds).
To play a seamless-loop sound, call the individual Sound object's (created in step #5) play(0, 9999) method.
I won't go too deep in details on how to stop the sounds (SoundChannel.stop(), bam!), but I'll explain the "magic-numbers" mentioned above. See this snippet:
var goldenOffset:UInt = (64 << 5);
var goldenDuration:UInt = (64 << 2);
var sampleRate:UInt = 44100;
for (id in loops) {
var sprite:AudioSpriteItem = _mapSprites.get(id);
var loop:Sound = _mapLoops.get(id);
var sampleBytes = new ByteArray();
var samplesTotal:UInt = cast(sprite.duration * sampleRate + goldenDuration);
var samplesStart:UInt = cast(sprite.start * sampleRate + goldenOffset);
_sound.extract(sampleBytes, samplesTotal, samplesStart);
sampleBytes.position = 0;
loop.loadPCMFromByteArray(sampleBytes, samplesTotal, "float", true);
}
Quite honestly, these magic goldenOffset and goldenDuration values were just found via Trial-and-Error. I could get close to a seamless loop without them by just calculating the start and duration with the sampleRate (assuming 44100 by default), but each endings had a bit of a hiccup to it.
After several adjustments, those couple "64 left bit-shifted" values made the loops sound smoother.
I posted the Haxe project on github (compiled SWC also available in /bin folder) if you wish to try it / read through the code.
FLAudioSprite
Github page: https://github.com/bigp/FLAudioSprite
SWF Demo (Download): bit.ly/FLAudioSpriteSWFDemo

How does the Flash/AS3 player read the length of mp3 files?

I have an AS3 music player built into an app that I'm putting together. It works perfectly with almost every file I've used, but there is one file that it stops early on. The file is roughly 56 seconds long, the player stops at about 44 seconds. I'm using trace to show the length, and for every other song the length is correct. In this case, trace shows roughly 44 seconds instead of 56. Here's the code I use to load the file:
length = 0;
request = new URLRequest(fileAddress);
track = new Sound();
track.load(request);
track.addEventListener(Event.COMPLETE, TrackLoaded);
And here's the TrackLoaded function:
private function TrackLoaded(e:Event):void{
length = track.length;
if (playWhenLoaded == true){
trackChannel = track.play(0);
trackChannel.addEventListener(Event.SOUND_COMPLETE, TrackFinishedPlaying);
playWhenLoaded = false;
}
Works perfectly with every other file. What am I missing?
Are you willing to host this 56 sec MP3 somewhere for download & analysis? Or if you can yourself check the header info via a Hexeditor. I suspect either of two things:
1) Header has incorrect time length embedded and Flash takes that as final duration and stops there. After all why read anymore remaining bytes? they could be just metadata not audio samples Besides what encoder would lie about true duration? So its accepted as final duration even if your ears know that its incorrect.
2) MP3 samplerate /bitrate issue: Consider what sample rate is this one problem MP3? Check the sample rate against of a working MP3. Also are these various found sounds or you made each yourself? I ask to confirm you put the same settings for each file yet this one does not work?
In anycase I think you could solve this particular MP3 by re-encoding it. Maybe save as WAV or AIFF first then take that new uncompressed audio and convert back to a MP3 with samplerate of 44100 khz + Stereo sound + Constant Bitrate (avoid Variable B.Rate like hell if you dont want issues)
Checking and fixing either of the above should get you are correctly parsed MP3. Hope it helps

flash media server (FMS) reattach camera to new stream

I'm using flash media server and trying to record the stream but having a problem as I've outlined below. Thanks for taking the time to read this post.
How do I detach my camera from one stream and attach it to a new stream? What I'm trying to do is record the camera stream for 10 seconds and then play it live on a different stream..I try to netstream.close and then netstream.attachCamera(cam0) but it won't let me attach it to the second new stream. What's wrong?
You dont have to close the stream. You can use the attachCamera method with a null argument.
netstream.attachCamera(null);

Recording sound with actionscript3 without waiting for mic activity to begin writing the bytearray

I am testing sound recording in as3 and now i have this question :
I am using this line to start the recording and write sound data to the bytearray (wich works) :
_mic.addEventListener(SampleDataEvent.SAMPLE_DATA, getMicAudio);
But the thing is that the sound only start to get written when there is some activity on the mic...
For example, let say i use a "rec" button and click it, i want the bytearray to be filled as soon as the button is clicked...not waiting for mic activity...
So, is it possible to fill a bytearray with "no sound" and how would that be done?
Thanks for any help you can bring!
Just adjust the microphone settings so that there is ALWAYS activity on it as long as you're running it. You do this by adjusting settings like microphone.silenceLevel:
http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/media/Microphone.html#setSilenceLevel()
So for example, you set the silence level to 0 and the timeout to 99999 or something. This way flash will no longer automatically shut off the microphone when there is no or low sound.

bytearray from a webcam

i try to get a bytearray from a webcam. i can get the image of the webcam, copy it to bitmapdata and get the bytearray. but is it possible to get the bytearray from the image and sound from the client's webcam ?
audio data to ByteArray:
* Microphone.sampleData Event
* SoundMixer.computeSpectrum() method
i know only the following way to get a ByteArray from Video instance:
* to draw a BitmapData each time the screen is updated and then BitmapData.getPixels(new Rectangle(0, 0, BitmapData.width, BitmapData.height))
it's also possible to join ByteArrays with video and audio data but it depends on how it'll be treated on the server side
if i had the rep, i'd thumbs up www0z0k's reply... should help me with my own app.
Mel, however you're planning on processing the ByteArray on the server (i'd assume something with NetConnection.send()), i'd be sure to ByteArray.inflate()/ByteArray.deflate() your data transfer.