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.

121 lines
3.9 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 "modules/video_coding/video_receiver2.h"
  14. #include "api/video_codecs/video_codec.h"
  15. #include "api/video_codecs/video_decoder.h"
  16. #include "modules/video_coding/decoder_database.h"
  17. #include "modules/video_coding/encoded_frame.h"
  18. #include "modules/video_coding/generic_decoder.h"
  19. #include "modules/video_coding/include/video_coding_defines.h"
  20. #include "modules/video_coding/timing.h"
  21. #include "rtc_base/checks.h"
  22. #include "rtc_base/trace_event.h"
  23. #include "system_wrappers/include/clock.h"
  24. namespace webrtc {
  25. VideoReceiver2::VideoReceiver2(Clock* clock, VCMTiming* timing)
  26. : clock_(clock),
  27. timing_(timing),
  28. decodedFrameCallback_(timing_, clock_),
  29. codecDataBase_() {
  30. decoder_thread_checker_.Detach();
  31. }
  32. VideoReceiver2::~VideoReceiver2() {
  33. RTC_DCHECK_RUN_ON(&construction_thread_checker_);
  34. }
  35. // Register a receive callback. Will be called whenever there is a new frame
  36. // ready for rendering.
  37. int32_t VideoReceiver2::RegisterReceiveCallback(
  38. VCMReceiveCallback* receiveCallback) {
  39. RTC_DCHECK_RUN_ON(&construction_thread_checker_);
  40. RTC_DCHECK(!IsDecoderThreadRunning());
  41. // This value is set before the decoder thread starts and unset after
  42. // the decoder thread has been stopped.
  43. decodedFrameCallback_.SetUserReceiveCallback(receiveCallback);
  44. return VCM_OK;
  45. }
  46. // Register an externally defined decoder object.
  47. void VideoReceiver2::RegisterExternalDecoder(VideoDecoder* externalDecoder,
  48. uint8_t payloadType) {
  49. RTC_DCHECK_RUN_ON(&construction_thread_checker_);
  50. RTC_DCHECK(!IsDecoderThreadRunning());
  51. if (externalDecoder == nullptr) {
  52. RTC_CHECK(codecDataBase_.DeregisterExternalDecoder(payloadType));
  53. return;
  54. }
  55. codecDataBase_.RegisterExternalDecoder(externalDecoder, payloadType);
  56. }
  57. void VideoReceiver2::DecoderThreadStarting() {
  58. RTC_DCHECK_RUN_ON(&construction_thread_checker_);
  59. RTC_DCHECK(!IsDecoderThreadRunning());
  60. #if RTC_DCHECK_IS_ON
  61. decoder_thread_is_running_ = true;
  62. #endif
  63. }
  64. void VideoReceiver2::DecoderThreadStopped() {
  65. RTC_DCHECK_RUN_ON(&construction_thread_checker_);
  66. RTC_DCHECK(IsDecoderThreadRunning());
  67. #if RTC_DCHECK_IS_ON
  68. decoder_thread_is_running_ = false;
  69. decoder_thread_checker_.Detach();
  70. #endif
  71. }
  72. // Must be called from inside the receive side critical section.
  73. int32_t VideoReceiver2::Decode(const VCMEncodedFrame* frame) {
  74. RTC_DCHECK_RUN_ON(&decoder_thread_checker_);
  75. TRACE_EVENT0("webrtc", "VideoReceiver2::Decode");
  76. // Change decoder if payload type has changed
  77. VCMGenericDecoder* decoder =
  78. codecDataBase_.GetDecoder(*frame, &decodedFrameCallback_);
  79. if (decoder == nullptr) {
  80. return VCM_NO_CODEC_REGISTERED;
  81. }
  82. return decoder->Decode(*frame, clock_->TimeInMilliseconds());
  83. }
  84. // Register possible receive codecs, can be called multiple times
  85. int32_t VideoReceiver2::RegisterReceiveCodec(const VideoCodec* receiveCodec,
  86. int32_t numberOfCores,
  87. bool requireKeyFrame) {
  88. RTC_DCHECK_RUN_ON(&construction_thread_checker_);
  89. RTC_DCHECK(!IsDecoderThreadRunning());
  90. if (receiveCodec == nullptr) {
  91. return VCM_PARAMETER_ERROR;
  92. }
  93. if (!codecDataBase_.RegisterReceiveCodec(receiveCodec, numberOfCores,
  94. requireKeyFrame)) {
  95. return -1;
  96. }
  97. return 0;
  98. }
  99. bool VideoReceiver2::IsDecoderThreadRunning() {
  100. #if RTC_DCHECK_IS_ON
  101. return decoder_thread_is_running_;
  102. #else
  103. return true;
  104. #endif
  105. }
  106. } // namespace webrtc