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