1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "content/common/media/media_param_traits.h"
6
7 #include "base/strings/stringprintf.h"
8 #include "media/audio/audio_parameters.h"
9 #include "media/base/limits.h"
10 #include "media/video/capture/video_capture_types.h"
11
12 using media::AudioParameters;
13 using media::ChannelLayout;
14 using media::VideoCaptureFormat;
15 using media::VideoPixelFormat;
16
17 namespace IPC {
18
Write(Message * m,const AudioParameters & p)19 void ParamTraits<AudioParameters>::Write(Message* m,
20 const AudioParameters& p) {
21 m->WriteInt(static_cast<int>(p.format()));
22 m->WriteInt(static_cast<int>(p.channel_layout()));
23 m->WriteInt(p.sample_rate());
24 m->WriteInt(p.bits_per_sample());
25 m->WriteInt(p.frames_per_buffer());
26 m->WriteInt(p.channels());
27 m->WriteInt(p.input_channels());
28 m->WriteInt(p.effects());
29 }
30
Read(const Message * m,PickleIterator * iter,AudioParameters * r)31 bool ParamTraits<AudioParameters>::Read(const Message* m,
32 PickleIterator* iter,
33 AudioParameters* r) {
34 int format, channel_layout, sample_rate, bits_per_sample,
35 frames_per_buffer, channels, input_channels, effects;
36
37 if (!m->ReadInt(iter, &format) ||
38 !m->ReadInt(iter, &channel_layout) ||
39 !m->ReadInt(iter, &sample_rate) ||
40 !m->ReadInt(iter, &bits_per_sample) ||
41 !m->ReadInt(iter, &frames_per_buffer) ||
42 !m->ReadInt(iter, &channels) ||
43 !m->ReadInt(iter, &input_channels) ||
44 !m->ReadInt(iter, &effects))
45 return false;
46 AudioParameters params(static_cast<AudioParameters::Format>(format),
47 static_cast<ChannelLayout>(channel_layout), channels,
48 input_channels, sample_rate, bits_per_sample, frames_per_buffer,
49 effects);
50 *r = params;
51 if (!r->IsValid())
52 return false;
53 return true;
54 }
55
Log(const AudioParameters & p,std::string * l)56 void ParamTraits<AudioParameters>::Log(const AudioParameters& p,
57 std::string* l) {
58 l->append(base::StringPrintf("<AudioParameters>"));
59 }
60
Write(Message * m,const VideoCaptureFormat & p)61 void ParamTraits<VideoCaptureFormat>::Write(Message* m,
62 const VideoCaptureFormat& p) {
63 m->WriteInt(p.frame_size.width());
64 m->WriteInt(p.frame_size.height());
65 m->WriteInt(p.frame_rate);
66 m->WriteInt(static_cast<int>(p.pixel_format));
67 }
68
Read(const Message * m,PickleIterator * iter,VideoCaptureFormat * r)69 bool ParamTraits<VideoCaptureFormat>::Read(const Message* m,
70 PickleIterator* iter,
71 VideoCaptureFormat* r) {
72 int frame_size_width, frame_size_height, pixel_format;
73 if (!m->ReadInt(iter, &frame_size_width) ||
74 !m->ReadInt(iter, &frame_size_height) ||
75 !m->ReadInt(iter, &r->frame_rate) ||
76 !m->ReadInt(iter, &pixel_format))
77 return false;
78
79 r->frame_size.SetSize(frame_size_width, frame_size_height);
80 r->pixel_format = static_cast<VideoPixelFormat>(pixel_format);
81 if (!r->IsValid())
82 return false;
83 return true;
84 }
85
Log(const VideoCaptureFormat & p,std::string * l)86 void ParamTraits<VideoCaptureFormat>::Log(const VideoCaptureFormat& p,
87 std::string* l) {
88 l->append(base::StringPrintf("<VideoCaptureFormat>"));
89 }
90
91 }
92