video_coding_impl.h 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. /*
  2. * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
  3. *
  4. * Use of this source code is governed by a BSD-style license
  5. * that can be found in the LICENSE file in the root of the source
  6. * tree. An additional intellectual property rights grant can be found
  7. * in the file PATENTS. All contributing project authors may
  8. * be found in the AUTHORS file in the root of the source tree.
  9. */
  10. #ifndef MODULES_VIDEO_CODING_VIDEO_CODING_IMPL_H_
  11. #define MODULES_VIDEO_CODING_VIDEO_CODING_IMPL_H_
  12. #include <memory>
  13. #include <string>
  14. #include <vector>
  15. #include "absl/types/optional.h"
  16. #include "modules/video_coding/decoder_database.h"
  17. #include "modules/video_coding/frame_buffer.h"
  18. #include "modules/video_coding/generic_decoder.h"
  19. #include "modules/video_coding/include/video_coding.h"
  20. #include "modules/video_coding/jitter_buffer.h"
  21. #include "modules/video_coding/receiver.h"
  22. #include "modules/video_coding/timing.h"
  23. #include "rtc_base/one_time_event.h"
  24. #include "rtc_base/synchronization/mutex.h"
  25. #include "rtc_base/synchronization/sequence_checker.h"
  26. #include "rtc_base/thread_annotations.h"
  27. #include "rtc_base/thread_checker.h"
  28. #include "system_wrappers/include/clock.h"
  29. namespace webrtc {
  30. class VideoBitrateAllocator;
  31. class VideoBitrateAllocationObserver;
  32. namespace vcm {
  33. class VCMProcessTimer {
  34. public:
  35. static const int64_t kDefaultProcessIntervalMs = 1000;
  36. VCMProcessTimer(int64_t periodMs, Clock* clock)
  37. : _clock(clock),
  38. _periodMs(periodMs),
  39. _latestMs(_clock->TimeInMilliseconds()) {}
  40. int64_t Period() const;
  41. int64_t TimeUntilProcess() const;
  42. void Processed();
  43. private:
  44. Clock* _clock;
  45. int64_t _periodMs;
  46. int64_t _latestMs;
  47. };
  48. class VideoReceiver : public Module {
  49. public:
  50. VideoReceiver(Clock* clock, VCMTiming* timing);
  51. ~VideoReceiver() override;
  52. int32_t RegisterReceiveCodec(uint8_t payload_type,
  53. const VideoCodec* receiveCodec,
  54. int32_t numberOfCores);
  55. void RegisterExternalDecoder(VideoDecoder* externalDecoder,
  56. uint8_t payloadType);
  57. int32_t RegisterReceiveCallback(VCMReceiveCallback* receiveCallback);
  58. int32_t RegisterFrameTypeCallback(VCMFrameTypeCallback* frameTypeCallback);
  59. int32_t RegisterPacketRequestCallback(VCMPacketRequestCallback* callback);
  60. int32_t Decode(uint16_t maxWaitTimeMs);
  61. int32_t IncomingPacket(const uint8_t* incomingPayload,
  62. size_t payloadLength,
  63. const RTPHeader& rtp_header,
  64. const RTPVideoHeader& video_header);
  65. void SetNackSettings(size_t max_nack_list_size,
  66. int max_packet_age_to_nack,
  67. int max_incomplete_time_ms);
  68. int64_t TimeUntilNextProcess() override;
  69. void Process() override;
  70. void ProcessThreadAttached(ProcessThread* process_thread) override;
  71. protected:
  72. int32_t Decode(const webrtc::VCMEncodedFrame& frame);
  73. int32_t RequestKeyFrame();
  74. private:
  75. // Used for DCHECKing thread correctness.
  76. // In build where DCHECKs are enabled, will return false before
  77. // DecoderThreadStarting is called, then true until DecoderThreadStopped
  78. // is called.
  79. // In builds where DCHECKs aren't enabled, it will return true.
  80. bool IsDecoderThreadRunning();
  81. rtc::ThreadChecker construction_thread_checker_;
  82. rtc::ThreadChecker decoder_thread_checker_;
  83. rtc::ThreadChecker module_thread_checker_;
  84. Clock* const clock_;
  85. Mutex process_mutex_;
  86. VCMTiming* _timing;
  87. VCMReceiver _receiver;
  88. VCMDecodedFrameCallback _decodedFrameCallback;
  89. // These callbacks are set on the construction thread before being attached
  90. // to the module thread or decoding started, so a lock is not required.
  91. VCMFrameTypeCallback* _frameTypeCallback;
  92. VCMPacketRequestCallback* _packetRequestCallback;
  93. // Used on both the module and decoder thread.
  94. bool _scheduleKeyRequest RTC_GUARDED_BY(process_mutex_);
  95. bool drop_frames_until_keyframe_ RTC_GUARDED_BY(process_mutex_);
  96. // Modified on the construction thread while not attached to the process
  97. // thread. Once attached to the process thread, its value is only read
  98. // so a lock is not required.
  99. size_t max_nack_list_size_;
  100. // Callbacks are set before the decoder thread starts.
  101. // Once the decoder thread has been started, usage of |_codecDataBase| moves
  102. // over to the decoder thread.
  103. VCMDecoderDataBase _codecDataBase;
  104. VCMProcessTimer _retransmissionTimer RTC_GUARDED_BY(module_thread_checker_);
  105. VCMProcessTimer _keyRequestTimer RTC_GUARDED_BY(module_thread_checker_);
  106. ThreadUnsafeOneTimeEvent first_frame_received_
  107. RTC_GUARDED_BY(decoder_thread_checker_);
  108. // Modified on the construction thread. Can be read without a lock and assumed
  109. // to be non-null on the module and decoder threads.
  110. ProcessThread* process_thread_ = nullptr;
  111. bool is_attached_to_process_thread_
  112. RTC_GUARDED_BY(construction_thread_checker_) = false;
  113. };
  114. } // namespace vcm
  115. } // namespace webrtc
  116. #endif // MODULES_VIDEO_CODING_VIDEO_CODING_IMPL_H_