ogg_saver.cpp
Go to the documentation of this file.
1 /*********************************************************************
2 * Software License Agreement (BSD License)
3 *
4 * Copyright (c) 20012, Willow Garage, Inc.
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 *
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * * Redistributions in binary form must reproduce the above
14 * copyright notice, this list of conditions and the following
15 * disclaimer in the documentation and/or other materials provided
16 * with the distribution.
17 * * Neither the name of the Willow Garage nor the names of its
18 * contributors may be used to endorse or promote products derived
19 * from this software without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
27 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
29 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
31 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32 * POSSIBILITY OF SUCH DAMAGE.
33 *********************************************************************/
34 
35 #include <ros/ros.h>
36 
37 #include <theora_image_transport/Packet.h>
38 
39 #include <theora/codec.h>
40 #include <theora/theoraenc.h>
41 #include <theora/theoradec.h>
42 #include <ogg/ogg.h>
43 
44 #include <fstream>
45 #include <vector>
46 #include <boost/scoped_array.hpp>
47 
48 using namespace std;
49 
50 class OggSaver
51 {
52 public:
53  OggSaver(const char* filename)
54  : fout_(filename, std::ios::out|std::ios::binary)
55  {
56  if (ogg_stream_init(&stream_state_, 0) == -1) {
57  ROS_FATAL("Unable to initialize ogg_stream_state structure");
58  exit(1);
59  }
60 
61  sub_ = nh_.subscribe("stream", 10, &OggSaver::processMsg, this);
62  }
63 
65  {
66  ogg_page page;
67  if (ogg_stream_flush(&stream_state_, &page) != 0)
68  writePage(page);
69  fout_.close();
70  ogg_stream_clear(&stream_state_);
71  }
72 
73 private:
74 
76  ogg_stream_state stream_state_;
77  ofstream fout_;
79 
80  // When using this caller is responsible for deleting oggpacket.packet!!
81  void msgToOggPacket(const theora_image_transport::Packet &msg, ogg_packet &oggpacket)
82  {
83  oggpacket.bytes = msg.data.size();
84  oggpacket.b_o_s = msg.b_o_s;
85  oggpacket.e_o_s = msg.e_o_s;
86  oggpacket.granulepos = msg.granulepos;
87  oggpacket.packetno = msg.packetno;
88  oggpacket.packet = new unsigned char[oggpacket.bytes];
89  memcpy(oggpacket.packet, &msg.data[0], oggpacket.bytes);
90  }
91 
92  void writePage(ogg_page& page)
93  {
94  fout_.write((char*)page.header, page.header_len);
95  fout_.write((char*)page.body, page.body_len);
96  }
97 
98  void processMsg(const theora_image_transport::PacketConstPtr& message)
99  {
105  ogg_packet oggpacket;
106  msgToOggPacket(*message, oggpacket);
107  boost::scoped_array<unsigned char> packet_guard(oggpacket.packet); // Make sure packet memory gets deleted
108 
109  if (ogg_stream_packetin(&stream_state_, &oggpacket)) {
110  ROS_ERROR("Error while adding packet to stream.");
111  exit(2);
112  }
113 
114  ogg_page page;
115  if (ogg_stream_pageout(&stream_state_, &page) != 0)
116  writePage(page);
117  }
118 };
119 
120 int main(int argc, char** argv)
121 {
124  ros::init(argc, argv, "OggSaver", ros::init_options::AnonymousName);
125 
126  if(argc < 2) {
127  cerr << "Usage: " << argv[0] << " stream:=/theora/image/stream outputFile" << endl;
128  exit(3);
129  }
130  if (ros::names::remap("stream") == "stream") {
131  ROS_WARN("ogg_saver: stream has not been remapped! Typical command-line usage:\n"
132  "\t$ ./ogg_saver stream:=<theora stream topic> outputFile");
133  }
134 
135  OggSaver saver(argv[1]);
136 
137  ros::spin();
138  return 0;
139 }
#define ROS_FATAL(...)
ogg_stream_state stream_state_
Definition: ogg_saver.cpp:76
ROSCPP_DECL void init(int &argc, char **argv, const std::string &name, uint32_t options=0)
int main(int argc, char **argv)
Definition: ogg_saver.cpp:120
#define ROS_WARN(...)
OggSaver(const char *filename)
Definition: ogg_saver.cpp:53
ROSCPP_DECL void spin(Spinner &spinner)
void msgToOggPacket(const theora_image_transport::Packet &msg, ogg_packet &oggpacket)
Definition: ogg_saver.cpp:81
ROSCPP_DECL std::string remap(const std::string &name)
ros::NodeHandle nh_
Definition: ogg_saver.cpp:75
void processMsg(const theora_image_transport::PacketConstPtr &message)
Definition: ogg_saver.cpp:98
void writePage(ogg_page &page)
Definition: ogg_saver.cpp:92
ofstream fout_
Definition: ogg_saver.cpp:77
ros::Subscriber sub_
Definition: ogg_saver.cpp:78
#define ROS_ERROR(...)


theora_image_transport
Author(s): Patrick Mihelich, Ethan Dreyfuss
autogenerated on Fri Sep 20 2019 03:32:16