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 |
| 20 static DWORD GetArea(const BITMAPINFOHEADER& info_header) { |
| 21 return info_header.biWidth * info_header.biHeight; |
| 22 } |
| 23 |
19 SinkInputPin::SinkInputPin(IBaseFilter* filter, | 24 SinkInputPin::SinkInputPin(IBaseFilter* filter, |
20 SinkFilterObserver* observer) | 25 SinkFilterObserver* observer) |
21 : observer_(observer), | 26 : requested_frame_rate_(0), |
| 27 observer_(observer), |
22 PinBase(filter) { | 28 PinBase(filter) { |
23 } | 29 } |
24 | 30 |
25 SinkInputPin::~SinkInputPin() {} | 31 SinkInputPin::~SinkInputPin() {} |
26 | 32 |
27 bool SinkInputPin::GetValidMediaType(int index, AM_MEDIA_TYPE* media_type) { | 33 bool SinkInputPin::GetValidMediaType(int index, AM_MEDIA_TYPE* media_type) { |
28 if (media_type->cbFormat < sizeof(VIDEOINFOHEADER)) | 34 if (media_type->cbFormat < sizeof(VIDEOINFOHEADER)) |
29 return false; | 35 return false; |
30 | 36 |
31 VIDEOINFOHEADER* pvi = | 37 VIDEOINFOHEADER* pvi = |
32 reinterpret_cast<VIDEOINFOHEADER*>(media_type->pbFormat); | 38 reinterpret_cast<VIDEOINFOHEADER*>(media_type->pbFormat); |
33 | 39 |
34 ZeroMemory(pvi, sizeof(VIDEOINFOHEADER)); | 40 ZeroMemory(pvi, sizeof(VIDEOINFOHEADER)); |
35 pvi->bmiHeader.biSize = sizeof(BITMAPINFOHEADER); | 41 pvi->bmiHeader.biSize = sizeof(BITMAPINFOHEADER); |
36 pvi->bmiHeader.biPlanes = 1; | 42 pvi->bmiHeader.biPlanes = 1; |
37 pvi->bmiHeader.biClrImportant = 0; | 43 pvi->bmiHeader.biClrImportant = 0; |
38 pvi->bmiHeader.biClrUsed = 0; | 44 pvi->bmiHeader.biClrUsed = 0; |
39 if (requested_format_.frame_rate > 0) { | 45 if (requested_frame_rate_ > 0) { |
40 pvi->AvgTimePerFrame = | 46 pvi->AvgTimePerFrame = |
41 kSecondsToReferenceTime / requested_format_.frame_rate; | 47 kSecondsToReferenceTime / requested_frame_rate_; |
42 } | 48 } |
43 | 49 |
44 media_type->majortype = MEDIATYPE_Video; | 50 media_type->majortype = MEDIATYPE_Video; |
45 media_type->formattype = FORMAT_VideoInfo; | 51 media_type->formattype = FORMAT_VideoInfo; |
46 media_type->bTemporalCompression = FALSE; | 52 media_type->bTemporalCompression = FALSE; |
47 | 53 |
48 if (requested_format_.pixel_format == PIXEL_FORMAT_MJPEG) { | 54 if (requested_pixel_format_ == PIXEL_FORMAT_MJPEG) { |
49 // If the requested pixel format is MJPEG, accept only MJPEG. | 55 // If the requested pixel format is MJPEG, accept only MJPEG. |
50 // This is ok since the capabilities of the capturer have been | 56 // This is ok since the capabilities of the capturer have been |
51 // enumerated and we know that it is supported. | 57 // enumerated and we know that it is supported. |
52 if (index != 0) | 58 if (index != 0) |
53 return false; | 59 return false; |
54 | 60 |
55 pvi->bmiHeader.biCompression = MAKEFOURCC('M', 'J', 'P', 'G'); | 61 pvi->bmiHeader = requested_info_header_; |
56 pvi->bmiHeader.biBitCount = 0; | |
57 pvi->bmiHeader.biWidth = requested_format_.frame_size.width(); | |
58 pvi->bmiHeader.biHeight = requested_format_.frame_size.height(); | |
59 pvi->bmiHeader.biSizeImage = 0; | |
60 media_type->subtype = MEDIASUBTYPE_MJPG; | |
61 media_type->bFixedSizeSamples = FALSE; | |
62 media_type->lSampleSize = pvi->bmiHeader.biSizeImage; | |
63 return true; | 62 return true; |
64 } | 63 } |
65 | 64 |
66 switch (index) { | 65 switch (index) { |
67 case 0: { | 66 case 0: { |
68 pvi->bmiHeader.biCompression = MAKEFOURCC('I', '4', '2', '0'); | 67 pvi->bmiHeader.biCompression = MAKEFOURCC('I', '4', '2', '0'); |
69 pvi->bmiHeader.biBitCount = 12; // bit per pixel | 68 pvi->bmiHeader.biBitCount = 12; // bit per pixel |
70 pvi->bmiHeader.biWidth = requested_format_.frame_size.width(); | 69 pvi->bmiHeader.biWidth = requested_info_header_.biWidth; |
71 pvi->bmiHeader.biHeight = requested_format_.frame_size.height(); | 70 pvi->bmiHeader.biHeight = requested_info_header_.biHeight; |
72 pvi->bmiHeader.biSizeImage = | 71 pvi->bmiHeader.biSizeImage = |
73 requested_format_.frame_size.GetArea() * 3 / 2; | 72 GetArea(requested_info_header_) * 3 / 2; |
74 media_type->subtype = kMediaSubTypeI420; | 73 media_type->subtype = kMediaSubTypeI420; |
75 break; | 74 break; |
76 } | 75 } |
77 case 1: { | 76 case 1: { |
78 pvi->bmiHeader.biCompression = MAKEFOURCC('Y', 'U', 'Y', '2'); | 77 pvi->bmiHeader.biCompression = MAKEFOURCC('Y', 'U', 'Y', '2'); |
79 pvi->bmiHeader.biBitCount = 16; | 78 pvi->bmiHeader.biBitCount = 16; |
80 pvi->bmiHeader.biWidth = requested_format_.frame_size.width(); | 79 pvi->bmiHeader.biWidth = requested_info_header_.biWidth; |
81 pvi->bmiHeader.biHeight = requested_format_.frame_size.height(); | 80 pvi->bmiHeader.biHeight = requested_info_header_.biHeight; |
82 pvi->bmiHeader.biSizeImage = requested_format_.frame_size.GetArea() * 2; | 81 pvi->bmiHeader.biSizeImage = GetArea(requested_info_header_) * 2; |
83 media_type->subtype = MEDIASUBTYPE_YUY2; | 82 media_type->subtype = MEDIASUBTYPE_YUY2; |
84 break; | 83 break; |
85 } | 84 } |
86 case 2: { | 85 case 2: { |
87 pvi->bmiHeader.biCompression = BI_RGB; | 86 pvi->bmiHeader.biCompression = BI_RGB; |
88 pvi->bmiHeader.biBitCount = 24; | 87 pvi->bmiHeader.biBitCount = 24; |
89 pvi->bmiHeader.biWidth = requested_format_.frame_size.width(); | 88 pvi->bmiHeader.biWidth = requested_info_header_.biWidth; |
90 pvi->bmiHeader.biHeight = requested_format_.frame_size.height(); | 89 pvi->bmiHeader.biHeight = requested_info_header_.biHeight; |
91 pvi->bmiHeader.biSizeImage = requested_format_.frame_size.GetArea() * 3; | 90 pvi->bmiHeader.biSizeImage = GetArea(requested_info_header_) * 3; |
92 media_type->subtype = MEDIASUBTYPE_RGB24; | 91 media_type->subtype = MEDIASUBTYPE_RGB24; |
93 break; | 92 break; |
94 } | 93 } |
95 default: | 94 default: |
96 return false; | 95 return false; |
97 } | 96 } |
98 | 97 |
99 media_type->bFixedSizeSamples = TRUE; | 98 media_type->bFixedSizeSamples = TRUE; |
100 media_type->lSampleSize = pvi->bmiHeader.biSizeImage; | 99 media_type->lSampleSize = pvi->bmiHeader.biSizeImage; |
101 return true; | 100 return true; |
(...skipping 15 matching lines...) Expand all Loading... |
117 if (pvi == NULL) | 116 if (pvi == NULL) |
118 return false; | 117 return false; |
119 | 118 |
120 // Store the incoming width and height. | 119 // Store the incoming width and height. |
121 resulting_format_.frame_size.SetSize(pvi->bmiHeader.biWidth, | 120 resulting_format_.frame_size.SetSize(pvi->bmiHeader.biWidth, |
122 abs(pvi->bmiHeader.biHeight)); | 121 abs(pvi->bmiHeader.biHeight)); |
123 if (pvi->AvgTimePerFrame > 0) { | 122 if (pvi->AvgTimePerFrame > 0) { |
124 resulting_format_.frame_rate = | 123 resulting_format_.frame_rate = |
125 static_cast<int>(kSecondsToReferenceTime / pvi->AvgTimePerFrame); | 124 static_cast<int>(kSecondsToReferenceTime / pvi->AvgTimePerFrame); |
126 } else { | 125 } else { |
127 resulting_format_.frame_rate = requested_format_.frame_rate; | 126 resulting_format_.frame_rate = requested_frame_rate_; |
128 } | 127 } |
129 if (sub_type == kMediaSubTypeI420 && | 128 if (sub_type == kMediaSubTypeI420 && |
130 pvi->bmiHeader.biCompression == MAKEFOURCC('I', '4', '2', '0')) { | 129 pvi->bmiHeader.biCompression == MAKEFOURCC('I', '4', '2', '0')) { |
131 resulting_format_.pixel_format = PIXEL_FORMAT_I420; | 130 resulting_format_.pixel_format = PIXEL_FORMAT_I420; |
132 return true; // This format is acceptable. | 131 return true; // This format is acceptable. |
133 } | 132 } |
134 if (sub_type == MEDIASUBTYPE_YUY2 && | 133 if (sub_type == MEDIASUBTYPE_YUY2 && |
135 pvi->bmiHeader.biCompression == MAKEFOURCC('Y', 'U', 'Y', '2')) { | 134 pvi->bmiHeader.biCompression == MAKEFOURCC('Y', 'U', 'Y', '2')) { |
136 resulting_format_.pixel_format = PIXEL_FORMAT_YUY2; | 135 resulting_format_.pixel_format = PIXEL_FORMAT_YUY2; |
137 return true; // This format is acceptable. | 136 return true; // This format is acceptable. |
(...skipping 14 matching lines...) Expand all Loading... |
152 HRESULT SinkInputPin::Receive(IMediaSample* sample) { | 151 HRESULT SinkInputPin::Receive(IMediaSample* sample) { |
153 const int length = sample->GetActualDataLength(); | 152 const int length = sample->GetActualDataLength(); |
154 uint8* buffer = NULL; | 153 uint8* buffer = NULL; |
155 if (FAILED(sample->GetPointer(&buffer))) | 154 if (FAILED(sample->GetPointer(&buffer))) |
156 return S_FALSE; | 155 return S_FALSE; |
157 | 156 |
158 observer_->FrameReceived(buffer, length); | 157 observer_->FrameReceived(buffer, length); |
159 return S_OK; | 158 return S_OK; |
160 } | 159 } |
161 | 160 |
162 void SinkInputPin::SetRequestedMediaFormat(const VideoCaptureFormat& format) { | 161 void SinkInputPin::SetRequestedMediaFormat( |
163 requested_format_ = format; | 162 VideoPixelFormat pixel_format, |
| 163 float frame_rate, |
| 164 const BITMAPINFOHEADER& info_header) { |
| 165 requested_pixel_format_ = pixel_format; |
| 166 requested_frame_rate_ = frame_rate; |
| 167 requested_info_header_ = info_header; |
164 resulting_format_.frame_size.SetSize(0, 0); | 168 resulting_format_.frame_size.SetSize(0, 0); |
165 resulting_format_.frame_rate = 0; | 169 resulting_format_.frame_rate = 0; |
166 resulting_format_.pixel_format = PIXEL_FORMAT_UNKNOWN; | 170 resulting_format_.pixel_format = PIXEL_FORMAT_UNKNOWN; |
167 } | 171 } |
168 | 172 |
169 const VideoCaptureFormat& SinkInputPin::ResultingFormat() { | 173 const VideoCaptureFormat& SinkInputPin::ResultingFormat() { |
170 return resulting_format_; | 174 return resulting_format_; |
171 } | 175 } |
172 | 176 |
173 } // namespace media | 177 } // namespace media |
OLD | NEW |