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 #ifndef MODULES_VIDEO_CODING_UTILITY_IVF_FILE_WRITER_H_
12 #define MODULES_VIDEO_CODING_UTILITY_IVF_FILE_WRITER_H_
13 
14 #include <stddef.h>
15 #include <stdint.h>
16 
17 #include <memory>
18 
19 #include "api/video/encoded_image.h"
20 #include "rtc_base/constructor_magic.h"
21 #include "rtc_base/system/file_wrapper.h"
22 #include "rtc_base/time_utils.h"
23 
24 namespace webrtc {
25 
26 class IvfFileWriter {
27  public:
28   // Takes ownership of the file, which will be closed either through
29   // Close or ~IvfFileWriter. If writing a frame would take the file above the
30   // |byte_limit| the file will be closed, the write (and all future writes)
31   // will fail. A |byte_limit| of 0 is equivalent to no limit.
32   static std::unique_ptr<IvfFileWriter> Wrap(FileWrapper file,
33                                              size_t byte_limit);
34   ~IvfFileWriter();
35 
36   bool WriteFrame(const EncodedImage& encoded_image, VideoCodecType codec_type);
37   bool Close();
38 
39  private:
40   explicit IvfFileWriter(FileWrapper file, size_t byte_limit);
41 
42   bool WriteHeader();
43   bool InitFromFirstFrame(const EncodedImage& encoded_image,
44                           VideoCodecType codec_type);
45   bool WriteOneSpatialLayer(int64_t timestamp,
46                             const uint8_t* data,
47                             size_t size);
48 
49   VideoCodecType codec_type_;
50   size_t bytes_written_;
51   size_t byte_limit_;
52   size_t num_frames_;
53   uint16_t width_;
54   uint16_t height_;
55   int64_t last_timestamp_;
56   bool using_capture_timestamps_;
57   rtc::TimestampWrapAroundHandler wrap_handler_;
58   FileWrapper file_;
59 
60   RTC_DISALLOW_COPY_AND_ASSIGN(IvfFileWriter);
61 };
62 
63 }  // namespace webrtc
64 
65 #endif  // MODULES_VIDEO_CODING_UTILITY_IVF_FILE_WRITER_H_
66