summaryrefslogtreecommitdiffstats
path: root/content/common/media/media_param_traits.cc
blob: be53c94dfbc2e6a8599cc9808db4d463b852b59d (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
// Copyright (c) 2012 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 "content/common/media/media_param_traits.h"

#include "base/strings/stringprintf.h"
#include "media/audio/audio_parameters.h"
#include "media/base/limits.h"
#include "media/video/capture/video_capture_types.h"

using media::AudioParameters;
using media::ChannelLayout;
using media::VideoCaptureFormat;
using media::VideoPixelFormat;

namespace IPC {

void ParamTraits<AudioParameters>::Write(Message* m,
                                         const AudioParameters& p) {
  m->WriteInt(static_cast<int>(p.format()));
  m->WriteInt(static_cast<int>(p.channel_layout()));
  m->WriteInt(p.sample_rate());
  m->WriteInt(p.bits_per_sample());
  m->WriteInt(p.frames_per_buffer());
  m->WriteInt(p.channels());
  m->WriteInt(p.input_channels());
  m->WriteInt(p.effects());
}

bool ParamTraits<AudioParameters>::Read(const Message* m,
                                        PickleIterator* iter,
                                        AudioParameters* r) {
  int format, channel_layout, sample_rate, bits_per_sample,
      frames_per_buffer, channels, input_channels, effects;

  if (!m->ReadInt(iter, &format) ||
      !m->ReadInt(iter, &channel_layout) ||
      !m->ReadInt(iter, &sample_rate) ||
      !m->ReadInt(iter, &bits_per_sample) ||
      !m->ReadInt(iter, &frames_per_buffer) ||
      !m->ReadInt(iter, &channels) ||
      !m->ReadInt(iter, &input_channels) ||
      !m->ReadInt(iter, &effects))
    return false;
  AudioParameters params(static_cast<AudioParameters::Format>(format),
         static_cast<ChannelLayout>(channel_layout), channels,
         input_channels, sample_rate, bits_per_sample, frames_per_buffer,
         effects);
  *r = params;
  if (!r->IsValid())
    return false;
  return true;
}

void ParamTraits<AudioParameters>::Log(const AudioParameters& p,
                                       std::string* l) {
  l->append(base::StringPrintf("<AudioParameters>"));
}

void ParamTraits<VideoCaptureFormat>::Write(Message* m,
                                            const VideoCaptureFormat& p) {
  // Crash during Send rather than have a failure at the message handler.
  CHECK(p.IsValid());
  m->WriteInt(p.frame_size.width());
  m->WriteInt(p.frame_size.height());
  m->WriteInt(p.frame_rate);
  m->WriteInt(static_cast<int>(p.pixel_format));
}

bool ParamTraits<VideoCaptureFormat>::Read(const Message* m,
                                           PickleIterator* iter,
                                           VideoCaptureFormat* r) {
  int frame_size_width, frame_size_height, pixel_format;
  if (!m->ReadInt(iter, &frame_size_width) ||
      !m->ReadInt(iter, &frame_size_height) ||
      !m->ReadInt(iter, &r->frame_rate) ||
      !m->ReadInt(iter, &pixel_format))
    return false;

  r->frame_size.SetSize(frame_size_width, frame_size_height);
  r->pixel_format = static_cast<VideoPixelFormat>(pixel_format);
  if (!r->IsValid())
    return false;
  return true;
}

void ParamTraits<VideoCaptureFormat>::Log(const VideoCaptureFormat& p,
                                          std::string* l) {
  l->append(base::StringPrintf("<VideoCaptureFormat>"));
}

}