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