Package io.antmedia.datastore.db
Class DataStore
java.lang.Object
io.antmedia.datastore.db.DataStore
- Direct Known Subclasses:
InMemoryDataStore,MapBasedDataStore,MongoStore
-
Field Summary
FieldsModifier and TypeFieldDescriptionprotected AppSettingsWe have appSettings fields because we need to refect the changes on the flyprotected booleanprivate longprotected static org.slf4j.Loggerstatic final intprivate static final intprivate static final intprivate static final intstatic final Stringstatic final longprivate longprotected intprotected long -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionprotected abstract booleanabstract booleanaddEndpoint(String id, Endpoint endpoint) voidaddQueryTime(long queryTimeNanoSec) Add query time in nanosecondsabstract booleanaddSubscriber(String streamId, Subscriber subscriber) adds subscriber to the datastore for this streambooleanaddSubscriberConnectionEvent(String streamId, String subscriberId, ConnectionEvent event) sets the connection status of the subscriber in the datastoreabstract StringAdd VoD record to the datastoreabstract booleanblockSubscriber(String streamId, String subscriberId, String blockedType, int seconds) blocks subscribe from playing or publishingabstract voidclearStreamInfoList(String streamId) Remove the stream info list from dbabstract voidclose(boolean deleteDB) Closes the databaseprivate booleancontainsIgnoreCase(String field, String searchLower) abstract booleanCreates new P2PConnectionabstract booleanabstract booleandeleteP2PConnection(String streamId) Deletes a P2PConnectionabstract booleandeleteSubscriber(String streamId, String subscriberId) deletes subscriber from the datastore for this streamabstract booleandeleteToken(String tokenId) Delete specific tokenabstract booleanabstract intfetchUserVodList(File filedir) Gets the video files under thefileDirdirectory parameter and saves them to the datastore as USER_VOD inVodclassabstract BroadcastReturn the broadcast in data storeabstract longReturn the number of active broadcasts in the serverlonggetActiveBroadcastCount(Map<String, String> broadcastMap, com.google.gson.Gson gson, String hostAddress) getActiveBroadcastList(Map<String, String> broadcastMap, com.google.gson.Gson gson, String hostAddress) getActiveSubtracks(String mainTrackId, int offset, int size, String role) Get active subtracks with pagination.getActiveSubtracks(String mainTrackId, String role) Get of active subtracks.abstract longgetActiveSubtracksCount(String mainTrackId, String role) Get the count of active subtracks.longCalculate total query time in millisecondslonggetBroadcastCount(Map<String, String> broadcastMap) getBroadcastList(int offset, int size, String type, String sortBy, String orderBy, String search) Returns the Broadcast List in orderabstract longgetConnectedSubscriberCount(String streamId) Returns the number of the subscribers of requested streamabstract List<Subscriber>getConnectedSubscribers(String streamId, int offset, int size) Lists connected subscribers of requested streamprotected static List<ConnectionEvent>getConnectionEventListFromCollection(Collection<ConnectionEvent> values, String streamId) Simple converter from Collection to Listabstract List<ConnectionEvent>getConnectionEvents(String streamId, String subscriberId, int offset, int size) Get connection events for a specific streamId and subscriberId ConnectionEvents are recorded ifAppSettings.isWriteSubscriberEventsToDatastore()is truelongGet executed query count.abstract longgetLocalLiveBroadcastCount(String hostAddress) getLocalLiveBroadcasts(String hostAddress) abstract P2PConnectiongetP2PConnection(String streamId) Get the P2PConnection by streamIdabstract longgetPartialBroadcastNumber(String search) Returns the number of broadcasts which contains searched stringabstract longgetPartialVodNumber(String search) Returns the number of vods which contains searched stringabstract List<StreamInfo>getStreamInfoList(String streamId) Returns stream info list added to dbabstract SubscribergetSubscriber(String streamId, String subscriberId) gets subscriber from the datastoregetSubscriber(Map<String, String> subscriberMap, String streamId, String subscriberId, com.google.gson.Gson gson) abstract SubscriberMetadatagetSubscriberMetaData(String subscriberId) Get subscriber metadataabstract longgetSubtrackCount(String mainTrackId, String role, String status) Get the count of subtracksgetSubtracks(String mainTrackId, int offset, int size, String role) Get the subtracks of the main trackgetSubtracks(String mainTrackId, int offset, int size, String role, String status, String sortBy, String orderBy, String search) Get the subtracks of the main trackabstract Tokenretrieve specific tokenabstract longlonggetTotalBroadcastNumber(Map<String, String> broadcastMap) abstract longlonggetTotalVodNumber(Map<String, String> broadcastMap) abstract intThis is used to get total number of WebRTC viewersintgetTotalWebRTCViewersCount(Map<String, String> broadcastMap, com.google.gson.Gson gson) abstract VoDReturn the vod by idgetVodList(int offset, int size, String sortBy, String orderBy, String filterStreamId, String search) Returns the VoD List in ordergetVodListV2(Map<String, String> vodMap, String streamId, String search, com.google.gson.Gson gson, String dbName) protected booleanhandleConnectionEvent(String streamId, String subscriberId, ConnectionEvent event) abstract booleanhasSubtracks(String streamId) booleanReturn if data store is available.booleanisSubscriberConnected(String streamId, String subscriberId) gets the connection status of the subscriber from the datastoreabstract List<Subscriber>listAllSubscribers(String streamId, int offset, int size) Lists all subscribers of requested streamlistAllSubscribers(Map<String, String> subscriberMap, String streamId, int offset, int size, com.google.gson.Gson gson, boolean connectedOnly) listAllTokens(String streamId, int offset, int size) Lists all tokens of requested streamlistAllTokens(Map<String, String> tokenMap, String streamId, int offset, int size, com.google.gson.Gson gson) private booleanmatchesVodSearch(VoD item, String searchLower) abstract voidputSubscriberMetaData(String subscriberId, SubscriberMetadata metadata) Put subscriber metadata.abstract booleanabstract booleanremoveEndpoint(String id, Endpoint endpoint, boolean checkRTMPUrl) abstract intresetBroadcasts(String hostAddress) Resets the broadcasts in the database.abstract booleansets the connection status of all the subscribers false in the datastore called after an ungraceful shutdownabstract booleanrevokeSubscribers(String streamId) deletes all subscriber from the datastore for this streamabstract booleanrevokeTokens(String streamId) Delete all tokens of the streamabstract Stringprotected BroadcastsaveBroadcast(Broadcast broadcast) abstract voidsaveStreamInfo(StreamInfo streamInfo) Saves the stream info to the dbabstract booleansaves token to storesearchOnServer(List<Broadcast> broadcastList, String search) searchOnServerVod(ArrayList<VoD> broadcastList, String search) Below search methods and sortandcrop methods are used for getting the searched items and sorting and pagination.voidsetAppSettings(AppSettings appSettings) Setter for appSettingsabstract booleansetMp4Muxing(String streamId, int enabled) enables or disables mp4 muxing for the streamabstract booleansetWebMMuxing(String streamId, int enabled) enables or disables WebM muxing for the streamvoidshowWarningIfElapsedTimeIsMoreThanThreshold(long elapsedNano, String methodName) sortAndCropBroadcastList(List<Broadcast> broadcastList, int offset, int size, String sortBy, String orderBy) sortAndCropVodList(List<VoD> vodList, int offset, int size, String sortBy, String orderBy) abstract booleanupdateBroadcastFields(String streamId, BroadcastUpdate broadcast) Updates the Broadcast objects fields if it's not null.booleanupdateDASHViewerCount(String streamId, int diffCount) Add or subtract the DASH viewer count from current valueprotected abstract booleanupdateDASHViewerCountLocal(String streamId, int diffCount) booleanupdateHLSViewerCount(String streamId, int diffCount) Add or subtract the HLS viewer count from current valueprotected abstract booleanupdateHLSViewerCountLocal(String streamId, int diffCount) booleanupdateRtmpViewerCount(String streamId, boolean increment) Update the RTMP viewer countprotected abstract booleanupdateRtmpViewerCountLocal(String streamId, boolean increment) abstract booleanupdateStatus(String id, String status) protected voidupdateStreamInfo(Broadcast broadcast, BroadcastUpdate newBroadcast) Updates the stream fields if it's not nullabstract booleanupdateStreamMetaData(String streamId, String metaData) This is used to update meta data for a bradcastbooleanupdateSubscriberBitrateEvent(String streamId, String subscriberId, long avgVideoBitrate, long avgAudioBitrate) sets the avarage bitrate of the subscriber in the datastoreabstract booleanupdateVoDProcessStatus(String id, String status) booleanupdateWebRTCViewerCount(String streamId, boolean increment) Update the WebRTC viewer countprotected abstract booleanupdateWebRTCViewerCountLocal(String streamId, boolean increment) abstract TokenvalidateToken(Token token) Validates token
-
Field Details
-
QUERY_TIME_THRESHOLD_MS_SEC
private static final int QUERY_TIME_THRESHOLD_MS_SEC- See Also:
-
QUERY_TIME_THRESHOLD_NANO_SEC
private static final int QUERY_TIME_THRESHOLD_NANO_SEC- See Also:
-
QUERY_TIME_EXTRA_LOG_THRESHOLD_NANO_SEC
private static final int QUERY_TIME_EXTRA_LOG_THRESHOLD_NANO_SEC- See Also:
-
MAX_ITEM_IN_ONE_LIST
public static final int MAX_ITEM_IN_ONE_LIST- See Also:
-
REPLACE_CHARS_REGEX
- See Also:
-
executedQueryCount
private long executedQueryCount -
totalQueryTimeNanoSec
private long totalQueryTimeNanoSec -
available
protected volatile boolean available -
logger
protected static org.slf4j.Logger logger -
appSettings
We have appSettings fields because we need to refect the changes on the fly -
TOTAL_WEBRTC_VIEWER_COUNT_CACHE_TIME
public static final long TOTAL_WEBRTC_VIEWER_COUNT_CACHE_TIME- See Also:
-
totalWebRTCViewerCount
protected int totalWebRTCViewerCount -
totalWebRTCViewerCountLastUpdateTime
protected long totalWebRTCViewerCountLastUpdateTime
-
-
Constructor Details
-
DataStore
public DataStore()
-
-
Method Details
-
save
-
saveBroadcast
-
get
Return the broadcast in data store- Parameters:
id-- Returns:
- broadcast
-
get
-
getVoD
Return the vod by id- Parameters:
id-- Returns:
- Vod object
-
getVoD
-
updateStatus
-
getPartialVodNumber
Returns the number of vods which contains searched string- Parameters:
search- is used for searching in vodIds and names of the vods- Returns:
-
getPartialBroadcastNumber
Returns the number of broadcasts which contains searched string- Parameters:
search- is used for searching in streamIds and names of the stream- Returns:
-
addEndpoint
-
addVod
Add VoD record to the datastore- Parameters:
vod-- Returns:
- the id of the VoD if it's successful or it returns null if it's failed
-
getBroadcastCount
-
delete
-
deleteVod
-
updateVoDProcessStatus
-
getBroadcastList
public abstract List<Broadcast> getBroadcastList(int offset, int size, String type, String sortBy, String orderBy, String search) Returns the Broadcast List in order- Parameters:
offset- the number of items to skipsize- batch sizetype- can get "liveStream" or "ipCamera" or "streamSource" or "VoD" values. Default is getting all broadcast types.sortBy- can get "name" or "date" or "status" valuesorderBy- can get "desc" or "asc"search- is used for searching in streamIds and names of the stream- Returns:
-
removeEndpoint
-
getExternalStreamsList
-
close
public abstract void close(boolean deleteDB) Closes the database- Parameters:
deleteDB- if it's true, it also deletes the db and closes
-
getVodList
public abstract List<VoD> getVodList(int offset, int size, String sortBy, String orderBy, String filterStreamId, String search) Returns the VoD List in order- Parameters:
offset- : the number of items to skipsize- : batch sizesortBy- can get "name" or "date" valuesorderBy- can get "desc" or "asc"filterStreamId- is used for filtering the vod by stream id. If it's null or empty, it's not usedsearch- is used for searching in vodIds and names of the vods.- Returns:
-
getVodListV2
-
removeAllEndpoints
-
getTotalVodNumber
public abstract long getTotalVodNumber() -
getTotalVodNumber
-
getTotalBroadcastNumber
public abstract long getTotalBroadcastNumber() -
getTotalBroadcastNumber
-
saveToken
saves token to store- Parameters:
token- - created token- Returns:
- true/false
-
listAllTokens
Lists all tokens of requested stream- Parameters:
streamId-offset-size-- Returns:
- lists of tokens
-
listAllTokens
-
validateToken
Validates token- Parameters:
token-streamId-- Returns:
- token if validated, null if not
-
revokeTokens
Delete all tokens of the stream- Parameters:
streamId-
-
deleteToken
Delete specific token- Parameters:
tokenId- id of the token
-
getToken
retrieve specific token- Parameters:
tokenId- id of the token
-
getToken
-
listAllSubscribers
Lists all subscribers of requested stream- Parameters:
streamId-offset-size-- Returns:
- lists of subscribers
-
getConnectedSubscriberCount
Returns the number of the subscribers of requested stream- Parameters:
streamId-- Returns:
- number of the subscribers of requested stream
-
getConnectedSubscribers
Lists connected subscribers of requested stream- Parameters:
streamId-offset-size-- Returns:
- lists of subscribers
-
listAllSubscribers
-
addSubscriber
adds subscriber to the datastore for this stream- Parameters:
streamId-subscriber- - subscriber to be added
-
deleteSubscriber
deletes subscriber from the datastore for this stream- Parameters:
streamId-subscriberId- - id of the subsciber to be deleted
-
blockSubscriber
public abstract boolean blockSubscriber(String streamId, String subscriberId, String blockedType, int seconds) blocks subscribe from playing or publishing- Parameters:
streamId-subscriberId- - id of the subsciber to be blockedblockedType- - it can be the value of the static fieldSubscriber.PLAY_TYPE,Subscriber.PUBLISH_TYPE,Subscriber.PUBLISH_AND_PLAY_TYPEpublish, play, publish_playseconds- - duration of seconds to block the user
-
revokeSubscribers
deletes all subscriber from the datastore for this stream- Parameters:
streamId-
-
getSubscriber
gets subscriber from the datastore- Parameters:
streamId-subscriberId- - id of the subsciber to be deleted
-
getSubscriber
-
isSubscriberConnected
gets the connection status of the subscriber from the datastore- Parameters:
streamId-subscriberId- - id of the subscriber
-
addSubscriberConnectionEvent
public boolean addSubscriberConnectionEvent(String streamId, String subscriberId, ConnectionEvent event) sets the connection status of the subscriber in the datastore- Parameters:
streamId-subscriberId- - id of the subscriberevent- - connection event which occured for this subscriber
-
handleConnectionEvent
protected boolean handleConnectionEvent(String streamId, String subscriberId, ConnectionEvent event) -
addConnectionEvent
-
updateSubscriberBitrateEvent
public boolean updateSubscriberBitrateEvent(String streamId, String subscriberId, long avgVideoBitrate, long avgAudioBitrate) sets the avarage bitrate of the subscriber in the datastore- Parameters:
streamId-subscriberId- - id of the subscriberevent- - bitrate measurement event
-
resetSubscribersConnectedStatus
public abstract boolean resetSubscribersConnectedStatus()sets the connection status of all the subscribers false in the datastore called after an ungraceful shutdown -
setMp4Muxing
enables or disables mp4 muxing for the stream- Parameters:
streamId- - id of the streamenabled- 1 means enabled, -1 means disabled, 0 means no setting for the stream
-
setWebMMuxing
enables or disables WebM muxing for the stream- Parameters:
streamId- - id of the streamenabled- 1 means enabled, -1 means disabled, 0 means no setting for the stream
-
fetchUserVodList
Gets the video files under thefileDirdirectory parameter and saves them to the datastore as USER_VOD inVodclass- Parameters:
file-- Returns:
- number of files that are saved to datastore
-
getActiveBroadcastCount
public abstract long getActiveBroadcastCount()Return the number of active broadcasts in the server- Returns:
-
getActiveBroadcastCount
-
getActiveBroadcastList
-
updateBroadcastFields
Updates the Broadcast objects fields if it's not null. The updated fields are as follows name, description, userName, password, IP address, streamUrl- Parameters:
broadcast-- Returns:
-
updateHLSViewerCount
Add or subtract the HLS viewer count from current value- Parameters:
streamId-diffCount-
-
updateHLSViewerCountLocal
-
updateDASHViewerCount
Add or subtract the DASH viewer count from current value- Parameters:
streamId-diffCount-
-
updateDASHViewerCountLocal
-
updateWebRTCViewerCount
Update the WebRTC viewer count- Parameters:
streamId-increment- if it is true, increment viewer count by one if it is false, decrement viewer count by one
-
updateWebRTCViewerCountLocal
-
updateRtmpViewerCount
Update the RTMP viewer count- Parameters:
streamId-increment- if it is true, increment viewer count by one if it is false, decrement viewer count by one
-
updateRtmpViewerCountLocal
-
saveStreamInfo
Saves the stream info to the db- Parameters:
streamInfo-
-
getStreamInfoList
Returns stream info list added to db- Parameters:
streamId-- Returns:
-
clearStreamInfoList
Remove the stream info list from db- Parameters:
streamId-
-
updateStreamInfo
Updates the stream fields if it's not null- Parameters:
broadcast-name-description-userName-password-ipAddr-streamUrl-
-
getLocalLiveBroadcastCount
-
getLocalLiveBroadcasts
-
searchOnServerVod
Below search methods and sortandcrop methods are used for getting the searched items and sorting and pagination. Sorting, search and cropping is available for Broadcasts, VoDs and Conference Rooms. They are used by InMemoryDataStore and MapDBStore, Mongodb implements the same functionality inside its own class. -
matchesVodSearch
-
containsIgnoreCase
-
sortAndCropVodList
-
searchOnServer
-
sortAndCropBroadcastList
-
createP2PConnection
Creates new P2PConnection- Parameters:
conn- - P2PConnection object- Returns:
- boolean - success
-
getP2PConnection
Get the P2PConnection by streamId- Parameters:
streamId- - stream id for P2PConnection- Returns:
- P2PConnection - if exist else null
-
deleteP2PConnection
Deletes a P2PConnection- Parameters:
conn- - P2PConnection object- Returns:
- boolean - success
-
resetBroadcasts
Resets the broadcasts in the database. It sets number of viewers to zero. It also delete the stream if it's zombi stream -
isAvailable
public boolean isAvailable()Return if data store is available. DataStore is available if it's initialized and not closed. It's not available if it's closed.- Returns:
- availability of the datastore
-
getTotalWebRTCViewersCount
public abstract int getTotalWebRTCViewersCount()This is used to get total number of WebRTC viewers -
getTotalWebRTCViewersCount
-
updateStreamMetaData
This is used to update meta data for a bradcast- Parameters:
streamId- id for the broadcastmetaData- new meta data
-
putSubscriberMetaData
Put subscriber metadata. It overwrites the metadata, if you need to update something, first get thegetSubscriberMetaData(String), update it and put it- Parameters:
subscriberId-SubscriberMetadata-
-
getSubscriberMetaData
Get subscriber metadata- Parameters:
subscriberId-- Returns:
-
getSubtracks
public abstract List<Broadcast> getSubtracks(String mainTrackId, int offset, int size, String role, String status, String sortBy, String orderBy, String search) Get the subtracks of the main track- Parameters:
mainTrackId- the main track to get the subtracksoffset- the offset to get the subtrackssize- number of items to getrole- the role of the subtracks for role based streaming especially in conferences. It can be nullstatus- the status of the stream broadcasting, finished etc. It can be nullsortBy- can get "name" or "date" or "status" valuesorderBy- can get "desc" or "asc"search- is used for searching in streamIds and names of the stream- Returns:
-
getSubtracks
Get the subtracks of the main track- Parameters:
mainTrackId- the main track to get the subtracksoffset- the offset to get the subtrackssize- number of items to getrole- the role of the subtracks for role based streaming especially in conferences. It can be null- Returns:
-
getSubtrackCount
Get the count of subtracks- Parameters:
mainTrackId- the main track to get the subtracksrole- the role of the subtracks for role based streaming especially in conferences- Returns:
- number of subtracks
-
getActiveSubtracksCount
Get the count of active subtracks. If subtrack is stucked in broadcasting or preparing, it will not count it.- Parameters:
mainTrackId-role-- Returns:
-
getActiveSubtracks
Get of active subtracks. If subtrack is stucked in broadcasting or preparing, it will not return it. This method is generally not recommended to use because it can be very costly. It's implemented for the poll mechanism in Subtracks and poll mechanismi will be replaced with event mechanism- Parameters:
mainTrackId-role-- Returns:
-
getActiveSubtracks
public abstract List<Broadcast> getActiveSubtracks(@Nonnull String mainTrackId, int offset, int size, String role) Get active subtracks with pagination. If subtrack is stucked in broadcasting or preparing, it will not return it.- Parameters:
mainTrackId- the main track to get the active subtracksoffset- the offset to get the active subtrackssize- number of items to getrole- the role of the subtracks for role based streaming especially in conferences. It can be null- Returns:
- list of active subtracks
-
hasSubtracks
- Parameters:
streamId-- Returns:
- If the stream has subtracks, it return true. If not, it returns false
-
getExecutedQueryCount
public long getExecutedQueryCount()Get executed query count. For now only mongodb queries are counted.- Returns:
- Executed query count.
-
getConnectionEvents
public abstract List<ConnectionEvent> getConnectionEvents(String streamId, @Nullable String subscriberId, int offset, int size) Get connection events for a specific streamId and subscriberId ConnectionEvents are recorded ifAppSettings.isWriteSubscriberEventsToDatastore()is true- Parameters:
streamId-subscriberId-offset-size-- Returns:
-
getConnectionEventListFromCollection
protected static List<ConnectionEvent> getConnectionEventListFromCollection(Collection<ConnectionEvent> values, String streamId) Simple converter from Collection to List- Parameters:
values-- Returns:
-
setAppSettings
Setter for appSettings- Parameters:
appSettings-
-
getAverageQueryTimeMs
public long getAverageQueryTimeMs()Calculate total query time in milliseconds- Returns:
-
addQueryTime
public void addQueryTime(long queryTimeNanoSec) Add query time in nanoseconds- Parameters:
queryTimeNanoSec-
-
showWarningIfElapsedTimeIsMoreThanThreshold
-