Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 /* | |
| 2 * Copyright (c) 2016 The WebRTC project authors. All Rights Reserved. | |
| 3 * | |
| 4 * Use of this source code is governed by a BSD-style license | |
| 5 * that can be found in the LICENSE file in the root of the source | |
| 6 * tree. An additional intellectual property rights grant can be found | |
| 7 * in the file PATENTS. All contributing project authors may | |
| 8 * be found in the AUTHORS file in the root of the source tree. | |
| 9 */ | |
| 10 | |
| 11 #include "webrtc/modules/video_coding/utility/ivf_file_writer.h" | |
| 12 | |
| 13 #include "webrtc/base/checks.h" | |
| 14 #include "webrtc/base/fileutils.h" | |
| 15 #include "webrtc/base/logging.h" | |
| 16 #include "webrtc/modules/rtp_rtcp/source/byte_io.h" | |
| 17 | |
| 18 namespace webrtc { | |
| 19 | |
| 20 IvfFileWriter::IvfFileWriter(rtc::Pathname file_name, | |
| 21 rtc::StreamInterface* out_stream) | |
|
pbos-webrtc
2016/04/07 16:16:04
Should this take an unique_ptr as part of the inte
sprang_webrtc
2016/04/11 15:57:28
Done.
| |
| 22 : num_frames_(0), | |
| 23 width_(0), | |
| 24 height_(0), | |
| 25 last_timestamp_(-1), | |
| 26 file_name_(file_name), | |
| 27 out_stream_(out_stream) { | |
| 28 RTC_CHECK(out_stream != nullptr); | |
|
pbos-webrtc
2016/04/07 16:16:04
drop != nullptr
sprang_webrtc
2016/04/11 15:57:28
Done.
| |
| 29 } | |
| 30 | |
| 31 IvfFileWriter::~IvfFileWriter() { | |
| 32 Close(); | |
| 33 } | |
| 34 | |
| 35 const int kResolutionOffset = 12; | |
| 36 const int kFrameCountOffset = 24; | |
| 37 const size_t kIvfHeaderSize = 32; | |
| 38 | |
| 39 std::unique_ptr<IvfFileWriter> IvfFileWriter::Open( | |
| 40 rtc::Pathname file_name, | |
| 41 RtpVideoCodecTypes codec_type) { | |
| 42 std::unique_ptr<IvfFileWriter> file_writer; | |
| 43 std::unique_ptr<rtc::FileStream> out_stream(new rtc::FileStream()); | |
| 44 if (!out_stream->Open(file_name.pathname(), "wb", nullptr)) | |
| 45 return file_writer; | |
| 46 | |
| 47 file_writer.reset(new IvfFileWriter(file_name, out_stream.release())); | |
| 48 if (!file_writer->WriteHeader(codec_type)) | |
| 49 file_writer.reset(); | |
| 50 | |
| 51 return file_writer; | |
| 52 } | |
| 53 | |
| 54 std::unique_ptr<IvfFileWriter> IvfFileWriter::Open( | |
| 55 rtc::StreamInterface* out_stream, | |
| 56 RtpVideoCodecTypes codec_type) { | |
| 57 std::unique_ptr<IvfFileWriter> file_writer( | |
| 58 new IvfFileWriter(rtc::Pathname(), out_stream)); | |
| 59 if (!file_writer->WriteHeader(codec_type)) | |
| 60 file_writer.reset(); | |
| 61 | |
| 62 return file_writer; | |
| 63 } | |
| 64 | |
| 65 bool IvfFileWriter::WriteHeader(RtpVideoCodecTypes codec_type) { | |
| 66 uint8_t ivf_header[kIvfHeaderSize] = {0}; | |
| 67 ivf_header[0] = 'D'; | |
| 68 ivf_header[1] = 'K'; | |
| 69 ivf_header[2] = 'I'; | |
| 70 ivf_header[3] = 'F'; | |
| 71 ByteWriter<uint16_t>::WriteLittleEndian(&ivf_header[4], 0); // Verison. | |
| 72 ByteWriter<uint16_t>::WriteLittleEndian(&ivf_header[6], 32); // Header size. | |
| 73 | |
| 74 switch (codec_type) { | |
| 75 case kRtpVideoVp8: | |
| 76 ivf_header[8] = 'V'; | |
| 77 ivf_header[9] = 'P'; | |
| 78 ivf_header[10] = '8'; | |
| 79 ivf_header[11] = '0'; | |
| 80 break; | |
| 81 case kRtpVideoVp9: | |
| 82 ivf_header[8] = 'V'; | |
| 83 ivf_header[9] = 'P'; | |
| 84 ivf_header[10] = '9'; | |
| 85 ivf_header[11] = '0'; | |
| 86 break; | |
| 87 case kRtpVideoH264: | |
| 88 ivf_header[8] = 'H'; | |
| 89 ivf_header[9] = '2'; | |
| 90 ivf_header[10] = '6'; | |
| 91 ivf_header[11] = '4'; | |
| 92 break; | |
| 93 default: | |
| 94 LOG(LS_ERROR) << "Unknown CODEC type: " << codec_type; | |
| 95 return false; | |
| 96 } | |
| 97 | |
| 98 // Width and Height (populated on first frame). | |
| 99 ByteWriter<uint16_t>::WriteLittleEndian(&ivf_header[12], 0); | |
| 100 ByteWriter<uint16_t>::WriteLittleEndian(&ivf_header[14], 0); | |
| 101 // Timestamps in milliseconds => time scale of 1/1000. | |
| 102 ByteWriter<uint32_t>::WriteLittleEndian(&ivf_header[16], 1000); | |
| 103 ByteWriter<uint32_t>::WriteLittleEndian(&ivf_header[20], 1); | |
| 104 // Number of frames (populated on close). | |
| 105 ByteWriter<uint32_t>::WriteLittleEndian(&ivf_header[24], 0); | |
| 106 ByteWriter<uint32_t>::WriteLittleEndian(&ivf_header[28], 0); // Reserved. | |
| 107 | |
| 108 if (out_stream_->WriteAll(ivf_header, kIvfHeaderSize, nullptr, nullptr) != | |
| 109 rtc::SR_SUCCESS) { | |
| 110 LOG(LS_ERROR) << "Unable to write IVF header."; | |
| 111 return false; | |
| 112 } | |
| 113 | |
| 114 return true; | |
| 115 } | |
| 116 | |
| 117 bool IvfFileWriter::WriteFrame(const EncodedImage& encoded_image) { | |
| 118 RTC_DCHECK(out_stream_->GetState() == rtc::SS_OPEN); | |
| 119 | |
| 120 if (num_frames_ == 0) { | |
| 121 if (!out_stream_->SetPosition(kResolutionOffset)) | |
| 122 return false; | |
| 123 | |
| 124 width_ = encoded_image._encodedWidth; | |
| 125 height_ = encoded_image._encodedHeight; | |
| 126 | |
| 127 const size_t kResolutionDataSize = 4; | |
| 128 uint8_t buffer[kResolutionDataSize]; | |
| 129 ByteWriter<uint16_t>::WriteLittleEndian(&buffer[0], | |
| 130 encoded_image._encodedWidth); | |
| 131 ByteWriter<uint16_t>::WriteLittleEndian(&buffer[2], | |
| 132 encoded_image._encodedHeight); | |
| 133 | |
| 134 if (out_stream_->WriteAll(buffer, kResolutionDataSize, nullptr, nullptr) != | |
| 135 rtc::SR_SUCCESS) { | |
| 136 LOG(LS_ERROR) << "Unable to write resolution to IVF file " | |
| 137 << file_name_.filename(); | |
| 138 return false; | |
| 139 } | |
| 140 if (!out_stream_->SetPosition(kIvfHeaderSize)) | |
| 141 return false; | |
| 142 } | |
| 143 | |
| 144 RTC_CHECK_EQ(height_, encoded_image._encodedHeight); | |
| 145 RTC_CHECK_EQ(width_, encoded_image._encodedWidth); | |
| 146 if (last_timestamp_ != -1) | |
| 147 RTC_CHECK_GT(encoded_image.capture_time_ms_, last_timestamp_); | |
| 148 last_timestamp_ = encoded_image.capture_time_ms_; | |
| 149 | |
| 150 const size_t kFrameHeaderSize = 12; | |
| 151 uint8_t frame_header[kFrameHeaderSize] = {}; | |
| 152 ByteWriter<uint32_t>::WriteLittleEndian(&frame_header[0], | |
| 153 encoded_image._length); | |
| 154 ByteWriter<uint64_t>::WriteLittleEndian(&frame_header[4], | |
| 155 encoded_image.capture_time_ms_); | |
| 156 if (out_stream_->WriteAll(frame_header, kFrameHeaderSize, nullptr, nullptr) != | |
| 157 rtc::SR_SUCCESS || | |
| 158 out_stream_->WriteAll(encoded_image._buffer, encoded_image._length, | |
| 159 nullptr, nullptr) != rtc::SR_SUCCESS) { | |
| 160 LOG(LS_ERROR) << "Unable to write frame to file " << file_name_.filename(); | |
| 161 return false; | |
| 162 } | |
| 163 | |
| 164 ++num_frames_; | |
| 165 return true; | |
| 166 } | |
| 167 | |
| 168 bool IvfFileWriter::Close() { | |
| 169 if (out_stream_->GetState() != rtc::SS_OPEN) | |
| 170 return false; | |
| 171 | |
| 172 if (num_frames_ == 0) { | |
| 173 // No frame written to file, close and remove it entirely if possible. | |
| 174 out_stream_->Close(); | |
| 175 if (!file_name_.empty()) | |
| 176 rtc::Filesystem::default_filesystem()->DeleteFile(file_name_); | |
| 177 return true; | |
| 178 } | |
| 179 | |
| 180 if (!out_stream_->SetPosition(kFrameCountOffset)) | |
| 181 return false; | |
| 182 const size_t kFrameCountLength = 4; | |
| 183 uint8_t size_data[kFrameCountLength]; | |
| 184 ByteWriter<uint32_t>::WriteLittleEndian(size_data, num_frames_); | |
| 185 if (out_stream_->WriteAll(size_data, kFrameCountLength, nullptr, nullptr) != | |
| 186 rtc::SR_SUCCESS) { | |
| 187 LOG(LS_ERROR) << "Unable to update header of IVF file when closing " | |
| 188 << file_name_.filename(); | |
| 189 } | |
| 190 | |
| 191 out_stream_->Close(); | |
| 192 return true; | |
| 193 } | |
| 194 | |
| 195 } // namespace webrtc | |
| OLD | NEW |