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; | 64 |
| 65 static const struct { |
| 66 CodecId codec_id; |
65 media::VideoCodecProfile min_profile; | 67 media::VideoCodecProfile min_profile; |
66 media::VideoCodecProfile max_profile; | 68 media::VideoCodecProfile max_profile; |
67 } const kSupportedVideoCodecIdToProfile[] = { | 69 } kPreferredCodecIdAndVEAProfiles[] = { |
68 {VideoTrackRecorder::CodecId::VP8, | 70 {CodecId::VP8, media::VP8PROFILE_MIN, media::VP8PROFILE_MAX}, |
69 media::VP8PROFILE_MIN, | 71 {CodecId::VP9, media::VP9PROFILE_MIN, media::VP9PROFILE_MAX}, |
70 media::VP8PROFILE_MAX}, | 72 #if BUILDFLAG(RTC_USE_H264) |
71 {VideoTrackRecorder::CodecId::VP9, | 73 {CodecId::H264, media::H264PROFILE_MIN, media::H264PROFILE_MAX} |
72 media::VP9PROFILE_MIN, | 74 #endif |
73 media::VP9PROFILE_MAX}, | 75 }; |
74 {VideoTrackRecorder::CodecId::H264, | |
75 media::H264PROFILE_MIN, | |
76 media::H264PROFILE_MAX}}; | |
77 | 76 |
78 // Returns the corresponding codec profile from VEA supported codecs. If no | 77 static_assert(arraysize(kPreferredCodecIdAndVEAProfiles) == |
79 // profile is found, returns VIDEO_CODEC_PROFILE_UNKNOWN. | 78 static_cast<int>(CodecId::LAST), |
80 media::VideoCodecProfile CodecIdToVEAProfile( | 79 "|kPreferredCodecIdAndVEAProfiles| should consider all CodecIds"); |
81 content::VideoTrackRecorder::CodecId codec) { | 80 |
| 81 // Class to encapsulate the enumeration of CodecIds/VideoCodecProfiles supported |
| 82 // by the VEA underlying platform. Provides methods to query the preferred |
| 83 // CodecId and to check if a given CodecId is supported. |
| 84 class CodecEnumerator { |
| 85 public: |
| 86 CodecEnumerator(); |
| 87 ~CodecEnumerator() = default; |
| 88 |
| 89 // Returns the first CodecId that has an associated VEA VideoCodecProfile, or |
| 90 // VP8 if none available. |
| 91 CodecId GetPreferredCodecId(); |
| 92 |
| 93 // Returns the VEA VideoCodedProfile for a given CodecId, if supported, or |
| 94 // VIDEO_CODEC_PROFILE_UNKNOWN otherwise. |
| 95 media::VideoCodecProfile CodecIdToVEAProfile(CodecId codec); |
| 96 |
| 97 private: |
| 98 // A map of VEA-supported CodecId-and-VEA-profile pairs. |
| 99 std::map<CodecId, media::VideoCodecProfile> codec_id_to_profile_; |
| 100 |
| 101 DISALLOW_COPY_AND_ASSIGN(CodecEnumerator); |
| 102 }; |
| 103 |
| 104 static base::LazyInstance<CodecEnumerator>::Leaky g_codec_enumerator = |
| 105 LAZY_INSTANCE_INITIALIZER; |
| 106 |
| 107 CodecEnumerator::CodecEnumerator() { |
| 108 #if defined(OS_CHROMEOS) |
82 // See https://crbug.com/616659. | 109 // See https://crbug.com/616659. |
83 #if defined(OS_CHROMEOS) | 110 return; |
84 return media::VIDEO_CODEC_PROFILE_UNKNOWN; | 111 #endif |
85 #endif // defined(OS_CHROMEOS) | |
86 | 112 |
87 // See https://crbug.com/653864. | |
88 #if defined(OS_ANDROID) | 113 #if defined(OS_ANDROID) |
89 return media::VIDEO_CODEC_PROFILE_UNKNOWN; | 114 // See https://crbug.com/653864. |
90 #endif // defined(OS_ANDROID) | 115 return; |
| 116 #endif |
91 | 117 |
92 content::RenderThreadImpl* const render_thread_impl = | 118 content::RenderThreadImpl* const render_thread_impl = |
93 content::RenderThreadImpl::current(); | 119 content::RenderThreadImpl::current(); |
94 if (!render_thread_impl) { | 120 if (!render_thread_impl) { |
95 DVLOG(3) << "Couldn't access the render thread"; | 121 DVLOG(2) << "Couldn't access the render thread"; |
96 return media::VIDEO_CODEC_PROFILE_UNKNOWN; | 122 return; |
97 } | 123 } |
98 | 124 |
99 media::GpuVideoAcceleratorFactories* const gpu_factories = | 125 media::GpuVideoAcceleratorFactories* const gpu_factories = |
100 render_thread_impl->GetGpuFactories(); | 126 render_thread_impl->GetGpuFactories(); |
101 if (!gpu_factories || !gpu_factories->IsGpuVideoAcceleratorEnabled()) { | 127 if (!gpu_factories || !gpu_factories->IsGpuVideoAcceleratorEnabled()) { |
102 DVLOG(3) << "Couldn't initialize GpuVideoAcceleratorFactories"; | 128 DVLOG(2) << "Couldn't initialize GpuVideoAcceleratorFactories"; |
103 return media::VIDEO_CODEC_PROFILE_UNKNOWN; | 129 return; |
104 } | 130 } |
105 | 131 |
106 const media::VideoEncodeAccelerator::SupportedProfiles& vea_profiles = | 132 const auto vea_supported_profiles = |
107 gpu_factories->GetVideoEncodeAcceleratorSupportedProfiles(); | 133 gpu_factories->GetVideoEncodeAcceleratorSupportedProfiles(); |
108 for (const auto& vea_profile : vea_profiles) { | 134 for (const auto& supported_profile : vea_supported_profiles) { |
109 for (const auto& supported_profile : kSupportedVideoCodecIdToProfile) { | 135 for (auto& codec_id_and_profile : kPreferredCodecIdAndVEAProfiles) { |
110 if (codec == supported_profile.codec_id && | 136 if (supported_profile.profile >= codec_id_and_profile.min_profile && |
111 vea_profile.profile >= supported_profile.min_profile && | 137 supported_profile.profile <= codec_id_and_profile.max_profile) { |
112 vea_profile.profile <= supported_profile.max_profile) | 138 DVLOG(2) << "Accelerated codec found: " |
113 return vea_profile.profile; | 139 << media::GetProfileName(supported_profile.profile); |
| 140 codec_id_to_profile_.insert(std::make_pair( |
| 141 codec_id_and_profile.codec_id, supported_profile.profile)); |
| 142 } |
114 } | 143 } |
115 } | 144 } |
116 return media::VIDEO_CODEC_PROFILE_UNKNOWN; | 145 } |
| 146 |
| 147 CodecId CodecEnumerator::GetPreferredCodecId() { |
| 148 if (codec_id_to_profile_.empty()) |
| 149 return CodecId::VP8; |
| 150 return codec_id_to_profile_.begin()->first; |
| 151 } |
| 152 |
| 153 media::VideoCodecProfile CodecEnumerator::CodecIdToVEAProfile(CodecId codec) { |
| 154 const auto profile = codec_id_to_profile_.find(codec); |
| 155 return profile == codec_id_to_profile_.end() |
| 156 ? media::VIDEO_CODEC_PROFILE_UNKNOWN |
| 157 : profile->second; |
117 } | 158 } |
118 | 159 |
119 } // anonymous namespace | 160 } // anonymous namespace |
120 | 161 |
121 // Base class to describe a generic Encoder, encapsulating all actual encoder | 162 // Base class to describe a generic Encoder, encapsulating all actual encoder |
122 // (re)configurations, encoding and delivery of received frames. This class is | 163 // (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 | 164 // 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 | 165 // the callback that MediaStreamVideoSink passes along) and to jump back and |
125 // forth to an internal encoder thread. Moreover, this class: | 166 // forth to an internal encoder thread. Moreover, this class: |
126 // - is created on its parent's thread (usually the main Render thread), | 167 // - 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; | 1109 return; |
1069 } | 1110 } |
1070 | 1111 |
1071 int pixel_format = EVideoFormatType::videoFormatI420; | 1112 int pixel_format = EVideoFormatType::videoFormatI420; |
1072 openh264_encoder_->SetOption(ENCODER_OPTION_DATAFORMAT, &pixel_format); | 1113 openh264_encoder_->SetOption(ENCODER_OPTION_DATAFORMAT, &pixel_format); |
1073 } | 1114 } |
1074 #endif //#if BUILDFLAG(RTC_USE_H264) | 1115 #endif //#if BUILDFLAG(RTC_USE_H264) |
1075 | 1116 |
1076 } // anonymous namespace | 1117 } // anonymous namespace |
1077 | 1118 |
| 1119 // static |
| 1120 VideoTrackRecorder::CodecId VideoTrackRecorder::GetPreferredCodecId() { |
| 1121 return g_codec_enumerator.Get().GetPreferredCodecId(); |
| 1122 } |
| 1123 |
1078 VideoTrackRecorder::VideoTrackRecorder( | 1124 VideoTrackRecorder::VideoTrackRecorder( |
1079 CodecId codec, | 1125 CodecId codec, |
1080 const blink::WebMediaStreamTrack& track, | 1126 const blink::WebMediaStreamTrack& track, |
1081 const OnEncodedVideoCB& on_encoded_video_callback, | 1127 const OnEncodedVideoCB& on_encoded_video_callback, |
1082 int32_t bits_per_second) | 1128 int32_t bits_per_second) |
1083 : track_(track), | 1129 : track_(track), |
1084 paused_before_init_(false), | 1130 paused_before_init_(false), |
1085 weak_ptr_factory_(this) { | 1131 weak_ptr_factory_(this) { |
1086 DCHECK(main_render_thread_checker_.CalledOnValidThread()); | 1132 DCHECK(main_render_thread_checker_.CalledOnValidThread()); |
1087 DCHECK(!track_.isNull()); | 1133 DCHECK(!track_.isNull()); |
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1136 const OnEncodedVideoCB& on_encoded_video_callback, | 1182 const OnEncodedVideoCB& on_encoded_video_callback, |
1137 int32_t bits_per_second, | 1183 int32_t bits_per_second, |
1138 const scoped_refptr<media::VideoFrame>& frame, | 1184 const scoped_refptr<media::VideoFrame>& frame, |
1139 base::TimeTicks capture_time) { | 1185 base::TimeTicks capture_time) { |
1140 DVLOG(3) << __func__ << frame->visible_rect().size().ToString(); | 1186 DVLOG(3) << __func__ << frame->visible_rect().size().ToString(); |
1141 DCHECK(main_render_thread_checker_.CalledOnValidThread()); | 1187 DCHECK(main_render_thread_checker_.CalledOnValidThread()); |
1142 | 1188 |
1143 MediaStreamVideoSink::DisconnectFromTrack(); | 1189 MediaStreamVideoSink::DisconnectFromTrack(); |
1144 | 1190 |
1145 const gfx::Size& input_size = frame->visible_rect().size(); | 1191 const gfx::Size& input_size = frame->visible_rect().size(); |
1146 const auto& vea_supported_profile = CodecIdToVEAProfile(codec); | 1192 const auto& vea_supported_profile = |
| 1193 g_codec_enumerator.Get().CodecIdToVEAProfile(codec); |
1147 if (vea_supported_profile != media::VIDEO_CODEC_PROFILE_UNKNOWN && | 1194 if (vea_supported_profile != media::VIDEO_CODEC_PROFILE_UNKNOWN && |
1148 input_size.width() >= kVEAEncoderMinResolutionWidth && | 1195 input_size.width() >= kVEAEncoderMinResolutionWidth && |
1149 input_size.height() >= kVEAEncoderMinResolutionHeight) { | 1196 input_size.height() >= kVEAEncoderMinResolutionHeight) { |
1150 encoder_ = new VEAEncoder(on_encoded_video_callback, bits_per_second, | 1197 encoder_ = new VEAEncoder(on_encoded_video_callback, bits_per_second, |
1151 vea_supported_profile, input_size); | 1198 vea_supported_profile, input_size); |
1152 } else { | 1199 } else { |
1153 switch (codec) { | 1200 switch (codec) { |
1154 #if BUILDFLAG(RTC_USE_H264) | 1201 #if BUILDFLAG(RTC_USE_H264) |
1155 case CodecId::H264: | 1202 case CodecId::H264: |
1156 encoder_ = | 1203 encoder_ = |
1157 new H264Encoder(on_encoded_video_callback, bits_per_second); | 1204 new H264Encoder(on_encoded_video_callback, bits_per_second); |
1158 break; | 1205 break; |
1159 #endif | 1206 #endif |
1160 case CodecId::VP8: | 1207 case CodecId::VP8: |
1161 case CodecId::VP9: | 1208 case CodecId::VP9: |
1162 encoder_ = new VpxEncoder(codec == CodecId::VP9, | 1209 encoder_ = new VpxEncoder(codec == CodecId::VP9, |
1163 on_encoded_video_callback, bits_per_second); | 1210 on_encoded_video_callback, bits_per_second); |
1164 break; | 1211 break; |
1165 default: | 1212 default: |
1166 NOTREACHED() << "Unsupported codec"; | 1213 NOTREACHED() << "Unsupported codec " << static_cast<int>(codec); |
1167 } | 1214 } |
1168 } | 1215 } |
1169 | 1216 |
1170 if (paused_before_init_) | 1217 if (paused_before_init_) |
1171 encoder_->SetPaused(paused_before_init_); | 1218 encoder_->SetPaused(paused_before_init_); |
1172 | 1219 |
1173 // StartFrameEncode() will be called on Render IO thread. | 1220 // StartFrameEncode() will be called on Render IO thread. |
1174 MediaStreamVideoSink::ConnectToTrack( | 1221 MediaStreamVideoSink::ConnectToTrack( |
1175 track_, | 1222 track_, |
1176 base::Bind(&VideoTrackRecorder::Encoder::StartFrameEncode, encoder_), | 1223 base::Bind(&VideoTrackRecorder::Encoder::StartFrameEncode, encoder_), |
1177 false); | 1224 false); |
1178 } | 1225 } |
1179 | 1226 |
1180 } // namespace content | 1227 } // namespace content |
OLD | NEW |