| OLD | NEW |
| (Empty) |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "media/capture/video/win/sink_input_pin_win.h" | |
| 6 | |
| 7 #include <cstring> | |
| 8 | |
| 9 // Avoid including strsafe.h via dshow as it will cause build warnings. | |
| 10 #define NO_DSHOW_STRSAFE | |
| 11 #include <dshow.h> | |
| 12 #include <stdint.h> | |
| 13 | |
| 14 #include "base/logging.h" | |
| 15 #include "base/macros.h" | |
| 16 #include "media/base/timestamp_constants.h" | |
| 17 | |
| 18 namespace media { | |
| 19 | |
| 20 const REFERENCE_TIME kSecondsToReferenceTime = 10000000; | |
| 21 | |
| 22 static DWORD GetArea(const BITMAPINFOHEADER& info_header) { | |
| 23 return info_header.biWidth * info_header.biHeight; | |
| 24 } | |
| 25 | |
| 26 SinkInputPin::SinkInputPin(IBaseFilter* filter, SinkFilterObserver* observer) | |
| 27 : PinBase(filter), requested_frame_rate_(0), observer_(observer) { | |
| 28 } | |
| 29 | |
| 30 void SinkInputPin::SetRequestedMediaFormat( | |
| 31 VideoPixelFormat pixel_format, | |
| 32 float frame_rate, | |
| 33 const BITMAPINFOHEADER& info_header) { | |
| 34 requested_pixel_format_ = pixel_format; | |
| 35 requested_frame_rate_ = frame_rate; | |
| 36 requested_info_header_ = info_header; | |
| 37 resulting_format_.frame_size.SetSize(0, 0); | |
| 38 resulting_format_.frame_rate = 0; | |
| 39 resulting_format_.pixel_format = PIXEL_FORMAT_UNKNOWN; | |
| 40 } | |
| 41 | |
| 42 bool SinkInputPin::IsMediaTypeValid(const AM_MEDIA_TYPE* media_type) { | |
| 43 const GUID type = media_type->majortype; | |
| 44 if (type != MEDIATYPE_Video) | |
| 45 return false; | |
| 46 | |
| 47 const GUID format_type = media_type->formattype; | |
| 48 if (format_type != FORMAT_VideoInfo) | |
| 49 return false; | |
| 50 | |
| 51 // Check for the sub types we support. | |
| 52 const GUID sub_type = media_type->subtype; | |
| 53 VIDEOINFOHEADER* pvi = | |
| 54 reinterpret_cast<VIDEOINFOHEADER*>(media_type->pbFormat); | |
| 55 if (pvi == NULL) | |
| 56 return false; | |
| 57 | |
| 58 // Store the incoming width and height. | |
| 59 resulting_format_.frame_size.SetSize(pvi->bmiHeader.biWidth, | |
| 60 abs(pvi->bmiHeader.biHeight)); | |
| 61 if (pvi->AvgTimePerFrame > 0) { | |
| 62 resulting_format_.frame_rate = | |
| 63 static_cast<int>(kSecondsToReferenceTime / pvi->AvgTimePerFrame); | |
| 64 } else { | |
| 65 resulting_format_.frame_rate = requested_frame_rate_; | |
| 66 } | |
| 67 if (sub_type == kMediaSubTypeI420 && | |
| 68 pvi->bmiHeader.biCompression == MAKEFOURCC('I', '4', '2', '0')) { | |
| 69 resulting_format_.pixel_format = PIXEL_FORMAT_I420; | |
| 70 return true; | |
| 71 } | |
| 72 if (sub_type == MEDIASUBTYPE_YUY2 && | |
| 73 pvi->bmiHeader.biCompression == MAKEFOURCC('Y', 'U', 'Y', '2')) { | |
| 74 resulting_format_.pixel_format = PIXEL_FORMAT_YUY2; | |
| 75 return true; | |
| 76 } | |
| 77 // This format is added after http:/crbug.com/508413. | |
| 78 if (sub_type == MEDIASUBTYPE_UYVY && | |
| 79 pvi->bmiHeader.biCompression == MAKEFOURCC('U', 'Y', 'V', 'Y')) { | |
| 80 resulting_format_.pixel_format = PIXEL_FORMAT_UYVY; | |
| 81 return true; | |
| 82 } | |
| 83 if (sub_type == MEDIASUBTYPE_MJPG && | |
| 84 pvi->bmiHeader.biCompression == MAKEFOURCC('M', 'J', 'P', 'G')) { | |
| 85 resulting_format_.pixel_format = PIXEL_FORMAT_MJPEG; | |
| 86 return true; | |
| 87 } | |
| 88 if (sub_type == MEDIASUBTYPE_RGB24 && | |
| 89 pvi->bmiHeader.biCompression == BI_RGB) { | |
| 90 resulting_format_.pixel_format = PIXEL_FORMAT_RGB24; | |
| 91 return true; | |
| 92 } | |
| 93 if (sub_type == MEDIASUBTYPE_RGB32 && | |
| 94 pvi->bmiHeader.biCompression == BI_RGB) { | |
| 95 resulting_format_.pixel_format = PIXEL_FORMAT_RGB32; | |
| 96 return true; | |
| 97 } | |
| 98 | |
| 99 #ifndef NDEBUG | |
| 100 WCHAR guid_str[128]; | |
| 101 StringFromGUID2(sub_type, guid_str, arraysize(guid_str)); | |
| 102 DVLOG(2) << __FUNCTION__ << " unsupported media type: " << guid_str; | |
| 103 #endif | |
| 104 return false; | |
| 105 } | |
| 106 | |
| 107 bool SinkInputPin::GetValidMediaType(int index, AM_MEDIA_TYPE* media_type) { | |
| 108 if (media_type->cbFormat < sizeof(VIDEOINFOHEADER)) | |
| 109 return false; | |
| 110 | |
| 111 VIDEOINFOHEADER* const pvi = | |
| 112 reinterpret_cast<VIDEOINFOHEADER*>(media_type->pbFormat); | |
| 113 | |
| 114 ZeroMemory(pvi, sizeof(VIDEOINFOHEADER)); | |
| 115 pvi->bmiHeader.biSize = sizeof(BITMAPINFOHEADER); | |
| 116 pvi->bmiHeader.biPlanes = 1; | |
| 117 pvi->bmiHeader.biClrImportant = 0; | |
| 118 pvi->bmiHeader.biClrUsed = 0; | |
| 119 if (requested_frame_rate_ > 0) | |
| 120 pvi->AvgTimePerFrame = kSecondsToReferenceTime / requested_frame_rate_; | |
| 121 | |
| 122 media_type->majortype = MEDIATYPE_Video; | |
| 123 media_type->formattype = FORMAT_VideoInfo; | |
| 124 media_type->bTemporalCompression = FALSE; | |
| 125 | |
| 126 if (requested_pixel_format_ == PIXEL_FORMAT_MJPEG) { | |
| 127 // If the requested pixel format is MJPEG, accept only MJPEG. | |
| 128 // This is ok since the capabilities of the capturer have been | |
| 129 // enumerated and we know that it is supported. | |
| 130 if (index != 0) | |
| 131 return false; | |
| 132 | |
| 133 pvi->bmiHeader = requested_info_header_; | |
| 134 return true; | |
| 135 } | |
| 136 | |
| 137 switch (index) { | |
| 138 case 0: { | |
| 139 pvi->bmiHeader.biCompression = MAKEFOURCC('I', '4', '2', '0'); | |
| 140 pvi->bmiHeader.biBitCount = 12; // bit per pixel | |
| 141 pvi->bmiHeader.biWidth = requested_info_header_.biWidth; | |
| 142 pvi->bmiHeader.biHeight = requested_info_header_.biHeight; | |
| 143 pvi->bmiHeader.biSizeImage = GetArea(requested_info_header_) * 3 / 2; | |
| 144 media_type->subtype = kMediaSubTypeI420; | |
| 145 break; | |
| 146 } | |
| 147 case 1: { | |
| 148 pvi->bmiHeader.biCompression = MAKEFOURCC('Y', 'U', 'Y', '2'); | |
| 149 pvi->bmiHeader.biBitCount = 16; | |
| 150 pvi->bmiHeader.biWidth = requested_info_header_.biWidth; | |
| 151 pvi->bmiHeader.biHeight = requested_info_header_.biHeight; | |
| 152 pvi->bmiHeader.biSizeImage = GetArea(requested_info_header_) * 2; | |
| 153 media_type->subtype = MEDIASUBTYPE_YUY2; | |
| 154 break; | |
| 155 } | |
| 156 case 2: { | |
| 157 pvi->bmiHeader.biCompression = MAKEFOURCC('U', 'Y', 'V', 'Y'); | |
| 158 pvi->bmiHeader.biBitCount = 16; | |
| 159 pvi->bmiHeader.biWidth = requested_info_header_.biWidth; | |
| 160 pvi->bmiHeader.biHeight = requested_info_header_.biHeight; | |
| 161 pvi->bmiHeader.biSizeImage = GetArea(requested_info_header_) * 2; | |
| 162 media_type->subtype = MEDIASUBTYPE_UYVY; | |
| 163 break; | |
| 164 } | |
| 165 case 3: { | |
| 166 pvi->bmiHeader.biCompression = BI_RGB; | |
| 167 pvi->bmiHeader.biBitCount = 24; | |
| 168 pvi->bmiHeader.biWidth = requested_info_header_.biWidth; | |
| 169 pvi->bmiHeader.biHeight = requested_info_header_.biHeight; | |
| 170 pvi->bmiHeader.biSizeImage = GetArea(requested_info_header_) * 3; | |
| 171 media_type->subtype = MEDIASUBTYPE_RGB24; | |
| 172 break; | |
| 173 } | |
| 174 case 4: { | |
| 175 pvi->bmiHeader.biCompression = BI_RGB; | |
| 176 pvi->bmiHeader.biBitCount = 32; | |
| 177 pvi->bmiHeader.biWidth = requested_info_header_.biWidth; | |
| 178 pvi->bmiHeader.biHeight = requested_info_header_.biHeight; | |
| 179 pvi->bmiHeader.biSizeImage = GetArea(requested_info_header_) * 4; | |
| 180 media_type->subtype = MEDIASUBTYPE_RGB32; | |
| 181 break; | |
| 182 } | |
| 183 default: | |
| 184 return false; | |
| 185 } | |
| 186 | |
| 187 media_type->bFixedSizeSamples = TRUE; | |
| 188 media_type->lSampleSize = pvi->bmiHeader.biSizeImage; | |
| 189 return true; | |
| 190 } | |
| 191 | |
| 192 HRESULT SinkInputPin::Receive(IMediaSample* sample) { | |
| 193 const int length = sample->GetActualDataLength(); | |
| 194 uint8_t* buffer = NULL; | |
| 195 | |
| 196 if (length <= 0) { | |
| 197 DLOG(WARNING) << "Media sample length is 0 or less."; | |
| 198 return S_FALSE; | |
| 199 } | |
| 200 | |
| 201 if (FAILED(sample->GetPointer(&buffer))) | |
| 202 return S_FALSE; | |
| 203 | |
| 204 REFERENCE_TIME start_time, end_time; | |
| 205 base::TimeDelta timestamp = media::kNoTimestamp(); | |
| 206 if (SUCCEEDED(sample->GetTime(&start_time, &end_time))) { | |
| 207 DCHECK(start_time <= end_time); | |
| 208 timestamp = base::TimeDelta::FromMicroseconds(start_time / 10); | |
| 209 } | |
| 210 | |
| 211 observer_->FrameReceived(buffer, length, timestamp); | |
| 212 return S_OK; | |
| 213 } | |
| 214 | |
| 215 SinkInputPin::~SinkInputPin() { | |
| 216 } | |
| 217 | |
| 218 } // namespace media | |
| OLD | NEW |