2

I'm buiding a streaming video server in Windows Media Http Streaming Protocol (WMSP) with C#. I've read WMSP Specification. In Framing Header of packets as $H, $D... has 2 fields :

B (1 bit): A 1-bit flag. This flag SHOULD be set to 1 if the next packet will be sent immediately after this packet is sent. In this context, "immediately" means that the server does not intentionally introduce a delay (such as a pacing delay) between the transmission of the two packets. In all other cases, the flag MUST be 0.<56>

Frame (7 bits): A 7-bit field. This field MUST have the value 0x24. (If the B and Frame fields are treated as a single byte, the value of this byte will be 0x24 when the B field is 0, and 0xA4 when the B field is 1.)

Data type of this 2 field is bit when the smallest data type in C# is byte. So, how to declare and set value for this 2 field ?

PenguinSh
  • 129
  • 2
  • 4
  • 12

4 Answers4

4

You could use some C-style bit manipulation technique:

byte data = 0;
data |= (0x1 << 7) & 0xFF; // set the 8th bit of data
data |= 0x24;
Console.WriteLine(data.ToString("x")); // outputs 'a4'

But since there are only two cases for these: 0xA4, 0x24. It might be a good idea to use these constants directly.

Gant
  • 29,661
  • 6
  • 46
  • 65
1

Just effectively assign values to both fields at once (since together they form a single byte for sending):

byte headerByte = (sendImmediately) ? 0xA4 : 0x24;

There is no need to treat them separately or do any bitwise calculations since you know there are only two possible values.

dlev
  • 48,024
  • 5
  • 125
  • 132
  • this does not set them both at once. –  Jul 27 '11 at 17:59
  • @0A0D If they can be treated as a single byte (and I'm pretty sure they can, since it's a single bit followed by seven bits) then this will set the high bit if the next packet should be sent immediately and doesn't otherwise. What's the problem here? – dlev Jul 27 '11 at 18:02
  • You are using a ternary operator. You said "assign them both at once". What this does is one or the other, not both. You are not assigning both. The statement is misleading. –  Jul 27 '11 at 18:04
  • @0A0D I'm not assigning both *values* at once; why would I want to do that? I'm assigning both *fields* at once: the first bit will be 1 if `sendImmediately == true`, and 0 otherwise. The lower 7 bits (which make up the subsequent Frame field) will be set to 0x24 regardless (as per the spec.) – dlev Jul 27 '11 at 18:06
  • I know. It's just your sentence needs clarification because if someone else comes along, it will not be clear. Remember: "What is clear to me is clear to me." –  Jul 27 '11 at 18:07
  • @0A0D Perhaps the sentence was unclear, but I think the code was pretty unambiguous: assign one value or the other to a single byte depending on the relevant flag. In any case, I've edited the answer to make that clearer. – dlev Jul 27 '11 at 18:08
  • Yes, if you took out "assign .. both fields at once" - I wouldn't of said anything. What would make more sense is something like, "put both values in the same statement." You are implying that you are assigning both fields at once, actually that is what you said, but not what the code is doing. –  Jul 27 '11 at 18:10
  • @0A0D That's what the code does; but you're right, I didn't make it clear that the single `byte` was being used for both fields. My edit makes that explicit. – dlev Jul 27 '11 at 18:12
  • dlev, I love you man but I don't think you get it :) I know that the same variable is being re-used for two different values. It's this few words that are confusing : "assign values to both fields at once " –  Jul 27 '11 at 18:13
  • @0A0D Right back at ya, bro. And I think we're in agreement: claiming to assign to two fields at once without explaining how that is happening (especially given that "field" means something very specific in C# semantics) was confusing. – dlev Jul 27 '11 at 18:15
  • @0A0D : yes, i think solve it by treat them as a single byte. But how to do that ? – PenguinSh Jul 27 '11 at 18:16
  • @PenguinSh: I think you already have the answer in your question: "If the B and Frame fields are treated as a single byte, the value of this byte will be 0x24 when the B field is 0, and 0xA4 when the B field is 1" –  Jul 27 '11 at 18:17
  • @0A0D : byte headerByte = (sendImmediately) ? 0xA4 : 0x24; I don't understand what you meant. (sendImmediately) ??? – PenguinSh Jul 27 '11 at 18:17
  • @PenguinSh Perhaps we could see the code you've got? I'm assuming your server will be writing out data in a burst of bytes, so when I say "treat them as one", I mean to just write out a single byte (either 0xA4 if B is set, 0x24 otherwise) instead of writing out B and Frame separately. – dlev Jul 27 '11 at 18:18
  • @PenguinSh (sendImmediately) is just a boolean variable that indicates whether or not the next packet should be sent immediately. It doesn't have to be called that. – dlev Jul 27 '11 at 18:19
  • @PenguinSh: you have to check the value of the B field, whether it is 0 or 1. –  Jul 27 '11 at 18:21
  • @PenguinSh: See here - https://nmparsers.svn.codeplex.com/svn/Develop_Branch/NPL/Windows/WmHttp.npl –  Jul 27 '11 at 18:22
  • @0A0D : I know, but in specification : http://msdn.microsoft.com/en-us/library/cc251201(v=PROT.10).aspx. It said : "Frame : This field MUST have the value 0x24" – PenguinSh Jul 27 '11 at 18:37
0

Use bitwise operations to flip them on/off, here's an or that will turn it on based on the ternary evaluation of the firstBitShouldBeSet boolean.

yourFrameByte = firstBitShouldBeSet ? yourFrameByte | 0x80 : yourFrameByte;
Jimmy Hoffa
  • 5,909
  • 30
  • 53
0

You can declare a byte and then set bits in the byte. The high bit of the byte would be your field 1, and the low 7 bits of the byte would be your field 2.

If you want to see some bit manipulation operations in C#, you can look at this SO question. However, it looks like the specification already gives you the values to set the byte to, 0x24 and 0xA4, so you can use those directly.

Community
  • 1
  • 1
dsolimano
  • 8,870
  • 3
  • 48
  • 63