• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
11 #ifndef MODULES_VIDEO_CODING_VIDEO_CODING_IMPL_H_
12 #define MODULES_VIDEO_CODING_VIDEO_CODING_IMPL_H_
13 
14 #include <memory>
15 #include <string>
16 #include <vector>
17 
18 #include "absl/types/optional.h"
19 #include "modules/video_coding/decoder_database.h"
20 #include "modules/video_coding/frame_buffer.h"
21 #include "modules/video_coding/generic_decoder.h"
22 #include "modules/video_coding/include/video_coding.h"
23 #include "modules/video_coding/jitter_buffer.h"
24 #include "modules/video_coding/receiver.h"
25 #include "modules/video_coding/timing.h"
26 #include "rtc_base/one_time_event.h"
27 #include "rtc_base/synchronization/mutex.h"
28 #include "rtc_base/synchronization/sequence_checker.h"
29 #include "rtc_base/thread_annotations.h"
30 #include "rtc_base/thread_checker.h"
31 #include "system_wrappers/include/clock.h"
32 
33 namespace webrtc {
34 
35 class VideoBitrateAllocator;
36 class VideoBitrateAllocationObserver;
37 
38 namespace vcm {
39 
40 class VCMProcessTimer {
41  public:
42   static const int64_t kDefaultProcessIntervalMs = 1000;
43 
VCMProcessTimer(int64_t periodMs,Clock * clock)44   VCMProcessTimer(int64_t periodMs, Clock* clock)
45       : _clock(clock),
46         _periodMs(periodMs),
47         _latestMs(_clock->TimeInMilliseconds()) {}
48   int64_t Period() const;
49   int64_t TimeUntilProcess() const;
50   void Processed();
51 
52  private:
53   Clock* _clock;
54   int64_t _periodMs;
55   int64_t _latestMs;
56 };
57 
58 class VideoReceiver : public Module {
59  public:
60   VideoReceiver(Clock* clock, VCMTiming* timing);
61   ~VideoReceiver() override;
62 
63   int32_t RegisterReceiveCodec(const VideoCodec* receiveCodec,
64                                int32_t numberOfCores,
65                                bool requireKeyFrame);
66 
67   void RegisterExternalDecoder(VideoDecoder* externalDecoder,
68                                uint8_t payloadType);
69   int32_t RegisterReceiveCallback(VCMReceiveCallback* receiveCallback);
70   int32_t RegisterFrameTypeCallback(VCMFrameTypeCallback* frameTypeCallback);
71   int32_t RegisterPacketRequestCallback(VCMPacketRequestCallback* callback);
72 
73   int32_t Decode(uint16_t maxWaitTimeMs);
74 
75   int32_t IncomingPacket(const uint8_t* incomingPayload,
76                          size_t payloadLength,
77                          const RTPHeader& rtp_header,
78                          const RTPVideoHeader& video_header);
79 
80   void SetNackSettings(size_t max_nack_list_size,
81                        int max_packet_age_to_nack,
82                        int max_incomplete_time_ms);
83 
84   int64_t TimeUntilNextProcess() override;
85   void Process() override;
86   void ProcessThreadAttached(ProcessThread* process_thread) override;
87 
88  protected:
89   int32_t Decode(const webrtc::VCMEncodedFrame& frame);
90   int32_t RequestKeyFrame();
91 
92  private:
93   // Used for DCHECKing thread correctness.
94   // In build where DCHECKs are enabled, will return false before
95   // DecoderThreadStarting is called, then true until DecoderThreadStopped
96   // is called.
97   // In builds where DCHECKs aren't enabled, it will return true.
98   bool IsDecoderThreadRunning();
99 
100   rtc::ThreadChecker construction_thread_checker_;
101   rtc::ThreadChecker decoder_thread_checker_;
102   rtc::ThreadChecker module_thread_checker_;
103   Clock* const clock_;
104   Mutex process_mutex_;
105   VCMTiming* _timing;
106   VCMReceiver _receiver;
107   VCMDecodedFrameCallback _decodedFrameCallback;
108 
109   // These callbacks are set on the construction thread before being attached
110   // to the module thread or decoding started, so a lock is not required.
111   VCMFrameTypeCallback* _frameTypeCallback;
112   VCMPacketRequestCallback* _packetRequestCallback;
113 
114   // Used on both the module and decoder thread.
115   bool _scheduleKeyRequest RTC_GUARDED_BY(process_mutex_);
116   bool drop_frames_until_keyframe_ RTC_GUARDED_BY(process_mutex_);
117 
118   // Modified on the construction thread while not attached to the process
119   // thread.  Once attached to the process thread, its value is only read
120   // so a lock is not required.
121   size_t max_nack_list_size_;
122 
123   // Callbacks are set before the decoder thread starts.
124   // Once the decoder thread has been started, usage of |_codecDataBase| moves
125   // over to the decoder thread.
126   VCMDecoderDataBase _codecDataBase;
127 
128   VCMProcessTimer _retransmissionTimer RTC_GUARDED_BY(module_thread_checker_);
129   VCMProcessTimer _keyRequestTimer RTC_GUARDED_BY(module_thread_checker_);
130   ThreadUnsafeOneTimeEvent first_frame_received_
131       RTC_GUARDED_BY(decoder_thread_checker_);
132   // Modified on the construction thread. Can be read without a lock and assumed
133   // to be non-null on the module and decoder threads.
134   ProcessThread* process_thread_ = nullptr;
135   bool is_attached_to_process_thread_
136       RTC_GUARDED_BY(construction_thread_checker_) = false;
137 };
138 
139 }  // namespace vcm
140 }  // namespace webrtc
141 #endif  // MODULES_VIDEO_CODING_VIDEO_CODING_IMPL_H_
142