OLD | NEW |
1 /* | 1 /* |
2 * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved. | 2 * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license | 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 | 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 | 6 * tree. An additional intellectual property rights grant can be found |
7 * in the file PATENTS. All contributing project authors may | 7 * in the file PATENTS. All contributing project authors may |
8 * be found in the AUTHORS file in the root of the source tree. | 8 * be found in the AUTHORS file in the root of the source tree. |
9 */ | 9 */ |
10 | 10 |
11 #include "webrtc/modules/video_coding/generic_encoder.h" | 11 #include "webrtc/modules/video_coding/generic_encoder.h" |
12 | 12 |
13 #include <vector> | 13 #include <vector> |
14 | 14 |
15 #include "webrtc/base/checks.h" | 15 #include "webrtc/base/checks.h" |
16 #include "webrtc/base/logging.h" | 16 #include "webrtc/base/logging.h" |
17 #include "webrtc/base/trace_event.h" | 17 #include "webrtc/base/trace_event.h" |
18 #include "webrtc/engine_configurations.h" | 18 #include "webrtc/engine_configurations.h" |
19 #include "webrtc/modules/video_coding/encoded_frame.h" | 19 #include "webrtc/modules/video_coding/encoded_frame.h" |
20 #include "webrtc/modules/video_coding/media_optimization.h" | 20 #include "webrtc/modules/video_coding/media_optimization.h" |
21 #include "webrtc/system_wrappers/include/critical_section_wrapper.h" | 21 #include "webrtc/system_wrappers/include/critical_section_wrapper.h" |
22 | 22 |
23 namespace webrtc { | 23 namespace webrtc { |
24 namespace { | |
25 // Map information from info into rtp. If no relevant information is found | |
26 // in info, rtp is set to NULL. | |
27 void CopyCodecSpecific(const CodecSpecificInfo* info, RTPVideoHeader* rtp) { | |
28 RTC_DCHECK(info); | |
29 switch (info->codecType) { | |
30 case kVideoCodecVP8: { | |
31 rtp->codec = kRtpVideoVp8; | |
32 rtp->codecHeader.VP8.InitRTPVideoHeaderVP8(); | |
33 rtp->codecHeader.VP8.pictureId = info->codecSpecific.VP8.pictureId; | |
34 rtp->codecHeader.VP8.nonReference = info->codecSpecific.VP8.nonReference; | |
35 rtp->codecHeader.VP8.temporalIdx = info->codecSpecific.VP8.temporalIdx; | |
36 rtp->codecHeader.VP8.layerSync = info->codecSpecific.VP8.layerSync; | |
37 rtp->codecHeader.VP8.tl0PicIdx = info->codecSpecific.VP8.tl0PicIdx; | |
38 rtp->codecHeader.VP8.keyIdx = info->codecSpecific.VP8.keyIdx; | |
39 rtp->simulcastIdx = info->codecSpecific.VP8.simulcastIdx; | |
40 return; | |
41 } | |
42 case kVideoCodecVP9: { | |
43 rtp->codec = kRtpVideoVp9; | |
44 rtp->codecHeader.VP9.InitRTPVideoHeaderVP9(); | |
45 rtp->codecHeader.VP9.inter_pic_predicted = | |
46 info->codecSpecific.VP9.inter_pic_predicted; | |
47 rtp->codecHeader.VP9.flexible_mode = | |
48 info->codecSpecific.VP9.flexible_mode; | |
49 rtp->codecHeader.VP9.ss_data_available = | |
50 info->codecSpecific.VP9.ss_data_available; | |
51 rtp->codecHeader.VP9.picture_id = info->codecSpecific.VP9.picture_id; | |
52 rtp->codecHeader.VP9.tl0_pic_idx = info->codecSpecific.VP9.tl0_pic_idx; | |
53 rtp->codecHeader.VP9.temporal_idx = info->codecSpecific.VP9.temporal_idx; | |
54 rtp->codecHeader.VP9.spatial_idx = info->codecSpecific.VP9.spatial_idx; | |
55 rtp->codecHeader.VP9.temporal_up_switch = | |
56 info->codecSpecific.VP9.temporal_up_switch; | |
57 rtp->codecHeader.VP9.inter_layer_predicted = | |
58 info->codecSpecific.VP9.inter_layer_predicted; | |
59 rtp->codecHeader.VP9.gof_idx = info->codecSpecific.VP9.gof_idx; | |
60 rtp->codecHeader.VP9.num_spatial_layers = | |
61 info->codecSpecific.VP9.num_spatial_layers; | |
62 | |
63 if (info->codecSpecific.VP9.ss_data_available) { | |
64 rtp->codecHeader.VP9.spatial_layer_resolution_present = | |
65 info->codecSpecific.VP9.spatial_layer_resolution_present; | |
66 if (info->codecSpecific.VP9.spatial_layer_resolution_present) { | |
67 for (size_t i = 0; i < info->codecSpecific.VP9.num_spatial_layers; | |
68 ++i) { | |
69 rtp->codecHeader.VP9.width[i] = info->codecSpecific.VP9.width[i]; | |
70 rtp->codecHeader.VP9.height[i] = info->codecSpecific.VP9.height[i]; | |
71 } | |
72 } | |
73 rtp->codecHeader.VP9.gof.CopyGofInfoVP9(info->codecSpecific.VP9.gof); | |
74 } | |
75 | |
76 rtp->codecHeader.VP9.num_ref_pics = info->codecSpecific.VP9.num_ref_pics; | |
77 for (int i = 0; i < info->codecSpecific.VP9.num_ref_pics; ++i) | |
78 rtp->codecHeader.VP9.pid_diff[i] = info->codecSpecific.VP9.p_diff[i]; | |
79 return; | |
80 } | |
81 case kVideoCodecH264: | |
82 rtp->codec = kRtpVideoH264; | |
83 return; | |
84 case kVideoCodecGeneric: | |
85 rtp->codec = kRtpVideoGeneric; | |
86 rtp->simulcastIdx = info->codecSpecific.generic.simulcast_idx; | |
87 return; | |
88 default: | |
89 return; | |
90 } | |
91 } | |
92 } // namespace | |
93 | |
94 VCMGenericEncoder::VCMGenericEncoder( | 24 VCMGenericEncoder::VCMGenericEncoder( |
95 VideoEncoder* encoder, | 25 VideoEncoder* encoder, |
96 VideoEncoderRateObserver* rate_observer, | 26 VideoEncoderRateObserver* rate_observer, |
97 VCMEncodedFrameCallback* encoded_frame_callback, | 27 VCMEncodedFrameCallback* encoded_frame_callback, |
98 bool internal_source) | 28 bool internal_source) |
99 : encoder_(encoder), | 29 : encoder_(encoder), |
100 rate_observer_(rate_observer), | 30 rate_observer_(rate_observer), |
101 vcm_encoded_frame_callback_(encoded_frame_callback), | 31 vcm_encoded_frame_callback_(encoded_frame_callback), |
102 internal_source_(internal_source), | 32 internal_source_(internal_source), |
103 encoder_params_({0, 0, 0, 0}), | 33 encoder_params_({0, 0, 0, 0}), |
(...skipping 115 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
219 } | 149 } |
220 | 150 |
221 int VCMGenericEncoder::GetTargetFramerate() { | 151 int VCMGenericEncoder::GetTargetFramerate() { |
222 return encoder_->GetTargetFramerate(); | 152 return encoder_->GetTargetFramerate(); |
223 } | 153 } |
224 | 154 |
225 VCMEncodedFrameCallback::VCMEncodedFrameCallback( | 155 VCMEncodedFrameCallback::VCMEncodedFrameCallback( |
226 EncodedImageCallback* post_encode_callback) | 156 EncodedImageCallback* post_encode_callback) |
227 : send_callback_(), | 157 : send_callback_(), |
228 media_opt_(nullptr), | 158 media_opt_(nullptr), |
229 payload_type_(0), | |
230 internal_source_(false), | 159 internal_source_(false), |
231 rotation_(kVideoRotation_0), | 160 rotation_(kVideoRotation_0), |
232 post_encode_callback_(post_encode_callback) {} | 161 post_encode_callback_(post_encode_callback) {} |
233 | 162 |
234 VCMEncodedFrameCallback::~VCMEncodedFrameCallback() {} | 163 VCMEncodedFrameCallback::~VCMEncodedFrameCallback() {} |
235 | 164 |
236 int32_t VCMEncodedFrameCallback::SetTransportCallback( | 165 int32_t VCMEncodedFrameCallback::SetTransportCallback( |
237 VCMPacketizationCallback* transport) { | 166 VCMPacketizationCallback* transport) { |
238 send_callback_ = transport; | 167 send_callback_ = transport; |
239 return VCM_OK; | 168 return VCM_OK; |
240 } | 169 } |
241 | 170 |
242 int32_t VCMEncodedFrameCallback::Encoded( | 171 int32_t VCMEncodedFrameCallback::Encoded( |
243 const EncodedImage& encoded_image, | 172 const EncodedImage& encoded_image, |
244 const CodecSpecificInfo* codec_specific, | 173 const CodecSpecificInfo* codec_specific, |
245 const RTPFragmentationHeader* fragmentation_header) { | 174 const RTPFragmentationHeader* fragmentation_header) { |
246 TRACE_EVENT_INSTANT1("webrtc", "VCMEncodedFrameCallback::Encoded", | 175 TRACE_EVENT_INSTANT1("webrtc", "VCMEncodedFrameCallback::Encoded", |
247 "timestamp", encoded_image._timeStamp); | 176 "timestamp", encoded_image._timeStamp); |
248 post_encode_callback_->Encoded(encoded_image, nullptr, nullptr); | 177 int ret_val = post_encode_callback_->Encoded(encoded_image, codec_specific, |
249 | 178 fragmentation_header); |
250 if (send_callback_ == nullptr) | |
251 return VCM_UNINITIALIZED; | |
252 | |
253 RTPVideoHeader rtp_video_header; | |
254 memset(&rtp_video_header, 0, sizeof(RTPVideoHeader)); | |
255 if (codec_specific) | |
256 CopyCodecSpecific(codec_specific, &rtp_video_header); | |
257 rtp_video_header.rotation = rotation_; | |
258 | |
259 int32_t ret_val = send_callback_->SendData( | |
260 payload_type_, encoded_image, fragmentation_header, &rtp_video_header); | |
261 if (ret_val < 0) | 179 if (ret_val < 0) |
262 return ret_val; | 180 return ret_val; |
263 | 181 |
264 if (media_opt_) { | 182 if (media_opt_) { |
265 media_opt_->UpdateWithEncodedData(encoded_image); | 183 media_opt_->UpdateWithEncodedData(encoded_image); |
266 if (internal_source_) | 184 if (internal_source_) |
267 return media_opt_->DropFrame(); // Signal to encoder to drop next frame. | 185 return media_opt_->DropFrame(); // Signal to encoder to drop next frame. |
268 } | 186 } |
269 return VCM_OK; | 187 return VCM_OK; |
270 } | 188 } |
271 | 189 |
272 void VCMEncodedFrameCallback::SetMediaOpt( | 190 void VCMEncodedFrameCallback::SetMediaOpt( |
273 media_optimization::MediaOptimization* mediaOpt) { | 191 media_optimization::MediaOptimization* mediaOpt) { |
274 media_opt_ = mediaOpt; | 192 media_opt_ = mediaOpt; |
275 } | 193 } |
276 | 194 |
277 void VCMEncodedFrameCallback::SignalLastEncoderImplementationUsed( | 195 void VCMEncodedFrameCallback::SignalLastEncoderImplementationUsed( |
278 const char* implementation_name) { | 196 const char* implementation_name) { |
279 if (send_callback_) | 197 if (send_callback_) |
280 send_callback_->OnEncoderImplementationName(implementation_name); | 198 send_callback_->OnEncoderImplementationName(implementation_name); |
281 } | 199 } |
282 } // namespace webrtc | 200 } // namespace webrtc |
OLD | NEW |