blob: 4631b93bc961891464c59c385c1a6add6da747da [file] [log] [blame]
jlmiller@webrtc.org5f93d0a2015-01-20 21:36:13 +00001/*
2 * libjingle
3 * Copyright 2004 Google Inc.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 *
8 * 1. Redistributions of source code must retain the above copyright notice,
9 * this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright notice,
11 * this list of conditions and the following disclaimer in the documentation
12 * and/or other materials provided with the distribution.
13 * 3. The name of the author may not be used to endorse or promote products
14 * derived from this software without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
17 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
18 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
19 * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
20 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
22 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
23 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
24 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
25 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
27
henrike@webrtc.org28e20752013-07-10 00:45:36 +000028//
29// This file contains two classes, VideoRecorder and FileVideoCapturer.
30// VideoRecorder records the captured frames into a file. The file stores a
31// sequence of captured frames; each frame has a header defined in struct
32// CapturedFrame, followed by the frame data.
33//
34// FileVideoCapturer, a subclass of VideoCapturer, is a simulated video capturer
35// that periodically reads images from a previously recorded file.
36
kjellandera96e2d72016-02-04 23:52:28 -080037#ifndef WEBRTC_MEDIA_DEVICES_FILEVIDEOCAPTURER_H_
38#define WEBRTC_MEDIA_DEVICES_FILEVIDEOCAPTURER_H_
henrike@webrtc.org28e20752013-07-10 00:45:36 +000039
40#include <string>
41#include <vector>
42
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000043#include "webrtc/base/stream.h"
44#include "webrtc/base/stringutils.h"
kjellandera96e2d72016-02-04 23:52:28 -080045#include "webrtc/media/base/videocapturer.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000046
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000047namespace rtc {
henrike@webrtc.org28e20752013-07-10 00:45:36 +000048class FileStream;
49}
50
51namespace cricket {
52
53// Utility class to record the frames captured by a video capturer into a file.
54class VideoRecorder {
55 public:
56 VideoRecorder() {}
57 ~VideoRecorder() { Stop(); }
58
59 // Start the recorder by opening the specified file. Return true if the file
60 // is opened successfully. write_header should normally be true; false means
61 // write raw frame pixel data to file without any headers.
62 bool Start(const std::string& filename, bool write_header);
63 // Stop the recorder by closing the file.
64 void Stop();
65 // Record a video frame to the file. Return true if the frame is written to
66 // the file successfully. This method needs to be called after Start() and
67 // before Stop().
68 bool RecordFrame(const CapturedFrame& frame);
69
70 private:
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000071 rtc::FileStream video_file_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000072 bool write_header_;
73
henrikg3c089d72015-09-16 05:37:44 -070074 RTC_DISALLOW_COPY_AND_ASSIGN(VideoRecorder);
henrike@webrtc.org28e20752013-07-10 00:45:36 +000075};
76
77// Simulated video capturer that periodically reads frames from a file.
78class FileVideoCapturer : public VideoCapturer {
79 public:
andresp@webrtc.org53d90122015-02-09 14:19:09 +000080 static const int kForever = -1;
81
henrike@webrtc.org28e20752013-07-10 00:45:36 +000082 FileVideoCapturer();
83 virtual ~FileVideoCapturer();
84
85 // Determines if the given device is actually a video file, to be captured
86 // with a FileVideoCapturer.
87 static bool IsFileVideoCapturerDevice(const Device& device) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000088 return rtc::starts_with(device.id.c_str(), kVideoFileDevicePrefix);
henrike@webrtc.org28e20752013-07-10 00:45:36 +000089 }
90
91 // Creates a fake device for the given filename.
92 static Device CreateFileVideoCapturerDevice(const std::string& filename) {
93 std::stringstream id;
94 id << kVideoFileDevicePrefix << filename;
95 return Device(filename, id.str());
96 }
97
98 // Set how many times to repeat reading the file. Repeat forever if the
andresp@webrtc.org53d90122015-02-09 14:19:09 +000099 // parameter is kForever; no repeat if the parameter is 0 or
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000100 // less than -1.
101 void set_repeat(int repeat) { repeat_ = repeat; }
102
103 // If ignore_framerate is true, file is read as quickly as possible. If
104 // false, read rate is controlled by the timestamps in the video file
105 // (thus simulating camera capture). Default value set to false.
106 void set_ignore_framerate(bool ignore_framerate) {
107 ignore_framerate_ = ignore_framerate;
108 }
109
110 // Initializes the capturer with the given file.
111 bool Init(const std::string& filename);
112
113 // Initializes the capturer with the given device. This should only be used
114 // if IsFileVideoCapturerDevice returned true for the given device.
115 bool Init(const Device& device);
116
117 // Override virtual methods of parent class VideoCapturer.
118 virtual CaptureState Start(const VideoFormat& capture_format);
119 virtual void Stop();
120 virtual bool IsRunning();
121 virtual bool IsScreencast() const { return false; }
122
123 protected:
124 // Override virtual methods of parent class VideoCapturer.
Peter Boström0c4e06b2015-10-07 12:23:21 +0200125 virtual bool GetPreferredFourccs(std::vector<uint32_t>* fourccs);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000126
127 // Read the frame header from the file stream, video_file_.
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000128 rtc::StreamResult ReadFrameHeader(CapturedFrame* frame);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000129
130 // Read a frame and determine how long to wait for the next frame. If the
131 // frame is read successfully, Set the output parameter, wait_time_ms and
132 // return true. Otherwise, do not change wait_time_ms and return false.
133 bool ReadFrame(bool first_frame, int* wait_time_ms);
134
135 // Return the CapturedFrame - useful for extracting contents after reading
136 // a frame. Should be used only while still reading a file (i.e. only while
137 // the CapturedFrame object still exists).
138 const CapturedFrame* frame() const {
139 return &captured_frame_;
140 }
141
142 private:
143 class FileReadThread; // Forward declaration, defined in .cc.
144
145 static const char* kVideoFileDevicePrefix;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000146 rtc::FileStream video_file_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000147 CapturedFrame captured_frame_;
148 // The number of bytes allocated buffer for captured_frame_.data.
Peter Boström0c4e06b2015-10-07 12:23:21 +0200149 uint32_t frame_buffer_size_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000150 FileReadThread* file_read_thread_;
151 int repeat_; // How many times to repeat the file.
Peter Boström0c4e06b2015-10-07 12:23:21 +0200152 int64_t last_frame_timestamp_ns_; // Timestamp of last read frame.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000153 bool ignore_framerate_;
154
henrikg3c089d72015-09-16 05:37:44 -0700155 RTC_DISALLOW_COPY_AND_ASSIGN(FileVideoCapturer);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000156};
157
158} // namespace cricket
159
kjellandera96e2d72016-02-04 23:52:28 -0800160#endif // WEBRTC_MEDIA_DEVICES_FILEVIDEOCAPTURER_H_