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 #include "webrtc/common_types.h"
12 #include "webrtc/common_video/libyuv/include/webrtc_libyuv.h"
13 #include "webrtc/modules/video_coding/codecs/interface/video_codec_interface.h"
14 #include "webrtc/modules/video_coding/main/source/encoded_frame.h"
15 #include "webrtc/modules/video_coding/main/source/jitter_buffer.h"
16 #include "webrtc/modules/video_coding/main/source/packet.h"
17 #include "webrtc/modules/video_coding/main/source/video_coding_impl.h"
18 #include "webrtc/system_wrappers/interface/clock.h"
19 #include "webrtc/system_wrappers/interface/trace_event.h"
20
21 namespace webrtc {
22 namespace vcm {
23
24 uint32_t
Period() const25 VCMProcessTimer::Period() const {
26 return _periodMs;
27 }
28
29 uint32_t
TimeUntilProcess() const30 VCMProcessTimer::TimeUntilProcess() const {
31 const int64_t time_since_process = _clock->TimeInMilliseconds() -
32 static_cast<int64_t>(_latestMs);
33 const int64_t time_until_process = static_cast<int64_t>(_periodMs) -
34 time_since_process;
35 if (time_until_process < 0)
36 return 0;
37 return time_until_process;
38 }
39
40 void
Processed()41 VCMProcessTimer::Processed() {
42 _latestMs = _clock->TimeInMilliseconds();
43 }
44 } // namespace vcm
45
46 namespace {
47 // This wrapper provides a way to modify the callback without the need to expose
48 // a register method all the way down to the function calling it.
49 class EncodedImageCallbackWrapper : public EncodedImageCallback {
50 public:
EncodedImageCallbackWrapper()51 EncodedImageCallbackWrapper()
52 : cs_(CriticalSectionWrapper::CreateCriticalSection()), callback_(NULL) {}
53
~EncodedImageCallbackWrapper()54 virtual ~EncodedImageCallbackWrapper() {}
55
Register(EncodedImageCallback * callback)56 void Register(EncodedImageCallback* callback) {
57 CriticalSectionScoped cs(cs_.get());
58 callback_ = callback;
59 }
60
61 // TODO(andresp): Change to void as return value is ignored.
Encoded(EncodedImage & encoded_image,const CodecSpecificInfo * codec_specific_info,const RTPFragmentationHeader * fragmentation)62 virtual int32_t Encoded(EncodedImage& encoded_image,
63 const CodecSpecificInfo* codec_specific_info,
64 const RTPFragmentationHeader* fragmentation) {
65 CriticalSectionScoped cs(cs_.get());
66 if (callback_)
67 return callback_->Encoded(
68 encoded_image, codec_specific_info, fragmentation);
69 return 0;
70 }
71
72 private:
73 scoped_ptr<CriticalSectionWrapper> cs_;
74 EncodedImageCallback* callback_ GUARDED_BY(cs_);
75 };
76
77 class VideoCodingModuleImpl : public VideoCodingModule {
78 public:
VideoCodingModuleImpl(Clock * clock,EventFactory * event_factory,bool owns_event_factory)79 VideoCodingModuleImpl(Clock* clock,
80 EventFactory* event_factory,
81 bool owns_event_factory)
82 : VideoCodingModule(),
83 sender_(new vcm::VideoSender(clock, &post_encode_callback_)),
84 receiver_(new vcm::VideoReceiver(clock, event_factory)),
85 own_event_factory_(owns_event_factory ? event_factory : NULL) {}
86
~VideoCodingModuleImpl()87 virtual ~VideoCodingModuleImpl() {
88 sender_.reset();
89 receiver_.reset();
90 own_event_factory_.reset();
91 }
92
TimeUntilNextProcess()93 virtual int32_t TimeUntilNextProcess() OVERRIDE {
94 int32_t sender_time = sender_->TimeUntilNextProcess();
95 int32_t receiver_time = receiver_->TimeUntilNextProcess();
96 assert(sender_time >= 0);
97 assert(receiver_time >= 0);
98 return VCM_MIN(sender_time, receiver_time);
99 }
100
Process()101 virtual int32_t Process() OVERRIDE {
102 int32_t sender_return = sender_->Process();
103 int32_t receiver_return = receiver_->Process();
104 if (sender_return != VCM_OK)
105 return sender_return;
106 return receiver_return;
107 }
108
InitializeSender()109 virtual int32_t InitializeSender() OVERRIDE {
110 return sender_->InitializeSender();
111 }
112
RegisterSendCodec(const VideoCodec * sendCodec,uint32_t numberOfCores,uint32_t maxPayloadSize)113 virtual int32_t RegisterSendCodec(const VideoCodec* sendCodec,
114 uint32_t numberOfCores,
115 uint32_t maxPayloadSize) OVERRIDE {
116 return sender_->RegisterSendCodec(sendCodec, numberOfCores, maxPayloadSize);
117 }
118
SendCodec(VideoCodec * currentSendCodec) const119 virtual int32_t SendCodec(VideoCodec* currentSendCodec) const OVERRIDE {
120 return sender_->SendCodec(currentSendCodec);
121 }
122
SendCodec() const123 virtual VideoCodecType SendCodec() const OVERRIDE {
124 return sender_->SendCodec();
125 }
126
RegisterExternalEncoder(VideoEncoder * externalEncoder,uint8_t payloadType,bool internalSource)127 virtual int32_t RegisterExternalEncoder(VideoEncoder* externalEncoder,
128 uint8_t payloadType,
129 bool internalSource) OVERRIDE {
130 return sender_->RegisterExternalEncoder(
131 externalEncoder, payloadType, internalSource);
132 }
133
CodecConfigParameters(uint8_t * buffer,int32_t size)134 virtual int32_t CodecConfigParameters(uint8_t* buffer,
135 int32_t size) OVERRIDE {
136 return sender_->CodecConfigParameters(buffer, size);
137 }
138
Bitrate(unsigned int * bitrate) const139 virtual int Bitrate(unsigned int* bitrate) const OVERRIDE {
140 return sender_->Bitrate(bitrate);
141 }
142
FrameRate(unsigned int * framerate) const143 virtual int FrameRate(unsigned int* framerate) const OVERRIDE {
144 return sender_->FrameRate(framerate);
145 }
146
SetChannelParameters(uint32_t target_bitrate,uint8_t lossRate,uint32_t rtt)147 virtual int32_t SetChannelParameters(uint32_t target_bitrate, // bits/s.
148 uint8_t lossRate,
149 uint32_t rtt) OVERRIDE {
150 return sender_->SetChannelParameters(target_bitrate, lossRate, rtt);
151 }
152
RegisterTransportCallback(VCMPacketizationCallback * transport)153 virtual int32_t RegisterTransportCallback(
154 VCMPacketizationCallback* transport) OVERRIDE {
155 return sender_->RegisterTransportCallback(transport);
156 }
157
RegisterSendStatisticsCallback(VCMSendStatisticsCallback * sendStats)158 virtual int32_t RegisterSendStatisticsCallback(
159 VCMSendStatisticsCallback* sendStats) OVERRIDE {
160 return sender_->RegisterSendStatisticsCallback(sendStats);
161 }
162
RegisterVideoQMCallback(VCMQMSettingsCallback * videoQMSettings)163 virtual int32_t RegisterVideoQMCallback(
164 VCMQMSettingsCallback* videoQMSettings) OVERRIDE {
165 return sender_->RegisterVideoQMCallback(videoQMSettings);
166 }
167
RegisterProtectionCallback(VCMProtectionCallback * protection)168 virtual int32_t RegisterProtectionCallback(
169 VCMProtectionCallback* protection) OVERRIDE {
170 return sender_->RegisterProtectionCallback(protection);
171 }
172
SetVideoProtection(VCMVideoProtection videoProtection,bool enable)173 virtual int32_t SetVideoProtection(VCMVideoProtection videoProtection,
174 bool enable) OVERRIDE {
175 int32_t sender_return =
176 sender_->SetVideoProtection(videoProtection, enable);
177 int32_t receiver_return =
178 receiver_->SetVideoProtection(videoProtection, enable);
179 if (sender_return == VCM_OK)
180 return receiver_return;
181 return sender_return;
182 }
183
AddVideoFrame(const I420VideoFrame & videoFrame,const VideoContentMetrics * contentMetrics,const CodecSpecificInfo * codecSpecificInfo)184 virtual int32_t AddVideoFrame(
185 const I420VideoFrame& videoFrame,
186 const VideoContentMetrics* contentMetrics,
187 const CodecSpecificInfo* codecSpecificInfo) OVERRIDE {
188 return sender_->AddVideoFrame(
189 videoFrame, contentMetrics, codecSpecificInfo);
190 }
191
IntraFrameRequest(int stream_index)192 virtual int32_t IntraFrameRequest(int stream_index) OVERRIDE {
193 return sender_->IntraFrameRequest(stream_index);
194 }
195
EnableFrameDropper(bool enable)196 virtual int32_t EnableFrameDropper(bool enable) OVERRIDE {
197 return sender_->EnableFrameDropper(enable);
198 }
199
SentFrameCount(VCMFrameCount & frameCount) const200 virtual int32_t SentFrameCount(VCMFrameCount& frameCount) const OVERRIDE {
201 return sender_->SentFrameCount(&frameCount);
202 }
203
SetSenderNackMode(SenderNackMode mode)204 virtual int SetSenderNackMode(SenderNackMode mode) OVERRIDE {
205 return sender_->SetSenderNackMode(mode);
206 }
207
SetSenderReferenceSelection(bool enable)208 virtual int SetSenderReferenceSelection(bool enable) OVERRIDE {
209 return sender_->SetSenderReferenceSelection(enable);
210 }
211
SetSenderFEC(bool enable)212 virtual int SetSenderFEC(bool enable) OVERRIDE {
213 return sender_->SetSenderFEC(enable);
214 }
215
SetSenderKeyFramePeriod(int periodMs)216 virtual int SetSenderKeyFramePeriod(int periodMs) OVERRIDE {
217 return sender_->SetSenderKeyFramePeriod(periodMs);
218 }
219
StartDebugRecording(const char * file_name_utf8)220 virtual int StartDebugRecording(const char* file_name_utf8) OVERRIDE {
221 return sender_->StartDebugRecording(file_name_utf8);
222 }
223
StopDebugRecording()224 virtual int StopDebugRecording() OVERRIDE {
225 sender_->StopDebugRecording();
226 return VCM_OK;
227 }
228
SuspendBelowMinBitrate()229 virtual void SuspendBelowMinBitrate() OVERRIDE {
230 return sender_->SuspendBelowMinBitrate();
231 }
232
VideoSuspended() const233 virtual bool VideoSuspended() const OVERRIDE {
234 return sender_->VideoSuspended();
235 }
236
InitializeReceiver()237 virtual int32_t InitializeReceiver() OVERRIDE {
238 return receiver_->InitializeReceiver();
239 }
240
RegisterReceiveCodec(const VideoCodec * receiveCodec,int32_t numberOfCores,bool requireKeyFrame)241 virtual int32_t RegisterReceiveCodec(const VideoCodec* receiveCodec,
242 int32_t numberOfCores,
243 bool requireKeyFrame) OVERRIDE {
244 return receiver_->RegisterReceiveCodec(
245 receiveCodec, numberOfCores, requireKeyFrame);
246 }
247
RegisterExternalDecoder(VideoDecoder * externalDecoder,uint8_t payloadType,bool internalRenderTiming)248 virtual int32_t RegisterExternalDecoder(VideoDecoder* externalDecoder,
249 uint8_t payloadType,
250 bool internalRenderTiming) OVERRIDE {
251 return receiver_->RegisterExternalDecoder(
252 externalDecoder, payloadType, internalRenderTiming);
253 }
254
RegisterReceiveCallback(VCMReceiveCallback * receiveCallback)255 virtual int32_t RegisterReceiveCallback(
256 VCMReceiveCallback* receiveCallback) OVERRIDE {
257 return receiver_->RegisterReceiveCallback(receiveCallback);
258 }
259
RegisterReceiveStatisticsCallback(VCMReceiveStatisticsCallback * receiveStats)260 virtual int32_t RegisterReceiveStatisticsCallback(
261 VCMReceiveStatisticsCallback* receiveStats) OVERRIDE {
262 return receiver_->RegisterReceiveStatisticsCallback(receiveStats);
263 }
264
RegisterDecoderTimingCallback(VCMDecoderTimingCallback * decoderTiming)265 virtual int32_t RegisterDecoderTimingCallback(
266 VCMDecoderTimingCallback* decoderTiming) OVERRIDE {
267 return receiver_->RegisterDecoderTimingCallback(decoderTiming);
268 }
269
RegisterFrameTypeCallback(VCMFrameTypeCallback * frameTypeCallback)270 virtual int32_t RegisterFrameTypeCallback(
271 VCMFrameTypeCallback* frameTypeCallback) OVERRIDE {
272 return receiver_->RegisterFrameTypeCallback(frameTypeCallback);
273 }
274
RegisterPacketRequestCallback(VCMPacketRequestCallback * callback)275 virtual int32_t RegisterPacketRequestCallback(
276 VCMPacketRequestCallback* callback) OVERRIDE {
277 return receiver_->RegisterPacketRequestCallback(callback);
278 }
279
RegisterRenderBufferSizeCallback(VCMRenderBufferSizeCallback * callback)280 virtual int RegisterRenderBufferSizeCallback(
281 VCMRenderBufferSizeCallback* callback) OVERRIDE {
282 return receiver_->RegisterRenderBufferSizeCallback(callback);
283 }
284
Decode(uint16_t maxWaitTimeMs)285 virtual int32_t Decode(uint16_t maxWaitTimeMs) OVERRIDE {
286 return receiver_->Decode(maxWaitTimeMs);
287 }
288
DecodeDualFrame(uint16_t maxWaitTimeMs)289 virtual int32_t DecodeDualFrame(uint16_t maxWaitTimeMs) OVERRIDE {
290 return receiver_->DecodeDualFrame(maxWaitTimeMs);
291 }
292
ResetDecoder()293 virtual int32_t ResetDecoder() OVERRIDE { return receiver_->ResetDecoder(); }
294
ReceiveCodec(VideoCodec * currentReceiveCodec) const295 virtual int32_t ReceiveCodec(VideoCodec* currentReceiveCodec) const OVERRIDE {
296 return receiver_->ReceiveCodec(currentReceiveCodec);
297 }
298
ReceiveCodec() const299 virtual VideoCodecType ReceiveCodec() const OVERRIDE {
300 return receiver_->ReceiveCodec();
301 }
302
IncomingPacket(const uint8_t * incomingPayload,uint32_t payloadLength,const WebRtcRTPHeader & rtpInfo)303 virtual int32_t IncomingPacket(const uint8_t* incomingPayload,
304 uint32_t payloadLength,
305 const WebRtcRTPHeader& rtpInfo) OVERRIDE {
306 return receiver_->IncomingPacket(incomingPayload, payloadLength, rtpInfo);
307 }
308
SetMinimumPlayoutDelay(uint32_t minPlayoutDelayMs)309 virtual int32_t SetMinimumPlayoutDelay(uint32_t minPlayoutDelayMs) OVERRIDE {
310 return receiver_->SetMinimumPlayoutDelay(minPlayoutDelayMs);
311 }
312
SetRenderDelay(uint32_t timeMS)313 virtual int32_t SetRenderDelay(uint32_t timeMS) OVERRIDE {
314 return receiver_->SetRenderDelay(timeMS);
315 }
316
Delay() const317 virtual int32_t Delay() const OVERRIDE { return receiver_->Delay(); }
318
ReceivedFrameCount(VCMFrameCount & frameCount) const319 virtual int32_t ReceivedFrameCount(VCMFrameCount& frameCount) const OVERRIDE {
320 return receiver_->ReceivedFrameCount(&frameCount);
321 }
322
DiscardedPackets() const323 virtual uint32_t DiscardedPackets() const OVERRIDE {
324 return receiver_->DiscardedPackets();
325 }
326
SetReceiverRobustnessMode(ReceiverRobustness robustnessMode,VCMDecodeErrorMode errorMode)327 virtual int SetReceiverRobustnessMode(ReceiverRobustness robustnessMode,
328 VCMDecodeErrorMode errorMode) OVERRIDE {
329 return receiver_->SetReceiverRobustnessMode(robustnessMode, errorMode);
330 }
331
SetNackSettings(size_t max_nack_list_size,int max_packet_age_to_nack,int max_incomplete_time_ms)332 virtual void SetNackSettings(size_t max_nack_list_size,
333 int max_packet_age_to_nack,
334 int max_incomplete_time_ms) OVERRIDE {
335 return receiver_->SetNackSettings(
336 max_nack_list_size, max_packet_age_to_nack, max_incomplete_time_ms);
337 }
338
SetDecodeErrorMode(VCMDecodeErrorMode decode_error_mode)339 void SetDecodeErrorMode(VCMDecodeErrorMode decode_error_mode) OVERRIDE {
340 return receiver_->SetDecodeErrorMode(decode_error_mode);
341 }
342
SetMinReceiverDelay(int desired_delay_ms)343 virtual int SetMinReceiverDelay(int desired_delay_ms) OVERRIDE {
344 return receiver_->SetMinReceiverDelay(desired_delay_ms);
345 }
346
SetReceiveChannelParameters(uint32_t rtt)347 virtual int32_t SetReceiveChannelParameters(uint32_t rtt) OVERRIDE {
348 return receiver_->SetReceiveChannelParameters(rtt);
349 }
350
RegisterPreDecodeImageCallback(EncodedImageCallback * observer)351 virtual void RegisterPreDecodeImageCallback(
352 EncodedImageCallback* observer) OVERRIDE {
353 receiver_->RegisterPreDecodeImageCallback(observer);
354 }
355
RegisterPostEncodeImageCallback(EncodedImageCallback * observer)356 virtual void RegisterPostEncodeImageCallback(
357 EncodedImageCallback* observer) OVERRIDE {
358 post_encode_callback_.Register(observer);
359 }
360
361 private:
362 EncodedImageCallbackWrapper post_encode_callback_;
363 scoped_ptr<vcm::VideoSender> sender_;
364 scoped_ptr<vcm::VideoReceiver> receiver_;
365 scoped_ptr<EventFactory> own_event_factory_;
366 };
367 } // namespace
368
NumberOfCodecs()369 uint8_t VideoCodingModule::NumberOfCodecs() {
370 return VCMCodecDataBase::NumberOfCodecs();
371 }
372
Codec(uint8_t listId,VideoCodec * codec)373 int32_t VideoCodingModule::Codec(uint8_t listId, VideoCodec* codec) {
374 if (codec == NULL) {
375 return VCM_PARAMETER_ERROR;
376 }
377 return VCMCodecDataBase::Codec(listId, codec) ? 0 : -1;
378 }
379
Codec(VideoCodecType codecType,VideoCodec * codec)380 int32_t VideoCodingModule::Codec(VideoCodecType codecType, VideoCodec* codec) {
381 if (codec == NULL) {
382 return VCM_PARAMETER_ERROR;
383 }
384 return VCMCodecDataBase::Codec(codecType, codec) ? 0 : -1;
385 }
386
Create()387 VideoCodingModule* VideoCodingModule::Create() {
388 return new VideoCodingModuleImpl(
389 Clock::GetRealTimeClock(), new EventFactoryImpl, true);
390 }
391
Create(Clock * clock,EventFactory * event_factory)392 VideoCodingModule* VideoCodingModule::Create(Clock* clock,
393 EventFactory* event_factory) {
394 assert(clock);
395 assert(event_factory);
396 return new VideoCodingModuleImpl(clock, event_factory, false);
397 }
398
Destroy(VideoCodingModule * module)399 void VideoCodingModule::Destroy(VideoCodingModule* module) {
400 if (module != NULL) {
401 delete static_cast<VideoCodingModuleImpl*>(module);
402 }
403 }
404 } // namespace webrtc
405