public final class Broadcaster extends Object
KITKAT
, Android 4.4.
An app that uses this class must have at least the following Android permissions:
CAMERA
,
RECORD_AUDIO
and
INTERNET
.
ACCESS_NETWORK_STATE
and
WAKE_LOCK
are recommended. If available, the library will automatically acquire
WifiLock
s when appropriate, and will check network
type when setResolution(width, height)
is used.
If the app wants to support broadcasting using bluetooth headsets,
the following permissions should also be declared in the manifest:
BLUETOOTH
,
MODIFY_AUDIO_SETTINGS
and
BROADCAST_STICKY
.
By default, the Broadcaster does not apply any rotation on the cameras. This implies the
traditional landscape orientation on most devices, and the easiest way to get started is to
lock the parent Activity screenOrientation
to
landscape. Support for other orientations, and cameras on some modern devices, require that
the Activity supplies the proper rotation value through setRotation(displayRotation)
. Read the method documentation carefully.
We recommend that the activity handles at least the "keyboard"
configChanges
, to avoid disconnection when the user
expands a physical keyboard.
Modifier and Type | Class and Description |
---|---|
static class |
Broadcaster.AudioSetting |
class |
Broadcaster.Camera
Basic information describing an available camera.
|
static interface |
Broadcaster.LocalMediaObserver
An Observer interface that should be passed to
storeLocalMedia(path, observer) to receive callbacks while
recording a local copy of a broadcast. |
static interface |
Broadcaster.Observer
An Observer interface that must be implemented for you to receive callbacks about important
state changes and events within the
Broadcaster . |
static interface |
Broadcaster.PictureObserver
A callback interface that can be passed to
takePicture(file, resolution, observer) |
static interface |
Broadcaster.TalkbackObserver
An Observer interface that should be passed to
setTalkbackObserver(observer) to receive callbacks about
talkback stream state changes. |
static interface |
Broadcaster.UplinkSpeedObserver
An interface that can optionally be used with
setUplinkSpeedObserver(observer) to receive callbacks about uplink speed tests which the
Broadcaster can do while not broadcasting. |
static interface |
Broadcaster.ViewerCountObserver
An interface that should be passed to
setViewerCountObserver(ViewerCountObserver) to receive callbacks about
the current and total number of viewers of the broadcast. |
Constructor and Description |
---|
Broadcaster(Activity activity,
String applicationId,
Broadcaster.Observer observer)
Keep only one instance of this class at a time, as camera hardware must be accessed exclusively.
|
Modifier and Type | Method and Description |
---|---|
void |
acceptTalkback(int id)
Call this method to accept an incoming talkback request
when the
onTalkbackStateChanged(state, id, caller, request) state is TalkbackState.NEEDS_ACCEPT . |
boolean |
canDisableShutterSound()
Determines whether the device supports disabling camera shutter sound.
|
boolean |
canStartBroadcasting()
This method can be called at any time to check whether the Broadcaster is idle and ready
to start broadcasting.
|
boolean |
canSwitchCameraWithoutResolutionChange()
Determines whether it is possible to switch camera without causing a resolution change
with the currently applied capture rotation.
|
boolean |
canSwitchCameraWithoutResolutionChange(String nextCameraId) |
void |
focus()
This method toggles focus mode on cameras that support focusing.
|
int |
getCameraCount()
Query the number of available cameras on the current device.
|
String |
getCameraId()
Get the ID of the currently selected camera.
|
int |
getCaptureRotation()
Get the currently active capture rotation value.
|
int |
getPreviewRotation()
Get the currently active preview rotation value.
|
Location |
getRecentPosition() |
Resolution |
getResolution()
Get the current selected camera resolution.
|
List<Broadcaster.Camera> |
getSupportedCameras()
Returns a list of supported cameras on the current device.
|
List<Resolution> |
getSupportedPictureResolutions()
Retrieve a list of still picture resolutions that the current camera supports.
|
List<Resolution> |
getSupportedResolutions()
Retrieve a list of video resolutions that the current camera on this device supports.
|
boolean |
getUplinkRecommendation()
Get whether the
Broadcaster recommends broadcasting or not, based on the latest
uplink bandwidth estimate. |
long |
getUplinkSpeed()
Get the latest known uplink bandwidth estimate.
|
int |
getZoom()
Get the currently active zoom.
|
List<Integer> |
getZoomRatios()
Get the zoom ratios supported by the current camera at the current resolution.
|
boolean |
hasFocus()
Check whether the current camera supports focusing.
|
boolean |
hasLocalMediaCapability()
Check if the current device is capable of writing a local media file
while broadcasting.
|
boolean |
hasTalkbackCapability()
Check if the current device is capable of receiving talkback streams
while broadcasting.
|
boolean |
hasTorch()
Check whether the current camera has a led torch that can be used for continuous lighting.
|
boolean |
hasZoom()
Check whether the current camera supports zooming.
|
void |
onActivityDestroy()
This must be called from the
onDestroy() method to
release all resources properly. |
void |
onActivityPause()
This must be called from the
onPause() method to stop
internal threads and release hardware resources such as the camera and microphone. |
void |
onActivityResume()
This must be called from the
onResume() method to start
internal threads and restart camera preview. |
void |
setAudioQuality(Broadcaster.AudioSetting value)
Set the audio quality to use.
|
void |
setAuthor(String author)
Set the author tag to send along with the broadcast.
|
void |
setCameraId(String id)
Set which camera the Broadcaster should activate.
|
void |
setCameraSurface(SurfaceView surface)
Set the camera surface.
|
void |
setCaptureSounds(boolean value)
Enable or disable camera capture sounds, i.e.
|
void |
setCustomData(String data)
Set custom data to send along with the broadcast.
|
void |
setMaxLiveResolution(int maxWidth,
int maxHeight)
Set the maximum allowed resolution for the live broadcast video.
|
void |
setResolution(int maxWidth,
int maxHeight)
Set the desired camera video resolution.
|
void |
setRotation(int displayRotation)
Invoke this method to rotate the camera preview, video and pictures according to the UI
orientation desired by the app.
|
void |
setRotation(int previewRotation,
int captureRotation)
Invoke this method to rotate the camera preview, video and pictures according to the app UI orientation.
|
void |
setRotation(int previewRotation,
int captureRotation,
int aspectWidth,
int aspectHeight)
Advanced rotation setter with support for cropping captured video to a desired aspect ratio.
|
void |
setSaveOnServer(boolean value)
Sets whether the broadcast is stored on Bambuser's servers or not.
|
void |
setSendPosition(boolean value)
Enable GPS and network based positioning, provided that the device supports it.
|
void |
setTalkbackMixin(boolean enabled)
Set whether received talkback audio should be mixed into the broadcast audio signal.
|
void |
setTalkbackObserver(Broadcaster.TalkbackObserver obs)
Enable talkback functionality in your app by providing a
Broadcaster.TalkbackObserver implementation. |
void |
setTitle(String title)
Set the title to use for the broadcast.
|
void |
setUplinkSpeedObserver(Broadcaster.UplinkSpeedObserver obs)
Set an uplink speed test observer.
|
void |
setViewerCountObserver(Broadcaster.ViewerCountObserver observer)
Get the number of viewers into your app by implementing a
Broadcaster.ViewerCountObserver . |
void |
setZoom(int zoom)
Asynchronously sets the desired zoom.
|
void |
startBroadcast()
Start broadcasting to Bambuser's servers using your secret application ID (provided through
Broadcaster(Activity, String, Observer) ). |
void |
startUplinkTest()
This method can be used to manually trigger a new uplink speed measurement, which is useful
for example if the last
onUplinkTestComplete(bitrate, recommendation) callback occurred a long time ago. |
void |
stopBroadcast() |
void |
stopTalkback()
Optionally call this at any time to stop a pending/ongoing talkback stream.
|
boolean |
storeLocalMedia(File mp4file,
Broadcaster.LocalMediaObserver observer)
Enable writing a local copy of a broadcast to the specified file.
|
void |
switchCamera()
Switch to using the next camera.
|
void |
takePicture(File file,
Resolution resolution,
Broadcaster.PictureObserver observer)
Take a picture, which will be stored as jpeg data in the provided File object.
|
void |
toggleTorch()
Toggle the torch on/off
|
public Broadcaster(Activity activity, String applicationId, Broadcaster.Observer observer)
We recommend that you construct a Broadcaster object in the onCreate()
stage of your Activity life-cycle, but it can also be done later. Make sure to forward all
onPause()
, onResume()
and
onDestroy()
calls to onActivityPause()
,
onActivityResume()
and onActivityDestroy()
.
setRotation(displayRotation)
. The library will not keep
any settings persistently.
setCameraSurface(SurfaceView)
to start the camera.
Broadcaster.Observer.onResolutionsScanned()
to know when camera information is
loaded and the Broadcaster is ready to start broadcasting.
setResolution(int, int)
.
startBroadcast()
to start broadcasting.
activity
- The parent activity, whose Display.getRotation()
value
should be passed to setRotation(displayRotation)
.applicationId
- Secret Bambuser-provided application specific ID.observer
- An implementation of Broadcaster.Observer
that will receive callbacks.public void setCameraSurface(SurfaceView surface)
onResume()
in your Activity, as the camera may be unavailable
before that.
If the SurfaceView
is an instance of SurfaceViewWithAutoAR
,
the camera will set the proper aspect ratio to the surface view and will start when the
changes have applied. Make sure that a SurfaceViewWithAutoAR
can resize itself
within the parent layout (use WRAP_CONTENT
), otherwise
the camera will not start. If you use a generic SurfaceView
, you need to manage
its aspect ratio manually.surface
- A SurfaceViewWithAutoAR
or generic SurfaceView
that has
been added to your UI layout.public void setUplinkSpeedObserver(Broadcaster.UplinkSpeedObserver obs)
Broadcaster(activity, applicationId, observer)
), the observer will receive results from
automatic uplink speed tests every time the Broadcaster instance prepares itself for
broadcasting. The speed test results can be used to decide whether the end user should be
allowed to broadcast or not.
Additional speed tests can be triggered manually through startUplinkTest()
.
Note: The Broadcaster library component itself will allow broadcasting regardless of the speed test results. The speed test results are only offered as guidance.
obs
- An instance of Broadcaster.UplinkSpeedObserver
, or null to disable automatic
uplink speed tests.public long getUplinkSpeed()
onUplinkTestComplete(bitrate, recommendation)
callback), but the accuracy of the result
improves until the measurement is complete.public boolean getUplinkRecommendation()
Broadcaster
recommends broadcasting or not, based on the latest
uplink bandwidth estimate. This method can be used also while a speed test is running (if you
don't have time to wait for the onUplinkTestComplete(bitrate, recommendation)
callback), but the accuracy of the result
improves until the measurement is complete.public void startUplinkTest()
onUplinkTestComplete(bitrate, recommendation)
callback occurred a long time ago.
Invoking this method will abort any ongoing test and start a new test.
This method does nothing if broadcasting is currently in progress, or if the
Broadcaster instance has just been constructed. Please use
setUplinkSpeedObserver(UplinkSpeedObserver)
first, to get notified when tests are
complete. While broadcasting, you may instead rely on
Broadcaster.Observer.onStreamHealthUpdate(int)
.
public void setTitle(String title)
startBroadcast()
, and can also be updated later during the broadcast.title
- Free-text broadcast titlepublic void setAuthor(String author)
startBroadcast()
, and can also be updated later during the broadcast.author
- Broadcast author stringpublic void setCustomData(String data)
startBroadcast()
,
and can also be updated later during the broadcast.data
- Custom data associated with broadcast.public List<Resolution> getSupportedResolutions()
onResolutionsScanned()
callback and then call
setResolution(width, height)
.Resolution
objects, sorted ascending. The list of
supported resolutions can change when switching cameras. Returns an empty list until the first
onResolutionsScanned()
callback.public List<Resolution> getSupportedPictureResolutions()
takePicture(file, resolution, observer)
.Resolution
objects, sorted ascending. The list of
supported resolutions can change when switching cameras. Returns an empty list until the first
onResolutionsScanned()
callback.public Resolution getResolution()
Resolution
object or null. When a camera is active, the returned value is
the actual camera video resolution, no rotation applied. Before a camera is started, this
returns the latest value set through setResolution(width, height)
.public void setResolution(int maxWidth, int maxHeight)
onResolutionsScanned()
callback, which is called
every time a camera starts. See getSupportedResolutions()
for a list of resolutions
supported by the chosen camera.
Don't swap the width and height values, any rotation is taken care of internally when
using the setRotation()
methods.
The live broadcast resolution is always automatically adjusted, up to and including the
chosen camera resolution, depending on current network conditions. Any stored local copy (see
storeLocalMedia(File, LocalMediaObserver)
) will use the chosen camera resolution.
Use maxWidth 0 and maxHeight 0 to use the default video resolution for the current camera. The default is generally 1280x720, but may be lower on older devices. If you set a resolution that isn't supported by the camera on the current device, the nearest supported lower resolution is chosen.
This may be called also while broadcasting but may result in a new broadcast on the servers. This setting is locked while local recording is in progress.
If the app has the ACCESS_NETWORK_STATE
permission,
automatic resolution switching will read the network type to quickly select a high resolution
when appropriate.
maxWidth
- Camera resolution width in pixelsmaxHeight
- Camera resolution height in pixelspublic void setMaxLiveResolution(int maxWidth, int maxHeight)
The live broadcast resolution will not be higher than the set limit, not higher
than the actual camera video resolution set through setResolution(int, int)
and will
still be automatically adjusted depending on current network conditions. This method only
sets an upper bound.
This should preferably be set before starting broadcasting. It may be called also while broadcasting but may result in a new broadcast on the servers.
Accepted values range from 320x240 to 1280x720. The default is currently 1280x720.
Don't swap the width and height values. Any rotation is taken care of internally when using
the setRotation()
methods.
maxWidth
- Allowed live video width in pixelsmaxHeight
- Allowed live video height in pixelspublic void setRotation(int displayRotation)
setRotation(previewRotation, captureRotation)
.displayRotation
- The current app UI rotation, see Display.getRotation()
.public void setRotation(int previewRotation, int captureRotation)
By default, the Broadcaster does not apply any rotation on the cameras, because it doesn't know the app orientation. Camera default is usually landscape orientation. Modern devices may have cameras mounted differently, and are only rotated correctly when this method is used.
Use Display.getRotation()
to get the current display rotation
constant suitable for preview rotation. You can set up an OrientationEventListener
and use setRotation(previewRotation, captureRotation)
when the value from Display.getRotation()
changes.
Captured video and pictures can be rotated independently of the preview rotation. This can be useful if the Activity is locked permanently to one orientation and manually detects how the device is held.
This may be called also while broadcasting but will result in a new broadcast on the servers if the output aspect ratio changes. Switching between opposite rotations (landscape to reverse landscape, or portrait to upside down portrait) will not interrupt the broadcast.
This setting is locked while local recording is in progress. If your app UI can rotate to
any orientation and you use storeLocalMedia(File, LocalMediaObserver)
, you should
lock the orientation before starting a broadcast, and can unlock it again after stopping.
previewRotation
- The current app UI rotation, see Display.getRotation()
.captureRotation
- One of the Display.getRotation()
constants. Should
usually be the same as previewRotation.public void setRotation(int previewRotation, int captureRotation, int aspectWidth, int aspectHeight)
See setRotation(preview, capture)
for documentation of the rotation parameters.
Note: The preview surface view will normally show the full input camera picture, even if
cropping is applied to the broadcast stream. If a custom aspect ratio is requested,
make sure the parent layout of the surface view has got an aspect ratio similar to what is
requested, and set the cropToParent
attribute to true on the SurfaceViewWithAutoAR
view (if using that class). An example
of how to crop the preview to a square:
<RelativeLayout android:layout_width="250dp" android:layout_height="250dp" android:layout_centerInParent="true"> <com.bambuser.broadcaster.SurfaceViewWithAutoAR android:id="@+id/PreviewSurfaceView" android:layout_height="wrap_content" android:layout_width="wrap_content" android:layout_centerInParent="true" app:cropToParent="true" /> </RelativeLayout>
If not using a hardcoded width/height as in this simplified example, the app needs to
take care of the layout of this container to keep the desired aspect ratio - e.g. by
subclassing the RelativeLayout
class and overriding the
View.onMeasure(int, int)
method.
previewRotation
- The current app UI rotation, see Display.getRotation()
.captureRotation
- One of the Display.getRotation()
constants. Should
usually be the same as previewRotation.aspectWidth
- Crop the rotated captured video to provided aspect ratio.
Set to 0 for no cropping.aspectHeight
- Crop the rotated captured video to provided aspect ratio.
Set to 0 for no cropping.public int getPreviewRotation()
setRotation(previewRotation, captureRotation)
had effect, and helps you decide how you should lock your UI when starting a broadcast.public int getCaptureRotation()
setRotation(previewRotation, captureRotation)
had effect, and determines how a broadcast will turn out.public boolean hasLocalMediaCapability()
storeLocalMedia(File, LocalMediaObserver)
does nothing.
Writing a local media file of the broadcast is only supported on modern devices known to have working hardware accelerated video encoding. If you have a new model where you need this feature, contact info@bambuser.com and we'll investigate.
public boolean storeLocalMedia(File mp4file, Broadcaster.LocalMediaObserver observer)
The recorded video resolution is equal to the chosen camera resolution, see
setResolution(width, height)
and
getSupportedResolutions()
, and is full frame rate regardless of the live stream
network conditions.
This method should be invoked before each broadcast that should be recorded.
Invoke hasLocalMediaCapability()
to determine if this is
available on the current device.
mp4file
- A new file to which the Broadcaster
should
write data. Any existing data in the file will be overwritten.observer
- A Broadcaster.LocalMediaObserver
which will be invoked
while writing a local media file.public boolean hasTalkbackCapability()
setTalkbackObserver(TalkbackObserver)
does nothing.public void setTalkbackObserver(Broadcaster.TalkbackObserver obs)
Broadcaster.TalkbackObserver
implementation.
This can not be changed while broadcasting.
obs
- An observer object enabling talkback functionality,
or null to disable talkback functionality.public void setTalkbackMixin(boolean enabled)
This setting can not be changed while broadcasting.
enabled
- True to enable talkback mix-in. False to only play talkback audio locally.public void setViewerCountObserver(Broadcaster.ViewerCountObserver observer)
Broadcaster.ViewerCountObserver
.observer
- An observer object enabling viewer count functionality.public void acceptTalkback(int id)
onTalkbackStateChanged(state, id, caller, request)
state is TalkbackState.NEEDS_ACCEPT
.
This only has effect while an incoming talkback request is pending.
id
- The id as received from onTalkbackStateChanged(state, id, caller, request)
, to verify which talkback stream you are
accepting.public void stopTalkback()
TalkbackState.IDLE
Talkback streams are automatically stopped when broadcasting stops.public void switchCamera()
This may be called also while broadcasting, but if the resolution changes,
it can result in a new broadcast on the servers. If you want to allow camera
switching while broadcasting, as long as it doesn't cause a new broadcast, use
canSwitchCameraWithoutResolutionChange()
.
If camera change requires a resolution change (or rotation change) and local recording is in progress, it is not possible to switch camera. In that case, this method will do nothing.
public int getCameraCount()
public List<Broadcaster.Camera> getSupportedCameras()
setCameraId(String)
.public String getCameraId()
public void setCameraId(String id)
id
- Id for the desired camera.public boolean canSwitchCameraWithoutResolutionChange()
switchCamera()
while broadcasting.
You can rely on the first Broadcaster.Observer.onCameraPreviewStateChanged()
callback to know when
it is meaningful to invoke this method.public boolean canSwitchCameraWithoutResolutionChange(String nextCameraId)
public void setAudioQuality(Broadcaster.AudioSetting value)
HIGH_QUALITY
.
We recommend that this is set before startBroadcast()
,
but it may also be changed while broadcasting.
This setting is locked while local recording is in progress.
value
- One of the Broadcaster.AudioSetting
valuespublic boolean canDisableShutterSound()
Will always return false for a newly constructed Broadcaster, until camera information
has been loaded. You can rely on the first Broadcaster.Observer.onCameraPreviewStateChanged()
callback to know when it is meaningful to invoke this method.
public void setCaptureSounds(boolean value)
Android guidelines strongly recommend playing capture sounds. If you disable the default capture sounds, it is your responsibility to provide alternatives and comply with any laws in countries where capture sounds are required.
Some devices do not support disabling the shutter sound, this can be
determined with canDisableShutterSound()
.
value
- True if capture sounds should be enabled, false otherwise.public void setSendPosition(boolean value)
ACCESS_COARSE_LOCATION
or ACCESS_FINE_LOCATION
.
This setting can be changed at any time. The setting will not apply retroactively for an ongoing broadcast (if a position is sent to the servers, it will be available there until updated or manually removed).
value
- True if geo-positioning should be enabled, false otherwise.public Location getRecentPosition()
Location
, when geotagging has been enabled using
setSendPosition(true)
. May be null if position
has not been determined yet or if positioning is disabled/unavailable in the device.public void setSaveOnServer(boolean value)
This setting can not be changed while broadcasting.
value
- True if broadcasts should be stored, false otherwise.public boolean hasFocus()
public void focus()
public boolean hasTorch()
public void toggleTorch()
public boolean hasZoom()
setZoom(int)
, getZoom()
and getZoomRatios()
.public int getZoom()
hasZoom()
and setZoom(int)
.getZoomRatios()
.public List<Integer> getZoomRatios()
hasZoom()
, getZoom()
and setZoom(int)
.public void setZoom(int zoom)
zoom
- A value in the range of getZoomRatios()
. If the
provided value is not found in the list, it may be rounded down.public void onActivityPause()
onPause()
method to stop
internal threads and release hardware resources such as the camera and microphone.public void onActivityResume()
onResume()
method to start
internal threads and restart camera preview.public void onActivityDestroy()
onDestroy()
method to
release all resources properly.public boolean canStartBroadcasting()
startBroadcast()
will do nothing while this method returns false.public void startBroadcast()
Broadcaster(Activity, String, Observer)
). This method will select a server location
according to your customer settings, possibly taking the end-user's location in consideration.
We recommend that you apply all settings (setAudioQuality(AudioSetting)
,
setAuthor(String)
, setResolution(int, int)
,
storeLocalMedia(File, LocalMediaObserver)
and more) before starting broadcasting.
Requires CAMERA
and
RECORD_AUDIO
permissions.
public void stopBroadcast()
public void takePicture(File file, Resolution resolution, Broadcaster.PictureObserver observer)
getSupportedPictureResolutions()
, up
to the desired resolution. While broadcasting live, the resolution of the taken picture will
be equal to the active camera resolution, see getResolution()
.
Taken pictures are not affected by cropping applied through setRotation(previewRotation, captureRotation, aspectWidth, aspectHeight)
.
Requires CAMERA
permission.
file
- The File
where the jpeg data will be stored.resolution
- An optional Resolution
specifying the upper limit for pictures
taken while not broadcasting. See getSupportedPictureResolutions()
.
The Broadcaster
will default to a resolution close to 1920x1080.observer
- An optional Broadcaster.PictureObserver
.