Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1247)

Side by Side Diff: chromecast/media/cma/ipc/media_message_unittest.cc

Issue 529223003: IPC to pass media data using a lock free circular fifo. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Rebase. Created 6 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « chromecast/media/cma/ipc/media_message_type.h ('k') | chromecast/media/media.gyp » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 // Copyright 2014 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 "base/basictypes.h"
6 #include "base/bind.h"
7 #include "base/memory/scoped_ptr.h"
8 #include "chromecast/media/cma/ipc/media_memory_chunk.h"
9 #include "chromecast/media/cma/ipc/media_message.h"
10 #include "testing/gtest/include/gtest/gtest.h"
11
12 namespace chromecast {
13 namespace media {
14
15 namespace {
16
17 class ExternalMemoryBlock
18 : public MediaMemoryChunk {
19 public:
20 ExternalMemoryBlock(void* data, size_t size)
21 : data_(data), size_(size) {}
22 virtual ~ExternalMemoryBlock() {}
23
24 // MediaMemoryChunk implementation.
25 virtual void* data() const OVERRIDE { return data_; }
26 virtual size_t size() const OVERRIDE { return size_; }
27 virtual bool valid() const OVERRIDE { return true; }
28
29 private:
30 void* const data_;
31 const size_t size_;
32 };
33
34 scoped_ptr<MediaMemoryChunk> DummyAllocator(
35 void* data, size_t size, size_t alloc_size) {
36 CHECK_LE(alloc_size, size);
37 return scoped_ptr<MediaMemoryChunk>(
38 new ExternalMemoryBlock(data, alloc_size));
39 }
40
41 }
42
43 TEST(MediaMessageTest, WriteRead) {
44 int buffer_size = 1024;
45 scoped_ptr<uint8[]> buffer(new uint8[buffer_size]);
46 MediaMessage::MemoryAllocatorCB mem_alloc_cb(
47 base::Bind(&DummyAllocator, buffer.get(), buffer_size));
48 uint32 type = 0x1;
49 int msg_content_capacity = 512;
50
51 // Write a message.
52 int count = 64;
53 scoped_ptr<MediaMessage> msg1(
54 MediaMessage::CreateMessage(type, mem_alloc_cb, msg_content_capacity));
55 for (int k = 0; k < count; k++) {
56 int v1 = 2 * k + 1;
57 EXPECT_TRUE(msg1->WritePod(v1));
58 uint8 v2 = k;
59 EXPECT_TRUE(msg1->WritePod(v2));
60 }
61 EXPECT_EQ(msg1->content_size(), count * (sizeof(int) + sizeof(uint8)));
62
63 // Verify the integrity of the message.
64 scoped_ptr<MediaMessage> msg2(
65 MediaMessage::MapMessage(scoped_ptr<MediaMemoryChunk>(
66 new ExternalMemoryBlock(&buffer[0], buffer_size))));
67 for (int k = 0; k < count; k++) {
68 int v1;
69 int expected_v1 = 2 * k + 1;
70 EXPECT_TRUE(msg2->ReadPod(&v1));
71 EXPECT_EQ(v1, expected_v1);
72 uint8 v2;
73 uint8 expected_v2 = k;
74 EXPECT_TRUE(msg2->ReadPod(&v2));
75 EXPECT_EQ(v2, expected_v2);
76 }
77 }
78
79 TEST(MediaMessageTest, WriteOverflow) {
80 int buffer_size = 1024;
81 scoped_ptr<uint8[]> buffer(new uint8[buffer_size]);
82 MediaMessage::MemoryAllocatorCB mem_alloc_cb(
83 base::Bind(&DummyAllocator, buffer.get(), buffer_size));
84 uint32 type = 0x1;
85 int msg_content_capacity = 8;
86
87 scoped_ptr<MediaMessage> msg1(
88 MediaMessage::CreateMessage(type, mem_alloc_cb, msg_content_capacity));
89 uint32 v1 = 0;
90 uint8 v2 = 0;
91 EXPECT_TRUE(msg1->WritePod(v1));
92 EXPECT_TRUE(msg1->WritePod(v1));
93
94 EXPECT_FALSE(msg1->WritePod(v1));
95 EXPECT_FALSE(msg1->WritePod(v2));
96 }
97
98 TEST(MediaMessageTest, ReadOverflow) {
99 int buffer_size = 1024;
100 scoped_ptr<uint8[]> buffer(new uint8[buffer_size]);
101 MediaMessage::MemoryAllocatorCB mem_alloc_cb(
102 base::Bind(&DummyAllocator, buffer.get(), buffer_size));
103 uint32 type = 0x1;
104 int msg_content_capacity = 8;
105
106 scoped_ptr<MediaMessage> msg1(
107 MediaMessage::CreateMessage(type, mem_alloc_cb, msg_content_capacity));
108 uint32 v1 = 0xcd;
109 EXPECT_TRUE(msg1->WritePod(v1));
110 EXPECT_TRUE(msg1->WritePod(v1));
111
112 scoped_ptr<MediaMessage> msg2(
113 MediaMessage::MapMessage(scoped_ptr<MediaMemoryChunk>(
114 new ExternalMemoryBlock(&buffer[0], buffer_size))));
115 uint32 v2;
116 EXPECT_TRUE(msg2->ReadPod(&v2));
117 EXPECT_EQ(v2, v1);
118 EXPECT_TRUE(msg2->ReadPod(&v2));
119 EXPECT_EQ(v2, v1);
120 EXPECT_FALSE(msg2->ReadPod(&v2));
121 }
122
123 TEST(MediaMessageTest, DummyMessage) {
124 int buffer_size = 1024;
125 scoped_ptr<uint8[]> buffer(new uint8[buffer_size]);
126 MediaMessage::MemoryAllocatorCB mem_alloc_cb(
127 base::Bind(&DummyAllocator, buffer.get(), buffer_size));
128 uint32 type = 0x1;
129
130 // Create first a dummy message to estimate the content size.
131 scoped_ptr<MediaMessage> msg1(
132 MediaMessage::CreateDummyMessage(type));
133 uint32 v1 = 0xcd;
134 EXPECT_TRUE(msg1->WritePod(v1));
135 EXPECT_TRUE(msg1->WritePod(v1));
136
137 // Create the real message and write the actual content.
138 scoped_ptr<MediaMessage> msg2(
139 MediaMessage::CreateMessage(type, mem_alloc_cb, msg1->content_size()));
140 EXPECT_TRUE(msg2->WritePod(v1));
141 EXPECT_TRUE(msg2->WritePod(v1));
142 EXPECT_FALSE(msg2->WritePod(v1));
143 }
144
145 } // namespace media
146 } // namespace chromecast
OLDNEW
« no previous file with comments | « chromecast/media/cma/ipc/media_message_type.h ('k') | chromecast/media/media.gyp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698