summaryrefslogtreecommitdiffstats
path: root/media/capture/webm_muxer.cc
blob: 3976c0fafbdf9fbc530c5ff73698b27ea05c7f4f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
// Copyright 2015 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "media/capture/webm_muxer.h"

#include "base/bind.h"
#include "media/audio/audio_parameters.h"
#include "media/base/limits.h"
#include "media/base/video_frame.h"
#include "media/filters/opus_constants.h"
#include "ui/gfx/geometry/size.h"

namespace media {

namespace {

void WriteOpusHeader(const media::AudioParameters& params, uint8* header) {
  // See https://wiki.xiph.org/OggOpus#ID_Header.
  // Set magic signature.
  std::string label = "OpusHead";
  memcpy(header + OPUS_EXTRADATA_LABEL_OFFSET, label.c_str(), label.size());
  // Set Opus version.
  header[OPUS_EXTRADATA_VERSION_OFFSET] = 1;
  // Set channel count.
  header[OPUS_EXTRADATA_CHANNELS_OFFSET] = params.channels();
  // Set pre-skip
  uint16 skip = 0;
  memcpy(header + OPUS_EXTRADATA_SKIP_SAMPLES_OFFSET, &skip, sizeof(uint16));
  // Set original input sample rate in Hz.
  uint32 sample_rate = params.sample_rate();
  memcpy(header + OPUS_EXTRADATA_SAMPLE_RATE_OFFSET, &sample_rate,
         sizeof(uint32));
  // Set output gain in dB.
  uint16 gain = 0;
  memcpy(header + OPUS_EXTRADATA_GAIN_OFFSET, &gain, 2);

  // Set channel mapping.
  if (params.channels() > 2) {
    // Also possible to have a multistream, not supported for now.
    DCHECK_LE(params.channels(), OPUS_MAX_VORBIS_CHANNELS);
    header[OPUS_EXTRADATA_CHANNEL_MAPPING_OFFSET] = 1;
    // Assuming no coupled streams. This should actually be
    // channels() - |coupled_streams|.
    header[OPUS_EXTRADATA_NUM_STREAMS_OFFSET] = params.channels();
    header[OPUS_EXTRADATA_NUM_COUPLED_OFFSET] = 0;
    // Set the actual stream map.
    for (int i = 0; i < params.channels(); ++i) {
      header[OPUS_EXTRADATA_STREAM_MAP_OFFSET + i] =
          kOpusVorbisChannelMap[params.channels() - 1][i];
    }
  } else {
    header[OPUS_EXTRADATA_CHANNEL_MAPPING_OFFSET] = 0;
  }
}

static double GetFrameRate(const scoped_refptr<VideoFrame>& video_frame) {
  const double kZeroFrameRate = 0.0;
  const double kDefaultFrameRate = 30.0;

  double frame_rate = kDefaultFrameRate;
  if (!video_frame->metadata()->GetDouble(
          VideoFrameMetadata::FRAME_RATE, &frame_rate) ||
      frame_rate <= kZeroFrameRate ||
      frame_rate > media::limits::kMaxFramesPerSecond) {
    frame_rate = kDefaultFrameRate;
  }
  return frame_rate;
}

}  // anonymous namespace

WebmMuxer::WebmMuxer(VideoCodec codec,
                     bool has_video,
                     bool has_audio,
                     const WriteDataCB& write_data_callback)
    : use_vp9_(codec == kCodecVP9),
      video_track_index_(0),
      audio_track_index_(0),
      has_video_(has_video),
      has_audio_(has_audio),
      write_data_callback_(write_data_callback),
      position_(0) {
  DCHECK(has_video_ || has_audio_);
  DCHECK(!write_data_callback_.is_null());
  DCHECK(codec == kCodecVP8 || codec == kCodecVP9)
      << " Only Vp8 and VP9 are supported in WebmMuxer";

  segment_.Init(this);
  segment_.set_mode(mkvmuxer::Segment::kLive);
  segment_.OutputCues(false);

  mkvmuxer::SegmentInfo* const info = segment_.GetSegmentInfo();
  info->set_writing_app("Chrome");
  info->set_muxing_app("Chrome");

  // Creation is done on a different thread than main activities.
  thread_checker_.DetachFromThread();
}

WebmMuxer::~WebmMuxer() {
  // No need to segment_.Finalize() since is not Seekable(), i.e. a live
  // stream, but is a good practice.
  DCHECK(thread_checker_.CalledOnValidThread());
  segment_.Finalize();
}

void WebmMuxer::OnEncodedVideo(const scoped_refptr<VideoFrame>& video_frame,
                               scoped_ptr<std::string> encoded_data,
                               base::TimeTicks timestamp,
                               bool is_key_frame) {
  DVLOG(1) << __FUNCTION__ << " - " << encoded_data->size() << "B";
  DCHECK(thread_checker_.CalledOnValidThread());

  if (!video_track_index_) {
    // |track_index_|, cannot be zero (!), initialize WebmMuxer in that case.
    // http://www.matroska.org/technical/specs/index.html#Tracks
    AddVideoTrack(video_frame->visible_rect().size(),
                  GetFrameRate(video_frame));
    if (first_frame_timestamp_.is_null())
      first_frame_timestamp_ = timestamp;
  }

  // TODO(ajose): Don't drop data. http://crbug.com/547948
  // TODO(ajose): Update this when we support multiple tracks.
  // http://crbug.com/528523
  if (has_audio_ && !audio_track_index_) {
    DVLOG(1) << __FUNCTION__ << ": delaying until audio track ready.";
    if (is_key_frame) {
      most_recent_encoded_video_keyframe_ = std::move(encoded_data);
      saved_keyframe_timestamp_ = timestamp;
    }
    return;
  }

  // If have a saved keyframe, add it first.
  if (most_recent_encoded_video_keyframe_.get())
    AddFrame(std::move(most_recent_encoded_video_keyframe_), video_track_index_,
             saved_keyframe_timestamp_, true /* is_key_frame */);

  AddFrame(std::move(encoded_data), video_track_index_, timestamp,
           is_key_frame);
}

void WebmMuxer::OnEncodedAudio(const media::AudioParameters& params,
                               scoped_ptr<std::string> encoded_data,
                               base::TimeTicks timestamp) {
  DVLOG(1) << __FUNCTION__ << " - " << encoded_data->size() << "B";
  DCHECK(thread_checker_.CalledOnValidThread());

  if (!audio_track_index_) {
    AddAudioTrack(params);
    if (first_frame_timestamp_.is_null())
      first_frame_timestamp_ = timestamp;
  }

  // TODO(ajose): Don't drop data. http://crbug.com/547948
  // TODO(ajose): Update this when we support multiple tracks.
  // http://crbug.com/528523
  if (has_video_ && !video_track_index_) {
    DVLOG(1) << __FUNCTION__ << ": delaying until video track ready.";
    return;
  }

  // If have a saved keyframe, add it first.
  if (most_recent_encoded_video_keyframe_.get())
    AddFrame(std::move(most_recent_encoded_video_keyframe_), video_track_index_,
             saved_keyframe_timestamp_, true /* is_key_frame */);

  AddFrame(std::move(encoded_data), audio_track_index_, timestamp,
           true /* is_key_frame -- always true for audio */);
}

void WebmMuxer::AddVideoTrack(const gfx::Size& frame_size, double frame_rate) {
  DCHECK(thread_checker_.CalledOnValidThread());
  DCHECK_EQ(0u, video_track_index_)
      << "WebmMuxer can only be initialized once.";

  video_track_index_ =
      segment_.AddVideoTrack(frame_size.width(), frame_size.height(), 0);
  DCHECK_GT(video_track_index_, 0u);

  mkvmuxer::VideoTrack* const video_track =
      reinterpret_cast<mkvmuxer::VideoTrack*>(
          segment_.GetTrackByNumber(video_track_index_));
  DCHECK(video_track);
  video_track->set_codec_id(use_vp9_ ? mkvmuxer::Tracks::kVp9CodecId
                                     : mkvmuxer::Tracks::kVp8CodecId);
  DCHECK_EQ(0ull, video_track->crop_right());
  DCHECK_EQ(0ull, video_track->crop_left());
  DCHECK_EQ(0ull, video_track->crop_top());
  DCHECK_EQ(0ull, video_track->crop_bottom());

  video_track->set_frame_rate(frame_rate);
  video_track->set_default_duration(base::Time::kNanosecondsPerSecond /
                                    frame_rate);
  // Segment's timestamps should be in milliseconds, DCHECK it. See
  // http://www.webmproject.org/docs/container/#muxer-guidelines
  DCHECK_EQ(1000000ull, segment_.GetSegmentInfo()->timecode_scale());
}

void WebmMuxer::AddAudioTrack(const media::AudioParameters& params) {
  DCHECK(thread_checker_.CalledOnValidThread());
  DCHECK_EQ(0u, audio_track_index_)
      << "WebmMuxer audio can only be initialised once.";

  audio_track_index_ =
      segment_.AddAudioTrack(params.sample_rate(), params.channels(), 0);
  DCHECK_GT(audio_track_index_, 0u);

  mkvmuxer::AudioTrack* const audio_track =
      reinterpret_cast<mkvmuxer::AudioTrack*>(
          segment_.GetTrackByNumber(audio_track_index_));
  DCHECK(audio_track);
  audio_track->set_codec_id(mkvmuxer::Tracks::kOpusCodecId);

  DCHECK_EQ(params.sample_rate(), audio_track->sample_rate());
  DCHECK_EQ(params.channels(), static_cast<int>(audio_track->channels()));

  uint8 opus_header[OPUS_EXTRADATA_SIZE];
  WriteOpusHeader(params, opus_header);

  if (!audio_track->SetCodecPrivate(opus_header, OPUS_EXTRADATA_SIZE))
    LOG(ERROR) << __FUNCTION__ << ": failed to set opus header.";

  // Segment's timestamps should be in milliseconds, DCHECK it. See
  // http://www.webmproject.org/docs/container/#muxer-guidelines
  DCHECK_EQ(1000000ull, segment_.GetSegmentInfo()->timecode_scale());
}

mkvmuxer::int32 WebmMuxer::Write(const void* buf, mkvmuxer::uint32 len) {
  DCHECK(thread_checker_.CalledOnValidThread());
  DCHECK(buf);
  write_data_callback_.Run(
      base::StringPiece(reinterpret_cast<const char*>(buf), len));
  position_ += len;
  return 0;
}

mkvmuxer::int64 WebmMuxer::Position() const {
  return position_.ValueOrDie();
}

mkvmuxer::int32 WebmMuxer::Position(mkvmuxer::int64 position) {
  // The stream is not Seekable() so indicate we cannot set the position.
  return -1;
}

bool WebmMuxer::Seekable() const {
  return false;
}

void WebmMuxer::ElementStartNotify(mkvmuxer::uint64 element_id,
                                   mkvmuxer::int64 position) {
  // This method gets pinged before items are sent to |write_data_callback_|.
  DCHECK_GE(position, position_.ValueOrDefault(0))
      << "Can't go back in a live WebM stream.";
}

void WebmMuxer::AddFrame(scoped_ptr<std::string> encoded_data,
                         uint8_t track_index,
                         base::TimeTicks timestamp,
                         bool is_key_frame) {
  DCHECK(thread_checker_.CalledOnValidThread());
  DCHECK(!has_video_ || video_track_index_);
  DCHECK(!has_audio_ || audio_track_index_);

  most_recent_timestamp_ =
      std::max(most_recent_timestamp_, timestamp - first_frame_timestamp_);

  segment_.AddFrame(reinterpret_cast<const uint8_t*>(encoded_data->data()),
                    encoded_data->size(), track_index,
                    most_recent_timestamp_.InMicroseconds() *
                        base::Time::kNanosecondsPerMicrosecond,
                    is_key_frame);
}

}  // namespace media