| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 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 "media/video/capture/win/sink_input_pin_win.h" | 5 #include "media/video/capture/win/sink_input_pin_win.h" |
| 6 | 6 |
| 7 #include <cstring> | 7 #include <cstring> |
| 8 | 8 |
| 9 // Avoid including strsafe.h via dshow as it will cause build warnings. | 9 // Avoid including strsafe.h via dshow as it will cause build warnings. |
| 10 #define NO_DSHOW_STRSAFE | 10 #define NO_DSHOW_STRSAFE |
| 11 #include <dshow.h> | 11 #include <dshow.h> |
| 12 | 12 |
| 13 #include "base/logging.h" | 13 #include "base/logging.h" |
| 14 | 14 |
| 15 namespace media { | 15 namespace media { |
| 16 | 16 |
| 17 const REFERENCE_TIME kSecondsToReferenceTime = 10000000; | 17 const REFERENCE_TIME kSecondsToReferenceTime = 10000000; |
| 18 | 18 |
| 19 SinkInputPin::SinkInputPin(IBaseFilter* filter, | 19 SinkInputPin::SinkInputPin(IBaseFilter* filter, |
| 20 SinkFilterObserver* observer) | 20 SinkFilterObserver* observer) |
| 21 : observer_(observer), | 21 : observer_(observer), |
| 22 PinBase(filter) { | 22 PinBase(filter) { |
| 23 memset(&requested_capability_, 0, sizeof(requested_capability_)); |
| 24 memset(&resulting_capability_, 0, sizeof(resulting_capability_)); |
| 23 } | 25 } |
| 24 | 26 |
| 25 SinkInputPin::~SinkInputPin() {} | 27 SinkInputPin::~SinkInputPin() {} |
| 26 | 28 |
| 27 bool SinkInputPin::GetValidMediaType(int index, AM_MEDIA_TYPE* media_type) { | 29 bool SinkInputPin::GetValidMediaType(int index, AM_MEDIA_TYPE* media_type) { |
| 28 if (media_type->cbFormat < sizeof(VIDEOINFOHEADER)) | 30 if (media_type->cbFormat < sizeof(VIDEOINFOHEADER)) |
| 29 return false; | 31 return false; |
| 30 | 32 |
| 31 VIDEOINFOHEADER* pvi = | 33 VIDEOINFOHEADER* pvi = |
| 32 reinterpret_cast<VIDEOINFOHEADER*>(media_type->pbFormat); | 34 reinterpret_cast<VIDEOINFOHEADER*>(media_type->pbFormat); |
| 33 | 35 |
| 34 ZeroMemory(pvi, sizeof(VIDEOINFOHEADER)); | 36 ZeroMemory(pvi, sizeof(VIDEOINFOHEADER)); |
| 35 pvi->bmiHeader.biSize = sizeof(BITMAPINFOHEADER); | 37 pvi->bmiHeader.biSize = sizeof(BITMAPINFOHEADER); |
| 36 pvi->bmiHeader.biPlanes = 1; | 38 pvi->bmiHeader.biPlanes = 1; |
| 37 pvi->bmiHeader.biClrImportant = 0; | 39 pvi->bmiHeader.biClrImportant = 0; |
| 38 pvi->bmiHeader.biClrUsed = 0; | 40 pvi->bmiHeader.biClrUsed = 0; |
| 39 if (requested_format_.frame_rate > 0) { | 41 if (requested_capability_.frame_rate > 0) { |
| 40 pvi->AvgTimePerFrame = | 42 pvi->AvgTimePerFrame = kSecondsToReferenceTime / |
| 41 kSecondsToReferenceTime / requested_format_.frame_rate; | 43 requested_capability_.frame_rate; |
| 42 } | 44 } |
| 43 | 45 |
| 44 media_type->majortype = MEDIATYPE_Video; | 46 media_type->majortype = MEDIATYPE_Video; |
| 45 media_type->formattype = FORMAT_VideoInfo; | 47 media_type->formattype = FORMAT_VideoInfo; |
| 46 media_type->bTemporalCompression = FALSE; | 48 media_type->bTemporalCompression = FALSE; |
| 47 | 49 |
| 48 switch (index) { | 50 switch (index) { |
| 49 case 0: { | 51 case 0: { |
| 50 pvi->bmiHeader.biCompression = MAKEFOURCC('I', '4', '2', '0'); | 52 pvi->bmiHeader.biCompression = MAKEFOURCC('I', '4', '2', '0'); |
| 51 pvi->bmiHeader.biBitCount = 12; // bit per pixel | 53 pvi->bmiHeader.biBitCount = 12; // bit per pixel |
| 52 pvi->bmiHeader.biWidth = requested_format_.frame_size.width(); | 54 pvi->bmiHeader.biWidth = requested_capability_.width; |
| 53 pvi->bmiHeader.biHeight = requested_format_.frame_size.height(); | 55 pvi->bmiHeader.biHeight = requested_capability_.height; |
| 54 pvi->bmiHeader.biSizeImage = | 56 pvi->bmiHeader.biSizeImage = 3 * requested_capability_.height * |
| 55 requested_format_.frame_size.GetArea() * 3 / 2; | 57 requested_capability_.width / 2; |
| 56 media_type->subtype = kMediaSubTypeI420; | 58 media_type->subtype = kMediaSubTypeI420; |
| 57 break; | 59 break; |
| 58 } | 60 } |
| 59 case 1: { | 61 case 1: { |
| 60 pvi->bmiHeader.biCompression = MAKEFOURCC('Y', 'U', 'Y', '2'); | 62 pvi->bmiHeader.biCompression = MAKEFOURCC('Y', 'U', 'Y', '2'); |
| 61 pvi->bmiHeader.biBitCount = 16; | 63 pvi->bmiHeader.biBitCount = 16; |
| 62 pvi->bmiHeader.biWidth = requested_format_.frame_size.width(); | 64 pvi->bmiHeader.biWidth = requested_capability_.width; |
| 63 pvi->bmiHeader.biHeight = requested_format_.frame_size.height(); | 65 pvi->bmiHeader.biHeight = requested_capability_.height; |
| 64 pvi->bmiHeader.biSizeImage = requested_format_.frame_size.GetArea() * 2; | 66 pvi->bmiHeader.biSizeImage = 2 * requested_capability_.width * |
| 67 requested_capability_.height; |
| 65 media_type->subtype = MEDIASUBTYPE_YUY2; | 68 media_type->subtype = MEDIASUBTYPE_YUY2; |
| 66 break; | 69 break; |
| 67 } | 70 } |
| 68 case 2: { | 71 case 2: { |
| 69 pvi->bmiHeader.biCompression = BI_RGB; | 72 pvi->bmiHeader.biCompression = BI_RGB; |
| 70 pvi->bmiHeader.biBitCount = 24; | 73 pvi->bmiHeader.biBitCount = 24; |
| 71 pvi->bmiHeader.biWidth = requested_format_.frame_size.width(); | 74 pvi->bmiHeader.biWidth = requested_capability_.width; |
| 72 pvi->bmiHeader.biHeight = requested_format_.frame_size.height(); | 75 pvi->bmiHeader.biHeight = requested_capability_.height; |
| 73 pvi->bmiHeader.biSizeImage = requested_format_.frame_size.GetArea() * 3; | 76 pvi->bmiHeader.biSizeImage = 3 * requested_capability_.height * |
| 77 requested_capability_.width; |
| 74 media_type->subtype = MEDIASUBTYPE_RGB24; | 78 media_type->subtype = MEDIASUBTYPE_RGB24; |
| 75 break; | 79 break; |
| 76 } | 80 } |
| 77 default: | 81 default: |
| 78 return false; | 82 return false; |
| 79 } | 83 } |
| 80 | 84 |
| 81 media_type->bFixedSizeSamples = TRUE; | 85 media_type->bFixedSizeSamples = TRUE; |
| 82 media_type->lSampleSize = pvi->bmiHeader.biSizeImage; | 86 media_type->lSampleSize = pvi->bmiHeader.biSizeImage; |
| 83 return true; | 87 return true; |
| 84 } | 88 } |
| 85 | 89 |
| 86 bool SinkInputPin::IsMediaTypeValid(const AM_MEDIA_TYPE* media_type) { | 90 bool SinkInputPin::IsMediaTypeValid(const AM_MEDIA_TYPE* media_type) { |
| 87 GUID type = media_type->majortype; | 91 GUID type = media_type->majortype; |
| 88 if (type != MEDIATYPE_Video) | 92 if (type != MEDIATYPE_Video) |
| 89 return false; | 93 return false; |
| 90 | 94 |
| 91 GUID format_type = media_type->formattype; | 95 GUID format_type = media_type->formattype; |
| 92 if (format_type != FORMAT_VideoInfo) | 96 if (format_type != FORMAT_VideoInfo) |
| 93 return false; | 97 return false; |
| 94 | 98 |
| 95 // Check for the sub types we support. | 99 // Check for the sub types we support. |
| 96 GUID sub_type = media_type->subtype; | 100 GUID sub_type = media_type->subtype; |
| 97 VIDEOINFOHEADER* pvi = | 101 VIDEOINFOHEADER* pvi = |
| 98 reinterpret_cast<VIDEOINFOHEADER*>(media_type->pbFormat); | 102 reinterpret_cast<VIDEOINFOHEADER*>(media_type->pbFormat); |
| 99 if (pvi == NULL) | 103 if (pvi == NULL) |
| 100 return false; | 104 return false; |
| 101 | 105 |
| 102 // Store the incoming width and height. | 106 // Store the incoming width and height. |
| 103 resulting_format_.frame_size.SetSize(pvi->bmiHeader.biWidth, | 107 resulting_capability_.width = pvi->bmiHeader.biWidth; |
| 104 abs(pvi->bmiHeader.biHeight)); | 108 resulting_capability_.height = abs(pvi->bmiHeader.biHeight); |
| 105 if (pvi->AvgTimePerFrame > 0) { | 109 if (pvi->AvgTimePerFrame > 0) { |
| 106 resulting_format_.frame_rate = | 110 resulting_capability_.frame_rate = |
| 107 static_cast<int>(kSecondsToReferenceTime / pvi->AvgTimePerFrame); | 111 static_cast<int>(kSecondsToReferenceTime / pvi->AvgTimePerFrame); |
| 108 } else { | 112 } else { |
| 109 resulting_format_.frame_rate = requested_format_.frame_rate; | 113 resulting_capability_.frame_rate = requested_capability_.frame_rate; |
| 110 } | 114 } |
| 111 if (sub_type == kMediaSubTypeI420 && | 115 if (sub_type == kMediaSubTypeI420 && |
| 112 pvi->bmiHeader.biCompression == MAKEFOURCC('I', '4', '2', '0')) { | 116 pvi->bmiHeader.biCompression == MAKEFOURCC('I', '4', '2', '0')) { |
| 113 resulting_format_.pixel_format = PIXEL_FORMAT_I420; | 117 resulting_capability_.color = PIXEL_FORMAT_I420; |
| 114 return true; // This format is acceptable. | 118 return true; // This format is acceptable. |
| 115 } | 119 } |
| 116 if (sub_type == MEDIASUBTYPE_YUY2 && | 120 if (sub_type == MEDIASUBTYPE_YUY2 && |
| 117 pvi->bmiHeader.biCompression == MAKEFOURCC('Y', 'U', 'Y', '2')) { | 121 pvi->bmiHeader.biCompression == MAKEFOURCC('Y', 'U', 'Y', '2')) { |
| 118 resulting_format_.pixel_format = PIXEL_FORMAT_YUY2; | 122 resulting_capability_.color = PIXEL_FORMAT_YUY2; |
| 119 return true; // This format is acceptable. | 123 return true; // This format is acceptable. |
| 120 } | 124 } |
| 121 if (sub_type == MEDIASUBTYPE_RGB24 && | 125 if (sub_type == MEDIASUBTYPE_RGB24 && |
| 122 pvi->bmiHeader.biCompression == BI_RGB) { | 126 pvi->bmiHeader.biCompression == BI_RGB) { |
| 123 resulting_format_.pixel_format = PIXEL_FORMAT_RGB24; | 127 resulting_capability_.color = PIXEL_FORMAT_RGB24; |
| 124 return true; // This format is acceptable. | 128 return true; // This format is acceptable. |
| 125 } | 129 } |
| 126 return false; | 130 return false; |
| 127 } | 131 } |
| 128 | 132 |
| 129 HRESULT SinkInputPin::Receive(IMediaSample* sample) { | 133 HRESULT SinkInputPin::Receive(IMediaSample* sample) { |
| 130 const int length = sample->GetActualDataLength(); | 134 const int length = sample->GetActualDataLength(); |
| 131 uint8* buffer = NULL; | 135 uint8* buffer = NULL; |
| 132 if (FAILED(sample->GetPointer(&buffer))) | 136 if (FAILED(sample->GetPointer(&buffer))) |
| 133 return S_FALSE; | 137 return S_FALSE; |
| 134 | 138 |
| 135 observer_->FrameReceived(buffer, length); | 139 observer_->FrameReceived(buffer, length); |
| 136 return S_OK; | 140 return S_OK; |
| 137 } | 141 } |
| 138 | 142 |
| 139 void SinkInputPin::SetRequestedMediaFormat(const VideoCaptureFormat& format) { | 143 void SinkInputPin::SetRequestedMediaCapability( |
| 140 requested_format_ = format; | 144 const VideoCaptureCapability& capability) { |
| 141 resulting_format_.frame_size.SetSize(0, 0); | 145 requested_capability_ = capability; |
| 142 resulting_format_.frame_rate = 0; | 146 resulting_capability_.width = 0; |
| 143 resulting_format_.pixel_format = PIXEL_FORMAT_UNKNOWN; | 147 resulting_capability_.height = 0; |
| 148 resulting_capability_.frame_rate = 0; |
| 149 resulting_capability_.color = PIXEL_FORMAT_UNKNOWN; |
| 144 } | 150 } |
| 145 | 151 |
| 146 const VideoCaptureFormat& SinkInputPin::ResultingFormat() { | 152 const VideoCaptureCapability& SinkInputPin::ResultingCapability() { |
| 147 return resulting_format_; | 153 return resulting_capability_; |
| 148 } | 154 } |
| 149 | 155 |
| 150 } // namespace media | 156 } // namespace media |
| OLD | NEW |