1 #include "core/advdumper.hpp"
2 #include "core/dispatch.hpp"
3 #include "video/tcp.hpp"
4 #include "library/serialization.hpp"
15 #define IS_RGB(m) (((m) + ((m) >> 3)) & 2)
16 #define IS_64(m) (m % 5 < 2)
17 #define IS_TCP(m) (((m % 5) * (m % 5)) % 5 == 1)
23 unsigned strhash(const std::string
& str
)
26 for(size_t i
= 0; i
< str
.length(); i
++)
27 h
= (2 * h
+ static_cast<unsigned char>(str
[i
])) % 11;
31 class raw_avsnoop
: public information_dispatch
34 raw_avsnoop(const std::string
& prefix
, bool _swap
, bool _bits64
, bool socket_mode
)
35 : information_dispatch("dump-raw")
39 socket_address videoaddr
= socket_address(prefix
);
40 socket_address audioaddr
= videoaddr
.next();
43 video
= &videoaddr
.connect();
44 audio
= &audioaddr
.connect();
51 video
= new std::ofstream(prefix
+ ".video", std::ios::out
| std::ios::binary
);
52 audio
= new std::ofstream(prefix
+ ".audio", std::ios::out
| std::ios::binary
);
54 if(!*video
|| !*audio
)
55 throw std::runtime_error("Can't open output files");
56 have_dumped_frame
= false;
61 ~raw_avsnoop() throw()
67 void on_frame(struct lcscreen
& _frame
, uint32_t fps_n
, uint32_t fps_d
)
76 unsigned r
= (reinterpret_cast<unsigned char*>(&magic
))[swap
? 2 : 0];
77 unsigned g
= (reinterpret_cast<unsigned char*>(&magic
))[1];
78 unsigned b
= (reinterpret_cast<unsigned char*>(&magic
))[swap
? 0 : 2];
79 uint32_t hscl
= (_frame
.width
< 400) ? 2 : 1;
80 uint32_t vscl
= (_frame
.height
< 400) ? 2 : 1;
82 render_video_hud(dscr2
, _frame
, hscl
, vscl
, r
, g
, b
, 0, 0, 0, 0, NULL
);
83 for(size_t i
= 0; i
< dscr2
.height
; i
++)
84 video
->write(reinterpret_cast<char*>(dscr2
.rowptr(i
)), 8 * dscr2
.width
);
86 render_video_hud(dscr
, _frame
, hscl
, vscl
, r
, g
, b
, 0, 0, 0, 0, NULL
);
87 for(size_t i
= 0; i
< dscr
.height
; i
++)
88 video
->write(reinterpret_cast<char*>(dscr
.rowptr(i
)), 4 * dscr
.width
);
91 messages
<< "Video write error" << std::endl
;
92 have_dumped_frame
= true;
95 void on_sample(short l
, short r
)
97 if(have_dumped_frame
&& audio
) {
99 write16sbe(buffer
+ 0, l
);
100 write16sbe(buffer
+ 2, r
);
101 audio
->write(buffer
, 4);
113 bool get_dumper_flag() throw()
120 bool have_dumped_frame
;
121 struct screen
<false> dscr
;
122 struct screen
<true> dscr2
;
127 raw_avsnoop
* vid_dumper
;
129 class adv_raw_dumper
: public adv_dumper
132 adv_raw_dumper() : adv_dumper("INTERNAL-RAW") {information_dispatch::do_dumper_update(); }
133 ~adv_raw_dumper() throw();
134 std::set
<std::string
> list_submodes() throw(std::bad_alloc
)
136 std::set
<std::string
> x
;
137 for(size_t i
= 0; i
< (socket_address::supported() ? 2 : 1); i
++)
138 for(size_t j
= 0; j
< 2; j
++)
139 for(size_t k
= 0; k
< 2; k
++)
140 x
.insert(std::string("") + (i
? "tcp" : "") + (j
? "bgr" : "rgb")
141 + (k
? "64" : "32"));
145 unsigned mode_details(const std::string
& mode
) throw()
147 return IS_TCP(strhash(mode
)) ? target_type_special
: target_type_prefix
;
150 std::string
name() throw(std::bad_alloc
)
155 std::string
modename(const std::string
& mode
) throw(std::bad_alloc
)
157 unsigned _mode
= strhash(mode
);
158 std::string x
= std::string((IS_RGB(_mode
) ? "RGB" : "BGR")) +
159 (IS_64(_mode
) ? " 64-bit" : " 32-bit") + (IS_TCP(_mode
) ? " over TCP/IP" : "");
165 return (vid_dumper
!= NULL
);
168 void start(const std::string
& mode
, const std::string
& prefix
) throw(std::bad_alloc
,
171 unsigned _mode
= strhash(mode
);
172 bool bits64
= IS_64(_mode
);
173 bool swap
= !IS_RGB(_mode
);
174 bool sock
= IS_TCP(_mode
);
177 throw std::runtime_error("Expected prefix");
179 throw std::runtime_error("RAW dumping already in progress");
181 vid_dumper
= new raw_avsnoop(prefix
, swap
, bits64
, sock
);
182 } catch(std::bad_alloc
& e
) {
184 } catch(std::exception
& e
) {
185 std::ostringstream x
;
186 x
<< "Error starting RAW dump: " << e
.what();
187 throw std::runtime_error(x
.str());
189 messages
<< "Dumping to " << prefix
<< std::endl
;
190 information_dispatch::do_dumper_update();
196 throw std::runtime_error("No RAW video dump in progress");
198 vid_dumper
->on_dump_end();
199 messages
<< "RAW Dump finished" << std::endl
;
200 } catch(std::bad_alloc
& e
) {
202 } catch(std::exception
& e
) {
203 messages
<< "Error ending RAW dump: " << e
.what() << std::endl
;
207 information_dispatch::do_dumper_update();
211 adv_raw_dumper::~adv_raw_dumper() throw()