3 * Copyright 2010 Google Inc.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
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.
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.
28 #include "talk/session/media/mediarecorder.h"
34 #include "talk/media/base/rtpdump.h"
35 #include "webrtc/base/fileutils.h"
36 #include "webrtc/base/logging.h"
37 #include "webrtc/base/pathutils.h"
42 ///////////////////////////////////////////////////////////////////////////
43 // Implementation of RtpDumpSink.
44 ///////////////////////////////////////////////////////////////////////////
45 RtpDumpSink::RtpDumpSink(rtc::StreamInterface* stream)
48 packet_filter_(PF_NONE) {
49 stream_.reset(stream);
52 RtpDumpSink::~RtpDumpSink() {}
54 void RtpDumpSink::SetMaxSize(size_t size) {
55 rtc::CritScope cs(&critical_section_);
59 bool RtpDumpSink::Enable(bool enable) {
60 rtc::CritScope cs(&critical_section_);
64 // Create a file and the RTP writer if we have not done yet.
65 if (recording_ && !writer_) {
69 writer_.reset(new RtpDumpWriter(stream_.get()));
70 writer_->set_packet_filter(packet_filter_);
71 } else if (!recording_ && stream_) {
77 void RtpDumpSink::OnPacket(const void* data, size_t size, bool rtcp) {
78 rtc::CritScope cs(&critical_section_);
80 if (recording_ && writer_) {
82 if (writer_->GetDumpSize(¤t_size) &&
83 current_size + RtpDumpPacket::kHeaderLength + size <= max_size_) {
85 writer_->WriteRtpPacket(data, size);
87 // TODO(whyuan): Enable recording RTCP.
93 void RtpDumpSink::set_packet_filter(int filter) {
94 rtc::CritScope cs(&critical_section_);
95 packet_filter_ = filter;
97 writer_->set_packet_filter(packet_filter_);
101 void RtpDumpSink::Flush() {
102 rtc::CritScope cs(&critical_section_);
108 ///////////////////////////////////////////////////////////////////////////
109 // Implementation of MediaRecorder.
110 ///////////////////////////////////////////////////////////////////////////
111 MediaRecorder::MediaRecorder() {}
113 MediaRecorder::~MediaRecorder() {
114 rtc::CritScope cs(&critical_section_);
115 std::map<BaseChannel*, SinkPair*>::iterator itr;
116 for (itr = sinks_.begin(); itr != sinks_.end(); ++itr) {
121 bool MediaRecorder::AddChannel(VoiceChannel* channel,
122 rtc::StreamInterface* send_stream,
123 rtc::StreamInterface* recv_stream,
125 return InternalAddChannel(channel, false, send_stream, recv_stream,
128 bool MediaRecorder::AddChannel(VideoChannel* channel,
129 rtc::StreamInterface* send_stream,
130 rtc::StreamInterface* recv_stream,
132 return InternalAddChannel(channel, true, send_stream, recv_stream,
136 bool MediaRecorder::InternalAddChannel(BaseChannel* channel,
138 rtc::StreamInterface* send_stream,
139 rtc::StreamInterface* recv_stream,
145 rtc::CritScope cs(&critical_section_);
146 if (sinks_.end() != sinks_.find(channel)) {
147 return false; // The channel was added already.
150 SinkPair* sink_pair = new SinkPair;
151 sink_pair->video_channel = video_channel;
152 sink_pair->filter = filter;
153 sink_pair->send_sink.reset(new RtpDumpSink(send_stream));
154 sink_pair->send_sink->set_packet_filter(filter);
155 sink_pair->recv_sink.reset(new RtpDumpSink(recv_stream));
156 sink_pair->recv_sink->set_packet_filter(filter);
157 sinks_[channel] = sink_pair;
162 void MediaRecorder::RemoveChannel(BaseChannel* channel,
164 rtc::CritScope cs(&critical_section_);
165 std::map<BaseChannel*, SinkPair*>::iterator itr = sinks_.find(channel);
166 if (sinks_.end() != itr) {
167 channel->UnregisterSendSink(itr->second->send_sink.get(), type);
168 channel->UnregisterRecvSink(itr->second->recv_sink.get(), type);
174 bool MediaRecorder::EnableChannel(
175 BaseChannel* channel, bool enable_send, bool enable_recv,
177 rtc::CritScope cs(&critical_section_);
178 std::map<BaseChannel*, SinkPair*>::iterator itr = sinks_.find(channel);
179 if (sinks_.end() == itr) {
183 SinkPair* sink_pair = itr->second;
184 RtpDumpSink* sink = sink_pair->send_sink.get();
185 sink->Enable(enable_send);
187 channel->RegisterSendSink(sink, &RtpDumpSink::OnPacket, type);
189 channel->UnregisterSendSink(sink, type);
192 sink = sink_pair->recv_sink.get();
193 sink->Enable(enable_recv);
195 channel->RegisterRecvSink(sink, &RtpDumpSink::OnPacket, type);
197 channel->UnregisterRecvSink(sink, type);
200 if (sink_pair->video_channel &&
201 (sink_pair->filter & PF_RTPPACKET) == PF_RTPPACKET) {
202 // Request a full intra frame.
203 VideoChannel* video_channel = static_cast<VideoChannel*>(channel);
205 video_channel->SendIntraFrame();
208 video_channel->RequestIntraFrame();
215 void MediaRecorder::FlushSinks() {
216 rtc::CritScope cs(&critical_section_);
217 std::map<BaseChannel*, SinkPair*>::iterator itr;
218 for (itr = sinks_.begin(); itr != sinks_.end(); ++itr) {
219 itr->second->send_sink->Flush();
220 itr->second->recv_sink->Flush();
224 } // namespace cricket