1/*
2 * libjingle
3 * Copyright 2010 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
28#ifndef TALK_SESSION_MEDIA_MEDIARECORDER_H_
29#define TALK_SESSION_MEDIA_MEDIARECORDER_H_
30
31#include <map>
32#include <string>
33
34#include "talk/session/media/channel.h"
35#include "talk/session/media/mediasink.h"
36#include "webrtc/base/criticalsection.h"
37#include "webrtc/base/scoped_ptr.h"
38#include "webrtc/base/sigslot.h"
39
40namespace rtc {
41class Pathname;
42class FileStream;
43}
44
45namespace cricket {
46
47class BaseChannel;
48class VideoChannel;
49class VoiceChannel;
50class RtpDumpWriter;
51
52// RtpDumpSink implements MediaSinkInterface by dumping the RTP/RTCP packets to
53// a file.
54class RtpDumpSink : public MediaSinkInterface, public sigslot::has_slots<> {
55 public:
56  // Takes ownership of stream.
57  explicit RtpDumpSink(rtc::StreamInterface* stream);
58  virtual ~RtpDumpSink();
59
60  virtual void SetMaxSize(size_t size);
61  virtual bool Enable(bool enable);
62  virtual bool IsEnabled() const { return recording_; }
63  virtual void OnPacket(const void* data, size_t size, bool rtcp);
64  virtual void set_packet_filter(int filter);
65  int packet_filter() const { return packet_filter_; }
66  void Flush();
67
68 private:
69  size_t max_size_;
70  bool recording_;
71  int packet_filter_;
72  rtc::scoped_ptr<rtc::StreamInterface> stream_;
73  rtc::scoped_ptr<RtpDumpWriter> writer_;
74  rtc::CriticalSection critical_section_;
75
76  DISALLOW_COPY_AND_ASSIGN(RtpDumpSink);
77};
78
79class MediaRecorder {
80 public:
81  MediaRecorder();
82  virtual ~MediaRecorder();
83
84  bool AddChannel(VoiceChannel* channel,
85                  rtc::StreamInterface* send_stream,
86                  rtc::StreamInterface* recv_stream,
87                  int filter);
88  bool AddChannel(VideoChannel* channel,
89                  rtc::StreamInterface* send_stream,
90                  rtc::StreamInterface* recv_stream,
91                  int filter);
92  void RemoveChannel(BaseChannel* channel, SinkType type);
93  bool EnableChannel(BaseChannel* channel, bool enable_send, bool enable_recv,
94                     SinkType type);
95  void FlushSinks();
96
97 private:
98  struct SinkPair {
99    bool video_channel;
100    int filter;
101    rtc::scoped_ptr<RtpDumpSink> send_sink;
102    rtc::scoped_ptr<RtpDumpSink> recv_sink;
103  };
104
105  bool InternalAddChannel(BaseChannel* channel,
106                          bool video_channel,
107                          rtc::StreamInterface* send_stream,
108                          rtc::StreamInterface* recv_stream,
109                          int filter);
110
111  std::map<BaseChannel*, SinkPair*> sinks_;
112  rtc::CriticalSection critical_section_;
113
114  DISALLOW_COPY_AND_ASSIGN(MediaRecorder);
115};
116
117}  // namespace cricket
118
119#endif  // TALK_SESSION_MEDIA_MEDIARECORDER_H_
120