OLD | NEW |
---|---|
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "content/renderer/media/video_track_recorder.h" | 5 #include "content/renderer/media/video_track_recorder.h" |
6 | 6 |
7 #include <utility> | 7 #include <utility> |
8 | 8 |
9 #include "base/bind.h" | 9 #include "base/bind.h" |
10 #include "base/logging.h" | 10 #include "base/logging.h" |
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
53 | 53 |
54 namespace { | 54 namespace { |
55 | 55 |
56 // HW encoders expect a nonzero bitrate, so |kVEADefaultBitratePerPixel| is used | 56 // HW encoders expect a nonzero bitrate, so |kVEADefaultBitratePerPixel| is used |
57 // to estimate bits per second for ~30 fps with ~1/16 compression rate. | 57 // to estimate bits per second for ~30 fps with ~1/16 compression rate. |
58 const int kVEADefaultBitratePerPixel = 2; | 58 const int kVEADefaultBitratePerPixel = 2; |
59 // Number of output buffers used to copy the encoded data coming from HW | 59 // Number of output buffers used to copy the encoded data coming from HW |
60 // encoders. | 60 // encoders. |
61 const int kVEAEncoderOutputBufferCount = 4; | 61 const int kVEAEncoderOutputBufferCount = 4; |
62 | 62 |
63 static struct { | 63 using CodecId = VideoTrackRecorder::CodecId; |
64 VideoTrackRecorder::CodecId codec_id; | |
65 media::VideoCodecProfile min_profile; | |
66 media::VideoCodecProfile max_profile; | |
67 } const kSupportedVideoCodecIdToProfile[] = { | |
68 {VideoTrackRecorder::CodecId::VP8, | |
69 media::VP8PROFILE_MIN, | |
70 media::VP8PROFILE_MAX}, | |
71 {VideoTrackRecorder::CodecId::VP9, | |
72 media::VP9PROFILE_MIN, | |
73 media::VP9PROFILE_MAX}, | |
74 {VideoTrackRecorder::CodecId::H264, | |
75 media::H264PROFILE_MIN, | |
76 media::H264PROFILE_MAX}}; | |
77 | 64 |
78 // Returns the corresponding codec profile from VEA supported codecs. If no | 65 // Class to encapsulate the enumeration of CodecIds/VideoCodecProfiles supported |
79 // profile is found, returns VIDEO_CODEC_PROFILE_UNKNOWN. | 66 // by the VEA underlying platform. Provides methods to query the preferred |
80 media::VideoCodecProfile CodecIdToVEAProfile( | 67 // CodecId and to check if a given CodecId is supported. |
81 content::VideoTrackRecorder::CodecId codec) { | 68 class CodecEnumerator { |
69 public: | |
70 CodecEnumerator(); | |
71 ~CodecEnumerator() = default; | |
72 | |
73 // Get the first CodecId that has an associated VEA VideoCodecProfile, or VP8. | |
74 CodecId GetPreferredCodecId(); | |
75 | |
76 // Returns the VEA VideoCodedProfile for a given CodecId, if supported, or | |
77 // VIDEO_CODEC_PROFILE_UNKNOWN otherwise. | |
78 media::VideoCodecProfile CodecIdToVEAProfile(CodecId codec); | |
79 | |
80 private: | |
81 static const struct CodecIdAndVEAProfile { | |
82 CodecId codec_id; | |
83 media::VideoCodecProfile min_profile; | |
84 media::VideoCodecProfile max_profile; | |
85 } constexpr kCodecIdAndVEAProfiles[] = { | |
86 {CodecId::VP8, media::VP8PROFILE_MIN, media::VP8PROFILE_MAX}, | |
87 {CodecId::VP9, media::VP9PROFILE_MIN, media::VP9PROFILE_MAX}, | |
88 {CodecId::H264, media::H264PROFILE_MIN, media::H264PROFILE_MAX}}; | |
89 | |
90 // A map of VEA-supported CodecId-and-VEA-profile pairs. | |
91 std::map<CodecId, media::VideoCodecProfile> codec_id_to_profile_; | |
92 | |
93 DISALLOW_COPY_AND_ASSIGN(CodecEnumerator); | |
94 }; | |
95 | |
96 constexpr CodecEnumerator::CodecIdAndVEAProfile | |
97 CodecEnumerator::kCodecIdAndVEAProfiles[]; | |
98 | |
99 static base::LazyInstance<CodecEnumerator>::Leaky g_codec_enumerator = | |
100 LAZY_INSTANCE_INITIALIZER; | |
101 | |
102 CodecEnumerator::CodecEnumerator() { | |
103 static_assert( | |
104 arraysize(kCodecIdAndVEAProfiles) == static_cast<int>(CodecId::LAST), | |
105 "|kCodecIdAndVEAProfiles| should consider all CodecIds"); | |
106 #if defined(OS_CHROMEOS) | |
82 // See https://crbug.com/616659. | 107 // See https://crbug.com/616659. |
83 #if defined(OS_CHROMEOS) | 108 return; |
84 return media::VIDEO_CODEC_PROFILE_UNKNOWN; | 109 #endif |
85 #endif // defined(OS_CHROMEOS) | |
86 | 110 |
87 // See https://crbug.com/653864. | |
88 #if defined(OS_ANDROID) | 111 #if defined(OS_ANDROID) |
89 return media::VIDEO_CODEC_PROFILE_UNKNOWN; | 112 // See https://crbug.com/653864. |
90 #endif // defined(OS_ANDROID) | 113 return; |
114 #endif | |
91 | 115 |
92 content::RenderThreadImpl* const render_thread_impl = | 116 content::RenderThreadImpl* const render_thread_impl = |
93 content::RenderThreadImpl::current(); | 117 content::RenderThreadImpl::current(); |
94 if (!render_thread_impl) { | 118 if (!render_thread_impl) { |
95 DVLOG(3) << "Couldn't access the render thread"; | 119 DVLOG(2) << "Couldn't access the render thread"; |
96 return media::VIDEO_CODEC_PROFILE_UNKNOWN; | 120 return; |
97 } | 121 } |
98 | 122 |
99 media::GpuVideoAcceleratorFactories* const gpu_factories = | 123 media::GpuVideoAcceleratorFactories* const gpu_factories = |
100 render_thread_impl->GetGpuFactories(); | 124 render_thread_impl->GetGpuFactories(); |
101 if (!gpu_factories || !gpu_factories->IsGpuVideoAcceleratorEnabled()) { | 125 if (!gpu_factories || !gpu_factories->IsGpuVideoAcceleratorEnabled()) { |
102 DVLOG(3) << "Couldn't initialize GpuVideoAcceleratorFactories"; | 126 DVLOG(2) << "Couldn't initialize GpuVideoAcceleratorFactories"; |
103 return media::VIDEO_CODEC_PROFILE_UNKNOWN; | 127 return; |
104 } | 128 } |
105 | 129 |
106 const media::VideoEncodeAccelerator::SupportedProfiles& vea_profiles = | 130 const media::VideoEncodeAccelerator::SupportedProfiles& vea_profiles = |
107 gpu_factories->GetVideoEncodeAcceleratorSupportedProfiles(); | 131 gpu_factories->GetVideoEncodeAcceleratorSupportedProfiles(); |
108 for (const auto& vea_profile : vea_profiles) { | 132 for (const auto& vea_profile : vea_profiles) { |
109 for (const auto& supported_profile : kSupportedVideoCodecIdToProfile) { | 133 for (auto& codec_id_and_profile : kCodecIdAndVEAProfiles) { |
110 if (codec == supported_profile.codec_id && | 134 if (vea_profile.profile >= codec_id_and_profile.min_profile && |
111 vea_profile.profile >= supported_profile.min_profile && | 135 vea_profile.profile <= codec_id_and_profile.max_profile) { |
112 vea_profile.profile <= supported_profile.max_profile) | 136 DVLOG(2) << media::GetProfileName(vea_profile.profile) << " supported"; |
113 return vea_profile.profile; | 137 codec_id_to_profile_.emplace( |
138 std::make_pair(codec_id_and_profile.codec_id, vea_profile.profile)); | |
emircan
2017/01/11 22:11:05
codec_id_to_profile_.emplace(codec_id_and_profile.
emircan
2017/01/11 22:11:05
Note that this would take the min profile if there
mcasas
2017/01/11 22:30:16
Done.
mcasas
2017/01/11 22:30:16
Gotcha. That's ok for the time being.
We'll be kee
mcasas
2017/01/12 00:20:54
Some bots were unhappy, see e.g. linux_chromium_co
| |
139 } | |
114 } | 140 } |
115 } | 141 } |
116 return media::VIDEO_CODEC_PROFILE_UNKNOWN; | 142 } |
143 | |
144 CodecId CodecEnumerator::GetPreferredCodecId() { | |
145 if (codec_id_to_profile_.empty()) | |
146 return CodecId::VP8; | |
147 return codec_id_to_profile_.begin()->first; | |
148 } | |
149 | |
150 media::VideoCodecProfile CodecEnumerator::CodecIdToVEAProfile(CodecId codec) { | |
151 const auto profile = codec_id_to_profile_.find(codec); | |
152 return profile == codec_id_to_profile_.end() | |
153 ? media::VIDEO_CODEC_PROFILE_UNKNOWN | |
154 : profile->second; | |
117 } | 155 } |
118 | 156 |
119 } // anonymous namespace | 157 } // anonymous namespace |
120 | 158 |
121 // Base class to describe a generic Encoder, encapsulating all actual encoder | 159 // Base class to describe a generic Encoder, encapsulating all actual encoder |
122 // (re)configurations, encoding and delivery of received frames. This class is | 160 // (re)configurations, encoding and delivery of received frames. This class is |
123 // ref-counted to allow the MediaStreamVideoTrack to hold a reference to it (via | 161 // ref-counted to allow the MediaStreamVideoTrack to hold a reference to it (via |
124 // the callback that MediaStreamVideoSink passes along) and to jump back and | 162 // the callback that MediaStreamVideoSink passes along) and to jump back and |
125 // forth to an internal encoder thread. Moreover, this class: | 163 // forth to an internal encoder thread. Moreover, this class: |
126 // - is created on its parent's thread (usually the main Render thread), | 164 // - is created on its parent's thread (usually the main Render thread), |
(...skipping 941 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1068 return; | 1106 return; |
1069 } | 1107 } |
1070 | 1108 |
1071 int pixel_format = EVideoFormatType::videoFormatI420; | 1109 int pixel_format = EVideoFormatType::videoFormatI420; |
1072 openh264_encoder_->SetOption(ENCODER_OPTION_DATAFORMAT, &pixel_format); | 1110 openh264_encoder_->SetOption(ENCODER_OPTION_DATAFORMAT, &pixel_format); |
1073 } | 1111 } |
1074 #endif //#if BUILDFLAG(RTC_USE_H264) | 1112 #endif //#if BUILDFLAG(RTC_USE_H264) |
1075 | 1113 |
1076 } // anonymous namespace | 1114 } // anonymous namespace |
1077 | 1115 |
1116 // static | |
1117 VideoTrackRecorder::CodecId VideoTrackRecorder::GetPreferredCodecId() { | |
1118 return g_codec_enumerator.Get().GetPreferredCodecId(); | |
1119 } | |
1120 | |
1078 VideoTrackRecorder::VideoTrackRecorder( | 1121 VideoTrackRecorder::VideoTrackRecorder( |
1079 CodecId codec, | 1122 CodecId codec, |
1080 const blink::WebMediaStreamTrack& track, | 1123 const blink::WebMediaStreamTrack& track, |
1081 const OnEncodedVideoCB& on_encoded_video_callback, | 1124 const OnEncodedVideoCB& on_encoded_video_callback, |
1082 int32_t bits_per_second) | 1125 int32_t bits_per_second) |
1083 : track_(track), | 1126 : track_(track), |
1084 paused_before_init_(false), | 1127 paused_before_init_(false), |
1085 weak_ptr_factory_(this) { | 1128 weak_ptr_factory_(this) { |
1086 DCHECK(main_render_thread_checker_.CalledOnValidThread()); | 1129 DCHECK(main_render_thread_checker_.CalledOnValidThread()); |
1087 DCHECK(!track_.isNull()); | 1130 DCHECK(!track_.isNull()); |
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1136 const OnEncodedVideoCB& on_encoded_video_callback, | 1179 const OnEncodedVideoCB& on_encoded_video_callback, |
1137 int32_t bits_per_second, | 1180 int32_t bits_per_second, |
1138 const scoped_refptr<media::VideoFrame>& frame, | 1181 const scoped_refptr<media::VideoFrame>& frame, |
1139 base::TimeTicks capture_time) { | 1182 base::TimeTicks capture_time) { |
1140 DVLOG(3) << __func__ << frame->visible_rect().size().ToString(); | 1183 DVLOG(3) << __func__ << frame->visible_rect().size().ToString(); |
1141 DCHECK(main_render_thread_checker_.CalledOnValidThread()); | 1184 DCHECK(main_render_thread_checker_.CalledOnValidThread()); |
1142 | 1185 |
1143 MediaStreamVideoSink::DisconnectFromTrack(); | 1186 MediaStreamVideoSink::DisconnectFromTrack(); |
1144 | 1187 |
1145 const gfx::Size& input_size = frame->visible_rect().size(); | 1188 const gfx::Size& input_size = frame->visible_rect().size(); |
1146 const auto& vea_supported_profile = CodecIdToVEAProfile(codec); | 1189 const auto& vea_supported_profile = |
1190 g_codec_enumerator.Get().CodecIdToVEAProfile(codec); | |
1147 if (vea_supported_profile != media::VIDEO_CODEC_PROFILE_UNKNOWN && | 1191 if (vea_supported_profile != media::VIDEO_CODEC_PROFILE_UNKNOWN && |
1148 input_size.width() >= kVEAEncoderMinResolutionWidth && | 1192 input_size.width() >= kVEAEncoderMinResolutionWidth && |
1149 input_size.height() >= kVEAEncoderMinResolutionHeight) { | 1193 input_size.height() >= kVEAEncoderMinResolutionHeight) { |
1150 encoder_ = new VEAEncoder(on_encoded_video_callback, bits_per_second, | 1194 encoder_ = new VEAEncoder(on_encoded_video_callback, bits_per_second, |
1151 vea_supported_profile, input_size); | 1195 vea_supported_profile, input_size); |
1152 } else { | 1196 } else { |
1153 switch (codec) { | 1197 switch (codec) { |
1154 #if BUILDFLAG(RTC_USE_H264) | 1198 #if BUILDFLAG(RTC_USE_H264) |
1155 case CodecId::H264: | 1199 case CodecId::H264: |
1156 encoder_ = | 1200 encoder_ = |
1157 new H264Encoder(on_encoded_video_callback, bits_per_second); | 1201 new H264Encoder(on_encoded_video_callback, bits_per_second); |
1158 break; | 1202 break; |
1159 #endif | 1203 #endif |
1160 case CodecId::VP8: | 1204 case CodecId::VP8: |
1161 case CodecId::VP9: | 1205 case CodecId::VP9: |
1162 encoder_ = new VpxEncoder(codec == CodecId::VP9, | 1206 encoder_ = new VpxEncoder(codec == CodecId::VP9, |
1163 on_encoded_video_callback, bits_per_second); | 1207 on_encoded_video_callback, bits_per_second); |
1164 break; | 1208 break; |
1165 default: | 1209 case CodecId::LAST: |
1166 NOTREACHED() << "Unsupported codec"; | 1210 NOTREACHED() << "Unsupported codec"; |
1167 } | 1211 } |
1168 } | 1212 } |
1169 | 1213 |
1170 if (paused_before_init_) | 1214 if (paused_before_init_) |
1171 encoder_->SetPaused(paused_before_init_); | 1215 encoder_->SetPaused(paused_before_init_); |
1172 | 1216 |
1173 // StartFrameEncode() will be called on Render IO thread. | 1217 // StartFrameEncode() will be called on Render IO thread. |
1174 MediaStreamVideoSink::ConnectToTrack( | 1218 MediaStreamVideoSink::ConnectToTrack( |
1175 track_, | 1219 track_, |
1176 base::Bind(&VideoTrackRecorder::Encoder::StartFrameEncode, encoder_), | 1220 base::Bind(&VideoTrackRecorder::Encoder::StartFrameEncode, encoder_), |
1177 false); | 1221 false); |
1178 } | 1222 } |
1179 | 1223 |
1180 } // namespace content | 1224 } // namespace content |
OLD | NEW |