Broadcaster API

If you want to build your own custom broadcaster which publishes video to the Justin.tv video system, you can use our broadcaster API to do it. There is a swf that exposes a broadcasting interface (described below) available from http://www.justin.tv/widgets/live_api_publisher.swf which can be loaded using the flash Loader class. If you don't need complete customization, you can also use our broadcaster embed, documentation for which can be found at channel/publisher_embed.

Upcoming Version

Documentation for upcoming changes: Broadcaster SWF API v0.2

Broadcaster extends MovieClip

Methods
new():Broadcaster

Returns new Broadcaster object.

setup(stream_id:String, setupMicrophone:Bool = true, setupCam:Bool = true):void

Sets up broadcaster and gets it ready to broadcast on the stream indicated by stream_id. Enables both microphone and cam by default.

set_quality_kbps(kbps:Int)

Sets the quality (in kbps) to broadcast at. Defaults to 128.

get_quality_kbps(kbps:Int)

Gets the quality (in kbps) broadcasted at.

set_camera_mode(cam_width:Int, cam_height:Int)

Sets the camera capture mode to cam_width by cam_height pixels. Flash default capture size is 160x120.

set_mic_loopback(loopback:Bool)

Set whether or not the sound picked up by the microphone is played back ("looped back") to the user.

setup_camera(cam_id_set:Int, use_default_cam:Bool = false)

Sets up the camera for broadcasting. cam_id_set is an index into the array returned by get_camera_names. Setting use_default_cam to true ignores cam_id_set and uses the default camera, which the user can choose in the Flash settings panel.

setup_mic(mic_id:Int, use_default_mic = false)

Sets up the mic for broadcasting. mic_id is an index into the array returned by get_microphone_names. Setting use_default_mic to true ignores mic_id_set and uses the default microphone, which the user can choose in the Flash settings panel.

set_sound_quality(quality:Int)

Sets the sound quality (in khz) to broadcast at. Valid settings are 5, 8, 11, 22, 44.

get_sound_quality():Int

Gets the sound quality broadcasted at, in khz.

get_volume():Int

Returns a value between 0-100 indicating the loudness of the sound the microphone picks up.

start_broadcast()

Starts broadcasting.

stop_broadcast()

Stops the current broadcast.

get_microphone_names():Array<Dynamic>

Returns an array of strings representing the names of all possible microphone inputs. Set the microphone to use with set_mic_id.

get_microphone_name():String

Returns the name of the currently setup camera, e.g. "Built-in Microphone".

get_camera_names():Array<Dynamic>

Returns an array of strings representing the cameras attached to the computer.

get_camera_name():String

Returns the name of the currently setup audio input/Microphone, e.g. "USB Video Class Video".

mute_mic()

Mutes the microphone currently in use.

unmute_mic()

Unmutes the microphone currently in use, setting its volume to the value it was before the call to mute_mic().

get_capture_height():Int

Returns height of video being captured by camera.

get_capture_width():Int

Returns width of video being captured by camera.

addEventListener("video", callback)

Dispatches video events, as listed below.

show_settings_dialog()

Shows a dialog which allows a user to set their video/audio source and quality.

Events
net_error

Happens when there's an error in connecting to the server.

security_net_error

Happens when there's a security violation error. Make sure you've called flash.Security.allowDomain in your program to prevent this.

connect_success

Raised once the connect is completed successfully.

connect_fail

Happens if an attempt to connect fails.

net_stream

Passes along events from NetStream to the API user. Check the Flash documentation on NetStreamEvent for further details.
You can access the NetStatusEvent object itself by calling .info on the JtvEvent raised.

camera_setup

Dispatched when a camera is successfully selected by the user.

Examples
With API SWF
    import flash.system.Security;
    var broadcaster:Object;
    var broadcasting:Boolean;

    var noise:Sprite;

    //You need to at least allow access to the justin.tv domain
    Security.allowDomain("*");

    //Load the API SWF into your program
    var api_loader:Loader = new Loader();
    var url_request:URLRequest = new URLRequest('http://www.justin.tv/widgets/live_api_publisher.swf');
    api_loader.contentLoaderInfo.addEventListener(Event.COMPLETE, loadingDone);
    api_loader.load (url_request);

    var broadcaster_api:Object;

    //Replace with your streamkey
    var streamkey:String = 'live_###';

    function loadingDone(e:Event) {

        //Add bare-bones api broadcaster to stage
        addChildAt (api_loader.content,0);
        broadcaster_api = Object(api_loader.content).api;

        //Set up camera and microphone to broadcast on streamkey
        broadcaster_api.setup(streamkey);
        broadcaster_api.setup_camera(0,null);
        broadcaster_api.setup_mic(0,null);

        //Set up JtvEvent handling
            broadcaster_api.addEventListener ("net_status",
              function (e:Object)
              {
                  trace ("Found videvent: " + e.event + " info: " + e.info.code);
              } );

        //Add button to start or stop broadcasting
        startbutton.addEventListener (MouseEvent.CLICK, startStopClicked);
        broadcasting = false;

        //Create "noise" object that displays the activity level detected by the microphone
        noise = new Sprite();
        addChild (noise);

        noise.x = 200;
        noise.y = 220;

        var noiseTimer:Timer = new Timer (50);
        noiseTimer.addEventListener(TimerEvent.TIMER, updateNoise);
        noiseTimer.start();
    }

    function startStopClicked (e:MouseEvent) {
        if (broadcasting) {
            //Stops the broadcast
            broadcaster_api.stop_broadcast();
            startbutton.label = "Start";
        }
        else {
            //Starts the broadcast
            broadcaster_api.setup(streamkey);
            broadcaster_api.start_broadcast();
            startbutton.label = "Stop";
        }

        broadcasting = !broadcasting;
    }

    function updateNoise(e:TimerEvent) {
        noise.graphics.clear();
        noise.graphics.beginFill (0xcc0000);

        //Get volume from broadcaster API, on a scale from 0-100
        //adjust to logarithmic scale, then display.
        for (var x = 0; x < Math.log(broadcaster_api.get_volume())/2*5; x++) {
            noise.graphics.drawRect (x*15,0,12,12);
        }
    }