Class DataStore

java.lang.Object
io.antmedia.datastore.db.DataStore
Direct Known Subclasses:
InMemoryDataStore, MapBasedDataStore, MongoStore

public abstract class DataStore extends Object
  • 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:
    • MAX_ITEM_IN_ONE_LIST

      public static final int MAX_ITEM_IN_ONE_LIST
      See Also:
    • REPLACE_CHARS_REGEX

      public static final String 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

      protected AppSettings 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

      public abstract String save(Broadcast broadcast)
    • saveBroadcast

      protected Broadcast saveBroadcast(Broadcast broadcast)
    • get

      public abstract Broadcast get(String id)
      Return the broadcast in data store
      Parameters:
      id -
      Returns:
      broadcast
    • get

      public Broadcast get(Map<String,String> broadcastMap, String streamId, com.google.gson.Gson gson)
    • getVoD

      public abstract VoD getVoD(String id)
      Return the vod by id
      Parameters:
      id -
      Returns:
      Vod object
    • getVoD

      public VoD getVoD(Map<String,String> vodMap, String vodId, com.google.gson.Gson gson)
    • updateStatus

      public abstract boolean updateStatus(String id, String status)
    • getPartialVodNumber

      public abstract long getPartialVodNumber(String search)
      Returns the number of vods which contains searched string
      Parameters:
      search - is used for searching in vodIds and names of the vods
      Returns:
    • getPartialBroadcastNumber

      public abstract long getPartialBroadcastNumber(String search)
      Returns the number of broadcasts which contains searched string
      Parameters:
      search - is used for searching in streamIds and names of the stream
      Returns:
    • addEndpoint

      public abstract boolean addEndpoint(String id, Endpoint endpoint)
    • addVod

      public abstract String addVod(VoD vod)
      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

      @Deprecated public abstract long getBroadcastCount()
      Deprecated.
      Use getTotalBroadcastNumber
    • getBroadcastCount

      public long getBroadcastCount(Map<String,String> broadcastMap)
    • delete

      public abstract boolean delete(String id)
    • deleteVod

      public abstract boolean deleteVod(String id)
    • updateVoDProcessStatus

      public abstract boolean updateVoDProcessStatus(String id, String status)
    • 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 skip
      size - batch size
      type - can get "liveStream" or "ipCamera" or "streamSource" or "VoD" values. Default is getting all broadcast types.
      sortBy - can get "name" or "date" or "status" values
      orderBy - can get "desc" or "asc"
      search - is used for searching in streamIds and names of the stream
      Returns:
    • removeEndpoint

      public abstract boolean removeEndpoint(String id, Endpoint endpoint, boolean checkRTMPUrl)
    • getExternalStreamsList

      public abstract List<Broadcast> 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 skip
      size - : batch size
      sortBy - can get "name" or "date" values
      orderBy - can get "desc" or "asc"
      filterStreamId - is used for filtering the vod by stream id. If it's null or empty, it's not used
      search - is used for searching in vodIds and names of the vods.
      Returns:
    • getVodListV2

      public List<VoD> getVodListV2(Map<String,String> vodMap, String streamId, String search, com.google.gson.Gson gson, String dbName)
    • removeAllEndpoints

      public abstract boolean removeAllEndpoints(String id)
    • getTotalVodNumber

      public abstract long getTotalVodNumber()
    • getTotalVodNumber

      public long getTotalVodNumber(Map<String,String> broadcastMap)
    • getTotalBroadcastNumber

      public abstract long getTotalBroadcastNumber()
    • getTotalBroadcastNumber

      public long getTotalBroadcastNumber(Map<String,String> broadcastMap)
    • saveDetection

      public abstract void saveDetection(String id, long timeElapsed, List<TensorFlowObject> detectedObjects)
    • getDetectionList

      public abstract List<TensorFlowObject> getDetectionList(String idFilter, int offsetSize, int batchSize)
    • getDetectionList

      public List<TensorFlowObject> getDetectionList(Map<String,String> detectionMap, String idFilter, int offsetSize, int batchSize, com.google.gson.Gson gson)
    • getDetection

      public abstract List<TensorFlowObject> getDetection(String id)
    • getDetection

      public List<TensorFlowObject> getDetection(Map<String,String> detectionMap, String id, com.google.gson.Gson gson)
    • saveToken

      public abstract boolean saveToken(Token token)
      saves token to store
      Parameters:
      token - - created token
      Returns:
      true/false
    • listAllTokens

      public abstract List<Token> listAllTokens(String streamId, int offset, int size)
      Lists all tokens of requested stream
      Parameters:
      streamId -
      offset -
      size -
      Returns:
      lists of tokens
    • listAllTokens

      public List<Token> listAllTokens(Map<String,String> tokenMap, String streamId, int offset, int size, com.google.gson.Gson gson)
    • validateToken

      public abstract Token validateToken(Token token)
      Validates token
      Parameters:
      token -
      streamId -
      Returns:
      token if validated, null if not
    • revokeTokens

      public abstract boolean revokeTokens(String streamId)
      Delete all tokens of the stream
      Parameters:
      streamId -
    • deleteToken

      public abstract boolean deleteToken(String tokenId)
      Delete specific token
      Parameters:
      tokenId - id of the token
    • getToken

      public abstract Token getToken(String tokenId)
      retrieve specific token
      Parameters:
      tokenId - id of the token
    • getToken

      public Token getToken(Map<String,String> tokenMap, String tokenId, com.google.gson.Gson gson)
    • listAllSubscribers

      public abstract List<Subscriber> listAllSubscribers(String streamId, int offset, int size)
      Lists all subscribers of requested stream
      Parameters:
      streamId -
      offset -
      size -
      Returns:
      lists of subscribers
    • listAllSubscribers

      public List<Subscriber> listAllSubscribers(Map<String,String> subscriberMap, String streamId, int offset, int size, com.google.gson.Gson gson)
    • listAllSubscriberStats

      public List<SubscriberStats> listAllSubscriberStats(String streamId, int offset, int size)
      Lists all subscriber statistics of requested stream
      Parameters:
      streamId -
      offset -
      size -
      Returns:
      lists of subscriber statistics
    • addSubscriber

      public abstract boolean addSubscriber(String streamId, Subscriber subscriber)
      adds subscriber to the datastore for this stream
      Parameters:
      streamId -
      subscriber - - subscriber to be added
    • deleteSubscriber

      public abstract boolean deleteSubscriber(String streamId, String subscriberId)
      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 blocked
      blockedType - - it can be the value of the static field Subscriber.PLAY_TYPE, Subscriber.PUBLISH_TYPE, Subscriber.PUBLISH_AND_PLAY_TYPE publish, play, publish_play
      seconds - - duration of seconds to block the user
    • revokeSubscribers

      public abstract boolean revokeSubscribers(String streamId)
      deletes all subscriber from the datastore for this stream
      Parameters:
      streamId -
    • getSubscriber

      public abstract Subscriber getSubscriber(String streamId, String subscriberId)
      gets subscriber from the datastore
      Parameters:
      streamId -
      subscriberId - - id of the subsciber to be deleted
    • getSubscriber

      public Subscriber getSubscriber(Map<String,String> subscriberMap, String streamId, String subscriberId, com.google.gson.Gson gson)
    • isSubscriberConnected

      public boolean isSubscriberConnected(String streamId, String subscriberId)
      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 subscriber
      event - - connection event which occured for this subscriber
    • handleConnectionEvent

      protected boolean handleConnectionEvent(String streamId, String subscriberId, ConnectionEvent event)
    • addConnectionEvent

      protected abstract boolean addConnectionEvent(ConnectionEvent event)
    • 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 subscriber
      event - - 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

      public abstract boolean setMp4Muxing(String streamId, int enabled)
      enables or disables mp4 muxing for the stream
      Parameters:
      streamId - - id of the stream
      enabled - 1 means enabled, -1 means disabled, 0 means no setting for the stream
    • setWebMMuxing

      public abstract boolean setWebMMuxing(String streamId, int enabled)
      enables or disables WebM muxing for the stream
      Parameters:
      streamId - - id of the stream
      enabled - 1 means enabled, -1 means disabled, 0 means no setting for the stream
    • fetchUserVodList

      public abstract int fetchUserVodList(File filedir)
      Gets the video files under the fileDir directory parameter and saves them to the datastore as USER_VOD in Vod class
      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

      public long getActiveBroadcastCount(Map<String,String> broadcastMap, com.google.gson.Gson gson, String hostAddress)
    • getActiveBroadcastList

      public List<Broadcast> getActiveBroadcastList(Map<String,String> broadcastMap, com.google.gson.Gson gson, String hostAddress)
    • updateBroadcastFields

      public abstract boolean updateBroadcastFields(String streamId, BroadcastUpdate broadcast)
      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

      public boolean updateHLSViewerCount(String streamId, int diffCount)
      Add or subtract the HLS viewer count from current value
      Parameters:
      streamId -
      diffCount -
    • updateHLSViewerCountLocal

      protected abstract boolean updateHLSViewerCountLocal(String streamId, int diffCount)
    • updateDASHViewerCount

      public boolean updateDASHViewerCount(String streamId, int diffCount)
      Add or subtract the DASH viewer count from current value
      Parameters:
      streamId -
      diffCount -
    • updateDASHViewerCountLocal

      protected abstract boolean updateDASHViewerCountLocal(String streamId, int diffCount)
    • getObjectDetectedTotal

      public abstract long getObjectDetectedTotal(String streamId)
      Returns the total number of detected objects in the stream
      Parameters:
      id - is the stream id
      Returns:
      total number of detected objects
    • getObjectDetectedTotal

      public long getObjectDetectedTotal(Map<String,String> detectionMap, String streamId, com.google.gson.Gson gson)
    • updateWebRTCViewerCount

      public boolean updateWebRTCViewerCount(String streamId, boolean increment)
      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

      protected abstract boolean updateWebRTCViewerCountLocal(String streamId, boolean increment)
    • updateRtmpViewerCount

      public boolean updateRtmpViewerCount(String streamId, boolean increment)
      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

      protected abstract boolean updateRtmpViewerCountLocal(String streamId, boolean increment)
    • saveStreamInfo

      public abstract void saveStreamInfo(StreamInfo streamInfo)
      Saves the stream info to the db
      Parameters:
      streamInfo -
    • getStreamInfoList

      public abstract List<StreamInfo> getStreamInfoList(String streamId)
      Returns stream info list added to db
      Parameters:
      streamId -
      Returns:
    • clearStreamInfoList

      public abstract void clearStreamInfoList(String streamId)
      Remove the stream info list from db
      Parameters:
      streamId -
    • updateStreamInfo

      protected void updateStreamInfo(Broadcast broadcast, BroadcastUpdate newBroadcast)
      Updates the stream fields if it's not null
      Parameters:
      broadcast -
      name -
      description -
      userName -
      password -
      ipAddr -
      streamUrl -
    • getLocalLiveBroadcastCount

      public abstract long getLocalLiveBroadcastCount(String hostAddress)
    • getLocalLiveBroadcasts

      public abstract List<Broadcast> getLocalLiveBroadcasts(String hostAddress)
    • searchOnServerVod

      protected ArrayList<VoD> searchOnServerVod(ArrayList<VoD> broadcastList, String search)
      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.
    • sortAndCropVodList

      protected List<VoD> sortAndCropVodList(List<VoD> vodList, int offset, int size, String sortBy, String orderBy)
    • searchOnServer

      protected ArrayList<Broadcast> searchOnServer(ArrayList<Broadcast> broadcastList, String search)
    • sortAndCropBroadcastList

      protected List<Broadcast> sortAndCropBroadcastList(List<Broadcast> broadcastList, int offset, int size, String sortBy, String orderBy)
    • createP2PConnection

      public abstract boolean createP2PConnection(P2PConnection conn)
      Creates new P2PConnection
      Parameters:
      conn - - P2PConnection object
      Returns:
      boolean - success
    • getP2PConnection

      public abstract P2PConnection getP2PConnection(String streamId)
      Get the P2PConnection by streamId
      Parameters:
      streamId - - stream id for P2PConnection
      Returns:
      P2PConnection - if exist else null
    • deleteP2PConnection

      public abstract boolean deleteP2PConnection(String streamId)
      Deletes a P2PConnection
      Parameters:
      conn - - P2PConnection object
      Returns:
      boolean - success
    • addSubTrack

      @Deprecated(since="2.9.1", forRemoval=true) public abstract boolean addSubTrack(String mainTrackId, String subTrackId)
      Deprecated, for removal: This API element is subject to removal in a future version.
      no need to use this method, logic has changed and we use directly getSubtracks, getActiveSubtracks. It's kept for backward compatibility Add a subtrack id to a main track (broadcast)
      Parameters:
      mainTrackId - - main track id
      subTrackId - - main track id
      Returns:
      boolean - success
    • removeSubTrack

      @Deprecated(since="2.9.1", forRemoval=true) public abstract boolean removeSubTrack(String mainTrackId, String subTrackId)
      Deprecated, for removal: This API element is subject to removal in a future version.
      no need to use this method, logic has changed and we use directly getSubtracks, getActiveSubtracks. It's kept for backward compatibility Remove a subtrack id from a main track (broadcast)
      Parameters:
      mainTrackId - - main track id
      subTrackId - - main track id
      Returns:
      boolean - success
    • resetBroadcasts

      public abstract int resetBroadcasts(String hostAddress)
      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

      public int getTotalWebRTCViewersCount(Map<String,String> broadcastMap, com.google.gson.Gson gson)
    • searchOnWebRTCViewerInfo

      protected ArrayList<WebRTCViewerInfo> searchOnWebRTCViewerInfo(ArrayList<WebRTCViewerInfo> list, String search)
    • sortAndCropWebRTCViewerInfoList

      protected List<WebRTCViewerInfo> sortAndCropWebRTCViewerInfoList(List<WebRTCViewerInfo> list, int offset, int size, String sortBy, String orderBy)
    • saveViewerInfo

      public abstract void saveViewerInfo(WebRTCViewerInfo info)
      This is used to save WebRTC Viewer Info to datastore
      Parameters:
      info - information for the WebRTC Viewer
    • getWebRTCViewerList

      public abstract List<WebRTCViewerInfo> getWebRTCViewerList(int offset, int size, String sortBy, String orderBy, String search)
      Get list of webrtc viewers
      Parameters:
      offset -
      size -
      search -
      orderBy -
      sortBy -
      Returns:
      list of webrtc viewers
    • getWebRTCViewerList

      public List<WebRTCViewerInfo> getWebRTCViewerList(Map<String,String> webRTCViewerMap, int offset, int size, String sortBy, String orderBy, String search, com.google.gson.Gson gson)
    • deleteWebRTCViewerInfo

      public abstract boolean deleteWebRTCViewerInfo(String viewerId)
      This is used to delete a WebRTC Viewer Info from datastore
      Parameters:
      viewerId - WebRTC Viewer Id
    • updateStreamMetaData

      public abstract boolean updateStreamMetaData(String streamId, String metaData)
      This is used to update meta data for a bradcast
      Parameters:
      streamId - id for the broadcast
      metaData - new meta data
    • putSubscriberMetaData

      public abstract void putSubscriberMetaData(String subscriberId, SubscriberMetadata metadata)
      Put subscriber metadata. It overwrites the metadata, if you need to update something, first get the getSubscriberMetaData(String) , update it and put it
      Parameters:
      subscriberId -
      SubscriberMetadata -
    • getSubscriberMetaData

      public abstract SubscriberMetadata getSubscriberMetaData(String subscriberId)
      Get subscriber metadata
      Parameters:
      subscriberId -
      Returns:
    • broadcastToConference

      public static ConferenceRoom broadcastToConference(Broadcast broadcast)
      This is a helper method to remove the ConferenceRoom in later versions May 11, 2024 - mekya
      Parameters:
      broadcast -
      Returns:
    • conferenceUpdateToBroadcastUpdate

      public static BroadcastUpdate conferenceUpdateToBroadcastUpdate(ConferenceRoom conferenceRoom) throws Exception
      This is a helper method to remove the ConferenceRoom in later versions May 11, 2024 - mekya
      Parameters:
      broadcast -
      Returns:
      Throws:
      Exception
    • conferenceToBroadcast

      public static Broadcast conferenceToBroadcast(ConferenceRoom conferenceRoom) throws Exception
      Throws:
      Exception
    • migrateConferenceRoomsToBroadcasts

      public abstract void migrateConferenceRoomsToBroadcasts()
      Move ConferenceRoom to Broadcast
    • getSubtracks

      public abstract List<Broadcast> getSubtracks(String mainTrackId, int offset, int size, String role, String status)
      Get the subtracks of the main track
      Parameters:
      mainTrackId - the main track to get the subtracks
      offset - the offset to get the subtracks
      size - number of items to get
      role - the role of the subtracks for role based streaming especially in conferences. It can be null
      status - the status of the stream broadcasting, finished etc. It can be null
      Returns:
    • getSubtracks

      public abstract List<Broadcast> getSubtracks(String mainTrackId, int offset, int size, String role)
      Get the subtracks of the main track
      Parameters:
      mainTrackId - the main track to get the subtracks
      offset - the offset to get the subtracks
      size - number of items to get
      role - the role of the subtracks for role based streaming especially in conferences. It can be null
      Returns:
    • getSubtrackCount

      public abstract long getSubtrackCount(String mainTrackId, String role, String status)
      Get the count of subtracks
      Parameters:
      mainTrackId - the main track to get the subtracks
      role - the role of the subtracks for role based streaming especially in conferences
      Returns:
      number of subtracks
    • getActiveSubtracksCount

      public abstract long getActiveSubtracksCount(String mainTrackId, String role)
      Get the count of active subtracks. If subtrack is stucked in broadcasting or preparing, it will not count it.
      Parameters:
      mainTrackId -
      role -
      Returns:
    • getActiveSubtracks

      public abstract List<Broadcast> getActiveSubtracks(String mainTrackId, String role)
      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:
    • hasSubtracks

      public abstract boolean hasSubtracks(String streamId)
      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 if AppSettings.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

      public void setAppSettings(AppSettings appSettings)
      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

      public void showWarningIfElapsedTimeIsMoreThanThreshold(long elapsedNano, String methodName)