1 /*
2  *  Copyright (c) 2012 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 WEBRTC_VIDEO_ENGINE_TEST_LIBVIETEST_INCLUDE_VIE_TO_FILE_RENDERER_H_
12 #define WEBRTC_VIDEO_ENGINE_TEST_LIBVIETEST_INCLUDE_VIE_TO_FILE_RENDERER_H_
13 
14 #include <stdio.h>
15 #include <string.h>
16 
17 #include <list>
18 #include <string>
19 
20 #include "webrtc/base/constructormagic.h"
21 #include "webrtc/base/scoped_ptr.h"
22 #include "webrtc/video_engine/include/vie_render.h"
23 
24 namespace webrtc {
25 class CriticalSectionWrapper;
26 class EventWrapper;
27 class ThreadWrapper;
28 };  // namespace webrtc
29 
30 namespace test {
31 struct Frame;
32 };  // namespace test
33 
34 class ViEToFileRenderer: public webrtc::ExternalRenderer {
35  public:
36   ViEToFileRenderer();
37   virtual ~ViEToFileRenderer();
38 
39   // Returns false if we fail opening the output filename for writing.
40   bool PrepareForRendering(const std::string& output_path,
41                            const std::string& output_filename);
42 
43   // Closes the output file.
44   void StopRendering();
45 
46   // Deletes the closed output file from the file system. This is one option
47   // after calling StopRendering, the other being KeepOutputFile. This file
48   // renderer will forget about the file after this call and can be used again.
49   bool DeleteOutputFile();
50 
51   // Renames the closed output file to its previous name with the provided
52   // prefix prepended. This file renderer will forget about the file after this
53   // call and can be used again.
54   bool SaveOutputFile(const std::string& prefix);
55 
56   // Implementation of ExternalRenderer:
57   int FrameSizeChange(unsigned int width,
58                       unsigned int height,
59                       unsigned int number_of_streams) override;
60 
61   int DeliverFrame(unsigned char* buffer,
62                    size_t buffer_size,
63                    uint32_t time_stamp,
64                    int64_t ntp_time_ms,
65                    int64_t render_time,
66                    void* handle) override;
67 
68   int DeliverI420Frame(const webrtc::I420VideoFrame& webrtc_frame) override;
69 
70   bool IsTextureSupported() override;
71 
72   const std::string GetFullOutputPath() const;
73 
74  private:
75   typedef std::list<test::Frame*> FrameQueue;
76 
77   // Returns a frame with the specified |buffer_size|. Tries to avoid allocating
78   // new frames by reusing frames from |free_frame_queue_|.
79   test::Frame* NewFrame(size_t buffer_size);
80   static bool RunRenderThread(void* obj);
81   void ForgetOutputFile();
82   bool ProcessRenderQueue();
83 
84   FILE* output_file_;
85   std::string output_path_;
86   std::string output_filename_;
87   rtc::scoped_ptr<webrtc::ThreadWrapper> thread_;
88   rtc::scoped_ptr<webrtc::CriticalSectionWrapper> frame_queue_cs_;
89   rtc::scoped_ptr<webrtc::EventWrapper> frame_render_event_;
90   FrameQueue render_queue_;
91   FrameQueue free_frame_queue_;
92 };
93 
94 #endif  // WEBRTC_VIDEO_ENGINE_TEST_LIBVIETEST_INCLUDE_VIE_TO_FILE_RENDERER_H_
95