You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

328 lines
11 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. /*
  2. * Copyright (c) 2013 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. #include <stddef.h>
  11. #include <cstdint>
  12. #include <vector>
  13. #include "api/rtp_headers.h"
  14. #include "api/video_codecs/video_codec.h"
  15. #include "api/video_codecs/video_decoder.h"
  16. #include "modules/include/module_common_types.h"
  17. #include "modules/utility/include/process_thread.h"
  18. #include "modules/video_coding/decoder_database.h"
  19. #include "modules/video_coding/encoded_frame.h"
  20. #include "modules/video_coding/generic_decoder.h"
  21. #include "modules/video_coding/include/video_coding.h"
  22. #include "modules/video_coding/include/video_coding_defines.h"
  23. #include "modules/video_coding/internal_defines.h"
  24. #include "modules/video_coding/jitter_buffer.h"
  25. #include "modules/video_coding/media_opt_util.h"
  26. #include "modules/video_coding/packet.h"
  27. #include "modules/video_coding/receiver.h"
  28. #include "modules/video_coding/timing.h"
  29. #include "modules/video_coding/video_coding_impl.h"
  30. #include "rtc_base/checks.h"
  31. #include "rtc_base/critical_section.h"
  32. #include "rtc_base/location.h"
  33. #include "rtc_base/logging.h"
  34. #include "rtc_base/one_time_event.h"
  35. #include "rtc_base/thread_checker.h"
  36. #include "rtc_base/trace_event.h"
  37. #include "system_wrappers/include/clock.h"
  38. namespace webrtc {
  39. namespace vcm {
  40. VideoReceiver::VideoReceiver(Clock* clock, VCMTiming* timing)
  41. : clock_(clock),
  42. _timing(timing),
  43. _receiver(_timing, clock_),
  44. _decodedFrameCallback(_timing, clock_),
  45. _frameTypeCallback(nullptr),
  46. _packetRequestCallback(nullptr),
  47. _scheduleKeyRequest(false),
  48. drop_frames_until_keyframe_(false),
  49. max_nack_list_size_(0),
  50. _codecDataBase(),
  51. _receiveStatsTimer(1000, clock_),
  52. _retransmissionTimer(10, clock_),
  53. _keyRequestTimer(500, clock_) {
  54. decoder_thread_checker_.Detach();
  55. module_thread_checker_.Detach();
  56. }
  57. VideoReceiver::~VideoReceiver() {
  58. RTC_DCHECK_RUN_ON(&construction_thread_checker_);
  59. }
  60. void VideoReceiver::Process() {
  61. RTC_DCHECK_RUN_ON(&module_thread_checker_);
  62. // Receive-side statistics
  63. // TODO(philipel): Remove this if block when we know what to do with
  64. // ReceiveStatisticsProxy::QualitySample.
  65. if (_receiveStatsTimer.TimeUntilProcess() == 0) {
  66. _receiveStatsTimer.Processed();
  67. }
  68. // Key frame requests
  69. if (_keyRequestTimer.TimeUntilProcess() == 0) {
  70. _keyRequestTimer.Processed();
  71. bool request_key_frame = _frameTypeCallback != nullptr;
  72. if (request_key_frame) {
  73. rtc::CritScope cs(&process_crit_);
  74. request_key_frame = _scheduleKeyRequest;
  75. }
  76. if (request_key_frame)
  77. RequestKeyFrame();
  78. }
  79. // Packet retransmission requests
  80. // TODO(holmer): Add API for changing Process interval and make sure it's
  81. // disabled when NACK is off.
  82. if (_retransmissionTimer.TimeUntilProcess() == 0) {
  83. _retransmissionTimer.Processed();
  84. bool callback_registered = _packetRequestCallback != nullptr;
  85. uint16_t length = max_nack_list_size_;
  86. if (callback_registered && length > 0) {
  87. // Collect sequence numbers from the default receiver.
  88. bool request_key_frame = false;
  89. std::vector<uint16_t> nackList = _receiver.NackList(&request_key_frame);
  90. int32_t ret = VCM_OK;
  91. if (request_key_frame) {
  92. ret = RequestKeyFrame();
  93. }
  94. if (ret == VCM_OK && !nackList.empty()) {
  95. rtc::CritScope cs(&process_crit_);
  96. if (_packetRequestCallback != nullptr) {
  97. _packetRequestCallback->ResendPackets(&nackList[0], nackList.size());
  98. }
  99. }
  100. }
  101. }
  102. }
  103. void VideoReceiver::ProcessThreadAttached(ProcessThread* process_thread) {
  104. RTC_DCHECK_RUN_ON(&construction_thread_checker_);
  105. if (process_thread) {
  106. is_attached_to_process_thread_ = true;
  107. RTC_DCHECK(!process_thread_ || process_thread_ == process_thread);
  108. process_thread_ = process_thread;
  109. } else {
  110. is_attached_to_process_thread_ = false;
  111. }
  112. }
  113. int64_t VideoReceiver::TimeUntilNextProcess() {
  114. RTC_DCHECK_RUN_ON(&module_thread_checker_);
  115. int64_t timeUntilNextProcess = _receiveStatsTimer.TimeUntilProcess();
  116. // We need a Process call more often if we are relying on
  117. // retransmissions
  118. timeUntilNextProcess =
  119. VCM_MIN(timeUntilNextProcess, _retransmissionTimer.TimeUntilProcess());
  120. timeUntilNextProcess =
  121. VCM_MIN(timeUntilNextProcess, _keyRequestTimer.TimeUntilProcess());
  122. return timeUntilNextProcess;
  123. }
  124. // Register a receive callback. Will be called whenever there is a new frame
  125. // ready for rendering.
  126. int32_t VideoReceiver::RegisterReceiveCallback(
  127. VCMReceiveCallback* receiveCallback) {
  128. RTC_DCHECK_RUN_ON(&construction_thread_checker_);
  129. // This value is set before the decoder thread starts and unset after
  130. // the decoder thread has been stopped.
  131. _decodedFrameCallback.SetUserReceiveCallback(receiveCallback);
  132. return VCM_OK;
  133. }
  134. // Register an externally defined decoder object.
  135. void VideoReceiver::RegisterExternalDecoder(VideoDecoder* externalDecoder,
  136. uint8_t payloadType) {
  137. RTC_DCHECK_RUN_ON(&construction_thread_checker_);
  138. if (externalDecoder == nullptr) {
  139. RTC_CHECK(_codecDataBase.DeregisterExternalDecoder(payloadType));
  140. return;
  141. }
  142. _codecDataBase.RegisterExternalDecoder(externalDecoder, payloadType);
  143. }
  144. // Register a frame type request callback.
  145. int32_t VideoReceiver::RegisterFrameTypeCallback(
  146. VCMFrameTypeCallback* frameTypeCallback) {
  147. RTC_DCHECK_RUN_ON(&construction_thread_checker_);
  148. RTC_DCHECK(!is_attached_to_process_thread_);
  149. // This callback is used on the module thread, but since we don't get
  150. // callbacks on the module thread while the decoder thread isn't running
  151. // (and this function must not be called when the decoder is running),
  152. // we don't need a lock here.
  153. _frameTypeCallback = frameTypeCallback;
  154. return VCM_OK;
  155. }
  156. int32_t VideoReceiver::RegisterPacketRequestCallback(
  157. VCMPacketRequestCallback* callback) {
  158. RTC_DCHECK_RUN_ON(&construction_thread_checker_);
  159. RTC_DCHECK(!is_attached_to_process_thread_);
  160. // This callback is used on the module thread, but since we don't get
  161. // callbacks on the module thread while the decoder thread isn't running
  162. // (and this function must not be called when the decoder is running),
  163. // we don't need a lock here.
  164. _packetRequestCallback = callback;
  165. return VCM_OK;
  166. }
  167. // Decode next frame, blocking.
  168. // Should be called as often as possible to get the most out of the decoder.
  169. int32_t VideoReceiver::Decode(uint16_t maxWaitTimeMs) {
  170. RTC_DCHECK_RUN_ON(&decoder_thread_checker_);
  171. VCMEncodedFrame* frame = _receiver.FrameForDecoding(
  172. maxWaitTimeMs, _codecDataBase.PrefersLateDecoding());
  173. if (!frame)
  174. return VCM_FRAME_NOT_READY;
  175. bool drop_frame = false;
  176. {
  177. rtc::CritScope cs(&process_crit_);
  178. if (drop_frames_until_keyframe_) {
  179. // Still getting delta frames, schedule another keyframe request as if
  180. // decode failed.
  181. if (frame->FrameType() != VideoFrameType::kVideoFrameKey) {
  182. drop_frame = true;
  183. _scheduleKeyRequest = true;
  184. // TODO(tommi): Consider if we could instead post a task to the module
  185. // thread and call RequestKeyFrame directly. Here we call WakeUp so that
  186. // TimeUntilNextProcess() gets called straight away.
  187. process_thread_->WakeUp(this);
  188. } else {
  189. drop_frames_until_keyframe_ = false;
  190. }
  191. }
  192. }
  193. if (drop_frame) {
  194. _receiver.ReleaseFrame(frame);
  195. return VCM_FRAME_NOT_READY;
  196. }
  197. // If this frame was too late, we should adjust the delay accordingly
  198. _timing->UpdateCurrentDelay(frame->RenderTimeMs(),
  199. clock_->TimeInMilliseconds());
  200. if (first_frame_received_()) {
  201. RTC_LOG(LS_INFO) << "Received first "
  202. << (frame->Complete() ? "complete" : "incomplete")
  203. << " decodable video frame";
  204. }
  205. const int32_t ret = Decode(*frame);
  206. _receiver.ReleaseFrame(frame);
  207. return ret;
  208. }
  209. int32_t VideoReceiver::RequestKeyFrame() {
  210. RTC_DCHECK_RUN_ON(&module_thread_checker_);
  211. TRACE_EVENT0("webrtc", "RequestKeyFrame");
  212. if (_frameTypeCallback != nullptr) {
  213. const int32_t ret = _frameTypeCallback->RequestKeyFrame();
  214. if (ret < 0) {
  215. return ret;
  216. }
  217. rtc::CritScope cs(&process_crit_);
  218. _scheduleKeyRequest = false;
  219. } else {
  220. return VCM_MISSING_CALLBACK;
  221. }
  222. return VCM_OK;
  223. }
  224. // Must be called from inside the receive side critical section.
  225. int32_t VideoReceiver::Decode(const VCMEncodedFrame& frame) {
  226. RTC_DCHECK_RUN_ON(&decoder_thread_checker_);
  227. TRACE_EVENT0("webrtc", "VideoReceiver::Decode");
  228. // Change decoder if payload type has changed
  229. VCMGenericDecoder* decoder =
  230. _codecDataBase.GetDecoder(frame, &_decodedFrameCallback);
  231. if (decoder == nullptr) {
  232. return VCM_NO_CODEC_REGISTERED;
  233. }
  234. return decoder->Decode(frame, clock_->TimeInMilliseconds());
  235. }
  236. // Register possible receive codecs, can be called multiple times
  237. int32_t VideoReceiver::RegisterReceiveCodec(const VideoCodec* receiveCodec,
  238. int32_t numberOfCores,
  239. bool requireKeyFrame) {
  240. RTC_DCHECK_RUN_ON(&construction_thread_checker_);
  241. if (receiveCodec == nullptr) {
  242. return VCM_PARAMETER_ERROR;
  243. }
  244. if (!_codecDataBase.RegisterReceiveCodec(receiveCodec, numberOfCores,
  245. requireKeyFrame)) {
  246. return -1;
  247. }
  248. return 0;
  249. }
  250. // Incoming packet from network parsed and ready for decode, non blocking.
  251. int32_t VideoReceiver::IncomingPacket(const uint8_t* incomingPayload,
  252. size_t payloadLength,
  253. const RTPHeader& rtp_header,
  254. const RTPVideoHeader& video_header) {
  255. RTC_DCHECK_RUN_ON(&module_thread_checker_);
  256. if (video_header.frame_type == VideoFrameType::kVideoFrameKey) {
  257. TRACE_EVENT1("webrtc", "VCM::PacketKeyFrame", "seqnum",
  258. rtp_header.sequenceNumber);
  259. }
  260. if (incomingPayload == nullptr) {
  261. // The jitter buffer doesn't handle non-zero payload lengths for packets
  262. // without payload.
  263. // TODO(holmer): We should fix this in the jitter buffer.
  264. payloadLength = 0;
  265. }
  266. // Callers don't provide any ntp time.
  267. const VCMPacket packet(incomingPayload, payloadLength, rtp_header,
  268. video_header, /*ntp_time_ms=*/0,
  269. clock_->TimeInMilliseconds());
  270. int32_t ret = _receiver.InsertPacket(packet);
  271. // TODO(holmer): Investigate if this somehow should use the key frame
  272. // request scheduling to throttle the requests.
  273. if (ret == VCM_FLUSH_INDICATOR) {
  274. {
  275. rtc::CritScope cs(&process_crit_);
  276. drop_frames_until_keyframe_ = true;
  277. }
  278. RequestKeyFrame();
  279. } else if (ret < 0) {
  280. return ret;
  281. }
  282. return VCM_OK;
  283. }
  284. void VideoReceiver::SetNackSettings(size_t max_nack_list_size,
  285. int max_packet_age_to_nack,
  286. int max_incomplete_time_ms) {
  287. RTC_DCHECK_RUN_ON(&construction_thread_checker_);
  288. if (max_nack_list_size != 0) {
  289. max_nack_list_size_ = max_nack_list_size;
  290. }
  291. _receiver.SetNackSettings(max_nack_list_size, max_packet_age_to_nack,
  292. max_incomplete_time_ms);
  293. }
  294. } // namespace vcm
  295. } // namespace webrtc