| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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/capture/video/file_video_capture_device.h" | 5 #include "device/capture/video/file_video_capture_device.h" |
| 6 | 6 |
| 7 #include <stddef.h> | 7 #include <stddef.h> |
| 8 #include <utility> | 8 #include <utility> |
| 9 | 9 |
| 10 #include "base/bind.h" | 10 #include "base/bind.h" |
| 11 #include "base/location.h" | 11 #include "base/location.h" |
| 12 #include "base/macros.h" | 12 #include "base/macros.h" |
| 13 #include "base/single_thread_task_runner.h" | 13 #include "base/single_thread_task_runner.h" |
| 14 #include "base/strings/string_number_conversions.h" | 14 #include "base/strings/string_number_conversions.h" |
| 15 #include "base/strings/string_piece.h" | 15 #include "base/strings/string_piece.h" |
| 16 #include "base/threading/thread_task_runner_handle.h" | 16 #include "base/threading/thread_task_runner_handle.h" |
| 17 #include "media/base/video_capture_types.h" | 17 #include "media/base/video_capture_types.h" |
| 18 #include "media/filters/jpeg_parser.h" | 18 #include "media/filters/jpeg_parser.h" |
| 19 | 19 |
| 20 namespace media { | 20 namespace device { |
| 21 | 21 |
| 22 static const int kY4MHeaderMaxSize = 200; | 22 static const int kY4MHeaderMaxSize = 200; |
| 23 static const char kY4MSimpleFrameDelimiter[] = "FRAME"; | 23 static const char kY4MSimpleFrameDelimiter[] = "FRAME"; |
| 24 static const int kY4MSimpleFrameDelimiterSize = 6; | 24 static const int kY4MSimpleFrameDelimiterSize = 6; |
| 25 static const float kMJpegFrameRate = 30.0f; | 25 static const float kMJpegFrameRate = 30.0f; |
| 26 | 26 |
| 27 int ParseY4MInt(const base::StringPiece& token) { | 27 int ParseY4MInt(const base::StringPiece& token) { |
| 28 int temp_int; | 28 int temp_int; |
| 29 CHECK(base::StringToInt(token, &temp_int)) << token; | 29 CHECK(base::StringToInt(token, &temp_int)) << token; |
| 30 return temp_int; | 30 return temp_int; |
| (...skipping 188 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 219 MjpegFileParser::~MjpegFileParser() {} | 219 MjpegFileParser::~MjpegFileParser() {} |
| 220 | 220 |
| 221 bool MjpegFileParser::Initialize(media::VideoCaptureFormat* capture_format) { | 221 bool MjpegFileParser::Initialize(media::VideoCaptureFormat* capture_format) { |
| 222 mapped_file_.reset(new base::MemoryMappedFile()); | 222 mapped_file_.reset(new base::MemoryMappedFile()); |
| 223 | 223 |
| 224 if (!mapped_file_->Initialize(file_path_) || !mapped_file_->IsValid()) { | 224 if (!mapped_file_->Initialize(file_path_) || !mapped_file_->IsValid()) { |
| 225 LOG(ERROR) << "File memory map error: " << file_path_.value(); | 225 LOG(ERROR) << "File memory map error: " << file_path_.value(); |
| 226 return false; | 226 return false; |
| 227 } | 227 } |
| 228 | 228 |
| 229 JpegParseResult result; | 229 media::JpegParseResult result; |
| 230 if (!ParseJpegStream(mapped_file_->data(), mapped_file_->length(), &result)) | 230 if (!media::ParseJpegStream(mapped_file_->data(), mapped_file_->length(), |
| 231 &result)) |
| 231 return false; | 232 return false; |
| 232 | 233 |
| 233 frame_size_ = result.image_size; | 234 frame_size_ = result.image_size; |
| 234 if (frame_size_ > static_cast<int>(mapped_file_->length())) { | 235 if (frame_size_ > static_cast<int>(mapped_file_->length())) { |
| 235 LOG(ERROR) << "File is incomplete"; | 236 LOG(ERROR) << "File is incomplete"; |
| 236 return false; | 237 return false; |
| 237 } | 238 } |
| 238 | 239 |
| 239 VideoCaptureFormat format; | 240 VideoCaptureFormat format; |
| 240 format.pixel_format = media::PIXEL_FORMAT_MJPEG; | 241 format.pixel_format = media::PIXEL_FORMAT_MJPEG; |
| 241 format.frame_size.set_width(result.frame_header.visible_width); | 242 format.frame_size.set_width(result.frame_header.visible_width); |
| 242 format.frame_size.set_height(result.frame_header.visible_height); | 243 format.frame_size.set_height(result.frame_header.visible_height); |
| 243 format.frame_rate = kMJpegFrameRate; | 244 format.frame_rate = kMJpegFrameRate; |
| 244 if (!format.IsValid()) | 245 if (!format.IsValid()) |
| 245 return false; | 246 return false; |
| 246 *capture_format = format; | 247 *capture_format = format; |
| 247 return true; | 248 return true; |
| 248 } | 249 } |
| 249 | 250 |
| 250 const uint8_t* MjpegFileParser::GetNextFrame(int* frame_size) { | 251 const uint8_t* MjpegFileParser::GetNextFrame(int* frame_size) { |
| 251 const uint8_t* buf_ptr = mapped_file_->data() + current_byte_index_; | 252 const uint8_t* buf_ptr = mapped_file_->data() + current_byte_index_; |
| 252 | 253 |
| 253 JpegParseResult result; | 254 media::JpegParseResult result; |
| 254 if (!ParseJpegStream(buf_ptr, mapped_file_->length() - current_byte_index_, | 255 if (!media::ParseJpegStream( |
| 255 &result)) { | 256 buf_ptr, mapped_file_->length() - current_byte_index_, &result)) { |
| 256 return nullptr; | 257 return nullptr; |
| 257 } | 258 } |
| 258 *frame_size = frame_size_ = result.image_size; | 259 *frame_size = frame_size_ = result.image_size; |
| 259 current_byte_index_ += frame_size_; | 260 current_byte_index_ += frame_size_; |
| 260 // Reset the pointer to play repeatedly. | 261 // Reset the pointer to play repeatedly. |
| 261 if (current_byte_index_ >= mapped_file_->length()) | 262 if (current_byte_index_ >= mapped_file_->length()) |
| 262 current_byte_index_ = first_frame_byte_index_; | 263 current_byte_index_ = first_frame_byte_index_; |
| 263 return buf_ptr; | 264 return buf_ptr; |
| 264 } | 265 } |
| 265 | 266 |
| (...skipping 118 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 384 // immediately and continue as normal. | 385 // immediately and continue as normal. |
| 385 if (next_frame_time_ < current_time) | 386 if (next_frame_time_ < current_time) |
| 386 next_frame_time_ = current_time; | 387 next_frame_time_ = current_time; |
| 387 } | 388 } |
| 388 base::ThreadTaskRunnerHandle::Get()->PostDelayedTask( | 389 base::ThreadTaskRunnerHandle::Get()->PostDelayedTask( |
| 389 FROM_HERE, base::Bind(&FileVideoCaptureDevice::OnCaptureTask, | 390 FROM_HERE, base::Bind(&FileVideoCaptureDevice::OnCaptureTask, |
| 390 base::Unretained(this)), | 391 base::Unretained(this)), |
| 391 next_frame_time_ - current_time); | 392 next_frame_time_ - current_time); |
| 392 } | 393 } |
| 393 | 394 |
| 394 } // namespace media | 395 } // namespace device |
| OLD | NEW |