video_jbuff_decoding_state.h 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. #ifndef VIDEO_JBUFFER_DECODING_STATE_H_
  2. #define VIDEO_JBUFFER_DECODING_STATE_H_
  3. #include <map>
  4. #include <set>
  5. #include <vector>
  6. #include <limits>
  7. #include "stdint.h"
  8. #include "../adaptive_jitter_buffer/video_jbuff_frame.h"
  9. namespace AJB {
  10. template <typename U>
  11. inline bool IsNewer(U value, U prev_value) {
  12. //static_assert(!std::numeric_limits<U>::is_signed, "U must be unsigned");
  13. // kBreakpoint is the half-way mark for the type U. For instance, for a
  14. // uint16_t it will be 0x8000, and for a uint32_t, it will be 0x8000000.
  15. const U kBreakpoint = (std::numeric_limits<U>::max() >> 1) + 1;
  16. // Distinguish between elements that are exactly kBreakpoint apart.
  17. // If t1>t2 and |t1-t2| = kBreakpoint: IsNewer(t1,t2)=true,
  18. // IsNewer(t2,t1)=false
  19. // rather than having IsNewer(t1,t2) = IsNewer(t2,t1) = false.
  20. if (value - prev_value == kBreakpoint) {
  21. return value > prev_value;
  22. }
  23. return value != prev_value &&
  24. static_cast<U>(value - prev_value) < kBreakpoint;
  25. }
  26. inline bool IsNewerSequenceNumber(uint16_t sequence_number,
  27. uint16_t prev_sequence_number) {
  28. return IsNewer(sequence_number, prev_sequence_number);
  29. }
  30. // NB: Doesn't fulfill strict weak ordering requirements.
  31. // Mustn't be used as std::map Compare function.
  32. inline bool IsNewerTimestamp(uint32_t timestamp, uint32_t prev_timestamp) {
  33. return IsNewer(timestamp, prev_timestamp);
  34. }
  35. inline uint16_t LatestSequenceNumber(uint16_t sequence_number1,
  36. uint16_t sequence_number2) {
  37. return IsNewerSequenceNumber(sequence_number1, sequence_number2)
  38. ? sequence_number1
  39. : sequence_number2;
  40. }
  41. inline uint32_t LatestTimestamp(uint32_t timestamp1, uint32_t timestamp2) {
  42. return IsNewerTimestamp(timestamp1, timestamp2) ? timestamp1 : timestamp2;
  43. }
  44. class DecodingState {
  45. public:
  46. DecodingState();
  47. ~DecodingState();
  48. // Check for old frame
  49. bool IsOldFrame(const Frame* frame);
  50. // Check for old packet
  51. bool IsOldPacket(const Packet* packet);
  52. // Check for frame continuity based on current decoded state. Use best method
  53. // possible, i.e. temporal info, picture ID or sequence number.
  54. bool ContinuousFrame(const Frame* frame);
  55. void SetState(const Frame* frame);
  56. void CopyFrom(const DecodingState& state);
  57. // Update the sequence number if the timestamp matches current state and the
  58. // sequence number is higher than the current one. This accounts for packets
  59. // arriving late.
  60. void UpdateOldPacket(const Packet* packet);
  61. void SetSeqNum(uint16_t new_seq_num);
  62. void Reset();
  63. uint32_t time_stamp() const;
  64. uint16_t sequence_num() const;
  65. // Return true if at initial state.
  66. bool in_initial_state() const;
  67. private:
  68. // Designated continuity functions
  69. bool ContinuousSeqNum(uint16_t seq_num);
  70. // Keep state of last decoded frame.
  71. // TODO(mikhal/stefan): create designated classes to handle these types.
  72. uint16_t sequence_num_;
  73. uint32_t time_stamp_;
  74. bool in_initial_state_;
  75. };
  76. } // namespace AJB
  77. #endif // VIDEO_JBUFFER_DECODING_STATE_H_