You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

85 line
3.4KB

  1. #include "flv-demuxer.h"
  2. #include "amf0.h"
  3. #include <errno.h>
  4. #include <assert.h>
  5. #include <string.h>
  6. #define N_ONMETADATA 12 // 2-LEN + 10-onMetaData
  7. /// http://www.cnblogs.com/musicfans/archive/2012/11/07/2819291.html
  8. /// metadata keyframes/filepositions
  9. /// @return >0-OK, 0-don't metadata, <0-error
  10. int flv_demuxer_script(struct flv_demuxer_t* flv, const uint8_t* data, size_t bytes)
  11. {
  12. const uint8_t* end;
  13. char buffer[64] = { 0 };
  14. double audiocodecid = 0;
  15. double audiodatarate = 0; // bitrate / 1024
  16. double audiodelay = 0;
  17. double audiosamplerate = 0;
  18. double audiosamplesize = 0;
  19. double videocodecid = 0;
  20. double videodatarate = 0; // bitrate / 1024
  21. double framerate = 0;
  22. double height = 0;
  23. double width = 0;
  24. double duration = 0;
  25. double filesize = 0;
  26. int canSeekToEnd = 0;
  27. int stereo = 0;
  28. struct amf_object_item_t keyframes[2];
  29. struct amf_object_item_t prop[16];
  30. struct amf_object_item_t items[1];
  31. #define AMF_OBJECT_ITEM_VALUE(v, amf_type, amf_name, amf_value, amf_size) { v.type=amf_type; v.name=amf_name; v.value=amf_value; v.size=amf_size; }
  32. AMF_OBJECT_ITEM_VALUE(keyframes[0], AMF_STRICT_ARRAY, "filepositions", NULL, 0); // ignore keyframes
  33. AMF_OBJECT_ITEM_VALUE(keyframes[1], AMF_STRICT_ARRAY, "times", NULL, 0);
  34. AMF_OBJECT_ITEM_VALUE(prop[0], AMF_NUMBER, "audiocodecid", &audiocodecid, sizeof(audiocodecid));
  35. AMF_OBJECT_ITEM_VALUE(prop[1], AMF_NUMBER, "audiodatarate", &audiodatarate, sizeof(audiodatarate));
  36. AMF_OBJECT_ITEM_VALUE(prop[2], AMF_NUMBER, "audiodelay", &audiodelay, sizeof(audiodelay));
  37. AMF_OBJECT_ITEM_VALUE(prop[3], AMF_NUMBER, "audiosamplerate", &audiosamplerate, sizeof(audiosamplerate));
  38. AMF_OBJECT_ITEM_VALUE(prop[4], AMF_NUMBER, "audiosamplesize", &audiosamplesize, sizeof(audiosamplesize));
  39. AMF_OBJECT_ITEM_VALUE(prop[5], AMF_BOOLEAN, "stereo", &stereo, sizeof(stereo));
  40. AMF_OBJECT_ITEM_VALUE(prop[6], AMF_BOOLEAN, "canSeekToEnd", &canSeekToEnd, sizeof(canSeekToEnd));
  41. AMF_OBJECT_ITEM_VALUE(prop[7], AMF_STRING, "creationdate", buffer, sizeof(buffer));
  42. AMF_OBJECT_ITEM_VALUE(prop[8], AMF_NUMBER, "duration", &duration, sizeof(duration));
  43. AMF_OBJECT_ITEM_VALUE(prop[9], AMF_NUMBER, "filesize", &filesize, sizeof(filesize));
  44. AMF_OBJECT_ITEM_VALUE(prop[10], AMF_NUMBER, "videocodecid", &videocodecid, sizeof(videocodecid));
  45. AMF_OBJECT_ITEM_VALUE(prop[11], AMF_NUMBER, "videodatarate", &videodatarate, sizeof(videodatarate));
  46. AMF_OBJECT_ITEM_VALUE(prop[12], AMF_NUMBER, "framerate", &framerate, sizeof(framerate));
  47. AMF_OBJECT_ITEM_VALUE(prop[13], AMF_NUMBER, "height", &height, sizeof(height));
  48. AMF_OBJECT_ITEM_VALUE(prop[14], AMF_NUMBER, "width", &width, sizeof(width));
  49. AMF_OBJECT_ITEM_VALUE(prop[15], AMF_OBJECT, "keyframes", keyframes, 2); // FLV I-index
  50. AMF_OBJECT_ITEM_VALUE(items[0], AMF_OBJECT, "onMetaData", prop, sizeof(prop) / sizeof(prop[0]));
  51. #undef AMF_OBJECT_ITEM_VALUE
  52. end = data + bytes;
  53. if (AMF_STRING != data[0] || NULL == (data = AMFReadString(data + 1, end, 0, buffer, sizeof(buffer) - 1)))
  54. {
  55. assert(0);
  56. return -1;
  57. }
  58. // filter @setDataFrame
  59. if (0 == strcmp(buffer, "@setDataFrame"))
  60. {
  61. if (AMF_STRING != data[0] || NULL == (data = AMFReadString(data + 1, end, 0, buffer, sizeof(buffer) - 1)))
  62. {
  63. assert(0);
  64. return -1;
  65. }
  66. }
  67. // onTextData/onCaption/onCaptionInfo/onCuePoint/|RtmpSampleAccess
  68. if (0 != strcmp(buffer, "onMetaData"))
  69. return 0; // skip
  70. (void)flv;
  71. return amf_read_items(data, end, items, sizeof(items) / sizeof(items[0])) ? N_ONMETADATA : -1;
  72. }