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 {CodecId::H264, media::H264PROFILE_MIN, media::H264PROFILE_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 | 73 |
78 // Returns the corresponding codec profile from VEA supported codecs. If no | 74 static_assert(arraysize(kPreferredCodecIdAndVEAProfiles) == |
79 // profile is found, returns VIDEO_CODEC_PROFILE_UNKNOWN. | 75 static_cast<int>(CodecId::LAST), |
80 media::VideoCodecProfile CodecIdToVEAProfile( | 76 "|kPreferredCodecIdAndVEAProfiles| should consider all CodecIds"); |
81 content::VideoTrackRecorder::CodecId codec) { | 77 |
| 78 // Class to encapsulate the enumeration of CodecIds/VideoCodecProfiles supported |
| 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) |
82 // See https://crbug.com/616659. | 106 // See https://crbug.com/616659. |
83 #if defined(OS_CHROMEOS) | 107 return; |
84 return media::VIDEO_CODEC_PROFILE_UNKNOWN; | 108 #endif |
85 #endif // defined(OS_CHROMEOS) | |
86 | 109 |
87 // See https://crbug.com/653864. | |
88 #if defined(OS_ANDROID) | 110 #if defined(OS_ANDROID) |
89 return media::VIDEO_CODEC_PROFILE_UNKNOWN; | 111 // See https://crbug.com/653864. |
90 #endif // defined(OS_ANDROID) | 112 return; |
| 113 #endif |
91 | 114 |
92 content::RenderThreadImpl* const render_thread_impl = | 115 content::RenderThreadImpl* const render_thread_impl = |
93 content::RenderThreadImpl::current(); | 116 content::RenderThreadImpl::current(); |
94 if (!render_thread_impl) { | 117 if (!render_thread_impl) { |
95 DVLOG(3) << "Couldn't access the render thread"; | 118 DVLOG(2) << "Couldn't access the render thread"; |
96 return media::VIDEO_CODEC_PROFILE_UNKNOWN; | 119 return; |
97 } | 120 } |
98 | 121 |
99 media::GpuVideoAcceleratorFactories* const gpu_factories = | 122 media::GpuVideoAcceleratorFactories* const gpu_factories = |
100 render_thread_impl->GetGpuFactories(); | 123 render_thread_impl->GetGpuFactories(); |
101 if (!gpu_factories || !gpu_factories->IsGpuVideoAcceleratorEnabled()) { | 124 if (!gpu_factories || !gpu_factories->IsGpuVideoAcceleratorEnabled()) { |
102 DVLOG(3) << "Couldn't initialize GpuVideoAcceleratorFactories"; | 125 DVLOG(2) << "Couldn't initialize GpuVideoAcceleratorFactories"; |
103 return media::VIDEO_CODEC_PROFILE_UNKNOWN; | 126 return; |
104 } | 127 } |
105 | 128 |
106 const media::VideoEncodeAccelerator::SupportedProfiles& vea_profiles = | 129 const auto vea_supported_profiles = |
107 gpu_factories->GetVideoEncodeAcceleratorSupportedProfiles(); | 130 gpu_factories->GetVideoEncodeAcceleratorSupportedProfiles(); |
108 for (const auto& vea_profile : vea_profiles) { | 131 for (const auto& supported_profile : vea_supported_profiles) { |
109 for (const auto& supported_profile : kSupportedVideoCodecIdToProfile) { | 132 for (auto& codec_id_and_profile : kPreferredCodecIdAndVEAProfiles) { |
110 if (codec == supported_profile.codec_id && | 133 if (supported_profile.profile >= codec_id_and_profile.min_profile && |
111 vea_profile.profile >= supported_profile.min_profile && | 134 supported_profile.profile <= codec_id_and_profile.max_profile) { |
112 vea_profile.profile <= supported_profile.max_profile) | 135 DVLOG(2) << "Accelerated codec found: " |
113 return vea_profile.profile; | 136 << media::GetProfileName(supported_profile.profile); |
| 137 codec_id_to_profile_.insert(std::make_pair( |
| 138 codec_id_and_profile.codec_id, supported_profile.profile)); |
| 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_ = |
(...skipping 14 matching lines...) Expand all Loading... |
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 |